|
@ -1,6 +1,6 @@ |
|
|
const chai = require("chai"); |
|
|
const chai = require("chai"); |
|
|
|
|
|
|
|
|
const Fr = require("ffjavascript").bn128.Fr; |
|
|
|
|
|
|
|
|
const Scalar = require("ffjavascript").Scalar; |
|
|
|
|
|
|
|
|
const smt = require("circomlib").smt; |
|
|
const smt = require("circomlib").smt; |
|
|
// const smt = require("../../iden3/circomlib/src/smt.js");
|
|
|
// const smt = require("../../iden3/circomlib/src/smt.js");
|
|
@ -16,67 +16,67 @@ describe("SMT javascript testvectors", function () { |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
console.log(tree); |
|
|
console.log(tree); |
|
|
|
|
|
|
|
|
let k = Fr.e(1); |
|
|
|
|
|
let v = Fr.e(2); |
|
|
|
|
|
|
|
|
let k = Scalar.e(1); |
|
|
|
|
|
let v = Scalar.e(2); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
console.log("insert(1,2)", tree.root); |
|
|
console.log("insert(1,2)", tree.root); |
|
|
|
|
|
|
|
|
k = Fr.e(33); |
|
|
|
|
|
v = Fr.e(44); |
|
|
|
|
|
|
|
|
k = Scalar.e(33); |
|
|
|
|
|
v = Scalar.e(44); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
console.log("insert(33, 44)", tree.root); |
|
|
console.log("insert(33, 44)", tree.root); |
|
|
|
|
|
|
|
|
k = Fr.e(1234); |
|
|
|
|
|
v = Fr.e(9876); |
|
|
|
|
|
|
|
|
k = Scalar.e(1234); |
|
|
|
|
|
v = Scalar.e(9876); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
console.log("insert(1234, 9876)", tree.root); |
|
|
console.log("insert(1234, 9876)", tree.root); |
|
|
|
|
|
|
|
|
await tree.delete(Fr.e(33)); |
|
|
|
|
|
|
|
|
await tree.delete(Scalar.e(33)); |
|
|
console.log("delete(33)", tree.root); |
|
|
console.log("delete(33)", tree.root); |
|
|
|
|
|
|
|
|
await tree.delete(Fr.e(1234)); |
|
|
|
|
|
await tree.delete(Fr.e(1)); |
|
|
|
|
|
|
|
|
await tree.delete(Scalar.e(1234)); |
|
|
|
|
|
await tree.delete(Scalar.e(1)); |
|
|
console.log("delete(1234 & 1)", tree.root); |
|
|
console.log("delete(1234 & 1)", tree.root); |
|
|
|
|
|
|
|
|
assert(Fr.isZero(tree.root)); |
|
|
|
|
|
|
|
|
assert(Scalar.isZero(tree.root)); |
|
|
}); |
|
|
}); |
|
|
it("TestDelete3", async () => { |
|
|
it("TestDelete3", async () => { |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
console.log(tree); |
|
|
console.log(tree); |
|
|
|
|
|
|
|
|
let k = Fr.e(1); |
|
|
|
|
|
let v = Fr.e(1); |
|
|
|
|
|
|
|
|
let k = Scalar.e(1); |
|
|
|
|
|
let v = Scalar.e(1); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
|
|
|
|
|
|
k = Fr.e(2); |
|
|
|
|
|
v = Fr.e(2); |
|
|
|
|
|
|
|
|
k = Scalar.e(2); |
|
|
|
|
|
v = Scalar.e(2); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
console.log("insert(2,2)", tree.root); |
|
|
console.log("insert(2,2)", tree.root); |
|
|
|
|
|
|
|
|
console.log("TestDelete3", tree.root); |
|
|
console.log("TestDelete3", tree.root); |
|
|
await tree.delete(Fr.e(1)); |
|
|
|
|
|
|
|
|
await tree.delete(Scalar.e(1)); |
|
|
console.log("TestDelete3", tree.root); |
|
|
console.log("TestDelete3", tree.root); |
|
|
}); |
|
|
}); |
|
|
it("TestDelete4", async () => { |
|
|
it("TestDelete4", async () => { |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
console.log(tree); |
|
|
console.log(tree); |
|
|
|
|
|
|
|
|
let k = Fr.e(1); |
|
|
|
|
|
let v = Fr.e(1); |
|
|
|
|
|
|
|
|
let k = Scalar.e(1); |
|
|
|
|
|
let v = Scalar.e(1); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
|
|
|
|
|
|
k = Fr.e(2); |
|
|
|
|
|
v = Fr.e(2); |
|
|
|
|
|
|
|
|
k = Scalar.e(2); |
|
|
|
|
|
v = Scalar.e(2); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
console.log(tree.root); |
|
|
console.log(tree.root); |
|
|
|
|
|
|
|
|
k = Fr.e(3); |
|
|
|
|
|
v = Fr.e(3); |
|
|
|
|
|
|
|
|
k = Scalar.e(3); |
|
|
|
|
|
v = Scalar.e(3); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
console.log(tree.root); |
|
|
console.log(tree.root); |
|
|
|
|
|
|
|
|
console.log("TestDelete4", tree.root); |
|
|
console.log("TestDelete4", tree.root); |
|
|
let res = await tree.delete(Fr.e(1)); |
|
|
|
|
|
|
|
|
let res = await tree.delete(Scalar.e(1)); |
|
|
console.log("TestDelete4 res", res); |
|
|
console.log("TestDelete4 res", res); |
|
|
console.log("TestDelete4", tree.root); |
|
|
console.log("TestDelete4", tree.root); |
|
|
}); |
|
|
}); |
|
@ -84,39 +84,39 @@ describe("SMT javascript testvectors", function () { |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
console.log(tree); |
|
|
console.log(tree); |
|
|
|
|
|
|
|
|
let k = Fr.e(1); |
|
|
|
|
|
let v = Fr.e(2); |
|
|
|
|
|
|
|
|
let k = Scalar.e(1); |
|
|
|
|
|
let v = Scalar.e(2); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
|
|
|
|
|
|
k = Fr.e(33); |
|
|
|
|
|
v = Fr.e(44); |
|
|
|
|
|
|
|
|
k = Scalar.e(33); |
|
|
|
|
|
v = Scalar.e(44); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
console.log(tree.root); |
|
|
console.log(tree.root); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log("TestDelete5", tree.root); |
|
|
console.log("TestDelete5", tree.root); |
|
|
await tree.delete(Fr.e(1)); |
|
|
|
|
|
|
|
|
await tree.delete(Scalar.e(1)); |
|
|
console.log("TestDelete5", tree.root); |
|
|
console.log("TestDelete5", tree.root); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
it("TestAddAndGetCircomProof", async () => { |
|
|
it("TestAddAndGetCircomProof", async () => { |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
const tree = await smt.newMemEmptyTrie(); |
|
|
|
|
|
|
|
|
let key = Fr.e(1); |
|
|
|
|
|
let value = Fr.e(2); |
|
|
|
|
|
|
|
|
let key = Scalar.e(1); |
|
|
|
|
|
let value = Scalar.e(2); |
|
|
|
|
|
|
|
|
let res = await tree.insert(key,value); |
|
|
let res = await tree.insert(key,value); |
|
|
res = fixRes(res); |
|
|
res = fixRes(res); |
|
|
console.log(res); |
|
|
console.log(res); |
|
|
|
|
|
|
|
|
key = Fr.e(33); |
|
|
|
|
|
value = Fr.e(44); |
|
|
|
|
|
|
|
|
key = Scalar.e(33); |
|
|
|
|
|
value = Scalar.e(44); |
|
|
res = await tree.insert(key,value); |
|
|
res = await tree.insert(key,value); |
|
|
res = fixRes(res); |
|
|
res = fixRes(res); |
|
|
console.log(res); |
|
|
console.log(res); |
|
|
|
|
|
|
|
|
key = Fr.e(55); |
|
|
|
|
|
value = Fr.e(66); |
|
|
|
|
|
|
|
|
key = Scalar.e(55); |
|
|
|
|
|
value = Scalar.e(66); |
|
|
res = await tree.insert(key,value); |
|
|
res = await tree.insert(key,value); |
|
|
res = fixRes(res); |
|
|
res = fixRes(res); |
|
|
console.log(res); |
|
|
console.log(res); |
|
@ -127,13 +127,13 @@ describe("SMT javascript testvectors", function () { |
|
|
console.log(tree); |
|
|
console.log(tree); |
|
|
|
|
|
|
|
|
for (let i=0; i<16; i++) { |
|
|
for (let i=0; i<16; i++) { |
|
|
let k = Fr.e(i); |
|
|
|
|
|
let v = Fr.e(i*2); |
|
|
|
|
|
|
|
|
let k = Scalar.e(i); |
|
|
|
|
|
let v = Scalar.e(i*2); |
|
|
await tree.insert(k,v); |
|
|
await tree.insert(k,v); |
|
|
} |
|
|
} |
|
|
console.log(tree.root); |
|
|
console.log(tree.root); |
|
|
k = Fr.e(10); |
|
|
|
|
|
v = Fr.e(1024); |
|
|
|
|
|
|
|
|
k = Scalar.e(10); |
|
|
|
|
|
v = Scalar.e(1024); |
|
|
let res = await tree.update(k, v) |
|
|
let res = await tree.update(k, v) |
|
|
console.log(res); |
|
|
console.log(res); |
|
|
|
|
|
|
|
@ -141,7 +141,7 @@ describe("SMT javascript testvectors", function () { |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
function fixRes(res) { |
|
|
function fixRes(res) { |
|
|
while (res.siblings.length<10) res.siblings.push(Fr.e(0)); |
|
|
|
|
|
|
|
|
while (res.siblings.length<10) res.siblings.push(Scalar.e(0)); |
|
|
res.oldKey = res.isOld0 ? 0 : res.oldKey; |
|
|
res.oldKey = res.isOld0 ? 0 : res.oldKey; |
|
|
res.oldValue = res.isOld0 ? 0 : res.oldValue; |
|
|
res.oldValue = res.isOld0 ? 0 : res.oldValue; |
|
|
res.isOld0 = res.isOld0 ? 1 : 0; |
|
|
res.isOld0 = res.isOld0 ? 1 : 0; |
|
|