circomlib/test/smtverifier.js
test/smtverifier.jsRaw
1const chai = require("chai");
2const path = require("path");
3const bigInt = require("big-integer");
4const tester = require("circom").tester;
5
6const smt = require("../src/smt.js");
7
8const assert = chai.assert;
9
10function print(circuit, w, s) {
11 console.log(s + ": " + w[circuit.getSignalIdx(s)]);
12}
13
14async function testInclusion(tree, key, circuit) {
15
16 const res = await tree.find(key);
17
18 assert(res.found);
19 let siblings = res.siblings;
20 while (siblings.length<10) siblings.push(bigInt(0));
21
22 const w = await circuit.calculateWitness({
23 enabled: 1,
24 fnc: 0,
25 root: tree.root,
26 siblings: siblings,
27 oldKey: 0,
28 oldValue: 0,
29 isOld0: 0,
30 key: key,
31 value: res.foundValue
32 });
33
34 await circuit.checkConstraints(w);
35
36}
37
38async function testExclusion(tree, key, circuit) {
39 const res = await tree.find(key);
40
41 assert(!res.found);
42 let siblings = res.siblings;
43 while (siblings.length<10) siblings.push(bigInt(0));
44
45 const w = await circuit.calculateWitness({
46 enabled: 1,
47 fnc: 1,
48 root: tree.root,
49 siblings: siblings,
50 oldKey: res.isOld0 ? 0 : res.notFoundKey,
51 oldValue: res.isOld0 ? 0 : res.notFoundValue,
52 isOld0: res.isOld0 ? 1 : 0,
53 key: key,
54 value: 0
55 });
56
57 await circuit.checkConstraints(w);
58
59}
60
61describe("SMT Verifier test", function () {
62 let circuit;
63 let tree;
64
65 this.timeout(100000);
66
67 before( async () => {
68 circuit = await tester(path.join(__dirname, "circuits", "smtverifier10_test.circom"));
69
70 tree = await smt.newMemEmptyTrie();
71 await tree.insert(7,77);
72 await tree.insert(8,88);
73 await tree.insert(32,3232);
74 });
75
76 it("Check inclussion in a tree of 3", async () => {
77 await testInclusion(tree, 7, circuit);
78 await testInclusion(tree, 8, circuit);
79 await testInclusion(tree, 32, circuit);
80 });
81
82 it("Check exclussion in a tree of 3", async () => {
83 await testExclusion(tree, 0, circuit);
84 await testExclusion(tree, 6, circuit);
85 await testExclusion(tree, 9, circuit);
86 await testExclusion(tree, 33, circuit);
87 await testExclusion(tree, 31, circuit);
88 await testExclusion(tree, 16, circuit);
89 await testExclusion(tree, 64, circuit);
90 });
91
92 it("Check not enabled accepts any thing", async () => {
93 let siblings = [];
94 for (let i=0; i<10; i++) siblings.push(i);
95
96 const w = await circuit.calculateWitness({
97 enabled: 0,
98 fnc: 0,
99 root: 1,
100 siblings: siblings,
101 oldKey: 22,
102 oldValue: 33,
103 isOld0: 0,
104 key: 44,
105 value: 0
106 });
107
108
109 await circuit.checkConstraints(w);
110 });
111
112 it("Check inclussion Adria case", async () => {
113 const e1_hi= bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839");
114 const e1_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179");
115
116 const e2ok_hi= bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262");
117 const e2ok_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179");
118
119 const e2fail_hi= bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579");
120 const e2fail_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179");
121
122 const tree1 = await smt.newMemEmptyTrie();
123 await tree1.insert(e1_hi,e1_hv);
124 await tree1.insert(e2ok_hi,e2ok_hv);
125
126 await testInclusion(tree1, e2ok_hi, circuit);
127
128 const tree2 = await smt.newMemEmptyTrie();
129 await tree2.insert(e1_hi,e1_hv);
130 await tree2.insert(e2fail_hi,e2fail_hv);
131
132 await testInclusion(tree2, e2fail_hi, circuit);
133 });
134
135
136});