bn256_fuzz.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2018 Péter Szilágyi. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be found
  3. // in the LICENSE file.
  4. // +build gofuzz
  5. package bn256
  6. import (
  7. "bytes"
  8. "fmt"
  9. "io"
  10. "math/big"
  11. "github.com/consensys/gnark-crypto/ecc/bn254"
  12. cloudflare "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  13. google "github.com/ethereum/go-ethereum/crypto/bn256/google"
  14. )
  15. func getG1Points(input io.Reader) (*cloudflare.G1, *google.G1, *bn254.G1Affine) {
  16. _, xc, err := cloudflare.RandomG1(input)
  17. if err != nil {
  18. // insufficient input
  19. return nil, nil, nil
  20. }
  21. xg := new(google.G1)
  22. if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
  23. panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
  24. }
  25. xs := new(bn254.G1Affine)
  26. if err := xs.Unmarshal(xc.Marshal()); err != nil {
  27. panic(fmt.Sprintf("Could not marshal cloudflare -> gnark: %v", err))
  28. }
  29. return xc, xg, xs
  30. }
  31. func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2, *bn254.G2Affine) {
  32. _, xc, err := cloudflare.RandomG2(input)
  33. if err != nil {
  34. // insufficient input
  35. return nil, nil, nil
  36. }
  37. xg := new(google.G2)
  38. if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
  39. panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
  40. }
  41. xs := new(bn254.G2Affine)
  42. if err := xs.Unmarshal(xc.Marshal()); err != nil {
  43. panic(fmt.Sprintf("Could not marshal cloudflare -> gnark: %v", err))
  44. }
  45. return xc, xg, xs
  46. }
  47. // FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries.
  48. func FuzzAdd(data []byte) int {
  49. input := bytes.NewReader(data)
  50. xc, xg, xs := getG1Points(input)
  51. if xc == nil {
  52. return 0
  53. }
  54. yc, yg, ys := getG1Points(input)
  55. if yc == nil {
  56. return 0
  57. }
  58. // Ensure both libs can parse the second curve point
  59. // Add the two points and ensure they result in the same output
  60. rc := new(cloudflare.G1)
  61. rc.Add(xc, yc)
  62. rg := new(google.G1)
  63. rg.Add(xg, yg)
  64. tmpX := new(bn254.G1Jac).FromAffine(xs)
  65. tmpY := new(bn254.G1Jac).FromAffine(ys)
  66. rs := new(bn254.G1Affine).FromJacobian(tmpX.AddAssign(tmpY))
  67. if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
  68. panic("add mismatch: cloudflare/google")
  69. }
  70. if !bytes.Equal(rc.Marshal(), rs.Marshal()) {
  71. panic("add mismatch: cloudflare/gnark")
  72. }
  73. return 1
  74. }
  75. // FuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare
  76. // libraries.
  77. func FuzzMul(data []byte) int {
  78. input := bytes.NewReader(data)
  79. pc, pg, ps := getG1Points(input)
  80. if pc == nil {
  81. return 0
  82. }
  83. // Add the two points and ensure they result in the same output
  84. remaining := input.Len()
  85. if remaining == 0 {
  86. return 0
  87. }
  88. if remaining > 128 {
  89. // The evm only ever uses 32 byte integers, we need to cap this otherwise
  90. // we run into slow exec. A 236Kb byte integer cause oss-fuzz to report it as slow.
  91. // 128 bytes should be fine though
  92. return 0
  93. }
  94. buf := make([]byte, remaining)
  95. input.Read(buf)
  96. rc := new(cloudflare.G1)
  97. rc.ScalarMult(pc, new(big.Int).SetBytes(buf))
  98. rg := new(google.G1)
  99. rg.ScalarMult(pg, new(big.Int).SetBytes(buf))
  100. rs := new(bn254.G1Jac)
  101. psJac := new(bn254.G1Jac).FromAffine(ps)
  102. rs.ScalarMultiplication(psJac, new(big.Int).SetBytes(buf))
  103. rsAffine := new(bn254.G1Affine).FromJacobian(rs)
  104. if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
  105. panic("scalar mul mismatch: cloudflare/google")
  106. }
  107. if !bytes.Equal(rc.Marshal(), rsAffine.Marshal()) {
  108. panic("scalar mul mismatch: cloudflare/gnark")
  109. }
  110. return 1
  111. }
  112. func FuzzPair(data []byte) int {
  113. input := bytes.NewReader(data)
  114. pc, pg, ps := getG1Points(input)
  115. if pc == nil {
  116. return 0
  117. }
  118. tc, tg, ts := getG2Points(input)
  119. if tc == nil {
  120. return 0
  121. }
  122. // Pair the two points and ensure they result in the same output
  123. clPair := cloudflare.Pair(pc, tc).Marshal()
  124. gPair := google.Pair(pg, tg).Marshal()
  125. if !bytes.Equal(clPair, gPair) {
  126. panic("pairing mismatch: cloudflare/google")
  127. }
  128. cPair, err := bn254.Pair([]bn254.G1Affine{*ps}, []bn254.G2Affine{*ts})
  129. if err != nil {
  130. panic(fmt.Sprintf("gnark/bn254 encountered error: %v", err))
  131. }
  132. if !bytes.Equal(clPair, cPair.Marshal()) {
  133. panic("pairing mismatch: cloudflare/gnark")
  134. }
  135. return 1
  136. }