ab0a2b3c52153bfb62c25e51d9d1fca13e63d306b9ece0f9fa155e92f75abe7eeb9a358a3cccca08976cb6af28f2bafb93177999312177b3fa84ca37f863b4 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # <img src="./logo.png" alt="bn.js" width="160" height="160" />
  2. > BigNum in pure javascript
  3. [![Build Status](https://secure.travis-ci.org/indutny/bn.js.png)](http://travis-ci.org/indutny/bn.js)
  4. ## Install
  5. `npm install --save bn.js`
  6. ## Usage
  7. ```js
  8. const BN = require('bn.js');
  9. var a = new BN('dead', 16);
  10. var b = new BN('101010', 2);
  11. var res = a.add(b);
  12. console.log(res.toString(10)); // 57047
  13. ```
  14. **Note**: decimals are not supported in this library.
  15. ## Sponsors
  16. [![Scout APM](./sponsors/scout-apm.png)](https://scoutapm.com/)
  17. My Open Source work is supported by [Scout APM](https://scoutapm.com/) and
  18. [other sponsors](https://github.com/sponsors/indutny).
  19. ## Notation
  20. ### Prefixes
  21. There are several prefixes to instructions that affect the way they work. Here
  22. is the list of them in the order of appearance in the function name:
  23. * `i` - perform operation in-place, storing the result in the host object (on
  24. which the method was invoked). Might be used to avoid number allocation costs
  25. * `u` - unsigned, ignore the sign of operands when performing operation, or
  26. always return positive value. Second case applies to reduction operations
  27. like `mod()`. In such cases if the result will be negative - modulo will be
  28. added to the result to make it positive
  29. ### Postfixes
  30. * `n` - the argument of the function must be a plain JavaScript
  31. Number. Decimals are not supported. The number passed must be smaller than 0x4000000 (67_108_864). Otherwise, an error is thrown.
  32. * `rn` - both argument and return value of the function are plain JavaScript
  33. Numbers. Decimals are not supported.
  34. ### Examples
  35. * `a.iadd(b)` - perform addition on `a` and `b`, storing the result in `a`
  36. * `a.umod(b)` - reduce `a` modulo `b`, returning positive value
  37. * `a.iushln(13)` - shift bits of `a` left by 13
  38. ## Instructions
  39. Prefixes/postfixes are put in parens at the end of the line. `endian` - could be
  40. either `le` (little-endian) or `be` (big-endian).
  41. ### Utilities
  42. * `a.clone()` - clone number
  43. * `a.toString(base, length)` - convert to base-string and pad with zeroes
  44. * `a.toNumber()` - convert to Javascript Number (limited to 53 bits)
  45. * `a.toJSON()` - convert to JSON compatible hex string (alias of `toString(16)`)
  46. * `a.toArray(endian, length)` - convert to byte `Array`, and optionally zero
  47. pad to length, throwing if already exceeding
  48. * `a.toArrayLike(type, endian, length)` - convert to an instance of `type`,
  49. which must behave like an `Array`
  50. * `a.toBuffer(endian, length)` - convert to Node.js Buffer (if available). `length` in bytes. For
  51. compatibility with browserify and similar tools, use this instead:
  52. `a.toArrayLike(Buffer, endian, length)`
  53. * `a.bitLength()` - get number of bits occupied
  54. * `a.zeroBits()` - return number of less-significant consequent zero bits
  55. (example: `1010000` has 4 zero bits)
  56. * `a.byteLength()` - return number of bytes occupied
  57. * `a.isNeg()` - true if the number is negative
  58. * `a.isEven()` - no comments
  59. * `a.isOdd()` - no comments
  60. * `a.isZero()` - no comments
  61. * `a.cmp(b)` - compare numbers and return `-1` (a `<` b), `0` (a `==` b), or `1` (a `>` b)
  62. depending on the comparison result (`ucmp`, `cmpn`)
  63. * `a.lt(b)` - `a` less than `b` (`n`)
  64. * `a.lte(b)` - `a` less than or equals `b` (`n`)
  65. * `a.gt(b)` - `a` greater than `b` (`n`)
  66. * `a.gte(b)` - `a` greater than or equals `b` (`n`)
  67. * `a.eq(b)` - `a` equals `b` (`n`)
  68. * `a.toTwos(width)` - convert to two's complement representation, where `width` is bit width
  69. * `a.fromTwos(width)` - convert from two's complement representation, where `width` is the bit width
  70. * `BN.isBN(object)` - returns true if the supplied `object` is a BN.js instance
  71. * `BN.max(a, b)` - return `a` if `a` bigger than `b`
  72. * `BN.min(a, b)` - return `a` if `a` less than `b`
  73. ### Arithmetics
  74. * `a.neg()` - negate sign (`i`)
  75. * `a.abs()` - absolute value (`i`)
  76. * `a.add(b)` - addition (`i`, `n`, `in`)
  77. * `a.sub(b)` - subtraction (`i`, `n`, `in`)
  78. * `a.mul(b)` - multiply (`i`, `n`, `in`)
  79. * `a.sqr()` - square (`i`)
  80. * `a.pow(b)` - raise `a` to the power of `b`
  81. * `a.div(b)` - divide (`divn`, `idivn`)
  82. * `a.mod(b)` - reduct (`u`, `n`) (but no `umodn`)
  83. * `a.divmod(b)` - quotient and modulus obtained by dividing
  84. * `a.divRound(b)` - rounded division
  85. ### Bit operations
  86. * `a.or(b)` - or (`i`, `u`, `iu`)
  87. * `a.and(b)` - and (`i`, `u`, `iu`, `andln`) (NOTE: `andln` is going to be replaced
  88. with `andn` in future)
  89. * `a.xor(b)` - xor (`i`, `u`, `iu`)
  90. * `a.setn(b, value)` - set specified bit to `value`
  91. * `a.shln(b)` - shift left (`i`, `u`, `iu`)
  92. * `a.shrn(b)` - shift right (`i`, `u`, `iu`)
  93. * `a.testn(b)` - test if specified bit is set
  94. * `a.maskn(b)` - clear bits with indexes higher or equal to `b` (`i`)
  95. * `a.bincn(b)` - add `1 << b` to the number
  96. * `a.notn(w)` - not (for the width specified by `w`) (`i`)
  97. ### Reduction
  98. * `a.gcd(b)` - GCD
  99. * `a.egcd(b)` - Extended GCD results (`{ a: ..., b: ..., gcd: ... }`)
  100. * `a.invm(b)` - inverse `a` modulo `b`
  101. ## Fast reduction
  102. When doing lots of reductions using the same modulo, it might be beneficial to
  103. use some tricks: like [Montgomery multiplication][0], or using special algorithm
  104. for [Mersenne Prime][1].
  105. ### Reduction context
  106. To enable this trick one should create a reduction context:
  107. ```js
  108. var red = BN.red(num);
  109. ```
  110. where `num` is just a BN instance.
  111. Or:
  112. ```js
  113. var red = BN.red(primeName);
  114. ```
  115. Where `primeName` is either of these [Mersenne Primes][1]:
  116. * `'k256'`
  117. * `'p224'`
  118. * `'p192'`
  119. * `'p25519'`
  120. Or:
  121. ```js
  122. var red = BN.mont(num);
  123. ```
  124. To reduce numbers with [Montgomery trick][0]. `.mont()` is generally faster than
  125. `.red(num)`, but slower than `BN.red(primeName)`.
  126. ### Converting numbers
  127. Before performing anything in reduction context - numbers should be converted
  128. to it. Usually, this means that one should:
  129. * Convert inputs to reducted ones
  130. * Operate on them in reduction context
  131. * Convert outputs back from the reduction context
  132. Here is how one may convert numbers to `red`:
  133. ```js
  134. var redA = a.toRed(red);
  135. ```
  136. Where `red` is a reduction context created using instructions above
  137. Here is how to convert them back:
  138. ```js
  139. var a = redA.fromRed();
  140. ```
  141. ### Red instructions
  142. Most of the instructions from the very start of this readme have their
  143. counterparts in red context:
  144. * `a.redAdd(b)`, `a.redIAdd(b)`
  145. * `a.redSub(b)`, `a.redISub(b)`
  146. * `a.redShl(num)`
  147. * `a.redMul(b)`, `a.redIMul(b)`
  148. * `a.redSqr()`, `a.redISqr()`
  149. * `a.redSqrt()` - square root modulo reduction context's prime
  150. * `a.redInvm()` - modular inverse of the number
  151. * `a.redNeg()`
  152. * `a.redPow(b)` - modular exponentiation
  153. ### Number Size
  154. Optimized for elliptic curves that work with 256-bit numbers.
  155. There is no limitation on the size of the numbers.
  156. ## LICENSE
  157. This software is licensed under the MIT License.
  158. [0]: https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
  159. [1]: https://en.wikipedia.org/wiki/Mersenne_prime