From cc4388ff86314a02dbb86bdd2317b5ab11444a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Men=C3=A9ndez?= Date: Wed, 4 Dec 2024 08:24:49 +0100 Subject: [PATCH] support to generate census proofs in batch for testing --- testutil/utils.go | 91 ++++++++++++++++------------- tree/arbo/verifier_bls12377_test.go | 8 +-- tree/arbo/verifier_bn254_test.go | 8 +-- 3 files changed, 60 insertions(+), 47 deletions(-) diff --git a/testutil/utils.go b/testutil/utils.go index 1e3e9ff..9387ea6 100644 --- a/testutil/utils.go +++ b/testutil/utils.go @@ -28,15 +28,21 @@ type CensusTestConfig struct { BaseFiled *big.Int } -// TestCensus is a structure to store the root, key, value, and siblings of a +// TestCensusProofs is a structure to store the key, value, and siblings of a // census proof for testing purposes. -type TestCensus struct { - Root *big.Int +type TestCensusProofs struct { Key *big.Int Value *big.Int Siblings []*big.Int } +// TestCensus is a structure to store the root and proofs of a census for +// testing purposes. +type TestCensus struct { + Root *big.Int + Proofs []*TestCensusProofs +} + // TestSignature is a structure to store the public key, R, S, and address of a // signature for testing purposes. type TestSignature struct { @@ -53,7 +59,7 @@ type TestSignature struct { // includes the temp directory to store the database, the number of valid // siblings, the total number of siblings, the key length, the hash function to // use in the merkle tree, and the base field to use in the finite field. -func GenerateCensusProofForTest(conf CensusTestConfig, k, v []byte) (*TestCensus, error) { +func GenerateCensusProofForTest(conf CensusTestConfig, ks, vs [][]byte) (*TestCensus, error) { defer func() { _ = os.RemoveAll(conf.Dir) }() @@ -69,11 +75,12 @@ func GenerateCensusProofForTest(conf CensusTestConfig, k, v []byte) (*TestCensus if err != nil { return nil, err } - - k = arbotree.BigToFF(conf.BaseFiled, new(big.Int).SetBytes(k)).Bytes() - // add the first key-value pair - if err = tree.Add(k, v); err != nil { - return nil, err + // add the key-value pairs + for i, k := range ks { + k = arbotree.BigToFF(conf.BaseFiled, new(big.Int).SetBytes(k)).Bytes() + if err = tree.Add(k, vs[i]); err != nil { + return nil, err + } } // add random addresses for i := 1; i < conf.ValidSiblings; i++ { @@ -83,42 +90,48 @@ func GenerateCensusProofForTest(conf CensusTestConfig, k, v []byte) (*TestCensus return nil, err } } - // generate the proof - _, _, siblings, exist, err := tree.GenProof(k) - if err != nil { - return nil, err - } - if !exist { - return nil, fmt.Errorf("error building the merkle tree: key not found") - } - unpackedSiblings, err := arbo.UnpackSiblings(tree.HashFunction(), siblings) - if err != nil { - return nil, err - } - paddedSiblings := make([]*big.Int, conf.TotalSiblings) - for i := 0; i < conf.TotalSiblings; i++ { - if i < len(unpackedSiblings) { - paddedSiblings[i] = arbo.BytesLEToBigInt(unpackedSiblings[i]) - } else { - paddedSiblings[i] = big.NewInt(0) - } - } + // generate the proofs root, err := tree.Root() if err != nil { return nil, err } - verified, err := arbotree.CheckProof(tree.HashFunction(), k, v, root, siblings) - if !verified { - return nil, fmt.Errorf("error verifying the proof") - } - if err != nil { - return nil, err + proofs := []*TestCensusProofs{} + for i, k := range ks { + _, _, siblings, exist, err := tree.GenProof(k) + if err != nil { + return nil, err + } + if !exist { + return nil, fmt.Errorf("error building the merkle tree: key not found") + } + unpackedSiblings, err := arbo.UnpackSiblings(tree.HashFunction(), siblings) + if err != nil { + return nil, err + } + paddedSiblings := make([]*big.Int, conf.TotalSiblings) + for i := 0; i < conf.TotalSiblings; i++ { + if i < len(unpackedSiblings) { + paddedSiblings[i] = arbo.BytesLEToBigInt(unpackedSiblings[i]) + } else { + paddedSiblings[i] = big.NewInt(0) + } + } + verified, err := arbotree.CheckProof(tree.HashFunction(), k, vs[i], root, siblings) + if !verified { + return nil, fmt.Errorf("error verifying the proof") + } + if err != nil { + return nil, err + } + proofs = append(proofs, &TestCensusProofs{ + Key: arbo.BytesLEToBigInt(k), + Value: new(big.Int).SetBytes(vs[i]), + Siblings: paddedSiblings, + }) } return &TestCensus{ - Root: arbo.BytesLEToBigInt(root), - Key: arbo.BytesLEToBigInt(k), - Value: new(big.Int).SetBytes(v), - Siblings: paddedSiblings, + Root: arbo.BytesLEToBigInt(root), + Proofs: proofs, }, nil } diff --git a/tree/arbo/verifier_bls12377_test.go b/tree/arbo/verifier_bls12377_test.go index 2cce205..8632366 100644 --- a/tree/arbo/verifier_bls12377_test.go +++ b/tree/arbo/verifier_bls12377_test.go @@ -62,17 +62,17 @@ func TestVerifierBLS12377(t *testing.T) { KeyLen: k_len, Hash: arbotree.HashFunctionMiMC_BLS12_377, BaseFiled: arbotree.BLS12377BaseField, - }, util.RandomBytes(k_len), big.NewInt(10).Bytes()) + }, [][]byte{util.RandomBytes(k_len)}, [][]byte{big.NewInt(10).Bytes()}) c.Assert(err, qt.IsNil) // init and print inputs fSiblings := [n_siblings]frontend.Variable{} for i := 0; i < n_siblings; i++ { - fSiblings[i] = testCensus.Siblings[i] + fSiblings[i] = testCensus.Proofs[0].Siblings[i] } inputs := testVerifierBLS12377{ Root: testCensus.Root, - Key: testCensus.Key, - Value: testCensus.Value, + Key: testCensus.Proofs[0].Key, + Value: testCensus.Proofs[0].Value, Siblings: fSiblings, } assert := test.NewAssert(t) diff --git a/tree/arbo/verifier_bn254_test.go b/tree/arbo/verifier_bn254_test.go index 15fe968..f703d0d 100644 --- a/tree/arbo/verifier_bn254_test.go +++ b/tree/arbo/verifier_bn254_test.go @@ -48,17 +48,17 @@ func TestVerifierBN254(t *testing.T) { KeyLen: k_len, Hash: arbotree.HashFunctionPoseidon, BaseFiled: arbotree.BN254BaseField, - }, util.RandomBytes(k_len), big.NewInt(10).Bytes()) + }, [][]byte{util.RandomBytes(k_len)}, [][]byte{big.NewInt(10).Bytes()}) c.Assert(err, qt.IsNil) // init and print inputs fSiblings := [n_siblings]frontend.Variable{} for i := 0; i < n_siblings; i++ { - fSiblings[i] = testCensus.Siblings[i] + fSiblings[i] = testCensus.Proofs[0].Siblings[i] } inputs := testVerifierBN254{ Root: testCensus.Root, - Key: testCensus.Key, - Value: testCensus.Value, + Key: testCensus.Proofs[0].Key, + Value: testCensus.Proofs[0].Value, Siblings: fSiblings, } assert := test.NewAssert(t)