arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/src/bigint.js
src/bigint.jsRaw
 1/* global BigInt */
 2const bigInt = require("big-integer");
 3
 4if (typeof(BigInt) != "undefined") {
 5    const wBigInt  = BigInt;
 6    wBigInt.prototype.modInv = function (q) {
 7        let t = wBigInt.zero;
 8        let r = q;
 9        let newt = wBigInt.one;
10        let newr = this;
11        while (newr!=wBigInt.zero) {
12            let q = r/newr;
13            [t, newt] = [newt, t-q*newt];
14            [r, newr] = [newr, r-q*newr];
15        }
16        if (t<wBigInt.zero) t += q;
17        return t;
18    };
19
20    wBigInt.prototype.add = function(b) {
21        return this+b;
22    };
23
24    wBigInt.prototype.minus = function(b) {
25        return this-b;
26    };
27
28    wBigInt.prototype.times = function(b) {
29        return this*b;
30    };
31
32    wBigInt.prototype.mod = function(q) {
33        return this%q;
34    };
35
36    wBigInt.prototype.square = function() {
37        return this*this;
38    };
39
40    wBigInt.prototype.double = function() {
41        return this+this;
42    };
43
44    wBigInt.prototype.isOdd = function() {
45        return (this & wBigInt.one) == 1;
46    };
47
48    wBigInt.prototype.isZero = function() {
49        return (this == wBigInt.zero);
50    };
51
52    wBigInt.prototype.isNegative = function() {
53        return this < wBigInt.zero;
54    };
55
56    wBigInt.prototype.shiftRight = function(f) {
57        return this >> wBigInt(f);
58    };
59
60    wBigInt.prototype.greaterOrEquals = function(b) {
61        return this >= b;
62    };
63
64    wBigInt.prototype.lesserOrEquals = function(b) {
65        return this <= b;
66    };
67
68    wBigInt.prototype.equals = function(b) {
69        return this == b;
70    };
71
72    wBigInt.one = BigInt(1);
73    wBigInt.zero = BigInt(0);
74
75    module.exports = wBigInt;
76} else {
77    module.exports = bigInt;
78}