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.

210 lines
6.0 KiB

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