bn.js 5.1.0 → 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 CHANGED
@@ -1,3 +1,25 @@
1
+ 5.2.0 / 2021-02-23
2
+ ------------------
3
+
4
+ - fix: Buffer not using global in browser (#260)
5
+ - Fix LE constructor for HEX (#265)
6
+
7
+ 5.1.3 / 2020-08-14
8
+ ------------------
9
+
10
+ - Add support for defined but not implemented Symbol.for (#252)
11
+
12
+ 5.1.2 / 2020-05-20
13
+ ------------------
14
+
15
+ - Fix BN v5/v4 interoperability issue (#249)
16
+
17
+ 5.1.1 / 2019-12-24
18
+ ------------------
19
+
20
+ - Temporary workaround for BN#_move (#236)
21
+ - Add eslintrc instead config in package.json (#237)
22
+
1
23
  5.1.0 / 2019-12-23
2
24
  ------------------
3
25
 
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 `1`
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 = require('buffer').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
- this._strip();
107
-
108
- if (endian !== 'le') return;
109
-
110
- this._initArray(this.toArray(), base, endian);
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 parseHex (str, start, end) {
187
- var r = 0;
188
- var len = Math.min(str.length, end);
189
- var z = 0;
190
- for (var i = start; i < len; i++) {
191
- var c = str.charCodeAt(i) - 48;
192
-
193
- r <<= 4;
194
-
195
- var b;
196
-
197
- // 'a' - 'f'
198
- if (c >= 49 && c <= 54) {
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
- assert(!(z & 0xf0), 'Invalid character in ' + str);
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
- var j, w;
227
- // Scan 24-bit chunks and add them to the number
218
+ // 24-bits chunks
228
219
  var off = 0;
229
- for (i = number.length - 6, j = 0; i >= start; i -= 6) {
230
- w = parseHex(number, i, i + 6);
231
- this.words[j] |= (w << off) & 0x3ffffff;
232
- // NOTE: `0x3fffff` is intentional here, 26bits max shift + 24bit hex limb
233
- this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
234
- off += 24;
235
- if (off >= 26) {
236
- off -= 26;
237
- j++;
238
- }
239
- }
240
- if (i + 6 !== start) {
241
- w = parseHex(number, start, i + 6);
242
- this.words[j] |= (w << off) & 0x3ffffff;
243
- this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
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) {
@@ -327,11 +334,15 @@
327
334
  dest.red = this.red;
328
335
  };
329
336
 
337
+ function move (dest, src) {
338
+ dest.words = src.words;
339
+ dest.length = src.length;
340
+ dest.negative = src.negative;
341
+ dest.red = src.red;
342
+ }
343
+
330
344
  BN.prototype._move = function _move (dest) {
331
- dest.words = this.words;
332
- dest.length = this.length;
333
- dest.negative = this.negative;
334
- dest.red = this.red;
345
+ move(dest, this);
335
346
  };
336
347
 
337
348
  BN.prototype.clone = function clone () {
@@ -366,7 +377,11 @@
366
377
  // Check Symbol.for because not everywhere where Symbol defined
367
378
  // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#Browser_compatibility
368
379
  if (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function') {
369
- BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect;
380
+ try {
381
+ BN.prototype[Symbol.for('nodejs.util.inspect.custom')] = inspect;
382
+ } catch (e) {
383
+ BN.prototype.inspect = inspect;
384
+ }
370
385
  } else {
371
386
  BN.prototype.inspect = inspect;
372
387
  }
@@ -3069,7 +3084,13 @@
3069
3084
  } else if (cmp > 0) {
3070
3085
  r.isub(this.p);
3071
3086
  } else {
3072
- r._strip();
3087
+ if (r.strip !== undefined) {
3088
+ // r is a BN v4 instance
3089
+ r.strip();
3090
+ } else {
3091
+ // r is a BN v5 instance
3092
+ r._strip();
3093
+ }
3073
3094
  }
3074
3095
 
3075
3096
  return r;
@@ -3243,7 +3264,7 @@
3243
3264
  Red.prototype.imod = function imod (a) {
3244
3265
  if (this.prime) return this.prime.ireduce(a)._forceRed(this);
3245
3266
 
3246
- a.umod(this.m)._forceRed(this)._move(a);
3267
+ move(a, a.umod(this.m)._forceRed(this));
3247
3268
  return a;
3248
3269
  };
3249
3270
 
package/package.json CHANGED
@@ -1,51 +1,39 @@
1
1
  {
2
2
  "name": "bn.js",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "Big number implementation in pure javascript",
5
- "main": "lib/bn.js",
6
- "scripts": {
7
- "lint": "standardx",
8
- "unit": "mocha --reporter=spec test/*-test.js",
9
- "test": "npm run lint && npm run unit"
10
- },
11
- "repository": {
12
- "type": "git",
13
- "url": "git@github.com:indutny/bn.js"
14
- },
15
5
  "keywords": [
16
6
  "BN",
17
- "BigNum",
18
7
  "Big number",
8
+ "BigNum",
19
9
  "Modulo",
20
10
  "Montgomery"
21
11
  ],
22
- "author": "Fedor Indutny <fedor@indutny.com>",
23
- "license": "MIT",
12
+ "homepage": "https://github.com/indutny/bn.js",
24
13
  "bugs": {
25
14
  "url": "https://github.com/indutny/bn.js/issues"
26
15
  },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git@github.com:indutny/bn.js"
19
+ },
20
+ "license": "MIT",
21
+ "author": "Fedor Indutny <fedor@indutny.com>",
27
22
  "files": [
28
- "lib"
23
+ "lib/bn.js"
29
24
  ],
30
- "homepage": "https://github.com/indutny/bn.js",
25
+ "main": "lib/bn.js",
31
26
  "browser": {
32
27
  "buffer": false
33
28
  },
34
- "devDependencies": {
35
- "babel-eslint": "^10.0.2",
36
- "mocha": "^6.1.4",
37
- "standardx": "^4.0.0"
38
- },
39
- "standardx": {
40
- "parser": "babel-eslint"
29
+ "scripts": {
30
+ "lint": "standardx",
31
+ "test": "npm run lint && npm run unit",
32
+ "unit": "mocha --reporter=spec test/*-test.js"
41
33
  },
42
- "eslintConfig": {
43
- "rules": {
44
- "semi": [
45
- 2,
46
- "always"
47
- ],
48
- "no-extra-semi": 2
49
- }
34
+ "devDependencies": {
35
+ "eslint-plugin-es5": "^1.5.0",
36
+ "mocha": "^8.3.0",
37
+ "standardx": "^7.0.0"
50
38
  }
51
39
  }