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