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

     1  import (
     2  	"math/big"
     3  	"testing"
     4  
     5  	"github.com/consensys/gnark-crypto/ecc/{{ .Name }}/fr"
     6  	"github.com/consensys/gnark-crypto/ecc/{{ .Name }}/kzg"
     7  )
     8  
     9  func TestLookupVector(t *testing.T) {
    10  
    11  	lookupVector := make(fr.Vector, 8)
    12  	fvector := make(fr.Vector, 7)
    13  	for i := 0; i < 8; i++ {
    14  		lookupVector[i].SetUint64(uint64(2 * i))
    15  	}
    16  	for i := 0; i < 7; i++ {
    17  		fvector[i].Set(&lookupVector[(4*i+1)%8])
    18  	}
    19  
    20  	kzgSrs, err := kzg.NewSRS(64, big.NewInt(13))
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	// correct proof vector
    26  	{
    27  		proof, err := ProveLookupVector(kzgSrs.Pk, fvector, lookupVector)
    28  		if err != nil {
    29  			t.Fatal(err)
    30  		}
    31  
    32  		err = VerifyLookupVector(kzgSrs.Vk, proof)
    33  		if err != nil {
    34  			t.Fatal(err)
    35  		}
    36  	}
    37  
    38  	// wrong proofs vector
    39  	{
    40  		fvector[0].SetRandom()
    41  
    42  		proof, err := ProveLookupVector(kzgSrs.Pk, fvector, lookupVector)
    43  		if err != nil {
    44  			t.Fatal(err)
    45  		}
    46  
    47  		err = VerifyLookupVector(kzgSrs.Vk, proof)
    48  		if err == nil {
    49  			t.Fatal(err)
    50  		}
    51  	}
    52  
    53  }
    54  
    55  func TestLookupTable(t *testing.T) {
    56  
    57  	kzgSrs, err := kzg.NewSRS(64, big.NewInt(13))
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	lookupTable := make([]fr.Vector, 3)
    63  	fTable := make([]fr.Vector, 3)
    64  	for i := 0; i < 3; i++ {
    65  		lookupTable[i] = make(fr.Vector, 8)
    66  		fTable[i] = make(fr.Vector, 7)
    67  		for j := 0; j < 8; j++ {
    68  			lookupTable[i][j].SetUint64(uint64(2*i + j))
    69  		}
    70  		for j := 0; j < 7; j++ {
    71  			fTable[i][j].Set(&lookupTable[i][(4*j+1)%8])
    72  		}
    73  	}
    74  
    75  	// correct proof
    76  	{
    77  		proof, err := ProveLookupTables(kzgSrs.Pk, fTable, lookupTable)
    78  		if err != nil {
    79  			t.Fatal(err)
    80  		}
    81  
    82  		err = VerifyLookupTables(kzgSrs.Vk, proof)
    83  		if err != nil {
    84  			t.Fatal(err)
    85  		}
    86  	}
    87  
    88  	// wrong proof
    89  	{
    90  		fTable[0][0].SetRandom()
    91  		proof, err := ProveLookupTables(kzgSrs.Pk, fTable, lookupTable)
    92  		if err != nil {
    93  			t.Fatal(err)
    94  		}
    95  
    96  		err = VerifyLookupTables(kzgSrs.Vk, proof)
    97  		if err == nil {
    98  			t.Fatal(err)
    99  		}
   100  	}
   101  
   102  }
   103  
   104  func BenchmarkPlookup(b *testing.B) {
   105  
   106  	srsSize := 1 << 15
   107  	polySize := 1 << 14
   108  
   109  	kzgSrs, _ := kzg.NewSRS(uint64(srsSize), big.NewInt(13))
   110  	a := make(fr.Vector, polySize)
   111  	c := make(fr.Vector, polySize)
   112  
   113  	for i := 0; i < 1<<14; i++ {
   114  		a[i].SetUint64(uint64(i))
   115  		c[i].SetUint64(uint64((8 * i) % polySize))
   116  	}
   117  
   118  	b.ResetTimer()
   119  	for i := 0; i < b.N; i++ {
   120  		ProveLookupVector(kzgSrs.Pk, a, c)
   121  	}
   122  }