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 bLen := tree.HashFunction().Len()
22
23 testVector := [][]int64{
24 {1, 11},
25 {2, 22},
26 {3, 33},
27 {4, 44},
28 }
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}