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