mirror of
https://github.com/zkitter/ffjavascript.git
synced 2026-01-09 15:07:56 -05:00
Fix bls12-381
This commit is contained in:
117
build/main.cjs
117
build/main.cjs
File diff suppressed because one or more lines are too long
1
main.js
1
main.js
@@ -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
package-lock.json
generated
6
package-lock.json
generated
@@ -2274,9 +2274,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"wasmcurves": {
|
||||
"version": "0.0.11",
|
||||
"resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.0.11.tgz",
|
||||
"integrity": "sha512-iRuX0slhizCSYGScgQH0P7j2GS5qgfnpYVPtKoj+wrlLGPZQZiviGj8AwJdeg7fI68yVw4Wquvyp0ZCPfcb0wQ==",
|
||||
"version": "0.0.12",
|
||||
"resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.0.12.tgz",
|
||||
"integrity": "sha512-1Jl9mkatyHSNj80ILjf85SZUNuZQBCkTjJlhzqHnZQXUmIimCIWkugaVaYNjozLs1Gun4h/keZe1MBeBN0sRpg==",
|
||||
"requires": {
|
||||
"big-integer": "^1.6.42",
|
||||
"blakejs": "^1.1.0"
|
||||
|
||||
@@ -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": {
|
||||
|
||||
51
src/curves.js
Normal file
51
src/curves.js
Normal file
@@ -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("");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user