Skip to content

Commit

Permalink
Fix bls12-381
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaylina committed Sep 24, 2020
1 parent 65d5640 commit 6f3f739
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 22 deletions.
117 changes: 101 additions & 16 deletions build/main.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export {default as ChaCha} from "./src/chacha.js";

export {default as BigBuffer} from "./src/bigbuffer.js";

export {getCurveFromR, getCurveFromQ, getCurveFromName} from "./src/curves.js";

6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"homepage": "https://github.com/iden3/ffjs#readme",
"dependencies": {
"big-integer": "^1.6.48",
"wasmcurves": "0.0.11",
"wasmcurves": "0.0.12",
"worker-threads": "^1.0.0"
},
"devDependencies": {
Expand Down
51 changes: 51 additions & 0 deletions src/curves.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as Scalar from "./scalar.js";
import {default as buildBn128} from "./bn128.js";
import {default as buildBls12381} from "./bn128.js";

const bls12381r = Scalar.e("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001", 16);
const bn128r = Scalar.e("21888242871839275222246405745257275088548364400416034343698204186575808495617");

const bls12381q = Scalar.e("1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab", 16);
const bn128q = Scalar.e("21888242871839275222246405745257275088696311157297823662689037894645226208583");

export async function getCurveFromR(r) {
let curve;
if (Scalar.eq(r, bn128r)) {
curve = await buildBn128();
} else if (Scalar.eq(r, bls12381r)) {
curve = await buildBls12381();
} else {
throw new Error(`Curve not supported: ${Scalar.toString(r)}`);
}
return curve;
}

export async function getCurveFromQ(q) {
let curve;
if (Scalar.eq(q, bn128q)) {
curve = await buildBn128();
} else if (Scalar.eq(q, bls12381q)) {
curve = await buildBls12381();
} else {
throw new Error(`Curve not supported: ${Scalar.toString(q)}`);
}
return curve;
}

export async function getCurveFromName(name) {
let curve;
const normName = normalizeName(name);
if (["BN128", "BN254", "ALTBN128"].indexOf(normName) >= 0) {
curve = await buildBn128();
} else if (["BLS12381"].indexOf(normName) >= 0) {
curve = await buildBls12381();
} else {
throw new Error(`Curve not supported: ${name}`);
}
return curve;

function normalizeName(n) {
return n.toUpperCase().match(/[A-Za-z0-9]+/g).join("");
}

}
4 changes: 2 additions & 2 deletions src/fsqrt.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function alg5_tonelliShanks(F) {
while (F.eq(c0, F.one)) {
const c = F.random();
F.sqrt_z = F.pow(c, F.sqrt_t);
c0 = F.pow(F.sqrt_z, 1 << (F.sqrt_s-1) );
c0 = F.pow(F.sqrt_z, 2 ** (F.sqrt_s-1) );
}

F.sqrt_tm1d2 = Scalar.div(Scalar.sub(F.sqrt_t, 1),2);
Expand All @@ -60,7 +60,7 @@ function alg5_tonelliShanks(F) {
const F=this;
if (F.isZero(a)) return F.zero;
let w = F.pow(a, F.sqrt_tm1d2);
const a0 = F.pow( F.mul(F.square(w), a), 1 << (F.sqrt_s-1) );
const a0 = F.pow( F.mul(F.square(w), a), 2 ** (F.sqrt_s-1) );
if (F.eq(a0, F.negone)) return null;

let v = F.sqrt_s;
Expand Down
10 changes: 10 additions & 0 deletions src/wasm_curve.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,16 @@ export default class WasmCurve {
return this.fromObject(a);
}

x(a) {
const tmp = this.toAffine(a);
return tmp.slice(0, this.F.n8);
}

y(a) {
const tmp = this.toAffine(a);
return tmp.slice(this.F.n8);
}

}


12 changes: 12 additions & 0 deletions src/wasm_field2.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export default class WasmField2 {
return this.op2("_mul", a, b);
}

mul1(a,b) {
return this.op2("_mul1", a, b);
}

div(a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
Expand Down Expand Up @@ -170,5 +174,13 @@ export default class WasmField2 {
return buff;
}

c1(a) {
return a.slice(0, this.F.n8);
}

c2(a) {
return a.slice(this.F.n8);
}

}

12 changes: 12 additions & 0 deletions src/wasm_field3.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ export default class WasmField3 {
return buff;
}

c1(a) {
return a.slice(0, this.F.n8);
}

c2(a) {
return a.slice(this.F.n8, this.F.n8*2);
}

c3(a) {
return a.slice(this.F.n8*2);
}

}


0 comments on commit 6f3f739

Please sign in to comment.