arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/smtverifier.js
test/smtverifier.jsRaw
 1const chai = require("chai");
 2const path = require("path");
 3const snarkjs = require("snarkjs");
 4const compiler = require("circom");
 5
 6const smt = require("../src/smt.js");
 7
 8const assert = chai.assert;
 9
10const bigInt = snarkjs.bigInt;
11
12function print(circuit, w, s) {
13    console.log(s + ": " + w[circuit.getSignalIdx(s)]);
14}
15
16async function testInclusion(tree, key, circuit) {
17
18    const res = await tree.find(key);
19
20    assert(res.found);
21    let siblings = res.siblings;
22    while (siblings.length<10) siblings.push(bigInt(0));
23
24    const w = circuit.calculateWitness({
25        fnc: 0,
26        root: tree.root,
27        siblings: siblings,
28        oldKey: 0,
29        oldValue: 0,
30        isOld0: 0,
31        key: key,
32        value: res.foundValue
33    });
34
35    assert(circuit.checkWitness(w));
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 = circuit.calculateWitness({
46        fnc: 1,
47        root: tree.root,
48        siblings: siblings,
49        oldKey: res.isOld0 ? 0 : res.notFoundKey,
50        oldValue: res.isOld0 ? 0 : res.notFoundValue,
51        isOld0: res.isOld0 ? 1 : 0,
52        key: key,
53        value: 0
54    });
55
56    assert(circuit.checkWitness(w));
57}
58
59describe("SMT test", function () {
60    let circuit;
61    let tree;
62
63    this.timeout(100000);
64
65    before( async () => {
66        const cirDef = await compiler(path.join(__dirname, "circuits", "smtverifier10_test.circom"));
67
68        circuit = new snarkjs.Circuit(cirDef);
69
70        console.log("NConstrains SMTVerifier: " + circuit.nConstraints);
71
72        tree = await smt.newMemEmptyTrie();
73        await tree.insert(7,77);
74        await tree.insert(8,88);
75        await tree.insert(32,3232);
76    });
77
78    it("Check inclussion in a tree of 3", async () => {
79        await testInclusion(tree, 7, circuit);
80        await testInclusion(tree, 8, circuit);
81        await testInclusion(tree, 32, circuit);
82    });
83
84    it("Check exclussion in a tree of 3", async () => {
85        await testExclusion(tree, 0, circuit);
86        await testExclusion(tree, 6, circuit);
87        await testExclusion(tree, 9, circuit);
88        await testExclusion(tree, 33, circuit);
89        await testExclusion(tree, 31, circuit);
90        await testExclusion(tree, 16, circuit);
91        await testExclusion(tree, 64, circuit);
92    });
93
94
95});