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