Browse Source

Add babyjubjub, mimc test vectors. Update imports

master
arnaucube 4 years ago
parent
commit
f5eda98c64
7 changed files with 620 additions and 2675 deletions
  1. +1
    -0
      README.md
  2. +509
    -2631
      package-lock.json
  3. +4
    -3
      package.json
  4. +37
    -0
      test/babyjubjub.test.js
  5. +30
    -0
      test/mimc.test.js
  6. +39
    -39
      test/mt.test.js
  7. +0
    -2
      test/poseidon.test.js

+ 1
- 0
README.md

@ -2,4 +2,5 @@
Test vectors to check compatible implementations of the [circomlib](https://github.com/iden3/circomlib), such as: Test vectors to check compatible implementations of the [circomlib](https://github.com/iden3/circomlib), such as:
- merkletree ([go](https://github.com/iden3/go-merkletree)) - merkletree ([go](https://github.com/iden3/go-merkletree))
- poseidon ([go](https://github.com/iden3/go-iden3-crypto/tree/master/poseidon), [rust](https://github.com/arnaucube/poseidon-rs)) - poseidon ([go](https://github.com/iden3/go-iden3-crypto/tree/master/poseidon), [rust](https://github.com/arnaucube/poseidon-rs))
- mimc ([go](https://github.com/iden3/go-iden3-crypto/tree/master/mimc7), [rust](https://github.com/arnaucube/mimc-rs))
- babyjubjub ([go](https://github.com/iden3/go-iden3-crypto/tree/master/babyjub), [rust](https://github.com/arnaucube/babyjubjub-rs)) - babyjubjub ([go](https://github.com/iden3/go-iden3-crypto/tree/master/babyjub), [rust](https://github.com/arnaucube/babyjubjub-rs))

+ 509
- 2631
package-lock.json
File diff suppressed because it is too large
View File


+ 4
- 3
package.json

@ -4,13 +4,14 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "mocha"
"test": "mocha",
"postinstall": "cd node_modules/circomlib && npm install && ../ffjavascript && npm install"
}, },
"author": "", "author": "",
"license": "GPLv3.0", "license": "GPLv3.0",
"dependencies": { "dependencies": {
"circomlib": "^0.2.3",
"ffjavascript": "^0.1.3"
"circomlib": "git+https://github.com/iden3/circomlib",
"ffjavascript": "git+https://github.com/iden3/ffjavascript"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^7.1.1" "mocha": "^7.1.1"

+ 37
- 0
test/babyjubjub.test.js

@ -0,0 +1,37 @@
const chai = require("chai");
const Scalar = require("ffjavascript").Scalar;
const utils = require("ffjavascript").utils;
// const babyjub = require("../../iden3/circomlib/src/babyjub.js");
const eddsa = require("circomlib").eddsa;
const babyjub = require("circomlib").babyJub;
const assert = chai.assert;
describe("BabyJubJub EDDSA javascript testvectors", function () {
it("BabyJubJub Keys test", async () => {
let sk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
let pk = eddsa.prv2pub(sk);
assert.equal(pk.toString("hex"), "13277427435165878497778222415993513565335242147425444199013288855685581939618,13622229784656158136036771217484571176836296686641868549125388198837476602820");
assert.equal(pk[0].toString(),
"13277427435165878497778222415993513565335242147425444199013288855685581939618");
assert.equal(pk[1].toString(),
"13622229784656158136036771217484571176836296686641868549125388198837476602820");
const msgBuf = Buffer.from("00010203040506070809", "hex");
const msg = utils.leBuff2int(msgBuf);
const signature = eddsa.signPoseidon(sk, msg);
assert.equal(signature.R8[0].toString(),
"11384336176656855268977457483345535180380036354188103142384839473266348197733");
assert.equal(signature.R8[1].toString(),
"15383486972088797283337779941324724402501462225528836549661220478783371668959");
assert.equal(signature.S.toString(),
"1398758333392199195742243841591064350253744445503462896781493968760929513778");
const pkPacked = babyjub.packPoint(pk);
assert.equal(pkPacked.toString("hex"), "c433f7a696b7aa3a5224efb3993baf0ccd9e92eecee0c29a3f6c8208a9e81d9e");
});
});

+ 30
- 0
test/mimc.test.js

@ -0,0 +1,30 @@
const chai = require("chai");
const Scalar = require("ffjavascript").Scalar;
const mimc = require("../../iden3/circomlib/src/mimc7.js");
const assert = chai.assert;
const SEED = "mimc";
describe("MiMC7 javascript testvectors", function () {
this.timeout(100000);
before( async () => {
});
it("MiMC7 Constants", async () => {
let cts = mimc.getConstants(SEED, 91);
console.log(cts[1]);
});
it("MiMC7", async () => {
let res = mimc.hash(1, 2, 91);
console.log("mimc.hash(1,2,91)", res);
res = mimc.multiHash([Scalar.e(1), Scalar.e(2), Scalar.e(3)], Scalar.e(0));
console.log("mimc.multiHash([1,2,3], 0)", res);
res = mimc.hash(12, 45, 91);
console.log("mimc.hash(12,45,91)", res);
});
});

+ 39
- 39
test/mt.test.js

@ -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;

+ 0
- 2
test/poseidon.test.js

@ -1,7 +1,5 @@
const chai = require("chai"); const chai = require("chai");
const Fr = require("ffjavascript").bn128.Fr;
const poseidon = require("../../iden3/circomlib/src/poseidon.js"); const poseidon = require("../../iden3/circomlib/src/poseidon.js");
const assert = chai.assert; const assert = chai.assert;

Loading…
Cancel
Save