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.

209 lines
5.8 KiB

  1. package babyjub
  2. import (
  3. "crypto/rand"
  4. "github.com/iden3/go-iden3-crypto/mimc7"
  5. "math/big"
  6. )
  7. // pruneBuffer prunes the buffer during key generation according to RFC 8032.
  8. // https://tools.ietf.org/html/rfc8032#page-13
  9. func pruneBuffer(buf *[32]byte) *[32]byte {
  10. buf[0] = buf[0] & 0xF8
  11. buf[31] = buf[31] & 0x7F
  12. buf[31] = buf[31] | 0x40
  13. return buf
  14. }
  15. // PrivateKey is an EdDSA private key, which is a 32byte buffer.
  16. type PrivateKey [32]byte
  17. // NewRandPrivKey generates a new random private key (using cryptographically
  18. // secure randomness).
  19. func NewRandPrivKey() PrivateKey {
  20. var k PrivateKey
  21. _, err := rand.Read(k[:])
  22. if err != nil {
  23. panic(err)
  24. }
  25. return k
  26. }
  27. // Scalar converts a private key into the scalar value s following the EdDSA
  28. // standard, and using blake-512 hash.
  29. func (k *PrivateKey) Scalar() *PrivKeyScalar {
  30. sBuf := Blake512(k[:])
  31. sBuf32 := [32]byte{}
  32. copy(sBuf32[:], sBuf[:32])
  33. pruneBuffer(&sBuf32)
  34. s := new(big.Int)
  35. SetBigIntFromLEBytes(s, sBuf32[:])
  36. s.Rsh(s, 3)
  37. return NewPrivKeyScalar(s)
  38. }
  39. // Pub returns the public key corresponding to a private key.
  40. func (k *PrivateKey) Public() *PublicKey {
  41. return k.Scalar().Public()
  42. }
  43. // PrivKeyScalar represents the scalar s output of a private key
  44. type PrivKeyScalar big.Int
  45. // NewPrivKeyScalar creates a new PrivKeyScalar from a big.Int
  46. func NewPrivKeyScalar(s *big.Int) *PrivKeyScalar {
  47. sk := PrivKeyScalar(*s)
  48. return &sk
  49. }
  50. // Pub returns the public key corresponding to the scalar value s of a private
  51. // key.
  52. func (s *PrivKeyScalar) Public() *PublicKey {
  53. p := NewPoint().Mul((*big.Int)(s), B8)
  54. pk := PublicKey(*p)
  55. return &pk
  56. }
  57. // BigInt returns the big.Int corresponding to a PrivKeyScalar.
  58. func (s *PrivKeyScalar) BigInt() *big.Int {
  59. return (*big.Int)(s)
  60. }
  61. // PublicKey represents an EdDSA public key, which is a curve point.
  62. type PublicKey Point
  63. func (pk PublicKey) MarshalText() ([]byte, error) {
  64. pkc := pk.Compress()
  65. return Hex(pkc[:]).MarshalText()
  66. }
  67. func (pk PublicKey) String() string {
  68. pkc := pk.Compress()
  69. return Hex(pkc[:]).String()
  70. }
  71. func (pk *PublicKey) UnmarshalText(h []byte) error {
  72. var pkc PublicKeyComp
  73. if err := HexDecodeInto(pkc[:], h); err != nil {
  74. return err
  75. }
  76. pkd, err := pkc.Decompress()
  77. if err != nil {
  78. return err
  79. }
  80. *pk = *pkd
  81. return nil
  82. }
  83. // Point returns the Point corresponding to a PublicKey.
  84. func (p *PublicKey) Point() *Point {
  85. return (*Point)(p)
  86. }
  87. // PublicKeyComp represents a compressed EdDSA Public key; it's a compressed curve
  88. // point.
  89. type PublicKeyComp [32]byte
  90. func (buf PublicKeyComp) MarshalText() ([]byte, error) { return Hex(buf[:]).MarshalText() }
  91. func (buf PublicKeyComp) String() string { return Hex(buf[:]).String() }
  92. func (buf *PublicKeyComp) UnmarshalText(h []byte) error { return HexDecodeInto(buf[:], h) }
  93. func (p *PublicKey) Compress() PublicKeyComp {
  94. return PublicKeyComp((*Point)(p).Compress())
  95. }
  96. func (p *PublicKeyComp) Decompress() (*PublicKey, error) {
  97. point, err := NewPoint().Decompress(*p)
  98. if err != nil {
  99. return nil, err
  100. }
  101. pk := PublicKey(*point)
  102. return &pk, nil
  103. }
  104. // Signature represents an EdDSA uncompressed signature.
  105. type Signature struct {
  106. R8 *Point
  107. S *big.Int
  108. }
  109. // SignatureComp represents a compressed EdDSA signature.
  110. type SignatureComp [64]byte
  111. func (buf SignatureComp) MarshalText() ([]byte, error) { return Hex(buf[:]).MarshalText() }
  112. func (buf SignatureComp) String() string { return Hex(buf[:]).String() }
  113. func (buf *SignatureComp) UnmarshalText(h []byte) error { return HexDecodeInto(buf[:], h) }
  114. // Compress an EdDSA signature by concatenating the compression of
  115. // the point R8 and the Little-Endian encoding of S.
  116. func (s *Signature) Compress() SignatureComp {
  117. R8p := s.R8.Compress()
  118. Sp := BigIntLEBytes(s.S)
  119. buf := [64]byte{}
  120. copy(buf[:32], R8p[:])
  121. copy(buf[32:], Sp[:])
  122. return SignatureComp(buf)
  123. }
  124. // Decompress a compressed signature into s, and also returns the decompressed
  125. // signature. Returns error if the Point decompression fails.
  126. func (s *Signature) Decompress(buf [64]byte) (*Signature, error) {
  127. R8p := [32]byte{}
  128. copy(R8p[:], buf[:32])
  129. var err error
  130. if s.R8, err = NewPoint().Decompress(R8p); err != nil {
  131. return nil, err
  132. }
  133. s.S = SetBigIntFromLEBytes(new(big.Int), buf[32:])
  134. return s, nil
  135. }
  136. // Decompress a compressed signature. Returns error if the Point decompression
  137. // fails.
  138. func (s *SignatureComp) Decompress() (*Signature, error) {
  139. return new(Signature).Decompress(*s)
  140. }
  141. // SignMimc7 signs a message encoded as a big.Int in Zq using blake-512 hash
  142. // for buffer hashing and mimc7 for big.Int hashing.
  143. func (k *PrivateKey) SignMimc7(msg *big.Int) *Signature {
  144. h1 := Blake512(k[:])
  145. msgBuf := BigIntLEBytes(msg)
  146. msgBuf32 := [32]byte{}
  147. copy(msgBuf32[:], msgBuf[:])
  148. rBuf := Blake512(append(h1[32:], msgBuf32[:]...))
  149. r := SetBigIntFromLEBytes(new(big.Int), rBuf) // r = H(H_{32..63}(k), msg)
  150. r.Mod(r, SubOrder)
  151. R8 := NewPoint().Mul(r, B8) // R8 = r * 8 * B
  152. A := k.Public().Point()
  153. hmInput, err := mimc7.BigIntsToRElems([]*big.Int{R8.X, R8.Y, A.X, A.Y, msg})
  154. if err != nil {
  155. panic(err)
  156. }
  157. hm := mimc7.Hash(hmInput, nil) // hm = H1(8*R.x, 8*R.y, A.x, A.y, msg)
  158. S := new(big.Int).Lsh(k.Scalar().BigInt(), 3)
  159. S = S.Mul(hm, S)
  160. S.Add(r, S)
  161. S.Mod(S, SubOrder) // S = r + hm * 8 * s
  162. return &Signature{R8: R8, S: S}
  163. }
  164. // VerifyMimc7 verifies the signature of a message encoded as a big.Int in Zq
  165. // using blake-512 hash for buffer hashing and mimc7 for big.Int hashing.
  166. func (p *PublicKey) VerifyMimc7(msg *big.Int, sig *Signature) bool {
  167. hmInput, err := mimc7.BigIntsToRElems([]*big.Int{sig.R8.X, sig.R8.Y, p.X, p.Y, msg})
  168. if err != nil {
  169. panic(err)
  170. }
  171. hm := mimc7.Hash(hmInput, nil) // hm = H1(8*R.x, 8*R.y, A.x, A.y, msg)
  172. left := NewPoint().Mul(sig.S, B8) // left = s * 8 * B
  173. r1 := big.NewInt(8)
  174. r1.Mul(r1, hm)
  175. right := NewPoint().Mul(r1, p.Point())
  176. right.Add(sig.R8, right) // right = 8 * R + 8 * hm * A
  177. return (left.X.Cmp(right.X) == 0) && (left.Y.Cmp(right.Y) == 0)
  178. }