bn.js is a fundamental pure JavaScript library providing a comprehensive implementation for arbitrary-precision integers, commonly referred to as "Big Numbers." It is currently at version 5.2.3 and maintains an active release cadence, frequently addressing bug fixes and minor improvements, as seen in recent updates like v5.2.1 and v5.2.0. A key design principle is its focus on integer arithmetic; it explicitly does not support decimal numbers, which is an important consideration for users. Differentiating features include support for in-place operations (e.g., `iadd`), unsigned operations (e.g., `umod`), and operations that take native JavaScript numbers as arguments (e.g., `addn`), allowing for performance optimizations and flexible usage patterns. It provides a wide array of utilities, arithmetic, and bitwise operations crucial for cryptographic applications, large financial calculations, and other domains requiring precision beyond standard JavaScript `Number` limits.
npm install bn.jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `BN` objects from various bases and large strings, perform basic arithmetic and bitwise operations including in-place and unsigned variants, and convert `BN` instances to different output formats. It also shows how to check if an object is a `BN` instance.
Ensure all string inputs to the `BN` constructor represent integers without decimal points. For example, use `new BN('123', 10)` instead of `new BN('123.45', 10)` (which is not supported).Replace `.modn()` with `.mod()` or `.umod()`, ensuring the modulus argument is also a `BN` instance if it's not a small JavaScript Number (which can be used with `modn` like alternatives).
If decimal precision is required, consider using alternative libraries like `bignumber.js` or `decimal.js`. Ensure all inputs and operations are strictly integer-based when using BN.js.
For `BN` values exceeding 53 bits, use `.toString(10)` for full decimal representation, or `.toArray()`, `.toBuffer()` for byte representations. Avoid `.toNumber()` for cryptographic keys, hashes, or large financial amounts.
Ensure all `bn.js` installations within a project are consistent at `v5.1.2` or later to avoid mixing major versions and their internal differences.
Upgrade to `bn.js@5.2.1` or newer to ensure correct hexadecimal string conversions. Verify critical output if upgrading is not immediately possible.
For JavaScript `Number` arguments exceeding `0x4000000`, convert the number into a `BN` instance first (e.g., `new BN(largeNum)`) and then use the corresponding method without the `n` suffix (e.g., `bnInstance.iadd(new BN(largeNum))`).
Ensure all string inputs to the `BN` constructor are pure integer representations matching the provided base (e.g., `new BN('10', 10)` is valid, `new BN('10.5', 10)` is not).Replace `bnInstance.modn(num)` with `bnInstance.mod(new BN(num))` or, for unsigned results, `bnInstance.umod(new BN(num))`. Alternatively, if `num` is a small JavaScript number, use `bnInstance.mod(new BN(num))` or `bnInstance.modn` alternatives for specific contexts if available in newer versions (refer to documentation).
For values larger than `Number.MAX_SAFE_INTEGER`, use `.toString(10)` to get the full decimal string representation. Other conversion methods like `.toArray()` or `.toBuffer()` can provide byte-level access without numeric precision limits.
For number arguments greater than `0x4000000`, convert the JavaScript `Number` into a `BN` instance first (e.g., `new BN(largeNum)`) and then use the corresponding method without the `n` suffix (e.g., `bnInstance.iadd(new BN(largeNum))`).
No dependency data recorded yet.