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
  7    wBigInt.prototype.affine = function (q) {
  8        let aux = this;
  9        if (aux < 0) {
 10            if (aux <= -q) {
 11                aux = aux % q;
 12            }
 13            if (aux.isNegative()) {
 14                aux = aux.add(q);
 15            }
 16        } else {
 17            if (aux >= q) {
 18                aux = aux % q;
 19            }
 20        }
 21        return aux;
 22    };
 23
 24    wBigInt.prototype.modInv = function (q) {
 25        let t = wBigInt.zero;
 26        let r = q;
 27        let newt = wBigInt.one;
 28        let newr = this.affine(q);
 29        while (newr!=wBigInt.zero) {
 30            let q = r/newr;
 31            [t, newt] = [newt, t-q*newt];
 32            [r, newr] = [newr, r-q*newr];
 33        }
 34        if (t<wBigInt.zero) t += q;
 35        return t;
 36    };
 37
 38    wBigInt.prototype.add = function(b) {
 39        return this+b;
 40    };
 41
 42    wBigInt.prototype.minus = function(b) {
 43        return this-b;
 44    };
 45
 46    wBigInt.prototype.times = function(b) {
 47        return this*b;
 48    };
 49
 50    wBigInt.prototype.mod = function(q) {
 51        return this%q;
 52    };
 53
 54    wBigInt.prototype.square = function() {
 55        return this*this;
 56    };
 57
 58    wBigInt.prototype.double = function() {
 59        return this+this;
 60    };
 61
 62    wBigInt.prototype.isOdd = function() {
 63        return (this & wBigInt.one) == 1;
 64    };
 65
 66    wBigInt.prototype.isZero = function() {
 67        return (this == wBigInt.zero);
 68    };
 69
 70    wBigInt.prototype.isNegative = function() {
 71        return this < wBigInt.zero;
 72    };
 73
 74    wBigInt.prototype.shiftRight = function(f) {
 75        return this >> wBigInt(f);
 76    };
 77
 78    wBigInt.prototype.greaterOrEquals = function(b) {
 79        return this >= b;
 80    };
 81
 82    wBigInt.prototype.lesserOrEquals = function(b) {
 83        return this <= b;
 84    };
 85
 86    wBigInt.prototype.equals = function(b) {
 87/*        console.log("..");
 88        console.log(this);
 89        console.log(b);
 90        console.log(this == b);
 91        console.log(".."); */
 92        return this.valueOf() == b.valueOf();
 93    };
 94
 95    wBigInt.prototype.mulMod = function(q, b) {
 96        return this * b % q;
 97    };
 98
 99    wBigInt.one = BigInt(1);
100    wBigInt.zero = BigInt(0);
101
102    module.exports = wBigInt;
103} else {
104
105    bigInt.prototype.mulMod = function(q, b) {
106        return this.times(b).mod(q);
107    };
108
109    bigInt.prototype.affine = function (q) {
110        let aux = this;
111        if (aux.isNegative()) {
112            const nq = bigInt.zero.minus(q);
113            if (aux.lesserOrEquals(nq)) {
114                aux = aux.mod(q);
115            }
116            if (aux.isNegative()) {
117                aux = aux.add(q);
118            }
119        } else {
120            if (aux.greaterOrEquals(q)) {
121                aux = aux.mod(q);
122            }
123        }
124        return aux;
125    };
126
127    module.exports = bigInt;
128}