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