mirror of
https://github.com/arnaucube/circomlib.git
synced 2026-02-06 18:56:43 +01:00
babyjub.js adapted
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const bn128 = require("snarkjs").bn128;
|
||||
const bigInt = require("snarkjs").bigInt;
|
||||
const bigInt = require("big-integer");
|
||||
const ZqField = require("fflib").ZqField;
|
||||
const utils = require("./utils.js");
|
||||
|
||||
exports.addPoint = addPoint;
|
||||
exports.mulPointEscalar = mulPointEscalar;
|
||||
@@ -16,14 +17,14 @@ exports.Base8 = [
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
exports.order = bigInt("21888242871839275222246405745257275088614511777268538073601725287587578984328");
|
||||
exports.subOrder = exports.order.shr(3);
|
||||
exports.p = bn128.r;
|
||||
exports.subOrder = exports.order.shiftRight(3);
|
||||
exports.p = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
exports.A = bigInt("168700");
|
||||
exports.D = bigInt("168696");
|
||||
|
||||
|
||||
function addPoint(a,b) {
|
||||
const q = bn128.r;
|
||||
const F = new ZqField(exports.p);
|
||||
|
||||
const res = [];
|
||||
|
||||
@@ -31,8 +32,25 @@ function addPoint(a,b) {
|
||||
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);
|
||||
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);
|
||||
*/
|
||||
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);
|
||||
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);
|
||||
|
||||
const beta = F.mul(a[0],b[1]);
|
||||
const gamma = F.mul(a[1],b[0]);
|
||||
const delta = F.mul(
|
||||
F.sub(a[1], F.mul(exports.A, a[0])),
|
||||
F.add(b[0], b[1])
|
||||
);
|
||||
const tau = F.mul(beta, gamma);
|
||||
const dtau = F.mul(exports.D, tau);
|
||||
|
||||
res[0] = F.div(
|
||||
F.add(beta, gamma),
|
||||
F.add(bigInt.one, dtau)
|
||||
);
|
||||
|
||||
res[1] = F.div(
|
||||
F.add(delta, F.sub(F.mul(exports.A,beta), gamma)),
|
||||
F.sub(bigInt.one, dtau)
|
||||
);
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -47,7 +65,7 @@ function mulPointEscalar(base, e) {
|
||||
res = addPoint(res, exp);
|
||||
}
|
||||
exp = addPoint(exp, exp);
|
||||
rem = rem.shr(1);
|
||||
rem = rem.shiftRight(1);
|
||||
}
|
||||
|
||||
return res;
|
||||
@@ -60,12 +78,12 @@ function inSubgroup(P) {
|
||||
}
|
||||
|
||||
function inCurve(P) {
|
||||
const F = bn128.Fr;
|
||||
const F = new ZqField(exports.p);
|
||||
|
||||
const x2 = F.square(P[0]);
|
||||
const y2 = F.square(P[1]);
|
||||
|
||||
if (!F.equals(
|
||||
if (!F.eq(
|
||||
F.add(F.mul(exports.A, x2), y2),
|
||||
F.add(F.one, F.mul(F.mul(x2, y2), exports.D)))) return false;
|
||||
|
||||
@@ -73,15 +91,15 @@ function inCurve(P) {
|
||||
}
|
||||
|
||||
function packPoint(P) {
|
||||
const buff = bigInt.leInt2Buff(P[1], 32);
|
||||
if (P[0].greater(exports.p.shr(1))) {
|
||||
const buff = utils.leInt2Buff(P[1], 32);
|
||||
if (P[0].greater(exports.p.shiftRight(1))) {
|
||||
buff[31] = buff[31] | 0x80;
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
||||
function unpackPoint(_buff) {
|
||||
const F = bn128.Fr;
|
||||
const F = new ZqField(exports.p);
|
||||
|
||||
const buff = Buffer.from(_buff);
|
||||
let sign = false;
|
||||
@@ -90,7 +108,7 @@ function unpackPoint(_buff) {
|
||||
sign = true;
|
||||
buff[31] = buff[31] & 0x7F;
|
||||
}
|
||||
P[1] = bigInt.leBuff2int(buff);
|
||||
P[1] = utils.leBuff2int(buff);
|
||||
if (P[1].greaterOrEquals(exports.p)) return null;
|
||||
|
||||
const y2 = F.square(P[1]);
|
||||
@@ -103,7 +121,7 @@ function unpackPoint(_buff) {
|
||||
|
||||
if (sign) x = F.neg(x);
|
||||
|
||||
P[0] = F.affine(x);
|
||||
P[0] = x;
|
||||
|
||||
return P;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user