mirror of
https://github.com/arnaucube/snarkjs.git
synced 2026-02-28 05:56:44 +01:00
Pairing working
This commit is contained in:
139
test/algebra.js
139
test/algebra.js
@@ -1,72 +1,151 @@
|
||||
const bigInt = require("big-integer");
|
||||
const F1Field = require("../src/f1field.js");
|
||||
const F2Field = require("../src/f2field.js");
|
||||
const GCurve = require("../src/gcurve.js");
|
||||
const constants = require("../src/constants.js");
|
||||
const chai = require("chai");
|
||||
|
||||
const bigInt = require("big-integer");
|
||||
const BN128 = require("../src/BN128.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Curve G1 Test", () => {
|
||||
it ("r*one == 0", () => {
|
||||
const F1 = new F1Field(constants.q);
|
||||
const G1 = new GCurve(F1, constants.g1);
|
||||
it("r*one == 0", () => {
|
||||
const bn128 = new BN128();
|
||||
|
||||
const res = G1.mulEscalar(G1.g, constants.r);
|
||||
const res = bn128.G1.mulEscalar(bn128.G1.g, bn128.r);
|
||||
|
||||
assert(G1.equals(res, G1.zero), "G1 does not have range r");
|
||||
assert(bn128.G1.equals(res, bn128.G1.zero), "G1 does not have range r");
|
||||
});
|
||||
|
||||
it("Should add match in various in G1", () => {
|
||||
const F1 = new F1Field(constants.q);
|
||||
const G1 = new GCurve(F1, constants.g1);
|
||||
|
||||
const bn128 = new BN128();
|
||||
|
||||
const r1 = bigInt(33);
|
||||
const r2 = bigInt(44);
|
||||
|
||||
const gr1 = G1.mulEscalar(G1.g, r1);
|
||||
const gr2 = G1.mulEscalar(G1.g, r2);
|
||||
const gr1 = bn128.G1.mulEscalar(bn128.G1.g, r1);
|
||||
const gr2 = bn128.G1.mulEscalar(bn128.G1.g, r2);
|
||||
|
||||
const grsum1 = G1.add(gr1, gr2);
|
||||
const grsum1 = bn128.G1.add(gr1, gr2);
|
||||
|
||||
const grsum2 = G1.mulEscalar(G1.g, r1.add(r2));
|
||||
const grsum2 = bn128.G1.mulEscalar(bn128.G1.g, r1.add(r2));
|
||||
|
||||
assert(G1.equals(grsum1, grsum2));
|
||||
assert(bn128.G1.equals(grsum1, grsum2));
|
||||
});
|
||||
});
|
||||
|
||||
describe("Curve G2 Test", () => {
|
||||
it ("r*one == 0", () => {
|
||||
const F1 = new F1Field(constants.q);
|
||||
const F2 = new F2Field(F1, constants.f2nonResidue);
|
||||
const G2 = new GCurve(F2, constants.g2);
|
||||
const bn128 = new BN128();
|
||||
|
||||
const res = G2.mulEscalar(G2.g, constants.r);
|
||||
const res = bn128.G2.mulEscalar(bn128.G2.g, bn128.r);
|
||||
|
||||
assert(G2.equals(res, G2.zero), "G2 does not have range r");
|
||||
assert(bn128.G2.equals(res, bn128.G2.zero), "G2 does not have range r");
|
||||
});
|
||||
|
||||
it("Should add match in various in G2", () => {
|
||||
const F1 = new F1Field(constants.q);
|
||||
const F2 = new F2Field(F1, constants.f2nonResidue);
|
||||
const G2 = new GCurve(F2, constants.g2);
|
||||
const bn128 = new BN128();
|
||||
|
||||
const r1 = bigInt(33);
|
||||
const r2 = bigInt(44);
|
||||
|
||||
const gr1 = G2.mulEscalar(G2.g, r1);
|
||||
const gr2 = G2.mulEscalar(G2.g, r2);
|
||||
const gr1 = bn128.G2.mulEscalar(bn128.G2.g, r1);
|
||||
const gr2 = bn128.G2.mulEscalar(bn128.G2.g, r2);
|
||||
|
||||
const grsum1 = G2.add(gr1, gr2);
|
||||
const grsum1 = bn128.G2.add(gr1, gr2);
|
||||
|
||||
const grsum2 = G2.mulEscalar(G2.g, r1.add(r2));
|
||||
const grsum2 = bn128.G2.mulEscalar(bn128.G2.g, r1.add(r2));
|
||||
|
||||
/*
|
||||
console.log(G2.toString(grsum1));
|
||||
console.log(G2.toString(grsum2));
|
||||
*/
|
||||
|
||||
assert(G2.equals(grsum1, grsum2));
|
||||
assert(bn128.G2.equals(grsum1, grsum2));
|
||||
});
|
||||
});
|
||||
|
||||
describe("F6 testing", () => {
|
||||
it("Should multiply and divide in F6", () => {
|
||||
const bn128 = new BN128();
|
||||
const a =
|
||||
[
|
||||
[bigInt("1"), bigInt("2")],
|
||||
[bigInt("3"), bigInt("4")],
|
||||
[bigInt("5"), bigInt("6")]
|
||||
];
|
||||
const b =
|
||||
[
|
||||
[bigInt("12"), bigInt("11")],
|
||||
[bigInt("10"), bigInt("9")],
|
||||
[bigInt("8"), bigInt("7")]
|
||||
];
|
||||
const c = bn128.F6.mul(a,b);
|
||||
const d = bn128.F6.div(c,b);
|
||||
|
||||
assert(bn128.F6.equals(a, d));
|
||||
});
|
||||
});
|
||||
|
||||
describe("F12 testing", () => {
|
||||
it("Should multiply and divide in F12", () => {
|
||||
const bn128 = new BN128();
|
||||
const a =
|
||||
[
|
||||
[
|
||||
[bigInt("1"), bigInt("2")],
|
||||
[bigInt("3"), bigInt("4")],
|
||||
[bigInt("5"), bigInt("6")]
|
||||
],
|
||||
[
|
||||
[bigInt("7"), bigInt("8")],
|
||||
[bigInt("9"), bigInt("10")],
|
||||
[bigInt("11"), bigInt("12")]
|
||||
]
|
||||
];
|
||||
const b =
|
||||
[
|
||||
[
|
||||
[bigInt("12"), bigInt("11")],
|
||||
[bigInt("10"), bigInt("9")],
|
||||
[bigInt("8"), bigInt("7")]
|
||||
],
|
||||
[
|
||||
[bigInt("6"), bigInt("5")],
|
||||
[bigInt("4"), bigInt("3")],
|
||||
[bigInt("2"), bigInt("1")]
|
||||
]
|
||||
];
|
||||
const c = bn128.F12.mul(a,b);
|
||||
const d = bn128.F12.div(c,b);
|
||||
|
||||
assert(bn128.F12.equals(a, d));
|
||||
});
|
||||
});
|
||||
|
||||
describe("Pairing", () => {
|
||||
it("Should match pairing", () => {
|
||||
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