circomlib/calcpedersenbases/calcpedersenbases.js
calcpedersenbases/calcpedersenbases.jsRaw
1const bn128 = require("snarkjs").bn128;
2const bigInt = require("snarkjs").bigInt;
3const createBlakeHash = require("blake-hash");
4const assert = require("assert");
5const babyJub = require("../src/babyjub");
6
7function getPoint(S) {
8 const F = bn128.Fr;
9 const h = createBlakeHash("blake256").update(S).digest();
10
11 assert(h.length == 32);
12
13 let sign = false;
14 if (h[31] & 0x80) {
15 h[31] = h[31] & 0x7F;
16 sign = true;
17 }
18
19 let y = bigInt(0);
20 for (let i=0; i<32; i++) {
21 y = y.shl(8);
22 y = y.add(bigInt(h[i]));
23 }
24
25 const a = bigInt("168700");
26 const d = bigInt("168696");
27
28 const y2 = F.square(y);
29
30 let x = F.sqrt(F.div(
31 F.sub(F.one, y2),
32 F.sub(a, F.mul(d, y2))));
33
34 if (x == null) return null;
35
36 if (sign) x = F.neg(x);
37
38 const p = [bn128.Fr.affine(x), bn128.Fr.affine(y)];
39
40 const p8 = babyJub.mulPointEscalar(p, 8);
41
42 return p8;
43}
44
45
46function generatePoint(S) {
47 let p= null;
48 let idx = 0;
49 while (p==null) {
50 let sidx = "" + idx;
51 while (sidx.length<16) sidx = "0"+sidx;
52 p = getPoint(S+"_"+sidx);
53 idx++;
54 }
55 assert(babyJub.inCurve(p), "Point not in curve");
56 return p;
57}
58
59
60
61
62
63const g = [
64 bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
65 bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")];
66
67// Sanity check
68if (!babyJub.inCurve(g)) {
69 throw new Error("Generator not In curve -> Some thing goes wrong...");
70}
71
72for (let i=0; i<25; i++) {
73 let S = "" +i;
74 while (S.length<16) S = "0"+S;
75 const P = generatePoint("Iden3_PedersenGenerator_"+S);
76 console.log(`[${P[0].toString()}, ${P[1].toString()}]`);
77}
78
79
80