arnaucube
arnaucube/circomlibPublic Self-hosted repository
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 addPoint(a,b, q) {
  7    const cta = bigInt("168700");
  8    const d = bigInt("168696");
  9
 10    const res = [];
 11    res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) *  bigInt(bigInt("1") + d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
 12    res[1] = bigInt((a[1]*b[1] - cta*a[0]*b[0]) * bigInt(bigInt("1") - d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
 13    return res;
 14}
 15
 16function mulPointEscalar(base, e, q) {
 17    let res = [bigInt("0"),bigInt("1")];
 18    let rem = bigInt(e);
 19    let exp = base;
 20
 21    while (! rem.isZero()) {
 22        if (rem.isOdd()) {
 23            res = addPoint(res, exp, q);
 24        }
 25        exp = addPoint(exp, exp, q);
 26        rem = rem.shr(1);
 27    }
 28
 29    return res;
 30}
 31
 32function getPoint(S) {
 33    const F = bn128.Fr;
 34    const h = createBlakeHash("blake256").update(S).digest();
 35
 36    assert(h.length == 32);
 37
 38    let sign = false;
 39    if (h[31] & 0x80) {
 40        h[31] = h[31] & 0x7F;
 41        sign = true;
 42    }
 43
 44    let y = bigInt(0);
 45    for (let i=0; i<32; i++) {
 46        y = y.shl(8);
 47        y = y.add(bigInt(h[i]));
 48    }
 49
 50    const a = bigInt("168700");
 51    const d = bigInt("168696");
 52
 53    const y2 = F.square(y);
 54
 55    let x = F.sqrt(F.div(
 56        F.sub(F.one, y2),
 57        F.sub(a, F.mul(d, y2))));
 58
 59    if (x == null) return null;
 60
 61    if (sign) x = F.neg(x);
 62
 63    const p = [bn128.Fr.affine(x), bn128.Fr.affine(y)];
 64
 65    const p8 =mulPointEscalar(p, 8, q);
 66
 67    return p8;
 68}
 69
 70
 71function generatePoint(S) {
 72    let p= null;
 73    let idx = 0;
 74    while (p==null) {
 75        let sidx = "" + idx;
 76        while (sidx.length<16) sidx = "0"+sidx;
 77        p = getPoint(S+"_"+sidx);
 78        idx++;
 79    }
 80    assert(inCurve(p), "Point not in curve");
 81    return p;
 82}
 83
 84
 85
 86
 87
 88const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
 89const r = bigInt("21888242871839275222246405745257275088614511777268538073601725287587578984328").shr(3);
 90function isLowGrade(p, q) {
 91    const res= mulPointEscalar(p, r, q);
 92    return (res[0].equals(bigInt(0))) && (res[1].equals(bigInt(1)));
 93}
 94
 95function inCurve(p) {
 96    const F = bn128.Fr;
 97
 98    const a = bigInt("168700");
 99    const d = bigInt("168696");
100
101    const x2 = F.square(p[0]);
102    const y2 = F.square(p[1]);
103
104    if (!F.equals(
105        F.add(F.mul(a, x2), y2),
106        F.add(F.one, F.mul(F.mul(x2, y2), d)))) return false;
107
108    if (!isLowGrade(p, q)) return false;
109
110    return true;
111}
112
113const g = [
114    bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
115    bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")];
116
117
118if (!inCurve(g)) {
119    throw new Error("Generator not In curve -> Some thing goes wrong...");
120}
121
122for (let i=0; i<25; i++) {
123    let S = "" +i;
124    while (S.length<16) S = "0"+S;
125    const P = generatePoint("Iden3_PedersenGenerator_"+S);
126    console.log(`[${P[0].toString()}, ${P[1].toString()}]`);
127}
128
129
130