bn.js 4.11.9 → 4.12.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/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
@@ -196,26 +196,5 @@ counterparts in red context:
196
196
 
197
197
  This software is licensed under the MIT License.
198
198
 
199
- Copyright Fedor Indutny, 2015.
200
-
201
- Permission is hereby granted, free of charge, to any person obtaining a
202
- copy of this software and associated documentation files (the
203
- "Software"), to deal in the Software without restriction, including
204
- without limitation the rights to use, copy, modify, merge, publish,
205
- distribute, sublicense, and/or sell copies of the Software, and to permit
206
- persons to whom the Software is furnished to do so, subject to the
207
- following conditions:
208
-
209
- The above copyright notice and this permission notice shall be included
210
- in all copies or substantial portions of the Software.
211
-
212
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
213
- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
214
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
215
- NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
216
- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
217
- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
218
- USE OR OTHER DEALINGS IN THE SOFTWARE.
219
-
220
199
  [0]: https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
221
200
  [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,31 +183,29 @@
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
- for (var i = start; i < len; i++) {
190
- var c = str.charCodeAt(i) - 48;
191
-
192
- r <<= 4;
193
-
194
- // 'a' - 'f'
195
- if (c >= 49 && c <= 54) {
196
- r |= c - 49 + 0xa;
197
-
198
- // 'A' - 'F'
199
- } else if (c >= 17 && c <= 22) {
200
- r |= c - 17 + 0xa;
186
+ function parseHex4Bits (string, index) {
187
+ var c = string.charCodeAt(index);
188
+ // 'A' - 'F'
189
+ if (c >= 65 && c <= 70) {
190
+ return c - 55;
191
+ // 'a' - 'f'
192
+ } else if (c >= 97 && c <= 102) {
193
+ return c - 87;
194
+ // '0' - '9'
195
+ } else {
196
+ return (c - 48) & 0xf;
197
+ }
198
+ }
201
199
 
202
- // '0' - '9'
203
- } else {
204
- r |= c & 0xf;
205
- }
200
+ function parseHexByte (string, lowerBound, index) {
201
+ var r = parseHex4Bits(string, index);
202
+ if (index - 1 >= lowerBound) {
203
+ r |= parseHex4Bits(string, index - 1) << 4;
206
204
  }
207
205
  return r;
208
206
  }
209
207
 
210
- BN.prototype._parseHex = function _parseHex (number, start) {
208
+ BN.prototype._parseHex = function _parseHex (number, start, endian) {
211
209
  // Create possibly bigger array to ensure that it fits the number
212
210
  this.length = Math.ceil((number.length - start) / 6);
213
211
  this.words = new Array(this.length);
@@ -215,25 +213,38 @@
215
213
  this.words[i] = 0;
216
214
  }
217
215
 
218
- var j, w;
219
- // Scan 24-bit chunks and add them to the number
216
+ // 24-bits chunks
220
217
  var off = 0;
221
- for (i = number.length - 6, j = 0; i >= start; i -= 6) {
222
- w = parseHex(number, i, i + 6);
223
- this.words[j] |= (w << off) & 0x3ffffff;
224
- // NOTE: `0x3fffff` is intentional here, 26bits max shift + 24bit hex limb
225
- this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
226
- off += 24;
227
- if (off >= 26) {
228
- off -= 26;
229
- j++;
230
- }
231
- }
232
- if (i + 6 !== start) {
233
- w = parseHex(number, start, i + 6);
234
- this.words[j] |= (w << off) & 0x3ffffff;
235
- this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
218
+ var j = 0;
219
+
220
+ var w;
221
+ if (endian === 'be') {
222
+ for (i = number.length - 1; i >= start; i -= 2) {
223
+ w = parseHexByte(number, start, i) << off;
224
+ this.words[j] |= w & 0x3ffffff;
225
+ if (off >= 18) {
226
+ off -= 18;
227
+ j += 1;
228
+ this.words[j] |= w >>> 26;
229
+ } else {
230
+ off += 8;
231
+ }
232
+ }
233
+ } else {
234
+ var parseLength = number.length - start;
235
+ for (i = parseLength % 2 === 0 ? start + 1 : start; i < number.length; i += 2) {
236
+ w = parseHexByte(number, start, i) << off;
237
+ this.words[j] |= w & 0x3ffffff;
238
+ if (off >= 18) {
239
+ off -= 18;
240
+ j += 1;
241
+ this.words[j] |= w >>> 26;
242
+ } else {
243
+ off += 8;
244
+ }
245
+ }
236
246
  }
247
+
237
248
  this.strip();
238
249
  };
239
250
 
@@ -304,6 +315,8 @@
304
315
  this._iaddn(word);
305
316
  }
306
317
  }
318
+
319
+ this.strip();
307
320
  };
308
321
 
309
322
  BN.prototype.copy = function copy (dest) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bn.js",
3
- "version": "4.11.9",
3
+ "version": "4.12.0",
4
4
  "description": "Big number implementation in pure javascript",
5
5
  "main": "lib/bn.js",
6
6
  "scripts": {
@@ -1,65 +0,0 @@
1
- 'use strict';
2
-
3
- // NOTE: This could be potentionally used to generate loop-less multiplications
4
- function genCombMulTo (alen, blen) {
5
- var len = alen + blen - 1;
6
- var src = [
7
- 'var a = self.words;',
8
- 'var b = num.words;',
9
- 'var o = out.words;',
10
- 'var c = 0;',
11
- 'var lo;',
12
- 'var mid;',
13
- 'var hi;'
14
- ];
15
- for (var i = 0; i < alen; i++) {
16
- src.push('var a' + i + ' = a[' + i + '] | 0;');
17
- src.push('var al' + i + ' = a' + i + ' & 0x1fff;');
18
- src.push('var ah' + i + ' = a' + i + ' >>> 13;');
19
- }
20
- for (i = 0; i < blen; i++) {
21
- src.push('var b' + i + ' = b[' + i + '] | 0;');
22
- src.push('var bl' + i + ' = b' + i + ' & 0x1fff;');
23
- src.push('var bh' + i + ' = b' + i + ' >>> 13;');
24
- }
25
- src.push('');
26
- src.push('out.negative = self.negative ^ num.negative;');
27
- src.push('out.length = ' + len + ';');
28
-
29
- for (var k = 0; k < len; k++) {
30
- var minJ = Math.max(0, k - alen + 1);
31
- var maxJ = Math.min(k, blen - 1);
32
-
33
- src.push('\/* k = ' + k + ' *\/');
34
- src.push('var w' + k + ' = c;');
35
- src.push('c = 0;');
36
- for (var j = minJ; j <= maxJ; j++) {
37
- i = k - j;
38
-
39
- src.push('lo = Math.imul(al' + i + ', bl' + j + ');');
40
- src.push('mid = Math.imul(al' + i + ', bh' + j + ');');
41
- src.push('mid = (mid + Math.imul(ah' + i + ', bl' + j + ')) | 0;');
42
- src.push('hi = Math.imul(ah' + i + ', bh' + j + ');');
43
-
44
- src.push('w' + k + ' = (w' + k + ' + lo) | 0;');
45
- src.push('w' + k + ' = (w' + k + ' + ((mid & 0x1fff) << 13)) | 0;');
46
- src.push('c = (c + hi) | 0;');
47
- src.push('c = (c + (mid >>> 13)) | 0;');
48
- src.push('c = (c + (w' + k + ' >>> 26)) | 0;');
49
- src.push('w' + k + ' &= 0x3ffffff;');
50
- }
51
- }
52
- // Store in separate step for better memory access
53
- for (k = 0; k < len; k++) {
54
- src.push('o[' + k + '] = w' + k + ';');
55
- }
56
- src.push('if (c !== 0) {',
57
- ' o[' + k + '] = c;',
58
- ' out.length++;',
59
- '}',
60
- 'return out;');
61
-
62
- return src.join('\n');
63
- }
64
-
65
- console.log(genCombMulTo(10, 10));
@@ -1,65 +0,0 @@
1
- 'use strict';
2
-
3
- function genCombMulTo (alen, blen) {
4
- var len = alen + blen - 1;
5
- var src = [
6
- 'var a = self.words;',
7
- 'var b = num.words;',
8
- 'var o = out.words;',
9
- 'var c = 0;',
10
- 'var lo;',
11
- 'var mid;',
12
- 'var hi;'
13
- ];
14
- for (var i = 0; i < alen; i++) {
15
- src.push('var a' + i + ' = a[' + i + '] | 0;');
16
- src.push('var al' + i + ' = a' + i + ' & 0x1fff;');
17
- src.push('var ah' + i + ' = a' + i + ' >>> 13;');
18
- }
19
- for (i = 0; i < blen; i++) {
20
- src.push('var b' + i + ' = b[' + i + '] | 0;');
21
- src.push('var bl' + i + ' = b' + i + ' & 0x1fff;');
22
- src.push('var bh' + i + ' = b' + i + ' >>> 13;');
23
- }
24
- src.push('');
25
- src.push('out.negative = self.negative ^ num.negative;');
26
- src.push('out.length = ' + len + ';');
27
-
28
- for (var k = 0; k < len; k++) {
29
- var minJ = Math.max(0, k - alen + 1);
30
- var maxJ = Math.min(k, blen - 1);
31
-
32
- src.push('\/* k = ' + k + ' *\/');
33
- src.push('lo = Math.imul(al' + (k - minJ) + ', bl' + minJ + ');');
34
- src.push('mid = Math.imul(al' + (k - minJ) + ', bh' + minJ + ');');
35
- src.push(
36
- 'mid = (mid + Math.imul(ah' + (k - minJ) + ', bl' + minJ + ')) | 0;');
37
- src.push('hi = Math.imul(ah' + (k - minJ) + ', bh' + minJ + ');');
38
-
39
- for (var j = minJ + 1; j <= maxJ; j++) {
40
- i = k - j;
41
-
42
- src.push('lo = (lo + Math.imul(al' + i + ', bl' + j + ')) | 0;');
43
- src.push('mid = (mid + Math.imul(al' + i + ', bh' + j + ')) | 0;');
44
- src.push('mid = (mid + Math.imul(ah' + i + ', bl' + j + ')) | 0;');
45
- src.push('hi = (hi + Math.imul(ah' + i + ', bh' + j + ')) | 0;');
46
- }
47
-
48
- src.push('var w' + k + ' = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;');
49
- src.push('c = (((hi + (mid >>> 13)) | 0) + (w' + k + ' >>> 26)) | 0;');
50
- src.push('w' + k + ' &= 0x3ffffff;');
51
- }
52
- // Store in separate step for better memory access
53
- for (k = 0; k < len; k++) {
54
- src.push('o[' + k + '] = w' + k + ';');
55
- }
56
- src.push('if (c !== 0) {',
57
- ' o[' + k + '] = c;',
58
- ' out.length++;',
59
- '}',
60
- 'return out;');
61
-
62
- return src.join('\n');
63
- }
64
-
65
- console.log(genCombMulTo(10, 10));