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