arnaucube
arnaucube/snarkjsPublic Self-hosted repository
Fix pairing calc
Jordi Baylina jordi@baylina.cat
Wed, 15 Aug 2018 15:13:21 +0200
commit

8bc1bb610b1dec869c70343ca24b64236e1bdf5e

parent

62a1dcfde1c8c51e0260a8816c89d958c074b79f

2 files changed, 26 insertions(+), 50 deletions(-)

jump to
M src/bigint.js → src/bigint.js

@@ -7,6 +7,8 @@ console.log("XXX");

if (typeof(BigInt) != "undefined") { wBigInt = BigInt; + wBigInt.one = wBigInt(1); + wBigInt.zero = wBigInt(0); // Affine wBigInt.genAffine = (q) => {

@@ -118,14 +120,14 @@ wBigInt.genIsZero = (q) => {

if (q) { return (a) => (a.affine(q) == wBigInt.zero); } else { - return (a) => a != 0; + return (a) => a == wBigInt.zero; } }; // Other minor functions wBigInt.prototype.isOdd = function() { - return (this & wBigInt.one) == 1; + return (this & wBigInt.one) == wBigInt(1); }; wBigInt.prototype.isNegative = function() {

@@ -313,28 +315,26 @@ wBigInt.square = function(a, q) {

return wBigInt.genSquare(q)(a); }; -wBigInt.prototype.square = function (a, q) { - return wBigInt.genSquare(q)(a); +wBigInt.prototype.square = function (q) { + return wBigInt.genSquare(q)(this); }; wBigInt.double = function(a, q) { return wBigInt.genDouble(q)(a); }; -wBigInt.prototype.double = function (a, q) { - return wBigInt.genDouble(q)(a); +wBigInt.prototype.double = function (q) { + return wBigInt.genDouble(q)(this); }; wBigInt.isZero = function(a, q) { return wBigInt.genIsZero(q)(a); }; -wBigInt.prototype.isZero = function (a, q) { - return wBigInt.genIsZero(q)(a); +wBigInt.prototype.isZero = function (q) { + return wBigInt.genIsZero(q)(this); }; -wBigInt.one = wBigInt(1); -wBigInt.zero = wBigInt(0); module.exports = wBigInt;
M test/algebra.js → test/algebra.js

@@ -145,54 +145,30 @@ });

describe("Pairing", () => { it("Should match pairing", () => { - const bn128 = new BN128(); + for (let i=0; i<1; i++) { + const bn128 = new BN128(); - - const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25); - const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30); + 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 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 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 r1 = bn128.millerLoop(pre1a, pre2a); + const r2 = bn128.millerLoop(pre1b, pre2b); - const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2)); + const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2)); - const res = bn128.finalExponentiation(rbe); + const res = bn128.finalExponentiation(rbe); - assert(bn128.F12.equals(res, bn128.F12.one)); + 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); });