arnaucube
arnaucube/arboPublic Self-hosted repository
arbo/testvectors/circom/go-data-generator/generator_test.go
testvectors/circom/go-data-generator/generator_test.goRaw
 1package main
 2
 3import (
 4	"encoding/json"
 5	"io/ioutil"
 6	"math/big"
 7	"testing"
 8
 9	qt "github.com/frankban/quicktest"
10	"github.com/vocdoni/arbo"
11	"go.vocdoni.io/dvote/db"
12	"go.vocdoni.io/dvote/db/badgerdb"
13)
14
15func TestGenerator(t *testing.T) {
16	c := qt.New(t)
17	database, err := badgerdb.New(db.Options{Path: c.TempDir()})
18	c.Assert(err, qt.IsNil)
19	tree, err := arbo.NewTree(database, 4, arbo.HashFunctionPoseidon)
20	c.Assert(err, qt.IsNil)
21
22	testVector := [][]int64{
23		{1, 11},
24		{2, 22},
25		{3, 33},
26		{4, 44},
27	}
28	bLen := 1
29	for i := 0; i < len(testVector); i++ {
30		k := arbo.BigIntToBytes(bLen, big.NewInt(testVector[i][0]))
31		v := arbo.BigIntToBytes(bLen, big.NewInt(testVector[i][1]))
32		if err := tree.Add(k, v); err != nil {
33			t.Fatal(err)
34		}
35	}
36
37	// proof of existence
38	k := arbo.BigIntToBytes(bLen, big.NewInt(int64(2)))
39	cvp, err := tree.GenerateCircomVerifierProof(k)
40	c.Assert(err, qt.IsNil)
41	jCvp, err := json.Marshal(cvp)
42	c.Assert(err, qt.IsNil)
43	// store the data into a file that will be used at the circom test
44	err = ioutil.WriteFile("go-smt-verifier-inputs.json", jCvp, 0600)
45	c.Assert(err, qt.IsNil)
46
47	// proof of non-existence
48	k = arbo.BigIntToBytes(bLen, big.NewInt(int64(5)))
49	cvp, err = tree.GenerateCircomVerifierProof(k)
50	c.Assert(err, qt.IsNil)
51	jCvp, err = json.Marshal(cvp)
52	c.Assert(err, qt.IsNil)
53	// store the data into a file that will be used at the circom test
54	err = ioutil.WriteFile("go-smt-verifier-non-existence-inputs.json", jCvp, 0600)
55	c.Assert(err, qt.IsNil)
56}