github.com/consensys/gnark-crypto@v0.14.0/internal/generator/iop/template/expressions.test.go.tmpl (about)

     1  import (
     2  	"testing"
     3  
     4  	"github.com/consensys/gnark-crypto/ecc/{{ .Name }}/fr"
     5  )
     6  
     7  func TestEvaluate(t *testing.T) {
     8  
     9  	f := func(_ int, x ...fr.Element) fr.Element {
    10  		var a fr.Element
    11  		a.Add(&x[0], &x[1]).Add(&a, &x[2])
    12  		return a
    13  	}
    14  
    15  	size := 64
    16  	u := make([]fr.Element, size)
    17  	v := make([]fr.Element, size)
    18  	w := make([]fr.Element, size)
    19  	for i := 0; i < size; i++ {
    20  		u[i].SetUint64(uint64(i))
    21  		v[i].SetUint64(uint64(i + 1))
    22  		w[i].SetUint64(uint64(i + 2))
    23  	}
    24  	r := make([]fr.Element, size)
    25  	for i := 0; i < size; i++ {
    26  		r[i].SetUint64(uint64(3 * (i + 1)))
    27  	}
    28  	form := Form{Layout: Regular, Basis: Canonical}
    29  	wu := NewPolynomial(&u, form)
    30  	wv := NewPolynomial(&v, form)
    31  	ww := NewPolynomial(&w, form)
    32  
    33  	rr, err := Evaluate(f, nil, form, wu, wv, ww)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	wu.ToBitReverse()
    39  	rrb, err := Evaluate(f, nil, form, wu, wv, ww)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	wv.ToBitReverse()
    45  	ww.ToBitReverse()
    46  	rrc, err := Evaluate(f, nil, form, wu, wv, ww)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	// compare with the expected result
    52  	for i := 0; i < size; i++ {
    53  		if !rr.Coefficients()[i].Equal(&r[i]) {
    54  			t.Fatal("error evaluation")
    55  		}
    56  		if !rrb.Coefficients()[i].Equal(&r[i]) {
    57  			t.Fatal("error evaluation")
    58  		}
    59  		if !rrc.Coefficients()[i].Equal(&r[i]) {
    60  			t.Fatal("error evaluation")
    61  		}
    62  
    63  	}
    64  }