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