web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.4 KiB

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