bn.js 4.11.6 → 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
@@ -40,12 +40,12 @@ is the list of them in the order of appearance in the function name:
40
40
  The only available postfix at the moment is:
41
41
 
42
42
  * `n` - which means that the argument of the function must be a plain JavaScript
43
- number
43
+ Number. Decimals are not supported.
44
44
 
45
45
  ### Examples
46
46
 
47
47
  * `a.iadd(b)` - perform addition on `a` and `b`, storing the result in `a`
48
- * `a.pmod(b)` - reduce `a` modulo `b`, returning positive value
48
+ * `a.umod(b)` - reduce `a` modulo `b`, returning positive value
49
49
  * `a.iushln(13)` - shift bits of `a` left by 13
50
50
 
51
51
  ## Instructions
@@ -63,7 +63,9 @@ either `le` (little-endian) or `be` (big-endian).
63
63
  pad to length, throwing if already exceeding
64
64
  * `a.toArrayLike(type, endian, length)` - convert to an instance of `type`,
65
65
  which must behave like an `Array`
66
- * `a.toBuffer(endian, length)` - convert to Node.js Buffer (if available)
66
+ * `a.toBuffer(endian, length)` - convert to Node.js Buffer (if available). For
67
+ compatibility with browserify and similar tools, use this instead:
68
+ `a.toArrayLike(Buffer, endian, length)`
67
69
  * `a.bitLength()` - get number of bits occupied
68
70
  * `a.zeroBits()` - return number of less-significant consequent zero bits
69
71
  (example: `1010000` has 4 zero bits)
@@ -81,7 +83,7 @@ either `le` (little-endian) or `be` (big-endian).
81
83
  * `a.eq(b)` - `a` equals `b` (`n`)
82
84
  * `a.toTwos(width)` - convert to two's complement representation, where `width` is bit width
83
85
  * `a.fromTwos(width)` - convert from two's complement representation, where `width` is the bit width
84
- * `a.isBN(object)` - returns true if the supplied `object` is a BN.js instance
86
+ * `BN.isBN(object)` - returns true if the supplied `object` is a BN.js instance
85
87
 
86
88
  ### Arithmetics
87
89
 
@@ -150,7 +152,7 @@ Or:
150
152
  var red = BN.mont(num);
151
153
  ```
152
154
 
153
- To reduce numbers with [Montgomery trick][1]. `.mont()` is generally faster than
155
+ To reduce numbers with [Montgomery trick][0]. `.mont()` is generally faster than
154
156
  `.red(num)`, but slower than `BN.red(primeName)`.
155
157
 
156
158
  ### Converting numbers
@@ -194,26 +196,5 @@ counterparts in red context:
194
196
 
195
197
  This software is licensed under the MIT License.
196
198
 
197
- Copyright Fedor Indutny, 2015.
198
-
199
- Permission is hereby granted, free of charge, to any person obtaining a
200
- copy of this software and associated documentation files (the
201
- "Software"), to deal in the Software without restriction, including
202
- without limitation the rights to use, copy, modify, merge, publish,
203
- distribute, sublicense, and/or sell copies of the Software, and to permit
204
- persons to whom the Software is furnished to do so, subject to the
205
- following conditions:
206
-
207
- The above copyright notice and this permission notice shall be included
208
- in all copies or substantial portions of the Software.
209
-
210
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
211
- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
212
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
213
- NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
214
- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
215
- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
216
- USE OR OTHER DEALINGS IN THE SOFTWARE.
217
-
218
199
  [0]: https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
219
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('buf' + 'fer').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) {
@@ -2972,7 +2985,13 @@
2972
2985
  } else if (cmp > 0) {
2973
2986
  r.isub(this.p);
2974
2987
  } else {
2975
- r.strip();
2988
+ if (r.strip !== undefined) {
2989
+ // r is BN v4 instance
2990
+ r.strip();
2991
+ } else {
2992
+ // r is BN v5 instance
2993
+ r._strip();
2994
+ }
2976
2995
  }
2977
2996
 
2978
2997
  return r;
@@ -3287,7 +3306,7 @@
3287
3306
  };
3288
3307
 
3289
3308
  Red.prototype.pow = function pow (a, num) {
3290
- if (num.isZero()) return new BN(1);
3309
+ if (num.isZero()) return new BN(1).toRed(this);
3291
3310
  if (num.cmpn(1) === 0) return a.clone();
3292
3311
 
3293
3312
  var windowSize = 4;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bn.js",
3
- "version": "4.11.6",
3
+ "version": "4.12.0",
4
4
  "description": "Big number implementation in pure javascript",
5
5
  "main": "lib/bn.js",
6
6
  "scripts": {
@@ -25,6 +25,9 @@
25
25
  "url": "https://github.com/indutny/bn.js/issues"
26
26
  },
27
27
  "homepage": "https://github.com/indutny/bn.js",
28
+ "browser": {
29
+ "buffer": false
30
+ },
28
31
  "devDependencies": {
29
32
  "istanbul": "^0.3.5",
30
33
  "mocha": "^2.1.0",
package/.npmignore DELETED
@@ -1,6 +0,0 @@
1
- benchmarks/
2
- coverage/
3
- node_modules/
4
- npm-debug.log
5
- 1.js
6
- logo.png
package/.travis.yml DELETED
@@ -1,15 +0,0 @@
1
- sudo: false
2
- language: node_js
3
- node_js:
4
- - "0.10"
5
- - "0.12"
6
- - "4"
7
- - "5"
8
- env:
9
- matrix:
10
- - TEST_SUITE=unit
11
- matrix:
12
- include:
13
- - node_js: "4"
14
- env: TEST_SUITE=lint
15
- script: npm run $TEST_SUITE