bls12381_fuzz.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2021 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // +build gofuzz
  17. package bls
  18. import (
  19. "bytes"
  20. "crypto/rand"
  21. "fmt"
  22. "io"
  23. "math/big"
  24. gnark "github.com/consensys/gnark-crypto/ecc/bls12-381"
  25. "github.com/consensys/gnark-crypto/ecc/bls12-381/fp"
  26. "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
  27. "github.com/ethereum/go-ethereum/crypto/bls12381"
  28. )
  29. func FuzzCrossPairing(data []byte) int {
  30. input := bytes.NewReader(data)
  31. // get random G1 points
  32. kpG1, cpG1, err := getG1Points(input)
  33. if err != nil {
  34. return 0
  35. }
  36. // get random G2 points
  37. kpG2, cpG2, err := getG2Points(input)
  38. if err != nil {
  39. return 0
  40. }
  41. // compute pairing using geth
  42. engine := bls12381.NewPairingEngine()
  43. engine.AddPair(kpG1, kpG2)
  44. kResult := engine.Result()
  45. // compute pairing using gnark
  46. cResult, err := gnark.Pair([]gnark.G1Affine{*cpG1}, []gnark.G2Affine{*cpG2})
  47. if err != nil {
  48. panic(fmt.Sprintf("gnark/bls12381 encountered error: %v", err))
  49. }
  50. // compare result
  51. if !(bytes.Equal(cResult.Marshal(), bls12381.NewGT().ToBytes(kResult))) {
  52. panic("pairing mismatch gnark / geth ")
  53. }
  54. return 1
  55. }
  56. func FuzzCrossG1Add(data []byte) int {
  57. input := bytes.NewReader(data)
  58. // get random G1 points
  59. kp1, cp1, err := getG1Points(input)
  60. if err != nil {
  61. return 0
  62. }
  63. // get random G1 points
  64. kp2, cp2, err := getG1Points(input)
  65. if err != nil {
  66. return 0
  67. }
  68. // compute kp = kp1 + kp2
  69. g1 := bls12381.NewG1()
  70. kp := bls12381.PointG1{}
  71. g1.Add(&kp, kp1, kp2)
  72. // compute cp = cp1 + cp2
  73. _cp1 := new(gnark.G1Jac).FromAffine(cp1)
  74. _cp2 := new(gnark.G1Jac).FromAffine(cp2)
  75. cp := new(gnark.G1Affine).FromJacobian(_cp1.AddAssign(_cp2))
  76. // compare result
  77. if !(bytes.Equal(cp.Marshal(), g1.ToBytes(&kp))) {
  78. panic("G1 point addition mismatch gnark / geth ")
  79. }
  80. return 1
  81. }
  82. func FuzzCrossG2Add(data []byte) int {
  83. input := bytes.NewReader(data)
  84. // get random G2 points
  85. kp1, cp1, err := getG2Points(input)
  86. if err != nil {
  87. return 0
  88. }
  89. // get random G2 points
  90. kp2, cp2, err := getG2Points(input)
  91. if err != nil {
  92. return 0
  93. }
  94. // compute kp = kp1 + kp2
  95. g2 := bls12381.NewG2()
  96. kp := bls12381.PointG2{}
  97. g2.Add(&kp, kp1, kp2)
  98. // compute cp = cp1 + cp2
  99. _cp1 := new(gnark.G2Jac).FromAffine(cp1)
  100. _cp2 := new(gnark.G2Jac).FromAffine(cp2)
  101. cp := new(gnark.G2Affine).FromJacobian(_cp1.AddAssign(_cp2))
  102. // compare result
  103. if !(bytes.Equal(cp.Marshal(), g2.ToBytes(&kp))) {
  104. panic("G2 point addition mismatch gnark / geth ")
  105. }
  106. return 1
  107. }
  108. func FuzzCrossG1MultiExp(data []byte) int {
  109. var (
  110. input = bytes.NewReader(data)
  111. gethScalars []*big.Int
  112. gnarkScalars []fr.Element
  113. gethPoints []*bls12381.PointG1
  114. gnarkPoints []gnark.G1Affine
  115. )
  116. // n random scalars (max 17)
  117. for i := 0; i < 17; i++ {
  118. // note that geth/crypto/bls12381 works only with scalars <= 32bytes
  119. s, err := randomScalar(input, fr.Modulus())
  120. if err != nil {
  121. break
  122. }
  123. // get a random G1 point as basis
  124. kp1, cp1, err := getG1Points(input)
  125. if err != nil {
  126. break
  127. }
  128. gethScalars = append(gethScalars, s)
  129. var gnarkScalar = &fr.Element{}
  130. gnarkScalar = gnarkScalar.SetBigInt(s).FromMont()
  131. gnarkScalars = append(gnarkScalars, *gnarkScalar)
  132. gethPoints = append(gethPoints, new(bls12381.PointG1).Set(kp1))
  133. gnarkPoints = append(gnarkPoints, *cp1)
  134. }
  135. if len(gethScalars) == 0{
  136. return 0
  137. }
  138. // compute multi exponentiation
  139. g1 := bls12381.NewG1()
  140. kp := bls12381.PointG1{}
  141. if _, err := g1.MultiExp(&kp, gethPoints, gethScalars); err != nil {
  142. panic(fmt.Sprintf("G1 multi exponentiation errored (geth): %v", err))
  143. }
  144. // note that geth/crypto/bls12381.MultiExp mutates the scalars slice (and sets all the scalars to zero)
  145. // gnark multi exp
  146. cp := new(gnark.G1Affine)
  147. cp.MultiExp(gnarkPoints, gnarkScalars)
  148. // compare result
  149. if !(bytes.Equal(cp.Marshal(), g1.ToBytes(&kp))) {
  150. panic("G1 multi exponentiation mismatch gnark / geth ")
  151. }
  152. return 1
  153. }
  154. func getG1Points(input io.Reader) (*bls12381.PointG1, *gnark.G1Affine, error) {
  155. // sample a random scalar
  156. s, err := randomScalar(input, fp.Modulus())
  157. if err != nil {
  158. return nil, nil, err
  159. }
  160. // compute a random point
  161. cp := new(gnark.G1Affine)
  162. _, _, g1Gen, _ := gnark.Generators()
  163. cp.ScalarMultiplication(&g1Gen, s)
  164. cpBytes := cp.Marshal()
  165. // marshal gnark point -> geth point
  166. g1 := bls12381.NewG1()
  167. kp, err := g1.FromBytes(cpBytes)
  168. if err != nil {
  169. panic(fmt.Sprintf("Could not marshal gnark.G1 -> geth.G1: %v", err))
  170. }
  171. if !bytes.Equal(g1.ToBytes(kp), cpBytes) {
  172. panic("bytes(gnark.G1) != bytes(geth.G1)")
  173. }
  174. return kp, cp, nil
  175. }
  176. func getG2Points(input io.Reader) (*bls12381.PointG2, *gnark.G2Affine, error) {
  177. // sample a random scalar
  178. s, err := randomScalar(input, fp.Modulus())
  179. if err != nil {
  180. return nil, nil, err
  181. }
  182. // compute a random point
  183. cp := new(gnark.G2Affine)
  184. _, _, _, g2Gen := gnark.Generators()
  185. cp.ScalarMultiplication(&g2Gen, s)
  186. cpBytes := cp.Marshal()
  187. // marshal gnark point -> geth point
  188. g2 := bls12381.NewG2()
  189. kp, err := g2.FromBytes(cpBytes)
  190. if err != nil {
  191. panic(fmt.Sprintf("Could not marshal gnark.G2 -> geth.G2: %v", err))
  192. }
  193. if !bytes.Equal(g2.ToBytes(kp), cpBytes) {
  194. panic("bytes(gnark.G2) != bytes(geth.G2)")
  195. }
  196. return kp, cp, nil
  197. }
  198. func randomScalar(r io.Reader, max *big.Int) (k *big.Int, err error) {
  199. for {
  200. k, err = rand.Int(r, max)
  201. if err != nil || k.Sign() > 0 {
  202. return
  203. }
  204. }
  205. }