circomlib/test/smtprocessor.js
test/smtprocessor.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 testInsert(tree, key, value, circuit, log ) {
15
16 const res = await tree.insert(key,value);
17 let siblings = res.siblings;
18 while (siblings.length<10) siblings.push(bigInt(0));
19
20 const w = await circuit.calculateWitness({
21 fnc: [1,0],
22 oldRoot: res.oldRoot,
23 siblings: siblings,
24 oldKey: res.isOld0 ? 0 : res.oldKey,
25 oldValue: res.isOld0 ? 0 : res.oldValue,
26 isOld0: res.isOld0 ? 1 : 0,
27 newKey: key,
28 newValue: value
29 }, log);
30
31 // TODO
32 // assert(circuit.checkWitness(w));
33
34 await circuit.assertOut(w, {newRoot: res.newRoot});
35
36}
37
38async function testDelete(tree, key, circuit) {
39 const res = await tree.delete(key);
40 let siblings = res.siblings;
41 while (siblings.length<10) siblings.push(bigInt(0));
42
43 const w = await circuit.calculateWitness({
44 fnc: [1,1],
45 oldRoot: res.oldRoot,
46 siblings: siblings,
47 oldKey: res.isOld0 ? 0 : res.oldKey,
48 oldValue: res.isOld0 ? 0 : res.oldValue,
49 isOld0: res.isOld0 ? 1 : 0,
50 newKey: res.delKey,
51 newValue: res.delValue
52 });
53
54 // TODO
55 // assert(circuit.checkWitness(w));
56
57 await circuit.assertOut(w, {newRoot: res.newRoot});
58}
59
60async function testUpdate(tree, key, newValue, circuit) {
61 const res = await tree.update(key, newValue);
62 let siblings = res.siblings;
63 while (siblings.length<10) siblings.push(bigInt(0));
64
65 const w = await circuit.calculateWitness({
66 fnc: [0,1],
67 oldRoot: res.oldRoot,
68 siblings: siblings,
69 oldKey: res.oldKey,
70 oldValue: res.oldValue,
71 isOld0: 0,
72 newKey: res.newKey,
73 newValue: res.newValue
74 });
75
76 // TODO
77 // assert(circuit.checkWitness(w));
78
79 await circuit.assertOut(w, {newRoot: res.newRoot});
80}
81
82
83describe("SMT test", function () {
84 let circuit;
85 let tree;
86
87 this.timeout(10000000);
88
89 before( async () => {
90 circuit = await tester(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
91 await circuit.loadSymbols();
92
93 tree = await smt.newMemEmptyTrie();
94 });
95
96 it("Should verify an insert to an empty tree", async () => {
97 const key = bigInt(111);
98 const value = bigInt(222);
99
100 await testInsert(tree, key, value, circuit);
101 });
102
103 it("It should add another element", async () => {
104 const key = bigInt(333);
105 const value = bigInt(444);
106
107 await testInsert(tree, key, value, circuit);
108 });
109
110
111
112 it("Should remove an element", async () => {
113 await testDelete(tree, 111, circuit);
114 await testDelete(tree, 333, circuit);
115 });
116
117 it("Should test convination of adding and removing 3 elements", async () => {
118 const keys = [bigInt(8), bigInt(9), bigInt(32)];
119 const values = [bigInt(88), bigInt(99), bigInt(3232)];
120 const tree1 = await smt.newMemEmptyTrie();
121 const tree2 = await smt.newMemEmptyTrie();
122 const tree3 = await smt.newMemEmptyTrie();
123 const tree4 = await smt.newMemEmptyTrie();
124 const tree5 = await smt.newMemEmptyTrie();
125 const tree6 = await smt.newMemEmptyTrie();
126
127 await testInsert(tree1,keys[0],values[0], circuit);
128 await testInsert(tree1,keys[1],values[1], circuit);
129 await testInsert(tree1,keys[2],values[2], circuit);
130
131 await testInsert(tree2,keys[0],values[0], circuit);
132 await testInsert(tree2,keys[2],values[2], circuit);
133 await testInsert(tree2,keys[1],values[1], circuit);
134
135 await testInsert(tree3,keys[1],values[1], circuit);
136 await testInsert(tree3,keys[0],values[0], circuit);
137 await testInsert(tree3,keys[2],values[2], circuit);
138
139 await testInsert(tree4,keys[1],values[1], circuit);
140 await testInsert(tree4,keys[2],values[2], circuit);
141 await testInsert(tree4,keys[0],values[0], circuit);
142
143 await testInsert(tree5,keys[2],values[2], circuit);
144 await testInsert(tree5,keys[0],values[0], circuit);
145 await testInsert(tree5,keys[1],values[1], circuit);
146
147 await testInsert(tree6,keys[2],values[2], circuit);
148 await testInsert(tree6,keys[1],values[1], circuit);
149 await testInsert(tree6,keys[0],values[0], circuit);
150
151
152 await testDelete(tree1, keys[0], circuit);
153 await testDelete(tree1, keys[1], circuit);
154 await testDelete(tree2, keys[1], circuit);
155 await testDelete(tree2, keys[0], circuit);
156
157 await testDelete(tree3, keys[0], circuit);
158 await testDelete(tree3, keys[2], circuit);
159 await testDelete(tree4, keys[2], circuit);
160 await testDelete(tree4, keys[0], circuit);
161
162
163 await testDelete(tree5, keys[1], circuit);
164 await testDelete(tree5, keys[2], circuit);
165 await testDelete(tree6, keys[2], circuit);
166 await testDelete(tree6, keys[1], circuit);
167
168 await testDelete(tree1, keys[2], circuit);
169 await testDelete(tree2, keys[2], circuit);
170 await testDelete(tree3, keys[1], circuit);
171 await testDelete(tree4, keys[1], circuit);
172 await testDelete(tree5, keys[0], circuit);
173 await testDelete(tree6, keys[0], circuit);
174 });
175
176 it("Should match a NOp with random vals", async () => {
177 let siblings = [];
178 while (siblings.length<10) siblings.push(bigInt(88));
179 const w = await circuit.calculateWitness({
180 fnc: [0,0],
181 oldRoot: 11,
182 siblings: siblings,
183 oldKey: 33,
184 oldValue: 44,
185 isOld0: 55,
186 newKey: 66,
187 newValue: 77
188 });
189
190 const root1 = w[circuit.symbols["main.oldRoot"].idxWit];
191 const root2 = w[circuit.symbols["main.newRoot"].idxWit];
192
193 // TODO
194 // assert(circuit.checkWitness(w));
195
196 assert(root1.equals(root2));
197 });
198 it("Should update an element", async () => {
199 const tree1 = await smt.newMemEmptyTrie();
200 const tree2 = await smt.newMemEmptyTrie();
201
202 await testInsert(tree1,8,88, circuit);
203 await testInsert(tree1,9,99, circuit);
204 await testInsert(tree1,32,3232, circuit);
205
206 await testInsert(tree2,8,888, circuit);
207 await testInsert(tree2,9,999, circuit);
208 await testInsert(tree2,32,323232, circuit);
209
210 await testUpdate(tree1, 8, 888, circuit);
211 await testUpdate(tree1, 9, 999, circuit);
212 await testUpdate(tree1, 32, 323232, circuit);
213 });
214
215});