working with some errors yet

This commit is contained in:
Jordi Baylina
2018-12-13 19:53:32 +01:00
parent 38fc4b7396
commit ccaa7ff23b
11 changed files with 24599 additions and 150 deletions

View File

@@ -13,6 +13,52 @@ function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}
async function testInsert(tree, key, value, circuit, log ) {
const res = await tree.insert(key,value);
let siblings = res.sibblings;
while (siblings.length<10) siblings.push(bigInt(0));
const w = circuit.calculateWitness({
fnc: [1,0],
oldRoot: res.oldRoot,
newRoot: res.newRoot,
siblings: siblings,
oldKey: res.oldKey,
oldValue: res.oldValue,
isOld0: res.isOld0 ? 1 : 0,
newKey: key,
newValue: value
}, log);
const root1 = w[circuit.getSignalIdx("main.topSwitcher.outR")];
assert(circuit.checkWitness(w));
assert(root1.equals(res.newRoot));
}
async function testDelete(tree, key, circuit) {
const res = await tree.delete(key);
let siblings = res.sibblings;
while (siblings.length<10) siblings.push(bigInt(0));
const w = circuit.calculateWitness({
fnc: [1,1],
oldRoot: res.oldRoot,
newRoot: res.newRoot,
siblings: siblings,
oldKey: res.oldKey,
oldValue: res.oldValue,
isOld0: res.isOld0 ? 1 : 0,
newKey: res.delKey,
newValue: res.delValue
});
const root1 = w[circuit.getSignalIdx("main.topSwitcher.outR")];
assert(circuit.checkWitness(w));
assert(root1.equals(res.newRoot));
}
describe("SMT test", function () {
let circuit;
@@ -34,55 +80,110 @@ describe("SMT test", function () {
const key = bigInt(111);
const value = bigInt(222);
const res = await tree.insert(key,value);
let siblings = res.sibblings;
while (siblings.length<10) siblings.push(bigInt(0));
const w = circuit.calculateWitness({
oldRoot: res.oldRoot,
newRoot: res.newRoot,
siblings: siblings,
oldKey: res.oldKey,
oldValue: res.oldValue,
isOld0: res.isOld0 ? 1 : 0,
newKey: key,
newValue: value
});
const root1 = w[circuit.getSignalIdx("main.levels[0].newRoot")];
console.log("root1: " + root1.toString());
console.log("root2: " + res.newRoot.toString());
assert(root1.equals(res.newRoot));
assert(circuit.checkWitness(w));
await testInsert(tree, key, value, circuit);
});
it("It should add another element", async () => {
const key = bigInt(333);
const value = bigInt(444);
const res = await tree.insert(key,value);
let siblings = res.sibblings;
while (siblings.length<10) siblings.push(bigInt(0));
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 = [bigInt(8), bigInt(9), bigInt(32)];
const values = [bigInt(88), bigInt(99), bigInt(3232)];
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);
await testInsert(tree1,keys[1],values[1], circuit, console.log);
/* await testInsert(tree1,keys[2],values[2], circuit);
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);
await testDelete(tree6, keys[0], circuit); */
});
it("Should match a NOp with random vals", async () => {
let siblings = [];
while (siblings.length<10) siblings.push(bigInt(88));
const w = circuit.calculateWitness({
oldRoot: res.oldRoot,
newRoot: res.newRoot,
fnc: [0,0],
oldRoot: 11,
newRoot: 22,
siblings: siblings,
oldKey: res.oldKey,
oldValue: res.oldValue,
isOld0: res.isOld0 ? 1 : 0,
newKey: key,
newValue: value
oldKey: 33,
oldValue: 44,
isOld0: 55,
newKey: 66,
newValue: 77
});
const root1 = w[circuit.getSignalIdx("main.levels[0].newRoot")];
console.log("root1: " + root1.toString());
console.log("root2: " + res.newRoot.toString());
assert(root1.equals(res.newRoot));
assert(circuit.checkWitness(w));
});
it("Should update an element", async () => {
});
it("Should verify existance of an element", async () => {
});
it("Should verify non existance of an element", async () => {
});
});

163
test/smtjs.js Normal file
View File

@@ -0,0 +1,163 @@
const chai = require("chai");
const snarkjs = require("snarkjs");
const smt = require("../src/smt.js");
const assert = chai.assert;
const bigInt = snarkjs.bigInt;
function stringifyBigInts(o) {
if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
return o.toString(10);
} else if (Array.isArray(o)) {
return o.map(stringifyBigInts);
} else if (typeof o == "object") {
const res = {};
for (let k in o) {
res[k] = stringifyBigInts(o[k]);
}
return res;
} else {
return o;
}
}
describe("SMT Javascript test", function () {
this.timeout(100000);
before( async () => {
});
it("Should insert 2 elements and empty them", async () => {
const tree = await smt.newMemEmptyTrie();
const key1 = bigInt(111);
const value1 = bigInt(222);
const key2 = bigInt(333);
const value2 = bigInt(444);
await tree.insert(key1,value1);
await tree.insert(key2,value2);
await tree.delete(key2);
await tree.delete(key1);
assert(tree.root.isZero());
});
it("Should insert 3 elements in dferent order and should be the same", async () => {
const keys = [bigInt(8), bigInt(9), bigInt(32)];
const values = [bigInt(88), bigInt(99), bigInt(3232)];
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 tree1.insert(keys[0],values[0]);
await tree1.insert(keys[1],values[1]);
await tree1.insert(keys[2],values[2]);
await tree2.insert(keys[0],values[0]);
await tree2.insert(keys[2],values[2]);
await tree2.insert(keys[1],values[1]);
await tree3.insert(keys[1],values[1]);
await tree3.insert(keys[0],values[0]);
await tree3.insert(keys[2],values[2]);
await tree4.insert(keys[1],values[1]);
await tree4.insert(keys[2],values[2]);
await tree4.insert(keys[0],values[0]);
await tree5.insert(keys[2],values[2]);
await tree5.insert(keys[0],values[0]);
await tree5.insert(keys[1],values[1]);
await tree6.insert(keys[2],values[2]);
await tree6.insert(keys[1],values[1]);
await tree6.insert(keys[0],values[0]);
assert(tree1.root.equals(tree2.root));
assert(tree2.root.equals(tree3.root));
assert(tree3.root.equals(tree4.root));
assert(tree4.root.equals(tree5.root));
assert(tree5.root.equals(tree6.root));
assert.equal(Object.keys(tree1.db.nodes).length, Object.keys(tree2.db.nodes).length);
assert.equal(Object.keys(tree2.db.nodes).length, Object.keys(tree3.db.nodes).length);
assert.equal(Object.keys(tree3.db.nodes).length, Object.keys(tree4.db.nodes).length);
assert.equal(Object.keys(tree4.db.nodes).length, Object.keys(tree5.db.nodes).length);
assert.equal(Object.keys(tree5.db.nodes).length, Object.keys(tree6.db.nodes).length);
await tree1.delete(keys[0]);
await tree1.delete(keys[1]);
await tree2.delete(keys[1]);
await tree2.delete(keys[0]);
assert(tree1.root.equals(tree2.root));
await tree3.delete(keys[0]);
await tree3.delete(keys[2]);
await tree4.delete(keys[2]);
await tree4.delete(keys[0]);
assert(tree3.root.equals(tree4.root));
await tree5.delete(keys[1]);
await tree5.delete(keys[2]);
await tree6.delete(keys[2]);
await tree6.delete(keys[1]);
assert(tree5.root.equals(tree6.root));
await tree1.delete(keys[2]);
await tree2.delete(keys[2]);
await tree3.delete(keys[1]);
await tree4.delete(keys[1]);
await tree5.delete(keys[0]);
await tree6.delete(keys[0]);
assert(tree1.root.isZero());
assert(tree2.root.isZero());
assert(tree3.root.isZero());
assert(tree4.root.isZero());
assert(tree5.root.isZero());
assert(tree6.root.isZero());
assert.equal(Object.keys(tree1.db.nodes).length, 0);
assert.equal(Object.keys(tree2.db.nodes).length, 0);
assert.equal(Object.keys(tree3.db.nodes).length, 0);
assert.equal(Object.keys(tree4.db.nodes).length, 0);
assert.equal(Object.keys(tree5.db.nodes).length, 0);
assert.equal(Object.keys(tree6.db.nodes).length, 0);
});
it("Insert and remove 100 numbers randomly", async () => {
function perm(a) {
const arr = a.slice();
const rArr = [];
for (let i=0; i<arr.length; i++) {
let rIdx = Math.floor(Math.random() * (arr.length - i));
rArr.push(arr[rIdx]);
arr[rIdx] = arr[arr.length - i - 1];
}
return rArr;
}
const tree = await smt.newMemEmptyTrie();
const arr = [];
const N = 100;
for (let i=0; i<N; i++) {
arr.push(bigInt(i));
}
const insArr = perm(arr);
for (let i=0; i<N; i++) {
await tree.insert(insArr[i], i);
}
const delArr = perm(insArr);
for (let i=0; i<N; i++) {
await tree.delete(delArr[i]);
}
assert(tree.root.isZero());
assert.equal(Object.keys(tree.db.nodes).length, 0);
});
});