From 644e7408753fa1f3a1c59f844ce98bbbecdf99e9 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Sat, 4 Dec 2021 18:09:05 +0100 Subject: [PATCH] Split parsers file old-uncompressed - new-compressed --- parsers.go | 75 ---------------------------------------- parsers_test.go | 56 ------------------------------ uncompressed.go | 81 ++++++++++++++++++++++++++++++++++++++++++++ uncompressed_test.go | 66 ++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 131 deletions(-) create mode 100644 uncompressed.go create mode 100644 uncompressed_test.go diff --git a/parsers.go b/parsers.go index e56fe7e..c5d9d67 100644 --- a/parsers.go +++ b/parsers.go @@ -72,31 +72,6 @@ func NewPointFromBytes(b []byte) (*Point, error) { 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", - 64, len(b)) - } - p := &Point{} - p.X = new(big.Int).SetBytes(swapEndianness(b[:32])) - p.Y = new(big.Int).SetBytes(swapEndianness(b[32:])) - return p, nil -} - // MarshalJSON implements the json marshaler for the PublicKey func (pk PublicKey) MarshalJSON() ([]byte, error) { return json.Marshal(pk.Point()) @@ -131,25 +106,6 @@ 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) { @@ -243,34 +199,3 @@ func NewSignatureFromBytes(b []byte) (*Signature, error) { 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.BytesUncompressed()) - return b[:] -} - -// 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 NewSignatureFromBytesUncompressed(b []byte) (*Signature, error) { - if len(b) != 96 { //nolint:gomnd - return nil, - fmt.Errorf("Can not parse bytes to Signature,"+ - " expected byte array of length %d, current %d", - 96, len(b)) - } - s := new(big.Int).SetBytes(swapEndianness(b[:32])) - f, err := NewPointFromBytesUncompressed(b[32:96]) - if err != nil { - return nil, err - } - return &Signature{ - S: s, - F: f, - }, nil -} diff --git a/parsers_test.go b/parsers_test.go index 184113a..0ee55d2 100644 --- a/parsers_test.go +++ b/parsers_test.go @@ -108,62 +108,6 @@ func TestBytes(t *testing.T) { 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() diff --git a/uncompressed.go b/uncompressed.go new file mode 100644 index 0000000..0821485 --- /dev/null +++ b/uncompressed.go @@ -0,0 +1,81 @@ +package blindsecp256k1 + +import ( + "fmt" + "math/big" +) + +// 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", + 64, len(b)) + } + p := &Point{} + p.X = new(big.Int).SetBytes(swapEndianness(b[:32])) + p.Y = new(big.Int).SetBytes(swapEndianness(b[32:])) + return p, 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 +} + +// 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.BytesUncompressed()) + return b[:] +} + +// 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 NewSignatureFromBytesUncompressed(b []byte) (*Signature, error) { + if len(b) != 96 { //nolint:gomnd + return nil, + fmt.Errorf("Can not parse bytes to Signature,"+ + " expected byte array of length %d, current %d", + 96, len(b)) + } + s := new(big.Int).SetBytes(swapEndianness(b[:32])) + f, err := NewPointFromBytesUncompressed(b[32:96]) + if err != nil { + return nil, err + } + return &Signature{ + S: s, + F: f, + }, nil +} diff --git a/uncompressed_test.go b/uncompressed_test.go new file mode 100644 index 0000000..4baa0cf --- /dev/null +++ b/uncompressed_test.go @@ -0,0 +1,66 @@ +package blindsecp256k1 + +import ( + "encoding/hex" + "math/big" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +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) +}