@ -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
}