Gathering detailed insights and metrics for bn-plus.js
Gathering detailed insights and metrics for bn-plus.js
Gathering detailed insights and metrics for bn-plus.js
Gathering detailed insights and metrics for bn-plus.js
npm install bn-plus.js
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
1 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jun 07, 2018
Latest Version
1.0.0
Package Id
bn-plus.js@1.0.0
Unpacked Size
97.98 kB
Size
21.65 kB
File Count
5
NPM Version
5.7.1
Node Version
8.9.4
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
BigNum in pure javascript Maximum support to 9999999999999900
npm install --save bn-plus.js
1const BN = require('bn-plus.js'); 2 3var a = new BN('dead', 16); 4var b = new BN('101010', 2); 5 6var res = a.add(b); 7console.log(res.toString(10)); // 57047
Note: decimals are not supported in this library.
There are several prefixes to instructions that affect the way the work. Here is the list of them in the order of appearance in the function name:
i
- perform operation in-place, storing the result in the host object (on
which the method was invoked). Might be used to avoid number allocation costsu
- unsigned, ignore the sign of operands when performing operation, or
always return positive value. Second case applies to reduction operations
like mod()
. In such cases if the result will be negative - modulo will be
added to the result to make it positiveThe only available postfix at the moment is:
n
- which means that the argument of the function must be a plain JavaScript
Number. Decimals are not supported.a.iadd(b)
- perform addition on a
and b
, storing the result in a
a.umod(b)
- reduce a
modulo b
, returning positive valuea.iushln(13)
- shift bits of a
left by 13Prefixes/postfixes are put in parens at the of the line. endian
- could be
either le
(little-endian) or be
(big-endian).
a.clone()
- clone numbera.toString(base, length)
- convert to base-string and pad with zeroesa.toNumber()
- convert to Javascript Number (limited to 53 bits)a.toJSON()
- convert to JSON compatible hex string (alias of toString(16)
)a.toArray(endian, length)
- convert to byte Array
, and optionally zero
pad to length, throwing if already exceedinga.toArrayLike(type, endian, length)
- convert to an instance of type
,
which must behave like an Array
a.toBuffer(endian, length)
- convert to Node.js Buffer (if available). For
compatibility with browserify and similar tools, use this instead:
a.toArrayLike(Buffer, endian, length)
a.bitLength()
- get number of bits occupieda.zeroBits()
- return number of less-significant consequent zero bits
(example: 1010000
has 4 zero bits)a.byteLength()
- return number of bytes occupieda.isNeg()
- true if the number is negativea.isEven()
- no commentsa.isOdd()
- no commentsa.isZero()
- no commentsa.cmp(b)
- compare numbers and return -1
(a <
b), 0
(a ==
b), or 1
(a >
b)
depending on the comparison result (ucmp
, cmpn
)a.lt(b)
- a
less than b
(n
)a.lte(b)
- a
less than or equals b
(n
)a.gt(b)
- a
greater than b
(n
)a.gte(b)
- a
greater than or equals b
(n
)a.eq(b)
- a
equals b
(n
)a.toTwos(width)
- convert to two's complement representation, where width
is bit widtha.fromTwos(width)
- convert from two's complement representation, where width
is the bit widthBN.isBN(object)
- returns true if the supplied object
is a BN.js instancea.neg()
- negate sign (i
)a.abs()
- absolute value (i
)a.add(b)
- addition (i
, n
, in
)a.sub(b)
- subtraction (i
, n
, in
)a.mul(b)
- multiply (i
, n
, in
)a.sqr()
- square (i
)a.pow(b)
- raise a
to the power of b
a.div(b)
- divide (divn
, idivn
)a.mod(b)
- reduct (u
, n
) (but no umodn
)a.divRound(b)
- rounded divisiona.or(b)
- or (i
, u
, iu
)a.and(b)
- and (i
, u
, iu
, andln
) (NOTE: andln
is going to be replaced
with andn
in future)a.xor(b)
- xor (i
, u
, iu
)a.setn(b)
- set specified bit to 1
a.shln(b)
- shift left (i
, u
, iu
)a.shrn(b)
- shift right (i
, u
, iu
)a.testn(b)
- test if specified bit is seta.maskn(b)
- clear bits with indexes higher or equal to b
(i
)a.bincn(b)
- add 1 << b
to the numbera.notn(w)
- not (for the width specified by w
) (i
)a.gcd(b)
- GCDa.egcd(b)
- Extended GCD results ({ a: ..., b: ..., gcd: ... }
)a.invm(b)
- inverse a
modulo b
When doing lots of reductions using the same modulo, it might be beneficial to use some tricks: like Montgomery multiplication, or using special algorithm for Mersenne Prime.
To enable this tricks one should create a reduction context:
1var red = BN.red(num);
where num
is just a BN instance.
Or:
1var red = BN.red(primeName);
Where primeName
is either of these Mersenne Primes:
'k256'
'p224'
'p192'
'p25519'
Or:
1var red = BN.mont(num);
To reduce numbers with Montgomery trick. .mont()
is generally faster than
.red(num)
, but slower than BN.red(primeName)
.
Before performing anything in reduction context - numbers should be converted to it. Usually, this means that one should:
Here is how one may convert numbers to red
:
1var redA = a.toRed(red);
Where red
is a reduction context created using instructions above
Here is how to convert them back:
1var a = redA.fromRed();
Most of the instructions from the very start of this readme have their counterparts in red context:
a.redAdd(b)
, a.redIAdd(b)
a.redSub(b)
, a.redISub(b)
a.redShl(num)
a.redMul(b)
, a.redIMul(b)
a.redSqr()
, a.redISqr()
a.redSqrt()
- square root modulo reduction context's primea.redInvm()
- modular inverse of the numbera.redNeg()
a.redPow(b)
- modular exponentiationThis software is licensed under the MIT License.
Copyright Fedor Indutny, 2015.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 0/1 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More