You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

big-integer

Package Overview
Dependencies
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.4.4 to 1.4.5

53

BigInteger.js

@@ -471,16 +471,33 @@ "use strict";

// Reference: http://en.wikipedia.org/wiki/Bitwise_operation#Mathematical_equivalents
function bitwise(x, y, fn) {
var sum = ZERO;
var limit = max(x.abs(), y.abs());
var n = 0, _2n = ONE;
while (_2n.lesserOrEquals(limit)) {
var xMod, yMod;
xMod = x.over(_2n).isEven() ? 0 : 1;
yMod = y.over(_2n).isEven() ? 0 : 1;
y = parseInput(y);
var xSign = x.isNegative(), ySign = y.isNegative();
var xRem = xSign ? x.not() : x,
yRem = ySign ? y.not() : y;
var xBits = [], yBits = [];
var xStop = false, yStop = false;
while (!xStop || !yStop) {
if (xRem.isZero()) { // virtual sign extension for simulating two's complement
xStop = true;
xBits.push(xSign ? 1 : 0);
}
else if (xSign) xBits.push(xRem.isEven() ? 1 : 0); // two's complement for negative numbers
else xBits.push(xRem.isEven() ? 0 : 1);
sum = sum.add(_2n.times(fn(xMod, yMod)));
if (yRem.isZero()) {
yStop = true;
yBits.push(ySign ? 1 : 0);
}
else if (ySign) yBits.push(yRem.isEven() ? 1 : 0);
else yBits.push(yRem.isEven() ? 0 : 1);
_2n = fastMultiply(_2n, 2);
xRem = xRem.over(2);
yRem = yRem.over(2);
}
var result = [];
for (var i = 0; i < xBits.length; i++) result.push(fn(xBits[i], yBits[i]));
var sum = bigInt(result.pop()).negate().times(bigInt(2).pow(result.length));
while (result.length) {
sum = sum.add(bigInt(result.pop()).times(bigInt(2).pow(result.length)));
}
return sum;

@@ -490,22 +507,15 @@ }

BigInteger.prototype.not = function () {
var body = bitwise(this, this, function (xMod) { return (xMod + 1) % 2; });
return !this.sign ? body.negate() : body;
return this.negate().minus(1);
};
BigInteger.prototype.and = function (n) {
n = parseInput(n);
var body = bitwise(this, n, function (xMod, yMod) { return xMod * yMod; });
return this.sign && n.sign ? body.negate() : body;
return bitwise(this, n, function (a, b) { return a & b });
};
BigInteger.prototype.or = function (n) {
n = parseInput(n);
var body = bitwise(this, n, function (xMod, yMod) { return (xMod + yMod + xMod * yMod) % 2 });
return this.sign || n.sign ? body.negate() : body;
return bitwise(this, n, function (a, b) { return a | b; });
};
BigInteger.prototype.xor = function (n) {
n = parseInput(n);
var body = bitwise(this, n, function (xMod, yMod) { return (xMod + yMod) % 2; });
return this.sign ^ n.sign ? body.negate() : body;
return bitwise(this, n, function (a, b) { return a ^ b });
};

@@ -670,2 +680,3 @@

fnReturn.randBetween = randBetween;
fnReturn.isInstance = function (x) { return x instanceof BigInteger; };
fnReturn.min = min;

@@ -672,0 +683,0 @@ fnReturn.max = max;

@@ -1,1 +0,1 @@

"use strict";var bigInt=function(){function i(e,t){this.value=e,this.sign=t}function s(e){while(e[e.length-1]===0&&e.length>1)e.pop();return e}function o(t,n){var r=n<0;if(t.sign!==r)return r?u(t.abs(),-n):u(t.abs(),n).negate();r&&(n=-n);var o=t.value,a=[],f=0;for(var l=0;l<o.length||f>0;l++){var c=(o[l]||0)+(l>0?0:n)+f;f=c>=e?1:0,a.push(c%e)}return new i(s(a),t.sign)}function u(t,n){var r=t.value;if(r.length===1)return r=r[0],t.sign&&(r=-r),new i([Math.abs(r-n)],r-n<0);if(t.sign!==n<0)return o(t,-n);var u=!1;t.sign&&(u=!0);if(r.length===1&&r[0]<n)return new i([n-r[0]],!u);u&&(n=-n);var a=[],f=0;for(var l=0;l<r.length;l++){var c=r[l]-f-(l>0?0:n);f=c<0?1:0,a.push(f*e+c)}return new i(s(a),u)}function a(t,n){var r=[],i=0;for(var s=0;s<t.length;s++){i+=n*t[s];var o=Math.floor(i/e);r[s]=i-o*e|0,i=o}return r[t.length]=i|0,r}function f(e,t){var n=a(e.value,t<0?-t:t);return new i(s(n),t<0?!e.sign:e.sign)}function l(t,n){var r=[];for(var i=0;i<t.length;i++)r[i]=0;var s=0;for(var i=t.length-1;i>=0;i--){var o=s*e+t[i],u=Math.floor(o/n);s=o-u*n,r[i]=u|0}return{quotient:r,remainder:s|0}}function c(e,t){if(t===0)throw new Error("Cannot divide by zero.");var n=l(e.value,t<0?-t:t);return{quotient:new i(s(n.quotient),t<0?!e.sign:e.sign),remainder:new i([n.remainder],e.sign)}}function h(t){return(typeof t=="number"||typeof t=="string")&&+Math.abs(t)<=e||t instanceof i&&t.value.length<=1}function p(e,t){return e=N(e).abs(),t=N(t).abs(),e.equals(t)?e:e.equals(S)?t:t.equals(S)?e:e.isEven()?t.isOdd()?p(e.divide(2),t):p(e.divide(2),t.divide(2)).multiply(2):t.isEven()?p(e,t.divide(2)):e.greater(t)?p(e.subtract(t).divide(2),t):p(t.subtract(e).divide(2),e)}function d(e,t){return e=N(e).abs(),t=N(t).abs(),e.multiply(t).divide(p(e,t))}function v(e,t){return e=N(e),t=N(t),e.greater(t)?e:t}function m(e,t){return e=N(e),t=N(t),e.lesser(t)?e:t}function g(t,n){t=N(t),n=N(n);var r=m(t,n),s=v(t,n),o=s.subtract(r),u=o.value.length-1,a=[],f=!0;for(var l=u;l>=0;l--){var c=f?o.value[l]:e,h=Math.floor(Math.random()*c);a.unshift(h),h<c&&(f=!1)}return r.add(new i(a,!1))}function E(e,t,n){var r=S,i=v(e.abs(),t.abs()),s=0,o=x;while(o.lesserOrEquals(i)){var u,a;u=e.over(o).isEven()?0:1,a=t.over(o).isEven()?0:1,r=r.add(o.times(n(u,a))),o=f(o,2)}return r}function N(n){if(n instanceof i)return n;if(Math.abs(+n)<e&&+n===(+n|0)){var o=+n;return new i([Math.abs(o)],o<0||1/o===-Infinity)}n+="";var u=r.positive,o=[];n[0]==="-"&&(u=r.negative,n=n.slice(1));var n=n.split(/e/i);if(n.length>2)throw new Error("Invalid integer: "+n.join("e"));if(n[1]){var a=n[1];a[0]==="+"&&(a=a.slice(1)),a=N(a);var f=n[0].indexOf(".");f>=0&&(a=a.minus(n[0].length-f),n[0]=n[0].slice(0,f)+n[0].slice(f+1));if(a.lesser(0))throw new Error("Cannot include negative exponent part for integers");while(a.notEquals(0))n[0]+="0",a=a.prev()}n=n[0],n==="-0"&&(n="0");var l=/^([0-9][0-9]*)$/.test(n);if(!l)throw new Error("Invalid integer: "+n);while(n.length){var c=n.length>t?n.length-t:0;o.push(+n.slice(c)),n=n.slice(0,c)}return new i(s(o),u)}function k(e){var t=e.value;return t.length===1&&t[0]<=36?"0123456789abcdefghijklmnopqrstuvwxyz".charAt(t[0]):"<"+t+">"}function L(e,t){t=bigInt(t);if(t.equals(0)){if(e.equals(0))return"0";throw new Error("Cannot convert nonzero numbers to base 0.")}if(t.equals(-1))return e.equals(0)?"0":e.lesser(0)?Array(1-e).join("10"):"1"+Array(+e).join("01");var n="";e.isNegative()&&t.isPositive()&&(n="-",e=e.abs());if(t.equals(1))return e.equals(0)?"0":n+Array(+e+1).join(1);var r=[],i=e,s;while(i.lesser(0)||i.compareAbs(t)>=0){s=i.divmod(t),i=s.quotient;var o=s.remainder;o.lesser(0)&&(o=t.minus(o).abs(),i=i.next()),r.push(k(o))}return r.push(k(i)),n+r.reverse().join("")}var e=1e7,t=7,n="0000000",r={positive:!1,negative:!0};i.prototype.negate=function(){return new i(this.value,!this.sign)},i.prototype.abs=function(){return new i(this.value,r.positive)},i.prototype.add=function(t){if(h(t))return o(this,+t);t=N(t);if(this.sign!==t.sign)return this.sign===r.positive?this.abs().subtract(t.abs()):t.abs().subtract(this.abs());var n=this.value,u=t.value,a=[],f=0,l=Math.max(n.length,u.length);for(var c=0;c<l||f>0;c++){var p=(n[c]||0)+(u[c]||0)+f;f=p>=e?1:0,a.push(p%e)}return new i(s(a),this.sign)},i.prototype.plus=i.prototype.add,i.prototype.subtract=function(t){if(h(t))return u(this,+t);t=N(t);if(this.sign!==t.sign)return this.add(t.negate());if(this.sign===r.negative)return t.negate().subtract(this.negate());if(this.compare(t)<0)return t.subtract(this).negate();var n=this.value,o=t.value,a=[],f=0,l=Math.max(n.length,o.length);for(var c=0;c<l;c++){var p=n[c]||0,d=o[c]||0,v=p-f;f=v<d?1:0,a.push(f*e+v-d)}return new i(s(a),r.positive)},i.prototype.minus=i.prototype.subtract,i.prototype.multiply=function(t){if(h(t))return f(this,+t);t=N(t);var n=this.sign!==t.sign,r=this.value,o=t.value,u=[];for(var a=r.length+o.length;a>0;a--)u.push(0);for(var a=0;a<r.length;a++){var l=r[a];for(var c=0;c<o.length;c++){var p=o[c],d=l*p+u[a+c],v=Math.floor(d/e);u[a+c]=d-v*e,u[a+c+1]+=v}}return new i(s(u),n)},i.prototype.times=i.prototype.multiply,i.prototype.divmod=function(t){if(h(t))return c(this,+t);t=N(t);var n=this.sign!==t.sign;if(t.equals(S))throw new Error("Cannot divide by zero");if(this.equals(S))return{quotient:new i([0],r.positive),remainder:new i([0],r.positive)};var o=this.value,u=t.value,f=[0];for(var p=0;p<u.length;p++)f[p]=0;var d=u[u.length-1],v=Math.ceil(e/2/d),m=a(o,v),g=a(u,v);d=g[u.length-1];for(var y=o.length-u.length;y>=0;y--){var b=e-1;m[y+u.length]!==d&&(b=Math.floor((m[y+u.length]*e+m[y+u.length-1])/d));var w=0,E=0;for(var p=0;p<g.length;p++){w+=b*g[p];var x=Math.floor(w/e);E+=m[y+p]-(w-x*e),w=x,E<0?(m[y+p]=E+e|0,E=-1):(m[y+p]=E|0,E=0)}while(E!==0){b-=1;var w=0;for(var p=0;p<g.length;p++)w+=m[y+p]-e+g[p],w<0?(m[y+p]=w+e|0,w=0):(m[y+p]=w|0,w=1);E+=w}f[y]=b|0}return m=l(m,v).quotient,{quotient:new i(s(f),n),remainder:new i(s(m),this.sign)}},i.prototype.divide=function(e){return this.divmod(e).quotient},i.prototype.over=i.prototype.divide,i.prototype.mod=function(e){return this.divmod(e).remainder},i.prototype.remainder=i.prototype.mod,i.prototype.pow=function(e){e=N(e);var t=this,n=e,r=x;if(n.equals(S))return r;if(t.equals(S)||n.lesser(S))return S;for(;;){n.isOdd()&&(r=r.times(t)),n=n.divide(2);if(n.equals(S))break;t=t.times(t)}return r},i.prototype.modPow=function(e,t){e=N(e),t=N(t);if(t.equals(S))throw new Error("Cannot take modPow with modulus 0");var n=x,r=this.mod(t);if(r.equals(S))return S;while(e.greater(0))e.isOdd()&&(n=n.multiply(r).mod(t)),e=e.divide(2),r=r.square().mod(t);return n},i.prototype.square=function(){return this.multiply(this)},i.prototype.next=function(){return o(this,1)},i.prototype.prev=function(){return u(this,1)},i.prototype.compare=function(e){var t=this,n=N(e);if(t.value.length===1&&n.value.length===1&&t.value[0]===0&&n.value[0]===0)return 0;if(n.sign!==t.sign)return t.sign===r.positive?1:-1;var i=t.sign===r.positive?1:-1,s=t.value,o=n.value,u=Math.max(s.length,o.length)-1;for(var a=u;a>=0;a--){var f=s[a]||0,l=o[a]||0;if(f>l)return 1*i;if(l>f)return-1*i}return 0},i.prototype.compareAbs=function(e){return this.abs().compare(e.abs())},i.prototype.equals=function(e){return this.compare(e)===0},i.prototype.notEquals=function(e){return!this.equals(e)},i.prototype.lesser=function(e){return this.compare(e)<0},i.prototype.greater=function(e){return this.compare(e)>0},i.prototype.greaterOrEquals=function(e){return this.compare(e)>=0},i.prototype.lesserOrEquals=function(e){return this.compare(e)<=0},i.prototype.compareTo=i.prototype.compare,i.prototype.lt=i.prototype.lesser,i.prototype.leq=i.prototype.lesserOrEquals,i.prototype.gt=i.prototype.greater,i.prototype.geq=i.prototype.greaterOrEquals,i.prototype.eq=i.prototype.equals,i.prototype.neq=i.prototype.notEquals,i.prototype.isPositive=function(){return this.value.length===1&&this.value[0]===0?!1:this.sign===r.positive},i.prototype.isNegative=function(){return this.value.length===1&&this.value[0]===0?!1:this.sign===r.negative},i.prototype.isEven=function(){return this.value[0]%2===0},i.prototype.isOdd=function(){return this.value[0]%2===1},i.prototype.isUnit=function(){return this.value.length===1&&this.value[0]===1},i.prototype.isZero=function(){return this.value.length===1&&this.value[0]===0},i.prototype.isDivisibleBy=function(e){return e=N(e),e.isZero()?!1:this.mod(e).equals(S)},i.prototype.isPrime=function(){var e=this.abs(),t=e.prev();if(e.isUnit())return!1;if(e.equals(2)||e.equals(3)||e.equals(5))return!0;if(e.isEven()||e.isDivisibleBy(3)||e.isDivisibleBy(5))return!1;if(e.lesser(25))return!0;var n=[2,3,5,7,11,13,17,19],r=t,i,s,o,u;while(r.isEven())r=r.divide(2);for(o=0;o<n.length;o++){u=bigInt(n[o]).modPow(r,e);if(u.equals(x)||u.equals(t))continue;for(s=!0,i=r;s&&i.lesser(t);i=i.multiply(2))u=u.square().mod(e),u.equals(t)&&(s=!1);if(s)return!1}return!0};var y=[1];while(y[y.length-1]<=e)y.push(2*y[y.length-1]);var b=y.length,w=y[b-1];i.prototype.shiftLeft=function(e){if(!h(e))return e.isNegative()?this.shiftRight(e.abs()):this.times(bigInt(2).pow(e));e=+e;if(e<0)return this.shiftRight(-e);var t=this;while(e>=b)t=f(t,w),e-=b-1;return f(t,y[e])},i.prototype.shiftRight=function(e){if(!h(e))return e.isNegative()?this.shiftLeft(e.abs()):this.over(bigInt(2).pow(e));e=+e;if(e<0)return this.shiftLeft(-e);var t=this;while(e>=b){if(t.equals(S))return t;t=c(t,w).quotient,e-=b-1}return c(t,y[e]).quotient},i.prototype.not=function(){var e=E(this,this,function(e){return(e+1)%2});return this.sign?e:e.negate()},i.prototype.and=function(e){e=N(e);var t=E(this,e,function(e,t){return e*t});return this.sign&&e.sign?t.negate():t},i.prototype.or=function(e){e=N(e);var t=E(this,e,function(e,t){return(e+t+e*t)%2});return this.sign||e.sign?t.negate():t},i.prototype.xor=function(e){e=N(e);var t=E(this,e,function(e,t){return(e+t)%2});return this.sign^e.sign?t.negate():t},i.prototype.toString=function(e){e===undefined&&(e=10);if(e!==10)return L(this,e);var t=this,i="",s=t.value.length;if(s===0||s===1&&t.value[0]===0)return"0";s-=1,i=t.value[s].toString();while(--s>=0){var o=t.value[s].toString();i+=n.slice(o.length)+o}var u=t.sign===r.positive?"":"-";return u+i},i.prototype.toJSNumber=function(){return this.valueOf()},i.prototype.valueOf=function(){return this.value.length===1?this.sign?-this.value[0]:this.value[0]:+this.toString()};var S=new i([0],r.positive),x=new i([1],r.positive),T=new i([1],r.negative),C=function(e,t){function o(e){var t=e[i].toLowerCase();if(i===0&&e[i]==="-"){s=!0;return}if(/[0-9]/.test(t))r.push(N(t));else if(/[a-z]/.test(t))r.push(N(t.charCodeAt(0)-87));else{if(t!=="<")throw new Error(t+" is not a valid character");var n=i;do i++;while(e[i]!==">");r.push(N(e.slice(n+1,i)))}}t=N(t);var n=S,r=[],i,s=!1;for(i=0;i<e.length;i++)o(e);r.reverse();for(i=0;i<r.length;i++)n=n.add(r[i].times(t.pow(i)));return s?n.negate():n},A=function(e,t){return typeof e=="undefined"?S:typeof t!="undefined"?C(e,t):N(e)};return A.zero=S,A.one=x,A.minusOne=T,A.randBetween=g,A.min=m,A.max=v,A.gcd=p,A.lcm=d,A}();typeof module!="undefined"&&(module.exports=bigInt);
"use strict";var bigInt=function(){function i(e,t){this.value=e,this.sign=t}function s(e){while(e[e.length-1]===0&&e.length>1)e.pop();return e}function o(t,n){var r=n<0;if(t.sign!==r)return r?u(t.abs(),-n):u(t.abs(),n).negate();r&&(n=-n);var o=t.value,a=[],f=0;for(var l=0;l<o.length||f>0;l++){var c=(o[l]||0)+(l>0?0:n)+f;f=c>=e?1:0,a.push(c%e)}return new i(s(a),t.sign)}function u(t,n){var r=t.value;if(r.length===1)return r=r[0],t.sign&&(r=-r),new i([Math.abs(r-n)],r-n<0);if(t.sign!==n<0)return o(t,-n);var u=!1;t.sign&&(u=!0);if(r.length===1&&r[0]<n)return new i([n-r[0]],!u);u&&(n=-n);var a=[],f=0;for(var l=0;l<r.length;l++){var c=r[l]-f-(l>0?0:n);f=c<0?1:0,a.push(f*e+c)}return new i(s(a),u)}function a(t,n){var r=[],i=0;for(var s=0;s<t.length;s++){i+=n*t[s];var o=Math.floor(i/e);r[s]=i-o*e|0,i=o}return r[t.length]=i|0,r}function f(e,t){var n=a(e.value,t<0?-t:t);return new i(s(n),t<0?!e.sign:e.sign)}function l(t,n){var r=[];for(var i=0;i<t.length;i++)r[i]=0;var s=0;for(var i=t.length-1;i>=0;i--){var o=s*e+t[i],u=Math.floor(o/n);s=o-u*n,r[i]=u|0}return{quotient:r,remainder:s|0}}function c(e,t){if(t===0)throw new Error("Cannot divide by zero.");var n=l(e.value,t<0?-t:t);return{quotient:new i(s(n.quotient),t<0?!e.sign:e.sign),remainder:new i([n.remainder],e.sign)}}function h(t){return(typeof t=="number"||typeof t=="string")&&+Math.abs(t)<=e||t instanceof i&&t.value.length<=1}function p(e,t){return e=N(e).abs(),t=N(t).abs(),e.equals(t)?e:e.equals(S)?t:t.equals(S)?e:e.isEven()?t.isOdd()?p(e.divide(2),t):p(e.divide(2),t.divide(2)).multiply(2):t.isEven()?p(e,t.divide(2)):e.greater(t)?p(e.subtract(t).divide(2),t):p(t.subtract(e).divide(2),e)}function d(e,t){return e=N(e).abs(),t=N(t).abs(),e.multiply(t).divide(p(e,t))}function v(e,t){return e=N(e),t=N(t),e.greater(t)?e:t}function m(e,t){return e=N(e),t=N(t),e.lesser(t)?e:t}function g(t,n){t=N(t),n=N(n);var r=m(t,n),s=v(t,n),o=s.subtract(r),u=o.value.length-1,a=[],f=!0;for(var l=u;l>=0;l--){var c=f?o.value[l]:e,h=Math.floor(Math.random()*c);a.unshift(h),h<c&&(f=!1)}return r.add(new i(a,!1))}function E(e,t,n){t=N(t);var r=e.isNegative(),i=t.isNegative(),s=r?e.not():e,o=i?t.not():t,u=[],a=[],f=!1,l=!1;while(!f||!l)s.isZero()?(f=!0,u.push(r?1:0)):r?u.push(s.isEven()?1:0):u.push(s.isEven()?0:1),o.isZero()?(l=!0,a.push(i?1:0)):i?a.push(o.isEven()?1:0):a.push(o.isEven()?0:1),s=s.over(2),o=o.over(2);var c=[];for(var h=0;h<u.length;h++)c.push(n(u[h],a[h]));var p=bigInt(c.pop()).negate().times(bigInt(2).pow(c.length));while(c.length)p=p.add(bigInt(c.pop()).times(bigInt(2).pow(c.length)));return p}function N(n){if(n instanceof i)return n;if(Math.abs(+n)<e&&+n===(+n|0)){var o=+n;return new i([Math.abs(o)],o<0||1/o===-Infinity)}n+="";var u=r.positive,o=[];n[0]==="-"&&(u=r.negative,n=n.slice(1));var n=n.split(/e/i);if(n.length>2)throw new Error("Invalid integer: "+n.join("e"));if(n[1]){var a=n[1];a[0]==="+"&&(a=a.slice(1)),a=N(a);var f=n[0].indexOf(".");f>=0&&(a=a.minus(n[0].length-f),n[0]=n[0].slice(0,f)+n[0].slice(f+1));if(a.lesser(0))throw new Error("Cannot include negative exponent part for integers");while(a.notEquals(0))n[0]+="0",a=a.prev()}n=n[0],n==="-0"&&(n="0");var l=/^([0-9][0-9]*)$/.test(n);if(!l)throw new Error("Invalid integer: "+n);while(n.length){var c=n.length>t?n.length-t:0;o.push(+n.slice(c)),n=n.slice(0,c)}return new i(s(o),u)}function k(e){var t=e.value;return t.length===1&&t[0]<=36?"0123456789abcdefghijklmnopqrstuvwxyz".charAt(t[0]):"<"+t+">"}function L(e,t){t=bigInt(t);if(t.equals(0)){if(e.equals(0))return"0";throw new Error("Cannot convert nonzero numbers to base 0.")}if(t.equals(-1))return e.equals(0)?"0":e.lesser(0)?Array(1-e).join("10"):"1"+Array(+e).join("01");var n="";e.isNegative()&&t.isPositive()&&(n="-",e=e.abs());if(t.equals(1))return e.equals(0)?"0":n+Array(+e+1).join(1);var r=[],i=e,s;while(i.lesser(0)||i.compareAbs(t)>=0){s=i.divmod(t),i=s.quotient;var o=s.remainder;o.lesser(0)&&(o=t.minus(o).abs(),i=i.next()),r.push(k(o))}return r.push(k(i)),n+r.reverse().join("")}var e=1e7,t=7,n="0000000",r={positive:!1,negative:!0};i.prototype.negate=function(){return new i(this.value,!this.sign)},i.prototype.abs=function(){return new i(this.value,r.positive)},i.prototype.add=function(t){if(h(t))return o(this,+t);t=N(t);if(this.sign!==t.sign)return this.sign===r.positive?this.abs().subtract(t.abs()):t.abs().subtract(this.abs());var n=this.value,u=t.value,a=[],f=0,l=Math.max(n.length,u.length);for(var c=0;c<l||f>0;c++){var p=(n[c]||0)+(u[c]||0)+f;f=p>=e?1:0,a.push(p%e)}return new i(s(a),this.sign)},i.prototype.plus=i.prototype.add,i.prototype.subtract=function(t){if(h(t))return u(this,+t);t=N(t);if(this.sign!==t.sign)return this.add(t.negate());if(this.sign===r.negative)return t.negate().subtract(this.negate());if(this.compare(t)<0)return t.subtract(this).negate();var n=this.value,o=t.value,a=[],f=0,l=Math.max(n.length,o.length);for(var c=0;c<l;c++){var p=n[c]||0,d=o[c]||0,v=p-f;f=v<d?1:0,a.push(f*e+v-d)}return new i(s(a),r.positive)},i.prototype.minus=i.prototype.subtract,i.prototype.multiply=function(t){if(h(t))return f(this,+t);t=N(t);var n=this.sign!==t.sign,r=this.value,o=t.value,u=[];for(var a=r.length+o.length;a>0;a--)u.push(0);for(var a=0;a<r.length;a++){var l=r[a];for(var c=0;c<o.length;c++){var p=o[c],d=l*p+u[a+c],v=Math.floor(d/e);u[a+c]=d-v*e,u[a+c+1]+=v}}return new i(s(u),n)},i.prototype.times=i.prototype.multiply,i.prototype.divmod=function(t){if(h(t))return c(this,+t);t=N(t);var n=this.sign!==t.sign;if(t.equals(S))throw new Error("Cannot divide by zero");if(this.equals(S))return{quotient:new i([0],r.positive),remainder:new i([0],r.positive)};var o=this.value,u=t.value,f=[0];for(var p=0;p<u.length;p++)f[p]=0;var d=u[u.length-1],v=Math.ceil(e/2/d),m=a(o,v),g=a(u,v);d=g[u.length-1];for(var y=o.length-u.length;y>=0;y--){var b=e-1;m[y+u.length]!==d&&(b=Math.floor((m[y+u.length]*e+m[y+u.length-1])/d));var w=0,E=0;for(var p=0;p<g.length;p++){w+=b*g[p];var x=Math.floor(w/e);E+=m[y+p]-(w-x*e),w=x,E<0?(m[y+p]=E+e|0,E=-1):(m[y+p]=E|0,E=0)}while(E!==0){b-=1;var w=0;for(var p=0;p<g.length;p++)w+=m[y+p]-e+g[p],w<0?(m[y+p]=w+e|0,w=0):(m[y+p]=w|0,w=1);E+=w}f[y]=b|0}return m=l(m,v).quotient,{quotient:new i(s(f),n),remainder:new i(s(m),this.sign)}},i.prototype.divide=function(e){return this.divmod(e).quotient},i.prototype.over=i.prototype.divide,i.prototype.mod=function(e){return this.divmod(e).remainder},i.prototype.remainder=i.prototype.mod,i.prototype.pow=function(e){e=N(e);var t=this,n=e,r=x;if(n.equals(S))return r;if(t.equals(S)||n.lesser(S))return S;for(;;){n.isOdd()&&(r=r.times(t)),n=n.divide(2);if(n.equals(S))break;t=t.times(t)}return r},i.prototype.modPow=function(e,t){e=N(e),t=N(t);if(t.equals(S))throw new Error("Cannot take modPow with modulus 0");var n=x,r=this.mod(t);if(r.equals(S))return S;while(e.greater(0))e.isOdd()&&(n=n.multiply(r).mod(t)),e=e.divide(2),r=r.square().mod(t);return n},i.prototype.square=function(){return this.multiply(this)},i.prototype.next=function(){return o(this,1)},i.prototype.prev=function(){return u(this,1)},i.prototype.compare=function(e){var t=this,n=N(e);if(t.value.length===1&&n.value.length===1&&t.value[0]===0&&n.value[0]===0)return 0;if(n.sign!==t.sign)return t.sign===r.positive?1:-1;var i=t.sign===r.positive?1:-1,s=t.value,o=n.value,u=Math.max(s.length,o.length)-1;for(var a=u;a>=0;a--){var f=s[a]||0,l=o[a]||0;if(f>l)return 1*i;if(l>f)return-1*i}return 0},i.prototype.compareAbs=function(e){return this.abs().compare(e.abs())},i.prototype.equals=function(e){return this.compare(e)===0},i.prototype.notEquals=function(e){return!this.equals(e)},i.prototype.lesser=function(e){return this.compare(e)<0},i.prototype.greater=function(e){return this.compare(e)>0},i.prototype.greaterOrEquals=function(e){return this.compare(e)>=0},i.prototype.lesserOrEquals=function(e){return this.compare(e)<=0},i.prototype.compareTo=i.prototype.compare,i.prototype.lt=i.prototype.lesser,i.prototype.leq=i.prototype.lesserOrEquals,i.prototype.gt=i.prototype.greater,i.prototype.geq=i.prototype.greaterOrEquals,i.prototype.eq=i.prototype.equals,i.prototype.neq=i.prototype.notEquals,i.prototype.isPositive=function(){return this.value.length===1&&this.value[0]===0?!1:this.sign===r.positive},i.prototype.isNegative=function(){return this.value.length===1&&this.value[0]===0?!1:this.sign===r.negative},i.prototype.isEven=function(){return this.value[0]%2===0},i.prototype.isOdd=function(){return this.value[0]%2===1},i.prototype.isUnit=function(){return this.value.length===1&&this.value[0]===1},i.prototype.isZero=function(){return this.value.length===1&&this.value[0]===0},i.prototype.isDivisibleBy=function(e){return e=N(e),e.isZero()?!1:this.mod(e).equals(S)},i.prototype.isPrime=function(){var e=this.abs(),t=e.prev();if(e.isUnit())return!1;if(e.equals(2)||e.equals(3)||e.equals(5))return!0;if(e.isEven()||e.isDivisibleBy(3)||e.isDivisibleBy(5))return!1;if(e.lesser(25))return!0;var n=[2,3,5,7,11,13,17,19],r=t,i,s,o,u;while(r.isEven())r=r.divide(2);for(o=0;o<n.length;o++){u=bigInt(n[o]).modPow(r,e);if(u.equals(x)||u.equals(t))continue;for(s=!0,i=r;s&&i.lesser(t);i=i.multiply(2))u=u.square().mod(e),u.equals(t)&&(s=!1);if(s)return!1}return!0};var y=[1];while(y[y.length-1]<=e)y.push(2*y[y.length-1]);var b=y.length,w=y[b-1];i.prototype.shiftLeft=function(e){if(!h(e))return e.isNegative()?this.shiftRight(e.abs()):this.times(bigInt(2).pow(e));e=+e;if(e<0)return this.shiftRight(-e);var t=this;while(e>=b)t=f(t,w),e-=b-1;return f(t,y[e])},i.prototype.shiftRight=function(e){if(!h(e))return e.isNegative()?this.shiftLeft(e.abs()):this.over(bigInt(2).pow(e));e=+e;if(e<0)return this.shiftLeft(-e);var t=this;while(e>=b){if(t.equals(S))return t;t=c(t,w).quotient,e-=b-1}return c(t,y[e]).quotient},i.prototype.not=function(){return this.negate().minus(1)},i.prototype.and=function(e){return E(this,e,function(e,t){return e&t})},i.prototype.or=function(e){return E(this,e,function(e,t){return e|t})},i.prototype.xor=function(e){return E(this,e,function(e,t){return e^t})},i.prototype.toString=function(e){e===undefined&&(e=10);if(e!==10)return L(this,e);var t=this,i="",s=t.value.length;if(s===0||s===1&&t.value[0]===0)return"0";s-=1,i=t.value[s].toString();while(--s>=0){var o=t.value[s].toString();i+=n.slice(o.length)+o}var u=t.sign===r.positive?"":"-";return u+i},i.prototype.toJSNumber=function(){return this.valueOf()},i.prototype.valueOf=function(){return this.value.length===1?this.sign?-this.value[0]:this.value[0]:+this.toString()};var S=new i([0],r.positive),x=new i([1],r.positive),T=new i([1],r.negative),C=function(e,t){function o(e){var t=e[i].toLowerCase();if(i===0&&e[i]==="-"){s=!0;return}if(/[0-9]/.test(t))r.push(N(t));else if(/[a-z]/.test(t))r.push(N(t.charCodeAt(0)-87));else{if(t!=="<")throw new Error(t+" is not a valid character");var n=i;do i++;while(e[i]!==">");r.push(N(e.slice(n+1,i)))}}t=N(t);var n=S,r=[],i,s=!1;for(i=0;i<e.length;i++)o(e);r.reverse();for(i=0;i<r.length;i++)n=n.add(r[i].times(t.pow(i)));return s?n.negate():n},A=function(e,t){return typeof e=="undefined"?S:typeof t!="undefined"?C(e,t):N(e)};return A.zero=S,A.one=x,A.minusOne=T,A.randBetween=g,A.isInstance=function(e){return e instanceof i},A.min=m,A.max=v,A.gcd=p,A.lcm=d,A}();typeof module!="undefined"&&(module.exports=bigInt);
{
"name": "big-integer",
"version": "1.4.4",
"version": "1.4.5",
"author": "Peter Olson <peter.e.c.olson+npm@gmail.com>",

@@ -5,0 +5,0 @@ "description": "An arbitrary length integer library for Javascript",

@@ -79,5 +79,6 @@ [![Build Status](https://travis-ci.org/peterolson/BigInteger.js.svg?branch=master)](https://travis-ci.org/peterolson/BigInteger.js)

---
Performs the bitwise AND operation.
Performs the bitwise AND operation. The operands are treated as if they were represented using [two's complement representation](http://en.wikipedia.org/wiki/Two%27s_complement).
- `bigInt(6).and(3)` => `2`
- `bigInt(6).and(-3)` => `4`

@@ -173,2 +174,9 @@ `compare(number)`

`isInstance(x)`
---
Returns `true` if `x` is a BigInteger, `false` otherwise.
- `bigInt.isInstance(bigInt(14))` => `true`
- `bigInt.isInstance(14)` => `false`
`isNegative()`

@@ -299,5 +307,6 @@ ---

---
Performs the bitwise NOT operation.
Performs the bitwise NOT operation. The operands are treated as if they were represented using [two's complement representation](http://en.wikipedia.org/wiki/Two%27s_complement).
- `bigInt(10).not()` => `-5`
- `bigInt(0).not()` => `-1`

@@ -315,5 +324,6 @@ `notEquals(number)`

---
Performs the bitwise OR operation.
Performs the bitwise OR operation. The operands are treated as if they were represented using [two's complement representation](http://en.wikipedia.org/wiki/Two%27s_complement).
- `bigInt(13).or(10)` => `15`
- `bigInt(13).or(-8)` => `-3`

@@ -394,5 +404,6 @@ `over(number)`

---
Performs the bitwise XOR operation.
Performs the bitwise XOR operation. The operands are treated as if they were represented using [two's complement representation](http://en.wikipedia.org/wiki/Two%27s_complement).
- `bigInt(12).xor(5)` => `9`
- `bigInt(12).xor(-5)` => `-9`

@@ -409,2 +420,8 @@ Override Methods

**Note that arithmetical operators will trigger the `valueOf` function rather than the `toString` function.** When converting a bigInteger to a string, you should use the `toString` method or the `String` function instead of adding the empty string.
- `bigInt("999999999999999999").toString()` => `"999999999999999999"`
- `String(bigInt("999999999999999999"))` => `"999999999999999999"`
- `bigInt("999999999999999999") + ""` => `1000000000000000000`
Bases larger than 36 are supported. If a digit is larger than 36, it will be enclosed in angle brackets.

@@ -411,0 +428,0 @@

@@ -750,5 +750,19 @@ if (typeof require === "function") bigInt = require("../BigInteger.js");

expect(bigInt("435783453").xor("902345074")).toEqual("741422703");
expect(bigInt("94981987261387596").not()).toEqual("-49133200814468275");
expect(bigInt("94981987261387596").not()).toEqual("-94981987261387597");
expect(bigInt("-6931047708307681506").xor("25214903917")).toEqual("-6931047723896018573");
expect(bigInt("-6931047723896018573").and("281474976710655")).toEqual("273577603885427");
expect(bigInt("-65").xor("-42")).toEqual("105");
expect(bigInt("6").and("-3")).toEqual("4");
expect(bigInt("0").not()).toEqual("-1");
expect(bigInt("13").or(-8)).toEqual("-3");
expect(bigInt("12").xor(-5)).toEqual("-9");
});
});
describe("isInstance", function () {
it("works", function () {
expect(bigInt.isInstance(bigInt(14))).toBe(true);
expect(bigInt.isInstance(14)).toBe(false);
});
});

@@ -755,0 +769,0 @@ describe("Aliases", function () {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc