diff --git a/src/bigint.js b/src/bigint.js new file mode 100644 index 0000000..6fb59e5 --- /dev/null +++ b/src/bigint.js @@ -0,0 +1,78 @@ +/* global BigInt */ +const bigInt = require("big-integer"); + +if (typeof(BigInt) != "undefined") { + const wBigInt = BigInt; + wBigInt.prototype.modInv = function (q) { + let t = wBigInt.zero; + let r = q; + let newt = wBigInt.one; + let newr = this; + while (newr!=wBigInt.zero) { + let q = r/newr; + [t, newt] = [newt, t-q*newt]; + [r, newr] = [newr, r-q*newr]; + } + if (t> wBigInt(f); + }; + + wBigInt.prototype.greaterOrEquals = function(b) { + return this >= b; + }; + + wBigInt.prototype.lesserOrEquals = function(b) { + return this <= b; + }; + + wBigInt.prototype.equals = function(b) { + return this == b; + }; + + wBigInt.one = BigInt(1); + wBigInt.zero = BigInt(0); + + module.exports = wBigInt; +} else { + module.exports = bigInt; +} diff --git a/src/bn128.js b/src/bn128.js index 9142d0b..013dd21 100644 --- a/src/bn128.js +++ b/src/bn128.js @@ -1,7 +1,7 @@ -const bigInt = require("big-integer"); +const bigInt = require("./bigint.js"); const assert = require("assert"); -const F1Field = require("./f1field.js"); +const F1Field = require("./zqfield.js"); const F2Field = require("./f2field.js"); const F3Field = require("./f3field.js"); const GCurve = require("./gcurve.js"); diff --git a/src/gcurve.js b/src/gcurve.js index 9055d8d..07c5ba7 100644 --- a/src/gcurve.js +++ b/src/gcurve.js @@ -4,7 +4,7 @@ class GCurve { constructor(F, g) { this.F = F; - this.g = F.copy(g); + this.g = [F.copy(g[0]), F.copy(g[1])]; if (this.g.length == 2) this.g[2] = this.F.one; this.zero = [this.F.zero, this.F.one, this.F.zero]; } diff --git a/src/f1field.js b/src/zqfield.js similarity index 97% rename from src/f1field.js rename to src/zqfield.js index 73ca62c..2b3826a 100644 --- a/src/f1field.js +++ b/src/zqfield.js @@ -1,4 +1,4 @@ -const bigInt = require("big-integer"); +const bigInt = require("./bigint"); const fUtils = require("./futils.js"); class F1Field { diff --git a/test/algebra.js b/test/algebra.js index 2b18262..60bc477 100644 --- a/test/algebra.js +++ b/test/algebra.js @@ -1,6 +1,6 @@ const chai = require("chai"); -const bigInt = require("big-integer"); +const bigInt = require("../src/bigint.js"); const BN128 = require("../src/BN128.js"); const assert = chai.assert; @@ -148,4 +148,29 @@ describe("Pairing", () => { assert(bn128.F12.equals(res, bn128.F12.one)); }).timeout(10000); + it("Should match pairing 2", () => { + const bn128 = new BN128(); + + + const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25); + const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30); + + const g1b = bn128.G1.mulEscalar(bn128.G1.g, 30); + const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25); + + + const pre1a = bn128.precomputeG1(g1a); + const pre2a = bn128.precomputeG2(g2a); + const pre1b = bn128.precomputeG1(g1b); + const pre2b = bn128.precomputeG2(g2b); + + const r1 = bn128.millerLoop(pre1a, pre2a); + const r2 = bn128.millerLoop(pre1b, pre2b); + + const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2)); + + const res = bn128.finalExponentiation(rbe); + + assert(bn128.F12.equals(res, bn128.F12.one)); + }).timeout(10000); });