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.

201 lines
4.6 KiB

  1. package blindsecp256k1
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/big"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. )
  8. // swapEndianness swaps the order of the bytes in the slice.
  9. func swapEndianness(b []byte) []byte {
  10. o := make([]byte, len(b))
  11. for i := range b {
  12. o[len(b)-1-i] = b[i]
  13. }
  14. return o
  15. }
  16. // MarshalJSON implements the json marshaler for the Point
  17. func (p Point) MarshalJSON() ([]byte, error) {
  18. return json.Marshal(&struct {
  19. X string `json:"x"`
  20. Y string `json:"y"`
  21. }{
  22. X: p.X.String(),
  23. Y: p.Y.String(),
  24. })
  25. }
  26. // UnmarshalJSON implements the json unmarshaler for the Point
  27. func (p *Point) UnmarshalJSON(b []byte) error {
  28. aux := &struct {
  29. X string `json:"x"`
  30. Y string `json:"y"`
  31. }{}
  32. err := json.Unmarshal(b, &aux)
  33. if err != nil {
  34. return err
  35. }
  36. x, ok := new(big.Int).SetString(aux.X, 10)
  37. if !ok {
  38. return fmt.Errorf("Can not parse Point.X %s", aux.X)
  39. }
  40. y, ok := new(big.Int).SetString(aux.Y, 10)
  41. if !ok {
  42. return fmt.Errorf("Can not parse Point.Y %s", aux.Y)
  43. }
  44. p.X = x
  45. p.Y = y
  46. return nil
  47. }
  48. // Bytes returns the compressed Point in a little-endian byte array
  49. func (p *Point) Bytes() []byte {
  50. b := p.Compress()
  51. return b[:]
  52. }
  53. // NewPointFromBytes returns a new *Point from a given byte array with length
  54. // 64 which has encoded the point coordinates each one as 32 bytes in
  55. // little-endian.
  56. func NewPointFromBytes(b []byte) (*Point, error) {
  57. if len(b) != 33 { //nolint:gomnd
  58. return nil, fmt.Errorf("Can not parse bytes to Point,"+
  59. " expected byte array of length %d, current %d",
  60. 33, len(b))
  61. }
  62. var pBytes [33]byte
  63. copy(pBytes[:], b[:])
  64. return DecompressPoint(pBytes)
  65. }
  66. // MarshalJSON implements the json marshaler for the PublicKey
  67. func (pk PublicKey) MarshalJSON() ([]byte, error) {
  68. return json.Marshal(pk.Point())
  69. }
  70. // UnmarshalJSON implements the json unmarshaler for the PublicKey
  71. func (pk *PublicKey) UnmarshalJSON(b []byte) error {
  72. var point *Point
  73. err := json.Unmarshal(b, &point)
  74. if err != nil {
  75. return err
  76. }
  77. pk.X = point.X
  78. pk.Y = point.Y
  79. return nil
  80. }
  81. // Bytes returns the compressed PublicKey in a little-endian byte array
  82. func (pk *PublicKey) Bytes() []byte {
  83. return pk.Point().Bytes()
  84. }
  85. // NewPublicKeyFromBytes returns a new *PublicKey from a given byte array with
  86. // length 64 which has encoded the public key coordinates each one as 32 bytes
  87. // in little-endian.
  88. func NewPublicKeyFromBytes(b []byte) (*PublicKey, error) {
  89. p, err := NewPointFromBytes(b)
  90. if err != nil {
  91. return nil, err
  92. }
  93. pk := PublicKey(*p)
  94. return &pk, nil
  95. }
  96. // NewPublicKeyFromECDSA returns a *PublicKey from a serialized/marshaled array
  97. // of bytes generated by the ethereum/standard ECDSA PubKey implementation.
  98. func NewPublicKeyFromECDSA(b []byte) (*PublicKey, error) {
  99. pub, err := crypto.UnmarshalPubkey(b)
  100. if err != nil {
  101. return nil, err
  102. }
  103. pk := new(PublicKey)
  104. pk.X = pub.X
  105. pk.Y = pub.Y
  106. return pk, nil
  107. }
  108. // MarshalJSON implements the json marshaler for the Signature
  109. func (sig Signature) MarshalJSON() ([]byte, error) {
  110. return json.Marshal(&struct {
  111. S string `json:"s"`
  112. F struct {
  113. X string `json:"x"`
  114. Y string `json:"y"`
  115. } `json:"f"`
  116. }{
  117. S: sig.S.String(),
  118. F: struct {
  119. X string `json:"x"`
  120. Y string `json:"y"`
  121. }{
  122. X: sig.F.X.String(),
  123. Y: sig.F.Y.String(),
  124. },
  125. })
  126. }
  127. // UnmarshalJSON implements the json unmarshaler for the Signature
  128. func (sig *Signature) UnmarshalJSON(b []byte) error {
  129. aux := &struct {
  130. S string `json:"s"`
  131. F struct {
  132. X string `json:"x"`
  133. Y string `json:"y"`
  134. } `json:"f"`
  135. }{}
  136. err := json.Unmarshal(b, &aux)
  137. if err != nil {
  138. return err
  139. }
  140. s, ok := new(big.Int).SetString(aux.S, 10)
  141. if !ok {
  142. return fmt.Errorf("Can not parse sig.S %s", aux.S)
  143. }
  144. sig.S = s
  145. x, ok := new(big.Int).SetString(aux.F.X, 10)
  146. if !ok {
  147. return fmt.Errorf("Can not parse sig.F.X %s", aux.F.X)
  148. }
  149. y, ok := new(big.Int).SetString(aux.F.Y, 10)
  150. if !ok {
  151. return fmt.Errorf("Can not parse sig.F.Y %s", aux.F.Y)
  152. }
  153. sig.F = &Point{}
  154. sig.F.X = x
  155. sig.F.Y = y
  156. return nil
  157. }
  158. // Bytes returns the compressed Signature in a little-endian byte array
  159. func (sig *Signature) Bytes() []byte {
  160. s := sig.Compress()
  161. return s[:]
  162. }
  163. // NewSignatureFromBytes returns a new *Signature from a given byte array with
  164. // length 96 which has encoded S and the F point coordinates each one as 32
  165. // bytes in little-endian.
  166. func NewSignatureFromBytes(b []byte) (*Signature, error) {
  167. if len(b) != 65 { //nolint:gomnd
  168. return nil,
  169. fmt.Errorf("Can not parse bytes to Signature,"+
  170. " expected byte array of length %d, current %d",
  171. 65, len(b))
  172. }
  173. s := new(big.Int).SetBytes(swapEndianness(b[:32]))
  174. f, err := NewPointFromBytes(b[32:65])
  175. if err != nil {
  176. return nil, err
  177. }
  178. return &Signature{
  179. S: s,
  180. F: f,
  181. }, nil
  182. }