From a7ac0b5d181d6b7e629996b6e5079b144fda6f0c Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Wed, 15 Aug 2018 11:05:17 +0200 Subject: [PATCH] Refactor --- src/bigint.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++-- src/zqfield.js | 24 ++++++---------------- test/algebra.js | 22 ++++++++++++++++++++ 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/src/bigint.js b/src/bigint.js index 6fb59e5..a6dbf97 100644 --- a/src/bigint.js +++ b/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 @@ if (typeof(BigInt) != "undefined") { }; 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 @@ if (typeof(BigInt) != "undefined") { 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; } diff --git a/src/zqfield.js b/src/zqfield.js index 2b3826a..1c20e99 100644 --- a/src/zqfield.js +++ b/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 @@ class F1Field { } 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 @@ class F1Field { } 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 @@ class F1Field { } } -module.exports = F1Field; + +module.exports = ZqField; diff --git a/test/algebra.js b/test/algebra.js index 60bc477..5f51503 100644 --- a/test/algebra.js +++ b/test/algebra.js @@ -2,9 +2,31 @@ 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", () => { const bn128 = new BN128();