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

     1  import (
     2  	"bytes"
     3      "github.com/consensys/gnark-crypto/ecc/{{ .Name }}/fr"
     4      "github.com/consensys/gnark-crypto/ecc/{{ .Name }}/fr/polynomial"
     5  )
     6  
     7  const (
     8  	fuzzInteresting = 1
     9  	fuzzNormal      = 0
    10  	fuzzDiscard     = -1
    11  )
    12  
    13  func Fuzz(data []byte) int {
    14      if len(data) == 0 {
    15          return fuzzDiscard
    16      }
    17      size := int(uint8(data[0])) + 2 // TODO fix min size in NewScheme
    18  	if size > (1 << 15) {
    19          size = 1 << 15
    20      }
    21      r := bytes.NewReader(data[1:])
    22      var alpha, point fr.Element
    23      alpha.SetRawBytes(r)
    24      point.SetRawBytes(r)
    25      s := NewScheme(size, alpha)
    26  
    27  	// create polynomials
    28  	f := make([]polynomial.Polynomial, size /2 )
    29  	for i := 0; i < len(f); i++ {
    30          f[i] = make(polynomial.Polynomial, size)
    31          for j:=0;j<len(f[i]);j++ {
    32              f[i][j].SetRawBytes(r)
    33          }
    34  	}
    35  
    36      // commit the polynomials
    37  	digests := make([]Digest, size /2)
    38  	for i := 0; i < len(digests); i++ {
    39  		digests[i], _ = s.Commit(f[i])
    40  
    41  	}
    42  
    43      proof, err := s.BatchOpenSinglePoint(&point, digests, f)
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  
    48      // verify the claimed values
    49  	for i := 0; i < len(f); i++ {
    50  		expectedClaim := f[i].Eval(&point)
    51  		if !expectedClaim.Equal(&proof.ClaimedValues[i]) {
    52              panic("inconsistent claimed values")
    53  		}
    54  	}
    55  
    56  	// verify correct proof
    57  	err = s.BatchVerifySinglePoint(digests, &proof)
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  
    62  
    63  	return fuzzNormal
    64  }
    65  
    66  
    67