Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bigintjs

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bigintjs - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

51

index.js

@@ -419,4 +419,51 @@ var array = require('./lib/array');

BigInt.prototype.toBytes = function (opt_destArray) {
return array.copy(this.values, BITS, 8, opt_destArray);
BigInt.prototype.toBytes = function (opt_destArray, opt_signed) {
if (typeof opt_signed == 'undefined') opt_signed = true;
if (opt_destArray && !(opt_destArray instanceof Uint8Array)) {
throw new Error('toBytes only supports Uint8Array');
}
if (!opt_signed && this.negative) {
throw new Error('Can\'t convert negative BigInt to unsigned');
}
var values = array.copy(this.values, BITS, 8, opt_destArray);
// If the value should be unsigned, just return the array.
if (!opt_signed) return values;
// If the highest bit is set, we may need to add a byte to the array.
var extend = values[0] & 128 && (!this.negative || values[0] & 127 || values.length > 1);
if (extend && this.negative && values.length > 1) {
// If all the bits after the first are 0, we don't need to extend.
for (var i = 1; i < values.length; i++) {
if (values[i]) break;
}
if (i == values.length) extend = false;
}
// Extend the array if necessary.
if (extend) {
if (opt_destArray) {
// We can't modify a provided array.
throw new Error('Destination array is too small');
}
var temp = values;
values = new Uint8Array(temp.length + 1);
values.set(temp, 1);
}
// Change to two's complement if the number is negative.
if (this.negative) {
var carry = 1;
for (var i = values.length - 1; i >= 0; i--) {
var value = (values[i] ^ 0xFF) + carry;
values[i] = value & 0xFF;
carry = value >> 8;
}
}
return values;
};

@@ -423,0 +470,0 @@

67

lib/array.js

@@ -17,10 +17,22 @@ exports.copy = copy;

var dest, numItems = fromArray.length;
var firstIndex = 0,
numItems = fromArray.length;
var totalBits = numItems * fromBits,
bytesToFill = Math.ceil(totalBits / toBits);
// Skip to the first non-zero value.
while (!fromArray[firstIndex] && firstIndex < numItems) firstIndex++;
// Figure out how many bits are actually in use in the first value.
var firstBits = 1;
while (fromArray[firstIndex] >>> firstBits && firstBits < 32) {
firstBits++;
}
// Calculate the number of "from" values, bits and "to" values needed.
var totalBits = Math.max(numItems - firstIndex - 1, 0) * fromBits + firstBits,
valuesToFill = Math.ceil(totalBits / toBits);
var toArray;
if (opt_destArray) {
// Assume that the user is passing in a compatible array.
dest = opt_destArray;
toArray = opt_destArray;
} else {

@@ -37,42 +49,31 @@ // Pick the smallest possible typed array to hold the copied items.

dest = new ArrayClass(bytesToFill);
toArray = new ArrayClass(valuesToFill);
}
var index = dest.length - bytesToFill,
bits = totalBits % toBits || toBits,
mask = toBits == 32 ? -1 >>> 0 : (1 << toBits >>> 0) - 1;
if (index < 0) {
if (toArray.length < valuesToFill) {
throw new Error('Destination array is too small');
}
for (var i = 0; i < numItems; i++) {
var value = fromArray[i];
var mask = toBits == 32 ? 0xFFFFFFFF : (1 << toBits >>> 0) - 1,
bits = 0,
a = fromArray.length - 1,
b = toArray.length - 1;
var overflow = fromBits - bits;
if (overflow < 0) {
dest[index] |= value << -overflow >>> 0 & mask;
} else {
dest[index] |= value >>> overflow & mask;
while (a >= firstIndex) {
var value = fromArray[a--];
if (bits < 0) {
toArray[b + 1] = (toArray[b + 1] | (value << (toBits + bits) >>> 0 & mask)) >>> 0;
value = value >>> -bits;
}
while (overflow > 0) {
index++;
if (overflow < toBits) {
dest[index] = value << (toBits - overflow) >>> 0 & mask;
} else {
dest[index] = value >>> (overflow - toBits) & mask;
bits += fromBits;
while (bits > 0) {
if (b >= 0) {
toArray[b--] = (value & mask) >>> 0;
value = value >>> toBits;
}
overflow -= toBits;
bits -= toBits;
}
bits -= fromBits;
while (bits < 0) bits += toBits;
}
// TODO: Calling normalized here is a hack – it can be done better above.
return normalized(dest);
return toArray;
}

@@ -79,0 +80,0 @@

{
"name": "bigintjs",
"version": "0.3.0",
"version": "0.3.1",
"description": "Allows working with integers of any size.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -10,25 +10,17 @@ BigInt [![Build Status Image][]][Build Status]

***Note:*** This is still a very early version and a lot of operations
are missing.
Example
-------
Here's how to create a `BigInt` from a series of binary values:
```js
var BigInt = require('bigintjs');
var bigint = require('bigintjs');
// Treat each value in the input array as a 32-bit integer.
var big = new BigInt([0xFF000000, 0x00000000, 0x00000000], 32);
console.log(big.toString());
console.log(bigint('99999999999999999999999999999').add('1').toString());
// 100000000000000000000000000000
// You can now perform arithmetics on the BigInt.
console.log(big.shiftRight(8).toString());
// Bitwise operations on a big number (JavaScript only supports up to 32 bits)
var value = bigint('0xFFFFFFFFFFFFFFFF').and('0xF0F0F0F0F0F0F0F0').shiftLeft(8);
```
In the future, it'll be possible to create a `BigInt` from a string.
Using this package

@@ -35,0 +27,0 @@ ------------------

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc