bn.js 5.1.2 → 5.2.1
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.
- package/LICENSE +19 -0
- package/README.md +11 -25
- package/lib/bn.js +77 -66
- package/package.json +4 -7
- package/CHANGELOG.md +0 -40
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
CHANGED
|
@@ -21,11 +21,17 @@ console.log(res.toString(10)); // 57047
|
|
|
21
21
|
|
|
22
22
|
**Note**: decimals are not supported in this library.
|
|
23
23
|
|
|
24
|
+
## Sponsors
|
|
25
|
+
|
|
26
|
+
[](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
|
+
|
|
24
30
|
## Notation
|
|
25
31
|
|
|
26
32
|
### Prefixes
|
|
27
33
|
|
|
28
|
-
There are several prefixes to instructions that affect the way
|
|
34
|
+
There are several prefixes to instructions that affect the way they work. Here
|
|
29
35
|
is the list of them in the order of appearance in the function name:
|
|
30
36
|
|
|
31
37
|
* `i` - perform operation in-place, storing the result in the host object (on
|
|
@@ -50,7 +56,7 @@ is the list of them in the order of appearance in the function name:
|
|
|
50
56
|
|
|
51
57
|
## Instructions
|
|
52
58
|
|
|
53
|
-
Prefixes/postfixes are put in parens at the of the line. `endian` - could be
|
|
59
|
+
Prefixes/postfixes are put in parens at the end of the line. `endian` - could be
|
|
54
60
|
either `le` (little-endian) or `be` (big-endian).
|
|
55
61
|
|
|
56
62
|
### Utilities
|
|
@@ -98,6 +104,7 @@ either `le` (little-endian) or `be` (big-endian).
|
|
|
98
104
|
* `a.pow(b)` - raise `a` to the power of `b`
|
|
99
105
|
* `a.div(b)` - divide (`divn`, `idivn`)
|
|
100
106
|
* `a.mod(b)` - reduct (`u`, `n`) (but no `umodn`)
|
|
107
|
+
* `a.divmod(b)` - quotient and modulus obtained by dividing
|
|
101
108
|
* `a.divRound(b)` - rounded division
|
|
102
109
|
|
|
103
110
|
### Bit operations
|
|
@@ -106,7 +113,7 @@ either `le` (little-endian) or `be` (big-endian).
|
|
|
106
113
|
* `a.and(b)` - and (`i`, `u`, `iu`, `andln`) (NOTE: `andln` is going to be replaced
|
|
107
114
|
with `andn` in future)
|
|
108
115
|
* `a.xor(b)` - xor (`i`, `u`, `iu`)
|
|
109
|
-
* `a.setn(b)` - set specified bit to `
|
|
116
|
+
* `a.setn(b, value)` - set specified bit to `value`
|
|
110
117
|
* `a.shln(b)` - shift left (`i`, `u`, `iu`)
|
|
111
118
|
* `a.shrn(b)` - shift right (`i`, `u`, `iu`)
|
|
112
119
|
* `a.testn(b)` - test if specified bit is set
|
|
@@ -128,7 +135,7 @@ for [Mersenne Prime][1].
|
|
|
128
135
|
|
|
129
136
|
### Reduction context
|
|
130
137
|
|
|
131
|
-
To enable this
|
|
138
|
+
To enable this trick one should create a reduction context:
|
|
132
139
|
|
|
133
140
|
```js
|
|
134
141
|
var red = BN.red(num);
|
|
@@ -203,26 +210,5 @@ There is no limitation on the size of the numbers.
|
|
|
203
210
|
|
|
204
211
|
This software is licensed under the MIT License.
|
|
205
212
|
|
|
206
|
-
Copyright Fedor Indutny, 2015.
|
|
207
|
-
|
|
208
|
-
Permission is hereby granted, free of charge, to any person obtaining a
|
|
209
|
-
copy of this software and associated documentation files (the
|
|
210
|
-
"Software"), to deal in the Software without restriction, including
|
|
211
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
|
212
|
-
distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
213
|
-
persons to whom the Software is furnished to do so, subject to the
|
|
214
|
-
following conditions:
|
|
215
|
-
|
|
216
|
-
The above copyright notice and this permission notice shall be included
|
|
217
|
-
in all copies or substantial portions of the Software.
|
|
218
|
-
|
|
219
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
220
|
-
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
221
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
222
|
-
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
223
|
-
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
224
|
-
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
225
|
-
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
226
|
-
|
|
227
213
|
[0]: https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
|
|
228
214
|
[1]: https://en.wikipedia.org/wiki/Mersenne_prime
|
package/lib/bn.js
CHANGED
|
@@ -50,7 +50,11 @@
|
|
|
50
50
|
|
|
51
51
|
var Buffer;
|
|
52
52
|
try {
|
|
53
|
-
Buffer
|
|
53
|
+
if (typeof window !== 'undefined' && typeof window.Buffer !== 'undefined') {
|
|
54
|
+
Buffer = window.Buffer;
|
|
55
|
+
} else {
|
|
56
|
+
Buffer = require('buffer').Buffer;
|
|
57
|
+
}
|
|
54
58
|
} catch (e) {
|
|
55
59
|
}
|
|
56
60
|
|
|
@@ -91,23 +95,19 @@
|
|
|
91
95
|
var start = 0;
|
|
92
96
|
if (number[0] === '-') {
|
|
93
97
|
start++;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (base === 16) {
|
|
97
|
-
this._parseHex(number, start);
|
|
98
|
-
} else {
|
|
99
|
-
this._parseBase(number, base, start);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (number[0] === '-') {
|
|
103
98
|
this.negative = 1;
|
|
104
99
|
}
|
|
105
100
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
101
|
+
if (start < number.length) {
|
|
102
|
+
if (base === 16) {
|
|
103
|
+
this._parseHex(number, start, endian);
|
|
104
|
+
} else {
|
|
105
|
+
this._parseBase(number, base, start);
|
|
106
|
+
if (endian === 'le') {
|
|
107
|
+
this._initArray(this.toArray(), base, endian);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
111
|
};
|
|
112
112
|
|
|
113
113
|
BN.prototype._initNumber = function _initNumber (number, base, endian) {
|
|
@@ -183,39 +183,31 @@
|
|
|
183
183
|
return this._strip();
|
|
184
184
|
};
|
|
185
185
|
|
|
186
|
-
function
|
|
187
|
-
var
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
b = c - 49 + 0xa;
|
|
200
|
-
|
|
201
|
-
// 'A' - 'F'
|
|
202
|
-
} else if (c >= 17 && c <= 22) {
|
|
203
|
-
b = c - 17 + 0xa;
|
|
204
|
-
|
|
205
|
-
// '0' - '9'
|
|
206
|
-
} else {
|
|
207
|
-
b = c;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
r |= b;
|
|
211
|
-
z |= b;
|
|
186
|
+
function parseHex4Bits (string, index) {
|
|
187
|
+
var c = string.charCodeAt(index);
|
|
188
|
+
// '0' - '9'
|
|
189
|
+
if (c >= 48 && c <= 57) {
|
|
190
|
+
return c - 48;
|
|
191
|
+
// 'A' - 'F'
|
|
192
|
+
} else if (c >= 65 && c <= 70) {
|
|
193
|
+
return c - 55;
|
|
194
|
+
// 'a' - 'f'
|
|
195
|
+
} else if (c >= 97 && c <= 102) {
|
|
196
|
+
return c - 87;
|
|
197
|
+
} else {
|
|
198
|
+
assert(false, 'Invalid character in ' + string);
|
|
212
199
|
}
|
|
200
|
+
}
|
|
213
201
|
|
|
214
|
-
|
|
202
|
+
function parseHexByte (string, lowerBound, index) {
|
|
203
|
+
var r = parseHex4Bits(string, index);
|
|
204
|
+
if (index - 1 >= lowerBound) {
|
|
205
|
+
r |= parseHex4Bits(string, index - 1) << 4;
|
|
206
|
+
}
|
|
215
207
|
return r;
|
|
216
208
|
}
|
|
217
209
|
|
|
218
|
-
BN.prototype._parseHex = function _parseHex (number, start) {
|
|
210
|
+
BN.prototype._parseHex = function _parseHex (number, start, endian) {
|
|
219
211
|
// Create possibly bigger array to ensure that it fits the number
|
|
220
212
|
this.length = Math.ceil((number.length - start) / 6);
|
|
221
213
|
this.words = new Array(this.length);
|
|
@@ -223,25 +215,38 @@
|
|
|
223
215
|
this.words[i] = 0;
|
|
224
216
|
}
|
|
225
217
|
|
|
226
|
-
|
|
227
|
-
// Scan 24-bit chunks and add them to the number
|
|
218
|
+
// 24-bits chunks
|
|
228
219
|
var off = 0;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
off
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
220
|
+
var j = 0;
|
|
221
|
+
|
|
222
|
+
var w;
|
|
223
|
+
if (endian === 'be') {
|
|
224
|
+
for (i = number.length - 1; i >= start; i -= 2) {
|
|
225
|
+
w = parseHexByte(number, start, i) << off;
|
|
226
|
+
this.words[j] |= w & 0x3ffffff;
|
|
227
|
+
if (off >= 18) {
|
|
228
|
+
off -= 18;
|
|
229
|
+
j += 1;
|
|
230
|
+
this.words[j] |= w >>> 26;
|
|
231
|
+
} else {
|
|
232
|
+
off += 8;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
var parseLength = number.length - start;
|
|
237
|
+
for (i = parseLength % 2 === 0 ? start + 1 : start; i < number.length; i += 2) {
|
|
238
|
+
w = parseHexByte(number, start, i) << off;
|
|
239
|
+
this.words[j] |= w & 0x3ffffff;
|
|
240
|
+
if (off >= 18) {
|
|
241
|
+
off -= 18;
|
|
242
|
+
j += 1;
|
|
243
|
+
this.words[j] |= w >>> 26;
|
|
244
|
+
} else {
|
|
245
|
+
off += 8;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
244
248
|
}
|
|
249
|
+
|
|
245
250
|
this._strip();
|
|
246
251
|
};
|
|
247
252
|
|
|
@@ -315,6 +320,8 @@
|
|
|
315
320
|
this._iaddn(word);
|
|
316
321
|
}
|
|
317
322
|
}
|
|
323
|
+
|
|
324
|
+
this._strip();
|
|
318
325
|
};
|
|
319
326
|
|
|
320
327
|
BN.prototype.copy = function copy (dest) {
|
|
@@ -370,7 +377,11 @@
|
|
|
370
377
|
// Check Symbol.for because not everywhere where Symbol defined
|
|
371
378
|
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#Browser_compatibility
|
|
372
379
|
if (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function') {
|
|
373
|
-
|
|
380
|
+
try {
|
|
381
|
+
BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect;
|
|
382
|
+
} catch (e) {
|
|
383
|
+
BN.prototype.inspect = inspect;
|
|
384
|
+
}
|
|
374
385
|
} else {
|
|
375
386
|
BN.prototype.inspect = inspect;
|
|
376
387
|
}
|
|
@@ -469,16 +480,16 @@
|
|
|
469
480
|
var w = this.words[i];
|
|
470
481
|
var word = (((w << off) | carry) & 0xffffff).toString(16);
|
|
471
482
|
carry = (w >>> (24 - off)) & 0xffffff;
|
|
472
|
-
if (carry !== 0 || i !== this.length - 1) {
|
|
473
|
-
out = zeros[6 - word.length] + word + out;
|
|
474
|
-
} else {
|
|
475
|
-
out = word + out;
|
|
476
|
-
}
|
|
477
483
|
off += 2;
|
|
478
484
|
if (off >= 26) {
|
|
479
485
|
off -= 26;
|
|
480
486
|
i--;
|
|
481
487
|
}
|
|
488
|
+
if (carry !== 0 || i !== this.length - 1) {
|
|
489
|
+
out = zeros[6 - word.length] + word + out;
|
|
490
|
+
} else {
|
|
491
|
+
out = word + out;
|
|
492
|
+
}
|
|
482
493
|
}
|
|
483
494
|
if (carry !== 0) {
|
|
484
495
|
out = carry.toString(16) + out;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bn.js",
|
|
3
|
-
"version": "5.1
|
|
3
|
+
"version": "5.2.1",
|
|
4
4
|
"description": "Big number implementation in pure javascript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"BN",
|
|
@@ -32,11 +32,8 @@
|
|
|
32
32
|
"unit": "mocha --reporter=spec test/*-test.js"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"
|
|
36
|
-
"mocha": "^
|
|
37
|
-
"standardx": "^
|
|
38
|
-
},
|
|
39
|
-
"standardx": {
|
|
40
|
-
"parser": "babel-eslint"
|
|
35
|
+
"eslint-plugin-es5": "^1.5.0",
|
|
36
|
+
"mocha": "^8.3.0",
|
|
37
|
+
"standardx": "^7.0.0"
|
|
41
38
|
}
|
|
42
39
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
5.1.2 / 2020-05-20
|
|
2
|
-
------------------
|
|
3
|
-
|
|
4
|
-
- Fix BN v5/v4 interoperability issue (#249)
|
|
5
|
-
|
|
6
|
-
5.1.1 / 2019-12-24
|
|
7
|
-
------------------
|
|
8
|
-
|
|
9
|
-
- Temporary workaround for BN#_move (#236)
|
|
10
|
-
- Add eslintrc instead config in package.json (#237)
|
|
11
|
-
|
|
12
|
-
5.1.0 / 2019-12-23
|
|
13
|
-
------------------
|
|
14
|
-
|
|
15
|
-
- Benchmark for BigInt (#226)
|
|
16
|
-
- Add documentation for max/min (#232)
|
|
17
|
-
- Update BN#inspect for Symbols (#225)
|
|
18
|
-
- Improve performance of toArrayLike (#222)
|
|
19
|
-
- temporary disable jumboMulTo in BN#mulTo (#221)
|
|
20
|
-
- optimize toBitArray function (#212)
|
|
21
|
-
- fix iaddn sign issue (#216)
|
|
22
|
-
|
|
23
|
-
5.0.0 / 2019-07-04
|
|
24
|
-
------------------
|
|
25
|
-
|
|
26
|
-
- travis: update node versions (#205)
|
|
27
|
-
- Refactor buffer constructor (#200)
|
|
28
|
-
- lib: fix for negative numbers: imuln, modrn, idivn (#185)
|
|
29
|
-
- bn: fix Red#imod (#178)
|
|
30
|
-
- check unexpected high bits for invalid characters (#173)
|
|
31
|
-
- document support very large integers (#158)
|
|
32
|
-
- only define toBuffer if Buffer is defined (#172)
|
|
33
|
-
- lib: better validation of string input (#151)
|
|
34
|
-
- tests: reject decimal input in constructor (#91)
|
|
35
|
-
- bn: make .strip() an internal method (#105)
|
|
36
|
-
- lib: deprecate `.modn()` introduce `.modrn()` (#112 #129 #130)
|
|
37
|
-
- bn: don't accept invalid characters (#141)
|
|
38
|
-
- package: use `files` insteadof `.npmignore` (#152)
|
|
39
|
-
- bn: improve allocation speed for buffers (#167)
|
|
40
|
-
- toJSON to default to interoperable hex (length % 2) (#164)
|