circomlib/test/smtverifier_adria.js
test/smtverifier_adria.jsRaw
1const path = require("path");
2const snarkjs = require("snarkjs");
3const compiler = require("circom");
4const fs = require("fs")
5
6const bigInt = snarkjs.bigInt;
7const smt = require("../src/smt.js");
8
9const circuitSource = `
10include "../circuits/smt/smtverifier.circom";
11template SMT(nLevels) {
12 signal input root;
13 signal input mtp[nLevels];
14 signal input hi;
15 signal input hv;
16
17 component smtClaimExists = SMTVerifier(nLevels);
18 smtClaimExists.enabled <== 1;
19 smtClaimExists.fnc <== 0;
20 smtClaimExists.root <== root;
21 for (var i=0; i<nLevels; i++) {
22 smtClaimExists.siblings[i] <== mtp[i];
23 }
24 smtClaimExists.oldKey <== 0;
25 smtClaimExists.oldValue <== 0;
26 smtClaimExists.isOld0 <== 0;
27
28 smtClaimExists.key <== hi;
29 smtClaimExists.value <== hv;
30}
31component main = SMT(4);
32`;
33
34describe("smt3test", function () {
35 this.timeout(200000);
36
37 let circuitFileName;
38
39 before( async () => {
40 circuitFileName = path.join(__dirname, ".", "rawsmt3.circom");
41 fs.writeFileSync(circuitFileName,circuitSource);
42 });
43
44 const levels = 4;
45 async function testsmt3(e1, e2) {
46 let tree = await smt.newMemEmptyTrie();
47
48 // insert e1, e2
49 await tree.insert(e1.hi, e1.hv);
50 await tree.insert(e2.hi, e2.hv);
51
52 // generate proof for e1
53 const findInfo = await tree.find(e1.hi);
54 const siblings = findInfo.siblings;
55 while (siblings.length < levels) siblings.push(bigInt(0));
56
57 const input = {
58 root: tree.root,
59 mtp: siblings,
60 hi: e1.hi,
61 hv: e1.hv,
62 };
63
64 const compiledCircuit = await compiler(
65 circuitFileName,
66 { reduceConstraints: false }
67 );
68
69 const circuit = new snarkjs.Circuit(compiledCircuit);
70 const witness = circuit.calculateWitness(input);
71 circuit.checkWitness(witness);
72 }
73
74 it("TestSmts", async () => {
75
76 const e1 = {
77 hi: bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839"),
78 hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
79 };
80
81 const e2ok = {
82 hi: bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262"),
83 hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
84 };
85
86 const e2fail = {
87 hi: bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579"),
88 hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
89 };
90
91 console.log("test e1, e2ok");
92 await testsmt3(e1, e2ok);
93
94 console.log("test e1, e2fail");
95 await testsmt3(e1, e2fail);
96 });
97});
98