1 Commits

Author SHA1 Message Date
Eduard S
5bc12a2cb8 WIP smt test 2019-01-15 15:13:19 +01:00
20 changed files with 870 additions and 516 deletions

View File

@@ -1,15 +1,14 @@
const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt;
const createBlakeHash = require("blake-hash");
const assert = require("assert");
const babyJub = require("../src/babyjub");
function getPoint(S) {
const F = bn128.Fr;
const h = createBlakeHash("blake256").update(S).digest();
if (h.length != 32) {
throw new Error("Invalid length")
}
assert(h.length == 32);
let sign = false;
if (h[31] & 0x80) {
@@ -53,9 +52,7 @@ function generatePoint(S) {
p = getPoint(S+"_"+sidx);
idx++;
}
if (!babyJub.inCurve(p)){
throw new Error("Point not in curve");
}
assert(babyJub.inCurve(p), "Point not in curve");
return p;
}

View File

@@ -58,7 +58,6 @@ template EdDSAMiMCVerifier() {
hash.in[2] <== Ax;
hash.in[3] <== Ay;
hash.in[4] <== M;
hash.k <== 0;
component h2bits = Num2Bits_strict();
h2bits.in <== hash.out;

View File

@@ -137,19 +137,18 @@ template MiMC7(nrounds) {
template MultiMiMC7(nInputs, nRounds) {
signal input in[nInputs];
signal input k;
signal output out;
signal r[nInputs +1];
component mims[nInputs];
r[0] <== k;
for (var i=0; i<nInputs; i++) {
mims[i] = MiMC7(nRounds);
mims[i].x_in <== in[i];
mims[i].k <== r[i];
r[i+1] <== r[i] + in[i] + mims[i].out;
if (i==0) {
mims[i].x_in <== 15021630795539610737508582392395901278341266317943626182700664337106830745361;
} else {
mims[i].x_in <== mims[i-1].out;
}
mims[i].k <== in[i];
}
out <== r[nInputs];
out <== mims[nInputs-1].out;
}

View File

@@ -29,12 +29,19 @@ template SMTHash1() {
signal input value;
signal output out;
component h = MultiMiMC7(2, 91); // Constant
h.in[0] <== key;
h.in[1] <== value;
h.k <== 1;
component h1 = MiMC7(91); // Constant
h1.x_in <== 15021630795539610737508582392395901278341266317943626182700664337106830745361;
h1.k <== 1;
out <== h.out;
component h2 = MiMC7(91);
h2.x_in <== h1.out;
h2.k <== key;
component h3 = MiMC7(91);
h3.x_in <== h2.out;
h3.k <== value;
out <== h3.out;
}
/*
@@ -48,10 +55,13 @@ template SMTHash2() {
signal input R;
signal output out;
component h = MultiMiMC7(2, 91); // Constant
h.in[0] <== L;
h.in[1] <== R;
h.k <== 0;
component h1 = MiMC7(91);
h1.x_in <== 15021630795539610737508582392395901278341266317943626182700664337106830745361;
h1.k <== L;
out <== h.out;
component h2 = MiMC7(91);
h2.x_in <== h1.out;
h2.k <== R;
out <== h2.out;
}

Binary file not shown.

Binary file not shown.

View File

@@ -1,4 +1,3 @@
exports.smt = require("./src/smt");
exports.eddsa = require("./src/eddsa");
exports.mimc7 = require("./src/mimc7");
exports.babyJub = require("./src/babyjub");

774
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "circomlib",
"version": "0.0.6",
"version": "0.0.5",
"description": "Basic circuits library for Circom",
"main": "index.js",
"directories": {
@@ -25,7 +25,7 @@
"license": "GPL-3.0",
"dependencies": {
"blake-hash": "^1.1.0",
"snarkjs": "0.1.11",
"snarkjs": "0.1.9",
"web3": "^1.0.0-beta.36"
},
"devDependencies": {

View File

@@ -3,7 +3,9 @@ const bigInt = require("snarkjs").bigInt;
const babyJub = require("./babyjub");
const pedersenHash = require("./pedersenHash").hash;
const mimc7 = require("./mimc7");
const crypto = require("crypto");
exports.cratePrvKey = cratePrvKey;
exports.prv2pub= prv2pub;
exports.sign = sign;
exports.signMiMC = signMiMC;
@@ -11,9 +13,12 @@ exports.verify = verify;
exports.verifyMiMC = verifyMiMC;
exports.packSignature = packSignature;
exports.unpackSignature = unpackSignature;
exports.pruneBuffer = pruneBuffer;
function cratePrvKey() {
return crypto.randomBytes(32);
}
function pruneBuffer(_buff) {
const buff = Buffer.from(_buff);
buff[0] = buff[0] & 0xF8;

View File

@@ -3,7 +3,8 @@
//
const Web3Utils = require("web3-utils");
const Web3 = require("web3");
const assert = require("assert");
class Contract {
constructor() {
@@ -38,7 +39,7 @@ class Contract {
genLoadedLength = C.code.length;
}
return Web3Utils.bytesToHex(C.code.concat(this.code));
return Web3.utils.bytesToHex(C.code.concat(this.code));
}
stop() { this.code.push(0x00); }
@@ -140,9 +141,7 @@ class Contract {
msize() { this.code.push(0x59); }
gas() { this.code.push(0x5a); }
label(name) {
if (typeof this.labels[name] != "undefined") {
throw new Error("Label already defined");
}
assert(typeof this.labels[name] == "undefined", "Label already defined");
this.labels[name] = this.code.length;
this.code.push(0x5b);
@@ -150,24 +149,21 @@ class Contract {
}
push(data) {
const d = Web3Utils.hexToBytes(Web3Utils.toHex(data));
if (d.length == 0 || d.length > 32) {
throw new Error("Assertion failed");
}
const d = Web3.utils.hexToBytes(Web3.utils.toHex(data));
assert(d.length>0);
assert(d.length<=32);
this.code = this.code.concat([0x5F + d.length], d);
}
dup(n) {
if (n < 0 || n >= 16) {
throw new Error("Assertion failed");
}
assert(n>=0);
assert(n<16);
this.code.push(0x80 + n);
}
swap(n) {
if (n < 1 || n > 16) {
throw new Error("Assertion failed");
}
assert(n>=1);
assert(n<=16);
this.code.push(0x8f + n);
}

View File

@@ -1,6 +1,6 @@
const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt;
const Web3Utils = require("web3-utils");
const Web3 = require("web3");
const F = bn128.Fr;
const SEED = "mimc";
@@ -8,8 +8,8 @@ const NROUNDS = 91;
exports.getIV = (seed) => {
if (typeof seed === "undefined") seed = SEED;
const c = Web3Utils.keccak256(seed+"_iv");
const cn = bigInt(Web3Utils.toBN(c).toString());
const c = Web3.utils.keccak256(seed+"_iv");
const cn = bigInt(Web3.utils.toBN(c).toString());
const iv = cn.mod(F.q);
return iv;
};
@@ -18,13 +18,13 @@ exports.getConstants = (seed, nRounds) => {
if (typeof seed === "undefined") seed = SEED;
if (typeof nRounds === "undefined") nRounds = NROUNDS;
const cts = new Array(nRounds);
let c = Web3Utils.keccak256(SEED);
let c = Web3.utils.keccak256(SEED);
for (let i=1; i<nRounds; i++) {
c = Web3Utils.keccak256(c);
c = Web3.utils.keccak256(c);
const n1 = Web3Utils.toBN(c).mod(Web3Utils.toBN(F.q.toString()));
const c2 = Web3Utils.padLeft(Web3Utils.toHex(n1), 64);
cts[i] = bigInt(Web3Utils.toBN(c2).toString());
const n1 = Web3.utils.toBN(c).mod(Web3.utils.toBN(F.q.toString()));
const c2 = Web3.utils.padLeft(Web3.utils.toHex(n1), 64);
cts[i] = bigInt(Web3.utils.toBN(c2).toString());
}
cts[0] = bigInt(0);
return cts;
@@ -44,21 +44,10 @@ exports.hash = (_x_in, _k) =>{
return F.affine(F.add(r, k));
};
exports.multiHash = (arr, key) => {
let r;
if (typeof(key) === "undefined") {
r = F.zero;
} else {
r = key;
}
exports.multiHash = (arr) => {
let r = exports.getIV();
for (let i=0; i<arr.length; i++) {
r = F.add(
F.add(
r,
arr[i]
),
exports.hash(bigInt(arr[i]), r)
);
r = exports.hash(r, bigInt(arr[i]));
}
return F.affine(r);
return r;
};

View File

@@ -2,13 +2,13 @@
// License: LGPL-3.0+
//
const Web3Utils = require("web3-utils");
const Web3 = require("web3");
const Contract = require("./evmasm");
function createCode(seed, n) {
let ci = Web3Utils.keccak256(seed);
let ci = Web3.utils.keccak256(seed);
const C = new Contract();
@@ -51,7 +51,7 @@ function createCode(seed, n) {
C.mulmod(); // r=t^7 k q
for (let i=0; i<n-1; i++) {
ci = Web3Utils.keccak256(ci);
ci = Web3.utils.keccak256(ci);
C.dup(2); // q r k q
C.dup(0); // q q r k q
C.dup(0); // q q q r k q

View File

@@ -1,6 +1,7 @@
const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt;
const babyJub = require("./babyjub");
const assert = require("assert");
const createBlakeHash = require("blake-hash");
const GENPOINT_PREFIX = "PedersenGenerator";
@@ -72,9 +73,7 @@ function getBasePoint(pointIdx) {
const p8 = babyJub.mulPointEscalar(p, 8);
if (!babyJub.inSubgroup(p8)) {
throw new Error("Point not in curve");
}
assert(babyJub.inSubgroup(p8), "Point not in curve");
bases[pointIdx] = p8;
return p8;

View File

@@ -46,8 +46,8 @@ class SMT {
const ins = [];
const dels = [];
let rtOld = mimc7.multiHash([key, resFind.foundValue], bigInt.one);
let rtNew = mimc7.multiHash([key, newValue], bigInt.one);
let rtOld = mimc7.multiHash([1, key, resFind.foundValue]);
let rtNew = mimc7.multiHash([1, key, newValue]);
ins.push([rtNew, [1, key, newValue ]]);
dels.push(rtOld);
@@ -59,11 +59,11 @@ class SMT {
oldNode = [sibling, rtOld];
newNode = [sibling, rtNew];
} else {
oldNode = [rtOld, sibling];
newNode = [rtNew, sibling];
oldNode = [rtOld, sibling, ];
newNode = [rtNew, sibling, ];
}
rtOld = mimc7.multiHash(oldNode, bigInt.zero);
rtNew = mimc7.multiHash(newNode, bigInt.zero);
rtOld = mimc7.multiHash(oldNode);
rtNew = mimc7.multiHash(newNode);
dels.push(rtOld);
ins.push([rtNew, newNode]);
}
@@ -92,7 +92,7 @@ class SMT {
const dels = [];
const ins = [];
let rtOld = mimc7.multiHash([key, resFind.foundValue], bigInt.one);
let rtOld = mimc7.multiHash([1, key, resFind.foundValue]);
let rtNew;
dels.push(rtOld);
@@ -130,9 +130,9 @@ class SMT {
}
const oldSibling = resFind.siblings[level];
if (keyBits[level]) {
rtOld = mimc7.multiHash([oldSibling, rtOld], bigInt.zero);
rtOld = mimc7.multiHash([oldSibling, rtOld]);
} else {
rtOld = mimc7.multiHash([rtOld, oldSibling], bigInt.zero);
rtOld = mimc7.multiHash([rtOld, oldSibling]);
}
dels.push(rtOld);
if (!newSibling.isZero()) {
@@ -147,7 +147,7 @@ class SMT {
} else {
newNode = [rtNew, newSibling];
}
rtNew = mimc7.multiHash(newNode, bigInt.zero);
rtNew = mimc7.multiHash(newNode);
ins.push([rtNew, newNode]);
}
}
@@ -185,7 +185,7 @@ class SMT {
for (let i= res.siblings.length; oldKeyits[i] == newKeyBits[i]; i++) {
res.siblings.push(bigInt.zero);
}
rtOld = mimc7.multiHash([resFind.notFoundKey, resFind.notFoundValue], bigInt.one);
rtOld = mimc7.multiHash([1, resFind.notFoundKey, resFind.notFoundValue]);
res.siblings.push(rtOld);
addedOne = true;
mixed = false;
@@ -197,7 +197,7 @@ class SMT {
const inserts = [];
const dels = [];
let rt = mimc7.multiHash([key, value], bigInt.one);
let rt = mimc7.multiHash([1, key, value]);
inserts.push([rt,[1, key, value]] );
for (let i=res.siblings.length-1; i>=0; i--) {
@@ -207,9 +207,9 @@ class SMT {
if (mixed) {
const oldSibling = resFind.siblings[i];
if (newKeyBits[i]) {
rtOld = mimc7.multiHash([oldSibling, rtOld], bigInt.zero);
rtOld = mimc7.multiHash([oldSibling, rtOld]);
} else {
rtOld = mimc7.multiHash([rtOld, oldSibling], bigInt.zero);
rtOld = mimc7.multiHash([rtOld, oldSibling]);
}
dels.push(rtOld);
}
@@ -217,10 +217,10 @@ class SMT {
let newRt;
if (newKeyBits[i]) {
newRt = mimc7.multiHash([res.siblings[i], rt], bigInt.zero);
newRt = mimc7.multiHash([res.siblings[i], rt]);
inserts.push([newRt,[res.siblings[i], rt]] );
} else {
newRt = mimc7.multiHash([rt, res.siblings[i]], bigInt.zero);
newRt = mimc7.multiHash([rt, res.siblings[i]]);
inserts.push([newRt,[rt, res.siblings[i]]] );
}
rt = newRt;

View File

@@ -1,156 +0,0 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const compiler = require("circom");
const babyjub = require("../src/babyjub.js");
const assert = chai.assert;
// const bigInt = require("big-integer");
describe("Baby Jub js test", function () {
this.timeout(100000);
it("Should add point (0,1) and (0,1)", () => {
const p1 = [
snarkjs.bigInt(0),
snarkjs.bigInt(1)];
const p2 = [
snarkjs.bigInt(0),
snarkjs.bigInt(1)
];
const out = babyjub.addPoint(p1, p2)
assert(out[0].equals(0));
assert(out[1].equals(1));
});
it("Should add 2 same numbers", () => {
const p1 = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const p2 = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const out = babyjub.addPoint(p1, p2)
assert(out[0].equals(snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
assert(out[1].equals(snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
});
it("Should add 2 different numbers", () => {
const p1 = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const p2 = [
snarkjs.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
];
const out = babyjub.addPoint(p1, p2)
assert(out[0].equals(snarkjs.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
assert(out[1].equals(snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
});
it("should mulPointEscalar 0", () => {
const p = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("3"));
let r2 = babyjub.addPoint(p, p);
r2 = babyjub.addPoint(r2, p);
assert.equal(r2[0].toString(), r[0].toString());
assert.equal(r2[1].toString(), r[1].toString());
assert.equal(r[0].toString(), "19372461775513343691590086534037741906533799473648040012278229434133483800898");
assert.equal(r[1].toString(), "9458658722007214007257525444427903161243386465067105737478306991484593958249");
});
it("should mulPointEscalar 1", () => {
const p = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499"));
assert.equal(r[0].toString(), "17070357974431721403481313912716834497662307308519659060910483826664480189605");
assert.equal(r[1].toString(), "4014745322800118607127020275658861516666525056516280575712425373174125159339");
});
it("should mulPointEscalar 2", () => {
const p = [
snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"));
assert.equal(r[0].toString(), "13563888653650925984868671744672725781658357821216877865297235725727006259983");
assert.equal(r[1].toString(), "8442587202676550862664528699803615547505326611544120184665036919364004251662");
});
it("should inCurve 1", () => {
const p = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
assert(babyjub.inCurve(p));
});
it("should inCurve 2", () => {
const p = [
snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
assert(babyjub.inCurve(p));
});
it("should inSubgroup 1", () => {
const p = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
assert(babyjub.inSubgroup(p));
});
it("should inSubgroup 2", () => {
const p = [
snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
assert(babyjub.inSubgroup(p));
});
it("should packPoint - unpackPoint 1", () => {
const p = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const buf = babyjub.packPoint(p);
assert.equal(buf.toString('hex'), '53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85');
const p2 = babyjub.unpackPoint(buf);
assert.equal(p2[0].toString(), "17777552123799933955779906779655732241715742912184938656739573121738514868268");
assert.equal(p2[1].toString(), "2626589144620713026669568689430873010625803728049924121243784502389097019475");
});
it("should packPoint - unpackPoint 2", () => {
const p = [
snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
const buf = babyjub.packPoint(p);
assert.equal(buf.toString('hex'), 'e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709');
const p2 = babyjub.unpackPoint(buf);
assert.equal(p2[0].toString(), "6890855772600357754907169075114257697580319025794532037257385534741338397365");
assert.equal(p2[1].toString(), "4338620300185947561074059802482547481416142213883829469920100239455078257889");
});
});

View File

@@ -2,7 +2,6 @@ const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const compiler = require("circom");
// const crypto = require("crypto");
const eddsa = require("../src/eddsa.js");
const babyJub = require("../src/babyjub.js");
@@ -46,7 +45,7 @@ describe("EdDSA test", function () {
it("Sign a single 10 bytes from 0 to 9", async () => {
const msg = Buffer.from("00010203040506070809", "hex");
// const prvKey = crypto.randomBytes(32);
// const prvKey = eddsa.cratePrvKey();
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");

View File

@@ -1,49 +0,0 @@
const chai = require("chai");
const snarkjs = require("snarkjs");
const eddsa = require("../src/eddsa.js");
const babyJub = require("../src/babyjub.js");
const assert = chai.assert;
const bigInt = snarkjs.bigInt;
describe("EdDSA js test", function () {
this.timeout(100000);
it("Sign a single 10 bytes from 0 to 9", () => {
const msgBuf = Buffer.from("00010203040506070809", "hex");
const msg = bigInt.leBuff2int(msgBuf);
// const prvKey = crypto.randomBytes(32);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
const pubKey = eddsa.prv2pub(prvKey);
assert.equal(pubKey[0].toString(),
"2610057752638682202795145288373380503107623443963127956230801721756904484787");
assert.equal(pubKey[1].toString(),
"16617171478497210597712478520507818259149717466230047843969353176573634386897");
const pPubKey = babyJub.packPoint(pubKey);
const signature = eddsa.signMiMC(prvKey, msg);
assert.equal(signature.R8[0].toString(),
"4974729414807584049518234760796200867685098748448054182902488636762478901554");
assert.equal(signature.R8[1].toString(),
"18714049394522540751536514815950425694461287643205706667341348804546050128733");
assert.equal(signature.S.toString(),
"2171284143457722024136077617757713039502332290425057126942676527240038689549");
const pSignature = eddsa.packSignature(signature);
assert.equal(pSignature.toString("hex"), ""+
"5dfb6f843c023fe3e52548ccf22e55c81b426f7af81b4f51f7152f2fcfc65f29"+
"0dab19c5a0a75973cd75a54780de0c3a41ede6f57396fe99b5307fff3ce7cc04");
const uSignature = eddsa.unpackSignature(pSignature);
assert(eddsa.verifyMiMC(msg, uSignature, pubKey));
});
});

View File

@@ -9,7 +9,7 @@ const assert = chai.assert;
const bigInt = snarkjs.bigInt;
describe("EdDSA MiMC test", function () {
describe("EdDSA test", function () {
let circuit;
this.timeout(100000);

231
test/smttestvectors.js Normal file
View File

@@ -0,0 +1,231 @@
const chai = require("chai");
const snarkjs = require("snarkjs");
const smt = require("../src/smt.js");
const mimcjs = require("../src/mimc7.js");
const assert = chai.assert;
const expect = chai.expect;
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;
}
}
function newEntryFromInts(a, b, c, d) {
return {
hi: mimcjs.hash(c, d),
hv: mimcjs.hash(a, b),
};
}
function smtHash(arr) {
let r = bigInt(0);
for (let i=0; i<arr.length; i++) {
r = mimcjs.hash(r, bigInt(arr[i]), 91 );
}
return r;
}
describe("SMT Javascript test", function () {
this.timeout(100000);
before( async () => {
});
it("test MIMC7", async() => {
//const h = mimcjs.multiHash(1,2,3);
//const h = smtHash([1,2,3]);
//console.log(h.toString(10));
const mimcjs = require("../src/mimc7.js");
const snarkjs = require("snarkjs");
const bigInt = snarkjs.bigInt;
const smt = require("../src/smt.js");
h = mimcjs.hash(1,2,91);
console.log(h);
function smtHash(arr) {
let r = bigInt(0);
for (let i=0; i<arr.length; i++) {
r = mimcjs.hash(r, bigInt(arr[i]), 91 );
}
return r;
}
console.log(smtHash([1, 2, 3]));
// Test from old ver
});
//it("Should calculate hindex", async() => {
// const entry = newEntryFromInts(12, 45, 78, 41);
// //console.log(entry.hi.toString(16));
// expect(entry.hi.toString(16)).to.be.
// equal("114438e8321f62c4a1708f443a5a66f9c8fcb0958e7b7008332b71442610b7a0");
//});
//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);
//});
//it("Should test update", async () => {
// const tree1 = await smt.newMemEmptyTrie();
// const tree2 = await smt.newMemEmptyTrie();
// await tree1.insert(8,88);
// await tree1.insert(9,99,);
// await tree1.insert(32,3232);
// await tree2.insert(8,888);
// await tree2.insert(9,999);
// await tree2.insert(32,323232);
// await tree1.update(8, 888);
// await tree1.update(9, 999);
// await tree1.update(32, 323232);
// assert(tree1.root.equals(tree2.root));
//});
});