arnaucube
arnaucube/go-blindsecp256k1Public Self-hosted repository
go-blindsecp256k1/uncompressed.go
uncompressed.goRaw
 1package blindsecp256k1
 2
 3import (
 4	"fmt"
 5	"math/big"
 6)
 7
 8// BytesUncompressed returns a byte array of length 64, with the X & Y
 9// coordinates of the Point encoded in little-endian.  [ X (32 bytes) | Y (32
10// bytes)]
11func (p *Point) BytesUncompressed() []byte {
12	var b [64]byte
13	copy(b[:32], swapEndianness(p.X.Bytes()))
14	copy(b[32:], swapEndianness(p.Y.Bytes()))
15	return b[:]
16}
17
18// NewPointFromBytesUncompressed returns a new *Point from a given byte array
19// with length 64 which has encoded the point coordinates each one as 32 bytes
20// in little-endian.
21func NewPointFromBytesUncompressed(b []byte) (*Point, error) {
22	if len(b) != 64 { //nolint:gomnd
23		return nil, fmt.Errorf("Can not parse bytes to Point,"+
24			" expected byte array of length %d, current %d",
25			64, len(b))
26	}
27	p := &Point{}
28	p.X = new(big.Int).SetBytes(swapEndianness(b[:32]))
29	p.Y = new(big.Int).SetBytes(swapEndianness(b[32:]))
30	return p, nil
31}
32
33// BytesUncompressed returns a byte array of length 64, with the X & Y
34// coordinates of the PublicKey encoded in little-endian.
35// [ X (32 bytes) | Y (32 bytes)]
36func (pk *PublicKey) BytesUncompressed() []byte {
37	return pk.Point().BytesUncompressed()
38}
39
40// NewPublicKeyFromBytesUncompressed returns a new *PublicKey from a given byte array with
41// length 64 which has encoded the public key coordinates each one as 32 bytes
42// in little-endian.
43func NewPublicKeyFromBytesUncompressed(b []byte) (*PublicKey, error) {
44	p, err := NewPointFromBytesUncompressed(b)
45	if err != nil {
46		return nil, err
47	}
48	pk := PublicKey(*p)
49	return &pk, nil
50}
51
52// BytesUncompressed returns a byte array of length 96, with the S, F.X and F.Y
53// coordinates of the Signature encoded in little-endian.
54// [ S (32 bytes | F.X (32 bytes) | F.Y (32 bytes)]
55func (sig *Signature) BytesUncompressed() []byte {
56	var b [96]byte
57	copy(b[:32], swapEndianness(sig.S.Bytes()))
58	copy(b[32:96], sig.F.BytesUncompressed())
59	return b[:]
60}
61
62// NewSignatureFromBytesUncompressed returns a new *Signature from a given byte array with
63// length 96 which has encoded S and the F point coordinates each one as 32
64// bytes in little-endian.
65func NewSignatureFromBytesUncompressed(b []byte) (*Signature, error) {
66	if len(b) != 96 { //nolint:gomnd
67		return nil,
68			fmt.Errorf("Can not parse bytes to Signature,"+
69				" expected byte array of length %d, current %d",
70				96, len(b))
71	}
72	s := new(big.Int).SetBytes(swapEndianness(b[:32]))
73	f, err := NewPointFromBytesUncompressed(b[32:96])
74	if err != nil {
75		return nil, err
76	}
77	return &Signature{
78		S: s,
79		F: f,
80	}, nil
81}