circomlib/test/smtprocessor.js

209 lines
6.5 KiB
JavaScript
Raw Normal View History

2018-12-11 19:25:21 +03:00
const chai = require("chai");
const path = require("path");
2019-12-14 22:32:45 +03:00
const tester = require("circom").tester;
const Fr = require("ffjavascript").bn128.Fr;
2018-12-11 19:25:21 +03:00
const smt = require("../src/smt.js");
const assert = chai.assert;
function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}
2020-03-26 21:24:20 +03:00
async function testInsert(tree, key, value, circuit ) {
2018-12-13 21:53:32 +03:00
const res = await tree.insert(key,value);
2018-12-14 16:24:30 +03:00
let siblings = res.siblings;
while (siblings.length<10) siblings.push(Fr.e(0));
2018-12-13 21:53:32 +03:00
2019-12-14 22:32:45 +03:00
const w = await circuit.calculateWitness({
2018-12-13 21:53:32 +03:00
fnc: [1,0],
oldRoot: res.oldRoot,
siblings: siblings,
2018-12-15 11:00:35 +03:00
oldKey: res.isOld0 ? 0 : res.oldKey,
oldValue: res.isOld0 ? 0 : res.oldValue,
2018-12-13 21:53:32 +03:00
isOld0: res.isOld0 ? 1 : 0,
newKey: key,
newValue: value
2020-03-26 21:24:20 +03:00
}, true);
2018-12-13 21:53:32 +03:00
2019-12-16 23:35:52 +03:00
await circuit.checkConstraints(w);
2019-12-14 22:32:45 +03:00
await circuit.assertOut(w, {newRoot: res.newRoot});
2018-12-13 21:53:32 +03:00
}
async function testDelete(tree, key, circuit) {
const res = await tree.delete(key);
2018-12-14 16:24:30 +03:00
let siblings = res.siblings;
while (siblings.length<10) siblings.push(Fr.e(0));
2018-12-13 21:53:32 +03:00
2019-12-14 22:32:45 +03:00
const w = await circuit.calculateWitness({
2018-12-13 21:53:32 +03:00
fnc: [1,1],
oldRoot: res.oldRoot,
siblings: siblings,
2018-12-15 11:00:35 +03:00
oldKey: res.isOld0 ? 0 : res.oldKey,
oldValue: res.isOld0 ? 0 : res.oldValue,
2018-12-13 21:53:32 +03:00
isOld0: res.isOld0 ? 1 : 0,
newKey: res.delKey,
newValue: res.delValue
2020-03-26 21:24:20 +03:00
}, true);
2018-12-13 21:53:32 +03:00
2019-12-16 23:35:52 +03:00
await circuit.checkConstraints(w);
2018-12-13 21:53:32 +03:00
2019-12-14 22:32:45 +03:00
await circuit.assertOut(w, {newRoot: res.newRoot});
2018-12-13 21:53:32 +03:00
}
2018-12-14 16:24:30 +03:00
async function testUpdate(tree, key, newValue, circuit) {
const res = await tree.update(key, newValue);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(Fr.e(0));
2018-12-14 16:24:30 +03:00
2019-12-14 22:32:45 +03:00
const w = await circuit.calculateWitness({
2018-12-14 16:24:30 +03:00
fnc: [0,1],
oldRoot: res.oldRoot,
siblings: siblings,
oldKey: res.oldKey,
oldValue: res.oldValue,
isOld0: 0,
newKey: res.newKey,
newValue: res.newValue
});
2019-12-16 23:35:52 +03:00
await circuit.checkConstraints(w);
2018-12-14 16:24:30 +03:00
2019-12-14 22:32:45 +03:00
await circuit.assertOut(w, {newRoot: res.newRoot});
2018-12-14 16:24:30 +03:00
}
2018-12-11 19:25:21 +03:00
2019-12-17 19:12:29 +03:00
describe("SMT Processor test", function () {
2018-12-11 19:25:21 +03:00
let circuit;
let tree;
2019-06-04 18:32:28 +03:00
this.timeout(10000000);
2018-12-11 19:25:21 +03:00
before( async () => {
2019-12-14 22:32:45 +03:00
circuit = await tester(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
await circuit.loadSymbols();
2018-12-11 19:25:21 +03:00
tree = await smt.newMemEmptyTrie();
});
it("Should verify an insert to an empty tree", async () => {
const key = Fr.e(111);
const value = Fr.e(222);
2018-12-11 19:25:21 +03:00
2018-12-13 21:53:32 +03:00
await testInsert(tree, key, value, circuit);
2018-12-11 19:25:21 +03:00
});
it("It should add another element", async () => {
const key = Fr.e(333);
const value = Fr.e(444);
2018-12-11 19:25:21 +03:00
2018-12-13 21:53:32 +03:00
await testInsert(tree, key, value, circuit);
});
it("Should remove an element", async () => {
await testDelete(tree, 111, circuit);
await testDelete(tree, 333, circuit);
});
it("Should test convination of adding and removing 3 elements", async () => {
const keys = [Fr.e(8), Fr.e(9), Fr.e(32)];
const values = [Fr.e(88), Fr.e(99), Fr.e(3232)];
2018-12-13 21:53:32 +03:00
const tree1 = await smt.newMemEmptyTrie();
const tree2 = await smt.newMemEmptyTrie();
const tree3 = await smt.newMemEmptyTrie();
const tree4 = await smt.newMemEmptyTrie();
const tree5 = await smt.newMemEmptyTrie();
const tree6 = await smt.newMemEmptyTrie();
await testInsert(tree1,keys[0],values[0], circuit);
2018-12-13 23:04:37 +03:00
await testInsert(tree1,keys[1],values[1], circuit);
await testInsert(tree1,keys[2],values[2], circuit);
2018-12-13 21:53:32 +03:00
await testInsert(tree2,keys[0],values[0], circuit);
await testInsert(tree2,keys[2],values[2], circuit);
await testInsert(tree2,keys[1],values[1], circuit);
await testInsert(tree3,keys[1],values[1], circuit);
await testInsert(tree3,keys[0],values[0], circuit);
await testInsert(tree3,keys[2],values[2], circuit);
await testInsert(tree4,keys[1],values[1], circuit);
await testInsert(tree4,keys[2],values[2], circuit);
await testInsert(tree4,keys[0],values[0], circuit);
await testInsert(tree5,keys[2],values[2], circuit);
await testInsert(tree5,keys[0],values[0], circuit);
await testInsert(tree5,keys[1],values[1], circuit);
await testInsert(tree6,keys[2],values[2], circuit);
await testInsert(tree6,keys[1],values[1], circuit);
await testInsert(tree6,keys[0],values[0], circuit);
await testDelete(tree1, keys[0], circuit);
await testDelete(tree1, keys[1], circuit);
await testDelete(tree2, keys[1], circuit);
await testDelete(tree2, keys[0], circuit);
await testDelete(tree3, keys[0], circuit);
await testDelete(tree3, keys[2], circuit);
await testDelete(tree4, keys[2], circuit);
await testDelete(tree4, keys[0], circuit);
await testDelete(tree5, keys[1], circuit);
await testDelete(tree5, keys[2], circuit);
await testDelete(tree6, keys[2], circuit);
await testDelete(tree6, keys[1], circuit);
await testDelete(tree1, keys[2], circuit);
await testDelete(tree2, keys[2], circuit);
await testDelete(tree3, keys[1], circuit);
await testDelete(tree4, keys[1], circuit);
await testDelete(tree5, keys[0], circuit);
2018-12-13 23:04:37 +03:00
await testDelete(tree6, keys[0], circuit);
2018-12-13 21:53:32 +03:00
});
2018-12-11 19:25:21 +03:00
2018-12-13 21:53:32 +03:00
it("Should match a NOp with random vals", async () => {
let siblings = [];
while (siblings.length<10) siblings.push(Fr.e(88));
2019-12-14 22:32:45 +03:00
const w = await circuit.calculateWitness({
2018-12-13 21:53:32 +03:00
fnc: [0,0],
oldRoot: 11,
2018-12-11 19:25:21 +03:00
siblings: siblings,
2018-12-13 21:53:32 +03:00
oldKey: 33,
oldValue: 44,
isOld0: 55,
newKey: 66,
newValue: 77
2018-12-11 19:25:21 +03:00
});
2020-03-26 21:24:20 +03:00
const root1 = w[circuit.symbols["main.oldRoot"].varIdx];
const root2 = w[circuit.symbols["main.newRoot"].varIdx];
2018-12-23 01:54:25 +03:00
2019-12-16 23:35:52 +03:00
await circuit.checkConstraints(w);
2018-12-13 21:53:32 +03:00
assert(Fr.eq(root1, root2));
2018-12-13 21:53:32 +03:00
});
it("Should update an element", async () => {
2018-12-14 16:24:30 +03:00
const tree1 = await smt.newMemEmptyTrie();
const tree2 = await smt.newMemEmptyTrie();
await testInsert(tree1,8,88, circuit);
await testInsert(tree1,9,99, circuit);
await testInsert(tree1,32,3232, circuit);
await testInsert(tree2,8,888, circuit);
await testInsert(tree2,9,999, circuit);
await testInsert(tree2,32,323232, circuit);
2018-12-13 21:53:32 +03:00
2018-12-14 16:24:30 +03:00
await testUpdate(tree1, 8, 888, circuit);
await testUpdate(tree1, 9, 999, circuit);
await testUpdate(tree1, 32, 323232, circuit);
2018-12-13 21:53:32 +03:00
});
2018-12-11 19:25:21 +03:00
});