From 2f9ad59c3af94c606a1405fa338ed07891b3e1a2 Mon Sep 17 00:00:00 2001 From: Jordi Date: Tue, 12 Feb 2019 12:11:25 +0100 Subject: [PATCH 1/3] Exposing babyjub and pruneBuffer --- index.js | 1 + src/eddsa.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e3d1bb3..de190c3 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ exports.smt = require("./src/smt"); exports.eddsa = require("./src/eddsa"); exports.mimc7 = require("./src/mimc7"); +exports.babyJub = require("./src/babyjub"); diff --git a/src/eddsa.js b/src/eddsa.js index f16625d..eb869c3 100644 --- a/src/eddsa.js +++ b/src/eddsa.js @@ -4,7 +4,7 @@ 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; @@ -13,6 +13,7 @@ exports.verify = verify; exports.verifyMiMC = verifyMiMC; exports.packSignature = packSignature; exports.unpackSignature = unpackSignature; +exports.pruneBuffer = pruneBuffer; function cratePrvKey() { From 138945bfdcd0a9c59f2b7fe20dff8bcf70a353da Mon Sep 17 00:00:00 2001 From: Jordi Date: Tue, 12 Feb 2019 12:19:17 +0100 Subject: [PATCH 2/3] Overcome undefined "Web3.utils" on web3@1.0.0-beta41 --- src/evmasm.js | 6 +++--- src/mimc7.js | 16 ++++++++-------- src/mimc_gencontract.js | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/evmasm.js b/src/evmasm.js index d68f791..351201b 100644 --- a/src/evmasm.js +++ b/src/evmasm.js @@ -3,7 +3,7 @@ // -const Web3 = require("web3"); +const Web3Utils = require("web3-utils"); const assert = require("assert"); class Contract { @@ -39,7 +39,7 @@ class Contract { genLoadedLength = C.code.length; } - return Web3.utils.bytesToHex(C.code.concat(this.code)); + return Web3Utils.bytesToHex(C.code.concat(this.code)); } stop() { this.code.push(0x00); } @@ -149,7 +149,7 @@ class Contract { } push(data) { - const d = Web3.utils.hexToBytes(Web3.utils.toHex(data)); + const d = Web3Utils.hexToBytes(Web3Utils.toHex(data)); assert(d.length>0); assert(d.length<=32); this.code = this.code.concat([0x5F + d.length], d); diff --git a/src/mimc7.js b/src/mimc7.js index dda4b07..47291d8 100644 --- a/src/mimc7.js +++ b/src/mimc7.js @@ -1,6 +1,6 @@ const bn128 = require("snarkjs").bn128; const bigInt = require("snarkjs").bigInt; -const Web3 = require("web3"); +const Web3Utils = require("web3-utils"); 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 = Web3.utils.keccak256(seed+"_iv"); - const cn = bigInt(Web3.utils.toBN(c).toString()); + const c = Web3Utils.keccak256(seed+"_iv"); + const cn = bigInt(Web3Utils.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 = Web3.utils.keccak256(SEED); + let c = Web3Utils.keccak256(SEED); for (let i=1; i Date: Tue, 12 Feb 2019 12:21:44 +0100 Subject: [PATCH 3/3] Make code independent of NodeJS core modules --- calcpedersenbases/calcpedersenbases.js | 9 ++++++--- src/eddsa.js | 6 ------ src/evmasm.js | 20 ++++++++++++-------- src/pedersenHash.js | 5 +++-- test/eddsa.js | 3 ++- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/calcpedersenbases/calcpedersenbases.js b/calcpedersenbases/calcpedersenbases.js index c7ae74c..b24842a 100644 --- a/calcpedersenbases/calcpedersenbases.js +++ b/calcpedersenbases/calcpedersenbases.js @@ -1,14 +1,15 @@ 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(); - assert(h.length == 32); + if (h.length != 32) { + throw new Error("Invalid length") + } let sign = false; if (h[31] & 0x80) { @@ -52,7 +53,9 @@ function generatePoint(S) { p = getPoint(S+"_"+sidx); idx++; } - assert(babyJub.inCurve(p), "Point not in curve"); + if (!babyJub.inCurve(p)){ + throw new Error("Point not in curve"); + } return p; } diff --git a/src/eddsa.js b/src/eddsa.js index eb869c3..4991881 100644 --- a/src/eddsa.js +++ b/src/eddsa.js @@ -3,9 +3,7 @@ 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; @@ -16,10 +14,6 @@ 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; diff --git a/src/evmasm.js b/src/evmasm.js index 351201b..ad4170c 100644 --- a/src/evmasm.js +++ b/src/evmasm.js @@ -4,7 +4,6 @@ const Web3Utils = require("web3-utils"); -const assert = require("assert"); class Contract { constructor() { @@ -141,7 +140,9 @@ class Contract { msize() { this.code.push(0x59); } gas() { this.code.push(0x5a); } label(name) { - assert(typeof this.labels[name] == "undefined", "Label already defined"); + if (typeof this.labels[name] != "undefined") { + throw new Error("Label already defined"); + } this.labels[name] = this.code.length; this.code.push(0x5b); @@ -150,20 +151,23 @@ class Contract { push(data) { const d = Web3Utils.hexToBytes(Web3Utils.toHex(data)); - assert(d.length>0); - assert(d.length<=32); + if (d.length == 0 || d.length > 32) { + throw new Error("Assertion failed"); + } this.code = this.code.concat([0x5F + d.length], d); } dup(n) { - assert(n>=0); - assert(n<16); + if (n < 0 || n >= 16) { + throw new Error("Assertion failed"); + } this.code.push(0x80 + n); } swap(n) { - assert(n>=1); - assert(n<=16); + if (n < 1 || n > 16) { + throw new Error("Assertion failed"); + } this.code.push(0x8f + n); } diff --git a/src/pedersenHash.js b/src/pedersenHash.js index a055297..e14f089 100644 --- a/src/pedersenHash.js +++ b/src/pedersenHash.js @@ -1,7 +1,6 @@ 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"; @@ -73,7 +72,9 @@ function getBasePoint(pointIdx) { const p8 = babyJub.mulPointEscalar(p, 8); - assert(babyJub.inSubgroup(p8), "Point not in curve"); + if (!babyJub.inSubgroup(p8)) { + throw new Error("Point not in curve"); + } bases[pointIdx] = p8; return p8; diff --git a/test/eddsa.js b/test/eddsa.js index 0dd29c4..8292a89 100644 --- a/test/eddsa.js +++ b/test/eddsa.js @@ -2,6 +2,7 @@ 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"); @@ -45,7 +46,7 @@ describe("EdDSA test", function () { it("Sign a single 10 bytes from 0 to 9", async () => { const msg = Buffer.from("00010203040506070809", "hex"); -// const prvKey = eddsa.cratePrvKey(); +// const prvKey = crypto.randomBytes(32); const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");