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