mirror of
https://github.com/arnaucube/snarkjs.git
synced 2026-02-28 05:56:44 +01:00
Fix pairing calc
This commit is contained in:
@@ -7,6 +7,8 @@ console.log("XXX");
|
|||||||
|
|
||||||
if (typeof(BigInt) != "undefined") {
|
if (typeof(BigInt) != "undefined") {
|
||||||
wBigInt = BigInt;
|
wBigInt = BigInt;
|
||||||
|
wBigInt.one = wBigInt(1);
|
||||||
|
wBigInt.zero = wBigInt(0);
|
||||||
|
|
||||||
// Affine
|
// Affine
|
||||||
wBigInt.genAffine = (q) => {
|
wBigInt.genAffine = (q) => {
|
||||||
@@ -118,14 +120,14 @@ if (typeof(BigInt) != "undefined") {
|
|||||||
if (q) {
|
if (q) {
|
||||||
return (a) => (a.affine(q) == wBigInt.zero);
|
return (a) => (a.affine(q) == wBigInt.zero);
|
||||||
} else {
|
} else {
|
||||||
return (a) => a != 0;
|
return (a) => a == wBigInt.zero;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Other minor functions
|
// Other minor functions
|
||||||
wBigInt.prototype.isOdd = function() {
|
wBigInt.prototype.isOdd = function() {
|
||||||
return (this & wBigInt.one) == 1;
|
return (this & wBigInt.one) == wBigInt(1);
|
||||||
};
|
};
|
||||||
|
|
||||||
wBigInt.prototype.isNegative = function() {
|
wBigInt.prototype.isNegative = function() {
|
||||||
@@ -313,28 +315,26 @@ wBigInt.square = function(a, q) {
|
|||||||
return wBigInt.genSquare(q)(a);
|
return wBigInt.genSquare(q)(a);
|
||||||
};
|
};
|
||||||
|
|
||||||
wBigInt.prototype.square = function (a, q) {
|
wBigInt.prototype.square = function (q) {
|
||||||
return wBigInt.genSquare(q)(a);
|
return wBigInt.genSquare(q)(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
wBigInt.double = function(a, q) {
|
wBigInt.double = function(a, q) {
|
||||||
return wBigInt.genDouble(q)(a);
|
return wBigInt.genDouble(q)(a);
|
||||||
};
|
};
|
||||||
|
|
||||||
wBigInt.prototype.double = function (a, q) {
|
wBigInt.prototype.double = function (q) {
|
||||||
return wBigInt.genDouble(q)(a);
|
return wBigInt.genDouble(q)(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
wBigInt.isZero = function(a, q) {
|
wBigInt.isZero = function(a, q) {
|
||||||
return wBigInt.genIsZero(q)(a);
|
return wBigInt.genIsZero(q)(a);
|
||||||
};
|
};
|
||||||
|
|
||||||
wBigInt.prototype.isZero = function (a, q) {
|
wBigInt.prototype.isZero = function (q) {
|
||||||
return wBigInt.genIsZero(q)(a);
|
return wBigInt.genIsZero(q)(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
wBigInt.one = wBigInt(1);
|
|
||||||
wBigInt.zero = wBigInt(0);
|
|
||||||
|
|
||||||
module.exports = wBigInt;
|
module.exports = wBigInt;
|
||||||
|
|
||||||
|
|||||||
@@ -145,54 +145,30 @@ describe("F12 testing", () => {
|
|||||||
|
|
||||||
describe("Pairing", () => {
|
describe("Pairing", () => {
|
||||||
it("Should match 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 g1b = bn128.G1.mulEscalar(bn128.G1.g, 30);
|
||||||
|
const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25);
|
||||||
|
|
||||||
|
|
||||||
const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25);
|
const pre1a = bn128.precomputeG1(g1a);
|
||||||
const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30);
|
const pre2a = bn128.precomputeG2(g2a);
|
||||||
|
const pre1b = bn128.precomputeG1(g1b);
|
||||||
|
const pre2b = bn128.precomputeG2(g2b);
|
||||||
|
|
||||||
const g1b = bn128.G1.mulEscalar(bn128.G1.g, 30);
|
const r1 = bn128.millerLoop(pre1a, pre2a);
|
||||||
const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25);
|
const r2 = bn128.millerLoop(pre1b, pre2b);
|
||||||
|
|
||||||
|
const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2));
|
||||||
|
|
||||||
const pre1a = bn128.precomputeG1(g1a);
|
const res = bn128.finalExponentiation(rbe);
|
||||||
const pre2a = bn128.precomputeG2(g2a);
|
|
||||||
const pre1b = bn128.precomputeG1(g1b);
|
|
||||||
const pre2b = bn128.precomputeG2(g2b);
|
|
||||||
|
|
||||||
const r1 = bn128.millerLoop(pre1a, pre2a);
|
assert(bn128.F12.equals(res, bn128.F12.one));
|
||||||
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);
|
}).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);
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user