arnaucube
arnaucube/arboPublic Self-hosted repository
arbo/circomproofs_test.go
circomproofs_test.goRaw
 1package arbo
 2
 3import (
 4	"encoding/json"
 5	"math/big"
 6	"testing"
 7
 8	qt "github.com/frankban/quicktest"
 9	"go.vocdoni.io/dvote/db/badgerdb"
10)
11
12func TestCircomVerifierProof(t *testing.T) {
13	c := qt.New(t)
14	database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})
15	c.Assert(err, qt.IsNil)
16	tree, err := NewTree(database, 4, HashFunctionPoseidon)
17	c.Assert(err, qt.IsNil)
18	defer tree.db.Close() //nolint:errcheck
19
20	testVector := [][]int64{
21		{1, 11},
22		{2, 22},
23		{3, 33},
24		{4, 44},
25	}
26	bLen := 1
27	for i := 0; i < len(testVector); i++ {
28		k := BigIntToBytes(bLen, big.NewInt(testVector[i][0]))
29		v := BigIntToBytes(bLen, big.NewInt(testVector[i][1]))
30		if err := tree.Add(k, v); err != nil {
31			t.Fatal(err)
32		}
33	}
34
35	// proof of existence
36	k := BigIntToBytes(bLen, big.NewInt(int64(2)))
37	cvp, err := tree.GenerateCircomVerifierProof(k)
38	c.Assert(err, qt.IsNil)
39	jCvp, err := json.Marshal(cvp)
40	c.Assert(err, qt.IsNil)
41	// test vector checked with a circom circuit (arbo/testvectors/circom)
42	c.Assert(string(jCvp), qt.Equals, `{"fnc":0,"isOld0":"0","key":"2","oldK`+
43		`ey":"0","oldValue":"0","root":"1355816845522055904274785395894906304622`+
44		`6645447188878859760119761585093422436","siblings":["1162013050763544193`+
45		`2056895853942898236773847390796721536119314875877874016518","5158240518`+
46		`874928563648144881543092238925265313977134167935552944620041388700","0"`+
47		`,"0"],"value":"22"}`)
48
49	// proof of non-existence
50	k = BigIntToBytes(bLen, big.NewInt(int64(5)))
51	cvp, err = tree.GenerateCircomVerifierProof(k)
52	c.Assert(err, qt.IsNil)
53	jCvp, err = json.Marshal(cvp)
54	c.Assert(err, qt.IsNil)
55	// test vector checked with a circom circuit (arbo/testvectors/circom)
56	c.Assert(string(jCvp), qt.Equals, `{"fnc":1,"isOld0":"0","key":"5","oldK`+
57		`ey":"1","oldValue":"11","root":"135581684552205590427478539589490630462`+
58		`26645447188878859760119761585093422436","siblings":["756056982086999933`+
59		`1905412009838015295115276841209205575174464206730109811365","1276103081`+
60		`3800436751877086580591648324911598798716611088294049841213649313596","0`+
61		`","0"],"value":"11"}`)
62}