snarkjs/src/bigint.js
src/bigint.jsRaw
1/* global BigInt */
2const bigInt = require("big-integer");
3
4let wBigInt;
5
6if (typeof(BigInt) != "undefined") {
7 wBigInt = BigInt;
8 wBigInt.one = wBigInt(1);
9 wBigInt.zero = wBigInt(0);
10
11 // Affine
12 wBigInt.genAffine = (q) => {
13 const nq = -q;
14 return (a) => {
15 let aux = a;
16 if (aux < 0) {
17 if (aux <= nq) {
18 aux = aux % q;
19 }
20 if (aux.isNegative()) {
21 aux = aux.add(q);
22 }
23 } else {
24 if (aux >= q) {
25 aux = aux % q;
26 }
27 }
28 return aux.valueOf();
29 };
30 };
31
32
33 // Inverse
34 wBigInt.genInverse = (q) => {
35 return (a) => {
36 let t = wBigInt.zero;
37 let r = q;
38 let newt = wBigInt.one;
39 let newr = wBigInt.affine(a, q);
40 while (newr!=wBigInt.zero) {
41 let q = r/newr;
42 [t, newt] = [newt, t-q*newt];
43 [r, newr] = [newr, r-q*newr];
44 }
45 if (t<wBigInt.zero) t += q;
46 return t;
47 };
48 };
49
50
51 // Add
52 wBigInt.genAdd = (q) => {
53 if (q) {
54 return (a,b) => (a+b) % q;
55 } else {
56 return (a,b) => a+b;
57 }
58 };
59
60 // Sub
61 wBigInt.genSub = (q) => {
62 if (q) {
63 return (a,b) => (a-b) % q;
64 } else {
65 return (a,b) => a-b;
66 }
67 };
68
69
70 // Neg
71 wBigInt.genNeg = (q) => {
72 if (q) {
73 return (a) => (-a) % q;
74 } else {
75 return (a) => -a;
76 }
77 };
78
79 // Mul
80 wBigInt.genMul = (q) => {
81 if (q) {
82 return (a,b) => (a*b) % q;
83 } else {
84 return (a,b) => a*b;
85 }
86 };
87
88 // Equals
89 wBigInt.genEquals = (q) => {
90 if (q) {
91 return (a,b) => (a.affine(q) == b.affine(q));
92 } else {
93 return (a,b) => a == b;
94 }
95 };
96
97 // Square
98 wBigInt.genSquare = (q) => {
99 if (q) {
100 return (a) => (a*a) %q;
101 } else {
102 return (a) => a*a;
103 }
104 };
105
106
107 // Double
108 wBigInt.genDouble = (q) => {
109 if (q) {
110 return (a) => (a+a) %q;
111 } else {
112 return (a) => a+a;
113 }
114 };
115
116 // IsZero
117 wBigInt.genIsZero = (q) => {
118 if (q) {
119 return (a) => (a.affine(q) == wBigInt.zero);
120 } else {
121 return (a) => a == wBigInt.zero;
122 }
123 };
124
125
126 // Other minor functions
127 wBigInt.prototype.isOdd = function() {
128 return (this & wBigInt.one) == wBigInt(1);
129 };
130
131 wBigInt.prototype.isNegative = function() {
132 return this < wBigInt.zero;
133 };
134
135 wBigInt.prototype.shiftRight = function(f) {
136 return this >> wBigInt(f);
137 };
138
139 wBigInt.prototype.greaterOrEquals = function(b) {
140 return this >= b;
141 };
142
143 wBigInt.prototype.lesserOrEquals = function(b) {
144 return this <= b;
145 };
146
147
148} else {
149
150 wBigInt = bigInt;
151
152 // Affine
153 wBigInt.genAffine = (q) => {
154 const nq = wBigInt.zero.minus(q);
155 return (a) => {
156 let aux = a;
157 if (aux.isNegative()) {
158 if (aux.lesserOrEquals(nq)) {
159 aux = aux.mod(q);
160 }
161 if (aux.isNegative()) {
162 aux = aux.add(q);
163 }
164 } else {
165 if (aux.greaterOrEquals(q)) {
166 aux = aux.mod(q);
167 }
168 }
169 return aux;
170 };
171 };
172
173
174 // Inverse
175 wBigInt.genInverse = (q) => {
176 return (a) => a.affine(q).modInv(q);
177 };
178
179 // Add
180 wBigInt.genAdd = (q) => {
181 if (q) {
182 return (a,b) => {
183 const r = a.add(b);
184 return r.greaterOrEquals(q) ? r.minus(q) : r;
185 };
186 } else {
187 return (a,b) => a.add(b);
188 }
189 };
190
191 // Sub
192 wBigInt.genSub = (q) => {
193 if (q) {
194 return (a,b) => a.greaterOrEquals(b) ? a.minus(b) : a.minus(b).add(q);
195 } else {
196 return (a,b) => a.minus(b);
197 }
198 };
199
200 wBigInt.genNeg = (q) => {
201 if (q) {
202 return (a) => a.isZero() ? a : q.minus(a);
203 } else {
204 return (a) => wBigInt.zero.minus(a);
205 }
206 };
207
208 // Mul
209 wBigInt.genMul = (q) => {
210 if (q) {
211 return (a,b) => a.times(b).mod(q);
212 } else {
213 return (a,b) => a.times(b);
214 }
215 };
216
217 // Square
218 wBigInt.genSquare = (q) => {
219 if (q) {
220 return (a) => a.square().mod(q);
221 } else {
222 return (a) => a.square();
223 }
224 };
225
226 // Double
227 wBigInt.genDouble = (q) => {
228 if (q) {
229 return (a) => a.add(a).mod(q);
230 } else {
231 return (a) => a.add(a);
232 }
233 };
234
235 // Equals
236 wBigInt.genEquals = (q) => {
237 if (q) {
238 return (a,b) => a.affine(q).equals(b.affine(q));
239 } else {
240 return (a,b) => a.equals(b);
241 }
242 };
243
244 // IsZero
245 wBigInt.genIsZero = (q) => {
246 if (q) {
247 return (a) => (a.affine(q).isZero());
248 } else {
249 return (a) => a.isZero();
250 }
251 };
252}
253
254
255
256wBigInt.affine = function(a, q) {
257 return wBigInt.genAffine(q)(a);
258};
259
260wBigInt.prototype.affine = function (q) {
261 return wBigInt.affine(this, q);
262};
263
264wBigInt.inverse = function(a, q) {
265 return wBigInt.genInverse(q)(a);
266};
267
268wBigInt.prototype.inverse = function (q) {
269 return wBigInt.genInverse(this, q);
270};
271
272wBigInt.add = function(a, b, q) {
273 return wBigInt.genAdd(q)(a,b);
274};
275
276wBigInt.prototype.add = function (a, q) {
277 return wBigInt.genAdd(q)(this, a);
278};
279
280wBigInt.sub = function(a, b, q) {
281 return wBigInt.genSub(q)(a,b);
282};
283
284wBigInt.prototype.sub = function (a, q) {
285 return wBigInt.genSub(q)(this, a);
286};
287
288wBigInt.neg = function(a, q) {
289 return wBigInt.genNeg(q)(a);
290};
291
292wBigInt.prototype.neg = function (q) {
293 return wBigInt.genNeg(q)(this);
294};
295
296wBigInt.mul = function(a, b, q) {
297 return wBigInt.genMul(q)(a,b);
298};
299
300wBigInt.prototype.mul = function (a, q) {
301 return wBigInt.genMul(q)(this, a);
302};
303
304wBigInt.equals = function(a, b, q) {
305 return wBigInt.genEquals(q)(a,b);
306};
307
308wBigInt.prototype.equals = function (a, q) {
309 return wBigInt.genEquals(q)(this, a);
310};
311
312wBigInt.square = function(a, q) {
313 return wBigInt.genSquare(q)(a);
314};
315
316wBigInt.prototype.square = function (q) {
317 return wBigInt.genSquare(q)(this);
318};
319
320wBigInt.double = function(a, q) {
321 return wBigInt.genDouble(q)(a);
322};
323
324wBigInt.prototype.double = function (q) {
325 return wBigInt.genDouble(q)(this);
326};
327
328wBigInt.isZero = function(a, q) {
329 return wBigInt.genIsZero(q)(a);
330};
331
332wBigInt.prototype.isZero = function (q) {
333 return wBigInt.genIsZero(q)(this);
334};
335
336
337module.exports = wBigInt;
338