What is bn.js?
The bn.js package is a library that provides support for arbitrary-precision arithmetic operations on big numbers in JavaScript. It is commonly used in cryptography, financial calculations, and anywhere precise arithmetic with large integers is required.
What are bn.js's main functionalities?
Big Number Arithmetic
Performing basic arithmetic operations such as addition, subtraction, multiplication, division, and modulo on big numbers.
const BN = require('bn.js');
let a = new BN('123456789');
let b = new BN('987654321');
let sum = a.add(b); // Addition
let diff = a.sub(b); // Subtraction
let product = a.mul(b); // Multiplication
let quotient = a.div(b); // Division
let remainder = a.mod(b); // Modulo
Comparison Operations
Comparing big numbers to determine if they are equal, less than, or greater than each other.
const BN = require('bn.js');
let a = new BN('12345');
let b = new BN('12345');
let c = new BN('54321');
a.eq(b); // true, a equals b
a.lt(c); // true, a is less than c
a.gt(c); // false, a is not greater than c
Bitwise Operations
Performing bitwise operations such as AND, OR, XOR, and NOT on big numbers.
const BN = require('bn.js');
let a = new BN('1011', 2);
let b = new BN('1101', 2);
let and = a.and(b); // AND operation
let or = a.or(b); // OR operation
let xor = a.xor(b); // XOR operation
let not = a.not(); // NOT operation
Reduction Contexts
Using reduction contexts to perform modular arithmetic efficiently, particularly useful in modular exponentiation and cryptographic operations.
const BN = require('bn.js');
let p = new BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16);
let a = new BN('7fffffffffffffffffffffff7ffffe17ff', 16);
let red = BN.red(p);
let b = a.toRed(red); // Convert to Montgomery representation
let c = b.redSqr().redMul(b); // Perform operations in reduction context
let d = c.fromRed(); // Convert out of Montgomery representation
Other packages similar to bn.js
big.js
big.js is a small, fast JavaScript library for arbitrary-precision decimal arithmetic. It focuses on decimal numbers and provides a simpler API but does not support bitwise operations like bn.js.
bignumber.js
bignumber.js is another arbitrary-precision arithmetic library for JavaScript and Node.js. It supports decimal and non-decimal arithmetic and has a more extensive API than bn.js, but it might be slower for some operations due to its focus on decimal precision.
decimal.js
decimal.js is a library for arbitrary-precision decimal arithmetic. It is similar to bignumber.js in its focus on decimal numbers and provides a comprehensive set of features for decimal arithmetic, but it does not provide the same level of support for non-decimal and bitwise operations as bn.js.
bn.js
Just a bike-shed.
LICENSE
This software is licensed under the MIT License.
Copyright Fedor Indutny, 2014.
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.