github.com/consensys/gnark-crypto@v0.14.0/ecc/bls12-381/fr/plookup/plookup_test.go (about)

     1  // Copyright 2020 Consensys Software Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Code generated by consensys/gnark-crypto DO NOT EDIT
    16  
    17  package plookup
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
    24  	"github.com/consensys/gnark-crypto/ecc/bls12-381/kzg"
    25  )
    26  
    27  func TestLookupVector(t *testing.T) {
    28  
    29  	lookupVector := make(fr.Vector, 8)
    30  	fvector := make(fr.Vector, 7)
    31  	for i := 0; i < 8; i++ {
    32  		lookupVector[i].SetUint64(uint64(2 * i))
    33  	}
    34  	for i := 0; i < 7; i++ {
    35  		fvector[i].Set(&lookupVector[(4*i+1)%8])
    36  	}
    37  
    38  	kzgSrs, err := kzg.NewSRS(64, big.NewInt(13))
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	// correct proof vector
    44  	{
    45  		proof, err := ProveLookupVector(kzgSrs.Pk, fvector, lookupVector)
    46  		if err != nil {
    47  			t.Fatal(err)
    48  		}
    49  
    50  		err = VerifyLookupVector(kzgSrs.Vk, proof)
    51  		if err != nil {
    52  			t.Fatal(err)
    53  		}
    54  	}
    55  
    56  	// wrong proofs vector
    57  	{
    58  		fvector[0].SetRandom()
    59  
    60  		proof, err := ProveLookupVector(kzgSrs.Pk, fvector, lookupVector)
    61  		if err != nil {
    62  			t.Fatal(err)
    63  		}
    64  
    65  		err = VerifyLookupVector(kzgSrs.Vk, proof)
    66  		if err == nil {
    67  			t.Fatal(err)
    68  		}
    69  	}
    70  
    71  }
    72  
    73  func TestLookupTable(t *testing.T) {
    74  
    75  	kzgSrs, err := kzg.NewSRS(64, big.NewInt(13))
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	lookupTable := make([]fr.Vector, 3)
    81  	fTable := make([]fr.Vector, 3)
    82  	for i := 0; i < 3; i++ {
    83  		lookupTable[i] = make(fr.Vector, 8)
    84  		fTable[i] = make(fr.Vector, 7)
    85  		for j := 0; j < 8; j++ {
    86  			lookupTable[i][j].SetUint64(uint64(2*i + j))
    87  		}
    88  		for j := 0; j < 7; j++ {
    89  			fTable[i][j].Set(&lookupTable[i][(4*j+1)%8])
    90  		}
    91  	}
    92  
    93  	// correct proof
    94  	{
    95  		proof, err := ProveLookupTables(kzgSrs.Pk, fTable, lookupTable)
    96  		if err != nil {
    97  			t.Fatal(err)
    98  		}
    99  
   100  		err = VerifyLookupTables(kzgSrs.Vk, proof)
   101  		if err != nil {
   102  			t.Fatal(err)
   103  		}
   104  	}
   105  
   106  	// wrong proof
   107  	{
   108  		fTable[0][0].SetRandom()
   109  		proof, err := ProveLookupTables(kzgSrs.Pk, fTable, lookupTable)
   110  		if err != nil {
   111  			t.Fatal(err)
   112  		}
   113  
   114  		err = VerifyLookupTables(kzgSrs.Vk, proof)
   115  		if err == nil {
   116  			t.Fatal(err)
   117  		}
   118  	}
   119  
   120  }
   121  
   122  func BenchmarkPlookup(b *testing.B) {
   123  
   124  	srsSize := 1 << 15
   125  	polySize := 1 << 14
   126  
   127  	kzgSrs, _ := kzg.NewSRS(uint64(srsSize), big.NewInt(13))
   128  	a := make(fr.Vector, polySize)
   129  	c := make(fr.Vector, polySize)
   130  
   131  	for i := 0; i < 1<<14; i++ {
   132  		a[i].SetUint64(uint64(i))
   133  		c[i].SetUint64(uint64((8 * i) % polySize))
   134  	}
   135  
   136  	b.ResetTimer()
   137  	for i := 0; i < b.N; i++ {
   138  		ProveLookupVector(kzgSrs.Pk, a, c)
   139  	}
   140  }