g1 and g2 tests works
Jordi Baylina jordi@baylina.cat
Sun, 12 Aug 2018 22:11:42 +0200
3 files changed,
122 insertions(+),
31 deletions(-)
M
src/constants.js
→
src/constants.js
@@ -22,7 +22,9 @@ [
bigInt("8495653923123431417604973247489272438418190587263600148770280649306958101930"), bigInt("4082367875863433681332203403145435568316851327593401208105741076214120093531") ] - ] + ], + + f2nonResidue: bigInt("21888242871839275222246405745257275088696311157297823662689037894645226208582") };
M
src/f2field.js
→
src/f2field.js
@@ -1,54 +1,106 @@
+const bigInt = require("big-integer"); +class F2Field { + constructor(F, nonResidue) { + this.F = F; + this.zero = [this.F.zero, this.F.zero]; + this.one = [this.F.one, this.F.zero]; + this.nonResidue = nonResidue; + } -class F2Field { - constructor(p) { - this.p = n; + e(c0, c1) { + return [bigInt(c0), bigInt(c1)]; + } + + copy(a) { + return [this.F.copy(a[0]), this.F.copy(a[1])]; } add(a, b) { - const maxGrade = Math.max(a.length, b.length); - const res = new Array(maxGrade); - for (let i=0; i<maxGrade; i++) { - res[i] = this.F.add(a[i], b[i]); - } - return this._reduce(res); + return [ + this.F.add(a[0], b[0]), + this.F.add(a[1], b[1]) + ]; } sub(a, b) { - // TODO - throw new Error("Not Implementted"); + return [ + this.F.sub(a[0], b[0]), + this.F.sub(a[1], b[1]) + ]; } neg(a) { - // TODO - throw new Error("Not Implementted"); + return this.sub(this.zero, a); } mul(a, b) { - // TODO - throw new Error("Not Implementted"); + const aA = this.F.mul(a[0] , b[0]); + const bB = this.F.mul(a[1] , b[1]); + + return [ + this.F.add( aA , this.F.mul(this.nonResidue , bB)), + this.F.sub( + this.F.mul( + this.F.add(a[0], a[1]), + this.F.add(b[0], b[1])), + this.F.add(aA, bB))]; } - inverse(a, b) { - // TODO - throw new Error("Not Implementted"); + inverse(a) { + const t0 = this.F.square(a[0]); + const t1 = this.F.square(a[1]); + const t2 = this.F.sub(t0, this.F.mul(this.nonResidue, t1)); + const t3 = this.F.inverse(t2); + return [ + this.F.mul(a[0], t3), + this.F.neg(this.F.mul( a[1], t3)) ]; } div(a, b) { - // TODO - throw new Error("Not Implementted"); + return this.mul(a, this.inverse(b)); + } + + square(a) { + const ab = this.F.mul(a[0] , a[1]); + + /* + [ + (a + b) * (a + non_residue * b) - ab - non_residue * ab, + ab + ab + ]; + */ + + return [ + this.F.sub( + this.F.mul( + this.F.add(a[0], a[1]) , + this.F.add( + a[0] , + this.F.mul(this.nonResidue , a[1]))), + this.F.add( + ab, + this.F.mul(this.nonResidue, ab))), + this.F.add(ab, ab) + ]; } isZero(a) { - // TODO - throw new Error("Not Implementted"); + return this.F.isZero(a[0]) && this.F.isZero(a[1]); + } + + equals(a, b) { + return this.F.equals(a[0], b[0]) && this.F.equals(a[1], b[1]); } - random() { - // TODO - throw new Error("Not Implementted"); + affine(a) { + return [this.F.affine(a[0]), this.F.affine(a[1])]; } + toString(a) { + const cp = this.affine(a); + return `[ ${this.F.toString(cp[0])} , ${this.F.toString(cp[1])} ]`; + } } module.exports = F2Field;
M
test/algebra.js
→
test/algebra.js
@@ -1,12 +1,13 @@
+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 chai = require("chai"); 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);@@ -16,12 +17,12 @@
assert(G1.equals(res, G1.zero), "G1 does not have range r"); }); - it("Should add match in various", () => { + it("Should add match in various in G1", () => { const F1 = new F1Field(constants.q); const G1 = new GCurve(F1, constants.g1); - const r1 = F1.e(33); - const r2 = F1.e(44); + const r1 = bigInt(33); + const r2 = bigInt(44); const gr1 = G1.mulEscalar(G1.g, r1); const gr2 = G1.mulEscalar(G1.g, r2);@@ -33,3 +34,39 @@
assert(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 res = G2.mulEscalar(G2.g, constants.r); + + assert(G2.equals(res, 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 r1 = bigInt(33); + const r2 = bigInt(44); + + const gr1 = G2.mulEscalar(G2.g, r1); + const gr2 = G2.mulEscalar(G2.g, r2); + + const grsum1 = G2.add(gr1, gr2); + + const grsum2 = G2.mulEscalar(G2.g, r1.add(r2)); + + /* + console.log(G2.toString(grsum1)); + console.log(G2.toString(grsum2)); + */ + + assert(G2.equals(grsum1, grsum2)); + }); + +});