Browse Source

Update Bytes method to use Compress

Update Bytes method to use .Compress() internally. For Point, PublicKey,
Signature.
Keep old uncompressed .Bytes() methods for backwards compatibility.
master
arnaucube 2 years ago
parent
commit
04b9532b53
3 changed files with 146 additions and 25 deletions
  1. +3
    -2
      blindsecp256k1.go
  2. +81
    -14
      parsers.go
  3. +62
    -9
      parsers_test.go

+ 3
- 2
blindsecp256k1.go

@ -77,7 +77,8 @@ func (p *Point) isValid() error {
return nil
}
// Compress packs a Point to a byte array of 33 bytes, encoded in little-endian.
// Compress packs a Point to a byte array of 33 bytes, encoded in
// little-endian.
func (p *Point) Compress() [33]byte {
xBytes := p.X.Bytes()
odd := byte(0)
@ -134,7 +135,7 @@ func DecompressPoint(b [33]byte) (*Point, error) {
}
p := &Point{X: x, Y: y}
return p, nil
return p, p.isValid()
}
// WIP

+ 81
- 14
parsers.go

@ -51,12 +51,9 @@ func (p *Point) UnmarshalJSON(b []byte) error {
return nil
}
// Bytes returns a byte array of length 64, with the X & Y coordinates of the
// Point encoded in little-endian. [ X (32 bytes) | Y (32 bytes)]
// Bytes returns the compressed Point in a little-endian byte array
func (p *Point) Bytes() []byte {
var b [64]byte
copy(b[:32], swapEndianness(p.X.Bytes()))
copy(b[32:], swapEndianness(p.Y.Bytes()))
b := p.Compress()
return b[:]
}
@ -64,6 +61,31 @@ func (p *Point) Bytes() []byte {
// 64 which has encoded the point coordinates each one as 32 bytes in
// little-endian.
func NewPointFromBytes(b []byte) (*Point, error) {
if len(b) != 33 { //nolint:gomnd
return nil, fmt.Errorf("Can not parse bytes to Point,"+
" expected byte array of length %d, current %d",
33, len(b))
}
var pBytes [33]byte
copy(pBytes[:], b[:])
return DecompressPoint(pBytes)
}
// BytesUncompressed returns a byte array of length 64, with the X & Y
// coordinates of the Point encoded in little-endian. [ X (32 bytes) | Y (32
// bytes)]
func (p *Point) BytesUncompressed() []byte {
var b [64]byte
copy(b[:32], swapEndianness(p.X.Bytes()))
copy(b[32:], swapEndianness(p.Y.Bytes()))
return b[:]
}
// NewPointFromBytesUncompressed returns a new *Point from a given byte array
// with length 64 which has encoded the point coordinates each one as 32 bytes
// in little-endian.
func NewPointFromBytesUncompressed(b []byte) (*Point, error) {
if len(b) != 64 { //nolint:gomnd
return nil, fmt.Errorf("Can not parse bytes to Point,"+
" expected byte array of length %d, current %d",
@ -92,8 +114,7 @@ func (pk *PublicKey) UnmarshalJSON(b []byte) error {
return nil
}
// Bytes returns a byte array of length 64, with the X & Y coordinates of the
// PublicKey encoded in little-endian. [ X (32 bytes) | Y (32 bytes)]
// Bytes returns the compressed PublicKey in a little-endian byte array
func (pk *PublicKey) Bytes() []byte {
return pk.Point().Bytes()
}
@ -110,6 +131,25 @@ func NewPublicKeyFromBytes(b []byte) (*PublicKey, error) {
return &pk, nil
}
// BytesUncompressed returns a byte array of length 64, with the X & Y
// coordinates of the PublicKey encoded in little-endian.
// [ X (32 bytes) | Y (32 bytes)]
func (pk *PublicKey) BytesUncompressed() []byte {
return pk.Point().BytesUncompressed()
}
// NewPublicKeyFromBytesUncompressed returns a new *PublicKey from a given byte array with
// length 64 which has encoded the public key coordinates each one as 32 bytes
// in little-endian.
func NewPublicKeyFromBytesUncompressed(b []byte) (*PublicKey, error) {
p, err := NewPointFromBytesUncompressed(b)
if err != nil {
return nil, err
}
pk := PublicKey(*p)
return &pk, nil
}
// NewPublicKeyFromECDSA returns a *PublicKey from a serialized/marshaled array
// of bytes generated by the ethereum/standard ECDSA PubKey implementation.
func NewPublicKeyFromECDSA(b []byte) (*PublicKey, error) {
@ -177,20 +217,47 @@ func (sig *Signature) UnmarshalJSON(b []byte) error {
return nil
}
// Bytes returns a byte array of length 96, with the S, F.X and F.Y coordinates
// of the Signature encoded in little-endian.
// [ S (32 bytes | F.X (32 bytes) | F.Y (32 bytes)]
// Bytes returns the compressed Signature in a little-endian byte array
func (sig *Signature) Bytes() []byte {
s := sig.Compress()
return s[:]
}
// NewSignatureFromBytes returns a new *Signature from a given byte array with
// length 96 which has encoded S and the F point coordinates each one as 32
// bytes in little-endian.
func NewSignatureFromBytes(b []byte) (*Signature, error) {
if len(b) != 65 { //nolint:gomnd
return nil,
fmt.Errorf("Can not parse bytes to Signature,"+
" expected byte array of length %d, current %d",
65, len(b))
}
s := new(big.Int).SetBytes(swapEndianness(b[:32]))
f, err := NewPointFromBytes(b[32:65])
if err != nil {
return nil, err
}
return &Signature{
S: s,
F: f,
}, nil
}
// BytesUncompressed returns a byte array of length 96, with the S, F.X and F.Y
// coordinates of the Signature encoded in little-endian.
// [ S (32 bytes | F.X (32 bytes) | F.Y (32 bytes)]
func (sig *Signature) BytesUncompressed() []byte {
var b [96]byte
copy(b[:32], swapEndianness(sig.S.Bytes()))
copy(b[32:96], sig.F.Bytes())
copy(b[32:96], sig.F.BytesUncompressed())
return b[:]
}
// NewSignatureFromBytes returns a new *Signature from a given byte array with
// NewSignatureFromBytesUncompressed returns a new *Signature from a given byte array with
// length 96 which has encoded S and the F point coordinates each one as 32
// bytes in little-endian.
func NewSignatureFromBytes(b []byte) (*Signature, error) {
func NewSignatureFromBytesUncompressed(b []byte) (*Signature, error) {
if len(b) != 96 { //nolint:gomnd
return nil,
fmt.Errorf("Can not parse bytes to Signature,"+
@ -198,7 +265,7 @@ func NewSignatureFromBytes(b []byte) (*Signature, error) {
96, len(b))
}
s := new(big.Int).SetBytes(swapEndianness(b[:32]))
f, err := NewPointFromBytes(b[32:96])
f, err := NewPointFromBytesUncompressed(b[32:96])
if err != nil {
return nil, err
}

+ 62
- 9
parsers_test.go

@ -57,19 +57,16 @@ func TestMarshalers(t *testing.T) {
func TestBytes(t *testing.T) {
// Point
p := &Point{
X: big.NewInt(3),
Y: big.NewInt(3),
}
p := G.Mul(big.NewInt(3))
b := p.Bytes()
assert.Equal(t, "03000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(b)) //nolint:lll
assert.Equal(t, "f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f900", hex.EncodeToString(b)) //nolint:lll
p2, err := NewPointFromBytes(b)
assert.Nil(t, err)
assert.Equal(t, p, p2)
p = G.Mul(big.NewInt(1234))
b = p.Bytes()
assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
assert.Equal(t, "e37648435c60dcd181b3d41d50857ba5b5abebe279429aa76558f6653f1658f200", hex.EncodeToString(b)) //nolint:lll
p2, err = NewPointFromBytes(b)
assert.Nil(t, err)
assert.Equal(t, p, p2)
@ -77,7 +74,7 @@ func TestBytes(t *testing.T) {
// PublicKey
pk := PublicKey(*p)
b = pk.Bytes()
assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
assert.Equal(t, "e37648435c60dcd181b3d41d50857ba5b5abebe279429aa76558f6653f1658f200", hex.EncodeToString(b)) //nolint:lll
pk2, err := NewPublicKeyFromBytes(b)
assert.Nil(t, err)
assert.Equal(t, &pk, pk2)
@ -88,7 +85,7 @@ func TestBytes(t *testing.T) {
F: p,
}
b = sig.Bytes()
assert.Equal(t, "9426000000000000000000000000000000000000000000000000000000000000f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
assert.Equal(t, "9426000000000000000000000000000000000000000000000000000000000000e37648435c60dcd181b3d41d50857ba5b5abebe279429aa76558f6653f1658f200", hex.EncodeToString(b)) //nolint:lll
sig2, err := NewSignatureFromBytes(b)
assert.Nil(t, err)
assert.Equal(t, &sig, sig2)
@ -105,12 +102,68 @@ func TestBytes(t *testing.T) {
F: &Point{X: x, Y: y},
}
b = sig.Bytes()
assert.Equal(t, "d7a75050259cc06415f19bde5460a58325e3050806ba949d9ac9728b71b9b6600457ba001981781ed31acafed3d1e82c2ad53d08e3f293eab2f199ed0193367c98311f1894598c91f10fe415ba4a6d04e1351d07430631c7decdbbdb2615e68a", hex.EncodeToString(b)) //nolint:lll
assert.Equal(t, "d7a75050259cc06415f19bde5460a58325e3050806ba949d9ac9728b71b9b6607c369301ed99f1b2ea93f2e3083dd52a2ce8d1d3feca1ad31e78811900ba570400", hex.EncodeToString(b)) //nolint:lll
sig2, err = NewSignatureFromBytes(b)
assert.Nil(t, err)
assert.Equal(t, &sig, sig2)
}
func TestBytesUncompressed(t *testing.T) {
// Point
p := &Point{
X: big.NewInt(3),
Y: big.NewInt(3),
}
b := p.BytesUncompressed()
assert.Equal(t, "03000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(b)) //nolint:lll
p2, err := NewPointFromBytesUncompressed(b)
assert.Nil(t, err)
assert.Equal(t, p, p2)
p = G.Mul(big.NewInt(1234))
b = p.BytesUncompressed()
assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
p2, err = NewPointFromBytesUncompressed(b)
assert.Nil(t, err)
assert.Equal(t, p, p2)
// PublicKey
pk := PublicKey(*p)
b = pk.BytesUncompressed()
assert.Equal(t, "f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
pk2, err := NewPublicKeyFromBytesUncompressed(b)
assert.Nil(t, err)
assert.Equal(t, &pk, pk2)
// Signature
sig := Signature{
S: big.NewInt(9876),
F: p,
}
b = sig.BytesUncompressed()
assert.Equal(t, "9426000000000000000000000000000000000000000000000000000000000000f258163f65f65865a79a4279e2ebabb5a57b85501dd4b381d1dc605c434876e34c308bd3f18f062d5cc07f34948ced82f9a76f9c3e65ae64f158412da8e92e6d", hex.EncodeToString(b)) //nolint:lll
sig2, err := NewSignatureFromBytesUncompressed(b)
assert.Nil(t, err)
assert.Equal(t, &sig, sig2)
// Signature with bigger values
s, ok := new(big.Int).SetString("43744879514016998261043792362491545206150700367692876136431010903034023684055", 10) //nolint:lll
require.True(t, ok)
x, ok := new(big.Int).SetString("56183217574518331862027285308947626162625485037257226169003339923450551228164", 10) //nolint:lll
require.True(t, ok)
y, ok := new(big.Int).SetString("62825693913681695979055350889339417157462875026935818721506450621762231021976", 10) //nolint:lll
require.True(t, ok)
sig = Signature{
S: s,
F: &Point{X: x, Y: y},
}
b = sig.BytesUncompressed()
assert.Equal(t, "d7a75050259cc06415f19bde5460a58325e3050806ba949d9ac9728b71b9b6600457ba001981781ed31acafed3d1e82c2ad53d08e3f293eab2f199ed0193367c98311f1894598c91f10fe415ba4a6d04e1351d07430631c7decdbbdb2615e68a", hex.EncodeToString(b)) //nolint:lll
sig2, err = NewSignatureFromBytesUncompressed(b)
assert.Nil(t, err)
assert.Equal(t, &sig, sig2)
}
func TestImportECDSApubKey(t *testing.T) {
// Generate an ECDSA key
k, err := crypto.GenerateKey()

Loading…
Cancel
Save