circomlib/calcpedersenbases/calcpedersenbases.js

75 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2023-09-12 07:43:48 +03:00
const bn128 = require("@tornado/snarkjs").bn128;
const bigInt = require("@tornado/snarkjs").bigInt;
2018-11-11 21:52:07 +03:00
const createBlakeHash = require("blake-hash");
2018-11-13 12:57:54 +03:00
const babyJub = require("../src/babyjub");
2018-11-11 21:52:07 +03:00
function getPoint(S) {
const F = bn128.Fr;
const h = createBlakeHash("blake256").update(S).digest();
if (h.length != 32) {
2023-09-12 07:43:48 +03:00
throw new Error("Invalid length");
}
2018-11-11 21:52:07 +03:00
let sign = false;
if (h[31] & 0x80) {
2023-09-12 07:43:48 +03:00
h[31] = h[31] & 0x7f;
2018-11-11 21:52:07 +03:00
sign = true;
}
let y = bigInt(0);
2023-09-12 07:43:48 +03:00
for (let i = 0; i < 32; i++) {
y = y.shl(8);
y = y.add(bigInt(h[i]));
2018-11-11 21:52:07 +03:00
}
const a = bigInt("168700");
const d = bigInt("168696");
const y2 = F.square(y);
2023-09-12 07:43:48 +03:00
let x = F.sqrt(F.div(F.sub(F.one, y2), F.sub(a, F.mul(d, y2))));
if (x == null) return null;
2018-11-11 21:52:07 +03:00
if (sign) x = F.neg(x);
2018-11-11 21:52:07 +03:00
const p = [bn128.Fr.affine(x), bn128.Fr.affine(y)];
2018-11-11 21:52:07 +03:00
2018-11-13 12:57:54 +03:00
const p8 = babyJub.mulPointEscalar(p, 8);
2018-11-11 21:52:07 +03:00
return p8;
2018-11-11 21:52:07 +03:00
}
function generatePoint(S) {
2023-09-12 07:43:48 +03:00
let p = null;
2018-11-11 21:52:07 +03:00
let idx = 0;
2023-09-12 07:43:48 +03:00
while (p == null) {
2018-11-11 21:52:07 +03:00
let sidx = "" + idx;
2023-09-12 07:43:48 +03:00
while (sidx.length < 16) sidx = "0" + sidx;
p = getPoint(S + "_" + sidx);
2018-11-11 21:52:07 +03:00
idx++;
}
2023-09-12 07:43:48 +03:00
if (!babyJub.inCurve(p)) {
throw new Error("Point not in curve");
}
2018-11-11 21:52:07 +03:00
return p;
}
const g = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
2023-09-12 07:43:48 +03:00
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
2018-11-11 21:52:07 +03:00
2018-11-13 12:57:54 +03:00
// Sanity check
if (!babyJub.inCurve(g)) {
2018-11-11 21:52:07 +03:00
throw new Error("Generator not In curve -> Some thing goes wrong...");
}
2023-09-12 07:43:48 +03:00
for (let i = 0; i < 25; i++) {
let S = "" + i;
while (S.length < 16) S = "0" + S;
const P = generatePoint("Iden3_PedersenGenerator_" + S);
2018-11-11 21:52:07 +03:00
console.log(`[${P[0].toString()}, ${P[1].toString()}]`);
}