Need help with node-bignum?

Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

410 Stars 116 Forks 258 Commits 30 Opened issues

Big integers for Node.js using OpenSSL

No Data

Readme

Arbitrary precision integral arithmetic for Node.js using OpenSSL.

This library is based on node-bigint by substack, but instead of using libgmp, it uses the builtin bignum functionality provided by OpenSSL. The advantage is that OpenSSL is already part of Node.js, so this library does not add any external dependency whatsoever.

JavaScript now has a BigInt object. If you are using Node 10.4 or newer, you should use or migrate to BigInt.

When switching from node-bigint to node-bignum, please be aware of these differences:

- Bignum rounds towards zero for integer divisions, e.g.
10 / -3 = -3

, whereas bigint rounds towards negative infinity, e.g.10 / -3 = -4

. - nextPrime() is not supported.
- sqrt() and root() are not supported.

(Patches for the missing functionality are welcome.)

var bignum = require('bignum');var b = bignum('782910138827292261791972728324982') .sub('182373273283402171237474774728373') .div(8) ; console.log(b);

$ node simple.js

Generate the perfect numbers:

// If 2**n-1 is prime, then (2**n-1) * 2**(n-1) is perfect. var bignum = require('bignum');for (var n = 0; n < 100; n++) { var p = bignum.pow(2, n).sub(1); if (p.probPrime(50)) { var perfect = p.mul(bignum.pow(2, n - 1)); console.log(perfect.toString()); } }

6 28 496 8128 33550336 8589869056 137438691328 2305843008139952128 2658455991569831744654692615953842176 191561942608236107294793378084303638130997321548169216

Create a new

bignumfrom

nand a base.

ncan be a string, integer, or another

bignum.

If you pass in a string you can set the base that string is encoded in.

Print out the

bignuminstance in the requested base as a string.

Create a new

bignumfrom a

Buffer.

The default options are:

{ endian : 'big', size : 1, // number of bytes in each word }

Note that endian doesn't matter when size = 1. If you wish to reverse the entire buffer byte by byte, pass size: 'auto'.

Generate a probable prime of length

bits. If

safeis true, it will be a "safe" prime of the form p=2p'+1 where p' is also prime.

Return true if

numis identified as a bignum instance. Otherwise, return false.

For all of the instance methods below you can write either

bignum.method(x, y, z)

or if x is a

bignuminstance``

x.method(y, z)

Turn a

bignuminto a

Number. If the

bignumis too big you'll lose precision or you'll get ±

Infinity.

Return a new

Bufferwith the data from the

bignum.

The default options are:

{ endian : 'big', size : 1, // number of bytes in each word }

Note that endian doesn't matter when size = 1. If you wish to reverse the entire buffer byte by byte, pass size: 'auto'.

Return a new

bignumcontaining the instance value plus

n.

Return a new

bignumcontaining the instance value minus

n.

Return a new

bignumcontaining the instance value multiplied by

n.

Return a new

bignumcontaining the instance value integrally divided by

n.

Return a new

bignumwith the absolute value of the instance.

Return a new

bignumwith the negative of the instance value.

Compare the instance value to

n. Return a positive integer if

> n, a negative integer if

< n, and 0 if

== n.

Return a boolean: whether the instance value is greater than n (

> n).

Return a boolean: whether the instance value is greater than or equal to n (

>= n).

Return a boolean: whether the instance value is equal to n (

== n).

Return a boolean: whether the instance value is less than n (

< n).

Return a boolean: whether the instance value is less than or equal to n (

<= n).

Return a new

bignumwith the instance value bitwise AND (&)-ed with

n.

Return a new

bignumwith the instance value bitwise inclusive-OR (|)-ed with

n.

Return a new

bignumwith the instance value bitwise exclusive-OR (^)-ed with

n.

Return a new

bignumwith the instance value modulo

n.

m.

Return a new

bignumwith the instance value raised to the

nth power.

Return a new

bignumwith the instance value raised to the

nth power modulo

m.

Compute the multiplicative inverse modulo

m.

If

upperBoundis supplied, return a random

bignumbetween the instance value and

upperBound - 1, inclusive.

Otherwise, return a random

bignumbetween 0 and the instance value - 1, inclusive.

Return whether the bignum is:

- certainly prime (true)
- probably prime ('maybe')
- certainly composite (false)

using BN*is*prime_ex.

Return a new

bignumthat is the square root. This truncates.

Return a new

bignumthat is the

nthroot. This truncates.

Return a new

bignumthat is the

2^nmultiple. Equivalent of the

<<operator.

Return a new

bignumof the value integer divided by

2^n. Equivalent of the

>>operator.

Return the greatest common divisor of the current

bignumwith

nas a new

bignum.

Return the Jacobi symbol (or Legendre symbol if

nis prime) of the current

bignum(= a) over

n. Note that

nmust be odd and >= 3. 0 <= a < n.

Returns -1 or 1 as an int (NOT a bignum). Throws an error on failure.

Return the number of bits used to represent the current

bignum.

To compile the package, your system needs to be set up for building Node.js modules.

You can install node-bignum with npm:

npm install bignum

You can clone the git repo and compile with

git clone git://github.com/justmoon/node-bignum.git cd node-bignum npm install

Run the tests with

npm test