update smttestvectors.js

This commit is contained in:
krlosMata
2019-01-15 13:00:13 +01:00
parent 49244ea6b7
commit 130e3906fb
3 changed files with 439 additions and 697 deletions

3
.gitignore vendored
View File

@@ -60,6 +60,9 @@ typings/
# next.js build output # next.js build output
.next .next
# Visual code configuration
.vscode
tmp tmp
.DS_Store .DS_Store

751
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -9,6 +9,29 @@ const expect = chai.expect;
const bigInt = snarkjs.bigInt; const bigInt = snarkjs.bigInt;
const bytesToHex = function (buff) {
return `0x${buff.toString("hex")}`;
};
const hexToBytes = function (hex) {
if (hex.substr(0, 2) === "0x") {
return Buffer.from(hex.substr(2), "hex");
}
return Buffer.from(hex, "hex");
};
function bigIntToBuffer(number) {
const buff = Buffer.alloc(32);
let pos = buff.length - 1;
while (!number.isZero()) {
buff[pos] = number.and(255);
number = number.shiftRight(8);
pos -= 1;
}
return buff;
}
function stringifyBigInts(o) { function stringifyBigInts(o) {
if ((typeof(o) == "bigint") || (o instanceof bigInt)) { if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
return o.toString(10); return o.toString(10);
@@ -25,10 +48,10 @@ function stringifyBigInts(o) {
} }
} }
function newEntryFromInts(a, b, c, d) { function newEntryFromBigInts(a, b, c, d) {
return { return {
hi: mimcjs.hash(c, d), hi: mimcjs.multiHash([c, d]),
hv: mimcjs.hash(a, b), hv: mimcjs.multiHash([a, b]),
}; };
} }
@@ -40,41 +63,40 @@ function smtHash(arr) {
return r; return r;
} }
describe("SMT Javascript test", function () { describe("[sparse-merkle-tree] Javascript test", function () {
this.timeout(100000); this.timeout(100000);
before( async () => { before( async () => {
}); });
it("test MIMC7", async() => { it("Mimc7 hash", async () => {
//const h = mimcjs.multiHash(1,2,3); const entries = [bigInt(12), bigInt(45), bigInt(78), bigInt(41)];
//const h = smtHash([1,2,3]); const test = entries.slice(2);
//console.log(h.toString(10)); const hi = mimcjs.multiHash(entries.slice(2));
const hiHex = bytesToHex(bigIntToBuffer(hi));
const mimcjs = require("../src/mimc7.js"); //"0x0a546e62170e636895398e3831a7834affd35adad67016ff2d5215828e643aaf"
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); // it("Add one claim", async () => {
// //console.log(entry.hi.toString(16)); // const entries = newEntryFromBigInts(bigInt(12), bigInt(45), bigInt(78), bigInt(41));
// expect(entry.hi.toString(16)).to.be. // const tree = await smt.newMemEmptyTrie();
// equal("114438e8321f62c4a1708f443a5a66f9c8fcb0958e7b7008332b71442610b7a0"); // const key1 = entries.hi;
// const value1 = entries.hv;
// await tree.insert(key1,value1);
// const root = tree.root;
// const rootBuff = bigIntToBuffer(root);
// const rootHex = bytesToHex(rootBuff);
// //0x2e4bd3b61579f9ed4d814dfa4228c743b853fb6d0b5d6a80735bd5aab579231f
// assert(tree.root.isZero());
//});
});
// describe("SMT Javascript test", function () {
// this.timeout(100000);
// before( async () => {
// }); // });
// it("Should insert 2 elements and empty them", async () => { // it("Should insert 2 elements and empty them", async () => {
@@ -227,5 +249,5 @@ console.log(smtHash([1, 2, 3]));
// assert(tree1.root.equals(tree2.root)); // assert(tree1.root.equals(tree2.root));
// }); // });
}); // });