-
Notifications
You must be signed in to change notification settings - Fork 58
/
babyjub.js
139 lines (111 loc) · 3.82 KB
/
babyjub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { getCurveFromName, Scalar } from "ffjavascript";
export default async function buildBabyJub() {
const bn128 = await getCurveFromName("bn128", true);
return new BabyJub(bn128.Fr);
}
class BabyJub {
constructor(F) {
this.F = F;
this.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
this.pm1d2 = Scalar.div(Scalar.sub(this.p, Scalar.e(1)), Scalar.e(2));
this.Generator = [
F.e("995203441582195749578291179787384436505546430278305826713579947235728471134"),
F.e("5472060717959818805561601436314318772137091100104008585924551046643952123905")
];
this.Base8 = [
F.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
F.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
this.order = Scalar.fromString("21888242871839275222246405745257275088614511777268538073601725287587578984328");
this.subOrder = Scalar.shiftRight(this.order, 3);
this.A = F.e("168700");
this.D = F.e("168696");
}
addPoint(a,b) {
const F = this.F;
const res = [];
/* does the equivalent of:
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);
*/
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(this.A, a[0])),
F.add(b[0], b[1])
);
const tau = F.mul(beta, gamma);
const dtau = F.mul(this.D, tau);
res[0] = F.div(
F.add(beta, gamma),
F.add(F.one, dtau)
);
res[1] = F.div(
F.add(delta, F.sub(F.mul(this.A,beta), gamma)),
F.sub(F.one, dtau)
);
return res;
}
mulPointEscalar(base, e) {
const F = this.F;
let res = [F.e("0"),F.e("1")];
let rem = e;
let exp = base;
while (! Scalar.isZero(rem)) {
if (Scalar.isOdd(rem)) {
res = this.addPoint(res, exp);
}
exp = this.addPoint(exp, exp);
rem = Scalar.shiftRight(rem, 1);
}
return res;
}
inSubgroup(P) {
const F = this.F;
if (!this.inCurve(P)) return false;
const res= this.mulPointEscalar(P, this.subOrder);
return (F.isZero(res[0]) && F.eq(res[1], F.one));
}
inCurve(P) {
const F = this.F;
const x2 = F.square(P[0]);
const y2 = F.square(P[1]);
if (!F.eq(
F.add(F.mul(this.A, x2), y2),
F.add(F.one, F.mul(F.mul(x2, y2), this.D)))) return false;
return true;
}
packPoint(P) {
const F = this.F;
const buff = new Uint8Array(32);
F.toRprLE(buff, 0, P[1]);
const n = F.toObject(P[0]);
if (Scalar.gt(n, this.pm1d2)) {
buff[31] = buff[31] | 0x80;
}
return buff;
}
unpackPoint(buff) {
const F = this.F;
let sign = false;
const P = new Array(2);
if (buff[31] & 0x80) {
sign = true;
buff[31] = buff[31] & 0x7F;
}
P[1] = F.fromRprLE(buff, 0);
if (Scalar.gt(F.toObject(P[1]), this.p)) return null;
const y2 = F.square(P[1]);
const x2 = F.div(
F.sub(F.one, y2),
F.sub(this.A, F.mul(this.D, y2))
);
const x2h = F.exp(x2, F.half);
if (! F.eq(F.one, x2h)) return null;
let x = F.sqrt(x2);
if (x == null) return null;
if (sign) x = F.neg(x);
P[0] = x;
return P;
}
}