arbo/testvectors/circom/test/smt.test.ts
testvectors/circom/test/smt.test.tsRaw
1const fs = require("fs");
2const path = require("path");
3const tester = require("circom").tester;
4const assert = require('assert');
5const circomlib = require("circomlib");
6const smt = require("circomlib").smt;
7
8describe("merkletreetree circom-proof-verifier", function () {
9 this.timeout(0);
10 const nLevels = 4;
11
12 let circuit;
13 let circuitPath = path.join(__dirname, "circuits", "mt-proof-verifier-main.test.circom");
14 before( async() => {
15 const circuitCode = `
16 include "smt-proof-verifier_test.circom";
17 component main = SMTVerifierTest(4);
18 `;
19 fs.writeFileSync(circuitPath, circuitCode, "utf8");
20
21 circuit = await tester(circuitPath, {reduceConstraints:false});
22 await circuit.loadConstraints();
23 console.log("Constraints: " + circuit.constraints.length + "\n");
24 });
25
26 after( async() => {
27 fs.unlinkSync(circuitPath);
28 });
29
30 let inputsVerifier, inputsVerifierNonExistence;
31
32 before("generate smt-verifier js inputs", async () => {
33 let tree = await smt.newMemEmptyTrie();
34 await tree.insert(1, 11);
35 await tree.insert(2, 22);
36 await tree.insert(3, 33);
37 await tree.insert(4, 44);
38 const res = await tree.find(2);
39 assert(res.found);
40 let root = tree.root;
41
42 let siblings = res.siblings;
43 while (siblings.length < nLevels) {
44 siblings.push("0");
45 };
46
47 inputsVerifier = {
48 "fnc": 0,
49 "key": 2,
50 "value": 22,
51 "siblings": siblings,
52 "root": root,
53 };
54
55 const res2 = await tree.find(5);
56 assert(!res2.found);
57 let siblings2 = res2.siblings;
58 while (siblings2.length < nLevels) {
59 siblings2.push("0");
60 };
61 inputsVerifierNonExistence = {
62 "fnc": 1,
63 "oldKey": 1,
64 "oldValue": 11,
65 "key": 5,
66 "value": 11,
67 "siblings": siblings2,
68 "root": root,
69 };
70 });
71
72 it("Test smt-verifier proof of existence go inputs", async () => {
73 // fromGo is a json CircomVerifierProof generated from Go code using
74 // https://github.com/vocdoni/arbo
75 let rawdata = fs.readFileSync('go-data-generator/go-smt-verifier-inputs.json');
76 let fromGo = JSON.parse(rawdata);
77 inputsVerifier=fromGo;
78 // console.log("smtverifier js inputs:\n", inputsVerifier);
79
80 const witness = await circuit.calculateWitness(inputsVerifier);
81 await circuit.checkConstraints(witness);
82 });
83 it("Test smt-verifier proof of non-existence go inputs", async () => {
84 // fromGo is a json CircomVerifierProof generated from Go code using
85 // https://github.com/vocdoni/arbo
86 let rawdata = fs.readFileSync('go-data-generator/go-smt-verifier-non-existence-inputs.json');
87 let fromGo = JSON.parse(rawdata);
88 inputsVerifierNonExistence=fromGo;
89 // console.log("smtverifier js inputs:\n", inputsVerifierNonExistence);
90
91 const witness = await circuit.calculateWitness(inputsVerifierNonExistence);
92 await circuit.checkConstraints(witness);
93 });
94});