github.com/consensys/gnark-crypto@v0.14.0/internal/generator/fft/template/fuzz.go.tmpl (about)

     1  import (
     2  	"bytes"
     3      "fmt"
     4      "github.com/consensys/gnark-crypto/ecc"
     5      {{ template "import_fr" . }}
     6  )
     7  
     8  const (
     9  	fuzzInteresting = 1
    10  	fuzzNormal      = 0
    11  	fuzzDiscard     = -1
    12  )
    13  
    14  
    15  func Fuzz(data []byte) int {
    16      r := bytes.NewReader(data)
    17  
    18      // random polynomial
    19      size := len(data) / 8
    20      if size == 0 {
    21          return fuzzDiscard
    22      }
    23      if size > (1 << 15) {
    24          size = 1 << 15
    25      }
    26      paddedSize := ecc.NextPowerOfTwo(uint64(size))
    27      p1 := make([]fr.Element, paddedSize)
    28      p2 := make([]fr.Element, paddedSize)
    29      for i := 0; i < len(p1); i++ {
    30          p1[i].SetRawBytes(r)
    31      }
    32      copy(p2, p1)
    33  
    34      // fft domain
    35  	domainWithPrecompute := NewDomain(paddedSize)
    36  	domainWOPrecompute := NewDomain(paddedSize)
    37  
    38      // bitReverse(DIF FFT(DIT FFT (bitReverse))))==id
    39    	// bitReverse(DIF FFT(DIT FFT (bitReverse))))==id
    40  	BitReverse(p1)
    41  	domainWithPrecompute.FFT(p1, DIT, true)
    42  	domainWOPrecompute.FFTInverse(p1, DIF, true)
    43  	BitReverse(p1)
    44  
    45      for i := 0; i < len(p1); i++ {
    46          if !p1[i].Equal(&p2[i]) {
    47              panic(fmt.Sprintf("bitReverse(DIF FFT(DIT FFT (bitReverse)))) != id, size %d", size) )
    48          }
    49      }
    50  
    51      return fuzzNormal
    52  }