bn.js 5.1.3 → 5.2.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.
- package/CHANGELOG.md +6 -0
- package/LICENSE +19 -0
- package/README.md +2 -22
- package/lib/bn.js +67 -60
- package/package.json +4 -7
package/CHANGELOG.md
CHANGED
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
|
@@ -98,6 +98,7 @@ either `le` (little-endian) or `be` (big-endian).
|
|
|
98
98
|
* `a.pow(b)` - raise `a` to the power of `b`
|
|
99
99
|
* `a.div(b)` - divide (`divn`, `idivn`)
|
|
100
100
|
* `a.mod(b)` - reduct (`u`, `n`) (but no `umodn`)
|
|
101
|
+
* `a.divmod(b)` - quotient and modulus obtained by dividing
|
|
101
102
|
* `a.divRound(b)` - rounded division
|
|
102
103
|
|
|
103
104
|
### Bit operations
|
|
@@ -106,7 +107,7 @@ either `le` (little-endian) or `be` (big-endian).
|
|
|
106
107
|
* `a.and(b)` - and (`i`, `u`, `iu`, `andln`) (NOTE: `andln` is going to be replaced
|
|
107
108
|
with `andn` in future)
|
|
108
109
|
* `a.xor(b)` - xor (`i`, `u`, `iu`)
|
|
109
|
-
* `a.setn(b)` - set specified bit to `
|
|
110
|
+
* `a.setn(b, value)` - set specified bit to `value`
|
|
110
111
|
* `a.shln(b)` - shift left (`i`, `u`, `iu`)
|
|
111
112
|
* `a.shrn(b)` - shift right (`i`, `u`, `iu`)
|
|
112
113
|
* `a.testn(b)` - test if specified bit is set
|
|
@@ -203,26 +204,5 @@ There is no limitation on the size of the numbers.
|
|
|
203
204
|
|
|
204
205
|
This software is licensed under the MIT License.
|
|
205
206
|
|
|
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
207
|
[0]: https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
|
|
228
208
|
[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) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bn.js",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
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
|
}
|