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