Browse Source

Fix pairing calc

master
Jordi Baylina 5 years ago
parent
commit
8bc1bb610b
No known key found for this signature in database GPG Key ID: 7480C80C1BE43112
2 changed files with 26 additions and 50 deletions
  1. +10
    -10
      src/bigint.js
  2. +16
    -40
      test/algebra.js

+ 10
- 10
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 @@ if (typeof(BigInt) != "undefined") {
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;

+ 16
- 40
test/algebra.js

@ -145,54 +145,30 @@ describe("F12 testing", () => {
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);
});

Loading…
Cancel
Save