bn256_fuzz.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2018 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 bn256
  18. import (
  19. "bytes"
  20. "math/big"
  21. cloudflare "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  22. google "github.com/ethereum/go-ethereum/crypto/bn256/google"
  23. )
  24. // FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries.
  25. func FuzzAdd(data []byte) int {
  26. // Ensure we have enough data in the first place
  27. if len(data) != 128 {
  28. return 0
  29. }
  30. // Ensure both libs can parse the first curve point
  31. xc := new(cloudflare.G1)
  32. _, errc := xc.Unmarshal(data[:64])
  33. xg := new(google.G1)
  34. _, errg := xg.Unmarshal(data[:64])
  35. if (errc == nil) != (errg == nil) {
  36. panic("parse mismatch")
  37. } else if errc != nil {
  38. return 0
  39. }
  40. // Ensure both libs can parse the second curve point
  41. yc := new(cloudflare.G1)
  42. _, errc = yc.Unmarshal(data[64:])
  43. yg := new(google.G1)
  44. _, errg = yg.Unmarshal(data[64:])
  45. if (errc == nil) != (errg == nil) {
  46. panic("parse mismatch")
  47. } else if errc != nil {
  48. return 0
  49. }
  50. // Add the two points and ensure they result in the same output
  51. rc := new(cloudflare.G1)
  52. rc.Add(xc, yc)
  53. rg := new(google.G1)
  54. rg.Add(xg, yg)
  55. if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
  56. panic("add mismatch")
  57. }
  58. return 0
  59. }
  60. // FuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare
  61. // libraries.
  62. func FuzzMul(data []byte) int {
  63. // Ensure we have enough data in the first place
  64. if len(data) != 96 {
  65. return 0
  66. }
  67. // Ensure both libs can parse the curve point
  68. pc := new(cloudflare.G1)
  69. _, errc := pc.Unmarshal(data[:64])
  70. pg := new(google.G1)
  71. _, errg := pg.Unmarshal(data[:64])
  72. if (errc == nil) != (errg == nil) {
  73. panic("parse mismatch")
  74. } else if errc != nil {
  75. return 0
  76. }
  77. // Add the two points and ensure they result in the same output
  78. rc := new(cloudflare.G1)
  79. rc.ScalarMult(pc, new(big.Int).SetBytes(data[64:]))
  80. rg := new(google.G1)
  81. rg.ScalarMult(pg, new(big.Int).SetBytes(data[64:]))
  82. if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
  83. panic("scalar mul mismatch")
  84. }
  85. return 0
  86. }
  87. func FuzzPair(data []byte) int {
  88. // Ensure we have enough data in the first place
  89. if len(data) != 192 {
  90. return 0
  91. }
  92. // Ensure both libs can parse the curve point
  93. pc := new(cloudflare.G1)
  94. _, errc := pc.Unmarshal(data[:64])
  95. pg := new(google.G1)
  96. _, errg := pg.Unmarshal(data[:64])
  97. if (errc == nil) != (errg == nil) {
  98. panic("parse mismatch")
  99. } else if errc != nil {
  100. return 0
  101. }
  102. // Ensure both libs can parse the twist point
  103. tc := new(cloudflare.G2)
  104. _, errc = tc.Unmarshal(data[64:])
  105. tg := new(google.G2)
  106. _, errg = tg.Unmarshal(data[64:])
  107. if (errc == nil) != (errg == nil) {
  108. panic("parse mismatch")
  109. } else if errc != nil {
  110. return 0
  111. }
  112. // Pair the two points and ensure thet result in the same output
  113. if cloudflare.PairingCheck([]*cloudflare.G1{pc}, []*cloudflare.G2{tc}) != google.PairingCheck([]*google.G1{pg}, []*google.G2{tg}) {
  114. panic("pair mismatch")
  115. }
  116. return 0
  117. }