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.

136 lines
2.5 KiB

  1. //go:build gofuzz
  2. // +build gofuzz
  3. // Copyright 2020 ConsenSys Software Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. // Code generated by consensys/gnark-crypto DO NOT EDIT
  17. package ff
  18. import (
  19. "bytes"
  20. "encoding/binary"
  21. "io"
  22. "math/big"
  23. "math/bits"
  24. )
  25. const (
  26. fuzzInteresting = 1
  27. fuzzNormal = 0
  28. fuzzDiscard = -1
  29. )
  30. // Fuzz arithmetic operations fuzzer
  31. func Fuzz(data []byte) int {
  32. r := bytes.NewReader(data)
  33. var e1, e2 Element
  34. e1.SetRawBytes(r)
  35. e2.SetRawBytes(r)
  36. {
  37. // mul assembly
  38. var c, _c Element
  39. a, _a, b, _b := e1, e1, e2, e2
  40. c.Mul(&a, &b)
  41. _mulGeneric(&_c, &_a, &_b)
  42. if !c.Equal(&_c) {
  43. panic("mul asm != mul generic on Element")
  44. }
  45. }
  46. {
  47. // inverse
  48. inv := e1
  49. inv.Inverse(&inv)
  50. var bInv, b1, b2 big.Int
  51. e1.ToBigIntRegular(&b1)
  52. bInv.ModInverse(&b1, Modulus())
  53. inv.ToBigIntRegular(&b2)
  54. if b2.Cmp(&bInv) != 0 {
  55. panic("inverse operation doesn't match big int result")
  56. }
  57. }
  58. {
  59. // a + -a == 0
  60. a, b := e1, e1
  61. b.Neg(&b)
  62. a.Add(&a, &b)
  63. if !a.IsZero() {
  64. panic("a + -a != 0")
  65. }
  66. }
  67. return fuzzNormal
  68. }
  69. // SetRawBytes reads up to Bytes (bytes needed to represent Element) from reader
  70. // and interpret it as big endian uint64
  71. // used for fuzzing purposes only
  72. func (z *Element) SetRawBytes(r io.Reader) {
  73. buf := make([]byte, 8)
  74. for i := 0; i < len(z); i++ {
  75. if _, err := io.ReadFull(r, buf); err != nil {
  76. goto eof
  77. }
  78. z[i] = binary.BigEndian.Uint64(buf[:])
  79. }
  80. eof:
  81. z[3] %= qElement[3]
  82. if z.BiggerModulus() {
  83. var b uint64
  84. z[0], b = bits.Sub64(z[0], qElement[0], 0)
  85. z[1], b = bits.Sub64(z[1], qElement[1], b)
  86. z[2], b = bits.Sub64(z[2], qElement[2], b)
  87. z[3], b = bits.Sub64(z[3], qElement[3], b)
  88. }
  89. return
  90. }
  91. func (z *Element) BiggerModulus() bool {
  92. if z[3] > qElement[3] {
  93. return true
  94. }
  95. if z[3] < qElement[3] {
  96. return false
  97. }
  98. if z[2] > qElement[2] {
  99. return true
  100. }
  101. if z[2] < qElement[2] {
  102. return false
  103. }
  104. if z[1] > qElement[1] {
  105. return true
  106. }
  107. if z[1] < qElement[1] {
  108. return false
  109. }
  110. return z[0] >= qElement[0]
  111. }