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"
10 "go.vocdoni.io/dvote/db/badgerdb"
11)
12
13func TestCircomVerifierProof(t *testing.T) {
14 c := qt.New(t)
15 database, err := badgerdb.New(db.Options{Path: c.TempDir()})
16 c.Assert(err, qt.IsNil)
17 tree, err := NewTree(database, 4, HashFunctionPoseidon)
18 c.Assert(err, qt.IsNil)
19 defer tree.db.Close() //nolint:errcheck
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 := BigIntToBytes(bLen, big.NewInt(testVector[i][0]))
30 v := 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 := 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 // test vector checked with a circom circuit (arbo/testvectors/circom)
43 c.Assert(string(jCvp), qt.Equals, `{"fnc":0,"isOld0":"0","key":"2","oldK`+
44 `ey":"0","oldValue":"0","root":"1355816845522055904274785395894906304622`+
45 `6645447188878859760119761585093422436","siblings":["1162013050763544193`+
46 `2056895853942898236773847390796721536119314875877874016518","5158240518`+
47 `874928563648144881543092238925265313977134167935552944620041388700","0"`+
48 `,"0"],"value":"22"}`)
49
50 // proof of non-existence
51 k = BigIntToBytes(bLen, big.NewInt(int64(5)))
52 cvp, err = tree.GenerateCircomVerifierProof(k)
53 c.Assert(err, qt.IsNil)
54 jCvp, err = json.Marshal(cvp)
55 c.Assert(err, qt.IsNil)
56 // test vector checked with a circom circuit (arbo/testvectors/circom)
57 c.Assert(string(jCvp), qt.Equals, `{"fnc":1,"isOld0":"0","key":"5","oldK`+
58 `ey":"1","oldValue":"11","root":"135581684552205590427478539589490630462`+
59 `26645447188878859760119761585093422436","siblings":["756056982086999933`+
60 `1905412009838015295115276841209205575174464206730109811365","1276103081`+
61 `3800436751877086580591648324911598798716611088294049841213649313596","0`+
62 `","0"],"value":"11"}`)
63}