Refactor
Jordi Baylina jordi@baylina.cat
Wed, 15 Aug 2018 11:05:17 +0200
3 files changed,
80 insertions(+),
20 deletions(-)
M
src/bigint.js
→
src/bigint.js
@@ -3,11 +3,29 @@ const bigInt = require("big-integer");
if (typeof(BigInt) != "undefined") { const wBigInt = BigInt; + + wBigInt.prototype.affine = function (q) { + let aux = this; + if (aux < 0) { + if (aux <= -q) { + aux = aux % q; + } + if (aux.isNegative()) { + aux = aux.add(q); + } + } else { + if (aux >= q) { + aux = aux % q; + } + } + return aux; + }; + wBigInt.prototype.modInv = function (q) { let t = wBigInt.zero; let r = q; let newt = wBigInt.one; - let newr = this; + let newr = this.affine(q); while (newr!=wBigInt.zero) { let q = r/newr; [t, newt] = [newt, t-q*newt];@@ -66,7 +84,16 @@ return this <= b;
}; wBigInt.prototype.equals = function(b) { - return this == b; +/* console.log(".."); + console.log(this); + console.log(b); + console.log(this == b); + console.log(".."); */ + return this.valueOf() == b.valueOf(); + }; + + wBigInt.prototype.mulMod = function(q, b) { + return this * b % q; }; wBigInt.one = BigInt(1);@@ -74,5 +101,28 @@ wBigInt.zero = BigInt(0);
module.exports = wBigInt; } else { + + bigInt.prototype.mulMod = function(q, b) { + return this.times(b).mod(q); + }; + + bigInt.prototype.affine = function (q) { + let aux = this; + if (aux.isNegative()) { + const nq = bigInt.zero.minus(q); + if (aux.lesserOrEquals(nq)) { + aux = aux.mod(q); + } + if (aux.isNegative()) { + aux = aux.add(q); + } + } else { + if (aux.greaterOrEquals(q)) { + aux = aux.mod(q); + } + } + return aux; + }; + module.exports = bigInt; }
M
src/zqfield.js
→
src/zqfield.js
@@ -1,7 +1,7 @@
const bigInt = require("./bigint"); const fUtils = require("./futils.js"); -class F1Field { +class ZqField { constructor(q) { this.q = q; this.nq = bigInt.zero.minus(q);@@ -30,11 +30,11 @@ return bigInt.zero.minus(a);
} mul(a, b) { - return a.times(b).mod(this.q); + return a.mulMod(this.q, b); } inverse(a) { - return this.affine(a).modInv(this.q); + return a.modInv(this.q); } div(a, b) {@@ -54,20 +54,7 @@ return this.affine(a).equals(this.affine(b));
} affine(a) { - let aux = a; - if (aux.isNegative()) { - if (aux.lesserOrEquals(this.nq)) { - aux = a.mod(this.q); - } - if (aux.isNegative()) { - aux = aux.add(this.q); - } - } else { - if (aux.greaterOrEquals(this.q)) { - aux = aux.mod(this.q); - } - } - return aux; + return a.affine(this.q); } mulEscalar(base, e) {@@ -84,4 +71,5 @@ return `"0x${ca.toString(16)}"`;
} } -module.exports = F1Field; + +module.exports = ZqField;
M
test/algebra.js
→
test/algebra.js
@@ -2,8 +2,30 @@ const chai = require("chai");
const bigInt = require("../src/bigint.js"); const BN128 = require("../src/BN128.js"); +const F1Field = require("../src/zqfield.js"); const assert = chai.assert; + + +describe("F1 testing", () => { + it("Should compute euclidean", () => { + const F = new F1Field(bigInt(7)); + const res = F.inverse(bigInt(4)); + + assert(F.equals(res, bigInt(2))); + }); + + it("Should multiply and divide in F1", () => { + const bn128 = new BN128(); + const a = bigInt("1"); + const b = bn128.F1.affine(bigInt("-3")); + const c = bn128.F1.mul(a,b); + const d = bn128.F1.div(c,b); + + assert(bn128.F1.equals(a, d)); + }); +}); + describe("Curve G1 Test", () => { it("r*one == 0", () => {