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