Browse Source

Refactor

master
Jordi Baylina 5 years ago
parent
commit
a7ac0b5d18
No known key found for this signature in database GPG Key ID: 7480C80C1BE43112
3 changed files with 80 additions and 20 deletions
  1. +52
    -2
      src/bigint.js
  2. +6
    -18
      src/zqfield.js
  3. +22
    -0
      test/algebra.js

+ 52
- 2
src/bigint.js

@ -3,11 +3,29 @@ const bigInt = require("big-integer");
if (typeof(BigInt) != "undefined") {
const wBigInt = BigInt;
wBigInt.prototype.affine = function (q) {
let aux = this;
if (aux < 0) {
if (aux <= -q) {
aux = aux % q;
}
if (aux.isNegative()) {
aux = aux.add(q);
}
} else {
if (aux >= q) {
aux = aux % q;
}
}
return aux;
};
wBigInt.prototype.modInv = function (q) {
let t = wBigInt.zero;
let r = q;
let newt = wBigInt.one;
let newr = this;
let newr = this.affine(q);
while (newr!=wBigInt.zero) {
let q = r/newr;
[t, newt] = [newt, t-q*newt];
@ -66,7 +84,16 @@ if (typeof(BigInt) != "undefined") {
};
wBigInt.prototype.equals = function(b) {
return this == b;
/* console.log("..");
console.log(this);
console.log(b);
console.log(this == b);
console.log(".."); */
return this.valueOf() == b.valueOf();
};
wBigInt.prototype.mulMod = function(q, b) {
return this * b % q;
};
wBigInt.one = BigInt(1);
@ -74,5 +101,28 @@ if (typeof(BigInt) != "undefined") {
module.exports = wBigInt;
} else {
bigInt.prototype.mulMod = function(q, b) {
return this.times(b).mod(q);
};
bigInt.prototype.affine = function (q) {
let aux = this;
if (aux.isNegative()) {
const nq = bigInt.zero.minus(q);
if (aux.lesserOrEquals(nq)) {
aux = aux.mod(q);
}
if (aux.isNegative()) {
aux = aux.add(q);
}
} else {
if (aux.greaterOrEquals(q)) {
aux = aux.mod(q);
}
}
return aux;
};
module.exports = bigInt;
}

+ 6
- 18
src/zqfield.js

@ -1,7 +1,7 @@
const bigInt = require("./bigint");
const fUtils = require("./futils.js");
class F1Field {
class ZqField {
constructor(q) {
this.q = q;
this.nq = bigInt.zero.minus(q);
@ -30,11 +30,11 @@ class F1Field {
}
mul(a, b) {
return a.times(b).mod(this.q);
return a.mulMod(this.q, b);
}
inverse(a) {
return this.affine(a).modInv(this.q);
return a.modInv(this.q);
}
div(a, b) {
@ -54,20 +54,7 @@ class F1Field {
}
affine(a) {
let aux = a;
if (aux.isNegative()) {
if (aux.lesserOrEquals(this.nq)) {
aux = a.mod(this.q);
}
if (aux.isNegative()) {
aux = aux.add(this.q);
}
} else {
if (aux.greaterOrEquals(this.q)) {
aux = aux.mod(this.q);
}
}
return aux;
return a.affine(this.q);
}
mulEscalar(base, e) {
@ -84,4 +71,5 @@ class F1Field {
}
}
module.exports = F1Field;
module.exports = ZqField;

+ 22
- 0
test/algebra.js

@ -2,9 +2,31 @@ const chai = require("chai");
const bigInt = require("../src/bigint.js");
const BN128 = require("../src/BN128.js");
const F1Field = require("../src/zqfield.js");
const assert = chai.assert;
describe("F1 testing", () => {
it("Should compute euclidean", () => {
const F = new F1Field(bigInt(7));
const res = F.inverse(bigInt(4));
assert(F.equals(res, bigInt(2)));
});
it("Should multiply and divide in F1", () => {
const bn128 = new BN128();
const a = bigInt("1");
const b = bn128.F1.affine(bigInt("-3"));
const c = bn128.F1.mul(a,b);
const d = bn128.F1.div(c,b);
assert(bn128.F1.equals(a, d));
});
});
describe("Curve G1 Test", () => {
it("r*one == 0", () => {
const bn128 = new BN128();

Loading…
Cancel
Save