arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/src/babyjub.js
src/babyjub.jsRaw
  1const bn128 = require("snarkjs").bn128;
  2const bigInt = require("snarkjs").bigInt;
  3
  4exports.addPoint = addPoint;
  5exports.mulPointEscalar = mulPointEscalar;
  6exports.inCurve = inCurve;
  7exports.inSubgroup = inSubgroup;
  8exports.packPoint = packPoint;
  9exports.unpackPoint = unpackPoint;
 10exports.Base8 = [
 11    bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
 12    bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
 13];
 14exports.order = bigInt("21888242871839275222246405745257275088614511777268538073601725287587578984328");
 15exports.subOrder = exports.order.shr(3);
 16exports.p = bn128.r;
 17
 18
 19function addPoint(a,b) {
 20    const q = bn128.r;
 21    const cta = bigInt("168700");
 22    const d = bigInt("168696");
 23
 24    const res = [];
 25
 26    /* does the equivalent of:
 27     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);
 28    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);
 29    */
 30    res[0] = bigInt((bigInt(a[0]).mul(b[1]).add(bigInt(b[0]).mul(a[1]))).mul(bigInt(bigInt("1").add(d.mul(a[0]).mul(b[0]).mul(a[1]).mul(b[1]))).inverse(q))).affine(q);
 31    res[1] = bigInt((bigInt(a[1]).mul(b[1]).sub(cta.mul(a[0]).mul(b[0]))).mul(bigInt(bigInt("1").sub(d.mul(a[0]).mul(b[0]).mul(a[1]).mul(b[1]))).inverse(q))).affine(q);
 32
 33    return res;
 34}
 35
 36function mulPointEscalar(base, e) {
 37    let res = [bigInt("0"),bigInt("1")];
 38    let rem = bigInt(e);
 39    let exp = base;
 40
 41    while (! rem.isZero()) {
 42        if (rem.isOdd()) {
 43            res = addPoint(res, exp);
 44        }
 45        exp = addPoint(exp, exp);
 46        rem = rem.shr(1);
 47    }
 48
 49    return res;
 50}
 51
 52function inSubgroup(P) {
 53    if (!inCurve(P)) return false;
 54    const res= mulPointEscalar(P, exports.subOrder);
 55    return (res[0].equals(bigInt(0))) && (res[1].equals(bigInt(1)));
 56}
 57
 58function inCurve(P) {
 59    const F = bn128.Fr;
 60
 61    const a = bigInt("168700");
 62    const d = bigInt("168696");
 63
 64    const x2 = F.square(P[0]);
 65    const y2 = F.square(P[1]);
 66
 67    if (!F.equals(
 68        F.add(F.mul(a, x2), y2),
 69        F.add(F.one, F.mul(F.mul(x2, y2), d)))) return false;
 70
 71    return true;
 72}
 73
 74function packPoint(P) {
 75    const buff = bigInt.leInt2Buff(P[1], 32);
 76    if (P[0].greater(exports.p.shr(1))) {
 77        buff[31] = buff[31] | 0x80;
 78    }
 79    return buff;
 80}
 81
 82function unpackPoint(_buff) {
 83    const F = bn128.Fr;
 84
 85    const buff = Buffer.from(_buff);
 86    let sign = false;
 87    const P = new Array(2);
 88    if (buff[31] & 0x80) {
 89        sign = true;
 90        buff[31] = buff[31] & 0x7F;
 91    }
 92    P[1] = bigInt.leBuff2int(buff);
 93    if (P[1].greaterOrEquals(exports.p)) return null;
 94
 95    const a = bigInt("168700");
 96    const d = bigInt("168696");
 97
 98    const y2 = F.square(P[1]);
 99
100    let x = F.sqrt(F.div(
101        F.sub(F.one, y2),
102        F.sub(a, F.mul(d, y2))));
103
104    if (x == null) return null;
105
106    if (sign) x = F.neg(x);
107
108    P[0] = F.affine(x);
109
110    return P;
111}