arnaucube
arnaucube/snarkjsPublic Self-hosted repository
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    // Shr
 89    wBigInt.genShr = () => {
 90        return (a,b) => a >> wBigInt(b);
 91    };
 92
 93    // Shl
 94    wBigInt.genShl = (q) => {
 95        if (q) {
 96            return (a,b) => (a << wBigInt(b)) % q;
 97        } else {
 98            return (a,b) => a << wBigInt(b);
 99        }
100    };
101
102    // Equals
103    wBigInt.genEquals = (q) => {
104        if (q) {
105            return (a,b) => (a.affine(q) == b.affine(q));
106        } else {
107            return (a,b) => a == b;
108        }
109    };
110
111    // Square
112    wBigInt.genSquare = (q) => {
113        if (q) {
114            return (a) => (a*a) %q;
115        } else {
116            return (a) => a*a;
117        }
118    };
119
120
121    // Double
122    wBigInt.genDouble = (q) => {
123        if (q) {
124            return (a) => (a+a) %q;
125        } else {
126            return (a) => a+a;
127        }
128    };
129
130    // IsZero
131    wBigInt.genIsZero = (q) => {
132        if (q) {
133            return (a) => (a.affine(q) == wBigInt.zero);
134        } else {
135            return (a) =>  a == wBigInt.zero;
136        }
137    };
138
139
140    // Other minor functions
141    wBigInt.prototype.isOdd = function() {
142        return (this & wBigInt.one) == wBigInt(1);
143    };
144
145    wBigInt.prototype.isNegative = function() {
146        return this < wBigInt.zero;
147    };
148
149    wBigInt.prototype.and = function(m) {
150        return this & m;
151    };
152
153    wBigInt.prototype.mod = function(c) {
154        return this % c;
155    };
156
157    wBigInt.prototype.modPow = function(e, m) {
158        return this ** e % m;
159    };
160
161    wBigInt.prototype.greaterOrEquals = function(b) {
162        return this >= b;
163    };
164
165    wBigInt.prototype.greater = function(b) {
166        return this > b;
167    };
168    wBigInt.prototype.gt = wBigInt.prototype.greater;
169
170    wBigInt.prototype.lesserOrEquals = function(b) {
171        return this <= b;
172    };
173
174    wBigInt.prototype.lesser = function(b) {
175        return this < b;
176    };
177    wBigInt.prototype.lt = wBigInt.prototype.lesser;
178
179    wBigInt.prototype.equals = function(b) {
180        return this.valueOf == b.valueOf;
181    };
182    wBigInt.prototype.eq = wBigInt.prototype.equals;
183
184} else {
185
186    wBigInt = bigInt;
187
188    // Affine
189    wBigInt.genAffine = (q) => {
190        const nq = wBigInt.zero.minus(q);
191        return (a) => {
192            let aux = a;
193            if (aux.isNegative()) {
194                if (aux.lesserOrEquals(nq)) {
195                    aux = aux.mod(q);
196                }
197                if (aux.isNegative()) {
198                    aux = aux.add(q);
199                }
200            } else {
201                if (aux.greaterOrEquals(q)) {
202                    aux = aux.mod(q);
203                }
204            }
205            return aux;
206        };
207    };
208
209
210    // Inverse
211    wBigInt.genInverse = (q) => {
212        return (a) => a.affine(q).modInv(q);
213    };
214
215    // Add
216    wBigInt.genAdd = (q) => {
217        if (q) {
218            return (a,b) => {
219                const r = a.add(b);
220                return r.greaterOrEquals(q) ? r.minus(q) : r;
221            };
222        } else {
223            return (a,b) => a.add(b);
224        }
225    };
226
227    // Sub
228    wBigInt.genSub = (q) => {
229        if (q) {
230            return (a,b) => a.greaterOrEquals(b) ? a.minus(b) : a.minus(b).add(q);
231        } else {
232            return (a,b) => a.minus(b);
233        }
234    };
235
236    wBigInt.genNeg = (q) => {
237        if (q) {
238            return (a) => a.isZero() ? a : q.minus(a);
239        } else {
240            return (a) => wBigInt.zero.minus(a);
241        }
242    };
243
244    // Mul
245    wBigInt.genMul = (q) => {
246        if (q) {
247            return (a,b) => a.times(b).mod(q);
248        } else {
249            return (a,b) => a.times(b);
250        }
251    };
252
253    // Shr
254    wBigInt.genShr = () => {
255        return (a,b) => a.shiftRight(wBigInt(b).value);
256    };
257
258    // Shr
259    wBigInt.genShl = (q) => {
260        if (q) {
261            return (a,b) => a.shiftLeft(wBigInt(b).value).mod(q);
262        } else {
263            return (a,b) => a.shiftLeft(wBigInt(b).value);
264        }
265    };
266
267    // Square
268    wBigInt.genSquare = (q) => {
269        if (q) {
270            return (a) => a.square().mod(q);
271        } else {
272            return (a) => a.square();
273        }
274    };
275
276    // Double
277    wBigInt.genDouble = (q) => {
278        if (q) {
279            return (a) => a.add(a).mod(q);
280        } else {
281            return (a) => a.add(a);
282        }
283    };
284
285    // Equals
286    wBigInt.genEquals = (q) => {
287        if (q) {
288            return (a,b) => a.affine(q).equals(b.affine(q));
289        } else {
290            return (a,b) => a.equals(b);
291        }
292    };
293
294    // IsZero
295    wBigInt.genIsZero = (q) => {
296        if (q) {
297            return (a) => (a.affine(q).isZero());
298        } else {
299            return (a) =>  a.isZero();
300        }
301    };
302}
303
304
305
306wBigInt.affine = function(a, q) {
307    return wBigInt.genAffine(q)(a);
308};
309
310wBigInt.prototype.affine = function (q) {
311    return wBigInt.affine(this, q);
312};
313
314wBigInt.inverse = function(a, q) {
315    return wBigInt.genInverse(q)(a);
316};
317
318wBigInt.prototype.inverse = function (q) {
319    return wBigInt.genInverse(this, q);
320};
321
322wBigInt.add = function(a, b, q) {
323    return wBigInt.genAdd(q)(a,b);
324};
325
326wBigInt.prototype.add = function (a, q) {
327    return wBigInt.genAdd(q)(this, a);
328};
329
330wBigInt.sub = function(a, b, q) {
331    return wBigInt.genSub(q)(a,b);
332};
333
334wBigInt.prototype.sub = function (a, q) {
335    return wBigInt.genSub(q)(this, a);
336};
337
338wBigInt.neg = function(a, q) {
339    return wBigInt.genNeg(q)(a);
340};
341
342wBigInt.prototype.neg = function (q) {
343    return wBigInt.genNeg(q)(this);
344};
345
346wBigInt.mul = function(a, b, q) {
347    return wBigInt.genMul(q)(a,b);
348};
349
350wBigInt.prototype.mul = function (a, q) {
351    return wBigInt.genMul(q)(this, a);
352};
353
354wBigInt.shr = function(a, b, q) {
355    return wBigInt.genShr(q)(a,b);
356};
357
358wBigInt.prototype.shr = function (a, q) {
359    return wBigInt.genShr(q)(this, a);
360};
361
362wBigInt.shl = function(a, b, q) {
363    return wBigInt.genShl(q)(a,b);
364};
365
366wBigInt.prototype.shl = function (a, q) {
367    return wBigInt.genShl(q)(this, a);
368};
369
370wBigInt.equals = function(a, b, q) {
371    return wBigInt.genEquals(q)(a,b);
372};
373
374wBigInt.prototype.equals = function (a, q) {
375    return wBigInt.genEquals(q)(this, a);
376};
377
378wBigInt.square = function(a, q) {
379    return wBigInt.genSquare(q)(a);
380};
381
382wBigInt.prototype.square = function (q) {
383    return wBigInt.genSquare(q)(this);
384};
385
386wBigInt.double = function(a, q) {
387    return wBigInt.genDouble(q)(a);
388};
389
390wBigInt.prototype.double = function (q) {
391    return wBigInt.genDouble(q)(this);
392};
393
394wBigInt.isZero = function(a, q) {
395    return wBigInt.genIsZero(q)(a);
396};
397
398wBigInt.prototype.isZero = function (q) {
399    return wBigInt.genIsZero(q)(this);
400};
401
402
403module.exports = wBigInt;
404