diff --git a/src/bn128.js b/src/bn128.js index b9097df..8977e8b 100644 --- a/src/bn128.js +++ b/src/bn128.js @@ -18,7 +18,6 @@ */ const bigInt = require("./bigint.js"); -const assert = require("assert"); const F1Field = require("./zqfield.js"); const F2Field = require("./f2field.js"); @@ -160,9 +159,15 @@ class BN128 { } const Q1 = this.G2.affine(this._g2MulByQ(Qcopy)); - assert(this.F2.equals(Q1[2], this.F2.one)); + if (!this.F2.equals(Q1[2], this.F2.one)) + { + throw new Error("Expected values are not equal"); + } const Q2 = this.G2.affine(this._g2MulByQ(Q1)); - assert(this.F2.equals(Q2[2], this.F2.one)); + if (!this.F2.equals(Q2[2], this.F2.one)) + { + throw new Error("Expected values are not equal"); + } if (this.loopCountNef) { diff --git a/src/polfield.js b/src/polfield.js index 8ade7b1..1d9046c 100644 --- a/src/polfield.js +++ b/src/polfield.js @@ -25,7 +25,6 @@ */ const bigInt = require("./bigint.js"); -const assert = require("assert"); class PolField { constructor (F) { @@ -446,8 +445,12 @@ class PolField { let res = this.F.one; let r = i; - assert(i=n) { + throw new Error("Given 'i' should be lower than 'n'"); + } + else if (1<0) { if (r & 1 == 1) { diff --git a/src/zqfield.js b/src/zqfield.js index eb6488b..02846eb 100644 --- a/src/zqfield.js +++ b/src/zqfield.js @@ -17,11 +17,25 @@ snarkjs. If not, see . */ -const crypto = require("crypto"); - const bigInt = require("./bigint"); const fUtils = require("./futils.js"); +function getRandomByte() { + if (typeof window !== "undefined") { // Browser + if (typeof window.crypto !== "undefined") { // Supported + let array = new Uint8Array(1); + window.crypto.getRandomValues(array); + return array[0]; + } + else { // fallback + return Math.floor(Math.random() * 256); + } + } + else { // NodeJS + return module.require("crypto").randomBytes(1)[0]; + } +} + class ZqField { constructor(q) { this.q = bigInt(q); @@ -85,7 +99,7 @@ class ZqField { let res = bigInt(0); let n = bigInt(this.q); while (!n.isZero()) { - res = res.shl(8).add(bigInt(crypto.randomBytes(1)[0])); + res = res.shl(8).add(bigInt(getRandomByte())); n = n.shr(8); } return res;