You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.5 KiB

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