buffernumber.js 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +214 -0
  3. package/lib/bn.js +3553 -0
  4. package/package.json +42 -0
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright Fedor Indutny, 2015.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,214 @@
1
+ # <img src="./logo.png" alt="bn.js" width="160" height="160" />
2
+
3
+ > BigNum in pure javascript
4
+
5
+ [![Build Status](https://secure.travis-ci.org/indutny/bn.js.png)](http://travis-ci.org/indutny/bn.js)
6
+
7
+ ## Install
8
+ `npm install --save bn.js`
9
+
10
+ ## Usage
11
+
12
+ ```js
13
+ const BN = require('bn.js');
14
+
15
+ var a = new BN('dead', 16);
16
+ var b = new BN('101010', 2);
17
+
18
+ var res = a.add(b);
19
+ console.log(res.toString(10)); // 57047
20
+ ```
21
+
22
+ **Note**: decimals are not supported in this library.
23
+
24
+ ## Sponsors
25
+
26
+ [![Scout APM](./sponsors/scout-apm.png)](https://scoutapm.com/)
27
+ My Open Source work is supported by [Scout APM](https://scoutapm.com/) and
28
+ [other sponsors](https://github.com/sponsors/indutny).
29
+
30
+ ## Notation
31
+
32
+ ### Prefixes
33
+
34
+ There are several prefixes to instructions that affect the way they work. Here
35
+ is the list of them in the order of appearance in the function name:
36
+
37
+ * `i` - perform operation in-place, storing the result in the host object (on
38
+ which the method was invoked). Might be used to avoid number allocation costs
39
+ * `u` - unsigned, ignore the sign of operands when performing operation, or
40
+ always return positive value. Second case applies to reduction operations
41
+ like `mod()`. In such cases if the result will be negative - modulo will be
42
+ added to the result to make it positive
43
+
44
+ ### Postfixes
45
+
46
+ * `n` - the argument of the function must be a plain JavaScript
47
+ Number. Decimals are not supported. The number passed must be smaller than 0x4000000 (67_108_864). Otherwise, an error is thrown.
48
+ * `rn` - both argument and return value of the function are plain JavaScript
49
+ Numbers. Decimals are not supported.
50
+
51
+ ### Examples
52
+
53
+ * `a.iadd(b)` - perform addition on `a` and `b`, storing the result in `a`
54
+ * `a.umod(b)` - reduce `a` modulo `b`, returning positive value
55
+ * `a.iushln(13)` - shift bits of `a` left by 13
56
+
57
+ ## Instructions
58
+
59
+ Prefixes/postfixes are put in parens at the end of the line. `endian` - could be
60
+ either `le` (little-endian) or `be` (big-endian).
61
+
62
+ ### Utilities
63
+
64
+ * `a.clone()` - clone number
65
+ * `a.toString(base, length)` - convert to base-string and pad with zeroes
66
+ * `a.toNumber()` - convert to Javascript Number (limited to 53 bits)
67
+ * `a.toJSON()` - convert to JSON compatible hex string (alias of `toString(16)`)
68
+ * `a.toArray(endian, length)` - convert to byte `Array`, and optionally zero
69
+ pad to length, throwing if already exceeding
70
+ * `a.toArrayLike(type, endian, length)` - convert to an instance of `type`,
71
+ which must behave like an `Array`
72
+ * `a.toBuffer(endian, length)` - convert to Node.js Buffer (if available). `length` in bytes. For
73
+ compatibility with browserify and similar tools, use this instead:
74
+ `a.toArrayLike(Buffer, endian, length)`
75
+ * `a.bitLength()` - get number of bits occupied
76
+ * `a.zeroBits()` - return number of less-significant consequent zero bits
77
+ (example: `1010000` has 4 zero bits)
78
+ * `a.byteLength()` - return number of bytes occupied
79
+ * `a.isNeg()` - true if the number is negative
80
+ * `a.isEven()` - no comments
81
+ * `a.isOdd()` - no comments
82
+ * `a.isZero()` - no comments
83
+ * `a.cmp(b)` - compare numbers and return `-1` (a `<` b), `0` (a `==` b), or `1` (a `>` b)
84
+ depending on the comparison result (`ucmp`, `cmpn`)
85
+ * `a.lt(b)` - `a` less than `b` (`n`)
86
+ * `a.lte(b)` - `a` less than or equals `b` (`n`)
87
+ * `a.gt(b)` - `a` greater than `b` (`n`)
88
+ * `a.gte(b)` - `a` greater than or equals `b` (`n`)
89
+ * `a.eq(b)` - `a` equals `b` (`n`)
90
+ * `a.toTwos(width)` - convert to two's complement representation, where `width` is bit width
91
+ * `a.fromTwos(width)` - convert from two's complement representation, where `width` is the bit width
92
+ * `BN.isBN(object)` - returns true if the supplied `object` is a BN.js instance
93
+ * `BN.max(a, b)` - return `a` if `a` bigger than `b`
94
+ * `BN.min(a, b)` - return `a` if `a` less than `b`
95
+
96
+ ### Arithmetics
97
+
98
+ * `a.neg()` - negate sign (`i`)
99
+ * `a.abs()` - absolute value (`i`)
100
+ * `a.add(b)` - addition (`i`, `n`, `in`)
101
+ * `a.sub(b)` - subtraction (`i`, `n`, `in`)
102
+ * `a.mul(b)` - multiply (`i`, `n`, `in`)
103
+ * `a.sqr()` - square (`i`)
104
+ * `a.pow(b)` - raise `a` to the power of `b`
105
+ * `a.div(b)` - divide (`divn`, `idivn`)
106
+ * `a.mod(b)` - reduct (`u`, `n`) (but no `umodn`)
107
+ * `a.divmod(b)` - quotient and modulus obtained by dividing
108
+ * `a.divRound(b)` - rounded division
109
+
110
+ ### Bit operations
111
+
112
+ * `a.or(b)` - or (`i`, `u`, `iu`)
113
+ * `a.and(b)` - and (`i`, `u`, `iu`, `andln`) (NOTE: `andln` is going to be replaced
114
+ with `andn` in future)
115
+ * `a.xor(b)` - xor (`i`, `u`, `iu`)
116
+ * `a.setn(b, value)` - set specified bit to `value`
117
+ * `a.shln(b)` - shift left (`i`, `u`, `iu`)
118
+ * `a.shrn(b)` - shift right (`i`, `u`, `iu`)
119
+ * `a.testn(b)` - test if specified bit is set
120
+ * `a.maskn(b)` - clear bits with indexes higher or equal to `b` (`i`)
121
+ * `a.bincn(b)` - add `1 << b` to the number
122
+ * `a.notn(w)` - not (for the width specified by `w`) (`i`)
123
+
124
+ ### Reduction
125
+
126
+ * `a.gcd(b)` - GCD
127
+ * `a.egcd(b)` - Extended GCD results (`{ a: ..., b: ..., gcd: ... }`)
128
+ * `a.invm(b)` - inverse `a` modulo `b`
129
+
130
+ ## Fast reduction
131
+
132
+ When doing lots of reductions using the same modulo, it might be beneficial to
133
+ use some tricks: like [Montgomery multiplication][0], or using special algorithm
134
+ for [Mersenne Prime][1].
135
+
136
+ ### Reduction context
137
+
138
+ To enable this trick one should create a reduction context:
139
+
140
+ ```js
141
+ var red = BN.red(num);
142
+ ```
143
+ where `num` is just a BN instance.
144
+
145
+ Or:
146
+
147
+ ```js
148
+ var red = BN.red(primeName);
149
+ ```
150
+
151
+ Where `primeName` is either of these [Mersenne Primes][1]:
152
+
153
+ * `'k256'`
154
+ * `'p224'`
155
+ * `'p192'`
156
+ * `'p25519'`
157
+
158
+ Or:
159
+
160
+ ```js
161
+ var red = BN.mont(num);
162
+ ```
163
+
164
+ To reduce numbers with [Montgomery trick][0]. `.mont()` is generally faster than
165
+ `.red(num)`, but slower than `BN.red(primeName)`.
166
+
167
+ ### Converting numbers
168
+
169
+ Before performing anything in reduction context - numbers should be converted
170
+ to it. Usually, this means that one should:
171
+
172
+ * Convert inputs to reducted ones
173
+ * Operate on them in reduction context
174
+ * Convert outputs back from the reduction context
175
+
176
+ Here is how one may convert numbers to `red`:
177
+
178
+ ```js
179
+ var redA = a.toRed(red);
180
+ ```
181
+ Where `red` is a reduction context created using instructions above
182
+
183
+ Here is how to convert them back:
184
+
185
+ ```js
186
+ var a = redA.fromRed();
187
+ ```
188
+
189
+ ### Red instructions
190
+
191
+ Most of the instructions from the very start of this readme have their
192
+ counterparts in red context:
193
+
194
+ * `a.redAdd(b)`, `a.redIAdd(b)`
195
+ * `a.redSub(b)`, `a.redISub(b)`
196
+ * `a.redShl(num)`
197
+ * `a.redMul(b)`, `a.redIMul(b)`
198
+ * `a.redSqr()`, `a.redISqr()`
199
+ * `a.redSqrt()` - square root modulo reduction context's prime
200
+ * `a.redInvm()` - modular inverse of the number
201
+ * `a.redNeg()`
202
+ * `a.redPow(b)` - modular exponentiation
203
+
204
+ ### Number Size
205
+
206
+ Optimized for elliptic curves that work with 256-bit numbers.
207
+ There is no limitation on the size of the numbers.
208
+
209
+ ## LICENSE
210
+
211
+ This software is licensed under the MIT License.
212
+
213
+ [0]: https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
214
+ [1]: https://en.wikipedia.org/wiki/Mersenne_prime