]>
Commit | Line | Data |
---|---|---|
a0091a40 IC |
1 | (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.bitcoinjs = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ |
2 | (function (global){ | |
3 | 'use strict'; | |
4 | ||
5 | // compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js | |
6 | // original notice: | |
7 | ||
8 | /*! | |
9 | * The buffer module from node.js, for the browser. | |
10 | * | |
11 | * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org> | |
12 | * @license MIT | |
13 | */ | |
14 | function compare(a, b) { | |
15 | if (a === b) { | |
16 | return 0; | |
17 | } | |
18 | ||
19 | var x = a.length; | |
20 | var y = b.length; | |
21 | ||
22 | for (var i = 0, len = Math.min(x, y); i < len; ++i) { | |
23 | if (a[i] !== b[i]) { | |
24 | x = a[i]; | |
25 | y = b[i]; | |
26 | break; | |
27 | } | |
28 | } | |
29 | ||
30 | if (x < y) { | |
31 | return -1; | |
32 | } | |
33 | if (y < x) { | |
34 | return 1; | |
35 | } | |
36 | return 0; | |
37 | } | |
38 | function isBuffer(b) { | |
39 | if (global.Buffer && typeof global.Buffer.isBuffer === 'function') { | |
40 | return global.Buffer.isBuffer(b); | |
41 | } | |
42 | return !!(b != null && b._isBuffer); | |
43 | } | |
44 | ||
45 | // based on node assert, original notice: | |
46 | ||
47 | // http://wiki.commonjs.org/wiki/Unit_Testing/1.0 | |
48 | // | |
49 | // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! | |
50 | // | |
51 | // Originally from narwhal.js (http://narwhaljs.org) | |
52 | // Copyright (c) 2009 Thomas Robinson <280north.com> | |
53 | // | |
54 | // Permission is hereby granted, free of charge, to any person obtaining a copy | |
55 | // of this software and associated documentation files (the 'Software'), to | |
56 | // deal in the Software without restriction, including without limitation the | |
57 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
58 | // sell copies of the Software, and to permit persons to whom the Software is | |
59 | // furnished to do so, subject to the following conditions: | |
60 | // | |
61 | // The above copyright notice and this permission notice shall be included in | |
62 | // all copies or substantial portions of the Software. | |
63 | // | |
64 | // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
65 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
66 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
67 | // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
68 | // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
69 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
70 | ||
71 | var util = require('util/'); | |
72 | var hasOwn = Object.prototype.hasOwnProperty; | |
73 | var pSlice = Array.prototype.slice; | |
74 | var functionsHaveNames = (function () { | |
75 | return function foo() {}.name === 'foo'; | |
76 | }()); | |
77 | function pToString (obj) { | |
78 | return Object.prototype.toString.call(obj); | |
79 | } | |
80 | function isView(arrbuf) { | |
81 | if (isBuffer(arrbuf)) { | |
82 | return false; | |
83 | } | |
84 | if (typeof global.ArrayBuffer !== 'function') { | |
85 | return false; | |
86 | } | |
87 | if (typeof ArrayBuffer.isView === 'function') { | |
88 | return ArrayBuffer.isView(arrbuf); | |
89 | } | |
90 | if (!arrbuf) { | |
91 | return false; | |
92 | } | |
93 | if (arrbuf instanceof DataView) { | |
94 | return true; | |
95 | } | |
96 | if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) { | |
97 | return true; | |
98 | } | |
99 | return false; | |
100 | } | |
101 | // 1. The assert module provides functions that throw | |
102 | // AssertionError's when particular conditions are not met. The | |
103 | // assert module must conform to the following interface. | |
104 | ||
105 | var assert = module.exports = ok; | |
106 | ||
107 | // 2. The AssertionError is defined in assert. | |
108 | // new assert.AssertionError({ message: message, | |
109 | // actual: actual, | |
110 | // expected: expected }) | |
111 | ||
112 | var regex = /\s*function\s+([^\(\s]*)\s*/; | |
113 | // based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js | |
114 | function getName(func) { | |
115 | if (!util.isFunction(func)) { | |
116 | return; | |
117 | } | |
118 | if (functionsHaveNames) { | |
119 | return func.name; | |
120 | } | |
121 | var str = func.toString(); | |
122 | var match = str.match(regex); | |
123 | return match && match[1]; | |
124 | } | |
125 | assert.AssertionError = function AssertionError(options) { | |
126 | this.name = 'AssertionError'; | |
127 | this.actual = options.actual; | |
128 | this.expected = options.expected; | |
129 | this.operator = options.operator; | |
130 | if (options.message) { | |
131 | this.message = options.message; | |
132 | this.generatedMessage = false; | |
133 | } else { | |
134 | this.message = getMessage(this); | |
135 | this.generatedMessage = true; | |
136 | } | |
137 | var stackStartFunction = options.stackStartFunction || fail; | |
138 | if (Error.captureStackTrace) { | |
139 | Error.captureStackTrace(this, stackStartFunction); | |
140 | } else { | |
141 | // non v8 browsers so we can have a stacktrace | |
142 | var err = new Error(); | |
143 | if (err.stack) { | |
144 | var out = err.stack; | |
145 | ||
146 | // try to strip useless frames | |
147 | var fn_name = getName(stackStartFunction); | |
148 | var idx = out.indexOf('\n' + fn_name); | |
149 | if (idx >= 0) { | |
150 | // once we have located the function frame | |
151 | // we need to strip out everything before it (and its line) | |
152 | var next_line = out.indexOf('\n', idx + 1); | |
153 | out = out.substring(next_line + 1); | |
154 | } | |
155 | ||
156 | this.stack = out; | |
157 | } | |
158 | } | |
159 | }; | |
160 | ||
161 | // assert.AssertionError instanceof Error | |
162 | util.inherits(assert.AssertionError, Error); | |
163 | ||
164 | function truncate(s, n) { | |
165 | if (typeof s === 'string') { | |
166 | return s.length < n ? s : s.slice(0, n); | |
167 | } else { | |
168 | return s; | |
169 | } | |
170 | } | |
171 | function inspect(something) { | |
172 | if (functionsHaveNames || !util.isFunction(something)) { | |
173 | return util.inspect(something); | |
174 | } | |
175 | var rawname = getName(something); | |
176 | var name = rawname ? ': ' + rawname : ''; | |
177 | return '[Function' + name + ']'; | |
178 | } | |
179 | function getMessage(self) { | |
180 | return truncate(inspect(self.actual), 128) + ' ' + | |
181 | self.operator + ' ' + | |
182 | truncate(inspect(self.expected), 128); | |
183 | } | |
184 | ||
185 | // At present only the three keys mentioned above are used and | |
186 | // understood by the spec. Implementations or sub modules can pass | |
187 | // other keys to the AssertionError's constructor - they will be | |
188 | // ignored. | |
189 | ||
190 | // 3. All of the following functions must throw an AssertionError | |
191 | // when a corresponding condition is not met, with a message that | |
192 | // may be undefined if not provided. All assertion methods provide | |
193 | // both the actual and expected values to the assertion error for | |
194 | // display purposes. | |
195 | ||
196 | function fail(actual, expected, message, operator, stackStartFunction) { | |
197 | throw new assert.AssertionError({ | |
198 | message: message, | |
199 | actual: actual, | |
200 | expected: expected, | |
201 | operator: operator, | |
202 | stackStartFunction: stackStartFunction | |
203 | }); | |
204 | } | |
205 | ||
206 | // EXTENSION! allows for well behaved errors defined elsewhere. | |
207 | assert.fail = fail; | |
208 | ||
209 | // 4. Pure assertion tests whether a value is truthy, as determined | |
210 | // by !!guard. | |
211 | // assert.ok(guard, message_opt); | |
212 | // This statement is equivalent to assert.equal(true, !!guard, | |
213 | // message_opt);. To test strictly for the value true, use | |
214 | // assert.strictEqual(true, guard, message_opt);. | |
215 | ||
216 | function ok(value, message) { | |
217 | if (!value) fail(value, true, message, '==', assert.ok); | |
218 | } | |
219 | assert.ok = ok; | |
220 | ||
221 | // 5. The equality assertion tests shallow, coercive equality with | |
222 | // ==. | |
223 | // assert.equal(actual, expected, message_opt); | |
224 | ||
225 | assert.equal = function equal(actual, expected, message) { | |
226 | if (actual != expected) fail(actual, expected, message, '==', assert.equal); | |
227 | }; | |
228 | ||
229 | // 6. The non-equality assertion tests for whether two objects are not equal | |
230 | // with != assert.notEqual(actual, expected, message_opt); | |
231 | ||
232 | assert.notEqual = function notEqual(actual, expected, message) { | |
233 | if (actual == expected) { | |
234 | fail(actual, expected, message, '!=', assert.notEqual); | |
235 | } | |
236 | }; | |
237 | ||
238 | // 7. The equivalence assertion tests a deep equality relation. | |
239 | // assert.deepEqual(actual, expected, message_opt); | |
240 | ||
241 | assert.deepEqual = function deepEqual(actual, expected, message) { | |
242 | if (!_deepEqual(actual, expected, false)) { | |
243 | fail(actual, expected, message, 'deepEqual', assert.deepEqual); | |
244 | } | |
245 | }; | |
246 | ||
247 | assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { | |
248 | if (!_deepEqual(actual, expected, true)) { | |
249 | fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual); | |
250 | } | |
251 | }; | |
252 | ||
253 | function _deepEqual(actual, expected, strict, memos) { | |
254 | // 7.1. All identical values are equivalent, as determined by ===. | |
255 | if (actual === expected) { | |
256 | return true; | |
257 | } else if (isBuffer(actual) && isBuffer(expected)) { | |
258 | return compare(actual, expected) === 0; | |
259 | ||
260 | // 7.2. If the expected value is a Date object, the actual value is | |
261 | // equivalent if it is also a Date object that refers to the same time. | |
262 | } else if (util.isDate(actual) && util.isDate(expected)) { | |
263 | return actual.getTime() === expected.getTime(); | |
264 | ||
265 | // 7.3 If the expected value is a RegExp object, the actual value is | |
266 | // equivalent if it is also a RegExp object with the same source and | |
267 | // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). | |
268 | } else if (util.isRegExp(actual) && util.isRegExp(expected)) { | |
269 | return actual.source === expected.source && | |
270 | actual.global === expected.global && | |
271 | actual.multiline === expected.multiline && | |
272 | actual.lastIndex === expected.lastIndex && | |
273 | actual.ignoreCase === expected.ignoreCase; | |
274 | ||
275 | // 7.4. Other pairs that do not both pass typeof value == 'object', | |
276 | // equivalence is determined by ==. | |
277 | } else if ((actual === null || typeof actual !== 'object') && | |
278 | (expected === null || typeof expected !== 'object')) { | |
279 | return strict ? actual === expected : actual == expected; | |
280 | ||
281 | // If both values are instances of typed arrays, wrap their underlying | |
282 | // ArrayBuffers in a Buffer each to increase performance | |
283 | // This optimization requires the arrays to have the same type as checked by | |
284 | // Object.prototype.toString (aka pToString). Never perform binary | |
285 | // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their | |
286 | // bit patterns are not identical. | |
287 | } else if (isView(actual) && isView(expected) && | |
288 | pToString(actual) === pToString(expected) && | |
289 | !(actual instanceof Float32Array || | |
290 | actual instanceof Float64Array)) { | |
291 | return compare(new Uint8Array(actual.buffer), | |
292 | new Uint8Array(expected.buffer)) === 0; | |
293 | ||
294 | // 7.5 For all other Object pairs, including Array objects, equivalence is | |
295 | // determined by having the same number of owned properties (as verified | |
296 | // with Object.prototype.hasOwnProperty.call), the same set of keys | |
297 | // (although not necessarily the same order), equivalent values for every | |
298 | // corresponding key, and an identical 'prototype' property. Note: this | |
299 | // accounts for both named and indexed properties on Arrays. | |
300 | } else if (isBuffer(actual) !== isBuffer(expected)) { | |
301 | return false; | |
302 | } else { | |
303 | memos = memos || {actual: [], expected: []}; | |
304 | ||
305 | var actualIndex = memos.actual.indexOf(actual); | |
306 | if (actualIndex !== -1) { | |
307 | if (actualIndex === memos.expected.indexOf(expected)) { | |
308 | return true; | |
309 | } | |
310 | } | |
311 | ||
312 | memos.actual.push(actual); | |
313 | memos.expected.push(expected); | |
314 | ||
315 | return objEquiv(actual, expected, strict, memos); | |
316 | } | |
317 | } | |
318 | ||
319 | function isArguments(object) { | |
320 | return Object.prototype.toString.call(object) == '[object Arguments]'; | |
321 | } | |
322 | ||
323 | function objEquiv(a, b, strict, actualVisitedObjects) { | |
324 | if (a === null || a === undefined || b === null || b === undefined) | |
325 | return false; | |
326 | // if one is a primitive, the other must be same | |
327 | if (util.isPrimitive(a) || util.isPrimitive(b)) | |
328 | return a === b; | |
329 | if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) | |
330 | return false; | |
331 | var aIsArgs = isArguments(a); | |
332 | var bIsArgs = isArguments(b); | |
333 | if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs)) | |
334 | return false; | |
335 | if (aIsArgs) { | |
336 | a = pSlice.call(a); | |
337 | b = pSlice.call(b); | |
338 | return _deepEqual(a, b, strict); | |
339 | } | |
340 | var ka = objectKeys(a); | |
341 | var kb = objectKeys(b); | |
342 | var key, i; | |
343 | // having the same number of owned properties (keys incorporates | |
344 | // hasOwnProperty) | |
345 | if (ka.length !== kb.length) | |
346 | return false; | |
347 | //the same set of keys (although not necessarily the same order), | |
348 | ka.sort(); | |
349 | kb.sort(); | |
350 | //~~~cheap key test | |
351 | for (i = ka.length - 1; i >= 0; i--) { | |
352 | if (ka[i] !== kb[i]) | |
353 | return false; | |
354 | } | |
355 | //equivalent values for every corresponding key, and | |
356 | //~~~possibly expensive deep test | |
357 | for (i = ka.length - 1; i >= 0; i--) { | |
358 | key = ka[i]; | |
359 | if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects)) | |
360 | return false; | |
361 | } | |
362 | return true; | |
363 | } | |
364 | ||
365 | // 8. The non-equivalence assertion tests for any deep inequality. | |
366 | // assert.notDeepEqual(actual, expected, message_opt); | |
367 | ||
368 | assert.notDeepEqual = function notDeepEqual(actual, expected, message) { | |
369 | if (_deepEqual(actual, expected, false)) { | |
370 | fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); | |
371 | } | |
372 | }; | |
373 | ||
374 | assert.notDeepStrictEqual = notDeepStrictEqual; | |
375 | function notDeepStrictEqual(actual, expected, message) { | |
376 | if (_deepEqual(actual, expected, true)) { | |
377 | fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual); | |
378 | } | |
379 | } | |
380 | ||
381 | ||
382 | // 9. The strict equality assertion tests strict equality, as determined by ===. | |
383 | // assert.strictEqual(actual, expected, message_opt); | |
384 | ||
385 | assert.strictEqual = function strictEqual(actual, expected, message) { | |
386 | if (actual !== expected) { | |
387 | fail(actual, expected, message, '===', assert.strictEqual); | |
388 | } | |
389 | }; | |
390 | ||
391 | // 10. The strict non-equality assertion tests for strict inequality, as | |
392 | // determined by !==. assert.notStrictEqual(actual, expected, message_opt); | |
393 | ||
394 | assert.notStrictEqual = function notStrictEqual(actual, expected, message) { | |
395 | if (actual === expected) { | |
396 | fail(actual, expected, message, '!==', assert.notStrictEqual); | |
397 | } | |
398 | }; | |
399 | ||
400 | function expectedException(actual, expected) { | |
401 | if (!actual || !expected) { | |
402 | return false; | |
403 | } | |
404 | ||
405 | if (Object.prototype.toString.call(expected) == '[object RegExp]') { | |
406 | return expected.test(actual); | |
407 | } | |
408 | ||
409 | try { | |
410 | if (actual instanceof expected) { | |
411 | return true; | |
412 | } | |
413 | } catch (e) { | |
414 | // Ignore. The instanceof check doesn't work for arrow functions. | |
415 | } | |
416 | ||
417 | if (Error.isPrototypeOf(expected)) { | |
418 | return false; | |
419 | } | |
420 | ||
421 | return expected.call({}, actual) === true; | |
422 | } | |
423 | ||
424 | function _tryBlock(block) { | |
425 | var error; | |
426 | try { | |
427 | block(); | |
428 | } catch (e) { | |
429 | error = e; | |
430 | } | |
431 | return error; | |
432 | } | |
433 | ||
434 | function _throws(shouldThrow, block, expected, message) { | |
435 | var actual; | |
436 | ||
437 | if (typeof block !== 'function') { | |
438 | throw new TypeError('"block" argument must be a function'); | |
439 | } | |
440 | ||
441 | if (typeof expected === 'string') { | |
442 | message = expected; | |
443 | expected = null; | |
444 | } | |
445 | ||
446 | actual = _tryBlock(block); | |
447 | ||
448 | message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + | |
449 | (message ? ' ' + message : '.'); | |
450 | ||
451 | if (shouldThrow && !actual) { | |
452 | fail(actual, expected, 'Missing expected exception' + message); | |
453 | } | |
454 | ||
455 | var userProvidedMessage = typeof message === 'string'; | |
456 | var isUnwantedException = !shouldThrow && util.isError(actual); | |
457 | var isUnexpectedException = !shouldThrow && actual && !expected; | |
458 | ||
459 | if ((isUnwantedException && | |
460 | userProvidedMessage && | |
461 | expectedException(actual, expected)) || | |
462 | isUnexpectedException) { | |
463 | fail(actual, expected, 'Got unwanted exception' + message); | |
464 | } | |
465 | ||
466 | if ((shouldThrow && actual && expected && | |
467 | !expectedException(actual, expected)) || (!shouldThrow && actual)) { | |
468 | throw actual; | |
469 | } | |
470 | } | |
471 | ||
472 | // 11. Expected to throw an error: | |
473 | // assert.throws(block, Error_opt, message_opt); | |
474 | ||
475 | assert.throws = function(block, /*optional*/error, /*optional*/message) { | |
476 | _throws(true, block, error, message); | |
477 | }; | |
478 | ||
479 | // EXTENSION! This is annoying to write outside this module. | |
480 | assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { | |
481 | _throws(false, block, error, message); | |
482 | }; | |
483 | ||
484 | assert.ifError = function(err) { if (err) throw err; }; | |
485 | ||
486 | var objectKeys = Object.keys || function (obj) { | |
487 | var keys = []; | |
488 | for (var key in obj) { | |
489 | if (hasOwn.call(obj, key)) keys.push(key); | |
490 | } | |
491 | return keys; | |
492 | }; | |
493 | ||
494 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | |
9f59e99b | 495 | },{"util/":33}],2:[function(require,module,exports){ |
a0091a40 IC |
496 | 'use strict' |
497 | ||
498 | exports.byteLength = byteLength | |
499 | exports.toByteArray = toByteArray | |
500 | exports.fromByteArray = fromByteArray | |
501 | ||
502 | var lookup = [] | |
503 | var revLookup = [] | |
504 | var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array | |
505 | ||
506 | var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | |
507 | for (var i = 0, len = code.length; i < len; ++i) { | |
508 | lookup[i] = code[i] | |
509 | revLookup[code.charCodeAt(i)] = i | |
510 | } | |
511 | ||
512 | revLookup['-'.charCodeAt(0)] = 62 | |
513 | revLookup['_'.charCodeAt(0)] = 63 | |
514 | ||
515 | function placeHoldersCount (b64) { | |
516 | var len = b64.length | |
517 | if (len % 4 > 0) { | |
518 | throw new Error('Invalid string. Length must be a multiple of 4') | |
519 | } | |
520 | ||
521 | // the number of equal signs (place holders) | |
522 | // if there are two placeholders, than the two characters before it | |
523 | // represent one byte | |
524 | // if there is only one, then the three characters before it represent 2 bytes | |
525 | // this is just a cheap hack to not do indexOf twice | |
526 | return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 | |
527 | } | |
528 | ||
529 | function byteLength (b64) { | |
530 | // base64 is 4/3 + up to two characters of the original data | |
9f59e99b | 531 | return b64.length * 3 / 4 - placeHoldersCount(b64) |
a0091a40 IC |
532 | } |
533 | ||
534 | function toByteArray (b64) { | |
9f59e99b | 535 | var i, j, l, tmp, placeHolders, arr |
a0091a40 IC |
536 | var len = b64.length |
537 | placeHolders = placeHoldersCount(b64) | |
538 | ||
9f59e99b | 539 | arr = new Arr(len * 3 / 4 - placeHolders) |
a0091a40 IC |
540 | |
541 | // if there are placeholders, only get up to the last complete 4 chars | |
542 | l = placeHolders > 0 ? len - 4 : len | |
543 | ||
544 | var L = 0 | |
545 | ||
9f59e99b | 546 | for (i = 0, j = 0; i < l; i += 4, j += 3) { |
a0091a40 IC |
547 | tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] |
548 | arr[L++] = (tmp >> 16) & 0xFF | |
549 | arr[L++] = (tmp >> 8) & 0xFF | |
550 | arr[L++] = tmp & 0xFF | |
551 | } | |
552 | ||
553 | if (placeHolders === 2) { | |
554 | tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) | |
555 | arr[L++] = tmp & 0xFF | |
556 | } else if (placeHolders === 1) { | |
557 | tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) | |
558 | arr[L++] = (tmp >> 8) & 0xFF | |
559 | arr[L++] = tmp & 0xFF | |
560 | } | |
561 | ||
562 | return arr | |
563 | } | |
564 | ||
565 | function tripletToBase64 (num) { | |
566 | return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] | |
567 | } | |
568 | ||
569 | function encodeChunk (uint8, start, end) { | |
570 | var tmp | |
571 | var output = [] | |
572 | for (var i = start; i < end; i += 3) { | |
573 | tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) | |
574 | output.push(tripletToBase64(tmp)) | |
575 | } | |
576 | return output.join('') | |
577 | } | |
578 | ||
579 | function fromByteArray (uint8) { | |
580 | var tmp | |
581 | var len = uint8.length | |
582 | var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes | |
583 | var output = '' | |
584 | var parts = [] | |
585 | var maxChunkLength = 16383 // must be multiple of 3 | |
586 | ||
587 | // go through the array every three bytes, we'll deal with trailing stuff later | |
588 | for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { | |
589 | parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) | |
590 | } | |
591 | ||
592 | // pad the end with zeros, but make sure to not forget the extra bytes | |
593 | if (extraBytes === 1) { | |
594 | tmp = uint8[len - 1] | |
595 | output += lookup[tmp >> 2] | |
596 | output += lookup[(tmp << 4) & 0x3F] | |
597 | output += '==' | |
598 | } else if (extraBytes === 2) { | |
599 | tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) | |
600 | output += lookup[tmp >> 10] | |
601 | output += lookup[(tmp >> 4) & 0x3F] | |
602 | output += lookup[(tmp << 2) & 0x3F] | |
603 | output += '=' | |
604 | } | |
605 | ||
606 | parts.push(output) | |
607 | ||
608 | return parts.join('') | |
609 | } | |
610 | ||
611 | },{}],3:[function(require,module,exports){ | |
612 | ||
613 | },{}],4:[function(require,module,exports){ | |
9f59e99b IC |
614 | (function (global){ |
615 | 'use strict'; | |
616 | ||
617 | var buffer = require('buffer'); | |
618 | var Buffer = buffer.Buffer; | |
619 | var SlowBuffer = buffer.SlowBuffer; | |
620 | var MAX_LEN = buffer.kMaxLength || 2147483647; | |
621 | exports.alloc = function alloc(size, fill, encoding) { | |
622 | if (typeof Buffer.alloc === 'function') { | |
623 | return Buffer.alloc(size, fill, encoding); | |
624 | } | |
625 | if (typeof encoding === 'number') { | |
626 | throw new TypeError('encoding must not be number'); | |
627 | } | |
628 | if (typeof size !== 'number') { | |
629 | throw new TypeError('size must be a number'); | |
630 | } | |
631 | if (size > MAX_LEN) { | |
632 | throw new RangeError('size is too large'); | |
633 | } | |
634 | var enc = encoding; | |
635 | var _fill = fill; | |
636 | if (_fill === undefined) { | |
637 | enc = undefined; | |
638 | _fill = 0; | |
639 | } | |
640 | var buf = new Buffer(size); | |
641 | if (typeof _fill === 'string') { | |
642 | var fillBuf = new Buffer(_fill, enc); | |
643 | var flen = fillBuf.length; | |
644 | var i = -1; | |
645 | while (++i < size) { | |
646 | buf[i] = fillBuf[i % flen]; | |
647 | } | |
648 | } else { | |
649 | buf.fill(_fill); | |
650 | } | |
651 | return buf; | |
652 | } | |
653 | exports.allocUnsafe = function allocUnsafe(size) { | |
654 | if (typeof Buffer.allocUnsafe === 'function') { | |
655 | return Buffer.allocUnsafe(size); | |
656 | } | |
657 | if (typeof size !== 'number') { | |
658 | throw new TypeError('size must be a number'); | |
659 | } | |
660 | if (size > MAX_LEN) { | |
661 | throw new RangeError('size is too large'); | |
662 | } | |
663 | return new Buffer(size); | |
664 | } | |
665 | exports.from = function from(value, encodingOrOffset, length) { | |
666 | if (typeof Buffer.from === 'function' && (!global.Uint8Array || Uint8Array.from !== Buffer.from)) { | |
667 | return Buffer.from(value, encodingOrOffset, length); | |
668 | } | |
669 | if (typeof value === 'number') { | |
670 | throw new TypeError('"value" argument must not be a number'); | |
671 | } | |
672 | if (typeof value === 'string') { | |
673 | return new Buffer(value, encodingOrOffset); | |
674 | } | |
675 | if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) { | |
676 | var offset = encodingOrOffset; | |
677 | if (arguments.length === 1) { | |
678 | return new Buffer(value); | |
679 | } | |
680 | if (typeof offset === 'undefined') { | |
681 | offset = 0; | |
682 | } | |
683 | var len = length; | |
684 | if (typeof len === 'undefined') { | |
685 | len = value.byteLength - offset; | |
686 | } | |
687 | if (offset >= value.byteLength) { | |
688 | throw new RangeError('\'offset\' is out of bounds'); | |
689 | } | |
690 | if (len > value.byteLength - offset) { | |
691 | throw new RangeError('\'length\' is out of bounds'); | |
692 | } | |
693 | return new Buffer(value.slice(offset, offset + len)); | |
694 | } | |
695 | if (Buffer.isBuffer(value)) { | |
696 | var out = new Buffer(value.length); | |
697 | value.copy(out, 0, 0, value.length); | |
698 | return out; | |
699 | } | |
700 | if (value) { | |
701 | if (Array.isArray(value) || (typeof ArrayBuffer !== 'undefined' && value.buffer instanceof ArrayBuffer) || 'length' in value) { | |
702 | return new Buffer(value); | |
703 | } | |
704 | if (value.type === 'Buffer' && Array.isArray(value.data)) { | |
705 | return new Buffer(value.data); | |
706 | } | |
707 | } | |
708 | ||
709 | throw new TypeError('First argument must be a string, Buffer, ' + 'ArrayBuffer, Array, or array-like object.'); | |
710 | } | |
711 | exports.allocUnsafeSlow = function allocUnsafeSlow(size) { | |
712 | if (typeof Buffer.allocUnsafeSlow === 'function') { | |
713 | return Buffer.allocUnsafeSlow(size); | |
714 | } | |
715 | if (typeof size !== 'number') { | |
716 | throw new TypeError('size must be a number'); | |
717 | } | |
718 | if (size >= MAX_LEN) { | |
719 | throw new RangeError('size is too large'); | |
720 | } | |
721 | return new SlowBuffer(size); | |
722 | } | |
723 | ||
724 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | |
725 | },{"buffer":5}],5:[function(require,module,exports){ | |
a0091a40 IC |
726 | /*! |
727 | * The buffer module from node.js, for the browser. | |
728 | * | |
729 | * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org> | |
730 | * @license MIT | |
731 | */ | |
732 | /* eslint-disable no-proto */ | |
733 | ||
734 | 'use strict' | |
735 | ||
736 | var base64 = require('base64-js') | |
737 | var ieee754 = require('ieee754') | |
738 | ||
739 | exports.Buffer = Buffer | |
740 | exports.SlowBuffer = SlowBuffer | |
741 | exports.INSPECT_MAX_BYTES = 50 | |
742 | ||
743 | var K_MAX_LENGTH = 0x7fffffff | |
744 | exports.kMaxLength = K_MAX_LENGTH | |
745 | ||
746 | /** | |
747 | * If `Buffer.TYPED_ARRAY_SUPPORT`: | |
748 | * === true Use Uint8Array implementation (fastest) | |
749 | * === false Print warning and recommend using `buffer` v4.x which has an Object | |
750 | * implementation (most compatible, even IE6) | |
751 | * | |
752 | * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, | |
753 | * Opera 11.6+, iOS 4.2+. | |
754 | * | |
755 | * We report that the browser does not support typed arrays if the are not subclassable | |
756 | * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` | |
757 | * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support | |
758 | * for __proto__ and has a buggy typed array implementation. | |
759 | */ | |
760 | Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport() | |
761 | ||
762 | if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && | |
763 | typeof console.error === 'function') { | |
764 | console.error( | |
765 | 'This browser lacks typed array (Uint8Array) support which is required by ' + | |
766 | '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' | |
767 | ) | |
768 | } | |
769 | ||
770 | function typedArraySupport () { | |
771 | // Can typed array instances can be augmented? | |
772 | try { | |
773 | var arr = new Uint8Array(1) | |
774 | arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }} | |
775 | return arr.foo() === 42 | |
776 | } catch (e) { | |
777 | return false | |
778 | } | |
779 | } | |
780 | ||
781 | function createBuffer (length) { | |
782 | if (length > K_MAX_LENGTH) { | |
783 | throw new RangeError('Invalid typed array length') | |
784 | } | |
785 | // Return an augmented `Uint8Array` instance | |
786 | var buf = new Uint8Array(length) | |
787 | buf.__proto__ = Buffer.prototype | |
788 | return buf | |
789 | } | |
790 | ||
791 | /** | |
792 | * The Buffer constructor returns instances of `Uint8Array` that have their | |
793 | * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of | |
794 | * `Uint8Array`, so the returned instances will have all the node `Buffer` methods | |
795 | * and the `Uint8Array` methods. Square bracket notation works as expected -- it | |
796 | * returns a single octet. | |
797 | * | |
798 | * The `Uint8Array` prototype remains unmodified. | |
799 | */ | |
800 | ||
801 | function Buffer (arg, encodingOrOffset, length) { | |
802 | // Common case. | |
803 | if (typeof arg === 'number') { | |
804 | if (typeof encodingOrOffset === 'string') { | |
805 | throw new Error( | |
806 | 'If encoding is specified then the first argument must be a string' | |
807 | ) | |
808 | } | |
809 | return allocUnsafe(arg) | |
810 | } | |
811 | return from(arg, encodingOrOffset, length) | |
812 | } | |
813 | ||
814 | // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 | |
815 | if (typeof Symbol !== 'undefined' && Symbol.species && | |
816 | Buffer[Symbol.species] === Buffer) { | |
817 | Object.defineProperty(Buffer, Symbol.species, { | |
818 | value: null, | |
819 | configurable: true, | |
820 | enumerable: false, | |
821 | writable: false | |
822 | }) | |
823 | } | |
824 | ||
825 | Buffer.poolSize = 8192 // not used by this implementation | |
826 | ||
827 | function from (value, encodingOrOffset, length) { | |
828 | if (typeof value === 'number') { | |
829 | throw new TypeError('"value" argument must not be a number') | |
830 | } | |
831 | ||
832 | if (value instanceof ArrayBuffer) { | |
833 | return fromArrayBuffer(value, encodingOrOffset, length) | |
834 | } | |
835 | ||
836 | if (typeof value === 'string') { | |
837 | return fromString(value, encodingOrOffset) | |
838 | } | |
839 | ||
840 | return fromObject(value) | |
841 | } | |
842 | ||
843 | /** | |
844 | * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError | |
845 | * if value is a number. | |
846 | * Buffer.from(str[, encoding]) | |
847 | * Buffer.from(array) | |
848 | * Buffer.from(buffer) | |
849 | * Buffer.from(arrayBuffer[, byteOffset[, length]]) | |
850 | **/ | |
851 | Buffer.from = function (value, encodingOrOffset, length) { | |
852 | return from(value, encodingOrOffset, length) | |
853 | } | |
854 | ||
855 | // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: | |
856 | // https://github.com/feross/buffer/pull/148 | |
857 | Buffer.prototype.__proto__ = Uint8Array.prototype | |
858 | Buffer.__proto__ = Uint8Array | |
859 | ||
860 | function assertSize (size) { | |
861 | if (typeof size !== 'number') { | |
862 | throw new TypeError('"size" argument must be a number') | |
863 | } else if (size < 0) { | |
864 | throw new RangeError('"size" argument must not be negative') | |
865 | } | |
866 | } | |
867 | ||
868 | function alloc (size, fill, encoding) { | |
869 | assertSize(size) | |
870 | if (size <= 0) { | |
871 | return createBuffer(size) | |
872 | } | |
873 | if (fill !== undefined) { | |
874 | // Only pay attention to encoding if it's a string. This | |
875 | // prevents accidentally sending in a number that would | |
876 | // be interpretted as a start offset. | |
877 | return typeof encoding === 'string' | |
878 | ? createBuffer(size).fill(fill, encoding) | |
879 | : createBuffer(size).fill(fill) | |
880 | } | |
881 | return createBuffer(size) | |
882 | } | |
883 | ||
884 | /** | |
885 | * Creates a new filled Buffer instance. | |
886 | * alloc(size[, fill[, encoding]]) | |
887 | **/ | |
888 | Buffer.alloc = function (size, fill, encoding) { | |
889 | return alloc(size, fill, encoding) | |
890 | } | |
891 | ||
892 | function allocUnsafe (size) { | |
893 | assertSize(size) | |
894 | return createBuffer(size < 0 ? 0 : checked(size) | 0) | |
895 | } | |
896 | ||
897 | /** | |
898 | * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. | |
899 | * */ | |
900 | Buffer.allocUnsafe = function (size) { | |
901 | return allocUnsafe(size) | |
902 | } | |
903 | /** | |
904 | * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. | |
905 | */ | |
906 | Buffer.allocUnsafeSlow = function (size) { | |
907 | return allocUnsafe(size) | |
908 | } | |
909 | ||
910 | function fromString (string, encoding) { | |
911 | if (typeof encoding !== 'string' || encoding === '') { | |
912 | encoding = 'utf8' | |
913 | } | |
914 | ||
915 | if (!Buffer.isEncoding(encoding)) { | |
916 | throw new TypeError('"encoding" must be a valid string encoding') | |
917 | } | |
918 | ||
919 | var length = byteLength(string, encoding) | 0 | |
920 | var buf = createBuffer(length) | |
921 | ||
922 | var actual = buf.write(string, encoding) | |
923 | ||
924 | if (actual !== length) { | |
925 | // Writing a hex string, for example, that contains invalid characters will | |
926 | // cause everything after the first invalid character to be ignored. (e.g. | |
927 | // 'abxxcd' will be treated as 'ab') | |
928 | buf = buf.slice(0, actual) | |
929 | } | |
930 | ||
931 | return buf | |
932 | } | |
933 | ||
934 | function fromArrayLike (array) { | |
935 | var length = array.length < 0 ? 0 : checked(array.length) | 0 | |
936 | var buf = createBuffer(length) | |
937 | for (var i = 0; i < length; i += 1) { | |
938 | buf[i] = array[i] & 255 | |
939 | } | |
940 | return buf | |
941 | } | |
942 | ||
943 | function fromArrayBuffer (array, byteOffset, length) { | |
944 | if (byteOffset < 0 || array.byteLength < byteOffset) { | |
945 | throw new RangeError('\'offset\' is out of bounds') | |
946 | } | |
947 | ||
948 | if (array.byteLength < byteOffset + (length || 0)) { | |
949 | throw new RangeError('\'length\' is out of bounds') | |
950 | } | |
951 | ||
952 | var buf | |
953 | if (byteOffset === undefined && length === undefined) { | |
954 | buf = new Uint8Array(array) | |
955 | } else if (length === undefined) { | |
956 | buf = new Uint8Array(array, byteOffset) | |
957 | } else { | |
958 | buf = new Uint8Array(array, byteOffset, length) | |
959 | } | |
960 | ||
961 | // Return an augmented `Uint8Array` instance | |
962 | buf.__proto__ = Buffer.prototype | |
963 | return buf | |
964 | } | |
965 | ||
966 | function fromObject (obj) { | |
967 | if (Buffer.isBuffer(obj)) { | |
968 | var len = checked(obj.length) | 0 | |
969 | var buf = createBuffer(len) | |
970 | ||
971 | if (buf.length === 0) { | |
972 | return buf | |
973 | } | |
974 | ||
975 | obj.copy(buf, 0, 0, len) | |
976 | return buf | |
977 | } | |
978 | ||
979 | if (obj) { | |
980 | if (isArrayBufferView(obj) || 'length' in obj) { | |
981 | if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { | |
982 | return createBuffer(0) | |
983 | } | |
984 | return fromArrayLike(obj) | |
985 | } | |
986 | ||
987 | if (obj.type === 'Buffer' && Array.isArray(obj.data)) { | |
988 | return fromArrayLike(obj.data) | |
989 | } | |
990 | } | |
991 | ||
992 | throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.') | |
993 | } | |
994 | ||
995 | function checked (length) { | |
996 | // Note: cannot use `length < K_MAX_LENGTH` here because that fails when | |
997 | // length is NaN (which is otherwise coerced to zero.) | |
998 | if (length >= K_MAX_LENGTH) { | |
999 | throw new RangeError('Attempt to allocate Buffer larger than maximum ' + | |
1000 | 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes') | |
1001 | } | |
1002 | return length | 0 | |
1003 | } | |
1004 | ||
1005 | function SlowBuffer (length) { | |
1006 | if (+length != length) { // eslint-disable-line eqeqeq | |
1007 | length = 0 | |
1008 | } | |
1009 | return Buffer.alloc(+length) | |
1010 | } | |
1011 | ||
1012 | Buffer.isBuffer = function isBuffer (b) { | |
1013 | return b != null && b._isBuffer === true | |
1014 | } | |
1015 | ||
1016 | Buffer.compare = function compare (a, b) { | |
1017 | if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { | |
1018 | throw new TypeError('Arguments must be Buffers') | |
1019 | } | |
1020 | ||
1021 | if (a === b) return 0 | |
1022 | ||
1023 | var x = a.length | |
1024 | var y = b.length | |
1025 | ||
1026 | for (var i = 0, len = Math.min(x, y); i < len; ++i) { | |
1027 | if (a[i] !== b[i]) { | |
1028 | x = a[i] | |
1029 | y = b[i] | |
1030 | break | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | if (x < y) return -1 | |
1035 | if (y < x) return 1 | |
1036 | return 0 | |
1037 | } | |
1038 | ||
1039 | Buffer.isEncoding = function isEncoding (encoding) { | |
1040 | switch (String(encoding).toLowerCase()) { | |
1041 | case 'hex': | |
1042 | case 'utf8': | |
1043 | case 'utf-8': | |
1044 | case 'ascii': | |
1045 | case 'latin1': | |
1046 | case 'binary': | |
1047 | case 'base64': | |
1048 | case 'ucs2': | |
1049 | case 'ucs-2': | |
1050 | case 'utf16le': | |
1051 | case 'utf-16le': | |
1052 | return true | |
1053 | default: | |
1054 | return false | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | Buffer.concat = function concat (list, length) { | |
1059 | if (!Array.isArray(list)) { | |
1060 | throw new TypeError('"list" argument must be an Array of Buffers') | |
1061 | } | |
1062 | ||
1063 | if (list.length === 0) { | |
1064 | return Buffer.alloc(0) | |
1065 | } | |
1066 | ||
1067 | var i | |
1068 | if (length === undefined) { | |
1069 | length = 0 | |
1070 | for (i = 0; i < list.length; ++i) { | |
1071 | length += list[i].length | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | var buffer = Buffer.allocUnsafe(length) | |
1076 | var pos = 0 | |
1077 | for (i = 0; i < list.length; ++i) { | |
1078 | var buf = list[i] | |
1079 | if (!Buffer.isBuffer(buf)) { | |
1080 | throw new TypeError('"list" argument must be an Array of Buffers') | |
1081 | } | |
1082 | buf.copy(buffer, pos) | |
1083 | pos += buf.length | |
1084 | } | |
1085 | return buffer | |
1086 | } | |
1087 | ||
1088 | function byteLength (string, encoding) { | |
1089 | if (Buffer.isBuffer(string)) { | |
1090 | return string.length | |
1091 | } | |
1092 | if (isArrayBufferView(string) || string instanceof ArrayBuffer) { | |
1093 | return string.byteLength | |
1094 | } | |
1095 | if (typeof string !== 'string') { | |
1096 | string = '' + string | |
1097 | } | |
1098 | ||
1099 | var len = string.length | |
1100 | if (len === 0) return 0 | |
1101 | ||
1102 | // Use a for loop to avoid recursion | |
1103 | var loweredCase = false | |
1104 | for (;;) { | |
1105 | switch (encoding) { | |
1106 | case 'ascii': | |
1107 | case 'latin1': | |
1108 | case 'binary': | |
1109 | return len | |
1110 | case 'utf8': | |
1111 | case 'utf-8': | |
1112 | case undefined: | |
1113 | return utf8ToBytes(string).length | |
1114 | case 'ucs2': | |
1115 | case 'ucs-2': | |
1116 | case 'utf16le': | |
1117 | case 'utf-16le': | |
1118 | return len * 2 | |
1119 | case 'hex': | |
1120 | return len >>> 1 | |
1121 | case 'base64': | |
1122 | return base64ToBytes(string).length | |
1123 | default: | |
1124 | if (loweredCase) return utf8ToBytes(string).length // assume utf8 | |
1125 | encoding = ('' + encoding).toLowerCase() | |
1126 | loweredCase = true | |
1127 | } | |
1128 | } | |
1129 | } | |
1130 | Buffer.byteLength = byteLength | |
1131 | ||
1132 | function slowToString (encoding, start, end) { | |
1133 | var loweredCase = false | |
1134 | ||
1135 | // No need to verify that "this.length <= MAX_UINT32" since it's a read-only | |
1136 | // property of a typed array. | |
1137 | ||
1138 | // This behaves neither like String nor Uint8Array in that we set start/end | |
1139 | // to their upper/lower bounds if the value passed is out of range. | |
1140 | // undefined is handled specially as per ECMA-262 6th Edition, | |
1141 | // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. | |
1142 | if (start === undefined || start < 0) { | |
1143 | start = 0 | |
1144 | } | |
1145 | // Return early if start > this.length. Done here to prevent potential uint32 | |
1146 | // coercion fail below. | |
1147 | if (start > this.length) { | |
1148 | return '' | |
1149 | } | |
1150 | ||
1151 | if (end === undefined || end > this.length) { | |
1152 | end = this.length | |
1153 | } | |
1154 | ||
1155 | if (end <= 0) { | |
1156 | return '' | |
1157 | } | |
1158 | ||
1159 | // Force coersion to uint32. This will also coerce falsey/NaN values to 0. | |
1160 | end >>>= 0 | |
1161 | start >>>= 0 | |
1162 | ||
1163 | if (end <= start) { | |
1164 | return '' | |
1165 | } | |
1166 | ||
1167 | if (!encoding) encoding = 'utf8' | |
1168 | ||
1169 | while (true) { | |
1170 | switch (encoding) { | |
1171 | case 'hex': | |
1172 | return hexSlice(this, start, end) | |
1173 | ||
1174 | case 'utf8': | |
1175 | case 'utf-8': | |
1176 | return utf8Slice(this, start, end) | |
1177 | ||
1178 | case 'ascii': | |
1179 | return asciiSlice(this, start, end) | |
1180 | ||
1181 | case 'latin1': | |
1182 | case 'binary': | |
1183 | return latin1Slice(this, start, end) | |
1184 | ||
1185 | case 'base64': | |
1186 | return base64Slice(this, start, end) | |
1187 | ||
1188 | case 'ucs2': | |
1189 | case 'ucs-2': | |
1190 | case 'utf16le': | |
1191 | case 'utf-16le': | |
1192 | return utf16leSlice(this, start, end) | |
1193 | ||
1194 | default: | |
1195 | if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) | |
1196 | encoding = (encoding + '').toLowerCase() | |
1197 | loweredCase = true | |
1198 | } | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) | |
1203 | // to detect a Buffer instance. It's not possible to use `instanceof Buffer` | |
1204 | // reliably in a browserify context because there could be multiple different | |
1205 | // copies of the 'buffer' package in use. This method works even for Buffer | |
1206 | // instances that were created from another copy of the `buffer` package. | |
1207 | // See: https://github.com/feross/buffer/issues/154 | |
1208 | Buffer.prototype._isBuffer = true | |
1209 | ||
1210 | function swap (b, n, m) { | |
1211 | var i = b[n] | |
1212 | b[n] = b[m] | |
1213 | b[m] = i | |
1214 | } | |
1215 | ||
1216 | Buffer.prototype.swap16 = function swap16 () { | |
1217 | var len = this.length | |
1218 | if (len % 2 !== 0) { | |
1219 | throw new RangeError('Buffer size must be a multiple of 16-bits') | |
1220 | } | |
1221 | for (var i = 0; i < len; i += 2) { | |
1222 | swap(this, i, i + 1) | |
1223 | } | |
1224 | return this | |
1225 | } | |
1226 | ||
1227 | Buffer.prototype.swap32 = function swap32 () { | |
1228 | var len = this.length | |
1229 | if (len % 4 !== 0) { | |
1230 | throw new RangeError('Buffer size must be a multiple of 32-bits') | |
1231 | } | |
1232 | for (var i = 0; i < len; i += 4) { | |
1233 | swap(this, i, i + 3) | |
1234 | swap(this, i + 1, i + 2) | |
1235 | } | |
1236 | return this | |
1237 | } | |
1238 | ||
1239 | Buffer.prototype.swap64 = function swap64 () { | |
1240 | var len = this.length | |
1241 | if (len % 8 !== 0) { | |
1242 | throw new RangeError('Buffer size must be a multiple of 64-bits') | |
1243 | } | |
1244 | for (var i = 0; i < len; i += 8) { | |
1245 | swap(this, i, i + 7) | |
1246 | swap(this, i + 1, i + 6) | |
1247 | swap(this, i + 2, i + 5) | |
1248 | swap(this, i + 3, i + 4) | |
1249 | } | |
1250 | return this | |
1251 | } | |
1252 | ||
1253 | Buffer.prototype.toString = function toString () { | |
1254 | var length = this.length | |
1255 | if (length === 0) return '' | |
1256 | if (arguments.length === 0) return utf8Slice(this, 0, length) | |
1257 | return slowToString.apply(this, arguments) | |
1258 | } | |
1259 | ||
1260 | Buffer.prototype.equals = function equals (b) { | |
1261 | if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') | |
1262 | if (this === b) return true | |
1263 | return Buffer.compare(this, b) === 0 | |
1264 | } | |
1265 | ||
1266 | Buffer.prototype.inspect = function inspect () { | |
1267 | var str = '' | |
1268 | var max = exports.INSPECT_MAX_BYTES | |
1269 | if (this.length > 0) { | |
1270 | str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') | |
1271 | if (this.length > max) str += ' ... ' | |
1272 | } | |
1273 | return '<Buffer ' + str + '>' | |
1274 | } | |
1275 | ||
1276 | Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { | |
1277 | if (!Buffer.isBuffer(target)) { | |
1278 | throw new TypeError('Argument must be a Buffer') | |
1279 | } | |
1280 | ||
1281 | if (start === undefined) { | |
1282 | start = 0 | |
1283 | } | |
1284 | if (end === undefined) { | |
1285 | end = target ? target.length : 0 | |
1286 | } | |
1287 | if (thisStart === undefined) { | |
1288 | thisStart = 0 | |
1289 | } | |
1290 | if (thisEnd === undefined) { | |
1291 | thisEnd = this.length | |
1292 | } | |
1293 | ||
1294 | if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { | |
1295 | throw new RangeError('out of range index') | |
1296 | } | |
1297 | ||
1298 | if (thisStart >= thisEnd && start >= end) { | |
1299 | return 0 | |
1300 | } | |
1301 | if (thisStart >= thisEnd) { | |
1302 | return -1 | |
1303 | } | |
1304 | if (start >= end) { | |
1305 | return 1 | |
1306 | } | |
1307 | ||
1308 | start >>>= 0 | |
1309 | end >>>= 0 | |
1310 | thisStart >>>= 0 | |
1311 | thisEnd >>>= 0 | |
1312 | ||
1313 | if (this === target) return 0 | |
1314 | ||
1315 | var x = thisEnd - thisStart | |
1316 | var y = end - start | |
1317 | var len = Math.min(x, y) | |
1318 | ||
1319 | var thisCopy = this.slice(thisStart, thisEnd) | |
1320 | var targetCopy = target.slice(start, end) | |
1321 | ||
1322 | for (var i = 0; i < len; ++i) { | |
1323 | if (thisCopy[i] !== targetCopy[i]) { | |
1324 | x = thisCopy[i] | |
1325 | y = targetCopy[i] | |
1326 | break | |
1327 | } | |
1328 | } | |
1329 | ||
1330 | if (x < y) return -1 | |
1331 | if (y < x) return 1 | |
1332 | return 0 | |
1333 | } | |
1334 | ||
1335 | // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, | |
1336 | // OR the last index of `val` in `buffer` at offset <= `byteOffset`. | |
1337 | // | |
1338 | // Arguments: | |
1339 | // - buffer - a Buffer to search | |
1340 | // - val - a string, Buffer, or number | |
1341 | // - byteOffset - an index into `buffer`; will be clamped to an int32 | |
1342 | // - encoding - an optional encoding, relevant is val is a string | |
1343 | // - dir - true for indexOf, false for lastIndexOf | |
1344 | function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { | |
1345 | // Empty buffer means no match | |
1346 | if (buffer.length === 0) return -1 | |
1347 | ||
1348 | // Normalize byteOffset | |
1349 | if (typeof byteOffset === 'string') { | |
1350 | encoding = byteOffset | |
1351 | byteOffset = 0 | |
1352 | } else if (byteOffset > 0x7fffffff) { | |
1353 | byteOffset = 0x7fffffff | |
1354 | } else if (byteOffset < -0x80000000) { | |
1355 | byteOffset = -0x80000000 | |
1356 | } | |
1357 | byteOffset = +byteOffset // Coerce to Number. | |
1358 | if (numberIsNaN(byteOffset)) { | |
1359 | // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer | |
1360 | byteOffset = dir ? 0 : (buffer.length - 1) | |
1361 | } | |
1362 | ||
1363 | // Normalize byteOffset: negative offsets start from the end of the buffer | |
1364 | if (byteOffset < 0) byteOffset = buffer.length + byteOffset | |
1365 | if (byteOffset >= buffer.length) { | |
1366 | if (dir) return -1 | |
1367 | else byteOffset = buffer.length - 1 | |
1368 | } else if (byteOffset < 0) { | |
1369 | if (dir) byteOffset = 0 | |
1370 | else return -1 | |
1371 | } | |
1372 | ||
1373 | // Normalize val | |
1374 | if (typeof val === 'string') { | |
1375 | val = Buffer.from(val, encoding) | |
1376 | } | |
1377 | ||
1378 | // Finally, search either indexOf (if dir is true) or lastIndexOf | |
1379 | if (Buffer.isBuffer(val)) { | |
1380 | // Special case: looking for empty string/buffer always fails | |
1381 | if (val.length === 0) { | |
1382 | return -1 | |
1383 | } | |
1384 | return arrayIndexOf(buffer, val, byteOffset, encoding, dir) | |
1385 | } else if (typeof val === 'number') { | |
1386 | val = val & 0xFF // Search for a byte value [0-255] | |
1387 | if (typeof Uint8Array.prototype.indexOf === 'function') { | |
1388 | if (dir) { | |
1389 | return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) | |
1390 | } else { | |
1391 | return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) | |
1392 | } | |
1393 | } | |
1394 | return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir) | |
1395 | } | |
1396 | ||
1397 | throw new TypeError('val must be string, number or Buffer') | |
1398 | } | |
1399 | ||
1400 | function arrayIndexOf (arr, val, byteOffset, encoding, dir) { | |
1401 | var indexSize = 1 | |
1402 | var arrLength = arr.length | |
1403 | var valLength = val.length | |
1404 | ||
1405 | if (encoding !== undefined) { | |
1406 | encoding = String(encoding).toLowerCase() | |
1407 | if (encoding === 'ucs2' || encoding === 'ucs-2' || | |
1408 | encoding === 'utf16le' || encoding === 'utf-16le') { | |
1409 | if (arr.length < 2 || val.length < 2) { | |
1410 | return -1 | |
1411 | } | |
1412 | indexSize = 2 | |
1413 | arrLength /= 2 | |
1414 | valLength /= 2 | |
1415 | byteOffset /= 2 | |
1416 | } | |
1417 | } | |
1418 | ||
1419 | function read (buf, i) { | |
1420 | if (indexSize === 1) { | |
1421 | return buf[i] | |
1422 | } else { | |
1423 | return buf.readUInt16BE(i * indexSize) | |
1424 | } | |
1425 | } | |
1426 | ||
1427 | var i | |
1428 | if (dir) { | |
1429 | var foundIndex = -1 | |
1430 | for (i = byteOffset; i < arrLength; i++) { | |
1431 | if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { | |
1432 | if (foundIndex === -1) foundIndex = i | |
1433 | if (i - foundIndex + 1 === valLength) return foundIndex * indexSize | |
1434 | } else { | |
1435 | if (foundIndex !== -1) i -= i - foundIndex | |
1436 | foundIndex = -1 | |
1437 | } | |
1438 | } | |
1439 | } else { | |
1440 | if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength | |
1441 | for (i = byteOffset; i >= 0; i--) { | |
1442 | var found = true | |
1443 | for (var j = 0; j < valLength; j++) { | |
1444 | if (read(arr, i + j) !== read(val, j)) { | |
1445 | found = false | |
1446 | break | |
1447 | } | |
1448 | } | |
1449 | if (found) return i | |
1450 | } | |
1451 | } | |
1452 | ||
1453 | return -1 | |
1454 | } | |
1455 | ||
1456 | Buffer.prototype.includes = function includes (val, byteOffset, encoding) { | |
1457 | return this.indexOf(val, byteOffset, encoding) !== -1 | |
1458 | } | |
1459 | ||
1460 | Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { | |
1461 | return bidirectionalIndexOf(this, val, byteOffset, encoding, true) | |
1462 | } | |
1463 | ||
1464 | Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) { | |
1465 | return bidirectionalIndexOf(this, val, byteOffset, encoding, false) | |
1466 | } | |
1467 | ||
1468 | function hexWrite (buf, string, offset, length) { | |
1469 | offset = Number(offset) || 0 | |
1470 | var remaining = buf.length - offset | |
1471 | if (!length) { | |
1472 | length = remaining | |
1473 | } else { | |
1474 | length = Number(length) | |
1475 | if (length > remaining) { | |
1476 | length = remaining | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | // must be an even number of digits | |
1481 | var strLen = string.length | |
1482 | if (strLen % 2 !== 0) throw new TypeError('Invalid hex string') | |
1483 | ||
1484 | if (length > strLen / 2) { | |
1485 | length = strLen / 2 | |
1486 | } | |
1487 | for (var i = 0; i < length; ++i) { | |
1488 | var parsed = parseInt(string.substr(i * 2, 2), 16) | |
1489 | if (numberIsNaN(parsed)) return i | |
1490 | buf[offset + i] = parsed | |
1491 | } | |
1492 | return i | |
1493 | } | |
1494 | ||
1495 | function utf8Write (buf, string, offset, length) { | |
1496 | return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) | |
1497 | } | |
1498 | ||
1499 | function asciiWrite (buf, string, offset, length) { | |
1500 | return blitBuffer(asciiToBytes(string), buf, offset, length) | |
1501 | } | |
1502 | ||
1503 | function latin1Write (buf, string, offset, length) { | |
1504 | return asciiWrite(buf, string, offset, length) | |
1505 | } | |
1506 | ||
1507 | function base64Write (buf, string, offset, length) { | |
1508 | return blitBuffer(base64ToBytes(string), buf, offset, length) | |
1509 | } | |
1510 | ||
1511 | function ucs2Write (buf, string, offset, length) { | |
1512 | return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) | |
1513 | } | |
1514 | ||
1515 | Buffer.prototype.write = function write (string, offset, length, encoding) { | |
1516 | // Buffer#write(string) | |
1517 | if (offset === undefined) { | |
1518 | encoding = 'utf8' | |
1519 | length = this.length | |
1520 | offset = 0 | |
1521 | // Buffer#write(string, encoding) | |
1522 | } else if (length === undefined && typeof offset === 'string') { | |
1523 | encoding = offset | |
1524 | length = this.length | |
1525 | offset = 0 | |
1526 | // Buffer#write(string, offset[, length][, encoding]) | |
1527 | } else if (isFinite(offset)) { | |
1528 | offset = offset >>> 0 | |
1529 | if (isFinite(length)) { | |
1530 | length = length >>> 0 | |
1531 | if (encoding === undefined) encoding = 'utf8' | |
1532 | } else { | |
1533 | encoding = length | |
1534 | length = undefined | |
1535 | } | |
1536 | } else { | |
1537 | throw new Error( | |
1538 | 'Buffer.write(string, encoding, offset[, length]) is no longer supported' | |
1539 | ) | |
1540 | } | |
1541 | ||
1542 | var remaining = this.length - offset | |
1543 | if (length === undefined || length > remaining) length = remaining | |
1544 | ||
1545 | if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { | |
1546 | throw new RangeError('Attempt to write outside buffer bounds') | |
1547 | } | |
1548 | ||
1549 | if (!encoding) encoding = 'utf8' | |
1550 | ||
1551 | var loweredCase = false | |
1552 | for (;;) { | |
1553 | switch (encoding) { | |
1554 | case 'hex': | |
1555 | return hexWrite(this, string, offset, length) | |
1556 | ||
1557 | case 'utf8': | |
1558 | case 'utf-8': | |
1559 | return utf8Write(this, string, offset, length) | |
1560 | ||
1561 | case 'ascii': | |
1562 | return asciiWrite(this, string, offset, length) | |
1563 | ||
1564 | case 'latin1': | |
1565 | case 'binary': | |
1566 | return latin1Write(this, string, offset, length) | |
1567 | ||
1568 | case 'base64': | |
1569 | // Warning: maxLength not taken into account in base64Write | |
1570 | return base64Write(this, string, offset, length) | |
1571 | ||
1572 | case 'ucs2': | |
1573 | case 'ucs-2': | |
1574 | case 'utf16le': | |
1575 | case 'utf-16le': | |
1576 | return ucs2Write(this, string, offset, length) | |
1577 | ||
1578 | default: | |
1579 | if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) | |
1580 | encoding = ('' + encoding).toLowerCase() | |
1581 | loweredCase = true | |
1582 | } | |
1583 | } | |
1584 | } | |
1585 | ||
1586 | Buffer.prototype.toJSON = function toJSON () { | |
1587 | return { | |
1588 | type: 'Buffer', | |
1589 | data: Array.prototype.slice.call(this._arr || this, 0) | |
1590 | } | |
1591 | } | |
1592 | ||
1593 | function base64Slice (buf, start, end) { | |
1594 | if (start === 0 && end === buf.length) { | |
1595 | return base64.fromByteArray(buf) | |
1596 | } else { | |
1597 | return base64.fromByteArray(buf.slice(start, end)) | |
1598 | } | |
1599 | } | |
1600 | ||
1601 | function utf8Slice (buf, start, end) { | |
1602 | end = Math.min(buf.length, end) | |
1603 | var res = [] | |
1604 | ||
1605 | var i = start | |
1606 | while (i < end) { | |
1607 | var firstByte = buf[i] | |
1608 | var codePoint = null | |
1609 | var bytesPerSequence = (firstByte > 0xEF) ? 4 | |
1610 | : (firstByte > 0xDF) ? 3 | |
1611 | : (firstByte > 0xBF) ? 2 | |
1612 | : 1 | |
1613 | ||
1614 | if (i + bytesPerSequence <= end) { | |
1615 | var secondByte, thirdByte, fourthByte, tempCodePoint | |
1616 | ||
1617 | switch (bytesPerSequence) { | |
1618 | case 1: | |
1619 | if (firstByte < 0x80) { | |
1620 | codePoint = firstByte | |
1621 | } | |
1622 | break | |
1623 | case 2: | |
1624 | secondByte = buf[i + 1] | |
1625 | if ((secondByte & 0xC0) === 0x80) { | |
1626 | tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) | |
1627 | if (tempCodePoint > 0x7F) { | |
1628 | codePoint = tempCodePoint | |
1629 | } | |
1630 | } | |
1631 | break | |
1632 | case 3: | |
1633 | secondByte = buf[i + 1] | |
1634 | thirdByte = buf[i + 2] | |
1635 | if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { | |
1636 | tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) | |
1637 | if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { | |
1638 | codePoint = tempCodePoint | |
1639 | } | |
1640 | } | |
1641 | break | |
1642 | case 4: | |
1643 | secondByte = buf[i + 1] | |
1644 | thirdByte = buf[i + 2] | |
1645 | fourthByte = buf[i + 3] | |
1646 | if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { | |
1647 | tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) | |
1648 | if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { | |
1649 | codePoint = tempCodePoint | |
1650 | } | |
1651 | } | |
1652 | } | |
1653 | } | |
1654 | ||
1655 | if (codePoint === null) { | |
1656 | // we did not generate a valid codePoint so insert a | |
1657 | // replacement char (U+FFFD) and advance only 1 byte | |
1658 | codePoint = 0xFFFD | |
1659 | bytesPerSequence = 1 | |
1660 | } else if (codePoint > 0xFFFF) { | |
1661 | // encode to utf16 (surrogate pair dance) | |
1662 | codePoint -= 0x10000 | |
1663 | res.push(codePoint >>> 10 & 0x3FF | 0xD800) | |
1664 | codePoint = 0xDC00 | codePoint & 0x3FF | |
1665 | } | |
1666 | ||
1667 | res.push(codePoint) | |
1668 | i += bytesPerSequence | |
1669 | } | |
1670 | ||
1671 | return decodeCodePointsArray(res) | |
1672 | } | |
1673 | ||
1674 | // Based on http://stackoverflow.com/a/22747272/680742, the browser with | |
1675 | // the lowest limit is Chrome, with 0x10000 args. | |
1676 | // We go 1 magnitude less, for safety | |
1677 | var MAX_ARGUMENTS_LENGTH = 0x1000 | |
1678 | ||
1679 | function decodeCodePointsArray (codePoints) { | |
1680 | var len = codePoints.length | |
1681 | if (len <= MAX_ARGUMENTS_LENGTH) { | |
1682 | return String.fromCharCode.apply(String, codePoints) // avoid extra slice() | |
1683 | } | |
1684 | ||
1685 | // Decode in chunks to avoid "call stack size exceeded". | |
1686 | var res = '' | |
1687 | var i = 0 | |
1688 | while (i < len) { | |
1689 | res += String.fromCharCode.apply( | |
1690 | String, | |
1691 | codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) | |
1692 | ) | |
1693 | } | |
1694 | return res | |
1695 | } | |
1696 | ||
1697 | function asciiSlice (buf, start, end) { | |
1698 | var ret = '' | |
1699 | end = Math.min(buf.length, end) | |
1700 | ||
1701 | for (var i = start; i < end; ++i) { | |
1702 | ret += String.fromCharCode(buf[i] & 0x7F) | |
1703 | } | |
1704 | return ret | |
1705 | } | |
1706 | ||
1707 | function latin1Slice (buf, start, end) { | |
1708 | var ret = '' | |
1709 | end = Math.min(buf.length, end) | |
1710 | ||
1711 | for (var i = start; i < end; ++i) { | |
1712 | ret += String.fromCharCode(buf[i]) | |
1713 | } | |
1714 | return ret | |
1715 | } | |
1716 | ||
1717 | function hexSlice (buf, start, end) { | |
1718 | var len = buf.length | |
1719 | ||
1720 | if (!start || start < 0) start = 0 | |
1721 | if (!end || end < 0 || end > len) end = len | |
1722 | ||
1723 | var out = '' | |
1724 | for (var i = start; i < end; ++i) { | |
1725 | out += toHex(buf[i]) | |
1726 | } | |
1727 | return out | |
1728 | } | |
1729 | ||
1730 | function utf16leSlice (buf, start, end) { | |
1731 | var bytes = buf.slice(start, end) | |
1732 | var res = '' | |
1733 | for (var i = 0; i < bytes.length; i += 2) { | |
1734 | res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)) | |
1735 | } | |
1736 | return res | |
1737 | } | |
1738 | ||
1739 | Buffer.prototype.slice = function slice (start, end) { | |
1740 | var len = this.length | |
1741 | start = ~~start | |
1742 | end = end === undefined ? len : ~~end | |
1743 | ||
1744 | if (start < 0) { | |
1745 | start += len | |
1746 | if (start < 0) start = 0 | |
1747 | } else if (start > len) { | |
1748 | start = len | |
1749 | } | |
1750 | ||
1751 | if (end < 0) { | |
1752 | end += len | |
1753 | if (end < 0) end = 0 | |
1754 | } else if (end > len) { | |
1755 | end = len | |
1756 | } | |
1757 | ||
1758 | if (end < start) end = start | |
1759 | ||
1760 | var newBuf = this.subarray(start, end) | |
1761 | // Return an augmented `Uint8Array` instance | |
1762 | newBuf.__proto__ = Buffer.prototype | |
1763 | return newBuf | |
1764 | } | |
1765 | ||
1766 | /* | |
1767 | * Need to make sure that buffer isn't trying to write out of bounds. | |
1768 | */ | |
1769 | function checkOffset (offset, ext, length) { | |
1770 | if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') | |
1771 | if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') | |
1772 | } | |
1773 | ||
1774 | Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { | |
1775 | offset = offset >>> 0 | |
1776 | byteLength = byteLength >>> 0 | |
1777 | if (!noAssert) checkOffset(offset, byteLength, this.length) | |
1778 | ||
1779 | var val = this[offset] | |
1780 | var mul = 1 | |
1781 | var i = 0 | |
1782 | while (++i < byteLength && (mul *= 0x100)) { | |
1783 | val += this[offset + i] * mul | |
1784 | } | |
1785 | ||
1786 | return val | |
1787 | } | |
1788 | ||
1789 | Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { | |
1790 | offset = offset >>> 0 | |
1791 | byteLength = byteLength >>> 0 | |
1792 | if (!noAssert) { | |
1793 | checkOffset(offset, byteLength, this.length) | |
1794 | } | |
1795 | ||
1796 | var val = this[offset + --byteLength] | |
1797 | var mul = 1 | |
1798 | while (byteLength > 0 && (mul *= 0x100)) { | |
1799 | val += this[offset + --byteLength] * mul | |
1800 | } | |
1801 | ||
1802 | return val | |
1803 | } | |
1804 | ||
1805 | Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { | |
1806 | offset = offset >>> 0 | |
1807 | if (!noAssert) checkOffset(offset, 1, this.length) | |
1808 | return this[offset] | |
1809 | } | |
1810 | ||
1811 | Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { | |
1812 | offset = offset >>> 0 | |
1813 | if (!noAssert) checkOffset(offset, 2, this.length) | |
1814 | return this[offset] | (this[offset + 1] << 8) | |
1815 | } | |
1816 | ||
1817 | Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { | |
1818 | offset = offset >>> 0 | |
1819 | if (!noAssert) checkOffset(offset, 2, this.length) | |
1820 | return (this[offset] << 8) | this[offset + 1] | |
1821 | } | |
1822 | ||
1823 | Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { | |
1824 | offset = offset >>> 0 | |
1825 | if (!noAssert) checkOffset(offset, 4, this.length) | |
1826 | ||
1827 | return ((this[offset]) | | |
1828 | (this[offset + 1] << 8) | | |
1829 | (this[offset + 2] << 16)) + | |
1830 | (this[offset + 3] * 0x1000000) | |
1831 | } | |
1832 | ||
1833 | Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { | |
1834 | offset = offset >>> 0 | |
1835 | if (!noAssert) checkOffset(offset, 4, this.length) | |
1836 | ||
1837 | return (this[offset] * 0x1000000) + | |
1838 | ((this[offset + 1] << 16) | | |
1839 | (this[offset + 2] << 8) | | |
1840 | this[offset + 3]) | |
1841 | } | |
1842 | ||
1843 | Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { | |
1844 | offset = offset >>> 0 | |
1845 | byteLength = byteLength >>> 0 | |
1846 | if (!noAssert) checkOffset(offset, byteLength, this.length) | |
1847 | ||
1848 | var val = this[offset] | |
1849 | var mul = 1 | |
1850 | var i = 0 | |
1851 | while (++i < byteLength && (mul *= 0x100)) { | |
1852 | val += this[offset + i] * mul | |
1853 | } | |
1854 | mul *= 0x80 | |
1855 | ||
1856 | if (val >= mul) val -= Math.pow(2, 8 * byteLength) | |
1857 | ||
1858 | return val | |
1859 | } | |
1860 | ||
1861 | Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { | |
1862 | offset = offset >>> 0 | |
1863 | byteLength = byteLength >>> 0 | |
1864 | if (!noAssert) checkOffset(offset, byteLength, this.length) | |
1865 | ||
1866 | var i = byteLength | |
1867 | var mul = 1 | |
1868 | var val = this[offset + --i] | |
1869 | while (i > 0 && (mul *= 0x100)) { | |
1870 | val += this[offset + --i] * mul | |
1871 | } | |
1872 | mul *= 0x80 | |
1873 | ||
1874 | if (val >= mul) val -= Math.pow(2, 8 * byteLength) | |
1875 | ||
1876 | return val | |
1877 | } | |
1878 | ||
1879 | Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { | |
1880 | offset = offset >>> 0 | |
1881 | if (!noAssert) checkOffset(offset, 1, this.length) | |
1882 | if (!(this[offset] & 0x80)) return (this[offset]) | |
1883 | return ((0xff - this[offset] + 1) * -1) | |
1884 | } | |
1885 | ||
1886 | Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { | |
1887 | offset = offset >>> 0 | |
1888 | if (!noAssert) checkOffset(offset, 2, this.length) | |
1889 | var val = this[offset] | (this[offset + 1] << 8) | |
1890 | return (val & 0x8000) ? val | 0xFFFF0000 : val | |
1891 | } | |
1892 | ||
1893 | Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { | |
1894 | offset = offset >>> 0 | |
1895 | if (!noAssert) checkOffset(offset, 2, this.length) | |
1896 | var val = this[offset + 1] | (this[offset] << 8) | |
1897 | return (val & 0x8000) ? val | 0xFFFF0000 : val | |
1898 | } | |
1899 | ||
1900 | Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { | |
1901 | offset = offset >>> 0 | |
1902 | if (!noAssert) checkOffset(offset, 4, this.length) | |
1903 | ||
1904 | return (this[offset]) | | |
1905 | (this[offset + 1] << 8) | | |
1906 | (this[offset + 2] << 16) | | |
1907 | (this[offset + 3] << 24) | |
1908 | } | |
1909 | ||
1910 | Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { | |
1911 | offset = offset >>> 0 | |
1912 | if (!noAssert) checkOffset(offset, 4, this.length) | |
1913 | ||
1914 | return (this[offset] << 24) | | |
1915 | (this[offset + 1] << 16) | | |
1916 | (this[offset + 2] << 8) | | |
1917 | (this[offset + 3]) | |
1918 | } | |
1919 | ||
1920 | Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { | |
1921 | offset = offset >>> 0 | |
1922 | if (!noAssert) checkOffset(offset, 4, this.length) | |
1923 | return ieee754.read(this, offset, true, 23, 4) | |
1924 | } | |
1925 | ||
1926 | Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { | |
1927 | offset = offset >>> 0 | |
1928 | if (!noAssert) checkOffset(offset, 4, this.length) | |
1929 | return ieee754.read(this, offset, false, 23, 4) | |
1930 | } | |
1931 | ||
1932 | Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { | |
1933 | offset = offset >>> 0 | |
1934 | if (!noAssert) checkOffset(offset, 8, this.length) | |
1935 | return ieee754.read(this, offset, true, 52, 8) | |
1936 | } | |
1937 | ||
1938 | Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { | |
1939 | offset = offset >>> 0 | |
1940 | if (!noAssert) checkOffset(offset, 8, this.length) | |
1941 | return ieee754.read(this, offset, false, 52, 8) | |
1942 | } | |
1943 | ||
1944 | function checkInt (buf, value, offset, ext, max, min) { | |
1945 | if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') | |
1946 | if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') | |
1947 | if (offset + ext > buf.length) throw new RangeError('Index out of range') | |
1948 | } | |
1949 | ||
1950 | Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { | |
1951 | value = +value | |
1952 | offset = offset >>> 0 | |
1953 | byteLength = byteLength >>> 0 | |
1954 | if (!noAssert) { | |
1955 | var maxBytes = Math.pow(2, 8 * byteLength) - 1 | |
1956 | checkInt(this, value, offset, byteLength, maxBytes, 0) | |
1957 | } | |
1958 | ||
1959 | var mul = 1 | |
1960 | var i = 0 | |
1961 | this[offset] = value & 0xFF | |
1962 | while (++i < byteLength && (mul *= 0x100)) { | |
1963 | this[offset + i] = (value / mul) & 0xFF | |
1964 | } | |
1965 | ||
1966 | return offset + byteLength | |
1967 | } | |
1968 | ||
1969 | Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { | |
1970 | value = +value | |
1971 | offset = offset >>> 0 | |
1972 | byteLength = byteLength >>> 0 | |
1973 | if (!noAssert) { | |
1974 | var maxBytes = Math.pow(2, 8 * byteLength) - 1 | |
1975 | checkInt(this, value, offset, byteLength, maxBytes, 0) | |
1976 | } | |
1977 | ||
1978 | var i = byteLength - 1 | |
1979 | var mul = 1 | |
1980 | this[offset + i] = value & 0xFF | |
1981 | while (--i >= 0 && (mul *= 0x100)) { | |
1982 | this[offset + i] = (value / mul) & 0xFF | |
1983 | } | |
1984 | ||
1985 | return offset + byteLength | |
1986 | } | |
1987 | ||
1988 | Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { | |
1989 | value = +value | |
1990 | offset = offset >>> 0 | |
1991 | if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) | |
1992 | this[offset] = (value & 0xff) | |
1993 | return offset + 1 | |
1994 | } | |
1995 | ||
1996 | Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { | |
1997 | value = +value | |
1998 | offset = offset >>> 0 | |
1999 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) | |
2000 | this[offset] = (value & 0xff) | |
2001 | this[offset + 1] = (value >>> 8) | |
2002 | return offset + 2 | |
2003 | } | |
2004 | ||
2005 | Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { | |
2006 | value = +value | |
2007 | offset = offset >>> 0 | |
2008 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) | |
2009 | this[offset] = (value >>> 8) | |
2010 | this[offset + 1] = (value & 0xff) | |
2011 | return offset + 2 | |
2012 | } | |
2013 | ||
2014 | Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { | |
2015 | value = +value | |
2016 | offset = offset >>> 0 | |
2017 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) | |
2018 | this[offset + 3] = (value >>> 24) | |
2019 | this[offset + 2] = (value >>> 16) | |
2020 | this[offset + 1] = (value >>> 8) | |
2021 | this[offset] = (value & 0xff) | |
2022 | return offset + 4 | |
2023 | } | |
2024 | ||
2025 | Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { | |
2026 | value = +value | |
2027 | offset = offset >>> 0 | |
2028 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) | |
2029 | this[offset] = (value >>> 24) | |
2030 | this[offset + 1] = (value >>> 16) | |
2031 | this[offset + 2] = (value >>> 8) | |
2032 | this[offset + 3] = (value & 0xff) | |
2033 | return offset + 4 | |
2034 | } | |
2035 | ||
2036 | Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { | |
2037 | value = +value | |
2038 | offset = offset >>> 0 | |
2039 | if (!noAssert) { | |
2040 | var limit = Math.pow(2, (8 * byteLength) - 1) | |
2041 | ||
2042 | checkInt(this, value, offset, byteLength, limit - 1, -limit) | |
2043 | } | |
2044 | ||
2045 | var i = 0 | |
2046 | var mul = 1 | |
2047 | var sub = 0 | |
2048 | this[offset] = value & 0xFF | |
2049 | while (++i < byteLength && (mul *= 0x100)) { | |
2050 | if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { | |
2051 | sub = 1 | |
2052 | } | |
2053 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF | |
2054 | } | |
2055 | ||
2056 | return offset + byteLength | |
2057 | } | |
2058 | ||
2059 | Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { | |
2060 | value = +value | |
2061 | offset = offset >>> 0 | |
2062 | if (!noAssert) { | |
2063 | var limit = Math.pow(2, (8 * byteLength) - 1) | |
2064 | ||
2065 | checkInt(this, value, offset, byteLength, limit - 1, -limit) | |
2066 | } | |
2067 | ||
2068 | var i = byteLength - 1 | |
2069 | var mul = 1 | |
2070 | var sub = 0 | |
2071 | this[offset + i] = value & 0xFF | |
2072 | while (--i >= 0 && (mul *= 0x100)) { | |
2073 | if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { | |
2074 | sub = 1 | |
2075 | } | |
2076 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF | |
2077 | } | |
2078 | ||
2079 | return offset + byteLength | |
2080 | } | |
2081 | ||
2082 | Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { | |
2083 | value = +value | |
2084 | offset = offset >>> 0 | |
2085 | if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) | |
2086 | if (value < 0) value = 0xff + value + 1 | |
2087 | this[offset] = (value & 0xff) | |
2088 | return offset + 1 | |
2089 | } | |
2090 | ||
2091 | Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { | |
2092 | value = +value | |
2093 | offset = offset >>> 0 | |
2094 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) | |
2095 | this[offset] = (value & 0xff) | |
2096 | this[offset + 1] = (value >>> 8) | |
2097 | return offset + 2 | |
2098 | } | |
2099 | ||
2100 | Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { | |
2101 | value = +value | |
2102 | offset = offset >>> 0 | |
2103 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) | |
2104 | this[offset] = (value >>> 8) | |
2105 | this[offset + 1] = (value & 0xff) | |
2106 | return offset + 2 | |
2107 | } | |
2108 | ||
2109 | Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { | |
2110 | value = +value | |
2111 | offset = offset >>> 0 | |
2112 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) | |
2113 | this[offset] = (value & 0xff) | |
2114 | this[offset + 1] = (value >>> 8) | |
2115 | this[offset + 2] = (value >>> 16) | |
2116 | this[offset + 3] = (value >>> 24) | |
2117 | return offset + 4 | |
2118 | } | |
2119 | ||
2120 | Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { | |
2121 | value = +value | |
2122 | offset = offset >>> 0 | |
2123 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) | |
2124 | if (value < 0) value = 0xffffffff + value + 1 | |
2125 | this[offset] = (value >>> 24) | |
2126 | this[offset + 1] = (value >>> 16) | |
2127 | this[offset + 2] = (value >>> 8) | |
2128 | this[offset + 3] = (value & 0xff) | |
2129 | return offset + 4 | |
2130 | } | |
2131 | ||
2132 | function checkIEEE754 (buf, value, offset, ext, max, min) { | |
2133 | if (offset + ext > buf.length) throw new RangeError('Index out of range') | |
2134 | if (offset < 0) throw new RangeError('Index out of range') | |
2135 | } | |
2136 | ||
2137 | function writeFloat (buf, value, offset, littleEndian, noAssert) { | |
2138 | value = +value | |
2139 | offset = offset >>> 0 | |
2140 | if (!noAssert) { | |
2141 | checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) | |
2142 | } | |
2143 | ieee754.write(buf, value, offset, littleEndian, 23, 4) | |
2144 | return offset + 4 | |
2145 | } | |
2146 | ||
2147 | Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { | |
2148 | return writeFloat(this, value, offset, true, noAssert) | |
2149 | } | |
2150 | ||
2151 | Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { | |
2152 | return writeFloat(this, value, offset, false, noAssert) | |
2153 | } | |
2154 | ||
2155 | function writeDouble (buf, value, offset, littleEndian, noAssert) { | |
2156 | value = +value | |
2157 | offset = offset >>> 0 | |
2158 | if (!noAssert) { | |
2159 | checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) | |
2160 | } | |
2161 | ieee754.write(buf, value, offset, littleEndian, 52, 8) | |
2162 | return offset + 8 | |
2163 | } | |
2164 | ||
2165 | Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { | |
2166 | return writeDouble(this, value, offset, true, noAssert) | |
2167 | } | |
2168 | ||
2169 | Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { | |
2170 | return writeDouble(this, value, offset, false, noAssert) | |
2171 | } | |
2172 | ||
2173 | // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) | |
2174 | Buffer.prototype.copy = function copy (target, targetStart, start, end) { | |
2175 | if (!start) start = 0 | |
2176 | if (!end && end !== 0) end = this.length | |
2177 | if (targetStart >= target.length) targetStart = target.length | |
2178 | if (!targetStart) targetStart = 0 | |
2179 | if (end > 0 && end < start) end = start | |
2180 | ||
2181 | // Copy 0 bytes; we're done | |
2182 | if (end === start) return 0 | |
2183 | if (target.length === 0 || this.length === 0) return 0 | |
2184 | ||
2185 | // Fatal error conditions | |
2186 | if (targetStart < 0) { | |
2187 | throw new RangeError('targetStart out of bounds') | |
2188 | } | |
2189 | if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds') | |
2190 | if (end < 0) throw new RangeError('sourceEnd out of bounds') | |
2191 | ||
2192 | // Are we oob? | |
2193 | if (end > this.length) end = this.length | |
2194 | if (target.length - targetStart < end - start) { | |
2195 | end = target.length - targetStart + start | |
2196 | } | |
2197 | ||
2198 | var len = end - start | |
2199 | var i | |
2200 | ||
2201 | if (this === target && start < targetStart && targetStart < end) { | |
2202 | // descending copy from end | |
2203 | for (i = len - 1; i >= 0; --i) { | |
2204 | target[i + targetStart] = this[i + start] | |
2205 | } | |
2206 | } else if (len < 1000) { | |
2207 | // ascending copy from start | |
2208 | for (i = 0; i < len; ++i) { | |
2209 | target[i + targetStart] = this[i + start] | |
2210 | } | |
2211 | } else { | |
2212 | Uint8Array.prototype.set.call( | |
2213 | target, | |
2214 | this.subarray(start, start + len), | |
2215 | targetStart | |
2216 | ) | |
2217 | } | |
2218 | ||
2219 | return len | |
2220 | } | |
2221 | ||
2222 | // Usage: | |
2223 | // buffer.fill(number[, offset[, end]]) | |
2224 | // buffer.fill(buffer[, offset[, end]]) | |
2225 | // buffer.fill(string[, offset[, end]][, encoding]) | |
2226 | Buffer.prototype.fill = function fill (val, start, end, encoding) { | |
2227 | // Handle string cases: | |
2228 | if (typeof val === 'string') { | |
2229 | if (typeof start === 'string') { | |
2230 | encoding = start | |
2231 | start = 0 | |
2232 | end = this.length | |
2233 | } else if (typeof end === 'string') { | |
2234 | encoding = end | |
2235 | end = this.length | |
2236 | } | |
2237 | if (val.length === 1) { | |
2238 | var code = val.charCodeAt(0) | |
2239 | if (code < 256) { | |
2240 | val = code | |
2241 | } | |
2242 | } | |
2243 | if (encoding !== undefined && typeof encoding !== 'string') { | |
2244 | throw new TypeError('encoding must be a string') | |
2245 | } | |
2246 | if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { | |
2247 | throw new TypeError('Unknown encoding: ' + encoding) | |
2248 | } | |
2249 | } else if (typeof val === 'number') { | |
2250 | val = val & 255 | |
2251 | } | |
2252 | ||
2253 | // Invalid ranges are not set to a default, so can range check early. | |
2254 | if (start < 0 || this.length < start || this.length < end) { | |
2255 | throw new RangeError('Out of range index') | |
2256 | } | |
2257 | ||
2258 | if (end <= start) { | |
2259 | return this | |
2260 | } | |
2261 | ||
2262 | start = start >>> 0 | |
2263 | end = end === undefined ? this.length : end >>> 0 | |
2264 | ||
2265 | if (!val) val = 0 | |
2266 | ||
2267 | var i | |
2268 | if (typeof val === 'number') { | |
2269 | for (i = start; i < end; ++i) { | |
2270 | this[i] = val | |
2271 | } | |
2272 | } else { | |
2273 | var bytes = Buffer.isBuffer(val) | |
2274 | ? val | |
2275 | : new Buffer(val, encoding) | |
2276 | var len = bytes.length | |
2277 | for (i = 0; i < end - start; ++i) { | |
2278 | this[i + start] = bytes[i % len] | |
2279 | } | |
2280 | } | |
2281 | ||
2282 | return this | |
2283 | } | |
2284 | ||
2285 | // HELPER FUNCTIONS | |
2286 | // ================ | |
2287 | ||
2288 | var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g | |
2289 | ||
2290 | function base64clean (str) { | |
2291 | // Node strips out invalid characters like \n and \t from the string, base64-js does not | |
2292 | str = str.trim().replace(INVALID_BASE64_RE, '') | |
2293 | // Node converts strings with length < 2 to '' | |
2294 | if (str.length < 2) return '' | |
2295 | // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not | |
2296 | while (str.length % 4 !== 0) { | |
2297 | str = str + '=' | |
2298 | } | |
2299 | return str | |
2300 | } | |
2301 | ||
2302 | function toHex (n) { | |
2303 | if (n < 16) return '0' + n.toString(16) | |
2304 | return n.toString(16) | |
2305 | } | |
2306 | ||
2307 | function utf8ToBytes (string, units) { | |
2308 | units = units || Infinity | |
2309 | var codePoint | |
2310 | var length = string.length | |
2311 | var leadSurrogate = null | |
2312 | var bytes = [] | |
2313 | ||
2314 | for (var i = 0; i < length; ++i) { | |
2315 | codePoint = string.charCodeAt(i) | |
2316 | ||
2317 | // is surrogate component | |
2318 | if (codePoint > 0xD7FF && codePoint < 0xE000) { | |
2319 | // last char was a lead | |
2320 | if (!leadSurrogate) { | |
2321 | // no lead yet | |
2322 | if (codePoint > 0xDBFF) { | |
2323 | // unexpected trail | |
2324 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) | |
2325 | continue | |
2326 | } else if (i + 1 === length) { | |
2327 | // unpaired lead | |
2328 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) | |
2329 | continue | |
2330 | } | |
2331 | ||
2332 | // valid lead | |
2333 | leadSurrogate = codePoint | |
2334 | ||
2335 | continue | |
2336 | } | |
2337 | ||
2338 | // 2 leads in a row | |
2339 | if (codePoint < 0xDC00) { | |
2340 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) | |
2341 | leadSurrogate = codePoint | |
2342 | continue | |
2343 | } | |
2344 | ||
2345 | // valid surrogate pair | |
2346 | codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 | |
2347 | } else if (leadSurrogate) { | |
2348 | // valid bmp char, but last char was a lead | |
2349 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) | |
2350 | } | |
2351 | ||
2352 | leadSurrogate = null | |
2353 | ||
2354 | // encode utf8 | |
2355 | if (codePoint < 0x80) { | |
2356 | if ((units -= 1) < 0) break | |
2357 | bytes.push(codePoint) | |
2358 | } else if (codePoint < 0x800) { | |
2359 | if ((units -= 2) < 0) break | |
2360 | bytes.push( | |
2361 | codePoint >> 0x6 | 0xC0, | |
2362 | codePoint & 0x3F | 0x80 | |
2363 | ) | |
2364 | } else if (codePoint < 0x10000) { | |
2365 | if ((units -= 3) < 0) break | |
2366 | bytes.push( | |
2367 | codePoint >> 0xC | 0xE0, | |
2368 | codePoint >> 0x6 & 0x3F | 0x80, | |
2369 | codePoint & 0x3F | 0x80 | |
2370 | ) | |
2371 | } else if (codePoint < 0x110000) { | |
2372 | if ((units -= 4) < 0) break | |
2373 | bytes.push( | |
2374 | codePoint >> 0x12 | 0xF0, | |
2375 | codePoint >> 0xC & 0x3F | 0x80, | |
2376 | codePoint >> 0x6 & 0x3F | 0x80, | |
2377 | codePoint & 0x3F | 0x80 | |
2378 | ) | |
2379 | } else { | |
2380 | throw new Error('Invalid code point') | |
2381 | } | |
2382 | } | |
2383 | ||
2384 | return bytes | |
2385 | } | |
2386 | ||
2387 | function asciiToBytes (str) { | |
2388 | var byteArray = [] | |
2389 | for (var i = 0; i < str.length; ++i) { | |
2390 | // Node's code seems to be doing this and not & 0x7F.. | |
2391 | byteArray.push(str.charCodeAt(i) & 0xFF) | |
2392 | } | |
2393 | return byteArray | |
2394 | } | |
2395 | ||
2396 | function utf16leToBytes (str, units) { | |
2397 | var c, hi, lo | |
2398 | var byteArray = [] | |
2399 | for (var i = 0; i < str.length; ++i) { | |
2400 | if ((units -= 2) < 0) break | |
2401 | ||
2402 | c = str.charCodeAt(i) | |
2403 | hi = c >> 8 | |
2404 | lo = c % 256 | |
2405 | byteArray.push(lo) | |
2406 | byteArray.push(hi) | |
2407 | } | |
2408 | ||
2409 | return byteArray | |
2410 | } | |
2411 | ||
2412 | function base64ToBytes (str) { | |
2413 | return base64.toByteArray(base64clean(str)) | |
2414 | } | |
2415 | ||
2416 | function blitBuffer (src, dst, offset, length) { | |
2417 | for (var i = 0; i < length; ++i) { | |
2418 | if ((i + offset >= dst.length) || (i >= src.length)) break | |
2419 | dst[i + offset] = src[i] | |
2420 | } | |
2421 | return i | |
2422 | } | |
2423 | ||
2424 | // Node 0.10 supports `ArrayBuffer` but lacks `ArrayBuffer.isView` | |
2425 | function isArrayBufferView (obj) { | |
2426 | return (typeof ArrayBuffer.isView === 'function') && ArrayBuffer.isView(obj) | |
2427 | } | |
2428 | ||
2429 | function numberIsNaN (obj) { | |
2430 | return obj !== obj // eslint-disable-line no-self-compare | |
2431 | } | |
2432 | ||
9f59e99b | 2433 | },{"base64-js":2,"ieee754":8}],6:[function(require,module,exports){ |
a0091a40 IC |
2434 | (function (Buffer){ |
2435 | // Copyright Joyent, Inc. and other Node contributors. | |
2436 | // | |
2437 | // Permission is hereby granted, free of charge, to any person obtaining a | |
2438 | // copy of this software and associated documentation files (the | |
2439 | // "Software"), to deal in the Software without restriction, including | |
2440 | // without limitation the rights to use, copy, modify, merge, publish, | |
2441 | // distribute, sublicense, and/or sell copies of the Software, and to permit | |
2442 | // persons to whom the Software is furnished to do so, subject to the | |
2443 | // following conditions: | |
2444 | // | |
2445 | // The above copyright notice and this permission notice shall be included | |
2446 | // in all copies or substantial portions of the Software. | |
2447 | // | |
2448 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
2449 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
2450 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
2451 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
2452 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
2453 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
2454 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
2455 | ||
2456 | // NOTE: These type checking functions intentionally don't use `instanceof` | |
2457 | // because it is fragile and can be easily faked with `Object.create()`. | |
2458 | ||
2459 | function isArray(arg) { | |
2460 | if (Array.isArray) { | |
2461 | return Array.isArray(arg); | |
2462 | } | |
2463 | return objectToString(arg) === '[object Array]'; | |
2464 | } | |
2465 | exports.isArray = isArray; | |
2466 | ||
2467 | function isBoolean(arg) { | |
2468 | return typeof arg === 'boolean'; | |
2469 | } | |
2470 | exports.isBoolean = isBoolean; | |
2471 | ||
2472 | function isNull(arg) { | |
2473 | return arg === null; | |
2474 | } | |
2475 | exports.isNull = isNull; | |
2476 | ||
2477 | function isNullOrUndefined(arg) { | |
2478 | return arg == null; | |
2479 | } | |
2480 | exports.isNullOrUndefined = isNullOrUndefined; | |
2481 | ||
2482 | function isNumber(arg) { | |
2483 | return typeof arg === 'number'; | |
2484 | } | |
2485 | exports.isNumber = isNumber; | |
2486 | ||
2487 | function isString(arg) { | |
2488 | return typeof arg === 'string'; | |
2489 | } | |
2490 | exports.isString = isString; | |
2491 | ||
2492 | function isSymbol(arg) { | |
2493 | return typeof arg === 'symbol'; | |
2494 | } | |
2495 | exports.isSymbol = isSymbol; | |
2496 | ||
2497 | function isUndefined(arg) { | |
2498 | return arg === void 0; | |
2499 | } | |
2500 | exports.isUndefined = isUndefined; | |
2501 | ||
2502 | function isRegExp(re) { | |
2503 | return objectToString(re) === '[object RegExp]'; | |
2504 | } | |
2505 | exports.isRegExp = isRegExp; | |
2506 | ||
2507 | function isObject(arg) { | |
2508 | return typeof arg === 'object' && arg !== null; | |
2509 | } | |
2510 | exports.isObject = isObject; | |
2511 | ||
2512 | function isDate(d) { | |
2513 | return objectToString(d) === '[object Date]'; | |
2514 | } | |
2515 | exports.isDate = isDate; | |
2516 | ||
2517 | function isError(e) { | |
2518 | return (objectToString(e) === '[object Error]' || e instanceof Error); | |
2519 | } | |
2520 | exports.isError = isError; | |
2521 | ||
2522 | function isFunction(arg) { | |
2523 | return typeof arg === 'function'; | |
2524 | } | |
2525 | exports.isFunction = isFunction; | |
2526 | ||
2527 | function isPrimitive(arg) { | |
2528 | return arg === null || | |
2529 | typeof arg === 'boolean' || | |
2530 | typeof arg === 'number' || | |
2531 | typeof arg === 'string' || | |
2532 | typeof arg === 'symbol' || // ES6 symbol | |
2533 | typeof arg === 'undefined'; | |
2534 | } | |
2535 | exports.isPrimitive = isPrimitive; | |
2536 | ||
2537 | exports.isBuffer = Buffer.isBuffer; | |
2538 | ||
2539 | function objectToString(o) { | |
2540 | return Object.prototype.toString.call(o); | |
2541 | } | |
2542 | ||
2543 | }).call(this,{"isBuffer":require("../../is-buffer/index.js")}) | |
9f59e99b | 2544 | },{"../../is-buffer/index.js":10}],7:[function(require,module,exports){ |
a0091a40 IC |
2545 | // Copyright Joyent, Inc. and other Node contributors. |
2546 | // | |
2547 | // Permission is hereby granted, free of charge, to any person obtaining a | |
2548 | // copy of this software and associated documentation files (the | |
2549 | // "Software"), to deal in the Software without restriction, including | |
2550 | // without limitation the rights to use, copy, modify, merge, publish, | |
2551 | // distribute, sublicense, and/or sell copies of the Software, and to permit | |
2552 | // persons to whom the Software is furnished to do so, subject to the | |
2553 | // following conditions: | |
2554 | // | |
2555 | // The above copyright notice and this permission notice shall be included | |
2556 | // in all copies or substantial portions of the Software. | |
2557 | // | |
2558 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
2559 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
2560 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
2561 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
2562 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
2563 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
2564 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
2565 | ||
2566 | function EventEmitter() { | |
2567 | this._events = this._events || {}; | |
2568 | this._maxListeners = this._maxListeners || undefined; | |
2569 | } | |
2570 | module.exports = EventEmitter; | |
2571 | ||
2572 | // Backwards-compat with node 0.10.x | |
2573 | EventEmitter.EventEmitter = EventEmitter; | |
2574 | ||
2575 | EventEmitter.prototype._events = undefined; | |
2576 | EventEmitter.prototype._maxListeners = undefined; | |
2577 | ||
2578 | // By default EventEmitters will print a warning if more than 10 listeners are | |
2579 | // added to it. This is a useful default which helps finding memory leaks. | |
2580 | EventEmitter.defaultMaxListeners = 10; | |
2581 | ||
2582 | // Obviously not all Emitters should be limited to 10. This function allows | |
2583 | // that to be increased. Set to zero for unlimited. | |
2584 | EventEmitter.prototype.setMaxListeners = function(n) { | |
2585 | if (!isNumber(n) || n < 0 || isNaN(n)) | |
2586 | throw TypeError('n must be a positive number'); | |
2587 | this._maxListeners = n; | |
2588 | return this; | |
2589 | }; | |
2590 | ||
2591 | EventEmitter.prototype.emit = function(type) { | |
2592 | var er, handler, len, args, i, listeners; | |
2593 | ||
2594 | if (!this._events) | |
2595 | this._events = {}; | |
2596 | ||
2597 | // If there is no 'error' event listener then throw. | |
2598 | if (type === 'error') { | |
2599 | if (!this._events.error || | |
2600 | (isObject(this._events.error) && !this._events.error.length)) { | |
2601 | er = arguments[1]; | |
2602 | if (er instanceof Error) { | |
2603 | throw er; // Unhandled 'error' event | |
2604 | } else { | |
2605 | // At least give some kind of context to the user | |
2606 | var err = new Error('Uncaught, unspecified "error" event. (' + er + ')'); | |
2607 | err.context = er; | |
2608 | throw err; | |
2609 | } | |
2610 | } | |
2611 | } | |
2612 | ||
2613 | handler = this._events[type]; | |
2614 | ||
2615 | if (isUndefined(handler)) | |
2616 | return false; | |
2617 | ||
2618 | if (isFunction(handler)) { | |
2619 | switch (arguments.length) { | |
2620 | // fast cases | |
2621 | case 1: | |
2622 | handler.call(this); | |
2623 | break; | |
2624 | case 2: | |
2625 | handler.call(this, arguments[1]); | |
2626 | break; | |
2627 | case 3: | |
2628 | handler.call(this, arguments[1], arguments[2]); | |
2629 | break; | |
2630 | // slower | |
2631 | default: | |
2632 | args = Array.prototype.slice.call(arguments, 1); | |
2633 | handler.apply(this, args); | |
2634 | } | |
2635 | } else if (isObject(handler)) { | |
2636 | args = Array.prototype.slice.call(arguments, 1); | |
2637 | listeners = handler.slice(); | |
2638 | len = listeners.length; | |
2639 | for (i = 0; i < len; i++) | |
2640 | listeners[i].apply(this, args); | |
2641 | } | |
2642 | ||
2643 | return true; | |
2644 | }; | |
2645 | ||
2646 | EventEmitter.prototype.addListener = function(type, listener) { | |
2647 | var m; | |
2648 | ||
2649 | if (!isFunction(listener)) | |
2650 | throw TypeError('listener must be a function'); | |
2651 | ||
2652 | if (!this._events) | |
2653 | this._events = {}; | |
2654 | ||
2655 | // To avoid recursion in the case that type === "newListener"! Before | |
2656 | // adding it to the listeners, first emit "newListener". | |
2657 | if (this._events.newListener) | |
2658 | this.emit('newListener', type, | |
2659 | isFunction(listener.listener) ? | |
2660 | listener.listener : listener); | |
2661 | ||
2662 | if (!this._events[type]) | |
2663 | // Optimize the case of one listener. Don't need the extra array object. | |
2664 | this._events[type] = listener; | |
2665 | else if (isObject(this._events[type])) | |
2666 | // If we've already got an array, just append. | |
2667 | this._events[type].push(listener); | |
2668 | else | |
2669 | // Adding the second element, need to change to array. | |
2670 | this._events[type] = [this._events[type], listener]; | |
2671 | ||
2672 | // Check for listener leak | |
2673 | if (isObject(this._events[type]) && !this._events[type].warned) { | |
2674 | if (!isUndefined(this._maxListeners)) { | |
2675 | m = this._maxListeners; | |
2676 | } else { | |
2677 | m = EventEmitter.defaultMaxListeners; | |
2678 | } | |
2679 | ||
2680 | if (m && m > 0 && this._events[type].length > m) { | |
2681 | this._events[type].warned = true; | |
2682 | console.error('(node) warning: possible EventEmitter memory ' + | |
2683 | 'leak detected. %d listeners added. ' + | |
2684 | 'Use emitter.setMaxListeners() to increase limit.', | |
2685 | this._events[type].length); | |
2686 | if (typeof console.trace === 'function') { | |
2687 | // not supported in IE 10 | |
2688 | console.trace(); | |
2689 | } | |
2690 | } | |
2691 | } | |
2692 | ||
2693 | return this; | |
2694 | }; | |
2695 | ||
2696 | EventEmitter.prototype.on = EventEmitter.prototype.addListener; | |
2697 | ||
2698 | EventEmitter.prototype.once = function(type, listener) { | |
2699 | if (!isFunction(listener)) | |
2700 | throw TypeError('listener must be a function'); | |
2701 | ||
2702 | var fired = false; | |
2703 | ||
2704 | function g() { | |
2705 | this.removeListener(type, g); | |
2706 | ||
2707 | if (!fired) { | |
2708 | fired = true; | |
2709 | listener.apply(this, arguments); | |
2710 | } | |
2711 | } | |
2712 | ||
2713 | g.listener = listener; | |
2714 | this.on(type, g); | |
2715 | ||
2716 | return this; | |
2717 | }; | |
2718 | ||
2719 | // emits a 'removeListener' event iff the listener was removed | |
2720 | EventEmitter.prototype.removeListener = function(type, listener) { | |
2721 | var list, position, length, i; | |
2722 | ||
2723 | if (!isFunction(listener)) | |
2724 | throw TypeError('listener must be a function'); | |
2725 | ||
2726 | if (!this._events || !this._events[type]) | |
2727 | return this; | |
2728 | ||
2729 | list = this._events[type]; | |
2730 | length = list.length; | |
2731 | position = -1; | |
2732 | ||
2733 | if (list === listener || | |
2734 | (isFunction(list.listener) && list.listener === listener)) { | |
2735 | delete this._events[type]; | |
2736 | if (this._events.removeListener) | |
2737 | this.emit('removeListener', type, listener); | |
2738 | ||
2739 | } else if (isObject(list)) { | |
2740 | for (i = length; i-- > 0;) { | |
2741 | if (list[i] === listener || | |
2742 | (list[i].listener && list[i].listener === listener)) { | |
2743 | position = i; | |
2744 | break; | |
2745 | } | |
2746 | } | |
2747 | ||
2748 | if (position < 0) | |
2749 | return this; | |
2750 | ||
2751 | if (list.length === 1) { | |
2752 | list.length = 0; | |
2753 | delete this._events[type]; | |
2754 | } else { | |
2755 | list.splice(position, 1); | |
2756 | } | |
2757 | ||
2758 | if (this._events.removeListener) | |
2759 | this.emit('removeListener', type, listener); | |
2760 | } | |
2761 | ||
2762 | return this; | |
2763 | }; | |
2764 | ||
2765 | EventEmitter.prototype.removeAllListeners = function(type) { | |
2766 | var key, listeners; | |
2767 | ||
2768 | if (!this._events) | |
2769 | return this; | |
2770 | ||
2771 | // not listening for removeListener, no need to emit | |
2772 | if (!this._events.removeListener) { | |
2773 | if (arguments.length === 0) | |
2774 | this._events = {}; | |
2775 | else if (this._events[type]) | |
2776 | delete this._events[type]; | |
2777 | return this; | |
2778 | } | |
2779 | ||
2780 | // emit removeListener for all listeners on all events | |
2781 | if (arguments.length === 0) { | |
2782 | for (key in this._events) { | |
2783 | if (key === 'removeListener') continue; | |
2784 | this.removeAllListeners(key); | |
2785 | } | |
2786 | this.removeAllListeners('removeListener'); | |
2787 | this._events = {}; | |
2788 | return this; | |
2789 | } | |
2790 | ||
2791 | listeners = this._events[type]; | |
2792 | ||
2793 | if (isFunction(listeners)) { | |
2794 | this.removeListener(type, listeners); | |
2795 | } else if (listeners) { | |
2796 | // LIFO order | |
2797 | while (listeners.length) | |
2798 | this.removeListener(type, listeners[listeners.length - 1]); | |
2799 | } | |
2800 | delete this._events[type]; | |
2801 | ||
2802 | return this; | |
2803 | }; | |
2804 | ||
2805 | EventEmitter.prototype.listeners = function(type) { | |
2806 | var ret; | |
2807 | if (!this._events || !this._events[type]) | |
2808 | ret = []; | |
2809 | else if (isFunction(this._events[type])) | |
2810 | ret = [this._events[type]]; | |
2811 | else | |
2812 | ret = this._events[type].slice(); | |
2813 | return ret; | |
2814 | }; | |
2815 | ||
2816 | EventEmitter.prototype.listenerCount = function(type) { | |
2817 | if (this._events) { | |
2818 | var evlistener = this._events[type]; | |
2819 | ||
2820 | if (isFunction(evlistener)) | |
2821 | return 1; | |
2822 | else if (evlistener) | |
2823 | return evlistener.length; | |
2824 | } | |
2825 | return 0; | |
2826 | }; | |
2827 | ||
2828 | EventEmitter.listenerCount = function(emitter, type) { | |
2829 | return emitter.listenerCount(type); | |
2830 | }; | |
2831 | ||
2832 | function isFunction(arg) { | |
2833 | return typeof arg === 'function'; | |
2834 | } | |
2835 | ||
2836 | function isNumber(arg) { | |
2837 | return typeof arg === 'number'; | |
2838 | } | |
2839 | ||
2840 | function isObject(arg) { | |
2841 | return typeof arg === 'object' && arg !== null; | |
2842 | } | |
2843 | ||
2844 | function isUndefined(arg) { | |
2845 | return arg === void 0; | |
2846 | } | |
2847 | ||
9f59e99b | 2848 | },{}],8:[function(require,module,exports){ |
a0091a40 IC |
2849 | exports.read = function (buffer, offset, isLE, mLen, nBytes) { |
2850 | var e, m | |
2851 | var eLen = nBytes * 8 - mLen - 1 | |
2852 | var eMax = (1 << eLen) - 1 | |
2853 | var eBias = eMax >> 1 | |
2854 | var nBits = -7 | |
2855 | var i = isLE ? (nBytes - 1) : 0 | |
2856 | var d = isLE ? -1 : 1 | |
2857 | var s = buffer[offset + i] | |
2858 | ||
2859 | i += d | |
2860 | ||
2861 | e = s & ((1 << (-nBits)) - 1) | |
2862 | s >>= (-nBits) | |
2863 | nBits += eLen | |
2864 | for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} | |
2865 | ||
2866 | m = e & ((1 << (-nBits)) - 1) | |
2867 | e >>= (-nBits) | |
2868 | nBits += mLen | |
2869 | for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} | |
2870 | ||
2871 | if (e === 0) { | |
2872 | e = 1 - eBias | |
2873 | } else if (e === eMax) { | |
2874 | return m ? NaN : ((s ? -1 : 1) * Infinity) | |
2875 | } else { | |
2876 | m = m + Math.pow(2, mLen) | |
2877 | e = e - eBias | |
2878 | } | |
2879 | return (s ? -1 : 1) * m * Math.pow(2, e - mLen) | |
2880 | } | |
2881 | ||
2882 | exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { | |
2883 | var e, m, c | |
2884 | var eLen = nBytes * 8 - mLen - 1 | |
2885 | var eMax = (1 << eLen) - 1 | |
2886 | var eBias = eMax >> 1 | |
2887 | var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) | |
2888 | var i = isLE ? 0 : (nBytes - 1) | |
2889 | var d = isLE ? 1 : -1 | |
2890 | var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 | |
2891 | ||
2892 | value = Math.abs(value) | |
2893 | ||
2894 | if (isNaN(value) || value === Infinity) { | |
2895 | m = isNaN(value) ? 1 : 0 | |
2896 | e = eMax | |
2897 | } else { | |
2898 | e = Math.floor(Math.log(value) / Math.LN2) | |
2899 | if (value * (c = Math.pow(2, -e)) < 1) { | |
2900 | e-- | |
2901 | c *= 2 | |
2902 | } | |
2903 | if (e + eBias >= 1) { | |
2904 | value += rt / c | |
2905 | } else { | |
2906 | value += rt * Math.pow(2, 1 - eBias) | |
2907 | } | |
2908 | if (value * c >= 2) { | |
2909 | e++ | |
2910 | c /= 2 | |
2911 | } | |
2912 | ||
2913 | if (e + eBias >= eMax) { | |
2914 | m = 0 | |
2915 | e = eMax | |
2916 | } else if (e + eBias >= 1) { | |
2917 | m = (value * c - 1) * Math.pow(2, mLen) | |
2918 | e = e + eBias | |
2919 | } else { | |
2920 | m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) | |
2921 | e = 0 | |
2922 | } | |
2923 | } | |
2924 | ||
2925 | for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} | |
2926 | ||
2927 | e = (e << mLen) | m | |
2928 | eLen += mLen | |
2929 | for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} | |
2930 | ||
2931 | buffer[offset + i - d] |= s * 128 | |
2932 | } | |
2933 | ||
9f59e99b | 2934 | },{}],9:[function(require,module,exports){ |
a0091a40 IC |
2935 | if (typeof Object.create === 'function') { |
2936 | // implementation from standard node.js 'util' module | |
2937 | module.exports = function inherits(ctor, superCtor) { | |
2938 | ctor.super_ = superCtor | |
2939 | ctor.prototype = Object.create(superCtor.prototype, { | |
2940 | constructor: { | |
2941 | value: ctor, | |
2942 | enumerable: false, | |
2943 | writable: true, | |
2944 | configurable: true | |
2945 | } | |
2946 | }); | |
2947 | }; | |
2948 | } else { | |
2949 | // old school shim for old browsers | |
2950 | module.exports = function inherits(ctor, superCtor) { | |
2951 | ctor.super_ = superCtor | |
2952 | var TempCtor = function () {} | |
2953 | TempCtor.prototype = superCtor.prototype | |
2954 | ctor.prototype = new TempCtor() | |
2955 | ctor.prototype.constructor = ctor | |
2956 | } | |
2957 | } | |
2958 | ||
9f59e99b | 2959 | },{}],10:[function(require,module,exports){ |
a0091a40 IC |
2960 | /*! |
2961 | * Determine if an object is a Buffer | |
2962 | * | |
2963 | * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org> | |
2964 | * @license MIT | |
2965 | */ | |
2966 | ||
2967 | // The _isBuffer check is for Safari 5-7 support, because it's missing | |
2968 | // Object.prototype.constructor. Remove this eventually | |
2969 | module.exports = function (obj) { | |
2970 | return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer) | |
2971 | } | |
2972 | ||
2973 | function isBuffer (obj) { | |
2974 | return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj) | |
2975 | } | |
2976 | ||
2977 | // For Node v0.10 support. Remove this eventually. | |
2978 | function isSlowBuffer (obj) { | |
2979 | return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0)) | |
2980 | } | |
2981 | ||
9f59e99b | 2982 | },{}],11:[function(require,module,exports){ |
a0091a40 IC |
2983 | var toString = {}.toString; |
2984 | ||
2985 | module.exports = Array.isArray || function (arr) { | |
2986 | return toString.call(arr) == '[object Array]'; | |
2987 | }; | |
2988 | ||
9f59e99b | 2989 | },{}],12:[function(require,module,exports){ |
a0091a40 IC |
2990 | (function (process){ |
2991 | 'use strict'; | |
2992 | ||
2993 | if (!process.version || | |
2994 | process.version.indexOf('v0.') === 0 || | |
2995 | process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { | |
2996 | module.exports = nextTick; | |
2997 | } else { | |
2998 | module.exports = process.nextTick; | |
2999 | } | |
3000 | ||
3001 | function nextTick(fn, arg1, arg2, arg3) { | |
3002 | if (typeof fn !== 'function') { | |
3003 | throw new TypeError('"callback" argument must be a function'); | |
3004 | } | |
3005 | var len = arguments.length; | |
3006 | var args, i; | |
3007 | switch (len) { | |
3008 | case 0: | |
3009 | case 1: | |
3010 | return process.nextTick(fn); | |
3011 | case 2: | |
3012 | return process.nextTick(function afterTickOne() { | |
3013 | fn.call(null, arg1); | |
3014 | }); | |
3015 | case 3: | |
3016 | return process.nextTick(function afterTickTwo() { | |
3017 | fn.call(null, arg1, arg2); | |
3018 | }); | |
3019 | case 4: | |
3020 | return process.nextTick(function afterTickThree() { | |
3021 | fn.call(null, arg1, arg2, arg3); | |
3022 | }); | |
3023 | default: | |
3024 | args = new Array(len - 1); | |
3025 | i = 0; | |
3026 | while (i < args.length) { | |
3027 | args[i++] = arguments[i]; | |
3028 | } | |
3029 | return process.nextTick(function afterTick() { | |
3030 | fn.apply(null, args); | |
3031 | }); | |
3032 | } | |
3033 | } | |
3034 | ||
3035 | }).call(this,require('_process')) | |
9f59e99b | 3036 | },{"_process":13}],13:[function(require,module,exports){ |
a0091a40 IC |
3037 | // shim for using process in browser |
3038 | var process = module.exports = {}; | |
3039 | ||
3040 | // cached from whatever global is present so that test runners that stub it | |
3041 | // don't break things. But we need to wrap it in a try catch in case it is | |
3042 | // wrapped in strict mode code which doesn't define any globals. It's inside a | |
3043 | // function because try/catches deoptimize in certain engines. | |
3044 | ||
3045 | var cachedSetTimeout; | |
3046 | var cachedClearTimeout; | |
3047 | ||
3048 | function defaultSetTimout() { | |
3049 | throw new Error('setTimeout has not been defined'); | |
3050 | } | |
3051 | function defaultClearTimeout () { | |
3052 | throw new Error('clearTimeout has not been defined'); | |
3053 | } | |
3054 | (function () { | |
3055 | try { | |
3056 | if (typeof setTimeout === 'function') { | |
3057 | cachedSetTimeout = setTimeout; | |
3058 | } else { | |
3059 | cachedSetTimeout = defaultSetTimout; | |
3060 | } | |
3061 | } catch (e) { | |
3062 | cachedSetTimeout = defaultSetTimout; | |
3063 | } | |
3064 | try { | |
3065 | if (typeof clearTimeout === 'function') { | |
3066 | cachedClearTimeout = clearTimeout; | |
3067 | } else { | |
3068 | cachedClearTimeout = defaultClearTimeout; | |
3069 | } | |
3070 | } catch (e) { | |
3071 | cachedClearTimeout = defaultClearTimeout; | |
3072 | } | |
3073 | } ()) | |
3074 | function runTimeout(fun) { | |
3075 | if (cachedSetTimeout === setTimeout) { | |
3076 | //normal enviroments in sane situations | |
3077 | return setTimeout(fun, 0); | |
3078 | } | |
3079 | // if setTimeout wasn't available but was latter defined | |
3080 | if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { | |
3081 | cachedSetTimeout = setTimeout; | |
3082 | return setTimeout(fun, 0); | |
3083 | } | |
3084 | try { | |
3085 | // when when somebody has screwed with setTimeout but no I.E. maddness | |
3086 | return cachedSetTimeout(fun, 0); | |
3087 | } catch(e){ | |
3088 | try { | |
3089 | // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally | |
3090 | return cachedSetTimeout.call(null, fun, 0); | |
3091 | } catch(e){ | |
3092 | // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error | |
3093 | return cachedSetTimeout.call(this, fun, 0); | |
3094 | } | |
3095 | } | |
3096 | ||
3097 | ||
3098 | } | |
3099 | function runClearTimeout(marker) { | |
3100 | if (cachedClearTimeout === clearTimeout) { | |
3101 | //normal enviroments in sane situations | |
3102 | return clearTimeout(marker); | |
3103 | } | |
3104 | // if clearTimeout wasn't available but was latter defined | |
3105 | if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { | |
3106 | cachedClearTimeout = clearTimeout; | |
3107 | return clearTimeout(marker); | |
3108 | } | |
3109 | try { | |
3110 | // when when somebody has screwed with setTimeout but no I.E. maddness | |
3111 | return cachedClearTimeout(marker); | |
3112 | } catch (e){ | |
3113 | try { | |
3114 | // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally | |
3115 | return cachedClearTimeout.call(null, marker); | |
3116 | } catch (e){ | |
3117 | // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. | |
3118 | // Some versions of I.E. have different rules for clearTimeout vs setTimeout | |
3119 | return cachedClearTimeout.call(this, marker); | |
3120 | } | |
3121 | } | |
3122 | ||
3123 | ||
3124 | ||
3125 | } | |
3126 | var queue = []; | |
3127 | var draining = false; | |
3128 | var currentQueue; | |
3129 | var queueIndex = -1; | |
3130 | ||
3131 | function cleanUpNextTick() { | |
3132 | if (!draining || !currentQueue) { | |
3133 | return; | |
3134 | } | |
3135 | draining = false; | |
3136 | if (currentQueue.length) { | |
3137 | queue = currentQueue.concat(queue); | |
3138 | } else { | |
3139 | queueIndex = -1; | |
3140 | } | |
3141 | if (queue.length) { | |
3142 | drainQueue(); | |
3143 | } | |
3144 | } | |
3145 | ||
3146 | function drainQueue() { | |
3147 | if (draining) { | |
3148 | return; | |
3149 | } | |
3150 | var timeout = runTimeout(cleanUpNextTick); | |
3151 | draining = true; | |
3152 | ||
3153 | var len = queue.length; | |
3154 | while(len) { | |
3155 | currentQueue = queue; | |
3156 | queue = []; | |
3157 | while (++queueIndex < len) { | |
3158 | if (currentQueue) { | |
3159 | currentQueue[queueIndex].run(); | |
3160 | } | |
3161 | } | |
3162 | queueIndex = -1; | |
3163 | len = queue.length; | |
3164 | } | |
3165 | currentQueue = null; | |
3166 | draining = false; | |
3167 | runClearTimeout(timeout); | |
3168 | } | |
3169 | ||
3170 | process.nextTick = function (fun) { | |
3171 | var args = new Array(arguments.length - 1); | |
3172 | if (arguments.length > 1) { | |
3173 | for (var i = 1; i < arguments.length; i++) { | |
3174 | args[i - 1] = arguments[i]; | |
3175 | } | |
3176 | } | |
3177 | queue.push(new Item(fun, args)); | |
3178 | if (queue.length === 1 && !draining) { | |
3179 | runTimeout(drainQueue); | |
3180 | } | |
3181 | }; | |
3182 | ||
3183 | // v8 likes predictible objects | |
3184 | function Item(fun, array) { | |
3185 | this.fun = fun; | |
3186 | this.array = array; | |
3187 | } | |
3188 | Item.prototype.run = function () { | |
3189 | this.fun.apply(null, this.array); | |
3190 | }; | |
3191 | process.title = 'browser'; | |
3192 | process.browser = true; | |
3193 | process.env = {}; | |
3194 | process.argv = []; | |
3195 | process.version = ''; // empty string to avoid regexp issues | |
3196 | process.versions = {}; | |
3197 | ||
3198 | function noop() {} | |
3199 | ||
3200 | process.on = noop; | |
3201 | process.addListener = noop; | |
3202 | process.once = noop; | |
3203 | process.off = noop; | |
3204 | process.removeListener = noop; | |
3205 | process.removeAllListeners = noop; | |
3206 | process.emit = noop; | |
3207 | process.prependListener = noop; | |
3208 | process.prependOnceListener = noop; | |
3209 | ||
3210 | process.listeners = function (name) { return [] } | |
3211 | ||
3212 | process.binding = function (name) { | |
3213 | throw new Error('process.binding is not supported'); | |
3214 | }; | |
3215 | ||
3216 | process.cwd = function () { return '/' }; | |
3217 | process.chdir = function (dir) { | |
3218 | throw new Error('process.chdir is not supported'); | |
3219 | }; | |
3220 | process.umask = function() { return 0; }; | |
3221 | ||
9f59e99b | 3222 | },{}],14:[function(require,module,exports){ |
a0091a40 IC |
3223 | module.exports = require('./lib/_stream_duplex.js'); |
3224 | ||
9f59e99b | 3225 | },{"./lib/_stream_duplex.js":15}],15:[function(require,module,exports){ |
a0091a40 IC |
3226 | // a duplex stream is just a stream that is both readable and writable. |
3227 | // Since JS doesn't have multiple prototypal inheritance, this class | |
3228 | // prototypally inherits from Readable, and then parasitically from | |
3229 | // Writable. | |
3230 | ||
3231 | 'use strict'; | |
3232 | ||
3233 | /*<replacement>*/ | |
3234 | ||
a0091a40 IC |
3235 | var objectKeys = Object.keys || function (obj) { |
3236 | var keys = []; | |
3237 | for (var key in obj) { | |
3238 | keys.push(key); | |
3239 | }return keys; | |
3240 | }; | |
3241 | /*</replacement>*/ | |
3242 | ||
3243 | module.exports = Duplex; | |
3244 | ||
9f59e99b IC |
3245 | /*<replacement>*/ |
3246 | var processNextTick = require('process-nextick-args'); | |
3247 | /*</replacement>*/ | |
3248 | ||
a0091a40 IC |
3249 | /*<replacement>*/ |
3250 | var util = require('core-util-is'); | |
3251 | util.inherits = require('inherits'); | |
3252 | /*</replacement>*/ | |
3253 | ||
3254 | var Readable = require('./_stream_readable'); | |
3255 | var Writable = require('./_stream_writable'); | |
3256 | ||
3257 | util.inherits(Duplex, Readable); | |
3258 | ||
3259 | var keys = objectKeys(Writable.prototype); | |
3260 | for (var v = 0; v < keys.length; v++) { | |
3261 | var method = keys[v]; | |
3262 | if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method]; | |
3263 | } | |
3264 | ||
3265 | function Duplex(options) { | |
3266 | if (!(this instanceof Duplex)) return new Duplex(options); | |
3267 | ||
3268 | Readable.call(this, options); | |
3269 | Writable.call(this, options); | |
3270 | ||
3271 | if (options && options.readable === false) this.readable = false; | |
3272 | ||
3273 | if (options && options.writable === false) this.writable = false; | |
3274 | ||
3275 | this.allowHalfOpen = true; | |
3276 | if (options && options.allowHalfOpen === false) this.allowHalfOpen = false; | |
3277 | ||
3278 | this.once('end', onend); | |
3279 | } | |
3280 | ||
3281 | // the no-half-open enforcer | |
3282 | function onend() { | |
3283 | // if we allow half-open state, or if the writable side ended, | |
3284 | // then we're ok. | |
3285 | if (this.allowHalfOpen || this._writableState.ended) return; | |
3286 | ||
3287 | // no more data can be written. | |
3288 | // But allow more writes to happen in this tick. | |
3289 | processNextTick(onEndNT, this); | |
3290 | } | |
3291 | ||
3292 | function onEndNT(self) { | |
3293 | self.end(); | |
3294 | } | |
3295 | ||
a0091a40 IC |
3296 | function forEach(xs, f) { |
3297 | for (var i = 0, l = xs.length; i < l; i++) { | |
3298 | f(xs[i], i); | |
3299 | } | |
3300 | } | |
9f59e99b | 3301 | },{"./_stream_readable":17,"./_stream_writable":19,"core-util-is":6,"inherits":9,"process-nextick-args":12}],16:[function(require,module,exports){ |
a0091a40 IC |
3302 | // a passthrough stream. |
3303 | // basically just the most minimal sort of Transform stream. | |
3304 | // Every written chunk gets output as-is. | |
3305 | ||
3306 | 'use strict'; | |
3307 | ||
3308 | module.exports = PassThrough; | |
3309 | ||
3310 | var Transform = require('./_stream_transform'); | |
3311 | ||
3312 | /*<replacement>*/ | |
3313 | var util = require('core-util-is'); | |
3314 | util.inherits = require('inherits'); | |
3315 | /*</replacement>*/ | |
3316 | ||
3317 | util.inherits(PassThrough, Transform); | |
3318 | ||
3319 | function PassThrough(options) { | |
3320 | if (!(this instanceof PassThrough)) return new PassThrough(options); | |
3321 | ||
3322 | Transform.call(this, options); | |
3323 | } | |
3324 | ||
3325 | PassThrough.prototype._transform = function (chunk, encoding, cb) { | |
3326 | cb(null, chunk); | |
3327 | }; | |
9f59e99b IC |
3328 | },{"./_stream_transform":18,"core-util-is":6,"inherits":9}],17:[function(require,module,exports){ |
3329 | (function (process){ | |
a0091a40 IC |
3330 | 'use strict'; |
3331 | ||
9f59e99b | 3332 | module.exports = Readable; |
a0091a40 | 3333 | |
9f59e99b | 3334 | /*<replacement>*/ |
a0091a40 IC |
3335 | var processNextTick = require('process-nextick-args'); |
3336 | /*</replacement>*/ | |
3337 | ||
a0091a40 IC |
3338 | /*<replacement>*/ |
3339 | var isArray = require('isarray'); | |
3340 | /*</replacement>*/ | |
3341 | ||
3342 | /*<replacement>*/ | |
3343 | var Duplex; | |
3344 | /*</replacement>*/ | |
3345 | ||
3346 | Readable.ReadableState = ReadableState; | |
3347 | ||
3348 | /*<replacement>*/ | |
3349 | var EE = require('events').EventEmitter; | |
3350 | ||
3351 | var EElistenerCount = function (emitter, type) { | |
3352 | return emitter.listeners(type).length; | |
3353 | }; | |
3354 | /*</replacement>*/ | |
3355 | ||
3356 | /*<replacement>*/ | |
3357 | var Stream = require('./internal/streams/stream'); | |
3358 | /*</replacement>*/ | |
3359 | ||
9f59e99b | 3360 | var Buffer = require('buffer').Buffer; |
a0091a40 | 3361 | /*<replacement>*/ |
9f59e99b | 3362 | var bufferShim = require('buffer-shims'); |
a0091a40 IC |
3363 | /*</replacement>*/ |
3364 | ||
3365 | /*<replacement>*/ | |
3366 | var util = require('core-util-is'); | |
3367 | util.inherits = require('inherits'); | |
3368 | /*</replacement>*/ | |
3369 | ||
3370 | /*<replacement>*/ | |
3371 | var debugUtil = require('util'); | |
3372 | var debug = void 0; | |
3373 | if (debugUtil && debugUtil.debuglog) { | |
3374 | debug = debugUtil.debuglog('stream'); | |
3375 | } else { | |
3376 | debug = function () {}; | |
3377 | } | |
3378 | /*</replacement>*/ | |
3379 | ||
3380 | var BufferList = require('./internal/streams/BufferList'); | |
a0091a40 IC |
3381 | var StringDecoder; |
3382 | ||
3383 | util.inherits(Readable, Stream); | |
3384 | ||
3385 | var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume']; | |
3386 | ||
3387 | function prependListener(emitter, event, fn) { | |
3388 | // Sadly this is not cacheable as some libraries bundle their own | |
3389 | // event emitter implementation with them. | |
3390 | if (typeof emitter.prependListener === 'function') { | |
3391 | return emitter.prependListener(event, fn); | |
3392 | } else { | |
3393 | // This is a hack to make sure that our error handler is attached before any | |
3394 | // userland ones. NEVER DO THIS. This is here only because this code needs | |
3395 | // to continue to work with older versions of Node.js that do not include | |
3396 | // the prependListener() method. The goal is to eventually remove this hack. | |
3397 | if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]]; | |
3398 | } | |
3399 | } | |
3400 | ||
3401 | function ReadableState(options, stream) { | |
3402 | Duplex = Duplex || require('./_stream_duplex'); | |
3403 | ||
3404 | options = options || {}; | |
3405 | ||
3406 | // object stream flag. Used to make read(n) ignore n and to | |
3407 | // make all the buffer merging and length checks go away | |
3408 | this.objectMode = !!options.objectMode; | |
3409 | ||
3410 | if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode; | |
3411 | ||
3412 | // the point at which it stops calling _read() to fill the buffer | |
3413 | // Note: 0 is a valid value, means "don't call _read preemptively ever" | |
3414 | var hwm = options.highWaterMark; | |
3415 | var defaultHwm = this.objectMode ? 16 : 16 * 1024; | |
3416 | this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; | |
3417 | ||
3418 | // cast to ints. | |
9f59e99b | 3419 | this.highWaterMark = ~~this.highWaterMark; |
a0091a40 IC |
3420 | |
3421 | // A linked list is used to store data chunks instead of an array because the | |
3422 | // linked list can remove elements from the beginning faster than | |
3423 | // array.shift() | |
3424 | this.buffer = new BufferList(); | |
3425 | this.length = 0; | |
3426 | this.pipes = null; | |
3427 | this.pipesCount = 0; | |
3428 | this.flowing = null; | |
3429 | this.ended = false; | |
3430 | this.endEmitted = false; | |
3431 | this.reading = false; | |
3432 | ||
9f59e99b IC |
3433 | // a flag to be able to tell if the onwrite cb is called immediately, |
3434 | // or on a later tick. We set this to true at first, because any | |
3435 | // actions that shouldn't happen until "later" should generally also | |
3436 | // not happen before the first write call. | |
a0091a40 IC |
3437 | this.sync = true; |
3438 | ||
3439 | // whenever we return null, then we set a flag to say | |
3440 | // that we're awaiting a 'readable' event emission. | |
3441 | this.needReadable = false; | |
3442 | this.emittedReadable = false; | |
3443 | this.readableListening = false; | |
3444 | this.resumeScheduled = false; | |
3445 | ||
a0091a40 IC |
3446 | // Crypto is kind of old and crusty. Historically, its default string |
3447 | // encoding is 'binary' so we have to make this configurable. | |
3448 | // Everything else in the universe uses 'utf8', though. | |
3449 | this.defaultEncoding = options.defaultEncoding || 'utf8'; | |
3450 | ||
9f59e99b IC |
3451 | // when piping, we only care about 'readable' events that happen |
3452 | // after read()ing all the bytes and not getting any pushback. | |
3453 | this.ranOut = false; | |
3454 | ||
a0091a40 IC |
3455 | // the number of writers that are awaiting a drain event in .pipe()s |
3456 | this.awaitDrain = 0; | |
3457 | ||
3458 | // if true, a maybeReadMore has been scheduled | |
3459 | this.readingMore = false; | |
3460 | ||
3461 | this.decoder = null; | |
3462 | this.encoding = null; | |
3463 | if (options.encoding) { | |
3464 | if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; | |
3465 | this.decoder = new StringDecoder(options.encoding); | |
3466 | this.encoding = options.encoding; | |
3467 | } | |
3468 | } | |
3469 | ||
3470 | function Readable(options) { | |
3471 | Duplex = Duplex || require('./_stream_duplex'); | |
3472 | ||
3473 | if (!(this instanceof Readable)) return new Readable(options); | |
3474 | ||
3475 | this._readableState = new ReadableState(options, this); | |
3476 | ||
3477 | // legacy | |
3478 | this.readable = true; | |
3479 | ||
9f59e99b | 3480 | if (options && typeof options.read === 'function') this._read = options.read; |
a0091a40 IC |
3481 | |
3482 | Stream.call(this); | |
3483 | } | |
3484 | ||
a0091a40 IC |
3485 | // Manually shove something into the read() buffer. |
3486 | // This returns true if the highWaterMark has not been hit yet, | |
3487 | // similar to how Writable.write() returns true if you should | |
3488 | // write() some more. | |
3489 | Readable.prototype.push = function (chunk, encoding) { | |
3490 | var state = this._readableState; | |
a0091a40 | 3491 | |
9f59e99b IC |
3492 | if (!state.objectMode && typeof chunk === 'string') { |
3493 | encoding = encoding || state.defaultEncoding; | |
3494 | if (encoding !== state.encoding) { | |
3495 | chunk = bufferShim.from(chunk, encoding); | |
3496 | encoding = ''; | |
a0091a40 | 3497 | } |
a0091a40 IC |
3498 | } |
3499 | ||
9f59e99b | 3500 | return readableAddChunk(this, state, chunk, encoding, false); |
a0091a40 IC |
3501 | }; |
3502 | ||
3503 | // Unshift should *always* be something directly out of read() | |
3504 | Readable.prototype.unshift = function (chunk) { | |
9f59e99b IC |
3505 | var state = this._readableState; |
3506 | return readableAddChunk(this, state, chunk, '', true); | |
a0091a40 IC |
3507 | }; |
3508 | ||
9f59e99b IC |
3509 | Readable.prototype.isPaused = function () { |
3510 | return this._readableState.flowing === false; | |
3511 | }; | |
3512 | ||
3513 | function readableAddChunk(stream, state, chunk, encoding, addToFront) { | |
3514 | var er = chunkInvalid(state, chunk); | |
3515 | if (er) { | |
3516 | stream.emit('error', er); | |
3517 | } else if (chunk === null) { | |
a0091a40 IC |
3518 | state.reading = false; |
3519 | onEofChunk(stream, state); | |
9f59e99b IC |
3520 | } else if (state.objectMode || chunk && chunk.length > 0) { |
3521 | if (state.ended && !addToFront) { | |
3522 | var e = new Error('stream.push() after EOF'); | |
3523 | stream.emit('error', e); | |
3524 | } else if (state.endEmitted && addToFront) { | |
3525 | var _e = new Error('stream.unshift() after end event'); | |
3526 | stream.emit('error', _e); | |
3527 | } else { | |
3528 | var skipAdd; | |
3529 | if (state.decoder && !addToFront && !encoding) { | |
3530 | chunk = state.decoder.write(chunk); | |
3531 | skipAdd = !state.objectMode && chunk.length === 0; | |
3532 | } | |
3533 | ||
3534 | if (!addToFront) state.reading = false; | |
3535 | ||
3536 | // Don't add to the buffer if we've decoded to an empty string chunk and | |
3537 | // we're not in object mode | |
3538 | if (!skipAdd) { | |
3539 | // if we want the data now, just emit it. | |
3540 | if (state.flowing && state.length === 0 && !state.sync) { | |
3541 | stream.emit('data', chunk); | |
3542 | stream.read(0); | |
a0091a40 | 3543 | } else { |
9f59e99b IC |
3544 | // update the buffer info. |
3545 | state.length += state.objectMode ? 1 : chunk.length; | |
3546 | if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk); | |
3547 | ||
3548 | if (state.needReadable) emitReadable(stream); | |
a0091a40 IC |
3549 | } |
3550 | } | |
9f59e99b IC |
3551 | |
3552 | maybeReadMore(stream, state); | |
a0091a40 | 3553 | } |
9f59e99b IC |
3554 | } else if (!addToFront) { |
3555 | state.reading = false; | |
a0091a40 IC |
3556 | } |
3557 | ||
3558 | return needMoreData(state); | |
3559 | } | |
3560 | ||
a0091a40 IC |
3561 | // if it's past the high water mark, we can push in some more. |
3562 | // Also, if we have no data yet, we can stand some | |
3563 | // more bytes. This is to work around cases where hwm=0, | |
3564 | // such as the repl. Also, if the push() triggered a | |
3565 | // readable event, and the user called read(largeNumber) such that | |
3566 | // needReadable was set, then we ought to push more, so that another | |
3567 | // 'readable' event will be triggered. | |
3568 | function needMoreData(state) { | |
3569 | return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0); | |
3570 | } | |
3571 | ||
a0091a40 IC |
3572 | // backwards compatibility. |
3573 | Readable.prototype.setEncoding = function (enc) { | |
3574 | if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; | |
3575 | this._readableState.decoder = new StringDecoder(enc); | |
3576 | this._readableState.encoding = enc; | |
3577 | return this; | |
3578 | }; | |
3579 | ||
3580 | // Don't raise the hwm > 8MB | |
3581 | var MAX_HWM = 0x800000; | |
3582 | function computeNewHighWaterMark(n) { | |
3583 | if (n >= MAX_HWM) { | |
3584 | n = MAX_HWM; | |
3585 | } else { | |
3586 | // Get the next highest power of 2 to prevent increasing hwm excessively in | |
3587 | // tiny amounts | |
3588 | n--; | |
3589 | n |= n >>> 1; | |
3590 | n |= n >>> 2; | |
3591 | n |= n >>> 4; | |
3592 | n |= n >>> 8; | |
3593 | n |= n >>> 16; | |
3594 | n++; | |
3595 | } | |
3596 | return n; | |
3597 | } | |
3598 | ||
3599 | // This function is designed to be inlinable, so please take care when making | |
3600 | // changes to the function body. | |
3601 | function howMuchToRead(n, state) { | |
3602 | if (n <= 0 || state.length === 0 && state.ended) return 0; | |
3603 | if (state.objectMode) return 1; | |
3604 | if (n !== n) { | |
3605 | // Only flow one buffer at a time | |
3606 | if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length; | |
3607 | } | |
3608 | // If we're asking for more than the current hwm, then raise the hwm. | |
3609 | if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); | |
3610 | if (n <= state.length) return n; | |
3611 | // Don't have enough | |
3612 | if (!state.ended) { | |
3613 | state.needReadable = true; | |
3614 | return 0; | |
3615 | } | |
3616 | return state.length; | |
3617 | } | |
3618 | ||
3619 | // you can override either this method, or the async _read(n) below. | |
3620 | Readable.prototype.read = function (n) { | |
3621 | debug('read', n); | |
3622 | n = parseInt(n, 10); | |
3623 | var state = this._readableState; | |
3624 | var nOrig = n; | |
3625 | ||
3626 | if (n !== 0) state.emittedReadable = false; | |
3627 | ||
3628 | // if we're doing read(0) to trigger a readable event, but we | |
3629 | // already have a bunch of data in the buffer, then just trigger | |
3630 | // the 'readable' event and move on. | |
3631 | if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { | |
3632 | debug('read: emitReadable', state.length, state.ended); | |
3633 | if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this); | |
3634 | return null; | |
3635 | } | |
3636 | ||
3637 | n = howMuchToRead(n, state); | |
3638 | ||
3639 | // if we've ended, and we're now clear, then finish it up. | |
3640 | if (n === 0 && state.ended) { | |
3641 | if (state.length === 0) endReadable(this); | |
3642 | return null; | |
3643 | } | |
3644 | ||
3645 | // All the actual chunk generation logic needs to be | |
3646 | // *below* the call to _read. The reason is that in certain | |
3647 | // synthetic stream cases, such as passthrough streams, _read | |
3648 | // may be a completely synchronous operation which may change | |
3649 | // the state of the read buffer, providing enough data when | |
3650 | // before there was *not* enough. | |
3651 | // | |
3652 | // So, the steps are: | |
3653 | // 1. Figure out what the state of things will be after we do | |
3654 | // a read from the buffer. | |
3655 | // | |
3656 | // 2. If that resulting state will trigger a _read, then call _read. | |
3657 | // Note that this may be asynchronous, or synchronous. Yes, it is | |
3658 | // deeply ugly to write APIs this way, but that still doesn't mean | |
3659 | // that the Readable class should behave improperly, as streams are | |
3660 | // designed to be sync/async agnostic. | |
3661 | // Take note if the _read call is sync or async (ie, if the read call | |
3662 | // has returned yet), so that we know whether or not it's safe to emit | |
3663 | // 'readable' etc. | |
3664 | // | |
3665 | // 3. Actually pull the requested chunks out of the buffer and return. | |
3666 | ||
3667 | // if we need a readable event, then we need to do some reading. | |
3668 | var doRead = state.needReadable; | |
3669 | debug('need readable', doRead); | |
3670 | ||
3671 | // if we currently have less than the highWaterMark, then also read some | |
3672 | if (state.length === 0 || state.length - n < state.highWaterMark) { | |
3673 | doRead = true; | |
3674 | debug('length less than watermark', doRead); | |
3675 | } | |
3676 | ||
3677 | // however, if we've ended, then there's no point, and if we're already | |
3678 | // reading, then it's unnecessary. | |
3679 | if (state.ended || state.reading) { | |
3680 | doRead = false; | |
3681 | debug('reading or ended', doRead); | |
3682 | } else if (doRead) { | |
3683 | debug('do read'); | |
3684 | state.reading = true; | |
3685 | state.sync = true; | |
3686 | // if the length is currently zero, then we *need* a readable event. | |
3687 | if (state.length === 0) state.needReadable = true; | |
3688 | // call internal read method | |
3689 | this._read(state.highWaterMark); | |
3690 | state.sync = false; | |
3691 | // If _read pushed data synchronously, then `reading` will be false, | |
3692 | // and we need to re-evaluate how much data we can return to the user. | |
3693 | if (!state.reading) n = howMuchToRead(nOrig, state); | |
3694 | } | |
3695 | ||
3696 | var ret; | |
3697 | if (n > 0) ret = fromList(n, state);else ret = null; | |
3698 | ||
3699 | if (ret === null) { | |
3700 | state.needReadable = true; | |
3701 | n = 0; | |
3702 | } else { | |
3703 | state.length -= n; | |
3704 | } | |
3705 | ||
3706 | if (state.length === 0) { | |
3707 | // If we have nothing in the buffer, then we want to know | |
3708 | // as soon as we *do* get something into the buffer. | |
3709 | if (!state.ended) state.needReadable = true; | |
3710 | ||
3711 | // If we tried to read() past the EOF, then emit end on the next tick. | |
3712 | if (nOrig !== n && state.ended) endReadable(this); | |
3713 | } | |
3714 | ||
3715 | if (ret !== null) this.emit('data', ret); | |
3716 | ||
3717 | return ret; | |
3718 | }; | |
3719 | ||
9f59e99b IC |
3720 | function chunkInvalid(state, chunk) { |
3721 | var er = null; | |
3722 | if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) { | |
3723 | er = new TypeError('Invalid non-string/buffer chunk'); | |
3724 | } | |
3725 | return er; | |
3726 | } | |
3727 | ||
a0091a40 IC |
3728 | function onEofChunk(stream, state) { |
3729 | if (state.ended) return; | |
3730 | if (state.decoder) { | |
3731 | var chunk = state.decoder.end(); | |
3732 | if (chunk && chunk.length) { | |
3733 | state.buffer.push(chunk); | |
3734 | state.length += state.objectMode ? 1 : chunk.length; | |
3735 | } | |
3736 | } | |
3737 | state.ended = true; | |
3738 | ||
3739 | // emit 'readable' now to make sure it gets picked up. | |
3740 | emitReadable(stream); | |
3741 | } | |
3742 | ||
3743 | // Don't emit readable right away in sync mode, because this can trigger | |
3744 | // another read() call => stack overflow. This way, it might trigger | |
3745 | // a nextTick recursion warning, but that's not so bad. | |
3746 | function emitReadable(stream) { | |
3747 | var state = stream._readableState; | |
3748 | state.needReadable = false; | |
3749 | if (!state.emittedReadable) { | |
3750 | debug('emitReadable', state.flowing); | |
3751 | state.emittedReadable = true; | |
3752 | if (state.sync) processNextTick(emitReadable_, stream);else emitReadable_(stream); | |
3753 | } | |
3754 | } | |
3755 | ||
3756 | function emitReadable_(stream) { | |
3757 | debug('emit readable'); | |
3758 | stream.emit('readable'); | |
3759 | flow(stream); | |
3760 | } | |
3761 | ||
3762 | // at this point, the user has presumably seen the 'readable' event, | |
3763 | // and called read() to consume some data. that may have triggered | |
3764 | // in turn another _read(n) call, in which case reading = true if | |
3765 | // it's in progress. | |
3766 | // However, if we're not ended, or reading, and the length < hwm, | |
3767 | // then go ahead and try to read some more preemptively. | |
3768 | function maybeReadMore(stream, state) { | |
3769 | if (!state.readingMore) { | |
3770 | state.readingMore = true; | |
3771 | processNextTick(maybeReadMore_, stream, state); | |
3772 | } | |
3773 | } | |
3774 | ||
3775 | function maybeReadMore_(stream, state) { | |
3776 | var len = state.length; | |
3777 | while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) { | |
3778 | debug('maybeReadMore read 0'); | |
3779 | stream.read(0); | |
3780 | if (len === state.length) | |
3781 | // didn't get any data, stop spinning. | |
3782 | break;else len = state.length; | |
3783 | } | |
3784 | state.readingMore = false; | |
3785 | } | |
3786 | ||
3787 | // abstract method. to be overridden in specific implementation classes. | |
3788 | // call cb(er, data) where data is <= n in length. | |
3789 | // for virtual (non-string, non-buffer) streams, "length" is somewhat | |
3790 | // arbitrary, and perhaps not very meaningful. | |
3791 | Readable.prototype._read = function (n) { | |
3792 | this.emit('error', new Error('_read() is not implemented')); | |
3793 | }; | |
3794 | ||
3795 | Readable.prototype.pipe = function (dest, pipeOpts) { | |
3796 | var src = this; | |
3797 | var state = this._readableState; | |
3798 | ||
3799 | switch (state.pipesCount) { | |
3800 | case 0: | |
3801 | state.pipes = dest; | |
3802 | break; | |
3803 | case 1: | |
3804 | state.pipes = [state.pipes, dest]; | |
3805 | break; | |
3806 | default: | |
3807 | state.pipes.push(dest); | |
3808 | break; | |
3809 | } | |
3810 | state.pipesCount += 1; | |
3811 | debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); | |
3812 | ||
3813 | var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; | |
3814 | ||
9f59e99b | 3815 | var endFn = doEnd ? onend : cleanup; |
a0091a40 IC |
3816 | if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn); |
3817 | ||
3818 | dest.on('unpipe', onunpipe); | |
9f59e99b | 3819 | function onunpipe(readable) { |
a0091a40 IC |
3820 | debug('onunpipe'); |
3821 | if (readable === src) { | |
9f59e99b | 3822 | cleanup(); |
a0091a40 IC |
3823 | } |
3824 | } | |
3825 | ||
3826 | function onend() { | |
3827 | debug('onend'); | |
3828 | dest.end(); | |
3829 | } | |
3830 | ||
3831 | // when the dest drains, it reduces the awaitDrain counter | |
3832 | // on the source. This would be more elegant with a .once() | |
3833 | // handler in flow(), but adding and removing repeatedly is | |
3834 | // too slow. | |
3835 | var ondrain = pipeOnDrain(src); | |
3836 | dest.on('drain', ondrain); | |
3837 | ||
3838 | var cleanedUp = false; | |
3839 | function cleanup() { | |
3840 | debug('cleanup'); | |
3841 | // cleanup event handlers once the pipe is broken | |
3842 | dest.removeListener('close', onclose); | |
3843 | dest.removeListener('finish', onfinish); | |
3844 | dest.removeListener('drain', ondrain); | |
3845 | dest.removeListener('error', onerror); | |
3846 | dest.removeListener('unpipe', onunpipe); | |
3847 | src.removeListener('end', onend); | |
9f59e99b | 3848 | src.removeListener('end', cleanup); |
a0091a40 IC |
3849 | src.removeListener('data', ondata); |
3850 | ||
3851 | cleanedUp = true; | |
3852 | ||
3853 | // if the reader is waiting for a drain event from this | |
3854 | // specific writer, then it would cause it to never start | |
3855 | // flowing again. | |
3856 | // So, if this is awaiting a drain, then we just call it now. | |
3857 | // If we don't know, then assume that we are waiting for one. | |
3858 | if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); | |
3859 | } | |
3860 | ||
3861 | // If the user pushes more data while we're writing to dest then we'll end up | |
3862 | // in ondata again. However, we only want to increase awaitDrain once because | |
3863 | // dest will only emit one 'drain' event for the multiple writes. | |
3864 | // => Introduce a guard on increasing awaitDrain. | |
3865 | var increasedAwaitDrain = false; | |
3866 | src.on('data', ondata); | |
3867 | function ondata(chunk) { | |
3868 | debug('ondata'); | |
3869 | increasedAwaitDrain = false; | |
3870 | var ret = dest.write(chunk); | |
3871 | if (false === ret && !increasedAwaitDrain) { | |
3872 | // If the user unpiped during `dest.write()`, it is possible | |
3873 | // to get stuck in a permanently paused state if that write | |
3874 | // also returned false. | |
3875 | // => Check whether `dest` is still a piping destination. | |
3876 | if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) { | |
3877 | debug('false write response, pause', src._readableState.awaitDrain); | |
3878 | src._readableState.awaitDrain++; | |
3879 | increasedAwaitDrain = true; | |
3880 | } | |
3881 | src.pause(); | |
3882 | } | |
3883 | } | |
3884 | ||
3885 | // if the dest has an error, then stop piping into it. | |
3886 | // however, don't suppress the throwing behavior for this. | |
3887 | function onerror(er) { | |
3888 | debug('onerror', er); | |
3889 | unpipe(); | |
3890 | dest.removeListener('error', onerror); | |
3891 | if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er); | |
3892 | } | |
3893 | ||
3894 | // Make sure our error handler is attached before userland ones. | |
3895 | prependListener(dest, 'error', onerror); | |
3896 | ||
3897 | // Both close and finish should trigger unpipe, but only once. | |
3898 | function onclose() { | |
3899 | dest.removeListener('finish', onfinish); | |
3900 | unpipe(); | |
3901 | } | |
3902 | dest.once('close', onclose); | |
3903 | function onfinish() { | |
3904 | debug('onfinish'); | |
3905 | dest.removeListener('close', onclose); | |
3906 | unpipe(); | |
3907 | } | |
3908 | dest.once('finish', onfinish); | |
3909 | ||
3910 | function unpipe() { | |
3911 | debug('unpipe'); | |
3912 | src.unpipe(dest); | |
3913 | } | |
3914 | ||
3915 | // tell the dest that it's being piped to | |
3916 | dest.emit('pipe', src); | |
3917 | ||
3918 | // start the flow if it hasn't been started already. | |
3919 | if (!state.flowing) { | |
3920 | debug('pipe resume'); | |
3921 | src.resume(); | |
3922 | } | |
3923 | ||
3924 | return dest; | |
3925 | }; | |
3926 | ||
3927 | function pipeOnDrain(src) { | |
3928 | return function () { | |
3929 | var state = src._readableState; | |
3930 | debug('pipeOnDrain', state.awaitDrain); | |
3931 | if (state.awaitDrain) state.awaitDrain--; | |
3932 | if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { | |
3933 | state.flowing = true; | |
3934 | flow(src); | |
3935 | } | |
3936 | }; | |
3937 | } | |
3938 | ||
3939 | Readable.prototype.unpipe = function (dest) { | |
3940 | var state = this._readableState; | |
a0091a40 IC |
3941 | |
3942 | // if we're not piping anywhere, then do nothing. | |
3943 | if (state.pipesCount === 0) return this; | |
3944 | ||
3945 | // just one destination. most common case. | |
3946 | if (state.pipesCount === 1) { | |
3947 | // passed in one, but it's not the right one. | |
3948 | if (dest && dest !== state.pipes) return this; | |
3949 | ||
3950 | if (!dest) dest = state.pipes; | |
3951 | ||
3952 | // got a match. | |
3953 | state.pipes = null; | |
3954 | state.pipesCount = 0; | |
3955 | state.flowing = false; | |
9f59e99b | 3956 | if (dest) dest.emit('unpipe', this); |
a0091a40 IC |
3957 | return this; |
3958 | } | |
3959 | ||
3960 | // slow case. multiple pipe destinations. | |
3961 | ||
3962 | if (!dest) { | |
3963 | // remove all. | |
3964 | var dests = state.pipes; | |
3965 | var len = state.pipesCount; | |
3966 | state.pipes = null; | |
3967 | state.pipesCount = 0; | |
3968 | state.flowing = false; | |
3969 | ||
3970 | for (var i = 0; i < len; i++) { | |
9f59e99b | 3971 | dests[i].emit('unpipe', this); |
a0091a40 IC |
3972 | }return this; |
3973 | } | |
3974 | ||
3975 | // try to find the right one. | |
3976 | var index = indexOf(state.pipes, dest); | |
3977 | if (index === -1) return this; | |
3978 | ||
3979 | state.pipes.splice(index, 1); | |
3980 | state.pipesCount -= 1; | |
3981 | if (state.pipesCount === 1) state.pipes = state.pipes[0]; | |
3982 | ||
9f59e99b | 3983 | dest.emit('unpipe', this); |
a0091a40 IC |
3984 | |
3985 | return this; | |
3986 | }; | |
3987 | ||
3988 | // set up data events if they are asked for | |
3989 | // Ensure readable listeners eventually get something | |
3990 | Readable.prototype.on = function (ev, fn) { | |
3991 | var res = Stream.prototype.on.call(this, ev, fn); | |
3992 | ||
3993 | if (ev === 'data') { | |
3994 | // Start flowing on next tick if stream isn't explicitly paused | |
3995 | if (this._readableState.flowing !== false) this.resume(); | |
3996 | } else if (ev === 'readable') { | |
3997 | var state = this._readableState; | |
3998 | if (!state.endEmitted && !state.readableListening) { | |
3999 | state.readableListening = state.needReadable = true; | |
4000 | state.emittedReadable = false; | |
4001 | if (!state.reading) { | |
4002 | processNextTick(nReadingNextTick, this); | |
4003 | } else if (state.length) { | |
9f59e99b | 4004 | emitReadable(this, state); |
a0091a40 IC |
4005 | } |
4006 | } | |
4007 | } | |
4008 | ||
4009 | return res; | |
4010 | }; | |
4011 | Readable.prototype.addListener = Readable.prototype.on; | |
4012 | ||
4013 | function nReadingNextTick(self) { | |
4014 | debug('readable nexttick read 0'); | |
4015 | self.read(0); | |
4016 | } | |
4017 | ||
4018 | // pause() and resume() are remnants of the legacy readable stream API | |
4019 | // If the user uses them, then switch into old mode. | |
4020 | Readable.prototype.resume = function () { | |
4021 | var state = this._readableState; | |
4022 | if (!state.flowing) { | |
4023 | debug('resume'); | |
4024 | state.flowing = true; | |
4025 | resume(this, state); | |
4026 | } | |
4027 | return this; | |
4028 | }; | |
4029 | ||
4030 | function resume(stream, state) { | |
4031 | if (!state.resumeScheduled) { | |
4032 | state.resumeScheduled = true; | |
4033 | processNextTick(resume_, stream, state); | |
4034 | } | |
4035 | } | |
4036 | ||
4037 | function resume_(stream, state) { | |
4038 | if (!state.reading) { | |
4039 | debug('resume read 0'); | |
4040 | stream.read(0); | |
4041 | } | |
4042 | ||
4043 | state.resumeScheduled = false; | |
4044 | state.awaitDrain = 0; | |
4045 | stream.emit('resume'); | |
4046 | flow(stream); | |
4047 | if (state.flowing && !state.reading) stream.read(0); | |
4048 | } | |
4049 | ||
4050 | Readable.prototype.pause = function () { | |
4051 | debug('call pause flowing=%j', this._readableState.flowing); | |
4052 | if (false !== this._readableState.flowing) { | |
4053 | debug('pause'); | |
4054 | this._readableState.flowing = false; | |
4055 | this.emit('pause'); | |
4056 | } | |
4057 | return this; | |
4058 | }; | |
4059 | ||
4060 | function flow(stream) { | |
4061 | var state = stream._readableState; | |
4062 | debug('flow', state.flowing); | |
4063 | while (state.flowing && stream.read() !== null) {} | |
4064 | } | |
4065 | ||
4066 | // wrap an old-style stream as the async data source. | |
4067 | // This is *not* part of the readable stream interface. | |
4068 | // It is an ugly unfortunate mess of history. | |
4069 | Readable.prototype.wrap = function (stream) { | |
4070 | var state = this._readableState; | |
4071 | var paused = false; | |
4072 | ||
4073 | var self = this; | |
4074 | stream.on('end', function () { | |
4075 | debug('wrapped end'); | |
4076 | if (state.decoder && !state.ended) { | |
4077 | var chunk = state.decoder.end(); | |
4078 | if (chunk && chunk.length) self.push(chunk); | |
4079 | } | |
4080 | ||
4081 | self.push(null); | |
4082 | }); | |
4083 | ||
4084 | stream.on('data', function (chunk) { | |
4085 | debug('wrapped data'); | |
4086 | if (state.decoder) chunk = state.decoder.write(chunk); | |
4087 | ||
4088 | // don't skip over falsy values in objectMode | |
4089 | if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return; | |
4090 | ||
4091 | var ret = self.push(chunk); | |
4092 | if (!ret) { | |
4093 | paused = true; | |
4094 | stream.pause(); | |
4095 | } | |
4096 | }); | |
4097 | ||
4098 | // proxy all the other methods. | |
4099 | // important when wrapping filters and duplexes. | |
4100 | for (var i in stream) { | |
4101 | if (this[i] === undefined && typeof stream[i] === 'function') { | |
4102 | this[i] = function (method) { | |
4103 | return function () { | |
4104 | return stream[method].apply(stream, arguments); | |
4105 | }; | |
4106 | }(i); | |
4107 | } | |
4108 | } | |
4109 | ||
4110 | // proxy certain important events. | |
4111 | for (var n = 0; n < kProxyEvents.length; n++) { | |
4112 | stream.on(kProxyEvents[n], self.emit.bind(self, kProxyEvents[n])); | |
4113 | } | |
4114 | ||
4115 | // when we try to consume some more bytes, simply unpause the | |
4116 | // underlying stream. | |
4117 | self._read = function (n) { | |
4118 | debug('wrapped _read', n); | |
4119 | if (paused) { | |
4120 | paused = false; | |
4121 | stream.resume(); | |
4122 | } | |
4123 | }; | |
4124 | ||
4125 | return self; | |
4126 | }; | |
4127 | ||
4128 | // exposed for testing purposes only. | |
4129 | Readable._fromList = fromList; | |
4130 | ||
4131 | // Pluck off n bytes from an array of buffers. | |
4132 | // Length is the combined lengths of all the buffers in the list. | |
4133 | // This function is designed to be inlinable, so please take care when making | |
4134 | // changes to the function body. | |
4135 | function fromList(n, state) { | |
4136 | // nothing buffered | |
4137 | if (state.length === 0) return null; | |
4138 | ||
4139 | var ret; | |
4140 | if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) { | |
4141 | // read it all, truncate the list | |
4142 | if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length); | |
4143 | state.buffer.clear(); | |
4144 | } else { | |
4145 | // read part of list | |
4146 | ret = fromListPartial(n, state.buffer, state.decoder); | |
4147 | } | |
4148 | ||
4149 | return ret; | |
4150 | } | |
4151 | ||
4152 | // Extracts only enough buffered data to satisfy the amount requested. | |
4153 | // This function is designed to be inlinable, so please take care when making | |
4154 | // changes to the function body. | |
4155 | function fromListPartial(n, list, hasStrings) { | |
4156 | var ret; | |
4157 | if (n < list.head.data.length) { | |
4158 | // slice is the same for buffers and strings | |
4159 | ret = list.head.data.slice(0, n); | |
4160 | list.head.data = list.head.data.slice(n); | |
4161 | } else if (n === list.head.data.length) { | |
4162 | // first chunk is a perfect match | |
4163 | ret = list.shift(); | |
4164 | } else { | |
4165 | // result spans more than one buffer | |
4166 | ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list); | |
4167 | } | |
4168 | return ret; | |
4169 | } | |
4170 | ||
4171 | // Copies a specified amount of characters from the list of buffered data | |
4172 | // chunks. | |
4173 | // This function is designed to be inlinable, so please take care when making | |
4174 | // changes to the function body. | |
4175 | function copyFromBufferString(n, list) { | |
4176 | var p = list.head; | |
4177 | var c = 1; | |
4178 | var ret = p.data; | |
4179 | n -= ret.length; | |
4180 | while (p = p.next) { | |
4181 | var str = p.data; | |
4182 | var nb = n > str.length ? str.length : n; | |
4183 | if (nb === str.length) ret += str;else ret += str.slice(0, n); | |
4184 | n -= nb; | |
4185 | if (n === 0) { | |
4186 | if (nb === str.length) { | |
4187 | ++c; | |
4188 | if (p.next) list.head = p.next;else list.head = list.tail = null; | |
4189 | } else { | |
4190 | list.head = p; | |
4191 | p.data = str.slice(nb); | |
4192 | } | |
4193 | break; | |
4194 | } | |
4195 | ++c; | |
4196 | } | |
4197 | list.length -= c; | |
4198 | return ret; | |
4199 | } | |
4200 | ||
4201 | // Copies a specified amount of bytes from the list of buffered data chunks. | |
4202 | // This function is designed to be inlinable, so please take care when making | |
4203 | // changes to the function body. | |
4204 | function copyFromBuffer(n, list) { | |
9f59e99b | 4205 | var ret = bufferShim.allocUnsafe(n); |
a0091a40 IC |
4206 | var p = list.head; |
4207 | var c = 1; | |
4208 | p.data.copy(ret); | |
4209 | n -= p.data.length; | |
4210 | while (p = p.next) { | |
4211 | var buf = p.data; | |
4212 | var nb = n > buf.length ? buf.length : n; | |
4213 | buf.copy(ret, ret.length - n, 0, nb); | |
4214 | n -= nb; | |
4215 | if (n === 0) { | |
4216 | if (nb === buf.length) { | |
4217 | ++c; | |
4218 | if (p.next) list.head = p.next;else list.head = list.tail = null; | |
4219 | } else { | |
4220 | list.head = p; | |
4221 | p.data = buf.slice(nb); | |
4222 | } | |
4223 | break; | |
4224 | } | |
4225 | ++c; | |
4226 | } | |
4227 | list.length -= c; | |
4228 | return ret; | |
4229 | } | |
4230 | ||
4231 | function endReadable(stream) { | |
4232 | var state = stream._readableState; | |
4233 | ||
4234 | // If we get here before consuming all the bytes, then that is a | |
4235 | // bug in node. Should never happen. | |
4236 | if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream'); | |
4237 | ||
4238 | if (!state.endEmitted) { | |
4239 | state.ended = true; | |
4240 | processNextTick(endReadableNT, state, stream); | |
4241 | } | |
4242 | } | |
4243 | ||
4244 | function endReadableNT(state, stream) { | |
4245 | // Check that we didn't get one last unshift. | |
4246 | if (!state.endEmitted && state.length === 0) { | |
4247 | state.endEmitted = true; | |
4248 | stream.readable = false; | |
4249 | stream.emit('end'); | |
4250 | } | |
4251 | } | |
4252 | ||
4253 | function forEach(xs, f) { | |
4254 | for (var i = 0, l = xs.length; i < l; i++) { | |
4255 | f(xs[i], i); | |
4256 | } | |
4257 | } | |
4258 | ||
4259 | function indexOf(xs, x) { | |
4260 | for (var i = 0, l = xs.length; i < l; i++) { | |
4261 | if (xs[i] === x) return i; | |
4262 | } | |
4263 | return -1; | |
4264 | } | |
9f59e99b IC |
4265 | }).call(this,require('_process')) |
4266 | },{"./_stream_duplex":15,"./internal/streams/BufferList":20,"./internal/streams/stream":21,"_process":13,"buffer":5,"buffer-shims":4,"core-util-is":6,"events":7,"inherits":9,"isarray":11,"process-nextick-args":12,"string_decoder/":22,"util":3}],18:[function(require,module,exports){ | |
a0091a40 IC |
4267 | // a transform stream is a readable/writable stream where you do |
4268 | // something with the data. Sometimes it's called a "filter", | |
4269 | // but that's not a great name for it, since that implies a thing where | |
4270 | // some bits pass through, and others are simply ignored. (That would | |
4271 | // be a valid example of a transform, of course.) | |
4272 | // | |
4273 | // While the output is causally related to the input, it's not a | |
4274 | // necessarily symmetric or synchronous transformation. For example, | |
4275 | // a zlib stream might take multiple plain-text writes(), and then | |
4276 | // emit a single compressed chunk some time in the future. | |
4277 | // | |
4278 | // Here's how this works: | |
4279 | // | |
4280 | // The Transform stream has all the aspects of the readable and writable | |
4281 | // stream classes. When you write(chunk), that calls _write(chunk,cb) | |
4282 | // internally, and returns false if there's a lot of pending writes | |
4283 | // buffered up. When you call read(), that calls _read(n) until | |
4284 | // there's enough pending readable data buffered up. | |
4285 | // | |
4286 | // In a transform stream, the written data is placed in a buffer. When | |
4287 | // _read(n) is called, it transforms the queued up data, calling the | |
4288 | // buffered _write cb's as it consumes chunks. If consuming a single | |
4289 | // written chunk would result in multiple output chunks, then the first | |
4290 | // outputted bit calls the readcb, and subsequent chunks just go into | |
4291 | // the read buffer, and will cause it to emit 'readable' if necessary. | |
4292 | // | |
4293 | // This way, back-pressure is actually determined by the reading side, | |
4294 | // since _read has to be called to start processing a new chunk. However, | |
4295 | // a pathological inflate type of transform can cause excessive buffering | |
4296 | // here. For example, imagine a stream where every byte of input is | |
4297 | // interpreted as an integer from 0-255, and then results in that many | |
4298 | // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in | |
4299 | // 1kb of data being output. In this case, you could write a very small | |
4300 | // amount of input, and end up with a very large amount of output. In | |
4301 | // such a pathological inflating mechanism, there'd be no way to tell | |
4302 | // the system to stop doing the transform. A single 4MB write could | |
4303 | // cause the system to run out of memory. | |
4304 | // | |
4305 | // However, even in such a pathological case, only a single written chunk | |
4306 | // would be consumed, and then the rest would wait (un-transformed) until | |
4307 | // the results of the previous transformed chunk were consumed. | |
4308 | ||
4309 | 'use strict'; | |
4310 | ||
4311 | module.exports = Transform; | |
4312 | ||
4313 | var Duplex = require('./_stream_duplex'); | |
4314 | ||
4315 | /*<replacement>*/ | |
4316 | var util = require('core-util-is'); | |
4317 | util.inherits = require('inherits'); | |
4318 | /*</replacement>*/ | |
4319 | ||
4320 | util.inherits(Transform, Duplex); | |
4321 | ||
4322 | function TransformState(stream) { | |
4323 | this.afterTransform = function (er, data) { | |
4324 | return afterTransform(stream, er, data); | |
4325 | }; | |
4326 | ||
4327 | this.needTransform = false; | |
4328 | this.transforming = false; | |
4329 | this.writecb = null; | |
4330 | this.writechunk = null; | |
4331 | this.writeencoding = null; | |
4332 | } | |
4333 | ||
4334 | function afterTransform(stream, er, data) { | |
4335 | var ts = stream._transformState; | |
4336 | ts.transforming = false; | |
4337 | ||
4338 | var cb = ts.writecb; | |
4339 | ||
9f59e99b | 4340 | if (!cb) return stream.emit('error', new Error('no writecb in Transform class')); |
a0091a40 IC |
4341 | |
4342 | ts.writechunk = null; | |
4343 | ts.writecb = null; | |
4344 | ||
4345 | if (data !== null && data !== undefined) stream.push(data); | |
4346 | ||
4347 | cb(er); | |
4348 | ||
4349 | var rs = stream._readableState; | |
4350 | rs.reading = false; | |
4351 | if (rs.needReadable || rs.length < rs.highWaterMark) { | |
4352 | stream._read(rs.highWaterMark); | |
4353 | } | |
4354 | } | |
4355 | ||
4356 | function Transform(options) { | |
4357 | if (!(this instanceof Transform)) return new Transform(options); | |
4358 | ||
4359 | Duplex.call(this, options); | |
4360 | ||
4361 | this._transformState = new TransformState(this); | |
4362 | ||
4363 | var stream = this; | |
4364 | ||
4365 | // start out asking for a readable event once data is transformed. | |
4366 | this._readableState.needReadable = true; | |
4367 | ||
4368 | // we have implemented the _read method, and done the other things | |
4369 | // that Readable wants before the first _read call, so unset the | |
4370 | // sync guard flag. | |
4371 | this._readableState.sync = false; | |
4372 | ||
4373 | if (options) { | |
4374 | if (typeof options.transform === 'function') this._transform = options.transform; | |
4375 | ||
4376 | if (typeof options.flush === 'function') this._flush = options.flush; | |
4377 | } | |
4378 | ||
4379 | // When the writable side finishes, then flush out anything remaining. | |
4380 | this.once('prefinish', function () { | |
4381 | if (typeof this._flush === 'function') this._flush(function (er, data) { | |
4382 | done(stream, er, data); | |
4383 | });else done(stream); | |
4384 | }); | |
4385 | } | |
4386 | ||
4387 | Transform.prototype.push = function (chunk, encoding) { | |
4388 | this._transformState.needTransform = false; | |
4389 | return Duplex.prototype.push.call(this, chunk, encoding); | |
4390 | }; | |
4391 | ||
4392 | // This is the part where you do stuff! | |
4393 | // override this function in implementation classes. | |
4394 | // 'chunk' is an input chunk. | |
4395 | // | |
4396 | // Call `push(newChunk)` to pass along transformed output | |
4397 | // to the readable side. You may call 'push' zero or more times. | |
4398 | // | |
4399 | // Call `cb(err)` when you are done with this chunk. If you pass | |
4400 | // an error, then that'll put the hurt on the whole operation. If you | |
4401 | // never call cb(), then you'll never get another chunk. | |
4402 | Transform.prototype._transform = function (chunk, encoding, cb) { | |
4403 | throw new Error('_transform() is not implemented'); | |
4404 | }; | |
4405 | ||
4406 | Transform.prototype._write = function (chunk, encoding, cb) { | |
4407 | var ts = this._transformState; | |
4408 | ts.writecb = cb; | |
4409 | ts.writechunk = chunk; | |
4410 | ts.writeencoding = encoding; | |
4411 | if (!ts.transforming) { | |
4412 | var rs = this._readableState; | |
4413 | if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark); | |
4414 | } | |
4415 | }; | |
4416 | ||
4417 | // Doesn't matter what the args are here. | |
4418 | // _transform does all the work. | |
4419 | // That we got here means that the readable side wants more data. | |
4420 | Transform.prototype._read = function (n) { | |
4421 | var ts = this._transformState; | |
4422 | ||
4423 | if (ts.writechunk !== null && ts.writecb && !ts.transforming) { | |
4424 | ts.transforming = true; | |
4425 | this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); | |
4426 | } else { | |
4427 | // mark that we need a transform, so that any data that comes in | |
4428 | // will get processed, now that we've asked for it. | |
4429 | ts.needTransform = true; | |
4430 | } | |
4431 | }; | |
4432 | ||
a0091a40 IC |
4433 | function done(stream, er, data) { |
4434 | if (er) return stream.emit('error', er); | |
4435 | ||
4436 | if (data !== null && data !== undefined) stream.push(data); | |
4437 | ||
4438 | // if there's nothing in the write buffer, then that means | |
4439 | // that nothing more will ever be provided | |
4440 | var ws = stream._writableState; | |
4441 | var ts = stream._transformState; | |
4442 | ||
4443 | if (ws.length) throw new Error('Calling transform done when ws.length != 0'); | |
4444 | ||
4445 | if (ts.transforming) throw new Error('Calling transform done when still transforming'); | |
4446 | ||
4447 | return stream.push(null); | |
4448 | } | |
9f59e99b IC |
4449 | },{"./_stream_duplex":15,"core-util-is":6,"inherits":9}],19:[function(require,module,exports){ |
4450 | (function (process){ | |
a0091a40 IC |
4451 | // A bit simpler than readable streams. |
4452 | // Implement an async ._write(chunk, encoding, cb), and it'll handle all | |
4453 | // the drain event emission and buffering. | |
4454 | ||
4455 | 'use strict'; | |
4456 | ||
9f59e99b | 4457 | module.exports = Writable; |
a0091a40 | 4458 | |
9f59e99b | 4459 | /*<replacement>*/ |
a0091a40 IC |
4460 | var processNextTick = require('process-nextick-args'); |
4461 | /*</replacement>*/ | |
4462 | ||
a0091a40 IC |
4463 | /*<replacement>*/ |
4464 | var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : processNextTick; | |
4465 | /*</replacement>*/ | |
4466 | ||
4467 | /*<replacement>*/ | |
4468 | var Duplex; | |
4469 | /*</replacement>*/ | |
4470 | ||
4471 | Writable.WritableState = WritableState; | |
4472 | ||
4473 | /*<replacement>*/ | |
4474 | var util = require('core-util-is'); | |
4475 | util.inherits = require('inherits'); | |
4476 | /*</replacement>*/ | |
4477 | ||
4478 | /*<replacement>*/ | |
4479 | var internalUtil = { | |
4480 | deprecate: require('util-deprecate') | |
4481 | }; | |
4482 | /*</replacement>*/ | |
4483 | ||
4484 | /*<replacement>*/ | |
4485 | var Stream = require('./internal/streams/stream'); | |
4486 | /*</replacement>*/ | |
4487 | ||
9f59e99b | 4488 | var Buffer = require('buffer').Buffer; |
a0091a40 | 4489 | /*<replacement>*/ |
9f59e99b | 4490 | var bufferShim = require('buffer-shims'); |
a0091a40 IC |
4491 | /*</replacement>*/ |
4492 | ||
a0091a40 IC |
4493 | util.inherits(Writable, Stream); |
4494 | ||
4495 | function nop() {} | |
4496 | ||
9f59e99b IC |
4497 | function WriteReq(chunk, encoding, cb) { |
4498 | this.chunk = chunk; | |
4499 | this.encoding = encoding; | |
4500 | this.callback = cb; | |
4501 | this.next = null; | |
4502 | } | |
4503 | ||
a0091a40 IC |
4504 | function WritableState(options, stream) { |
4505 | Duplex = Duplex || require('./_stream_duplex'); | |
4506 | ||
4507 | options = options || {}; | |
4508 | ||
4509 | // object stream flag to indicate whether or not this stream | |
4510 | // contains buffers or objects. | |
4511 | this.objectMode = !!options.objectMode; | |
4512 | ||
4513 | if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.writableObjectMode; | |
4514 | ||
4515 | // the point at which write() starts returning false | |
4516 | // Note: 0 is a valid value, means that we always return false if | |
4517 | // the entire buffer is not flushed immediately on write() | |
4518 | var hwm = options.highWaterMark; | |
4519 | var defaultHwm = this.objectMode ? 16 : 16 * 1024; | |
4520 | this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; | |
4521 | ||
4522 | // cast to ints. | |
9f59e99b | 4523 | this.highWaterMark = ~~this.highWaterMark; |
a0091a40 IC |
4524 | |
4525 | // drain event flag. | |
4526 | this.needDrain = false; | |
4527 | // at the start of calling end() | |
4528 | this.ending = false; | |
4529 | // when end() has been called, and returned | |
4530 | this.ended = false; | |
4531 | // when 'finish' is emitted | |
4532 | this.finished = false; | |
4533 | ||
a0091a40 IC |
4534 | // should we decode strings into buffers before passing to _write? |
4535 | // this is here so that some node-core streams can optimize string | |
4536 | // handling at a lower level. | |
4537 | var noDecode = options.decodeStrings === false; | |
4538 | this.decodeStrings = !noDecode; | |
4539 | ||
4540 | // Crypto is kind of old and crusty. Historically, its default string | |
4541 | // encoding is 'binary' so we have to make this configurable. | |
4542 | // Everything else in the universe uses 'utf8', though. | |
4543 | this.defaultEncoding = options.defaultEncoding || 'utf8'; | |
4544 | ||
4545 | // not an actual buffer we keep track of, but a measurement | |
4546 | // of how much we're waiting to get pushed to some underlying | |
4547 | // socket or file. | |
4548 | this.length = 0; | |
4549 | ||
4550 | // a flag to see when we're in the middle of a write. | |
4551 | this.writing = false; | |
4552 | ||
4553 | // when true all writes will be buffered until .uncork() call | |
4554 | this.corked = 0; | |
4555 | ||
4556 | // a flag to be able to tell if the onwrite cb is called immediately, | |
4557 | // or on a later tick. We set this to true at first, because any | |
4558 | // actions that shouldn't happen until "later" should generally also | |
4559 | // not happen before the first write call. | |
4560 | this.sync = true; | |
4561 | ||
4562 | // a flag to know if we're processing previously buffered items, which | |
4563 | // may call the _write() callback in the same tick, so that we don't | |
4564 | // end up in an overlapped onwrite situation. | |
4565 | this.bufferProcessing = false; | |
4566 | ||
4567 | // the callback that's passed to _write(chunk,cb) | |
4568 | this.onwrite = function (er) { | |
4569 | onwrite(stream, er); | |
4570 | }; | |
4571 | ||
4572 | // the callback that the user supplies to write(chunk,encoding,cb) | |
4573 | this.writecb = null; | |
4574 | ||
4575 | // the amount that is being written when _write is called. | |
4576 | this.writelen = 0; | |
4577 | ||
4578 | this.bufferedRequest = null; | |
4579 | this.lastBufferedRequest = null; | |
4580 | ||
4581 | // number of pending user-supplied write callbacks | |
4582 | // this must be 0 before 'finish' can be emitted | |
4583 | this.pendingcb = 0; | |
4584 | ||
4585 | // emit prefinish if the only thing we're waiting for is _write cbs | |
4586 | // This is relevant for synchronous Transform streams | |
4587 | this.prefinished = false; | |
4588 | ||
4589 | // True if the error was already emitted and should not be thrown again | |
4590 | this.errorEmitted = false; | |
4591 | ||
4592 | // count buffered requests | |
4593 | this.bufferedRequestCount = 0; | |
4594 | ||
4595 | // allocate the first CorkedRequest, there is always | |
4596 | // one allocated and free to use, and we maintain at most two | |
4597 | this.corkedRequestsFree = new CorkedRequest(this); | |
4598 | } | |
4599 | ||
4600 | WritableState.prototype.getBuffer = function getBuffer() { | |
4601 | var current = this.bufferedRequest; | |
4602 | var out = []; | |
4603 | while (current) { | |
4604 | out.push(current); | |
4605 | current = current.next; | |
4606 | } | |
4607 | return out; | |
4608 | }; | |
4609 | ||
4610 | (function () { | |
4611 | try { | |
4612 | Object.defineProperty(WritableState.prototype, 'buffer', { | |
4613 | get: internalUtil.deprecate(function () { | |
4614 | return this.getBuffer(); | |
9f59e99b | 4615 | }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.') |
a0091a40 IC |
4616 | }); |
4617 | } catch (_) {} | |
4618 | })(); | |
4619 | ||
4620 | // Test _writableState for inheritance to account for Duplex streams, | |
4621 | // whose prototype chain only points to Readable. | |
4622 | var realHasInstance; | |
4623 | if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') { | |
4624 | realHasInstance = Function.prototype[Symbol.hasInstance]; | |
4625 | Object.defineProperty(Writable, Symbol.hasInstance, { | |
4626 | value: function (object) { | |
4627 | if (realHasInstance.call(this, object)) return true; | |
4628 | ||
4629 | return object && object._writableState instanceof WritableState; | |
4630 | } | |
4631 | }); | |
4632 | } else { | |
4633 | realHasInstance = function (object) { | |
4634 | return object instanceof this; | |
4635 | }; | |
4636 | } | |
4637 | ||
4638 | function Writable(options) { | |
4639 | Duplex = Duplex || require('./_stream_duplex'); | |
4640 | ||
4641 | // Writable ctor is applied to Duplexes, too. | |
4642 | // `realHasInstance` is necessary because using plain `instanceof` | |
4643 | // would return false, as no `_writableState` property is attached. | |
4644 | ||
4645 | // Trying to use the custom `instanceof` for Writable here will also break the | |
4646 | // Node.js LazyTransform implementation, which has a non-trivial getter for | |
4647 | // `_writableState` that would lead to infinite recursion. | |
4648 | if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) { | |
4649 | return new Writable(options); | |
4650 | } | |
4651 | ||
4652 | this._writableState = new WritableState(options, this); | |
4653 | ||
4654 | // legacy. | |
4655 | this.writable = true; | |
4656 | ||
4657 | if (options) { | |
4658 | if (typeof options.write === 'function') this._write = options.write; | |
4659 | ||
4660 | if (typeof options.writev === 'function') this._writev = options.writev; | |
a0091a40 IC |
4661 | } |
4662 | ||
4663 | Stream.call(this); | |
4664 | } | |
4665 | ||
4666 | // Otherwise people can pipe Writable streams, which is just wrong. | |
4667 | Writable.prototype.pipe = function () { | |
4668 | this.emit('error', new Error('Cannot pipe, not readable')); | |
4669 | }; | |
4670 | ||
4671 | function writeAfterEnd(stream, cb) { | |
4672 | var er = new Error('write after end'); | |
4673 | // TODO: defer error events consistently everywhere, not just the cb | |
4674 | stream.emit('error', er); | |
4675 | processNextTick(cb, er); | |
4676 | } | |
4677 | ||
4678 | // Checks that a user-supplied chunk is valid, especially for the particular | |
4679 | // mode the stream is in. Currently this means that `null` is never accepted | |
4680 | // and undefined/non-string values are only allowed in object mode. | |
4681 | function validChunk(stream, state, chunk, cb) { | |
4682 | var valid = true; | |
4683 | var er = false; | |
4684 | ||
4685 | if (chunk === null) { | |
4686 | er = new TypeError('May not write null values to stream'); | |
4687 | } else if (typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { | |
4688 | er = new TypeError('Invalid non-string/buffer chunk'); | |
4689 | } | |
4690 | if (er) { | |
4691 | stream.emit('error', er); | |
4692 | processNextTick(cb, er); | |
4693 | valid = false; | |
4694 | } | |
4695 | return valid; | |
4696 | } | |
4697 | ||
4698 | Writable.prototype.write = function (chunk, encoding, cb) { | |
4699 | var state = this._writableState; | |
4700 | var ret = false; | |
9f59e99b | 4701 | var isBuf = Buffer.isBuffer(chunk); |
a0091a40 IC |
4702 | |
4703 | if (typeof encoding === 'function') { | |
4704 | cb = encoding; | |
4705 | encoding = null; | |
4706 | } | |
4707 | ||
4708 | if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding; | |
4709 | ||
4710 | if (typeof cb !== 'function') cb = nop; | |
4711 | ||
4712 | if (state.ended) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) { | |
4713 | state.pendingcb++; | |
4714 | ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb); | |
4715 | } | |
4716 | ||
4717 | return ret; | |
4718 | }; | |
4719 | ||
4720 | Writable.prototype.cork = function () { | |
4721 | var state = this._writableState; | |
4722 | ||
4723 | state.corked++; | |
4724 | }; | |
4725 | ||
4726 | Writable.prototype.uncork = function () { | |
4727 | var state = this._writableState; | |
4728 | ||
4729 | if (state.corked) { | |
4730 | state.corked--; | |
4731 | ||
4732 | if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state); | |
4733 | } | |
4734 | }; | |
4735 | ||
4736 | Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) { | |
4737 | // node::ParseEncoding() requires lower case. | |
4738 | if (typeof encoding === 'string') encoding = encoding.toLowerCase(); | |
4739 | if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding); | |
4740 | this._writableState.defaultEncoding = encoding; | |
4741 | return this; | |
4742 | }; | |
4743 | ||
4744 | function decodeChunk(state, chunk, encoding) { | |
4745 | if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') { | |
9f59e99b | 4746 | chunk = bufferShim.from(chunk, encoding); |
a0091a40 IC |
4747 | } |
4748 | return chunk; | |
4749 | } | |
4750 | ||
4751 | // if we're already writing something, then just put this | |
4752 | // in the queue, and wait our turn. Otherwise, call _write | |
4753 | // If we return false, then we need a drain event, so set that flag. | |
4754 | function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) { | |
4755 | if (!isBuf) { | |
9f59e99b IC |
4756 | chunk = decodeChunk(state, chunk, encoding); |
4757 | if (Buffer.isBuffer(chunk)) encoding = 'buffer'; | |
a0091a40 IC |
4758 | } |
4759 | var len = state.objectMode ? 1 : chunk.length; | |
4760 | ||
4761 | state.length += len; | |
4762 | ||
4763 | var ret = state.length < state.highWaterMark; | |
4764 | // we must ensure that previous needDrain will not be reset to false. | |
4765 | if (!ret) state.needDrain = true; | |
4766 | ||
4767 | if (state.writing || state.corked) { | |
4768 | var last = state.lastBufferedRequest; | |
9f59e99b | 4769 | state.lastBufferedRequest = new WriteReq(chunk, encoding, cb); |
a0091a40 IC |
4770 | if (last) { |
4771 | last.next = state.lastBufferedRequest; | |
4772 | } else { | |
4773 | state.bufferedRequest = state.lastBufferedRequest; | |
4774 | } | |
4775 | state.bufferedRequestCount += 1; | |
4776 | } else { | |
4777 | doWrite(stream, state, false, len, chunk, encoding, cb); | |
4778 | } | |
4779 | ||
4780 | return ret; | |
4781 | } | |
4782 | ||
4783 | function doWrite(stream, state, writev, len, chunk, encoding, cb) { | |
4784 | state.writelen = len; | |
4785 | state.writecb = cb; | |
4786 | state.writing = true; | |
4787 | state.sync = true; | |
4788 | if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite); | |
4789 | state.sync = false; | |
4790 | } | |
4791 | ||
4792 | function onwriteError(stream, state, sync, er, cb) { | |
4793 | --state.pendingcb; | |
9f59e99b | 4794 | if (sync) processNextTick(cb, er);else cb(er); |
a0091a40 | 4795 | |
9f59e99b IC |
4796 | stream._writableState.errorEmitted = true; |
4797 | stream.emit('error', er); | |
a0091a40 IC |
4798 | } |
4799 | ||
4800 | function onwriteStateUpdate(state) { | |
4801 | state.writing = false; | |
4802 | state.writecb = null; | |
4803 | state.length -= state.writelen; | |
4804 | state.writelen = 0; | |
4805 | } | |
4806 | ||
4807 | function onwrite(stream, er) { | |
4808 | var state = stream._writableState; | |
4809 | var sync = state.sync; | |
4810 | var cb = state.writecb; | |
4811 | ||
4812 | onwriteStateUpdate(state); | |
4813 | ||
4814 | if (er) onwriteError(stream, state, sync, er, cb);else { | |
4815 | // Check if we're actually ready to finish, but don't emit yet | |
4816 | var finished = needFinish(state); | |
4817 | ||
4818 | if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) { | |
4819 | clearBuffer(stream, state); | |
4820 | } | |
4821 | ||
4822 | if (sync) { | |
4823 | /*<replacement>*/ | |
4824 | asyncWrite(afterWrite, stream, state, finished, cb); | |
4825 | /*</replacement>*/ | |
4826 | } else { | |
4827 | afterWrite(stream, state, finished, cb); | |
4828 | } | |
4829 | } | |
4830 | } | |
4831 | ||
4832 | function afterWrite(stream, state, finished, cb) { | |
4833 | if (!finished) onwriteDrain(stream, state); | |
4834 | state.pendingcb--; | |
4835 | cb(); | |
4836 | finishMaybe(stream, state); | |
4837 | } | |
4838 | ||
4839 | // Must force callback to be called on nextTick, so that we don't | |
4840 | // emit 'drain' before the write() consumer gets the 'false' return | |
4841 | // value, and has a chance to attach a 'drain' listener. | |
4842 | function onwriteDrain(stream, state) { | |
4843 | if (state.length === 0 && state.needDrain) { | |
4844 | state.needDrain = false; | |
4845 | stream.emit('drain'); | |
4846 | } | |
4847 | } | |
4848 | ||
4849 | // if there's something in the buffer waiting, then process it | |
4850 | function clearBuffer(stream, state) { | |
4851 | state.bufferProcessing = true; | |
4852 | var entry = state.bufferedRequest; | |
4853 | ||
4854 | if (stream._writev && entry && entry.next) { | |
4855 | // Fast case, write everything using _writev() | |
4856 | var l = state.bufferedRequestCount; | |
4857 | var buffer = new Array(l); | |
4858 | var holder = state.corkedRequestsFree; | |
4859 | holder.entry = entry; | |
4860 | ||
4861 | var count = 0; | |
a0091a40 IC |
4862 | while (entry) { |
4863 | buffer[count] = entry; | |
a0091a40 IC |
4864 | entry = entry.next; |
4865 | count += 1; | |
4866 | } | |
a0091a40 IC |
4867 | |
4868 | doWrite(stream, state, true, state.length, buffer, '', holder.finish); | |
4869 | ||
4870 | // doWrite is almost always async, defer these to save a bit of time | |
4871 | // as the hot path ends with doWrite | |
4872 | state.pendingcb++; | |
4873 | state.lastBufferedRequest = null; | |
4874 | if (holder.next) { | |
4875 | state.corkedRequestsFree = holder.next; | |
4876 | holder.next = null; | |
4877 | } else { | |
4878 | state.corkedRequestsFree = new CorkedRequest(state); | |
4879 | } | |
4880 | } else { | |
4881 | // Slow case, write chunks one-by-one | |
4882 | while (entry) { | |
4883 | var chunk = entry.chunk; | |
4884 | var encoding = entry.encoding; | |
4885 | var cb = entry.callback; | |
4886 | var len = state.objectMode ? 1 : chunk.length; | |
4887 | ||
4888 | doWrite(stream, state, false, len, chunk, encoding, cb); | |
4889 | entry = entry.next; | |
4890 | // if we didn't call the onwrite immediately, then | |
4891 | // it means that we need to wait until it does. | |
4892 | // also, that means that the chunk and cb are currently | |
4893 | // being processed, so move the buffer counter past them. | |
4894 | if (state.writing) { | |
4895 | break; | |
4896 | } | |
4897 | } | |
4898 | ||
4899 | if (entry === null) state.lastBufferedRequest = null; | |
4900 | } | |
4901 | ||
4902 | state.bufferedRequestCount = 0; | |
4903 | state.bufferedRequest = entry; | |
4904 | state.bufferProcessing = false; | |
4905 | } | |
4906 | ||
4907 | Writable.prototype._write = function (chunk, encoding, cb) { | |
4908 | cb(new Error('_write() is not implemented')); | |
4909 | }; | |
4910 | ||
4911 | Writable.prototype._writev = null; | |
4912 | ||
4913 | Writable.prototype.end = function (chunk, encoding, cb) { | |
4914 | var state = this._writableState; | |
4915 | ||
4916 | if (typeof chunk === 'function') { | |
4917 | cb = chunk; | |
4918 | chunk = null; | |
4919 | encoding = null; | |
4920 | } else if (typeof encoding === 'function') { | |
4921 | cb = encoding; | |
4922 | encoding = null; | |
4923 | } | |
4924 | ||
4925 | if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); | |
4926 | ||
4927 | // .end() fully uncorks | |
4928 | if (state.corked) { | |
4929 | state.corked = 1; | |
4930 | this.uncork(); | |
4931 | } | |
4932 | ||
4933 | // ignore unnecessary end() calls. | |
4934 | if (!state.ending && !state.finished) endWritable(this, state, cb); | |
4935 | }; | |
4936 | ||
4937 | function needFinish(state) { | |
4938 | return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing; | |
4939 | } | |
9f59e99b IC |
4940 | |
4941 | function prefinish(stream, state) { | |
4942 | if (!state.prefinished) { | |
a0091a40 IC |
4943 | state.prefinished = true; |
4944 | stream.emit('prefinish'); | |
a0091a40 IC |
4945 | } |
4946 | } | |
4947 | ||
4948 | function finishMaybe(stream, state) { | |
4949 | var need = needFinish(state); | |
4950 | if (need) { | |
a0091a40 | 4951 | if (state.pendingcb === 0) { |
9f59e99b | 4952 | prefinish(stream, state); |
a0091a40 IC |
4953 | state.finished = true; |
4954 | stream.emit('finish'); | |
9f59e99b IC |
4955 | } else { |
4956 | prefinish(stream, state); | |
a0091a40 IC |
4957 | } |
4958 | } | |
4959 | return need; | |
4960 | } | |
4961 | ||
4962 | function endWritable(stream, state, cb) { | |
4963 | state.ending = true; | |
4964 | finishMaybe(stream, state); | |
4965 | if (cb) { | |
4966 | if (state.finished) processNextTick(cb);else stream.once('finish', cb); | |
4967 | } | |
4968 | state.ended = true; | |
4969 | stream.writable = false; | |
4970 | } | |
4971 | ||
9f59e99b IC |
4972 | // It seems a linked list but it is not |
4973 | // there will be only 2 of these for each stream | |
4974 | function CorkedRequest(state) { | |
4975 | var _this = this; | |
a0091a40 | 4976 | |
9f59e99b IC |
4977 | this.next = null; |
4978 | this.entry = null; | |
4979 | this.finish = function (err) { | |
4980 | var entry = _this.entry; | |
4981 | _this.entry = null; | |
4982 | while (entry) { | |
4983 | var cb = entry.callback; | |
4984 | state.pendingcb--; | |
4985 | cb(err); | |
4986 | entry = entry.next; | |
a0091a40 | 4987 | } |
9f59e99b IC |
4988 | if (state.corkedRequestsFree) { |
4989 | state.corkedRequestsFree.next = _this; | |
4990 | } else { | |
4991 | state.corkedRequestsFree = _this; | |
a0091a40 | 4992 | } |
9f59e99b IC |
4993 | }; |
4994 | } | |
4995 | }).call(this,require('_process')) | |
4996 | },{"./_stream_duplex":15,"./internal/streams/stream":21,"_process":13,"buffer":5,"buffer-shims":4,"core-util-is":6,"inherits":9,"process-nextick-args":12,"util-deprecate":30}],20:[function(require,module,exports){ | |
a0091a40 IC |
4997 | 'use strict'; |
4998 | ||
9f59e99b | 4999 | var Buffer = require('buffer').Buffer; |
a0091a40 | 5000 | /*<replacement>*/ |
9f59e99b | 5001 | var bufferShim = require('buffer-shims'); |
a0091a40 IC |
5002 | /*</replacement>*/ |
5003 | ||
9f59e99b | 5004 | module.exports = BufferList; |
a0091a40 | 5005 | |
9f59e99b IC |
5006 | function BufferList() { |
5007 | this.head = null; | |
5008 | this.tail = null; | |
5009 | this.length = 0; | |
5010 | } | |
a0091a40 | 5011 | |
9f59e99b IC |
5012 | BufferList.prototype.push = function (v) { |
5013 | var entry = { data: v, next: null }; | |
5014 | if (this.length > 0) this.tail.next = entry;else this.head = entry; | |
5015 | this.tail = entry; | |
5016 | ++this.length; | |
5017 | }; | |
a0091a40 | 5018 | |
9f59e99b IC |
5019 | BufferList.prototype.unshift = function (v) { |
5020 | var entry = { data: v, next: this.head }; | |
5021 | if (this.length === 0) this.tail = entry; | |
5022 | this.head = entry; | |
5023 | ++this.length; | |
5024 | }; | |
a0091a40 | 5025 | |
9f59e99b IC |
5026 | BufferList.prototype.shift = function () { |
5027 | if (this.length === 0) return; | |
5028 | var ret = this.head.data; | |
5029 | if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next; | |
5030 | --this.length; | |
5031 | return ret; | |
5032 | }; | |
a0091a40 | 5033 | |
9f59e99b IC |
5034 | BufferList.prototype.clear = function () { |
5035 | this.head = this.tail = null; | |
5036 | this.length = 0; | |
5037 | }; | |
a0091a40 | 5038 | |
9f59e99b IC |
5039 | BufferList.prototype.join = function (s) { |
5040 | if (this.length === 0) return ''; | |
5041 | var p = this.head; | |
5042 | var ret = '' + p.data; | |
5043 | while (p = p.next) { | |
5044 | ret += s + p.data; | |
5045 | }return ret; | |
5046 | }; | |
a0091a40 | 5047 | |
9f59e99b IC |
5048 | BufferList.prototype.concat = function (n) { |
5049 | if (this.length === 0) return bufferShim.alloc(0); | |
5050 | if (this.length === 1) return this.head.data; | |
5051 | var ret = bufferShim.allocUnsafe(n >>> 0); | |
5052 | var p = this.head; | |
5053 | var i = 0; | |
5054 | while (p) { | |
5055 | p.data.copy(ret, i); | |
5056 | i += p.data.length; | |
5057 | p = p.next; | |
a0091a40 | 5058 | } |
9f59e99b IC |
5059 | return ret; |
5060 | }; | |
5061 | },{"buffer":5,"buffer-shims":4}],21:[function(require,module,exports){ | |
5062 | module.exports = require('events').EventEmitter; | |
a0091a40 | 5063 | |
9f59e99b IC |
5064 | },{"events":7}],22:[function(require,module,exports){ |
5065 | 'use strict'; | |
a0091a40 | 5066 | |
9f59e99b | 5067 | var Buffer = require('safe-buffer').Buffer; |
a0091a40 | 5068 | |
9f59e99b IC |
5069 | var isEncoding = Buffer.isEncoding || function (encoding) { |
5070 | encoding = '' + encoding; | |
5071 | switch (encoding && encoding.toLowerCase()) { | |
5072 | case 'hex':case 'utf8':case 'utf-8':case 'ascii':case 'binary':case 'base64':case 'ucs2':case 'ucs-2':case 'utf16le':case 'utf-16le':case 'raw': | |
5073 | return true; | |
5074 | default: | |
5075 | return false; | |
a0091a40 | 5076 | } |
9f59e99b | 5077 | }; |
a0091a40 | 5078 | |
9f59e99b IC |
5079 | function _normalizeEncoding(enc) { |
5080 | if (!enc) return 'utf8'; | |
5081 | var retried; | |
5082 | while (true) { | |
5083 | switch (enc) { | |
5084 | case 'utf8': | |
5085 | case 'utf-8': | |
5086 | return 'utf8'; | |
5087 | case 'ucs2': | |
5088 | case 'ucs-2': | |
5089 | case 'utf16le': | |
5090 | case 'utf-16le': | |
5091 | return 'utf16le'; | |
5092 | case 'latin1': | |
5093 | case 'binary': | |
5094 | return 'latin1'; | |
5095 | case 'base64': | |
5096 | case 'ascii': | |
5097 | case 'hex': | |
5098 | return enc; | |
5099 | default: | |
5100 | if (retried) return; // undefined | |
5101 | enc = ('' + enc).toLowerCase(); | |
5102 | retried = true; | |
a0091a40 | 5103 | } |
a0091a40 | 5104 | } |
9f59e99b | 5105 | }; |
a0091a40 | 5106 | |
9f59e99b IC |
5107 | // Do not cache `Buffer.isEncoding` when checking encoding names as some |
5108 | // modules monkey-patch it to support additional encodings | |
5109 | function normalizeEncoding(enc) { | |
5110 | var nenc = _normalizeEncoding(enc); | |
5111 | if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc); | |
5112 | return nenc || enc; | |
a0091a40 IC |
5113 | } |
5114 | ||
9f59e99b IC |
5115 | // StringDecoder provides an interface for efficiently splitting a series of |
5116 | // buffers into a series of JS strings without breaking apart multi-byte | |
5117 | // characters. | |
5118 | exports.StringDecoder = StringDecoder; | |
5119 | function StringDecoder(encoding) { | |
5120 | this.encoding = normalizeEncoding(encoding); | |
5121 | var nb; | |
5122 | switch (this.encoding) { | |
5123 | case 'utf16le': | |
5124 | this.text = utf16Text; | |
5125 | this.end = utf16End; | |
5126 | nb = 4; | |
5127 | break; | |
5128 | case 'utf8': | |
5129 | this.fillLast = utf8FillLast; | |
5130 | nb = 4; | |
5131 | break; | |
5132 | case 'base64': | |
5133 | this.text = base64Text; | |
5134 | this.end = base64End; | |
5135 | nb = 3; | |
5136 | break; | |
5137 | default: | |
5138 | this.write = simpleWrite; | |
5139 | this.end = simpleEnd; | |
5140 | return; | |
5141 | } | |
5142 | this.lastNeed = 0; | |
5143 | this.lastTotal = 0; | |
5144 | this.lastChar = Buffer.allocUnsafe(nb); | |
a0091a40 IC |
5145 | } |
5146 | ||
9f59e99b IC |
5147 | StringDecoder.prototype.write = function (buf) { |
5148 | if (buf.length === 0) return ''; | |
5149 | var r; | |
5150 | var i; | |
5151 | if (this.lastNeed) { | |
5152 | r = this.fillLast(buf); | |
5153 | if (r === undefined) return ''; | |
5154 | i = this.lastNeed; | |
5155 | this.lastNeed = 0; | |
5156 | } else { | |
5157 | i = 0; | |
5158 | } | |
5159 | if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i); | |
5160 | return r || ''; | |
a0091a40 | 5161 | }; |
a0091a40 | 5162 | |
9f59e99b | 5163 | StringDecoder.prototype.end = utf8End; |
a0091a40 | 5164 | |
9f59e99b IC |
5165 | // Returns only complete characters in a Buffer |
5166 | StringDecoder.prototype.text = utf8Text; | |
a0091a40 | 5167 | |
9f59e99b IC |
5168 | // Attempts to complete a partial non-UTF-8 character using bytes from a Buffer |
5169 | StringDecoder.prototype.fillLast = function (buf) { | |
5170 | if (this.lastNeed <= buf.length) { | |
5171 | buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed); | |
5172 | return this.lastChar.toString(this.encoding, 0, this.lastTotal); | |
5173 | } | |
5174 | buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length); | |
5175 | this.lastNeed -= buf.length; | |
5176 | }; | |
a0091a40 | 5177 | |
9f59e99b IC |
5178 | // Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a |
5179 | // continuation byte. | |
5180 | function utf8CheckByte(byte) { | |
5181 | if (byte <= 0x7F) return 0;else if (byte >> 5 === 0x06) return 2;else if (byte >> 4 === 0x0E) return 3;else if (byte >> 3 === 0x1E) return 4; | |
5182 | return -1; | |
5183 | } | |
a0091a40 | 5184 | |
9f59e99b IC |
5185 | // Checks at most 3 bytes at the end of a Buffer in order to detect an |
5186 | // incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4) | |
5187 | // needed to complete the UTF-8 character (if applicable) are returned. | |
5188 | function utf8CheckIncomplete(self, buf, i) { | |
5189 | var j = buf.length - 1; | |
5190 | if (j < i) return 0; | |
5191 | var nb = utf8CheckByte(buf[j]); | |
5192 | if (nb >= 0) { | |
5193 | if (nb > 0) self.lastNeed = nb - 1; | |
5194 | return nb; | |
5195 | } | |
5196 | if (--j < i) return 0; | |
5197 | nb = utf8CheckByte(buf[j]); | |
5198 | if (nb >= 0) { | |
5199 | if (nb > 0) self.lastNeed = nb - 2; | |
5200 | return nb; | |
5201 | } | |
5202 | if (--j < i) return 0; | |
5203 | nb = utf8CheckByte(buf[j]); | |
5204 | if (nb >= 0) { | |
5205 | if (nb > 0) { | |
5206 | if (nb === 2) nb = 0;else self.lastNeed = nb - 3; | |
5207 | } | |
5208 | return nb; | |
a0091a40 | 5209 | } |
9f59e99b | 5210 | return 0; |
a0091a40 | 5211 | } |
9f59e99b IC |
5212 | |
5213 | // Validates as many continuation bytes for a multi-byte UTF-8 character as | |
5214 | // needed or are available. If we see a non-continuation byte where we expect | |
5215 | // one, we "replace" the validated continuation bytes we've seen so far with | |
5216 | // UTF-8 replacement characters ('\ufffd'), to match v8's UTF-8 decoding | |
5217 | // behavior. The continuation byte check is included three times in the case | |
5218 | // where all of the continuation bytes for a character exist in the same buffer. | |
5219 | // It is also done this way as a slight performance increase instead of using a | |
5220 | // loop. | |
5221 | function utf8CheckExtraBytes(self, buf, p) { | |
5222 | if ((buf[0] & 0xC0) !== 0x80) { | |
5223 | self.lastNeed = 0; | |
5224 | return '\ufffd'.repeat(p); | |
5225 | } | |
5226 | if (self.lastNeed > 1 && buf.length > 1) { | |
5227 | if ((buf[1] & 0xC0) !== 0x80) { | |
5228 | self.lastNeed = 1; | |
5229 | return '\ufffd'.repeat(p + 1); | |
5230 | } | |
5231 | if (self.lastNeed > 2 && buf.length > 2) { | |
5232 | if ((buf[2] & 0xC0) !== 0x80) { | |
5233 | self.lastNeed = 2; | |
5234 | return '\ufffd'.repeat(p + 2); | |
5235 | } | |
5236 | } | |
5237 | } | |
a0091a40 IC |
5238 | } |
5239 | ||
9f59e99b IC |
5240 | // Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer. |
5241 | function utf8FillLast(buf) { | |
5242 | var p = this.lastTotal - this.lastNeed; | |
5243 | var r = utf8CheckExtraBytes(this, buf, p); | |
5244 | if (r !== undefined) return r; | |
5245 | if (this.lastNeed <= buf.length) { | |
5246 | buf.copy(this.lastChar, p, 0, this.lastNeed); | |
5247 | return this.lastChar.toString(this.encoding, 0, this.lastTotal); | |
5248 | } | |
5249 | buf.copy(this.lastChar, p, 0, buf.length); | |
5250 | this.lastNeed -= buf.length; | |
a0091a40 IC |
5251 | } |
5252 | ||
9f59e99b IC |
5253 | // Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a |
5254 | // partial character, the character's bytes are buffered until the required | |
5255 | // number of bytes are available. | |
5256 | function utf8Text(buf, i) { | |
5257 | var total = utf8CheckIncomplete(this, buf, i); | |
5258 | if (!this.lastNeed) return buf.toString('utf8', i); | |
5259 | this.lastTotal = total; | |
5260 | var end = buf.length - (total - this.lastNeed); | |
5261 | buf.copy(this.lastChar, 0, end); | |
5262 | return buf.toString('utf8', i, end); | |
5263 | } | |
a0091a40 | 5264 | |
9f59e99b IC |
5265 | // For UTF-8, a replacement character for each buffered byte of a (partial) |
5266 | // character needs to be added to the output. | |
5267 | function utf8End(buf) { | |
5268 | var r = buf && buf.length ? this.write(buf) : ''; | |
5269 | if (this.lastNeed) return r + '\ufffd'.repeat(this.lastTotal - this.lastNeed); | |
5270 | return r; | |
a0091a40 IC |
5271 | } |
5272 | ||
9f59e99b IC |
5273 | // UTF-16LE typically needs two bytes per character, but even if we have an even |
5274 | // number of bytes available, we need to check if we end on a leading/high | |
5275 | // surrogate. In that case, we need to wait for the next two bytes in order to | |
5276 | // decode the last character properly. | |
5277 | function utf16Text(buf, i) { | |
5278 | if ((buf.length - i) % 2 === 0) { | |
5279 | var r = buf.toString('utf16le', i); | |
5280 | if (r) { | |
5281 | var c = r.charCodeAt(r.length - 1); | |
5282 | if (c >= 0xD800 && c <= 0xDBFF) { | |
5283 | this.lastNeed = 2; | |
5284 | this.lastTotal = 4; | |
5285 | this.lastChar[0] = buf[buf.length - 2]; | |
5286 | this.lastChar[1] = buf[buf.length - 1]; | |
5287 | return r.slice(0, -1); | |
5288 | } | |
a0091a40 | 5289 | } |
9f59e99b | 5290 | return r; |
a0091a40 | 5291 | } |
9f59e99b IC |
5292 | this.lastNeed = 1; |
5293 | this.lastTotal = 2; | |
5294 | this.lastChar[0] = buf[buf.length - 1]; | |
5295 | return buf.toString('utf16le', i, buf.length - 1); | |
a0091a40 IC |
5296 | } |
5297 | ||
9f59e99b IC |
5298 | // For UTF-16LE we do not explicitly append special replacement characters if we |
5299 | // end on a partial character, we simply let v8 handle that. | |
5300 | function utf16End(buf) { | |
5301 | var r = buf && buf.length ? this.write(buf) : ''; | |
5302 | if (this.lastNeed) { | |
5303 | var end = this.lastTotal - this.lastNeed; | |
5304 | return r + this.lastChar.toString('utf16le', 0, end); | |
a0091a40 | 5305 | } |
9f59e99b | 5306 | return r; |
a0091a40 IC |
5307 | } |
5308 | ||
9f59e99b IC |
5309 | function base64Text(buf, i) { |
5310 | var n = (buf.length - i) % 3; | |
5311 | if (n === 0) return buf.toString('base64', i); | |
5312 | this.lastNeed = 3 - n; | |
5313 | this.lastTotal = 3; | |
5314 | if (n === 1) { | |
5315 | this.lastChar[0] = buf[buf.length - 1]; | |
5316 | } else { | |
5317 | this.lastChar[0] = buf[buf.length - 2]; | |
5318 | this.lastChar[1] = buf[buf.length - 1]; | |
a0091a40 | 5319 | } |
9f59e99b IC |
5320 | return buf.toString('base64', i, buf.length - n); |
5321 | } | |
5322 | ||
5323 | function base64End(buf) { | |
5324 | var r = buf && buf.length ? this.write(buf) : ''; | |
5325 | if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed); | |
5326 | return r; | |
5327 | } | |
5328 | ||
5329 | // Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex) | |
5330 | function simpleWrite(buf) { | |
5331 | return buf.toString(this.encoding); | |
5332 | } | |
5333 | ||
5334 | function simpleEnd(buf) { | |
5335 | return buf && buf.length ? this.write(buf) : ''; | |
a0091a40 | 5336 | } |
9f59e99b IC |
5337 | },{"safe-buffer":27}],23:[function(require,module,exports){ |
5338 | module.exports = require('./readable').PassThrough | |
5339 | ||
5340 | },{"./readable":24}],24:[function(require,module,exports){ | |
5341 | exports = module.exports = require('./lib/_stream_readable.js'); | |
5342 | exports.Stream = exports; | |
5343 | exports.Readable = exports; | |
5344 | exports.Writable = require('./lib/_stream_writable.js'); | |
5345 | exports.Duplex = require('./lib/_stream_duplex.js'); | |
5346 | exports.Transform = require('./lib/_stream_transform.js'); | |
5347 | exports.PassThrough = require('./lib/_stream_passthrough.js'); | |
5348 | ||
5349 | },{"./lib/_stream_duplex.js":15,"./lib/_stream_passthrough.js":16,"./lib/_stream_readable.js":17,"./lib/_stream_transform.js":18,"./lib/_stream_writable.js":19}],25:[function(require,module,exports){ | |
5350 | module.exports = require('./readable').Transform | |
5351 | ||
5352 | },{"./readable":24}],26:[function(require,module,exports){ | |
5353 | module.exports = require('./lib/_stream_writable.js'); | |
5354 | ||
5355 | },{"./lib/_stream_writable.js":19}],27:[function(require,module,exports){ | |
5356 | module.exports = require('buffer') | |
a0091a40 | 5357 | |
9f59e99b | 5358 | },{"buffer":5}],28:[function(require,module,exports){ |
a0091a40 IC |
5359 | // Copyright Joyent, Inc. and other Node contributors. |
5360 | // | |
5361 | // Permission is hereby granted, free of charge, to any person obtaining a | |
5362 | // copy of this software and associated documentation files (the | |
5363 | // "Software"), to deal in the Software without restriction, including | |
5364 | // without limitation the rights to use, copy, modify, merge, publish, | |
5365 | // distribute, sublicense, and/or sell copies of the Software, and to permit | |
5366 | // persons to whom the Software is furnished to do so, subject to the | |
5367 | // following conditions: | |
5368 | // | |
5369 | // The above copyright notice and this permission notice shall be included | |
5370 | // in all copies or substantial portions of the Software. | |
5371 | // | |
5372 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
5373 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
5374 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
5375 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
5376 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
5377 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
5378 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
5379 | ||
5380 | module.exports = Stream; | |
5381 | ||
5382 | var EE = require('events').EventEmitter; | |
5383 | var inherits = require('inherits'); | |
5384 | ||
5385 | inherits(Stream, EE); | |
5386 | Stream.Readable = require('readable-stream/readable.js'); | |
5387 | Stream.Writable = require('readable-stream/writable.js'); | |
5388 | Stream.Duplex = require('readable-stream/duplex.js'); | |
5389 | Stream.Transform = require('readable-stream/transform.js'); | |
5390 | Stream.PassThrough = require('readable-stream/passthrough.js'); | |
5391 | ||
5392 | // Backwards-compat with node 0.4.x | |
5393 | Stream.Stream = Stream; | |
5394 | ||
5395 | ||
5396 | ||
5397 | // old-style streams. Note that the pipe method (the only relevant | |
5398 | // part of this class) is overridden in the Readable class. | |
5399 | ||
5400 | function Stream() { | |
5401 | EE.call(this); | |
5402 | } | |
5403 | ||
5404 | Stream.prototype.pipe = function(dest, options) { | |
5405 | var source = this; | |
5406 | ||
5407 | function ondata(chunk) { | |
5408 | if (dest.writable) { | |
5409 | if (false === dest.write(chunk) && source.pause) { | |
5410 | source.pause(); | |
5411 | } | |
5412 | } | |
5413 | } | |
5414 | ||
5415 | source.on('data', ondata); | |
5416 | ||
5417 | function ondrain() { | |
5418 | if (source.readable && source.resume) { | |
5419 | source.resume(); | |
5420 | } | |
5421 | } | |
5422 | ||
5423 | dest.on('drain', ondrain); | |
5424 | ||
5425 | // If the 'end' option is not supplied, dest.end() will be called when | |
5426 | // source gets the 'end' or 'close' events. Only dest.end() once. | |
5427 | if (!dest._isStdio && (!options || options.end !== false)) { | |
5428 | source.on('end', onend); | |
5429 | source.on('close', onclose); | |
5430 | } | |
5431 | ||
5432 | var didOnEnd = false; | |
5433 | function onend() { | |
5434 | if (didOnEnd) return; | |
5435 | didOnEnd = true; | |
5436 | ||
5437 | dest.end(); | |
5438 | } | |
5439 | ||
5440 | ||
5441 | function onclose() { | |
5442 | if (didOnEnd) return; | |
5443 | didOnEnd = true; | |
5444 | ||
5445 | if (typeof dest.destroy === 'function') dest.destroy(); | |
5446 | } | |
5447 | ||
5448 | // don't leave dangling pipes when there are errors. | |
5449 | function onerror(er) { | |
5450 | cleanup(); | |
5451 | if (EE.listenerCount(this, 'error') === 0) { | |
5452 | throw er; // Unhandled stream error in pipe. | |
5453 | } | |
5454 | } | |
5455 | ||
5456 | source.on('error', onerror); | |
5457 | dest.on('error', onerror); | |
5458 | ||
5459 | // remove all the event listeners that were added. | |
5460 | function cleanup() { | |
5461 | source.removeListener('data', ondata); | |
5462 | dest.removeListener('drain', ondrain); | |
5463 | ||
5464 | source.removeListener('end', onend); | |
5465 | source.removeListener('close', onclose); | |
5466 | ||
5467 | source.removeListener('error', onerror); | |
5468 | dest.removeListener('error', onerror); | |
5469 | ||
5470 | source.removeListener('end', cleanup); | |
5471 | source.removeListener('close', cleanup); | |
5472 | ||
5473 | dest.removeListener('close', cleanup); | |
5474 | } | |
5475 | ||
5476 | source.on('end', cleanup); | |
5477 | source.on('close', cleanup); | |
5478 | ||
5479 | dest.on('close', cleanup); | |
5480 | ||
5481 | dest.emit('pipe', source); | |
5482 | ||
5483 | // Allow for unix-like usage: A.pipe(B).pipe(C) | |
5484 | return dest; | |
5485 | }; | |
5486 | ||
9f59e99b IC |
5487 | },{"events":7,"inherits":9,"readable-stream/duplex.js":14,"readable-stream/passthrough.js":23,"readable-stream/readable.js":24,"readable-stream/transform.js":25,"readable-stream/writable.js":26}],29:[function(require,module,exports){ |
5488 | // Copyright Joyent, Inc. and other Node contributors. | |
5489 | // | |
5490 | // Permission is hereby granted, free of charge, to any person obtaining a | |
5491 | // copy of this software and associated documentation files (the | |
5492 | // "Software"), to deal in the Software without restriction, including | |
5493 | // without limitation the rights to use, copy, modify, merge, publish, | |
5494 | // distribute, sublicense, and/or sell copies of the Software, and to permit | |
5495 | // persons to whom the Software is furnished to do so, subject to the | |
5496 | // following conditions: | |
5497 | // | |
5498 | // The above copyright notice and this permission notice shall be included | |
5499 | // in all copies or substantial portions of the Software. | |
5500 | // | |
5501 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
5502 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
5503 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
5504 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
5505 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
5506 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
5507 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
a0091a40 | 5508 | |
9f59e99b | 5509 | var Buffer = require('buffer').Buffer; |
a0091a40 | 5510 | |
9f59e99b IC |
5511 | var isBufferEncoding = Buffer.isEncoding |
5512 | || function(encoding) { | |
5513 | switch (encoding && encoding.toLowerCase()) { | |
5514 | case 'hex': case 'utf8': case 'utf-8': case 'ascii': case 'binary': case 'base64': case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': case 'raw': return true; | |
5515 | default: return false; | |
5516 | } | |
5517 | } | |
a0091a40 | 5518 | |
a0091a40 | 5519 | |
9f59e99b IC |
5520 | function assertEncoding(encoding) { |
5521 | if (encoding && !isBufferEncoding(encoding)) { | |
5522 | throw new Error('Unknown encoding: ' + encoding); | |
5523 | } | |
a0091a40 IC |
5524 | } |
5525 | ||
5526 | // StringDecoder provides an interface for efficiently splitting a series of | |
5527 | // buffers into a series of JS strings without breaking apart multi-byte | |
9f59e99b IC |
5528 | // characters. CESU-8 is handled as part of the UTF-8 encoding. |
5529 | // | |
5530 | // @TODO Handling all encodings inside a single object makes it very difficult | |
5531 | // to reason about this code, so it should be split up in the future. | |
5532 | // @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code | |
5533 | // points as used by CESU-8. | |
5534 | var StringDecoder = exports.StringDecoder = function(encoding) { | |
5535 | this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, ''); | |
5536 | assertEncoding(encoding); | |
a0091a40 | 5537 | switch (this.encoding) { |
a0091a40 | 5538 | case 'utf8': |
9f59e99b IC |
5539 | // CESU-8 represents each of Surrogate Pair by 3-bytes |
5540 | this.surrogateSize = 3; | |
5541 | break; | |
5542 | case 'ucs2': | |
5543 | case 'utf16le': | |
5544 | // UTF-16 represents each of Surrogate Pair by 2-bytes | |
5545 | this.surrogateSize = 2; | |
5546 | this.detectIncompleteChar = utf16DetectIncompleteChar; | |
a0091a40 IC |
5547 | break; |
5548 | case 'base64': | |
9f59e99b IC |
5549 | // Base-64 stores 3 bytes in 4 chars, and pads the remainder. |
5550 | this.surrogateSize = 3; | |
5551 | this.detectIncompleteChar = base64DetectIncompleteChar; | |
a0091a40 IC |
5552 | break; |
5553 | default: | |
9f59e99b | 5554 | this.write = passThroughWrite; |
a0091a40 IC |
5555 | return; |
5556 | } | |
a0091a40 | 5557 | |
9f59e99b IC |
5558 | // Enough space to store all bytes of a single character. UTF-8 needs 4 |
5559 | // bytes, but CESU-8 may require up to 6 (3 bytes per surrogate). | |
5560 | this.charBuffer = new Buffer(6); | |
5561 | // Number of bytes received for the current incomplete multi-byte character. | |
5562 | this.charReceived = 0; | |
5563 | // Number of bytes expected for the current incomplete multi-byte character. | |
5564 | this.charLength = 0; | |
a0091a40 IC |
5565 | }; |
5566 | ||
a0091a40 | 5567 | |
9f59e99b IC |
5568 | // write decodes the given buffer and returns it as JS string that is |
5569 | // guaranteed to not contain any partial multi-byte characters. Any partial | |
5570 | // character found at the end of the buffer is buffered up, and will be | |
5571 | // returned when calling write again with the remaining bytes. | |
5572 | // | |
5573 | // Note: Converting a Buffer containing an orphan surrogate to a String | |
5574 | // currently works, but converting a String to a Buffer (via `new Buffer`, or | |
5575 | // Buffer#write) will replace incomplete surrogates with the unicode | |
5576 | // replacement character. See https://codereview.chromium.org/121173009/ . | |
5577 | StringDecoder.prototype.write = function(buffer) { | |
5578 | var charStr = ''; | |
5579 | // if our last write ended with an incomplete multibyte character | |
5580 | while (this.charLength) { | |
5581 | // determine how many remaining bytes this buffer has to offer for this char | |
5582 | var available = (buffer.length >= this.charLength - this.charReceived) ? | |
5583 | this.charLength - this.charReceived : | |
5584 | buffer.length; | |
5585 | ||
5586 | // add the new bytes to the char buffer | |
5587 | buffer.copy(this.charBuffer, this.charReceived, 0, available); | |
5588 | this.charReceived += available; | |
5589 | ||
5590 | if (this.charReceived < this.charLength) { | |
5591 | // still not enough chars in this buffer? wait for more ... | |
5592 | return ''; | |
5593 | } | |
a0091a40 | 5594 | |
9f59e99b IC |
5595 | // remove bytes belonging to the current character from the buffer |
5596 | buffer = buffer.slice(available, buffer.length); | |
a0091a40 | 5597 | |
9f59e99b IC |
5598 | // get the character that was split |
5599 | charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding); | |
a0091a40 | 5600 | |
9f59e99b IC |
5601 | // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character |
5602 | var charCode = charStr.charCodeAt(charStr.length - 1); | |
5603 | if (charCode >= 0xD800 && charCode <= 0xDBFF) { | |
5604 | this.charLength += this.surrogateSize; | |
5605 | charStr = ''; | |
5606 | continue; | |
a0091a40 | 5607 | } |
9f59e99b | 5608 | this.charReceived = this.charLength = 0; |
a0091a40 | 5609 | |
9f59e99b IC |
5610 | // if there are no more bytes in this buffer, just emit our char |
5611 | if (buffer.length === 0) { | |
5612 | return charStr; | |
a0091a40 | 5613 | } |
9f59e99b | 5614 | break; |
a0091a40 | 5615 | } |
a0091a40 | 5616 | |
9f59e99b IC |
5617 | // determine and set charLength / charReceived |
5618 | this.detectIncompleteChar(buffer); | |
5619 | ||
5620 | var end = buffer.length; | |
5621 | if (this.charLength) { | |
5622 | // buffer the incomplete character bytes we got | |
5623 | buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end); | |
5624 | end -= this.charReceived; | |
a0091a40 | 5625 | } |
a0091a40 | 5626 | |
9f59e99b | 5627 | charStr += buffer.toString(this.encoding, 0, end); |
a0091a40 | 5628 | |
9f59e99b IC |
5629 | var end = charStr.length - 1; |
5630 | var charCode = charStr.charCodeAt(end); | |
5631 | // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character | |
5632 | if (charCode >= 0xD800 && charCode <= 0xDBFF) { | |
5633 | var size = this.surrogateSize; | |
5634 | this.charLength += size; | |
5635 | this.charReceived += size; | |
5636 | this.charBuffer.copy(this.charBuffer, size, 0, size); | |
5637 | buffer.copy(this.charBuffer, 0, 0, size); | |
5638 | return charStr.substring(0, end); | |
5639 | } | |
a0091a40 | 5640 | |
9f59e99b IC |
5641 | // or just emit the charStr |
5642 | return charStr; | |
5643 | }; | |
5644 | ||
5645 | // detectIncompleteChar determines if there is an incomplete UTF-8 character at | |
5646 | // the end of the given buffer. If so, it sets this.charLength to the byte | |
5647 | // length that character, and sets this.charReceived to the number of bytes | |
5648 | // that are available for this character. | |
5649 | StringDecoder.prototype.detectIncompleteChar = function(buffer) { | |
5650 | // determine how many bytes we have to check at the end of this buffer | |
5651 | var i = (buffer.length >= 3) ? 3 : buffer.length; | |
5652 | ||
5653 | // Figure out if one of the last i bytes of our buffer announces an | |
5654 | // incomplete char. | |
5655 | for (; i > 0; i--) { | |
5656 | var c = buffer[buffer.length - i]; | |
5657 | ||
5658 | // See http://en.wikipedia.org/wiki/UTF-8#Description | |
5659 | ||
5660 | // 110XXXXX | |
5661 | if (i == 1 && c >> 5 == 0x06) { | |
5662 | this.charLength = 2; | |
5663 | break; | |
a0091a40 | 5664 | } |
a0091a40 | 5665 | |
9f59e99b IC |
5666 | // 1110XXXX |
5667 | if (i <= 2 && c >> 4 == 0x0E) { | |
5668 | this.charLength = 3; | |
5669 | break; | |
5670 | } | |
5671 | ||
5672 | // 11110XXX | |
5673 | if (i <= 3 && c >> 3 == 0x1E) { | |
5674 | this.charLength = 4; | |
5675 | break; | |
5676 | } | |
a0091a40 | 5677 | } |
9f59e99b IC |
5678 | this.charReceived = i; |
5679 | }; | |
a0091a40 | 5680 | |
9f59e99b IC |
5681 | StringDecoder.prototype.end = function(buffer) { |
5682 | var res = ''; | |
5683 | if (buffer && buffer.length) | |
5684 | res = this.write(buffer); | |
5685 | ||
5686 | if (this.charReceived) { | |
5687 | var cr = this.charReceived; | |
5688 | var buf = this.charBuffer; | |
5689 | var enc = this.encoding; | |
5690 | res += buf.slice(0, cr).toString(enc); | |
a0091a40 | 5691 | } |
a0091a40 | 5692 | |
9f59e99b IC |
5693 | return res; |
5694 | }; | |
5695 | ||
5696 | function passThroughWrite(buffer) { | |
5697 | return buffer.toString(this.encoding); | |
a0091a40 IC |
5698 | } |
5699 | ||
9f59e99b IC |
5700 | function utf16DetectIncompleteChar(buffer) { |
5701 | this.charReceived = buffer.length % 2; | |
5702 | this.charLength = this.charReceived ? 2 : 0; | |
a0091a40 IC |
5703 | } |
5704 | ||
9f59e99b IC |
5705 | function base64DetectIncompleteChar(buffer) { |
5706 | this.charReceived = buffer.length % 3; | |
5707 | this.charLength = this.charReceived ? 3 : 0; | |
a0091a40 | 5708 | } |
9f59e99b IC |
5709 | |
5710 | },{"buffer":5}],30:[function(require,module,exports){ | |
a0091a40 IC |
5711 | (function (global){ |
5712 | ||
5713 | /** | |
5714 | * Module exports. | |
5715 | */ | |
5716 | ||
5717 | module.exports = deprecate; | |
5718 | ||
5719 | /** | |
5720 | * Mark that a method should not be used. | |
5721 | * Returns a modified function which warns once by default. | |
5722 | * | |
5723 | * If `localStorage.noDeprecation = true` is set, then it is a no-op. | |
5724 | * | |
5725 | * If `localStorage.throwDeprecation = true` is set, then deprecated functions | |
5726 | * will throw an Error when invoked. | |
5727 | * | |
5728 | * If `localStorage.traceDeprecation = true` is set, then deprecated functions | |
5729 | * will invoke `console.trace()` instead of `console.error()`. | |
5730 | * | |
5731 | * @param {Function} fn - the function to deprecate | |
5732 | * @param {String} msg - the string to print to the console when `fn` is invoked | |
5733 | * @returns {Function} a new "deprecated" version of `fn` | |
5734 | * @api public | |
5735 | */ | |
5736 | ||
5737 | function deprecate (fn, msg) { | |
5738 | if (config('noDeprecation')) { | |
5739 | return fn; | |
5740 | } | |
5741 | ||
5742 | var warned = false; | |
5743 | function deprecated() { | |
5744 | if (!warned) { | |
5745 | if (config('throwDeprecation')) { | |
5746 | throw new Error(msg); | |
5747 | } else if (config('traceDeprecation')) { | |
5748 | console.trace(msg); | |
5749 | } else { | |
5750 | console.warn(msg); | |
5751 | } | |
5752 | warned = true; | |
5753 | } | |
5754 | return fn.apply(this, arguments); | |
5755 | } | |
5756 | ||
5757 | return deprecated; | |
5758 | } | |
5759 | ||
5760 | /** | |
5761 | * Checks `localStorage` for boolean values for the given `name`. | |
5762 | * | |
5763 | * @param {String} name | |
5764 | * @returns {Boolean} | |
5765 | * @api private | |
5766 | */ | |
5767 | ||
5768 | function config (name) { | |
5769 | // accessing global.localStorage can trigger a DOMException in sandboxed iframes | |
5770 | try { | |
5771 | if (!global.localStorage) return false; | |
5772 | } catch (_) { | |
5773 | return false; | |
5774 | } | |
5775 | var val = global.localStorage[name]; | |
5776 | if (null == val) return false; | |
5777 | return String(val).toLowerCase() === 'true'; | |
5778 | } | |
5779 | ||
5780 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | |
9f59e99b IC |
5781 | },{}],31:[function(require,module,exports){ |
5782 | arguments[4][9][0].apply(exports,arguments) | |
5783 | },{"dup":9}],32:[function(require,module,exports){ | |
a0091a40 IC |
5784 | module.exports = function isBuffer(arg) { |
5785 | return arg && typeof arg === 'object' | |
5786 | && typeof arg.copy === 'function' | |
5787 | && typeof arg.fill === 'function' | |
5788 | && typeof arg.readUInt8 === 'function'; | |
5789 | } | |
9f59e99b | 5790 | },{}],33:[function(require,module,exports){ |
a0091a40 IC |
5791 | (function (process,global){ |
5792 | // Copyright Joyent, Inc. and other Node contributors. | |
5793 | // | |
5794 | // Permission is hereby granted, free of charge, to any person obtaining a | |
5795 | // copy of this software and associated documentation files (the | |
5796 | // "Software"), to deal in the Software without restriction, including | |
5797 | // without limitation the rights to use, copy, modify, merge, publish, | |
5798 | // distribute, sublicense, and/or sell copies of the Software, and to permit | |
5799 | // persons to whom the Software is furnished to do so, subject to the | |
5800 | // following conditions: | |
5801 | // | |
5802 | // The above copyright notice and this permission notice shall be included | |
5803 | // in all copies or substantial portions of the Software. | |
5804 | // | |
5805 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
5806 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
5807 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
5808 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
5809 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
5810 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
5811 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
5812 | ||
5813 | var formatRegExp = /%[sdj%]/g; | |
5814 | exports.format = function(f) { | |
5815 | if (!isString(f)) { | |
5816 | var objects = []; | |
5817 | for (var i = 0; i < arguments.length; i++) { | |
5818 | objects.push(inspect(arguments[i])); | |
5819 | } | |
5820 | return objects.join(' '); | |
5821 | } | |
5822 | ||
5823 | var i = 1; | |
5824 | var args = arguments; | |
5825 | var len = args.length; | |
5826 | var str = String(f).replace(formatRegExp, function(x) { | |
5827 | if (x === '%%') return '%'; | |
5828 | if (i >= len) return x; | |
5829 | switch (x) { | |
5830 | case '%s': return String(args[i++]); | |
5831 | case '%d': return Number(args[i++]); | |
5832 | case '%j': | |
5833 | try { | |
5834 | return JSON.stringify(args[i++]); | |
5835 | } catch (_) { | |
5836 | return '[Circular]'; | |
5837 | } | |
5838 | default: | |
5839 | return x; | |
5840 | } | |
5841 | }); | |
5842 | for (var x = args[i]; i < len; x = args[++i]) { | |
5843 | if (isNull(x) || !isObject(x)) { | |
5844 | str += ' ' + x; | |
5845 | } else { | |
5846 | str += ' ' + inspect(x); | |
5847 | } | |
5848 | } | |
5849 | return str; | |
5850 | }; | |
5851 | ||
5852 | ||
5853 | // Mark that a method should not be used. | |
5854 | // Returns a modified function which warns once by default. | |
5855 | // If --no-deprecation is set, then it is a no-op. | |
5856 | exports.deprecate = function(fn, msg) { | |
5857 | // Allow for deprecating things in the process of starting up. | |
5858 | if (isUndefined(global.process)) { | |
5859 | return function() { | |
5860 | return exports.deprecate(fn, msg).apply(this, arguments); | |
5861 | }; | |
5862 | } | |
5863 | ||
5864 | if (process.noDeprecation === true) { | |
5865 | return fn; | |
5866 | } | |
5867 | ||
5868 | var warned = false; | |
5869 | function deprecated() { | |
5870 | if (!warned) { | |
5871 | if (process.throwDeprecation) { | |
5872 | throw new Error(msg); | |
5873 | } else if (process.traceDeprecation) { | |
5874 | console.trace(msg); | |
5875 | } else { | |
5876 | console.error(msg); | |
5877 | } | |
5878 | warned = true; | |
5879 | } | |
5880 | return fn.apply(this, arguments); | |
5881 | } | |
5882 | ||
5883 | return deprecated; | |
5884 | }; | |
5885 | ||
5886 | ||
5887 | var debugs = {}; | |
5888 | var debugEnviron; | |
5889 | exports.debuglog = function(set) { | |
5890 | if (isUndefined(debugEnviron)) | |
5891 | debugEnviron = process.env.NODE_DEBUG || ''; | |
5892 | set = set.toUpperCase(); | |
5893 | if (!debugs[set]) { | |
5894 | if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { | |
5895 | var pid = process.pid; | |
5896 | debugs[set] = function() { | |
5897 | var msg = exports.format.apply(exports, arguments); | |
5898 | console.error('%s %d: %s', set, pid, msg); | |
5899 | }; | |
5900 | } else { | |
5901 | debugs[set] = function() {}; | |
5902 | } | |
5903 | } | |
5904 | return debugs[set]; | |
5905 | }; | |
5906 | ||
5907 | ||
5908 | /** | |
5909 | * Echos the value of a value. Trys to print the value out | |
5910 | * in the best way possible given the different types. | |
5911 | * | |
5912 | * @param {Object} obj The object to print out. | |
5913 | * @param {Object} opts Optional options object that alters the output. | |
5914 | */ | |
5915 | /* legacy: obj, showHidden, depth, colors*/ | |
5916 | function inspect(obj, opts) { | |
5917 | // default options | |
5918 | var ctx = { | |
5919 | seen: [], | |
5920 | stylize: stylizeNoColor | |
5921 | }; | |
5922 | // legacy... | |
5923 | if (arguments.length >= 3) ctx.depth = arguments[2]; | |
5924 | if (arguments.length >= 4) ctx.colors = arguments[3]; | |
5925 | if (isBoolean(opts)) { | |
5926 | // legacy... | |
5927 | ctx.showHidden = opts; | |
5928 | } else if (opts) { | |
5929 | // got an "options" object | |
5930 | exports._extend(ctx, opts); | |
5931 | } | |
5932 | // set default options | |
5933 | if (isUndefined(ctx.showHidden)) ctx.showHidden = false; | |
5934 | if (isUndefined(ctx.depth)) ctx.depth = 2; | |
5935 | if (isUndefined(ctx.colors)) ctx.colors = false; | |
5936 | if (isUndefined(ctx.customInspect)) ctx.customInspect = true; | |
5937 | if (ctx.colors) ctx.stylize = stylizeWithColor; | |
5938 | return formatValue(ctx, obj, ctx.depth); | |
5939 | } | |
5940 | exports.inspect = inspect; | |
5941 | ||
5942 | ||
5943 | // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics | |
5944 | inspect.colors = { | |
5945 | 'bold' : [1, 22], | |
5946 | 'italic' : [3, 23], | |
5947 | 'underline' : [4, 24], | |
5948 | 'inverse' : [7, 27], | |
5949 | 'white' : [37, 39], | |
5950 | 'grey' : [90, 39], | |
5951 | 'black' : [30, 39], | |
5952 | 'blue' : [34, 39], | |
5953 | 'cyan' : [36, 39], | |
5954 | 'green' : [32, 39], | |
5955 | 'magenta' : [35, 39], | |
5956 | 'red' : [31, 39], | |
5957 | 'yellow' : [33, 39] | |
5958 | }; | |
5959 | ||
5960 | // Don't use 'blue' not visible on cmd.exe | |
5961 | inspect.styles = { | |
5962 | 'special': 'cyan', | |
5963 | 'number': 'yellow', | |
5964 | 'boolean': 'yellow', | |
5965 | 'undefined': 'grey', | |
5966 | 'null': 'bold', | |
5967 | 'string': 'green', | |
5968 | 'date': 'magenta', | |
5969 | // "name": intentionally not styling | |
5970 | 'regexp': 'red' | |
5971 | }; | |
5972 | ||
5973 | ||
5974 | function stylizeWithColor(str, styleType) { | |
5975 | var style = inspect.styles[styleType]; | |
5976 | ||
5977 | if (style) { | |
5978 | return '\u001b[' + inspect.colors[style][0] + 'm' + str + | |
5979 | '\u001b[' + inspect.colors[style][1] + 'm'; | |
5980 | } else { | |
5981 | return str; | |
5982 | } | |
5983 | } | |
5984 | ||
5985 | ||
5986 | function stylizeNoColor(str, styleType) { | |
5987 | return str; | |
5988 | } | |
5989 | ||
5990 | ||
5991 | function arrayToHash(array) { | |
5992 | var hash = {}; | |
5993 | ||
5994 | array.forEach(function(val, idx) { | |
5995 | hash[val] = true; | |
5996 | }); | |
5997 | ||
5998 | return hash; | |
5999 | } | |
6000 | ||
6001 | ||
6002 | function formatValue(ctx, value, recurseTimes) { | |
6003 | // Provide a hook for user-specified inspect functions. | |
6004 | // Check that value is an object with an inspect function on it | |
6005 | if (ctx.customInspect && | |
6006 | value && | |
6007 | isFunction(value.inspect) && | |
6008 | // Filter out the util module, it's inspect function is special | |
6009 | value.inspect !== exports.inspect && | |
6010 | // Also filter out any prototype objects using the circular check. | |
6011 | !(value.constructor && value.constructor.prototype === value)) { | |
6012 | var ret = value.inspect(recurseTimes, ctx); | |
6013 | if (!isString(ret)) { | |
6014 | ret = formatValue(ctx, ret, recurseTimes); | |
6015 | } | |
6016 | return ret; | |
6017 | } | |
6018 | ||
6019 | // Primitive types cannot have properties | |
6020 | var primitive = formatPrimitive(ctx, value); | |
6021 | if (primitive) { | |
6022 | return primitive; | |
6023 | } | |
6024 | ||
6025 | // Look up the keys of the object. | |
6026 | var keys = Object.keys(value); | |
6027 | var visibleKeys = arrayToHash(keys); | |
6028 | ||
6029 | if (ctx.showHidden) { | |
6030 | keys = Object.getOwnPropertyNames(value); | |
6031 | } | |
6032 | ||
6033 | // IE doesn't make error fields non-enumerable | |
6034 | // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx | |
6035 | if (isError(value) | |
6036 | && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { | |
6037 | return formatError(value); | |
6038 | } | |
6039 | ||
6040 | // Some type of object without properties can be shortcutted. | |
6041 | if (keys.length === 0) { | |
6042 | if (isFunction(value)) { | |
6043 | var name = value.name ? ': ' + value.name : ''; | |
6044 | return ctx.stylize('[Function' + name + ']', 'special'); | |
6045 | } | |
6046 | if (isRegExp(value)) { | |
6047 | return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); | |
6048 | } | |
6049 | if (isDate(value)) { | |
6050 | return ctx.stylize(Date.prototype.toString.call(value), 'date'); | |
6051 | } | |
6052 | if (isError(value)) { | |
6053 | return formatError(value); | |
6054 | } | |
6055 | } | |
6056 | ||
6057 | var base = '', array = false, braces = ['{', '}']; | |
6058 | ||
6059 | // Make Array say that they are Array | |
6060 | if (isArray(value)) { | |
6061 | array = true; | |
6062 | braces = ['[', ']']; | |
6063 | } | |
6064 | ||
6065 | // Make functions say that they are functions | |
6066 | if (isFunction(value)) { | |
6067 | var n = value.name ? ': ' + value.name : ''; | |
6068 | base = ' [Function' + n + ']'; | |
6069 | } | |
6070 | ||
6071 | // Make RegExps say that they are RegExps | |
6072 | if (isRegExp(value)) { | |
6073 | base = ' ' + RegExp.prototype.toString.call(value); | |
6074 | } | |
6075 | ||
6076 | // Make dates with properties first say the date | |
6077 | if (isDate(value)) { | |
6078 | base = ' ' + Date.prototype.toUTCString.call(value); | |
6079 | } | |
6080 | ||
6081 | // Make error with message first say the error | |
6082 | if (isError(value)) { | |
6083 | base = ' ' + formatError(value); | |
6084 | } | |
6085 | ||
6086 | if (keys.length === 0 && (!array || value.length == 0)) { | |
6087 | return braces[0] + base + braces[1]; | |
6088 | } | |
6089 | ||
6090 | if (recurseTimes < 0) { | |
6091 | if (isRegExp(value)) { | |
6092 | return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); | |
6093 | } else { | |
6094 | return ctx.stylize('[Object]', 'special'); | |
6095 | } | |
6096 | } | |
6097 | ||
6098 | ctx.seen.push(value); | |
6099 | ||
6100 | var output; | |
6101 | if (array) { | |
6102 | output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); | |
6103 | } else { | |
6104 | output = keys.map(function(key) { | |
6105 | return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); | |
6106 | }); | |
6107 | } | |
6108 | ||
6109 | ctx.seen.pop(); | |
6110 | ||
6111 | return reduceToSingleString(output, base, braces); | |
6112 | } | |
6113 | ||
6114 | ||
6115 | function formatPrimitive(ctx, value) { | |
6116 | if (isUndefined(value)) | |
6117 | return ctx.stylize('undefined', 'undefined'); | |
6118 | if (isString(value)) { | |
6119 | var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') | |
6120 | .replace(/'/g, "\\'") | |
6121 | .replace(/\\"/g, '"') + '\''; | |
6122 | return ctx.stylize(simple, 'string'); | |
6123 | } | |
6124 | if (isNumber(value)) | |
6125 | return ctx.stylize('' + value, 'number'); | |
6126 | if (isBoolean(value)) | |
6127 | return ctx.stylize('' + value, 'boolean'); | |
6128 | // For some reason typeof null is "object", so special case here. | |
6129 | if (isNull(value)) | |
6130 | return ctx.stylize('null', 'null'); | |
6131 | } | |
6132 | ||
6133 | ||
6134 | function formatError(value) { | |
6135 | return '[' + Error.prototype.toString.call(value) + ']'; | |
6136 | } | |
6137 | ||
6138 | ||
6139 | function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { | |
6140 | var output = []; | |
6141 | for (var i = 0, l = value.length; i < l; ++i) { | |
6142 | if (hasOwnProperty(value, String(i))) { | |
6143 | output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, | |
6144 | String(i), true)); | |
6145 | } else { | |
6146 | output.push(''); | |
6147 | } | |
6148 | } | |
6149 | keys.forEach(function(key) { | |
6150 | if (!key.match(/^\d+$/)) { | |
6151 | output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, | |
6152 | key, true)); | |
6153 | } | |
6154 | }); | |
6155 | return output; | |
6156 | } | |
6157 | ||
6158 | ||
6159 | function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { | |
6160 | var name, str, desc; | |
6161 | desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; | |
6162 | if (desc.get) { | |
6163 | if (desc.set) { | |
6164 | str = ctx.stylize('[Getter/Setter]', 'special'); | |
6165 | } else { | |
6166 | str = ctx.stylize('[Getter]', 'special'); | |
6167 | } | |
6168 | } else { | |
6169 | if (desc.set) { | |
6170 | str = ctx.stylize('[Setter]', 'special'); | |
6171 | } | |
6172 | } | |
6173 | if (!hasOwnProperty(visibleKeys, key)) { | |
6174 | name = '[' + key + ']'; | |
6175 | } | |
6176 | if (!str) { | |
6177 | if (ctx.seen.indexOf(desc.value) < 0) { | |
6178 | if (isNull(recurseTimes)) { | |
6179 | str = formatValue(ctx, desc.value, null); | |
6180 | } else { | |
6181 | str = formatValue(ctx, desc.value, recurseTimes - 1); | |
6182 | } | |
6183 | if (str.indexOf('\n') > -1) { | |
6184 | if (array) { | |
6185 | str = str.split('\n').map(function(line) { | |
6186 | return ' ' + line; | |
6187 | }).join('\n').substr(2); | |
6188 | } else { | |
6189 | str = '\n' + str.split('\n').map(function(line) { | |
6190 | return ' ' + line; | |
6191 | }).join('\n'); | |
6192 | } | |
6193 | } | |
6194 | } else { | |
6195 | str = ctx.stylize('[Circular]', 'special'); | |
6196 | } | |
6197 | } | |
6198 | if (isUndefined(name)) { | |
6199 | if (array && key.match(/^\d+$/)) { | |
6200 | return str; | |
6201 | } | |
6202 | name = JSON.stringify('' + key); | |
6203 | if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { | |
6204 | name = name.substr(1, name.length - 2); | |
6205 | name = ctx.stylize(name, 'name'); | |
6206 | } else { | |
6207 | name = name.replace(/'/g, "\\'") | |
6208 | .replace(/\\"/g, '"') | |
6209 | .replace(/(^"|"$)/g, "'"); | |
6210 | name = ctx.stylize(name, 'string'); | |
6211 | } | |
6212 | } | |
6213 | ||
6214 | return name + ': ' + str; | |
6215 | } | |
6216 | ||
6217 | ||
6218 | function reduceToSingleString(output, base, braces) { | |
6219 | var numLinesEst = 0; | |
6220 | var length = output.reduce(function(prev, cur) { | |
6221 | numLinesEst++; | |
6222 | if (cur.indexOf('\n') >= 0) numLinesEst++; | |
6223 | return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; | |
6224 | }, 0); | |
6225 | ||
6226 | if (length > 60) { | |
6227 | return braces[0] + | |
6228 | (base === '' ? '' : base + '\n ') + | |
6229 | ' ' + | |
6230 | output.join(',\n ') + | |
6231 | ' ' + | |
6232 | braces[1]; | |
6233 | } | |
6234 | ||
6235 | return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; | |
6236 | } | |
6237 | ||
6238 | ||
6239 | // NOTE: These type checking functions intentionally don't use `instanceof` | |
6240 | // because it is fragile and can be easily faked with `Object.create()`. | |
6241 | function isArray(ar) { | |
6242 | return Array.isArray(ar); | |
6243 | } | |
6244 | exports.isArray = isArray; | |
6245 | ||
6246 | function isBoolean(arg) { | |
6247 | return typeof arg === 'boolean'; | |
6248 | } | |
6249 | exports.isBoolean = isBoolean; | |
6250 | ||
6251 | function isNull(arg) { | |
6252 | return arg === null; | |
6253 | } | |
6254 | exports.isNull = isNull; | |
6255 | ||
6256 | function isNullOrUndefined(arg) { | |
6257 | return arg == null; | |
6258 | } | |
6259 | exports.isNullOrUndefined = isNullOrUndefined; | |
6260 | ||
6261 | function isNumber(arg) { | |
6262 | return typeof arg === 'number'; | |
6263 | } | |
6264 | exports.isNumber = isNumber; | |
6265 | ||
6266 | function isString(arg) { | |
6267 | return typeof arg === 'string'; | |
6268 | } | |
6269 | exports.isString = isString; | |
6270 | ||
6271 | function isSymbol(arg) { | |
6272 | return typeof arg === 'symbol'; | |
6273 | } | |
6274 | exports.isSymbol = isSymbol; | |
6275 | ||
6276 | function isUndefined(arg) { | |
6277 | return arg === void 0; | |
6278 | } | |
6279 | exports.isUndefined = isUndefined; | |
6280 | ||
6281 | function isRegExp(re) { | |
6282 | return isObject(re) && objectToString(re) === '[object RegExp]'; | |
6283 | } | |
6284 | exports.isRegExp = isRegExp; | |
6285 | ||
6286 | function isObject(arg) { | |
6287 | return typeof arg === 'object' && arg !== null; | |
6288 | } | |
6289 | exports.isObject = isObject; | |
6290 | ||
6291 | function isDate(d) { | |
6292 | return isObject(d) && objectToString(d) === '[object Date]'; | |
6293 | } | |
6294 | exports.isDate = isDate; | |
6295 | ||
6296 | function isError(e) { | |
6297 | return isObject(e) && | |
6298 | (objectToString(e) === '[object Error]' || e instanceof Error); | |
6299 | } | |
6300 | exports.isError = isError; | |
6301 | ||
6302 | function isFunction(arg) { | |
6303 | return typeof arg === 'function'; | |
6304 | } | |
6305 | exports.isFunction = isFunction; | |
6306 | ||
6307 | function isPrimitive(arg) { | |
6308 | return arg === null || | |
6309 | typeof arg === 'boolean' || | |
6310 | typeof arg === 'number' || | |
6311 | typeof arg === 'string' || | |
6312 | typeof arg === 'symbol' || // ES6 symbol | |
6313 | typeof arg === 'undefined'; | |
6314 | } | |
6315 | exports.isPrimitive = isPrimitive; | |
6316 | ||
6317 | exports.isBuffer = require('./support/isBuffer'); | |
6318 | ||
6319 | function objectToString(o) { | |
6320 | return Object.prototype.toString.call(o); | |
6321 | } | |
6322 | ||
6323 | ||
6324 | function pad(n) { | |
6325 | return n < 10 ? '0' + n.toString(10) : n.toString(10); | |
6326 | } | |
6327 | ||
6328 | ||
6329 | var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', | |
6330 | 'Oct', 'Nov', 'Dec']; | |
6331 | ||
6332 | // 26 Feb 16:19:34 | |
6333 | function timestamp() { | |
6334 | var d = new Date(); | |
6335 | var time = [pad(d.getHours()), | |
6336 | pad(d.getMinutes()), | |
6337 | pad(d.getSeconds())].join(':'); | |
6338 | return [d.getDate(), months[d.getMonth()], time].join(' '); | |
6339 | } | |
6340 | ||
6341 | ||
6342 | // log is just a thin wrapper to console.log that prepends a timestamp | |
6343 | exports.log = function() { | |
6344 | console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); | |
6345 | }; | |
6346 | ||
6347 | ||
6348 | /** | |
6349 | * Inherit the prototype methods from one constructor into another. | |
6350 | * | |
6351 | * The Function.prototype.inherits from lang.js rewritten as a standalone | |
6352 | * function (not on Function.prototype). NOTE: If this file is to be loaded | |
6353 | * during bootstrapping this function needs to be rewritten using some native | |
6354 | * functions as prototype setup using normal JavaScript does not work as | |
6355 | * expected during bootstrapping (see mirror.js in r114903). | |
6356 | * | |
6357 | * @param {function} ctor Constructor function which needs to inherit the | |
6358 | * prototype. | |
6359 | * @param {function} superCtor Constructor function to inherit prototype from. | |
6360 | */ | |
6361 | exports.inherits = require('inherits'); | |
6362 | ||
6363 | exports._extend = function(origin, add) { | |
6364 | // Don't do anything if add isn't an object | |
6365 | if (!add || !isObject(add)) return origin; | |
6366 | ||
6367 | var keys = Object.keys(add); | |
6368 | var i = keys.length; | |
6369 | while (i--) { | |
6370 | origin[keys[i]] = add[keys[i]]; | |
6371 | } | |
6372 | return origin; | |
6373 | }; | |
6374 | ||
6375 | function hasOwnProperty(obj, prop) { | |
6376 | return Object.prototype.hasOwnProperty.call(obj, prop); | |
6377 | } | |
6378 | ||
6379 | }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | |
9f59e99b IC |
6380 | },{"./support/isBuffer":32,"_process":13,"inherits":31}],34:[function(require,module,exports){ |
6381 | bitcoin = { | |
a0091a40 IC |
6382 | base58: require('bs58'), |
6383 | bitcoin: require('bitcoinjs-lib'), | |
6384 | ecurve: require('ecurve'), | |
9f59e99b IC |
6385 | BigInteger: require('bigi'), |
6386 | Buffer: require('buffer'), | |
6387 | elliptic: require('elliptic'), | |
6388 | bs58check: require('bs58check'), | |
a0091a40 IC |
6389 | } |
6390 | ||
9f59e99b IC |
6391 | module.exports = bitcoin; |
6392 | ||
6393 | },{"bigi":39,"bitcoinjs-lib":52,"bs58":83,"bs58check":85,"buffer":5,"ecurve":94,"elliptic":97}],35:[function(require,module,exports){ | |
a0091a40 IC |
6394 | // base-x encoding |
6395 | // Forked from https://github.com/cryptocoinjs/bs58 | |
6396 | // Originally written by Mike Hearn for BitcoinJ | |
6397 | // Copyright (c) 2011 Google Inc | |
6398 | // Ported to JavaScript by Stefan Thomas | |
6399 | // Merged Buffer refactorings from base58-native by Stephen Pair | |
6400 | // Copyright (c) 2013 BitPay Inc | |
6401 | ||
6402 | var Buffer = require('safe-buffer').Buffer | |
6403 | ||
6404 | module.exports = function base (ALPHABET) { | |
6405 | var ALPHABET_MAP = {} | |
6406 | var BASE = ALPHABET.length | |
6407 | var LEADER = ALPHABET.charAt(0) | |
6408 | ||
6409 | // pre-compute lookup table | |
6410 | for (var z = 0; z < ALPHABET.length; z++) { | |
6411 | var x = ALPHABET.charAt(z) | |
6412 | ||
6413 | if (ALPHABET_MAP[x] !== undefined) throw new TypeError(x + ' is ambiguous') | |
6414 | ALPHABET_MAP[x] = z | |
6415 | } | |
6416 | ||
6417 | function encode (source) { | |
6418 | if (source.length === 0) return '' | |
6419 | ||
6420 | var digits = [0] | |
6421 | for (var i = 0; i < source.length; ++i) { | |
6422 | for (var j = 0, carry = source[i]; j < digits.length; ++j) { | |
6423 | carry += digits[j] << 8 | |
6424 | digits[j] = carry % BASE | |
6425 | carry = (carry / BASE) | 0 | |
6426 | } | |
6427 | ||
6428 | while (carry > 0) { | |
6429 | digits.push(carry % BASE) | |
6430 | carry = (carry / BASE) | 0 | |
6431 | } | |
6432 | } | |
6433 | ||
6434 | var string = '' | |
6435 | ||
6436 | // deal with leading zeros | |
6437 | for (var k = 0; source[k] === 0 && k < source.length - 1; ++k) string += ALPHABET[0] | |
6438 | // convert digits to a string | |
6439 | for (var q = digits.length - 1; q >= 0; --q) string += ALPHABET[digits[q]] | |
6440 | ||
6441 | return string | |
6442 | } | |
6443 | ||
6444 | function decodeUnsafe (string) { | |
6445 | if (string.length === 0) return Buffer.allocUnsafe(0) | |
6446 | ||
6447 | var bytes = [0] | |
6448 | for (var i = 0; i < string.length; i++) { | |
6449 | var value = ALPHABET_MAP[string[i]] | |
6450 | if (value === undefined) return | |
6451 | ||
6452 | for (var j = 0, carry = value; j < bytes.length; ++j) { | |
6453 | carry += bytes[j] * BASE | |
6454 | bytes[j] = carry & 0xff | |
6455 | carry >>= 8 | |
6456 | } | |
6457 | ||
6458 | while (carry > 0) { | |
6459 | bytes.push(carry & 0xff) | |
6460 | carry >>= 8 | |
6461 | } | |
6462 | } | |
6463 | ||
6464 | // deal with leading zeros | |
6465 | for (var k = 0; string[k] === LEADER && k < string.length - 1; ++k) { | |
6466 | bytes.push(0) | |
6467 | } | |
6468 | ||
6469 | return Buffer.from(bytes.reverse()) | |
6470 | } | |
6471 | ||
6472 | function decode (string) { | |
6473 | var buffer = decodeUnsafe(string) | |
6474 | if (buffer) return buffer | |
6475 | ||
6476 | throw new Error('Non-base' + BASE + ' character') | |
6477 | } | |
6478 | ||
6479 | return { | |
6480 | encode: encode, | |
6481 | decodeUnsafe: decodeUnsafe, | |
6482 | decode: decode | |
6483 | } | |
6484 | } | |
6485 | ||
9f59e99b IC |
6486 | },{"safe-buffer":128}],36:[function(require,module,exports){ |
6487 | 'use strict' | |
6488 | let ALPHABET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l' | |
a0091a40 | 6489 | |
9f59e99b IC |
6490 | // pre-compute lookup table |
6491 | let ALPHABET_MAP = {} | |
6492 | for (let z = 0; z < ALPHABET.length; z++) { | |
6493 | let x = ALPHABET.charAt(z) | |
6494 | ||
6495 | if (ALPHABET_MAP[x] !== undefined) throw new TypeError(x + ' is ambiguous') | |
6496 | ALPHABET_MAP[x] = z | |
a0091a40 IC |
6497 | } |
6498 | ||
9f59e99b IC |
6499 | function polymodStep (pre) { |
6500 | let b = pre >> 25 | |
6501 | return ((pre & 0x1FFFFFF) << 5) ^ | |
6502 | (-((b >> 0) & 1) & 0x3b6a57b2) ^ | |
6503 | (-((b >> 1) & 1) & 0x26508e6d) ^ | |
6504 | (-((b >> 2) & 1) & 0x1ea119fa) ^ | |
6505 | (-((b >> 3) & 1) & 0x3d4233dd) ^ | |
6506 | (-((b >> 4) & 1) & 0x2a1462b3) | |
6507 | } | |
a0091a40 | 6508 | |
9f59e99b IC |
6509 | function prefixChk (prefix) { |
6510 | let chk = 1 | |
6511 | for (let i = 0; i < prefix.length; ++i) { | |
6512 | let c = prefix.charCodeAt(i) | |
6513 | if (c < 33 || c > 126) throw new Error('Invalid prefix (' + prefix + ')') | |
6514 | ||
6515 | chk = polymodStep(chk) ^ (c >> 5) | |
6516 | } | |
6517 | chk = polymodStep(chk) | |
6518 | ||
6519 | for (let i = 0; i < prefix.length; ++i) { | |
6520 | let v = prefix.charCodeAt(i) | |
6521 | chk = polymodStep(chk) ^ (v & 0x1f) | |
6522 | } | |
6523 | return chk | |
a0091a40 IC |
6524 | } |
6525 | ||
9f59e99b IC |
6526 | function encode (prefix, words) { |
6527 | // too long? | |
6528 | if ((prefix.length + 7 + words.length) > 90) throw new TypeError('Exceeds Bech32 maximum length') | |
6529 | prefix = prefix.toLowerCase() | |
a0091a40 | 6530 | |
9f59e99b IC |
6531 | // determine chk mod |
6532 | let chk = prefixChk(prefix) | |
6533 | let result = prefix + '1' | |
6534 | for (let i = 0; i < words.length; ++i) { | |
6535 | let x = words[i] | |
6536 | if ((x >> 5) !== 0) throw new Error('Non 5-bit word') | |
a0091a40 | 6537 | |
9f59e99b IC |
6538 | chk = polymodStep(chk) ^ x |
6539 | result += ALPHABET.charAt(x) | |
a0091a40 | 6540 | } |
9f59e99b IC |
6541 | |
6542 | for (let i = 0; i < 6; ++i) { | |
6543 | chk = polymodStep(chk) | |
6544 | } | |
6545 | chk ^= 1 | |
6546 | ||
6547 | for (let i = 0; i < 6; ++i) { | |
6548 | let v = (chk >> ((5 - i) * 5)) & 0x1f | |
6549 | result += ALPHABET.charAt(v) | |
6550 | } | |
6551 | ||
6552 | return result | |
a0091a40 | 6553 | } |
9f59e99b IC |
6554 | |
6555 | function decode (str) { | |
6556 | if (str.length < 8) throw new TypeError(str + ' too short') | |
6557 | if (str.length > 90) throw new TypeError(str + ' too long') | |
6558 | ||
6559 | // don't allow mixed case | |
6560 | let lowered = str.toLowerCase() | |
6561 | let uppered = str.toUpperCase() | |
6562 | if (str !== lowered && str !== uppered) throw new Error('Mixed-case string ' + str) | |
6563 | str = lowered | |
6564 | ||
6565 | let split = str.lastIndexOf('1') | |
6566 | if (split === 0) throw new Error('Missing prefix for ' + str) | |
6567 | ||
6568 | let prefix = str.slice(0, split) | |
6569 | let wordChars = str.slice(split + 1) | |
6570 | if (wordChars.length < 6) throw new Error('Data too short') | |
6571 | ||
6572 | let chk = prefixChk(prefix) | |
6573 | let words = [] | |
6574 | for (let i = 0; i < wordChars.length; ++i) { | |
6575 | let c = wordChars.charAt(i) | |
6576 | let v = ALPHABET_MAP[c] | |
6577 | if (v === undefined) throw new Error('Unknown character ' + c) | |
6578 | chk = polymodStep(chk) ^ v | |
6579 | ||
6580 | // not in the checksum? | |
6581 | if (i + 6 >= wordChars.length) continue | |
6582 | words.push(v) | |
6583 | } | |
6584 | ||
6585 | if (chk !== 1) throw new Error('Invalid checksum for ' + str) | |
6586 | return { prefix, words } | |
6587 | } | |
6588 | ||
6589 | function convert (data, inBits, outBits, pad) { | |
6590 | let value = 0 | |
6591 | let bits = 0 | |
6592 | let maxV = (1 << outBits) - 1 | |
6593 | ||
6594 | let result = [] | |
6595 | for (let i = 0; i < data.length; ++i) { | |
6596 | value = (value << inBits) | data[i] | |
6597 | bits += inBits | |
6598 | ||
6599 | while (bits >= outBits) { | |
6600 | bits -= outBits | |
6601 | result.push((value >> bits) & maxV) | |
6602 | } | |
6603 | } | |
6604 | ||
6605 | if (pad) { | |
6606 | if (bits > 0) { | |
6607 | result.push((value << (outBits - bits)) & maxV) | |
6608 | } | |
6609 | } else { | |
6610 | if (bits >= inBits) throw new Error('Excess padding') | |
6611 | if ((value << (outBits - bits)) & maxV) throw new Error('Non-zero padding') | |
6612 | } | |
6613 | ||
6614 | return result | |
6615 | } | |
6616 | ||
6617 | function toWords (bytes) { | |
6618 | return convert(bytes, 8, 5, true) | |
6619 | } | |
6620 | ||
6621 | function fromWords (words) { | |
6622 | return convert(words, 5, 8, false) | |
6623 | } | |
6624 | ||
6625 | module.exports = { decode, encode, toWords, fromWords } | |
6626 | ||
6627 | },{}],37:[function(require,module,exports){ | |
6628 | // (public) Constructor | |
6629 | function BigInteger(a, b, c) { | |
6630 | if (!(this instanceof BigInteger)) | |
6631 | return new BigInteger(a, b, c) | |
6632 | ||
6633 | if (a != null) { | |
6634 | if ("number" == typeof a) this.fromNumber(a, b, c) | |
6635 | else if (b == null && "string" != typeof a) this.fromString(a, 256) | |
6636 | else this.fromString(a, b) | |
6637 | } | |
6638 | } | |
6639 | ||
6640 | var proto = BigInteger.prototype | |
6641 | ||
6642 | // duck-typed isBigInteger | |
6643 | proto.__bigi = require('../package.json').version | |
6644 | BigInteger.isBigInteger = function (obj, check_ver) { | |
6645 | return obj && obj.__bigi && (!check_ver || obj.__bigi === proto.__bigi) | |
6646 | } | |
6647 | ||
6648 | // Bits per digit | |
6649 | var dbits | |
6650 | ||
6651 | // am: Compute w_j += (x*this_i), propagate carries, | |
6652 | // c is initial carry, returns final carry. | |
6653 | // c < 3*dvalue, x < 2*dvalue, this_i < dvalue | |
6654 | // We need to select the fastest one that works in this environment. | |
6655 | ||
6656 | // am1: use a single mult and divide to get the high bits, | |
6657 | // max digit bits should be 26 because | |
6658 | // max internal value = 2*dvalue^2-2*dvalue (< 2^53) | |
6659 | function am1(i, x, w, j, c, n) { | |
6660 | while (--n >= 0) { | |
6661 | var v = x * this[i++] + w[j] + c | |
6662 | c = Math.floor(v / 0x4000000) | |
6663 | w[j++] = v & 0x3ffffff | |
6664 | } | |
6665 | return c | |
6666 | } | |
6667 | // am2 avoids a big mult-and-extract completely. | |
6668 | // Max digit bits should be <= 30 because we do bitwise ops | |
6669 | // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31) | |
6670 | function am2(i, x, w, j, c, n) { | |
6671 | var xl = x & 0x7fff, | |
a0091a40 IC |
6672 | xh = x >> 15 |
6673 | while (--n >= 0) { | |
6674 | var l = this[i] & 0x7fff | |
6675 | var h = this[i++] >> 15 | |
6676 | var m = xh * l + h * xl | |
6677 | l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff) | |
6678 | c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30) | |
6679 | w[j++] = l & 0x3fffffff | |
6680 | } | |
6681 | return c | |
6682 | } | |
6683 | // Alternately, set max digit bits to 28 since some | |
6684 | // browsers slow down when dealing with 32-bit numbers. | |
6685 | function am3(i, x, w, j, c, n) { | |
6686 | var xl = x & 0x3fff, | |
6687 | xh = x >> 14 | |
6688 | while (--n >= 0) { | |
6689 | var l = this[i] & 0x3fff | |
6690 | var h = this[i++] >> 14 | |
6691 | var m = xh * l + h * xl | |
6692 | l = xl * l + ((m & 0x3fff) << 14) + w[j] + c | |
6693 | c = (l >> 28) + (m >> 14) + xh * h | |
6694 | w[j++] = l & 0xfffffff | |
6695 | } | |
6696 | return c | |
6697 | } | |
6698 | ||
6699 | // wtf? | |
6700 | BigInteger.prototype.am = am1 | |
6701 | dbits = 26 | |
6702 | ||
6703 | BigInteger.prototype.DB = dbits | |
6704 | BigInteger.prototype.DM = ((1 << dbits) - 1) | |
6705 | var DV = BigInteger.prototype.DV = (1 << dbits) | |
6706 | ||
6707 | var BI_FP = 52 | |
6708 | BigInteger.prototype.FV = Math.pow(2, BI_FP) | |
6709 | BigInteger.prototype.F1 = BI_FP - dbits | |
6710 | BigInteger.prototype.F2 = 2 * dbits - BI_FP | |
6711 | ||
6712 | // Digit conversions | |
6713 | var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz" | |
6714 | var BI_RC = new Array() | |
6715 | var rr, vv | |
6716 | rr = "0".charCodeAt(0) | |
6717 | for (vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv | |
6718 | rr = "a".charCodeAt(0) | |
6719 | for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv | |
6720 | rr = "A".charCodeAt(0) | |
6721 | for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv | |
6722 | ||
6723 | function int2char(n) { | |
6724 | return BI_RM.charAt(n) | |
6725 | } | |
6726 | ||
6727 | function intAt(s, i) { | |
6728 | var c = BI_RC[s.charCodeAt(i)] | |
6729 | return (c == null) ? -1 : c | |
6730 | } | |
6731 | ||
6732 | // (protected) copy this to r | |
6733 | function bnpCopyTo(r) { | |
6734 | for (var i = this.t - 1; i >= 0; --i) r[i] = this[i] | |
6735 | r.t = this.t | |
6736 | r.s = this.s | |
6737 | } | |
6738 | ||
6739 | // (protected) set from integer value x, -DV <= x < DV | |
6740 | function bnpFromInt(x) { | |
6741 | this.t = 1 | |
6742 | this.s = (x < 0) ? -1 : 0 | |
6743 | if (x > 0) this[0] = x | |
6744 | else if (x < -1) this[0] = x + DV | |
6745 | else this.t = 0 | |
6746 | } | |
6747 | ||
6748 | // return bigint initialized to value | |
6749 | function nbv(i) { | |
6750 | var r = new BigInteger() | |
6751 | r.fromInt(i) | |
6752 | return r | |
6753 | } | |
6754 | ||
6755 | // (protected) set from string and radix | |
6756 | function bnpFromString(s, b) { | |
6757 | var self = this | |
6758 | ||
6759 | var k | |
6760 | if (b == 16) k = 4 | |
6761 | else if (b == 8) k = 3 | |
6762 | else if (b == 256) k = 8; // byte array | |
6763 | else if (b == 2) k = 1 | |
6764 | else if (b == 32) k = 5 | |
6765 | else if (b == 4) k = 2 | |
6766 | else { | |
6767 | self.fromRadix(s, b) | |
6768 | return | |
6769 | } | |
6770 | self.t = 0 | |
6771 | self.s = 0 | |
6772 | var i = s.length, | |
6773 | mi = false, | |
6774 | sh = 0 | |
6775 | while (--i >= 0) { | |
6776 | var x = (k == 8) ? s[i] & 0xff : intAt(s, i) | |
6777 | if (x < 0) { | |
6778 | if (s.charAt(i) == "-") mi = true | |
6779 | continue | |
6780 | } | |
6781 | mi = false | |
6782 | if (sh == 0) | |
6783 | self[self.t++] = x | |
6784 | else if (sh + k > self.DB) { | |
6785 | self[self.t - 1] |= (x & ((1 << (self.DB - sh)) - 1)) << sh | |
6786 | self[self.t++] = (x >> (self.DB - sh)) | |
6787 | } else | |
6788 | self[self.t - 1] |= x << sh | |
6789 | sh += k | |
6790 | if (sh >= self.DB) sh -= self.DB | |
6791 | } | |
6792 | if (k == 8 && (s[0] & 0x80) != 0) { | |
6793 | self.s = -1 | |
6794 | if (sh > 0) self[self.t - 1] |= ((1 << (self.DB - sh)) - 1) << sh | |
6795 | } | |
6796 | self.clamp() | |
6797 | if (mi) BigInteger.ZERO.subTo(self, self) | |
6798 | } | |
6799 | ||
6800 | // (protected) clamp off excess high words | |
6801 | function bnpClamp() { | |
6802 | var c = this.s & this.DM | |
6803 | while (this.t > 0 && this[this.t - 1] == c)--this.t | |
6804 | } | |
6805 | ||
6806 | // (public) return string representation in given radix | |
6807 | function bnToString(b) { | |
6808 | var self = this | |
6809 | if (self.s < 0) return "-" + self.negate() | |
6810 | .toString(b) | |
6811 | var k | |
6812 | if (b == 16) k = 4 | |
6813 | else if (b == 8) k = 3 | |
6814 | else if (b == 2) k = 1 | |
6815 | else if (b == 32) k = 5 | |
6816 | else if (b == 4) k = 2 | |
6817 | else return self.toRadix(b) | |
6818 | var km = (1 << k) - 1, | |
6819 | d, m = false, | |
6820 | r = "", | |
6821 | i = self.t | |
6822 | var p = self.DB - (i * self.DB) % k | |
6823 | if (i-- > 0) { | |
6824 | if (p < self.DB && (d = self[i] >> p) > 0) { | |
6825 | m = true | |
6826 | r = int2char(d) | |
6827 | } | |
6828 | while (i >= 0) { | |
6829 | if (p < k) { | |
6830 | d = (self[i] & ((1 << p) - 1)) << (k - p) | |
6831 | d |= self[--i] >> (p += self.DB - k) | |
6832 | } else { | |
6833 | d = (self[i] >> (p -= k)) & km | |
6834 | if (p <= 0) { | |
6835 | p += self.DB | |
6836 | --i | |
6837 | } | |
6838 | } | |
6839 | if (d > 0) m = true | |
6840 | if (m) r += int2char(d) | |
6841 | } | |
6842 | } | |
6843 | return m ? r : "0" | |
6844 | } | |
6845 | ||
6846 | // (public) -this | |
6847 | function bnNegate() { | |
6848 | var r = new BigInteger() | |
6849 | BigInteger.ZERO.subTo(this, r) | |
6850 | return r | |
6851 | } | |
6852 | ||
6853 | // (public) |this| | |
6854 | function bnAbs() { | |
6855 | return (this.s < 0) ? this.negate() : this | |
6856 | } | |
6857 | ||
6858 | // (public) return + if this > a, - if this < a, 0 if equal | |
6859 | function bnCompareTo(a) { | |
6860 | var r = this.s - a.s | |
6861 | if (r != 0) return r | |
6862 | var i = this.t | |
6863 | r = i - a.t | |
6864 | if (r != 0) return (this.s < 0) ? -r : r | |
6865 | while (--i >= 0) | |
6866 | if ((r = this[i] - a[i]) != 0) return r | |
6867 | return 0 | |
6868 | } | |
6869 | ||
6870 | // returns bit length of the integer x | |
6871 | function nbits(x) { | |
6872 | var r = 1, | |
6873 | t | |
6874 | if ((t = x >>> 16) != 0) { | |
6875 | x = t | |
6876 | r += 16 | |
6877 | } | |
6878 | if ((t = x >> 8) != 0) { | |
6879 | x = t | |
6880 | r += 8 | |
6881 | } | |
6882 | if ((t = x >> 4) != 0) { | |
6883 | x = t | |
6884 | r += 4 | |
6885 | } | |
6886 | if ((t = x >> 2) != 0) { | |
6887 | x = t | |
6888 | r += 2 | |
6889 | } | |
6890 | if ((t = x >> 1) != 0) { | |
6891 | x = t | |
6892 | r += 1 | |
6893 | } | |
6894 | return r | |
6895 | } | |
6896 | ||
6897 | // (public) return the number of bits in "this" | |
6898 | function bnBitLength() { | |
6899 | if (this.t <= 0) return 0 | |
6900 | return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ (this.s & this.DM)) | |
6901 | } | |
6902 | ||
6903 | // (public) return the number of bytes in "this" | |
6904 | function bnByteLength() { | |
6905 | return this.bitLength() >> 3 | |
6906 | } | |
6907 | ||
6908 | // (protected) r = this << n*DB | |
6909 | function bnpDLShiftTo(n, r) { | |
6910 | var i | |
6911 | for (i = this.t - 1; i >= 0; --i) r[i + n] = this[i] | |
6912 | for (i = n - 1; i >= 0; --i) r[i] = 0 | |
6913 | r.t = this.t + n | |
6914 | r.s = this.s | |
6915 | } | |
6916 | ||
6917 | // (protected) r = this >> n*DB | |
6918 | function bnpDRShiftTo(n, r) { | |
6919 | for (var i = n; i < this.t; ++i) r[i - n] = this[i] | |
6920 | r.t = Math.max(this.t - n, 0) | |
6921 | r.s = this.s | |
6922 | } | |
6923 | ||
6924 | // (protected) r = this << n | |
6925 | function bnpLShiftTo(n, r) { | |
6926 | var self = this | |
6927 | var bs = n % self.DB | |
6928 | var cbs = self.DB - bs | |
6929 | var bm = (1 << cbs) - 1 | |
6930 | var ds = Math.floor(n / self.DB), | |
6931 | c = (self.s << bs) & self.DM, | |
6932 | i | |
6933 | for (i = self.t - 1; i >= 0; --i) { | |
6934 | r[i + ds + 1] = (self[i] >> cbs) | c | |
6935 | c = (self[i] & bm) << bs | |
6936 | } | |
6937 | for (i = ds - 1; i >= 0; --i) r[i] = 0 | |
6938 | r[ds] = c | |
6939 | r.t = self.t + ds + 1 | |
6940 | r.s = self.s | |
6941 | r.clamp() | |
6942 | } | |
6943 | ||
6944 | // (protected) r = this >> n | |
6945 | function bnpRShiftTo(n, r) { | |
6946 | var self = this | |
6947 | r.s = self.s | |
6948 | var ds = Math.floor(n / self.DB) | |
6949 | if (ds >= self.t) { | |
6950 | r.t = 0 | |
6951 | return | |
6952 | } | |
6953 | var bs = n % self.DB | |
6954 | var cbs = self.DB - bs | |
6955 | var bm = (1 << bs) - 1 | |
6956 | r[0] = self[ds] >> bs | |
6957 | for (var i = ds + 1; i < self.t; ++i) { | |
6958 | r[i - ds - 1] |= (self[i] & bm) << cbs | |
6959 | r[i - ds] = self[i] >> bs | |
6960 | } | |
6961 | if (bs > 0) r[self.t - ds - 1] |= (self.s & bm) << cbs | |
6962 | r.t = self.t - ds | |
6963 | r.clamp() | |
6964 | } | |
6965 | ||
6966 | // (protected) r = this - a | |
6967 | function bnpSubTo(a, r) { | |
6968 | var self = this | |
6969 | var i = 0, | |
6970 | c = 0, | |
6971 | m = Math.min(a.t, self.t) | |
6972 | while (i < m) { | |
6973 | c += self[i] - a[i] | |
6974 | r[i++] = c & self.DM | |
6975 | c >>= self.DB | |
6976 | } | |
6977 | if (a.t < self.t) { | |
6978 | c -= a.s | |
6979 | while (i < self.t) { | |
6980 | c += self[i] | |
6981 | r[i++] = c & self.DM | |
6982 | c >>= self.DB | |
6983 | } | |
6984 | c += self.s | |
6985 | } else { | |
6986 | c += self.s | |
6987 | while (i < a.t) { | |
6988 | c -= a[i] | |
6989 | r[i++] = c & self.DM | |
6990 | c >>= self.DB | |
6991 | } | |
6992 | c -= a.s | |
6993 | } | |
6994 | r.s = (c < 0) ? -1 : 0 | |
6995 | if (c < -1) r[i++] = self.DV + c | |
6996 | else if (c > 0) r[i++] = c | |
6997 | r.t = i | |
6998 | r.clamp() | |
6999 | } | |
7000 | ||
7001 | // (protected) r = this * a, r != this,a (HAC 14.12) | |
7002 | // "this" should be the larger one if appropriate. | |
7003 | function bnpMultiplyTo(a, r) { | |
7004 | var x = this.abs(), | |
7005 | y = a.abs() | |
7006 | var i = x.t | |
7007 | r.t = i + y.t | |
7008 | while (--i >= 0) r[i] = 0 | |
7009 | for (i = 0; i < y.t; ++i) r[i + x.t] = x.am(0, y[i], r, i, 0, x.t) | |
7010 | r.s = 0 | |
7011 | r.clamp() | |
7012 | if (this.s != a.s) BigInteger.ZERO.subTo(r, r) | |
7013 | } | |
7014 | ||
7015 | // (protected) r = this^2, r != this (HAC 14.16) | |
7016 | function bnpSquareTo(r) { | |
7017 | var x = this.abs() | |
7018 | var i = r.t = 2 * x.t | |
7019 | while (--i >= 0) r[i] = 0 | |
7020 | for (i = 0; i < x.t - 1; ++i) { | |
7021 | var c = x.am(i, x[i], r, 2 * i, 0, 1) | |
7022 | if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) { | |
7023 | r[i + x.t] -= x.DV | |
7024 | r[i + x.t + 1] = 1 | |
7025 | } | |
7026 | } | |
7027 | if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1) | |
7028 | r.s = 0 | |
7029 | r.clamp() | |
7030 | } | |
7031 | ||
7032 | // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20) | |
7033 | // r != q, this != m. q or r may be null. | |
7034 | function bnpDivRemTo(m, q, r) { | |
7035 | var self = this | |
7036 | var pm = m.abs() | |
7037 | if (pm.t <= 0) return | |
7038 | var pt = self.abs() | |
7039 | if (pt.t < pm.t) { | |
7040 | if (q != null) q.fromInt(0) | |
7041 | if (r != null) self.copyTo(r) | |
7042 | return | |
7043 | } | |
7044 | if (r == null) r = new BigInteger() | |
7045 | var y = new BigInteger(), | |
7046 | ts = self.s, | |
7047 | ms = m.s | |
7048 | var nsh = self.DB - nbits(pm[pm.t - 1]); // normalize modulus | |
7049 | if (nsh > 0) { | |
7050 | pm.lShiftTo(nsh, y) | |
7051 | pt.lShiftTo(nsh, r) | |
7052 | } else { | |
7053 | pm.copyTo(y) | |
7054 | pt.copyTo(r) | |
7055 | } | |
7056 | var ys = y.t | |
7057 | var y0 = y[ys - 1] | |
7058 | if (y0 == 0) return | |
7059 | var yt = y0 * (1 << self.F1) + ((ys > 1) ? y[ys - 2] >> self.F2 : 0) | |
7060 | var d1 = self.FV / yt, | |
7061 | d2 = (1 << self.F1) / yt, | |
7062 | e = 1 << self.F2 | |
7063 | var i = r.t, | |
7064 | j = i - ys, | |
7065 | t = (q == null) ? new BigInteger() : q | |
7066 | y.dlShiftTo(j, t) | |
7067 | if (r.compareTo(t) >= 0) { | |
7068 | r[r.t++] = 1 | |
7069 | r.subTo(t, r) | |
7070 | } | |
7071 | BigInteger.ONE.dlShiftTo(ys, t) | |
7072 | t.subTo(y, y); // "negative" y so we can replace sub with am later | |
7073 | while (y.t < ys) y[y.t++] = 0 | |
7074 | while (--j >= 0) { | |
7075 | // Estimate quotient digit | |
7076 | var qd = (r[--i] == y0) ? self.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2) | |
7077 | if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) { // Try it out | |
7078 | y.dlShiftTo(j, t) | |
7079 | r.subTo(t, r) | |
7080 | while (r[i] < --qd) r.subTo(t, r) | |
7081 | } | |
7082 | } | |
7083 | if (q != null) { | |
7084 | r.drShiftTo(ys, q) | |
7085 | if (ts != ms) BigInteger.ZERO.subTo(q, q) | |
7086 | } | |
7087 | r.t = ys | |
7088 | r.clamp() | |
7089 | if (nsh > 0) r.rShiftTo(nsh, r); // Denormalize remainder | |
7090 | if (ts < 0) BigInteger.ZERO.subTo(r, r) | |
7091 | } | |
7092 | ||
7093 | // (public) this mod a | |
7094 | function bnMod(a) { | |
7095 | var r = new BigInteger() | |
7096 | this.abs() | |
7097 | .divRemTo(a, null, r) | |
7098 | if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r, r) | |
7099 | return r | |
7100 | } | |
7101 | ||
7102 | // Modular reduction using "classic" algorithm | |
7103 | function Classic(m) { | |
7104 | this.m = m | |
7105 | } | |
7106 | ||
7107 | function cConvert(x) { | |
7108 | if (x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m) | |
7109 | else return x | |
7110 | } | |
7111 | ||
7112 | function cRevert(x) { | |
7113 | return x | |
7114 | } | |
7115 | ||
7116 | function cReduce(x) { | |
7117 | x.divRemTo(this.m, null, x) | |
7118 | } | |
7119 | ||
7120 | function cMulTo(x, y, r) { | |
7121 | x.multiplyTo(y, r) | |
7122 | this.reduce(r) | |
7123 | } | |
7124 | ||
7125 | function cSqrTo(x, r) { | |
7126 | x.squareTo(r) | |
7127 | this.reduce(r) | |
7128 | } | |
7129 | ||
7130 | Classic.prototype.convert = cConvert | |
7131 | Classic.prototype.revert = cRevert | |
7132 | Classic.prototype.reduce = cReduce | |
7133 | Classic.prototype.mulTo = cMulTo | |
7134 | Classic.prototype.sqrTo = cSqrTo | |
7135 | ||
7136 | // (protected) return "-1/this % 2^DB"; useful for Mont. reduction | |
7137 | // justification: | |
7138 | // xy == 1 (mod m) | |
7139 | // xy = 1+km | |
7140 | // xy(2-xy) = (1+km)(1-km) | |
7141 | // x[y(2-xy)] = 1-k^2m^2 | |
7142 | // x[y(2-xy)] == 1 (mod m^2) | |
7143 | // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2 | |
7144 | // should reduce x and y(2-xy) by m^2 at each step to keep size bounded. | |
7145 | // JS multiply "overflows" differently from C/C++, so care is needed here. | |
7146 | function bnpInvDigit() { | |
7147 | if (this.t < 1) return 0 | |
7148 | var x = this[0] | |
7149 | if ((x & 1) == 0) return 0 | |
7150 | var y = x & 3; // y == 1/x mod 2^2 | |
7151 | y = (y * (2 - (x & 0xf) * y)) & 0xf; // y == 1/x mod 2^4 | |
7152 | y = (y * (2 - (x & 0xff) * y)) & 0xff; // y == 1/x mod 2^8 | |
7153 | y = (y * (2 - (((x & 0xffff) * y) & 0xffff))) & 0xffff; // y == 1/x mod 2^16 | |
7154 | // last step - calculate inverse mod DV directly | |
7155 | // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints | |
7156 | y = (y * (2 - x * y % this.DV)) % this.DV; // y == 1/x mod 2^dbits | |
7157 | // we really want the negative inverse, and -DV < y < DV | |
7158 | return (y > 0) ? this.DV - y : -y | |
7159 | } | |
7160 | ||
7161 | // Montgomery reduction | |
7162 | function Montgomery(m) { | |
7163 | this.m = m | |
7164 | this.mp = m.invDigit() | |
7165 | this.mpl = this.mp & 0x7fff | |
7166 | this.mph = this.mp >> 15 | |
7167 | this.um = (1 << (m.DB - 15)) - 1 | |
7168 | this.mt2 = 2 * m.t | |
7169 | } | |
7170 | ||
7171 | // xR mod m | |
7172 | function montConvert(x) { | |
7173 | var r = new BigInteger() | |
7174 | x.abs() | |
7175 | .dlShiftTo(this.m.t, r) | |
7176 | r.divRemTo(this.m, null, r) | |
7177 | if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r, r) | |
7178 | return r | |
7179 | } | |
7180 | ||
7181 | // x/R mod m | |
7182 | function montRevert(x) { | |
7183 | var r = new BigInteger() | |
7184 | x.copyTo(r) | |
7185 | this.reduce(r) | |
7186 | return r | |
7187 | } | |
7188 | ||
7189 | // x = x/R mod m (HAC 14.32) | |
7190 | function montReduce(x) { | |
7191 | while (x.t <= this.mt2) // pad x so am has enough room later | |
7192 | x[x.t++] = 0 | |
7193 | for (var i = 0; i < this.m.t; ++i) { | |
7194 | // faster way of calculating u0 = x[i]*mp mod DV | |
7195 | var j = x[i] & 0x7fff | |
7196 | var u0 = (j * this.mpl + (((j * this.mph + (x[i] >> 15) * this.mpl) & this.um) << 15)) & x.DM | |
7197 | // use am to combine the multiply-shift-add into one call | |
7198 | j = i + this.m.t | |
7199 | x[j] += this.m.am(0, u0, x, i, 0, this.m.t) | |
7200 | // propagate carry | |
7201 | while (x[j] >= x.DV) { | |
7202 | x[j] -= x.DV | |
7203 | x[++j]++ | |
7204 | } | |
7205 | } | |
7206 | x.clamp() | |
7207 | x.drShiftTo(this.m.t, x) | |
7208 | if (x.compareTo(this.m) >= 0) x.subTo(this.m, x) | |
7209 | } | |
7210 | ||
7211 | // r = "x^2/R mod m"; x != r | |
7212 | function montSqrTo(x, r) { | |
7213 | x.squareTo(r) | |
7214 | this.reduce(r) | |
7215 | } | |
7216 | ||
7217 | // r = "xy/R mod m"; x,y != r | |
7218 | function montMulTo(x, y, r) { | |
7219 | x.multiplyTo(y, r) | |
7220 | this.reduce(r) | |
7221 | } | |
7222 | ||
7223 | Montgomery.prototype.convert = montConvert | |
7224 | Montgomery.prototype.revert = montRevert | |
7225 | Montgomery.prototype.reduce = montReduce | |
7226 | Montgomery.prototype.mulTo = montMulTo | |
7227 | Montgomery.prototype.sqrTo = montSqrTo | |
7228 | ||
7229 | // (protected) true iff this is even | |
7230 | function bnpIsEven() { | |
7231 | return ((this.t > 0) ? (this[0] & 1) : this.s) == 0 | |
7232 | } | |
7233 | ||
7234 | // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) | |
7235 | function bnpExp(e, z) { | |
7236 | if (e > 0xffffffff || e < 1) return BigInteger.ONE | |
7237 | var r = new BigInteger(), | |
7238 | r2 = new BigInteger(), | |
7239 | g = z.convert(this), | |
7240 | i = nbits(e) - 1 | |
7241 | g.copyTo(r) | |
7242 | while (--i >= 0) { | |
7243 | z.sqrTo(r, r2) | |
7244 | if ((e & (1 << i)) > 0) z.mulTo(r2, g, r) | |
7245 | else { | |
7246 | var t = r | |
7247 | r = r2 | |
7248 | r2 = t | |
7249 | } | |
7250 | } | |
7251 | return z.revert(r) | |
7252 | } | |
7253 | ||
7254 | // (public) this^e % m, 0 <= e < 2^32 | |
7255 | function bnModPowInt(e, m) { | |
7256 | var z | |
7257 | if (e < 256 || m.isEven()) z = new Classic(m) | |
7258 | else z = new Montgomery(m) | |
7259 | return this.exp(e, z) | |
7260 | } | |
7261 | ||
7262 | // protected | |
7263 | proto.copyTo = bnpCopyTo | |
7264 | proto.fromInt = bnpFromInt | |
7265 | proto.fromString = bnpFromString | |
7266 | proto.clamp = bnpClamp | |
7267 | proto.dlShiftTo = bnpDLShiftTo | |
7268 | proto.drShiftTo = bnpDRShiftTo | |
7269 | proto.lShiftTo = bnpLShiftTo | |
7270 | proto.rShiftTo = bnpRShiftTo | |
7271 | proto.subTo = bnpSubTo | |
7272 | proto.multiplyTo = bnpMultiplyTo | |
7273 | proto.squareTo = bnpSquareTo | |
7274 | proto.divRemTo = bnpDivRemTo | |
7275 | proto.invDigit = bnpInvDigit | |
7276 | proto.isEven = bnpIsEven | |
7277 | proto.exp = bnpExp | |
7278 | ||
7279 | // public | |
7280 | proto.toString = bnToString | |
7281 | proto.negate = bnNegate | |
7282 | proto.abs = bnAbs | |
7283 | proto.compareTo = bnCompareTo | |
7284 | proto.bitLength = bnBitLength | |
7285 | proto.byteLength = bnByteLength | |
7286 | proto.mod = bnMod | |
7287 | proto.modPowInt = bnModPowInt | |
7288 | ||
7289 | // (public) | |
7290 | function bnClone() { | |
7291 | var r = new BigInteger() | |
7292 | this.copyTo(r) | |
7293 | return r | |
7294 | } | |
7295 | ||
7296 | // (public) return value as integer | |
7297 | function bnIntValue() { | |
7298 | if (this.s < 0) { | |
7299 | if (this.t == 1) return this[0] - this.DV | |
7300 | else if (this.t == 0) return -1 | |
7301 | } else if (this.t == 1) return this[0] | |
7302 | else if (this.t == 0) return 0 | |
7303 | // assumes 16 < DB < 32 | |
7304 | return ((this[1] & ((1 << (32 - this.DB)) - 1)) << this.DB) | this[0] | |
7305 | } | |
7306 | ||
7307 | // (public) return value as byte | |
7308 | function bnByteValue() { | |
7309 | return (this.t == 0) ? this.s : (this[0] << 24) >> 24 | |
7310 | } | |
7311 | ||
7312 | // (public) return value as short (assumes DB>=16) | |
7313 | function bnShortValue() { | |
7314 | return (this.t == 0) ? this.s : (this[0] << 16) >> 16 | |
7315 | } | |
7316 | ||
7317 | // (protected) return x s.t. r^x < DV | |
7318 | function bnpChunkSize(r) { | |
7319 | return Math.floor(Math.LN2 * this.DB / Math.log(r)) | |
7320 | } | |
7321 | ||
7322 | // (public) 0 if this == 0, 1 if this > 0 | |
7323 | function bnSigNum() { | |
7324 | if (this.s < 0) return -1 | |
7325 | else if (this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0 | |
7326 | else return 1 | |
7327 | } | |
7328 | ||
7329 | // (protected) convert to radix string | |
7330 | function bnpToRadix(b) { | |
7331 | if (b == null) b = 10 | |
7332 | if (this.signum() == 0 || b < 2 || b > 36) return "0" | |
7333 | var cs = this.chunkSize(b) | |
7334 | var a = Math.pow(b, cs) | |
7335 | var d = nbv(a), | |
7336 | y = new BigInteger(), | |
7337 | z = new BigInteger(), | |
7338 | r = "" | |
7339 | this.divRemTo(d, y, z) | |
7340 | while (y.signum() > 0) { | |
7341 | r = (a + z.intValue()) | |
7342 | .toString(b) | |
7343 | .substr(1) + r | |
7344 | y.divRemTo(d, y, z) | |
7345 | } | |
7346 | return z.intValue() | |
7347 | .toString(b) + r | |
7348 | } | |
7349 | ||
7350 | // (protected) convert from radix string | |
7351 | function bnpFromRadix(s, b) { | |
7352 | var self = this | |
7353 | self.fromInt(0) | |
7354 | if (b == null) b = 10 | |
7355 | var cs = self.chunkSize(b) | |
7356 | var d = Math.pow(b, cs), | |
7357 | mi = false, | |
7358 | j = 0, | |
7359 | w = 0 | |
7360 | for (var i = 0; i < s.length; ++i) { | |
7361 | var x = intAt(s, i) | |
7362 | if (x < 0) { | |
7363 | if (s.charAt(i) == "-" && self.signum() == 0) mi = true | |
7364 | continue | |
7365 | } | |
7366 | w = b * w + x | |
7367 | if (++j >= cs) { | |
7368 | self.dMultiply(d) | |
7369 | self.dAddOffset(w, 0) | |
7370 | j = 0 | |
7371 | w = 0 | |
7372 | } | |
7373 | } | |
7374 | if (j > 0) { | |
7375 | self.dMultiply(Math.pow(b, j)) | |
7376 | self.dAddOffset(w, 0) | |
7377 | } | |
7378 | if (mi) BigInteger.ZERO.subTo(self, self) | |
7379 | } | |
7380 | ||
7381 | // (protected) alternate constructor | |
7382 | function bnpFromNumber(a, b, c) { | |
7383 | var self = this | |
7384 | if ("number" == typeof b) { | |
7385 | // new BigInteger(int,int,RNG) | |
7386 | if (a < 2) self.fromInt(1) | |
7387 | else { | |
7388 | self.fromNumber(a, c) | |
7389 | if (!self.testBit(a - 1)) // force MSB set | |
7390 | self.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, self) | |
7391 | if (self.isEven()) self.dAddOffset(1, 0); // force odd | |
7392 | while (!self.isProbablePrime(b)) { | |
7393 | self.dAddOffset(2, 0) | |
7394 | if (self.bitLength() > a) self.subTo(BigInteger.ONE.shiftLeft(a - 1), self) | |
7395 | } | |
7396 | } | |
7397 | } else { | |
7398 | // new BigInteger(int,RNG) | |
7399 | var x = new Array(), | |
7400 | t = a & 7 | |
7401 | x.length = (a >> 3) + 1 | |
7402 | b.nextBytes(x) | |
7403 | if (t > 0) x[0] &= ((1 << t) - 1) | |
7404 | else x[0] = 0 | |
7405 | self.fromString(x, 256) | |
7406 | } | |
7407 | } | |
7408 | ||
7409 | // (public) convert to bigendian byte array | |
7410 | function bnToByteArray() { | |
7411 | var self = this | |
7412 | var i = self.t, | |
7413 | r = new Array() | |
7414 | r[0] = self.s | |
7415 | var p = self.DB - (i * self.DB) % 8, | |
7416 | d, k = 0 | |
7417 | if (i-- > 0) { | |
7418 | if (p < self.DB && (d = self[i] >> p) != (self.s & self.DM) >> p) | |
7419 | r[k++] = d | (self.s << (self.DB - p)) | |
7420 | while (i >= 0) { | |
7421 | if (p < 8) { | |
7422 | d = (self[i] & ((1 << p) - 1)) << (8 - p) | |
7423 | d |= self[--i] >> (p += self.DB - 8) | |
7424 | } else { | |
7425 | d = (self[i] >> (p -= 8)) & 0xff | |
7426 | if (p <= 0) { | |
7427 | p += self.DB | |
7428 | --i | |
7429 | } | |
7430 | } | |
7431 | if ((d & 0x80) != 0) d |= -256 | |
7432 | if (k === 0 && (self.s & 0x80) != (d & 0x80))++k | |
7433 | if (k > 0 || d != self.s) r[k++] = d | |
7434 | } | |
7435 | } | |
7436 | return r | |
7437 | } | |
7438 | ||
7439 | function bnEquals(a) { | |
7440 | return (this.compareTo(a) == 0) | |
7441 | } | |
7442 | ||
7443 | function bnMin(a) { | |
7444 | return (this.compareTo(a) < 0) ? this : a | |
7445 | } | |
7446 | ||
7447 | function bnMax(a) { | |
7448 | return (this.compareTo(a) > 0) ? this : a | |
7449 | } | |
7450 | ||
7451 | // (protected) r = this op a (bitwise) | |
7452 | function bnpBitwiseTo(a, op, r) { | |
7453 | var self = this | |
7454 | var i, f, m = Math.min(a.t, self.t) | |
7455 | for (i = 0; i < m; ++i) r[i] = op(self[i], a[i]) | |
7456 | if (a.t < self.t) { | |
7457 | f = a.s & self.DM | |
7458 | for (i = m; i < self.t; ++i) r[i] = op(self[i], f) | |
7459 | r.t = self.t | |
7460 | } else { | |
7461 | f = self.s & self.DM | |
7462 | for (i = m; i < a.t; ++i) r[i] = op(f, a[i]) | |
7463 | r.t = a.t | |
7464 | } | |
7465 | r.s = op(self.s, a.s) | |
7466 | r.clamp() | |
7467 | } | |
7468 | ||
7469 | // (public) this & a | |
7470 | function op_and(x, y) { | |
7471 | return x & y | |
7472 | } | |
7473 | ||
7474 | function bnAnd(a) { | |
7475 | var r = new BigInteger() | |
7476 | this.bitwiseTo(a, op_and, r) | |
7477 | return r | |
7478 | } | |
7479 | ||
7480 | // (public) this | a | |
7481 | function op_or(x, y) { | |
7482 | return x | y | |
7483 | } | |
7484 | ||
7485 | function bnOr(a) { | |
7486 | var r = new BigInteger() | |
7487 | this.bitwiseTo(a, op_or, r) | |
7488 | return r | |
7489 | } | |
7490 | ||
7491 | // (public) this ^ a | |
7492 | function op_xor(x, y) { | |
7493 | return x ^ y | |
7494 | } | |
7495 | ||
7496 | function bnXor(a) { | |
7497 | var r = new BigInteger() | |
7498 | this.bitwiseTo(a, op_xor, r) | |
7499 | return r | |
7500 | } | |
7501 | ||
7502 | // (public) this & ~a | |
7503 | function op_andnot(x, y) { | |
7504 | return x & ~y | |
7505 | } | |
7506 | ||
7507 | function bnAndNot(a) { | |
7508 | var r = new BigInteger() | |
7509 | this.bitwiseTo(a, op_andnot, r) | |
7510 | return r | |
7511 | } | |
7512 | ||
7513 | // (public) ~this | |
7514 | function bnNot() { | |
7515 | var r = new BigInteger() | |
7516 | for (var i = 0; i < this.t; ++i) r[i] = this.DM & ~this[i] | |
7517 | r.t = this.t | |
7518 | r.s = ~this.s | |
7519 | return r | |
7520 | } | |
7521 | ||
7522 | // (public) this << n | |
7523 | function bnShiftLeft(n) { | |
7524 | var r = new BigInteger() | |
7525 | if (n < 0) this.rShiftTo(-n, r) | |
7526 | else this.lShiftTo(n, r) | |
7527 | return r | |
7528 | } | |
7529 | ||
7530 | // (public) this >> n | |
7531 | function bnShiftRight(n) { | |
7532 | var r = new BigInteger() | |
7533 | if (n < 0) this.lShiftTo(-n, r) | |
7534 | else this.rShiftTo(n, r) | |
7535 | return r | |
7536 | } | |
7537 | ||
7538 | // return index of lowest 1-bit in x, x < 2^31 | |
7539 | function lbit(x) { | |
7540 | if (x == 0) return -1 | |
7541 | var r = 0 | |
7542 | if ((x & 0xffff) == 0) { | |
7543 | x >>= 16 | |
7544 | r += 16 | |
7545 | } | |
7546 | if ((x & 0xff) == 0) { | |
7547 | x >>= 8 | |
7548 | r += 8 | |
7549 | } | |
7550 | if ((x & 0xf) == 0) { | |
7551 | x >>= 4 | |
7552 | r += 4 | |
7553 | } | |
7554 | if ((x & 3) == 0) { | |
7555 | x >>= 2 | |
7556 | r += 2 | |
7557 | } | |
7558 | if ((x & 1) == 0)++r | |
7559 | return r | |
7560 | } | |
7561 | ||
7562 | // (public) returns index of lowest 1-bit (or -1 if none) | |
7563 | function bnGetLowestSetBit() { | |
7564 | for (var i = 0; i < this.t; ++i) | |
7565 | if (this[i] != 0) return i * this.DB + lbit(this[i]) | |
7566 | if (this.s < 0) return this.t * this.DB | |
7567 | return -1 | |
7568 | } | |
7569 | ||
7570 | // return number of 1 bits in x | |
7571 | function cbit(x) { | |
7572 | var r = 0 | |
7573 | while (x != 0) { | |
7574 | x &= x - 1 | |
7575 | ++r | |
7576 | } | |
7577 | return r | |
7578 | } | |
7579 | ||
7580 | // (public) return number of set bits | |
7581 | function bnBitCount() { | |
7582 | var r = 0, | |
7583 | x = this.s & this.DM | |
7584 | for (var i = 0; i < this.t; ++i) r += cbit(this[i] ^ x) | |
7585 | return r | |
7586 | } | |
7587 | ||
7588 | // (public) true iff nth bit is set | |
7589 | function bnTestBit(n) { | |
7590 | var j = Math.floor(n / this.DB) | |
7591 | if (j >= this.t) return (this.s != 0) | |
7592 | return ((this[j] & (1 << (n % this.DB))) != 0) | |
7593 | } | |
7594 | ||
7595 | // (protected) this op (1<<n) | |
7596 | function bnpChangeBit(n, op) { | |
7597 | var r = BigInteger.ONE.shiftLeft(n) | |
7598 | this.bitwiseTo(r, op, r) | |
7599 | return r | |
7600 | } | |
7601 | ||
7602 | // (public) this | (1<<n) | |
7603 | function bnSetBit(n) { | |
7604 | return this.changeBit(n, op_or) | |
7605 | } | |
7606 | ||
7607 | // (public) this & ~(1<<n) | |
7608 | function bnClearBit(n) { | |
7609 | return this.changeBit(n, op_andnot) | |
7610 | } | |
7611 | ||
7612 | // (public) this ^ (1<<n) | |
7613 | function bnFlipBit(n) { | |
7614 | return this.changeBit(n, op_xor) | |
7615 | } | |
7616 | ||
7617 | // (protected) r = this + a | |
7618 | function bnpAddTo(a, r) { | |
7619 | var self = this | |
7620 | ||
7621 | var i = 0, | |
7622 | c = 0, | |
7623 | m = Math.min(a.t, self.t) | |
7624 | while (i < m) { | |
7625 | c += self[i] + a[i] | |
7626 | r[i++] = c & self.DM | |
7627 | c >>= self.DB | |
7628 | } | |
7629 | if (a.t < self.t) { | |
7630 | c += a.s | |
7631 | while (i < self.t) { | |
7632 | c += self[i] | |
7633 | r[i++] = c & self.DM | |
7634 | c >>= self.DB | |
7635 | } | |
7636 | c += self.s | |
7637 | } else { | |
7638 | c += self.s | |
7639 | while (i < a.t) { | |
7640 | c += a[i] | |
7641 | r[i++] = c & self.DM | |
7642 | c >>= self.DB | |
7643 | } | |
7644 | c += a.s | |
7645 | } | |
7646 | r.s = (c < 0) ? -1 : 0 | |
7647 | if (c > 0) r[i++] = c | |
7648 | else if (c < -1) r[i++] = self.DV + c | |
7649 | r.t = i | |
7650 | r.clamp() | |
7651 | } | |
7652 | ||
7653 | // (public) this + a | |
7654 | function bnAdd(a) { | |
7655 | var r = new BigInteger() | |
7656 | this.addTo(a, r) | |
7657 | return r | |
7658 | } | |
7659 | ||
7660 | // (public) this - a | |
7661 | function bnSubtract(a) { | |
7662 | var r = new BigInteger() | |
7663 | this.subTo(a, r) | |
7664 | return r | |
7665 | } | |
7666 | ||
7667 | // (public) this * a | |
7668 | function bnMultiply(a) { | |
7669 | var r = new BigInteger() | |
7670 | this.multiplyTo(a, r) | |
7671 | return r | |
7672 | } | |
7673 | ||
7674 | // (public) this^2 | |
7675 | function bnSquare() { | |
7676 | var r = new BigInteger() | |
7677 | this.squareTo(r) | |
7678 | return r | |
7679 | } | |
7680 | ||
7681 | // (public) this / a | |
7682 | function bnDivide(a) { | |
7683 | var r = new BigInteger() | |
7684 | this.divRemTo(a, r, null) | |
7685 | return r | |
7686 | } | |
7687 | ||
7688 | // (public) this % a | |
7689 | function bnRemainder(a) { | |
7690 | var r = new BigInteger() | |
7691 | this.divRemTo(a, null, r) | |
7692 | return r | |
7693 | } | |
7694 | ||
7695 | // (public) [this/a,this%a] | |
7696 | function bnDivideAndRemainder(a) { | |
7697 | var q = new BigInteger(), | |
7698 | r = new BigInteger() | |
7699 | this.divRemTo(a, q, r) | |
7700 | return new Array(q, r) | |
7701 | } | |
7702 | ||
7703 | // (protected) this *= n, this >= 0, 1 < n < DV | |
7704 | function bnpDMultiply(n) { | |
7705 | this[this.t] = this.am(0, n - 1, this, 0, 0, this.t) | |
7706 | ++this.t | |
7707 | this.clamp() | |
7708 | } | |
7709 | ||
7710 | // (protected) this += n << w words, this >= 0 | |
7711 | function bnpDAddOffset(n, w) { | |
7712 | if (n == 0) return | |
7713 | while (this.t <= w) this[this.t++] = 0 | |
7714 | this[w] += n | |
7715 | while (this[w] >= this.DV) { | |
7716 | this[w] -= this.DV | |
7717 | if (++w >= this.t) this[this.t++] = 0 | |
7718 | ++this[w] | |
7719 | } | |
7720 | } | |
7721 | ||
7722 | // A "null" reducer | |
7723 | function NullExp() {} | |
7724 | ||
7725 | function nNop(x) { | |
7726 | return x | |
7727 | } | |
7728 | ||
7729 | function nMulTo(x, y, r) { | |
7730 | x.multiplyTo(y, r) | |
7731 | } | |
7732 | ||
7733 | function nSqrTo(x, r) { | |
7734 | x.squareTo(r) | |
7735 | } | |
7736 | ||
7737 | NullExp.prototype.convert = nNop | |
7738 | NullExp.prototype.revert = nNop | |
7739 | NullExp.prototype.mulTo = nMulTo | |
7740 | NullExp.prototype.sqrTo = nSqrTo | |
7741 | ||
7742 | // (public) this^e | |
7743 | function bnPow(e) { | |
7744 | return this.exp(e, new NullExp()) | |
7745 | } | |
7746 | ||
7747 | // (protected) r = lower n words of "this * a", a.t <= n | |
7748 | // "this" should be the larger one if appropriate. | |
7749 | function bnpMultiplyLowerTo(a, n, r) { | |
7750 | var i = Math.min(this.t + a.t, n) | |
7751 | r.s = 0; // assumes a,this >= 0 | |
7752 | r.t = i | |
7753 | while (i > 0) r[--i] = 0 | |
7754 | var j | |
7755 | for (j = r.t - this.t; i < j; ++i) r[i + this.t] = this.am(0, a[i], r, i, 0, this.t) | |
7756 | for (j = Math.min(a.t, n); i < j; ++i) this.am(0, a[i], r, i, 0, n - i) | |
7757 | r.clamp() | |
7758 | } | |
7759 | ||
7760 | // (protected) r = "this * a" without lower n words, n > 0 | |
7761 | // "this" should be the larger one if appropriate. | |
7762 | function bnpMultiplyUpperTo(a, n, r) { | |
7763 | --n | |
7764 | var i = r.t = this.t + a.t - n | |
7765 | r.s = 0; // assumes a,this >= 0 | |
7766 | while (--i >= 0) r[i] = 0 | |
7767 | for (i = Math.max(n - this.t, 0); i < a.t; ++i) | |
7768 | r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n) | |
7769 | r.clamp() | |
7770 | r.drShiftTo(1, r) | |
7771 | } | |
7772 | ||
7773 | // Barrett modular reduction | |
7774 | function Barrett(m) { | |
7775 | // setup Barrett | |
7776 | this.r2 = new BigInteger() | |
7777 | this.q3 = new BigInteger() | |
7778 | BigInteger.ONE.dlShiftTo(2 * m.t, this.r2) | |
7779 | this.mu = this.r2.divide(m) | |
7780 | this.m = m | |
7781 | } | |
7782 | ||
7783 | function barrettConvert(x) { | |
7784 | if (x.s < 0 || x.t > 2 * this.m.t) return x.mod(this.m) | |
7785 | else if (x.compareTo(this.m) < 0) return x | |
7786 | else { | |
7787 | var r = new BigInteger() | |
7788 | x.copyTo(r) | |
7789 | this.reduce(r) | |
7790 | return r | |
7791 | } | |
7792 | } | |
7793 | ||
7794 | function barrettRevert(x) { | |
7795 | return x | |
7796 | } | |
7797 | ||
7798 | // x = x mod m (HAC 14.42) | |
7799 | function barrettReduce(x) { | |
7800 | var self = this | |
7801 | x.drShiftTo(self.m.t - 1, self.r2) | |
7802 | if (x.t > self.m.t + 1) { | |
7803 | x.t = self.m.t + 1 | |
7804 | x.clamp() | |
7805 | } | |
7806 | self.mu.multiplyUpperTo(self.r2, self.m.t + 1, self.q3) | |
7807 | self.m.multiplyLowerTo(self.q3, self.m.t + 1, self.r2) | |
7808 | while (x.compareTo(self.r2) < 0) x.dAddOffset(1, self.m.t + 1) | |
7809 | x.subTo(self.r2, x) | |
7810 | while (x.compareTo(self.m) >= 0) x.subTo(self.m, x) | |
7811 | } | |
7812 | ||
7813 | // r = x^2 mod m; x != r | |
7814 | function barrettSqrTo(x, r) { | |
7815 | x.squareTo(r) | |
7816 | this.reduce(r) | |
7817 | } | |
7818 | ||
7819 | // r = x*y mod m; x,y != r | |
7820 | function barrettMulTo(x, y, r) { | |
7821 | x.multiplyTo(y, r) | |
7822 | this.reduce(r) | |
7823 | } | |
7824 | ||
7825 | Barrett.prototype.convert = barrettConvert | |
7826 | Barrett.prototype.revert = barrettRevert | |
7827 | Barrett.prototype.reduce = barrettReduce | |
7828 | Barrett.prototype.mulTo = barrettMulTo | |
7829 | Barrett.prototype.sqrTo = barrettSqrTo | |
7830 | ||
7831 | // (public) this^e % m (HAC 14.85) | |
7832 | function bnModPow(e, m) { | |
7833 | var i = e.bitLength(), | |
7834 | k, r = nbv(1), | |
7835 | z | |
7836 | if (i <= 0) return r | |
7837 | else if (i < 18) k = 1 | |
7838 | else if (i < 48) k = 3 | |
7839 | else if (i < 144) k = 4 | |
7840 | else if (i < 768) k = 5 | |
7841 | else k = 6 | |
7842 | if (i < 8) | |
7843 | z = new Classic(m) | |
7844 | else if (m.isEven()) | |
7845 | z = new Barrett(m) | |
7846 | else | |
7847 | z = new Montgomery(m) | |
7848 | ||
7849 | // precomputation | |
7850 | var g = new Array(), | |
7851 | n = 3, | |
7852 | k1 = k - 1, | |
7853 | km = (1 << k) - 1 | |
7854 | g[1] = z.convert(this) | |
7855 | if (k > 1) { | |
7856 | var g2 = new BigInteger() | |
7857 | z.sqrTo(g[1], g2) | |
7858 | while (n <= km) { | |
7859 | g[n] = new BigInteger() | |
7860 | z.mulTo(g2, g[n - 2], g[n]) | |
7861 | n += 2 | |
7862 | } | |
7863 | } | |
7864 | ||
7865 | var j = e.t - 1, | |
7866 | w, is1 = true, | |
7867 | r2 = new BigInteger(), | |
7868 | t | |
7869 | i = nbits(e[j]) - 1 | |
7870 | while (j >= 0) { | |
7871 | if (i >= k1) w = (e[j] >> (i - k1)) & km | |
7872 | else { | |
7873 | w = (e[j] & ((1 << (i + 1)) - 1)) << (k1 - i) | |
7874 | if (j > 0) w |= e[j - 1] >> (this.DB + i - k1) | |
7875 | } | |
7876 | ||
7877 | n = k | |
7878 | while ((w & 1) == 0) { | |
7879 | w >>= 1 | |
7880 | --n | |
7881 | } | |
7882 | if ((i -= n) < 0) { | |
7883 | i += this.DB | |
7884 | --j | |
7885 | } | |
7886 | if (is1) { // ret == 1, don't bother squaring or multiplying it | |
7887 | g[w].copyTo(r) | |
7888 | is1 = false | |
7889 | } else { | |
7890 | while (n > 1) { | |
7891 | z.sqrTo(r, r2) | |
7892 | z.sqrTo(r2, r) | |
7893 | n -= 2 | |
7894 | } | |
7895 | if (n > 0) z.sqrTo(r, r2) | |
7896 | else { | |
7897 | t = r | |
7898 | r = r2 | |
7899 | r2 = t | |
7900 | } | |
7901 | z.mulTo(r2, g[w], r) | |
7902 | } | |
7903 | ||
7904 | while (j >= 0 && (e[j] & (1 << i)) == 0) { | |
7905 | z.sqrTo(r, r2) | |
7906 | t = r | |
7907 | r = r2 | |
7908 | r2 = t | |
7909 | if (--i < 0) { | |
7910 | i = this.DB - 1 | |
7911 | --j | |
7912 | } | |
7913 | } | |
7914 | } | |
7915 | return z.revert(r) | |
7916 | } | |
7917 | ||
7918 | // (public) gcd(this,a) (HAC 14.54) | |
7919 | function bnGCD(a) { | |
7920 | var x = (this.s < 0) ? this.negate() : this.clone() | |
7921 | var y = (a.s < 0) ? a.negate() : a.clone() | |
7922 | if (x.compareTo(y) < 0) { | |
7923 | var t = x | |
7924 | x = y | |
7925 | y = t | |
7926 | } | |
7927 | var i = x.getLowestSetBit(), | |
7928 | g = y.getLowestSetBit() | |
7929 | if (g < 0) return x | |
7930 | if (i < g) g = i | |
7931 | if (g > 0) { | |
7932 | x.rShiftTo(g, x) | |
7933 | y.rShiftTo(g, y) | |
7934 | } | |
7935 | while (x.signum() > 0) { | |
7936 | if ((i = x.getLowestSetBit()) > 0) x.rShiftTo(i, x) | |
7937 | if ((i = y.getLowestSetBit()) > 0) y.rShiftTo(i, y) | |
7938 | if (x.compareTo(y) >= 0) { | |
7939 | x.subTo(y, x) | |
7940 | x.rShiftTo(1, x) | |
7941 | } else { | |
7942 | y.subTo(x, y) | |
7943 | y.rShiftTo(1, y) | |
7944 | } | |
7945 | } | |
7946 | if (g > 0) y.lShiftTo(g, y) | |
7947 | return y | |
7948 | } | |
7949 | ||
7950 | // (protected) this % n, n < 2^26 | |
7951 | function bnpModInt(n) { | |
7952 | if (n <= 0) return 0 | |
7953 | var d = this.DV % n, | |
7954 | r = (this.s < 0) ? n - 1 : 0 | |
7955 | if (this.t > 0) | |
7956 | if (d == 0) r = this[0] % n | |
7957 | else | |
7958 | for (var i = this.t - 1; i >= 0; --i) r = (d * r + this[i]) % n | |
7959 | return r | |
7960 | } | |
7961 | ||
7962 | // (public) 1/this % m (HAC 14.61) | |
7963 | function bnModInverse(m) { | |
7964 | var ac = m.isEven() | |
7965 | if (this.signum() === 0) throw new Error('division by zero') | |
7966 | if ((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO | |
7967 | var u = m.clone(), | |
7968 | v = this.clone() | |
7969 | var a = nbv(1), | |
7970 | b = nbv(0), | |
7971 | c = nbv(0), | |
7972 | d = nbv(1) | |
7973 | while (u.signum() != 0) { | |
7974 | while (u.isEven()) { | |
7975 | u.rShiftTo(1, u) | |
7976 | if (ac) { | |
7977 | if (!a.isEven() || !b.isEven()) { | |
7978 | a.addTo(this, a) | |
7979 | b.subTo(m, b) | |
7980 | } | |
7981 | a.rShiftTo(1, a) | |
7982 | } else if (!b.isEven()) b.subTo(m, b) | |
7983 | b.rShiftTo(1, b) | |
7984 | } | |
7985 | while (v.isEven()) { | |
7986 | v.rShiftTo(1, v) | |
7987 | if (ac) { | |
7988 | if (!c.isEven() || !d.isEven()) { | |
7989 | c.addTo(this, c) | |
7990 | d.subTo(m, d) | |
7991 | } | |
7992 | c.rShiftTo(1, c) | |
7993 | } else if (!d.isEven()) d.subTo(m, d) | |
7994 | d.rShiftTo(1, d) | |
7995 | } | |
7996 | if (u.compareTo(v) >= 0) { | |
7997 | u.subTo(v, u) | |
7998 | if (ac) a.subTo(c, a) | |
7999 | b.subTo(d, b) | |
8000 | } else { | |
8001 | v.subTo(u, v) | |
8002 | if (ac) c.subTo(a, c) | |
8003 | d.subTo(b, d) | |
8004 | } | |
8005 | } | |
8006 | if (v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO | |
8007 | while (d.compareTo(m) >= 0) d.subTo(m, d) | |
8008 | while (d.signum() < 0) d.addTo(m, d) | |
8009 | return d | |
8010 | } | |
8011 | ||
8012 | var lowprimes = [ | |
8013 | 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, | |
8014 | 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, | |
8015 | 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, | |
8016 | 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, | |
8017 | 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, | |
8018 | 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, | |
8019 | 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, | |
8020 | 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, | |
8021 | 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, | |
8022 | 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, | |
8023 | 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 | |
8024 | ] | |
8025 | ||
8026 | var lplim = (1 << 26) / lowprimes[lowprimes.length - 1] | |
8027 | ||
8028 | // (public) test primality with certainty >= 1-.5^t | |
8029 | function bnIsProbablePrime(t) { | |
8030 | var i, x = this.abs() | |
8031 | if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) { | |
8032 | for (i = 0; i < lowprimes.length; ++i) | |
8033 | if (x[0] == lowprimes[i]) return true | |
8034 | return false | |
8035 | } | |
8036 | if (x.isEven()) return false | |
8037 | i = 1 | |
8038 | while (i < lowprimes.length) { | |
8039 | var m = lowprimes[i], | |
8040 | j = i + 1 | |
8041 | while (j < lowprimes.length && m < lplim) m *= lowprimes[j++] | |
8042 | m = x.modInt(m) | |
8043 | while (i < j) if (m % lowprimes[i++] == 0) return false | |
8044 | } | |
8045 | return x.millerRabin(t) | |
8046 | } | |
8047 | ||
8048 | // (protected) true if probably prime (HAC 4.24, Miller-Rabin) | |
8049 | function bnpMillerRabin(t) { | |
8050 | var n1 = this.subtract(BigInteger.ONE) | |
8051 | var k = n1.getLowestSetBit() | |
8052 | if (k <= 0) return false | |
8053 | var r = n1.shiftRight(k) | |
8054 | t = (t + 1) >> 1 | |
8055 | if (t > lowprimes.length) t = lowprimes.length | |
8056 | var a = new BigInteger(null) | |
8057 | var j, bases = [] | |
8058 | for (var i = 0; i < t; ++i) { | |
8059 | for (;;) { | |
8060 | j = lowprimes[Math.floor(Math.random() * lowprimes.length)] | |
8061 | if (bases.indexOf(j) == -1) break | |
8062 | } | |
8063 | bases.push(j) | |
8064 | a.fromInt(j) | |
8065 | var y = a.modPow(r, this) | |
8066 | if (y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) { | |
8067 | var j = 1 | |
8068 | while (j++ < k && y.compareTo(n1) != 0) { | |
8069 | y = y.modPowInt(2, this) | |
8070 | if (y.compareTo(BigInteger.ONE) == 0) return false | |
8071 | } | |
8072 | if (y.compareTo(n1) != 0) return false | |
8073 | } | |
8074 | } | |
8075 | return true | |
8076 | } | |
8077 | ||
8078 | // protected | |
8079 | proto.chunkSize = bnpChunkSize | |
8080 | proto.toRadix = bnpToRadix | |
8081 | proto.fromRadix = bnpFromRadix | |
8082 | proto.fromNumber = bnpFromNumber | |
8083 | proto.bitwiseTo = bnpBitwiseTo | |
8084 | proto.changeBit = bnpChangeBit | |
8085 | proto.addTo = bnpAddTo | |
8086 | proto.dMultiply = bnpDMultiply | |
8087 | proto.dAddOffset = bnpDAddOffset | |
8088 | proto.multiplyLowerTo = bnpMultiplyLowerTo | |
8089 | proto.multiplyUpperTo = bnpMultiplyUpperTo | |
8090 | proto.modInt = bnpModInt | |
8091 | proto.millerRabin = bnpMillerRabin | |
8092 | ||
8093 | // public | |
8094 | proto.clone = bnClone | |
8095 | proto.intValue = bnIntValue | |
8096 | proto.byteValue = bnByteValue | |
8097 | proto.shortValue = bnShortValue | |
8098 | proto.signum = bnSigNum | |
8099 | proto.toByteArray = bnToByteArray | |
8100 | proto.equals = bnEquals | |
8101 | proto.min = bnMin | |
8102 | proto.max = bnMax | |
8103 | proto.and = bnAnd | |
8104 | proto.or = bnOr | |
8105 | proto.xor = bnXor | |
8106 | proto.andNot = bnAndNot | |
8107 | proto.not = bnNot | |
8108 | proto.shiftLeft = bnShiftLeft | |
8109 | proto.shiftRight = bnShiftRight | |
8110 | proto.getLowestSetBit = bnGetLowestSetBit | |
8111 | proto.bitCount = bnBitCount | |
8112 | proto.testBit = bnTestBit | |
8113 | proto.setBit = bnSetBit | |
8114 | proto.clearBit = bnClearBit | |
8115 | proto.flipBit = bnFlipBit | |
8116 | proto.add = bnAdd | |
8117 | proto.subtract = bnSubtract | |
8118 | proto.multiply = bnMultiply | |
8119 | proto.divide = bnDivide | |
8120 | proto.remainder = bnRemainder | |
8121 | proto.divideAndRemainder = bnDivideAndRemainder | |
8122 | proto.modPow = bnModPow | |
8123 | proto.modInverse = bnModInverse | |
8124 | proto.pow = bnPow | |
8125 | proto.gcd = bnGCD | |
8126 | proto.isProbablePrime = bnIsProbablePrime | |
8127 | ||
8128 | // JSBN-specific extension | |
8129 | proto.square = bnSquare | |
8130 | ||
8131 | // constants | |
8132 | BigInteger.ZERO = nbv(0) | |
8133 | BigInteger.ONE = nbv(1) | |
8134 | BigInteger.valueOf = nbv | |
8135 | ||
8136 | module.exports = BigInteger | |
8137 | ||
9f59e99b | 8138 | },{"../package.json":40}],38:[function(require,module,exports){ |
a0091a40 IC |
8139 | (function (Buffer){ |
8140 | // FIXME: Kind of a weird way to throw exceptions, consider removing | |
8141 | var assert = require('assert') | |
8142 | var BigInteger = require('./bigi') | |
8143 | ||
8144 | /** | |
8145 | * Turns a byte array into a big integer. | |
8146 | * | |
8147 | * This function will interpret a byte array as a big integer in big | |
8148 | * endian notation. | |
8149 | */ | |
8150 | BigInteger.fromByteArrayUnsigned = function(byteArray) { | |
8151 | // BigInteger expects a DER integer conformant byte array | |
8152 | if (byteArray[0] & 0x80) { | |
8153 | return new BigInteger([0].concat(byteArray)) | |
8154 | } | |
8155 | ||
8156 | return new BigInteger(byteArray) | |
8157 | } | |
8158 | ||
8159 | /** | |
8160 | * Returns a byte array representation of the big integer. | |
8161 | * | |
8162 | * This returns the absolute of the contained value in big endian | |
8163 | * form. A value of zero results in an empty array. | |
8164 | */ | |
8165 | BigInteger.prototype.toByteArrayUnsigned = function() { | |
8166 | var byteArray = this.toByteArray() | |
8167 | return byteArray[0] === 0 ? byteArray.slice(1) : byteArray | |
8168 | } | |
8169 | ||
8170 | BigInteger.fromDERInteger = function(byteArray) { | |
8171 | return new BigInteger(byteArray) | |
8172 | } | |
8173 | ||
8174 | /* | |
8175 | * Converts BigInteger to a DER integer representation. | |
8176 | * | |
8177 | * The format for this value uses the most significant bit as a sign | |
8178 | * bit. If the most significant bit is already set and the integer is | |
8179 | * positive, a 0x00 is prepended. | |
8180 | * | |
8181 | * Examples: | |
8182 | * | |
8183 | * 0 => 0x00 | |
8184 | * 1 => 0x01 | |
8185 | * -1 => 0xff | |
8186 | * 127 => 0x7f | |
8187 | * -127 => 0x81 | |
8188 | * 128 => 0x0080 | |
8189 | * -128 => 0x80 | |
8190 | * 255 => 0x00ff | |
8191 | * -255 => 0xff01 | |
8192 | * 16300 => 0x3fac | |
8193 | * -16300 => 0xc054 | |
8194 | * 62300 => 0x00f35c | |
8195 | * -62300 => 0xff0ca4 | |
8196 | */ | |
8197 | BigInteger.prototype.toDERInteger = BigInteger.prototype.toByteArray | |
8198 | ||
8199 | BigInteger.fromBuffer = function(buffer) { | |
8200 | // BigInteger expects a DER integer conformant byte array | |
8201 | if (buffer[0] & 0x80) { | |
8202 | var byteArray = Array.prototype.slice.call(buffer) | |
8203 | ||
8204 | return new BigInteger([0].concat(byteArray)) | |
8205 | } | |
8206 | ||
8207 | return new BigInteger(buffer) | |
8208 | } | |
8209 | ||
8210 | BigInteger.fromHex = function(hex) { | |
8211 | if (hex === '') return BigInteger.ZERO | |
8212 | ||
8213 | assert.equal(hex, hex.match(/^[A-Fa-f0-9]+/), 'Invalid hex string') | |
8214 | assert.equal(hex.length % 2, 0, 'Incomplete hex') | |
8215 | return new BigInteger(hex, 16) | |
8216 | } | |
8217 | ||
8218 | BigInteger.prototype.toBuffer = function(size) { | |
8219 | var byteArray = this.toByteArrayUnsigned() | |
8220 | var zeros = [] | |
8221 | ||
8222 | var padding = size - byteArray.length | |
8223 | while (zeros.length < padding) zeros.push(0) | |
8224 | ||
8225 | return new Buffer(zeros.concat(byteArray)) | |
8226 | } | |
8227 | ||
8228 | BigInteger.prototype.toHex = function(size) { | |
8229 | return this.toBuffer(size).toString('hex') | |
8230 | } | |
8231 | ||
8232 | }).call(this,require("buffer").Buffer) | |
9f59e99b | 8233 | },{"./bigi":37,"assert":1,"buffer":5}],39:[function(require,module,exports){ |
a0091a40 IC |
8234 | var BigInteger = require('./bigi') |
8235 | ||
8236 | //addons | |
8237 | require('./convert') | |
8238 | ||
8239 | module.exports = BigInteger | |
9f59e99b | 8240 | },{"./bigi":37,"./convert":38}],40:[function(require,module,exports){ |
a0091a40 IC |
8241 | module.exports={ |
8242 | "_args": [ | |
8243 | [ | |
8244 | "bigi", | |
8245 | "/home/ian/git/bitcoin/bitcoinjs-lib-browser" | |
8246 | ] | |
8247 | ], | |
8248 | "_from": "bigi@latest", | |
8249 | "_id": "bigi@1.4.2", | |
8250 | "_inCache": true, | |
8251 | "_installable": true, | |
8252 | "_location": "/bigi", | |
8253 | "_nodeVersion": "6.1.0", | |
8254 | "_npmOperationalInternal": { | |
8255 | "host": "packages-12-west.internal.npmjs.com", | |
8256 | "tmp": "tmp/bigi-1.4.2.tgz_1469584192413_0.6801238611806184" | |
8257 | }, | |
8258 | "_npmUser": { | |
8259 | "email": "jprichardson@gmail.com", | |
8260 | "name": "jprichardson" | |
8261 | }, | |
8262 | "_npmVersion": "3.8.6", | |
8263 | "_phantomChildren": {}, | |
8264 | "_requested": { | |
8265 | "name": "bigi", | |
8266 | "raw": "bigi", | |
8267 | "rawSpec": "", | |
8268 | "scope": null, | |
8269 | "spec": "latest", | |
8270 | "type": "tag" | |
8271 | }, | |
8272 | "_requiredBy": [ | |
8273 | "#USER", | |
8274 | "/bitcoinjs-lib", | |
8275 | "/ecurve" | |
8276 | ], | |
8277 | "_resolved": "https://registry.npmjs.org/bigi/-/bigi-1.4.2.tgz", | |
8278 | "_shasum": "9c665a95f88b8b08fc05cfd731f561859d725825", | |
8279 | "_shrinkwrap": null, | |
8280 | "_spec": "bigi", | |
8281 | "_where": "/home/ian/git/bitcoin/bitcoinjs-lib-browser", | |
8282 | "bugs": { | |
8283 | "url": "https://github.com/cryptocoinjs/bigi/issues" | |
8284 | }, | |
8285 | "dependencies": {}, | |
8286 | "description": "Big integers.", | |
8287 | "devDependencies": { | |
8288 | "coveralls": "^2.11.2", | |
8289 | "istanbul": "^0.3.5", | |
8290 | "jshint": "^2.5.1", | |
8291 | "mocha": "^2.1.0", | |
8292 | "mochify": "^2.1.0" | |
8293 | }, | |
8294 | "directories": {}, | |
8295 | "dist": { | |
8296 | "shasum": "9c665a95f88b8b08fc05cfd731f561859d725825", | |
8297 | "tarball": "https://registry.npmjs.org/bigi/-/bigi-1.4.2.tgz" | |
8298 | }, | |
8299 | "gitHead": "c25308081c896ff84702303722bf5ecd8b3f78e3", | |
8300 | "homepage": "https://github.com/cryptocoinjs/bigi#readme", | |
8301 | "keywords": [ | |
8302 | "cryptography", | |
8303 | "math", | |
8304 | "bitcoin", | |
8305 | "arbitrary", | |
8306 | "precision", | |
8307 | "arithmetic", | |
8308 | "big", | |
8309 | "integer", | |
8310 | "int", | |
8311 | "number", | |
8312 | "biginteger", | |
8313 | "bigint", | |
8314 | "bignumber", | |
8315 | "decimal", | |
8316 | "float" | |
8317 | ], | |
8318 | "main": "./lib/index.js", | |
8319 | "maintainers": [ | |
8320 | { | |
8321 | "email": "boydb@midnightdesign.ws", | |
8322 | "name": "midnightlightning" | |
8323 | }, | |
8324 | { | |
8325 | "email": "sidazhang89@gmail.com", | |
8326 | "name": "sidazhang" | |
8327 | }, | |
8328 | { | |
8329 | "email": "npm@shesek.info", | |
8330 | "name": "nadav" | |
8331 | }, | |
8332 | { | |
8333 | "email": "jprichardson@gmail.com", | |
8334 | "name": "jprichardson" | |
8335 | } | |
8336 | ], | |
8337 | "name": "bigi", | |
8338 | "optionalDependencies": {}, | |
8339 | "readme": "ERROR: No README data found!", | |
8340 | "repository": { | |
8341 | "type": "git", | |
8342 | "url": "git+https://github.com/cryptocoinjs/bigi.git" | |
8343 | }, | |
8344 | "scripts": { | |
8345 | "browser-test": "mochify --wd -R spec", | |
8346 | "coverage": "istanbul cover ./node_modules/.bin/_mocha -- --reporter list test/*.js", | |
8347 | "coveralls": "npm run-script coverage && node ./node_modules/.bin/coveralls < coverage/lcov.info", | |
8348 | "jshint": "jshint --config jshint.json lib/*.js ; true", | |
8349 | "test": "_mocha -- test/*.js", | |
8350 | "unit": "mocha" | |
8351 | }, | |
8352 | "testling": { | |
8353 | "browsers": [ | |
8354 | "ie/9..latest", | |
8355 | "firefox/latest", | |
8356 | "chrome/latest", | |
8357 | "safari/6.0..latest", | |
8358 | "iphone/6.0..latest", | |
8359 | "android-browser/4.2..latest" | |
8360 | ], | |
8361 | "files": "test/*.js", | |
8362 | "harness": "mocha" | |
8363 | }, | |
8364 | "version": "1.4.2" | |
8365 | } | |
8366 | ||
9f59e99b | 8367 | },{}],41:[function(require,module,exports){ |
a0091a40 IC |
8368 | // Reference https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki |
8369 | // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] | |
8370 | // NOTE: SIGHASH byte ignored AND restricted, truncate before use | |
8371 | ||
8372 | var Buffer = require('safe-buffer').Buffer | |
8373 | ||
8374 | function check (buffer) { | |
8375 | if (buffer.length < 8) return false | |
8376 | if (buffer.length > 72) return false | |
8377 | if (buffer[0] !== 0x30) return false | |
8378 | if (buffer[1] !== buffer.length - 2) return false | |
8379 | if (buffer[2] !== 0x02) return false | |
8380 | ||
8381 | var lenR = buffer[3] | |
8382 | if (lenR === 0) return false | |
8383 | if (5 + lenR >= buffer.length) return false | |
8384 | if (buffer[4 + lenR] !== 0x02) return false | |
8385 | ||
8386 | var lenS = buffer[5 + lenR] | |
8387 | if (lenS === 0) return false | |
8388 | if ((6 + lenR + lenS) !== buffer.length) return false | |
8389 | ||
8390 | if (buffer[4] & 0x80) return false | |
8391 | if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) return false | |
8392 | ||
8393 | if (buffer[lenR + 6] & 0x80) return false | |
8394 | if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) return false | |
8395 | return true | |
8396 | } | |
8397 | ||
8398 | function decode (buffer) { | |
8399 | if (buffer.length < 8) throw new Error('DER sequence length is too short') | |
8400 | if (buffer.length > 72) throw new Error('DER sequence length is too long') | |
8401 | if (buffer[0] !== 0x30) throw new Error('Expected DER sequence') | |
8402 | if (buffer[1] !== buffer.length - 2) throw new Error('DER sequence length is invalid') | |
8403 | if (buffer[2] !== 0x02) throw new Error('Expected DER integer') | |
8404 | ||
8405 | var lenR = buffer[3] | |
8406 | if (lenR === 0) throw new Error('R length is zero') | |
8407 | if (5 + lenR >= buffer.length) throw new Error('R length is too long') | |
8408 | if (buffer[4 + lenR] !== 0x02) throw new Error('Expected DER integer (2)') | |
8409 | ||
8410 | var lenS = buffer[5 + lenR] | |
8411 | if (lenS === 0) throw new Error('S length is zero') | |
8412 | if ((6 + lenR + lenS) !== buffer.length) throw new Error('S length is invalid') | |
8413 | ||
8414 | if (buffer[4] & 0x80) throw new Error('R value is negative') | |
8415 | if (lenR > 1 && (buffer[4] === 0x00) && !(buffer[5] & 0x80)) throw new Error('R value excessively padded') | |
8416 | ||
8417 | if (buffer[lenR + 6] & 0x80) throw new Error('S value is negative') | |
8418 | if (lenS > 1 && (buffer[lenR + 6] === 0x00) && !(buffer[lenR + 7] & 0x80)) throw new Error('S value excessively padded') | |
8419 | ||
8420 | // non-BIP66 - extract R, S values | |
8421 | return { | |
8422 | r: buffer.slice(4, 4 + lenR), | |
8423 | s: buffer.slice(6 + lenR) | |
8424 | } | |
8425 | } | |
8426 | ||
8427 | /* | |
8428 | * Expects r and s to be positive DER integers. | |
8429 | * | |
8430 | * The DER format uses the most significant bit as a sign bit (& 0x80). | |
8431 | * If the significant bit is set AND the integer is positive, a 0x00 is prepended. | |
8432 | * | |
8433 | * Examples: | |
8434 | * | |
8435 | * 0 => 0x00 | |
8436 | * 1 => 0x01 | |
8437 | * -1 => 0xff | |
8438 | * 127 => 0x7f | |
8439 | * -127 => 0x81 | |
8440 | * 128 => 0x0080 | |
8441 | * -128 => 0x80 | |
8442 | * 255 => 0x00ff | |
8443 | * -255 => 0xff01 | |
8444 | * 16300 => 0x3fac | |
8445 | * -16300 => 0xc054 | |
8446 | * 62300 => 0x00f35c | |
8447 | * -62300 => 0xff0ca4 | |
8448 | */ | |
8449 | function encode (r, s) { | |
8450 | var lenR = r.length | |
8451 | var lenS = s.length | |
8452 | if (lenR === 0) throw new Error('R length is zero') | |
8453 | if (lenS === 0) throw new Error('S length is zero') | |
8454 | if (lenR > 33) throw new Error('R length is too long') | |
8455 | if (lenS > 33) throw new Error('S length is too long') | |
8456 | if (r[0] & 0x80) throw new Error('R value is negative') | |
8457 | if (s[0] & 0x80) throw new Error('S value is negative') | |
8458 | if (lenR > 1 && (r[0] === 0x00) && !(r[1] & 0x80)) throw new Error('R value excessively padded') | |
8459 | if (lenS > 1 && (s[0] === 0x00) && !(s[1] & 0x80)) throw new Error('S value excessively padded') | |
8460 | ||
8461 | var signature = Buffer.allocUnsafe(6 + lenR + lenS) | |
8462 | ||
8463 | // 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] | |
8464 | signature[0] = 0x30 | |
8465 | signature[1] = signature.length - 2 | |
8466 | signature[2] = 0x02 | |
8467 | signature[3] = r.length | |
8468 | r.copy(signature, 4) | |
8469 | signature[4 + lenR] = 0x02 | |
8470 | signature[5 + lenR] = s.length | |
8471 | s.copy(signature, 6 + lenR) | |
8472 | ||
8473 | return signature | |
8474 | } | |
8475 | ||
8476 | module.exports = { | |
8477 | check: check, | |
8478 | decode: decode, | |
8479 | encode: encode | |
8480 | } | |
8481 | ||
9f59e99b | 8482 | },{"safe-buffer":128}],42:[function(require,module,exports){ |
a0091a40 IC |
8483 | module.exports={ |
8484 | "OP_FALSE": 0, | |
8485 | "OP_0": 0, | |
8486 | "OP_PUSHDATA1": 76, | |
8487 | "OP_PUSHDATA2": 77, | |
8488 | "OP_PUSHDATA4": 78, | |
8489 | "OP_1NEGATE": 79, | |
8490 | "OP_RESERVED": 80, | |
8491 | "OP_1": 81, | |
8492 | "OP_TRUE": 81, | |
8493 | "OP_2": 82, | |
8494 | "OP_3": 83, | |
8495 | "OP_4": 84, | |
8496 | "OP_5": 85, | |
8497 | "OP_6": 86, | |
8498 | "OP_7": 87, | |
8499 | "OP_8": 88, | |
8500 | "OP_9": 89, | |
8501 | "OP_10": 90, | |
8502 | "OP_11": 91, | |
8503 | "OP_12": 92, | |
8504 | "OP_13": 93, | |
8505 | "OP_14": 94, | |
8506 | "OP_15": 95, | |
8507 | "OP_16": 96, | |
8508 | ||
8509 | "OP_NOP": 97, | |
8510 | "OP_VER": 98, | |
8511 | "OP_IF": 99, | |
8512 | "OP_NOTIF": 100, | |
8513 | "OP_VERIF": 101, | |
8514 | "OP_VERNOTIF": 102, | |
8515 | "OP_ELSE": 103, | |
8516 | "OP_ENDIF": 104, | |
8517 | "OP_VERIFY": 105, | |
8518 | "OP_RETURN": 106, | |
8519 | ||
8520 | "OP_TOALTSTACK": 107, | |
8521 | "OP_FROMALTSTACK": 108, | |
8522 | "OP_2DROP": 109, | |
8523 | "OP_2DUP": 110, | |
8524 | "OP_3DUP": 111, | |
8525 | "OP_2OVER": 112, | |
8526 | "OP_2ROT": 113, | |
8527 | "OP_2SWAP": 114, | |
8528 | "OP_IFDUP": 115, | |
8529 | "OP_DEPTH": 116, | |
8530 | "OP_DROP": 117, | |
8531 | "OP_DUP": 118, | |
8532 | "OP_NIP": 119, | |
8533 | "OP_OVER": 120, | |
8534 | "OP_PICK": 121, | |
8535 | "OP_ROLL": 122, | |
8536 | "OP_ROT": 123, | |
8537 | "OP_SWAP": 124, | |
8538 | "OP_TUCK": 125, | |
8539 | ||
8540 | "OP_CAT": 126, | |
8541 | "OP_SUBSTR": 127, | |
8542 | "OP_LEFT": 128, | |
8543 | "OP_RIGHT": 129, | |
8544 | "OP_SIZE": 130, | |
8545 | ||
8546 | "OP_INVERT": 131, | |
8547 | "OP_AND": 132, | |
8548 | "OP_OR": 133, | |
8549 | "OP_XOR": 134, | |
8550 | "OP_EQUAL": 135, | |
8551 | "OP_EQUALVERIFY": 136, | |
8552 | "OP_RESERVED1": 137, | |
8553 | "OP_RESERVED2": 138, | |
8554 | ||
8555 | "OP_1ADD": 139, | |
8556 | "OP_1SUB": 140, | |
8557 | "OP_2MUL": 141, | |
8558 | "OP_2DIV": 142, | |
8559 | "OP_NEGATE": 143, | |
8560 | "OP_ABS": 144, | |
8561 | "OP_NOT": 145, | |
8562 | "OP_0NOTEQUAL": 146, | |
8563 | "OP_ADD": 147, | |
8564 | "OP_SUB": 148, | |
8565 | "OP_MUL": 149, | |
8566 | "OP_DIV": 150, | |
8567 | "OP_MOD": 151, | |
8568 | "OP_LSHIFT": 152, | |
8569 | "OP_RSHIFT": 153, | |
8570 | ||
8571 | "OP_BOOLAND": 154, | |
8572 | "OP_BOOLOR": 155, | |
8573 | "OP_NUMEQUAL": 156, | |
8574 | "OP_NUMEQUALVERIFY": 157, | |
8575 | "OP_NUMNOTEQUAL": 158, | |
8576 | "OP_LESSTHAN": 159, | |
8577 | "OP_GREATERTHAN": 160, | |
8578 | "OP_LESSTHANOREQUAL": 161, | |
8579 | "OP_GREATERTHANOREQUAL": 162, | |
8580 | "OP_MIN": 163, | |
8581 | "OP_MAX": 164, | |
8582 | ||
8583 | "OP_WITHIN": 165, | |
8584 | ||
8585 | "OP_RIPEMD160": 166, | |
8586 | "OP_SHA1": 167, | |
8587 | "OP_SHA256": 168, | |
8588 | "OP_HASH160": 169, | |
8589 | "OP_HASH256": 170, | |
8590 | "OP_CODESEPARATOR": 171, | |
8591 | "OP_CHECKSIG": 172, | |
8592 | "OP_CHECKSIGVERIFY": 173, | |
8593 | "OP_CHECKMULTISIG": 174, | |
8594 | "OP_CHECKMULTISIGVERIFY": 175, | |
8595 | ||
8596 | "OP_NOP1": 176, | |
8597 | "OP_NOP2": 177, | |
8598 | "OP_CHECKLOCKTIMEVERIFY": 177, | |
8599 | ||
8600 | "OP_NOP3": 178, | |
8601 | "OP_NOP4": 179, | |
8602 | "OP_NOP5": 180, | |
8603 | "OP_NOP6": 181, | |
8604 | "OP_NOP7": 182, | |
8605 | "OP_NOP8": 183, | |
8606 | "OP_NOP9": 184, | |
8607 | "OP_NOP10": 185, | |
8608 | ||
8609 | "OP_PUBKEYHASH": 253, | |
8610 | "OP_PUBKEY": 254, | |
8611 | "OP_INVALIDOPCODE": 255 | |
8612 | } | |
8613 | ||
9f59e99b | 8614 | },{}],43:[function(require,module,exports){ |
a0091a40 IC |
8615 | var OPS = require('./index.json') |
8616 | ||
8617 | var map = {} | |
8618 | for (var op in OPS) { | |
8619 | var code = OPS[op] | |
8620 | map[code] = op | |
8621 | } | |
8622 | ||
8623 | module.exports = map | |
8624 | ||
9f59e99b | 8625 | },{"./index.json":42}],44:[function(require,module,exports){ |
a0091a40 | 8626 | var Buffer = require('safe-buffer').Buffer |
9f59e99b | 8627 | var bech32 = require('bech32') |
a0091a40 IC |
8628 | var bs58check = require('bs58check') |
8629 | var bscript = require('./script') | |
9f59e99b | 8630 | var btemplates = require('./templates') |
a0091a40 IC |
8631 | var networks = require('./networks') |
8632 | var typeforce = require('typeforce') | |
8633 | var types = require('./types') | |
8634 | ||
8635 | function fromBase58Check (address) { | |
8636 | var payload = bs58check.decode(address) | |
9f59e99b IC |
8637 | |
8638 | // TODO: 4.0.0, move to "toOutputScript" | |
a0091a40 IC |
8639 | if (payload.length < 21) throw new TypeError(address + ' is too short') |
8640 | if (payload.length > 21) throw new TypeError(address + ' is too long') | |
8641 | ||
8642 | var version = payload.readUInt8(0) | |
8643 | var hash = payload.slice(1) | |
8644 | ||
9f59e99b IC |
8645 | return { version: version, hash: hash } |
8646 | } | |
8647 | ||
8648 | function fromBech32 (address) { | |
8649 | var result = bech32.decode(address) | |
8650 | var data = bech32.fromWords(result.words.slice(1)) | |
8651 | ||
8652 | return { | |
8653 | version: result.words[0], | |
8654 | prefix: result.prefix, | |
8655 | data: Buffer.from(data) | |
8656 | } | |
a0091a40 IC |
8657 | } |
8658 | ||
8659 | function toBase58Check (hash, version) { | |
8660 | typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments) | |
8661 | ||
8662 | var payload = Buffer.allocUnsafe(21) | |
8663 | payload.writeUInt8(version, 0) | |
8664 | hash.copy(payload, 1) | |
8665 | ||
8666 | return bs58check.encode(payload) | |
8667 | } | |
8668 | ||
9f59e99b IC |
8669 | function toBech32 (data, version, prefix) { |
8670 | var words = bech32.toWords(data) | |
8671 | words.unshift(version) | |
8672 | ||
8673 | return bech32.encode(prefix, words) | |
8674 | } | |
8675 | ||
a0091a40 IC |
8676 | function fromOutputScript (outputScript, network) { |
8677 | network = network || networks.bitcoin | |
8678 | ||
9f59e99b IC |
8679 | if (btemplates.pubKeyHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(3, 23), network.pubKeyHash) |
8680 | if (btemplates.scriptHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(2, 22), network.scriptHash) | |
8681 | if (btemplates.witnessPubKeyHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 22), 0, network.bech32) | |
8682 | if (btemplates.witnessScriptHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 34), 0, network.bech32) | |
a0091a40 IC |
8683 | |
8684 | throw new Error(bscript.toASM(outputScript) + ' has no matching Address') | |
8685 | } | |
8686 | ||
8687 | function toOutputScript (address, network) { | |
8688 | network = network || networks.bitcoin | |
8689 | ||
9f59e99b IC |
8690 | var decode |
8691 | try { | |
8692 | decode = fromBase58Check(address) | |
8693 | } catch (e) {} | |
8694 | ||
8695 | if (decode) { | |
8696 | if (decode.version === network.pubKeyHash) return btemplates.pubKeyHash.output.encode(decode.hash) | |
8697 | if (decode.version === network.scriptHash) return btemplates.scriptHash.output.encode(decode.hash) | |
8698 | } else { | |
8699 | try { | |
8700 | decode = fromBech32(address) | |
8701 | } catch (e) {} | |
8702 | ||
8703 | if (decode) { | |
8704 | if (decode.prefix !== network.bech32) throw new Error(address + ' has an invalid prefix') | |
8705 | if (decode.version === 0) { | |
8706 | if (decode.data.length === 20) return btemplates.witnessPubKeyHash.output.encode(decode.data) | |
8707 | if (decode.data.length === 32) return btemplates.witnessScriptHash.output.encode(decode.data) | |
8708 | } | |
8709 | } | |
8710 | } | |
a0091a40 IC |
8711 | |
8712 | throw new Error(address + ' has no matching Script') | |
8713 | } | |
8714 | ||
8715 | module.exports = { | |
8716 | fromBase58Check: fromBase58Check, | |
9f59e99b | 8717 | fromBech32: fromBech32, |
a0091a40 IC |
8718 | fromOutputScript: fromOutputScript, |
8719 | toBase58Check: toBase58Check, | |
9f59e99b | 8720 | toBech32: toBech32, |
a0091a40 IC |
8721 | toOutputScript: toOutputScript |
8722 | } | |
8723 | ||
9f59e99b | 8724 | },{"./networks":53,"./script":54,"./templates":56,"./types":80,"bech32":36,"bs58check":85,"safe-buffer":128,"typeforce":139}],45:[function(require,module,exports){ |
a0091a40 IC |
8725 | var Buffer = require('safe-buffer').Buffer |
8726 | var bcrypto = require('./crypto') | |
8727 | var fastMerkleRoot = require('merkle-lib/fastRoot') | |
8728 | var typeforce = require('typeforce') | |
8729 | var types = require('./types') | |
8730 | var varuint = require('varuint-bitcoin') | |
8731 | ||
8732 | var Transaction = require('./transaction') | |
8733 | ||
8734 | function Block () { | |
8735 | this.version = 1 | |
8736 | this.prevHash = null | |
8737 | this.merkleRoot = null | |
8738 | this.timestamp = 0 | |
8739 | this.bits = 0 | |
8740 | this.nonce = 0 | |
8741 | } | |
8742 | ||
8743 | Block.fromBuffer = function (buffer) { | |
8744 | if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)') | |
8745 | ||
8746 | var offset = 0 | |
8747 | function readSlice (n) { | |
8748 | offset += n | |
8749 | return buffer.slice(offset - n, offset) | |
8750 | } | |
8751 | ||
8752 | function readUInt32 () { | |
8753 | var i = buffer.readUInt32LE(offset) | |
8754 | offset += 4 | |
8755 | return i | |
8756 | } | |
8757 | ||
8758 | function readInt32 () { | |
8759 | var i = buffer.readInt32LE(offset) | |
8760 | offset += 4 | |
8761 | return i | |
8762 | } | |
8763 | ||
8764 | var block = new Block() | |
8765 | block.version = readInt32() | |
8766 | block.prevHash = readSlice(32) | |
8767 | block.merkleRoot = readSlice(32) | |
8768 | block.timestamp = readUInt32() | |
8769 | block.bits = readUInt32() | |
8770 | block.nonce = readUInt32() | |
8771 | ||
8772 | if (buffer.length === 80) return block | |
8773 | ||
8774 | function readVarInt () { | |
8775 | var vi = varuint.decode(buffer, offset) | |
8776 | offset += varuint.decode.bytes | |
8777 | return vi | |
8778 | } | |
8779 | ||
8780 | function readTransaction () { | |
8781 | var tx = Transaction.fromBuffer(buffer.slice(offset), true) | |
8782 | offset += tx.byteLength() | |
8783 | return tx | |
8784 | } | |
8785 | ||
8786 | var nTransactions = readVarInt() | |
8787 | block.transactions = [] | |
8788 | ||
8789 | for (var i = 0; i < nTransactions; ++i) { | |
8790 | var tx = readTransaction() | |
8791 | block.transactions.push(tx) | |
8792 | } | |
8793 | ||
8794 | return block | |
8795 | } | |
8796 | ||
8797 | Block.prototype.byteLength = function (headersOnly) { | |
8798 | if (headersOnly || !this.transactions) return 80 | |
8799 | ||
8800 | return 80 + varuint.encodingLength(this.transactions.length) + this.transactions.reduce(function (a, x) { | |
8801 | return a + x.byteLength() | |
8802 | }, 0) | |
8803 | } | |
8804 | ||
8805 | Block.fromHex = function (hex) { | |
8806 | return Block.fromBuffer(Buffer.from(hex, 'hex')) | |
8807 | } | |
8808 | ||
8809 | Block.prototype.getHash = function () { | |
8810 | return bcrypto.hash256(this.toBuffer(true)) | |
8811 | } | |
8812 | ||
8813 | Block.prototype.getId = function () { | |
8814 | return this.getHash().reverse().toString('hex') | |
8815 | } | |
8816 | ||
8817 | Block.prototype.getUTCDate = function () { | |
8818 | var date = new Date(0) // epoch | |
8819 | date.setUTCSeconds(this.timestamp) | |
8820 | ||
8821 | return date | |
8822 | } | |
8823 | ||
8824 | // TODO: buffer, offset compatibility | |
8825 | Block.prototype.toBuffer = function (headersOnly) { | |
8826 | var buffer = Buffer.allocUnsafe(this.byteLength(headersOnly)) | |
8827 | ||
8828 | var offset = 0 | |
8829 | function writeSlice (slice) { | |
8830 | slice.copy(buffer, offset) | |
8831 | offset += slice.length | |
8832 | } | |
8833 | ||
8834 | function writeInt32 (i) { | |
8835 | buffer.writeInt32LE(i, offset) | |
8836 | offset += 4 | |
8837 | } | |
8838 | function writeUInt32 (i) { | |
8839 | buffer.writeUInt32LE(i, offset) | |
8840 | offset += 4 | |
8841 | } | |
8842 | ||
8843 | writeInt32(this.version) | |
8844 | writeSlice(this.prevHash) | |
8845 | writeSlice(this.merkleRoot) | |
8846 | writeUInt32(this.timestamp) | |
8847 | writeUInt32(this.bits) | |
8848 | writeUInt32(this.nonce) | |
8849 | ||
8850 | if (headersOnly || !this.transactions) return buffer | |
8851 | ||
8852 | varuint.encode(this.transactions.length, buffer, offset) | |
8853 | offset += varuint.encode.bytes | |
8854 | ||
8855 | this.transactions.forEach(function (tx) { | |
8856 | var txSize = tx.byteLength() // TODO: extract from toBuffer? | |
8857 | tx.toBuffer(buffer, offset) | |
8858 | offset += txSize | |
8859 | }) | |
8860 | ||
8861 | return buffer | |
8862 | } | |
8863 | ||
8864 | Block.prototype.toHex = function (headersOnly) { | |
8865 | return this.toBuffer(headersOnly).toString('hex') | |
8866 | } | |
8867 | ||
8868 | Block.calculateTarget = function (bits) { | |
8869 | var exponent = ((bits & 0xff000000) >> 24) - 3 | |
8870 | var mantissa = bits & 0x007fffff | |
8871 | var target = Buffer.alloc(32, 0) | |
8872 | target.writeUInt32BE(mantissa, 28 - exponent) | |
8873 | return target | |
8874 | } | |
8875 | ||
8876 | Block.calculateMerkleRoot = function (transactions) { | |
8877 | typeforce([{ getHash: types.Function }], transactions) | |
8878 | if (transactions.length === 0) throw TypeError('Cannot compute merkle root for zero transactions') | |
8879 | ||
8880 | var hashes = transactions.map(function (transaction) { | |
8881 | return transaction.getHash() | |
8882 | }) | |
8883 | ||
8884 | return fastMerkleRoot(hashes, bcrypto.hash256) | |
8885 | } | |
8886 | ||
8887 | Block.prototype.checkMerkleRoot = function () { | |
8888 | if (!this.transactions) return false | |
8889 | ||
8890 | var actualMerkleRoot = Block.calculateMerkleRoot(this.transactions) | |
8891 | return this.merkleRoot.compare(actualMerkleRoot) === 0 | |
8892 | } | |
8893 | ||
8894 | Block.prototype.checkProofOfWork = function () { | |
8895 | var hash = this.getHash().reverse() | |
8896 | var target = Block.calculateTarget(this.bits) | |
8897 | ||
8898 | return hash.compare(target) <= 0 | |
8899 | } | |
8900 | ||
8901 | module.exports = Block | |
8902 | ||
9f59e99b | 8903 | },{"./crypto":47,"./transaction":78,"./types":80,"merkle-lib/fastRoot":122,"safe-buffer":128,"typeforce":139,"varuint-bitcoin":141}],46:[function(require,module,exports){ |
a0091a40 IC |
8904 | var pushdata = require('pushdata-bitcoin') |
8905 | var varuint = require('varuint-bitcoin') | |
8906 | ||
8907 | // https://github.com/feross/buffer/blob/master/index.js#L1127 | |
8908 | function verifuint (value, max) { | |
8909 | if (typeof value !== 'number') throw new Error('cannot write a non-number as a number') | |
8910 | if (value < 0) throw new Error('specified a negative value for writing an unsigned value') | |
8911 | if (value > max) throw new Error('RangeError: value out of range') | |
8912 | if (Math.floor(value) !== value) throw new Error('value has a fractional component') | |
8913 | } | |
8914 | ||
8915 | function readUInt64LE (buffer, offset) { | |
8916 | var a = buffer.readUInt32LE(offset) | |
8917 | var b = buffer.readUInt32LE(offset + 4) | |
8918 | b *= 0x100000000 | |
8919 | ||
8920 | verifuint(b + a, 0x001fffffffffffff) | |
8921 | ||
8922 | return b + a | |
8923 | } | |
8924 | ||
8925 | function writeUInt64LE (buffer, value, offset) { | |
8926 | verifuint(value, 0x001fffffffffffff) | |
8927 | ||
8928 | buffer.writeInt32LE(value & -1, offset) | |
8929 | buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4) | |
8930 | return offset + 8 | |
8931 | } | |
8932 | ||
8933 | // TODO: remove in 4.0.0? | |
8934 | function readVarInt (buffer, offset) { | |
8935 | var result = varuint.decode(buffer, offset) | |
8936 | ||
8937 | return { | |
8938 | number: result, | |
8939 | size: varuint.decode.bytes | |
8940 | } | |
8941 | } | |
8942 | ||
8943 | // TODO: remove in 4.0.0? | |
8944 | function writeVarInt (buffer, number, offset) { | |
8945 | varuint.encode(number, buffer, offset) | |
8946 | return varuint.encode.bytes | |
8947 | } | |
8948 | ||
8949 | module.exports = { | |
8950 | pushDataSize: pushdata.encodingLength, | |
8951 | readPushDataInt: pushdata.decode, | |
8952 | readUInt64LE: readUInt64LE, | |
8953 | readVarInt: readVarInt, | |
8954 | varIntBuffer: varuint.encode, | |
8955 | varIntSize: varuint.encodingLength, | |
8956 | writePushDataInt: pushdata.encode, | |
8957 | writeUInt64LE: writeUInt64LE, | |
8958 | writeVarInt: writeVarInt | |
8959 | } | |
8960 | ||
9f59e99b | 8961 | },{"pushdata-bitcoin":125,"varuint-bitcoin":141}],47:[function(require,module,exports){ |
a0091a40 IC |
8962 | var createHash = require('create-hash') |
8963 | ||
8964 | function ripemd160 (buffer) { | |
8965 | return createHash('rmd160').update(buffer).digest() | |
8966 | } | |
8967 | ||
8968 | function sha1 (buffer) { | |
8969 | return createHash('sha1').update(buffer).digest() | |
8970 | } | |
8971 | ||
8972 | function sha256 (buffer) { | |
8973 | return createHash('sha256').update(buffer).digest() | |
8974 | } | |
8975 | ||
8976 | function hash160 (buffer) { | |
8977 | return ripemd160(sha256(buffer)) | |
8978 | } | |
8979 | ||
8980 | function hash256 (buffer) { | |
8981 | return sha256(sha256(buffer)) | |
8982 | } | |
8983 | ||
8984 | module.exports = { | |
8985 | hash160: hash160, | |
8986 | hash256: hash256, | |
8987 | ripemd160: ripemd160, | |
8988 | sha1: sha1, | |
8989 | sha256: sha256 | |
8990 | } | |
8991 | ||
9f59e99b | 8992 | },{"create-hash":87}],48:[function(require,module,exports){ |
a0091a40 IC |
8993 | var Buffer = require('safe-buffer').Buffer |
8994 | var createHmac = require('create-hmac') | |
8995 | var typeforce = require('typeforce') | |
8996 | var types = require('./types') | |
8997 | ||
8998 | var BigInteger = require('bigi') | |
8999 | var ECSignature = require('./ecsignature') | |
9000 | ||
9001 | var ZERO = Buffer.alloc(1, 0) | |
9002 | var ONE = Buffer.alloc(1, 1) | |
9003 | ||
9004 | var ecurve = require('ecurve') | |
9005 | var secp256k1 = ecurve.getCurveByName('secp256k1') | |
9006 | ||
9007 | // https://tools.ietf.org/html/rfc6979#section-3.2 | |
9008 | function deterministicGenerateK (hash, x, checkSig) { | |
9009 | typeforce(types.tuple( | |
9010 | types.Hash256bit, | |
9011 | types.Buffer256bit, | |
9012 | types.Function | |
9013 | ), arguments) | |
9014 | ||
9015 | // Step A, ignored as hash already provided | |
9016 | // Step B | |
9017 | // Step C | |
9018 | var k = Buffer.alloc(32, 0) | |
9019 | var v = Buffer.alloc(32, 1) | |
9020 | ||
9021 | // Step D | |
9022 | k = createHmac('sha256', k) | |
9023 | .update(v) | |
9024 | .update(ZERO) | |
9025 | .update(x) | |
9026 | .update(hash) | |
9027 | .digest() | |
9028 | ||
9029 | // Step E | |
9030 | v = createHmac('sha256', k).update(v).digest() | |
9031 | ||
9032 | // Step F | |
9033 | k = createHmac('sha256', k) | |
9034 | .update(v) | |
9035 | .update(ONE) | |
9036 | .update(x) | |
9037 | .update(hash) | |
9038 | .digest() | |
9039 | ||
9040 | // Step G | |
9041 | v = createHmac('sha256', k).update(v).digest() | |
9042 | ||
9043 | // Step H1/H2a, ignored as tlen === qlen (256 bit) | |
9044 | // Step H2b | |
9045 | v = createHmac('sha256', k).update(v).digest() | |
9046 | ||
9047 | var T = BigInteger.fromBuffer(v) | |
9048 | ||
9049 | // Step H3, repeat until T is within the interval [1, n - 1] and is suitable for ECDSA | |
9050 | while (T.signum() <= 0 || T.compareTo(secp256k1.n) >= 0 || !checkSig(T)) { | |
9051 | k = createHmac('sha256', k) | |
9052 | .update(v) | |
9053 | .update(ZERO) | |
9054 | .digest() | |
9055 | ||
9056 | v = createHmac('sha256', k).update(v).digest() | |
9057 | ||
9058 | // Step H1/H2a, again, ignored as tlen === qlen (256 bit) | |
9059 | // Step H2b again | |
9060 | v = createHmac('sha256', k).update(v).digest() | |
9061 | T = BigInteger.fromBuffer(v) | |
9062 | } | |
9063 | ||
9064 | return T | |
9065 | } | |
9066 | ||
9067 | var N_OVER_TWO = secp256k1.n.shiftRight(1) | |
9068 | ||
9069 | function sign (hash, d) { | |
9070 | typeforce(types.tuple(types.Hash256bit, types.BigInt), arguments) | |
9071 | ||
9072 | var x = d.toBuffer(32) | |
9073 | var e = BigInteger.fromBuffer(hash) | |
9074 | var n = secp256k1.n | |
9075 | var G = secp256k1.G | |
9076 | ||
9077 | var r, s | |
9078 | deterministicGenerateK(hash, x, function (k) { | |
9079 | var Q = G.multiply(k) | |
9080 | ||
9081 | if (secp256k1.isInfinity(Q)) return false | |
9082 | ||
9083 | r = Q.affineX.mod(n) | |
9084 | if (r.signum() === 0) return false | |
9085 | ||
9086 | s = k.modInverse(n).multiply(e.add(d.multiply(r))).mod(n) | |
9087 | if (s.signum() === 0) return false | |
9088 | ||
9089 | return true | |
9090 | }) | |
9091 | ||
9092 | // enforce low S values, see bip62: 'low s values in signatures' | |
9093 | if (s.compareTo(N_OVER_TWO) > 0) { | |
9094 | s = n.subtract(s) | |
9095 | } | |
9096 | ||
9097 | return new ECSignature(r, s) | |
9098 | } | |
9099 | ||
9100 | function verify (hash, signature, Q) { | |
9101 | typeforce(types.tuple( | |
9102 | types.Hash256bit, | |
9103 | types.ECSignature, | |
9104 | types.ECPoint | |
9105 | ), arguments) | |
9106 | ||
9107 | var n = secp256k1.n | |
9108 | var G = secp256k1.G | |
9109 | ||
9110 | var r = signature.r | |
9111 | var s = signature.s | |
9112 | ||
9113 | // 1.4.1 Enforce r and s are both integers in the interval [1, n − 1] | |
9114 | if (r.signum() <= 0 || r.compareTo(n) >= 0) return false | |
9115 | if (s.signum() <= 0 || s.compareTo(n) >= 0) return false | |
9116 | ||
9117 | // 1.4.2 H = Hash(M), already done by the user | |
9118 | // 1.4.3 e = H | |
9119 | var e = BigInteger.fromBuffer(hash) | |
9120 | ||
9121 | // Compute s^-1 | |
9122 | var sInv = s.modInverse(n) | |
9123 | ||
9124 | // 1.4.4 Compute u1 = es^−1 mod n | |
9125 | // u2 = rs^−1 mod n | |
9126 | var u1 = e.multiply(sInv).mod(n) | |
9127 | var u2 = r.multiply(sInv).mod(n) | |
9128 | ||
9129 | // 1.4.5 Compute R = (xR, yR) | |
9130 | // R = u1G + u2Q | |
9131 | var R = G.multiplyTwo(u1, Q, u2) | |
9132 | ||
9133 | // 1.4.5 (cont.) Enforce R is not at infinity | |
9134 | if (secp256k1.isInfinity(R)) return false | |
9135 | ||
9136 | // 1.4.6 Convert the field element R.x to an integer | |
9137 | var xR = R.affineX | |
9138 | ||
9139 | // 1.4.7 Set v = xR mod n | |
9140 | var v = xR.mod(n) | |
9141 | ||
9142 | // 1.4.8 If v = r, output "valid", and if v != r, output "invalid" | |
9143 | return v.equals(r) | |
9144 | } | |
9145 | ||
9146 | module.exports = { | |
9147 | deterministicGenerateK: deterministicGenerateK, | |
9148 | sign: sign, | |
9149 | verify: verify, | |
9150 | ||
9151 | // TODO: remove | |
9152 | __curve: secp256k1 | |
9153 | } | |
9154 | ||
9f59e99b | 9155 | },{"./ecsignature":50,"./types":80,"bigi":39,"create-hmac":90,"ecurve":94,"safe-buffer":128,"typeforce":139}],49:[function(require,module,exports){ |
a0091a40 IC |
9156 | var baddress = require('./address') |
9157 | var bcrypto = require('./crypto') | |
9158 | var ecdsa = require('./ecdsa') | |
9159 | var randomBytes = require('randombytes') | |
9160 | var typeforce = require('typeforce') | |
9161 | var types = require('./types') | |
9162 | var wif = require('wif') | |
9163 | ||
9164 | var NETWORKS = require('./networks') | |
9165 | var BigInteger = require('bigi') | |
9166 | ||
9167 | var ecurve = require('ecurve') | |
9168 | var secp256k1 = ecdsa.__curve | |
9169 | ||
9170 | function ECPair (d, Q, options) { | |
9171 | if (options) { | |
9172 | typeforce({ | |
9173 | compressed: types.maybe(types.Boolean), | |
9174 | network: types.maybe(types.Network) | |
9175 | }, options) | |
9176 | } | |
9177 | ||
9178 | options = options || {} | |
9179 | ||
9180 | if (d) { | |
9181 | if (d.signum() <= 0) throw new Error('Private key must be greater than 0') | |
9182 | if (d.compareTo(secp256k1.n) >= 0) throw new Error('Private key must be less than the curve order') | |
9183 | if (Q) throw new TypeError('Unexpected publicKey parameter') | |
9184 | ||
9185 | this.d = d | |
9186 | } else { | |
9187 | typeforce(types.ECPoint, Q) | |
9188 | ||
9189 | this.__Q = Q | |
9190 | } | |
9191 | ||
9192 | this.compressed = options.compressed === undefined ? true : options.compressed | |
9193 | this.network = options.network || NETWORKS.bitcoin | |
9194 | } | |
9195 | ||
9196 | Object.defineProperty(ECPair.prototype, 'Q', { | |
9197 | get: function () { | |
9198 | if (!this.__Q && this.d) { | |
9199 | this.__Q = secp256k1.G.multiply(this.d) | |
9200 | } | |
9201 | ||
9202 | return this.__Q | |
9203 | } | |
9204 | }) | |
9205 | ||
9206 | ECPair.fromPublicKeyBuffer = function (buffer, network) { | |
9207 | var Q = ecurve.Point.decodeFrom(secp256k1, buffer) | |
9208 | ||
9209 | return new ECPair(null, Q, { | |
9210 | compressed: Q.compressed, | |
9211 | network: network | |
9212 | }) | |
9213 | } | |
9214 | ||
9215 | ECPair.fromWIF = function (string, network) { | |
9216 | var decoded = wif.decode(string) | |
9217 | var version = decoded.version | |
9218 | ||
9219 | // list of networks? | |
9220 | if (types.Array(network)) { | |
9221 | network = network.filter(function (x) { | |
9222 | return version === x.wif | |
9223 | }).pop() | |
9224 | ||
9225 | if (!network) throw new Error('Unknown network version') | |
9226 | ||
9227 | // otherwise, assume a network object (or default to bitcoin) | |
9228 | } else { | |
9229 | network = network || NETWORKS.bitcoin | |
9230 | ||
9231 | if (version !== network.wif) throw new Error('Invalid network version') | |
9232 | } | |
9233 | ||
9234 | var d = BigInteger.fromBuffer(decoded.privateKey) | |
9235 | ||
9236 | return new ECPair(d, null, { | |
9237 | compressed: decoded.compressed, | |
9238 | network: network | |
9239 | }) | |
9240 | } | |
9241 | ||
9242 | ECPair.makeRandom = function (options) { | |
9243 | options = options || {} | |
9244 | ||
9245 | var rng = options.rng || randomBytes | |
9246 | ||
9247 | var d | |
9248 | do { | |
9249 | var buffer = rng(32) | |
9250 | typeforce(types.Buffer256bit, buffer) | |
9251 | ||
9252 | d = BigInteger.fromBuffer(buffer) | |
9253 | } while (d.signum() <= 0 || d.compareTo(secp256k1.n) >= 0) | |
9254 | ||
9255 | return new ECPair(d, null, options) | |
9256 | } | |
9257 | ||
9258 | ECPair.prototype.getAddress = function () { | |
9259 | return baddress.toBase58Check(bcrypto.hash160(this.getPublicKeyBuffer()), this.getNetwork().pubKeyHash) | |
9260 | } | |
9261 | ||
9262 | ECPair.prototype.getNetwork = function () { | |
9263 | return this.network | |
9264 | } | |
9265 | ||
9266 | ECPair.prototype.getPublicKeyBuffer = function () { | |
9267 | return this.Q.getEncoded(this.compressed) | |
9268 | } | |
9269 | ||
9270 | ECPair.prototype.sign = function (hash) { | |
9271 | if (!this.d) throw new Error('Missing private key') | |
9272 | ||
9273 | return ecdsa.sign(hash, this.d) | |
9274 | } | |
9275 | ||
9276 | ECPair.prototype.toWIF = function () { | |
9277 | if (!this.d) throw new Error('Missing private key') | |
9278 | ||
9279 | return wif.encode(this.network.wif, this.d.toBuffer(32), this.compressed) | |
9280 | } | |
9281 | ||
9282 | ECPair.prototype.verify = function (hash, signature) { | |
9283 | return ecdsa.verify(hash, signature, this.Q) | |
9284 | } | |
9285 | ||
9286 | module.exports = ECPair | |
9287 | ||
9f59e99b | 9288 | },{"./address":44,"./crypto":47,"./ecdsa":48,"./networks":53,"./types":80,"bigi":39,"ecurve":94,"randombytes":126,"typeforce":139,"wif":142}],50:[function(require,module,exports){ |
a0091a40 IC |
9289 | (function (Buffer){ |
9290 | var bip66 = require('bip66') | |
9291 | var typeforce = require('typeforce') | |
9292 | var types = require('./types') | |
9293 | ||
9294 | var BigInteger = require('bigi') | |
9295 | ||
9296 | function ECSignature (r, s) { | |
9297 | typeforce(types.tuple(types.BigInt, types.BigInt), arguments) | |
9298 | ||
9299 | this.r = r | |
9300 | this.s = s | |
9301 | } | |
9302 | ||
9303 | ECSignature.parseCompact = function (buffer) { | |
9f59e99b | 9304 | typeforce(types.BufferN(65), buffer) |
a0091a40 IC |
9305 | |
9306 | var flagByte = buffer.readUInt8(0) - 27 | |
9307 | if (flagByte !== (flagByte & 7)) throw new Error('Invalid signature parameter') | |
9308 | ||
9309 | var compressed = !!(flagByte & 4) | |
9310 | var recoveryParam = flagByte & 3 | |
9f59e99b | 9311 | var signature = ECSignature.fromRSBuffer(buffer.slice(1)) |
a0091a40 IC |
9312 | |
9313 | return { | |
9314 | compressed: compressed, | |
9315 | i: recoveryParam, | |
9f59e99b | 9316 | signature: signature |
a0091a40 IC |
9317 | } |
9318 | } | |
9319 | ||
9f59e99b IC |
9320 | ECSignature.fromRSBuffer = function (buffer) { |
9321 | typeforce(types.BufferN(64), buffer) | |
9322 | ||
9323 | var r = BigInteger.fromBuffer(buffer.slice(0, 32)) | |
9324 | var s = BigInteger.fromBuffer(buffer.slice(32, 64)) | |
9325 | return new ECSignature(r, s) | |
9326 | } | |
9327 | ||
a0091a40 IC |
9328 | ECSignature.fromDER = function (buffer) { |
9329 | var decode = bip66.decode(buffer) | |
9330 | var r = BigInteger.fromDERInteger(decode.r) | |
9331 | var s = BigInteger.fromDERInteger(decode.s) | |
9332 | ||
9333 | return new ECSignature(r, s) | |
9334 | } | |
9335 | ||
9336 | // BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed) | |
9337 | ECSignature.parseScriptSignature = function (buffer) { | |
9338 | var hashType = buffer.readUInt8(buffer.length - 1) | |
9339 | var hashTypeMod = hashType & ~0x80 | |
9340 | ||
9341 | if (hashTypeMod <= 0x00 || hashTypeMod >= 0x04) throw new Error('Invalid hashType ' + hashType) | |
9342 | ||
9343 | return { | |
9344 | signature: ECSignature.fromDER(buffer.slice(0, -1)), | |
9345 | hashType: hashType | |
9346 | } | |
9347 | } | |
9348 | ||
9349 | ECSignature.prototype.toCompact = function (i, compressed) { | |
9350 | if (compressed) { | |
9351 | i += 4 | |
9352 | } | |
9353 | ||
9354 | i += 27 | |
9355 | ||
9356 | var buffer = Buffer.alloc(65) | |
9357 | buffer.writeUInt8(i, 0) | |
9f59e99b | 9358 | this.toRSBuffer(buffer, 1) |
a0091a40 IC |
9359 | return buffer |
9360 | } | |
9361 | ||
9362 | ECSignature.prototype.toDER = function () { | |
9363 | var r = Buffer.from(this.r.toDERInteger()) | |
9364 | var s = Buffer.from(this.s.toDERInteger()) | |
9365 | ||
9366 | return bip66.encode(r, s) | |
9367 | } | |
9368 | ||
9f59e99b IC |
9369 | ECSignature.prototype.toRSBuffer = function (buffer, offset) { |
9370 | buffer = buffer || Buffer.alloc(64) | |
9371 | this.r.toBuffer(32).copy(buffer, offset) | |
9372 | this.s.toBuffer(32).copy(buffer, offset + 32) | |
9373 | return buffer | |
9374 | } | |
9375 | ||
a0091a40 IC |
9376 | ECSignature.prototype.toScriptSignature = function (hashType) { |
9377 | var hashTypeMod = hashType & ~0x80 | |
9378 | if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType) | |
9379 | ||
9380 | var hashTypeBuffer = Buffer.alloc(1) | |
9381 | hashTypeBuffer.writeUInt8(hashType, 0) | |
9382 | ||
9383 | return Buffer.concat([this.toDER(), hashTypeBuffer]) | |
9384 | } | |
9385 | ||
9386 | module.exports = ECSignature | |
9387 | ||
9388 | }).call(this,require("buffer").Buffer) | |
9f59e99b | 9389 | },{"./types":80,"bigi":39,"bip66":41,"buffer":5,"typeforce":139}],51:[function(require,module,exports){ |
a0091a40 IC |
9390 | var Buffer = require('safe-buffer').Buffer |
9391 | var base58check = require('bs58check') | |
9392 | var bcrypto = require('./crypto') | |
9393 | var createHmac = require('create-hmac') | |
9394 | var typeforce = require('typeforce') | |
9395 | var types = require('./types') | |
9396 | var NETWORKS = require('./networks') | |
9397 | ||
9398 | var BigInteger = require('bigi') | |
9399 | var ECPair = require('./ecpair') | |
9400 | ||
9401 | var ecurve = require('ecurve') | |
9402 | var curve = ecurve.getCurveByName('secp256k1') | |
9403 | ||
9404 | function HDNode (keyPair, chainCode) { | |
9405 | typeforce(types.tuple('ECPair', types.Buffer256bit), arguments) | |
9406 | ||
9407 | if (!keyPair.compressed) throw new TypeError('BIP32 only allows compressed keyPairs') | |
9408 | ||
9409 | this.keyPair = keyPair | |
9410 | this.chainCode = chainCode | |
9411 | this.depth = 0 | |
9412 | this.index = 0 | |
9413 | this.parentFingerprint = 0x00000000 | |
9414 | } | |
9415 | ||
9416 | HDNode.HIGHEST_BIT = 0x80000000 | |
9417 | HDNode.LENGTH = 78 | |
9418 | HDNode.MASTER_SECRET = Buffer.from('Bitcoin seed', 'utf8') | |
9419 | ||
9420 | HDNode.fromSeedBuffer = function (seed, network) { | |
9421 | typeforce(types.tuple(types.Buffer, types.maybe(types.Network)), arguments) | |
9422 | ||
9423 | if (seed.length < 16) throw new TypeError('Seed should be at least 128 bits') | |
9424 | if (seed.length > 64) throw new TypeError('Seed should be at most 512 bits') | |
9425 | ||
9426 | var I = createHmac('sha512', HDNode.MASTER_SECRET).update(seed).digest() | |
9427 | var IL = I.slice(0, 32) | |
9428 | var IR = I.slice(32) | |
9429 | ||
9430 | // In case IL is 0 or >= n, the master key is invalid | |
9431 | // This is handled by the ECPair constructor | |
9432 | var pIL = BigInteger.fromBuffer(IL) | |
9433 | var keyPair = new ECPair(pIL, null, { | |
9434 | network: network | |
9435 | }) | |
9436 | ||
9437 | return new HDNode(keyPair, IR) | |
9438 | } | |
9439 | ||
9440 | HDNode.fromSeedHex = function (hex, network) { | |
9441 | return HDNode.fromSeedBuffer(Buffer.from(hex, 'hex'), network) | |
9442 | } | |
9443 | ||
9444 | HDNode.fromBase58 = function (string, networks) { | |
9445 | var buffer = base58check.decode(string) | |
9446 | if (buffer.length !== 78) throw new Error('Invalid buffer length') | |
9447 | ||
9448 | // 4 bytes: version bytes | |
9449 | var version = buffer.readUInt32BE(0) | |
9450 | var network | |
9451 | ||
9452 | // list of networks? | |
9453 | if (Array.isArray(networks)) { | |
9454 | network = networks.filter(function (x) { | |
9455 | return version === x.bip32.private || | |
9456 | version === x.bip32.public | |
9457 | }).pop() | |
9458 | ||
9459 | if (!network) throw new Error('Unknown network version') | |
9460 | ||
9461 | // otherwise, assume a network object (or default to bitcoin) | |
9462 | } else { | |
9463 | network = networks || NETWORKS.bitcoin | |
9464 | } | |
9465 | ||
9466 | if (version !== network.bip32.private && | |
9467 | version !== network.bip32.public) throw new Error('Invalid network version') | |
9468 | ||
9469 | // 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ... | |
9470 | var depth = buffer[4] | |
9471 | ||
9472 | // 4 bytes: the fingerprint of the parent's key (0x00000000 if master key) | |
9473 | var parentFingerprint = buffer.readUInt32BE(5) | |
9474 | if (depth === 0) { | |
9475 | if (parentFingerprint !== 0x00000000) throw new Error('Invalid parent fingerprint') | |
9476 | } | |
9477 | ||
9478 | // 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized. | |
9479 | // This is encoded in MSB order. (0x00000000 if master key) | |
9480 | var index = buffer.readUInt32BE(9) | |
9481 | if (depth === 0 && index !== 0) throw new Error('Invalid index') | |
9482 | ||
9483 | // 32 bytes: the chain code | |
9484 | var chainCode = buffer.slice(13, 45) | |
9485 | var keyPair | |
9486 | ||
9487 | // 33 bytes: private key data (0x00 + k) | |
9488 | if (version === network.bip32.private) { | |
9489 | if (buffer.readUInt8(45) !== 0x00) throw new Error('Invalid private key') | |
9490 | ||
9491 | var d = BigInteger.fromBuffer(buffer.slice(46, 78)) | |
9492 | keyPair = new ECPair(d, null, { network: network }) | |
9493 | ||
9494 | // 33 bytes: public key data (0x02 + X or 0x03 + X) | |
9495 | } else { | |
9496 | var Q = ecurve.Point.decodeFrom(curve, buffer.slice(45, 78)) | |
9497 | // Q.compressed is assumed, if somehow this assumption is broken, `new HDNode` will throw | |
9498 | ||
9499 | // Verify that the X coordinate in the public point corresponds to a point on the curve. | |
9500 | // If not, the extended public key is invalid. | |
9501 | curve.validate(Q) | |
9502 | ||
9503 | keyPair = new ECPair(null, Q, { network: network }) | |
9504 | } | |
9505 | ||
9506 | var hd = new HDNode(keyPair, chainCode) | |
9507 | hd.depth = depth | |
9508 | hd.index = index | |
9509 | hd.parentFingerprint = parentFingerprint | |
9510 | ||
9511 | return hd | |
9512 | } | |
9513 | ||
9514 | HDNode.prototype.getAddress = function () { | |
9515 | return this.keyPair.getAddress() | |
9516 | } | |
9517 | ||
9518 | HDNode.prototype.getIdentifier = function () { | |
9519 | return bcrypto.hash160(this.keyPair.getPublicKeyBuffer()) | |
9520 | } | |
9521 | ||
9522 | HDNode.prototype.getFingerprint = function () { | |
9523 | return this.getIdentifier().slice(0, 4) | |
9524 | } | |
9525 | ||
9526 | HDNode.prototype.getNetwork = function () { | |
9527 | return this.keyPair.getNetwork() | |
9528 | } | |
9529 | ||
9530 | HDNode.prototype.getPublicKeyBuffer = function () { | |
9531 | return this.keyPair.getPublicKeyBuffer() | |
9532 | } | |
9533 | ||
9534 | HDNode.prototype.neutered = function () { | |
9535 | var neuteredKeyPair = new ECPair(null, this.keyPair.Q, { | |
9536 | network: this.keyPair.network | |
9537 | }) | |
9538 | ||
9539 | var neutered = new HDNode(neuteredKeyPair, this.chainCode) | |
9540 | neutered.depth = this.depth | |
9541 | neutered.index = this.index | |
9542 | neutered.parentFingerprint = this.parentFingerprint | |
9543 | ||
9544 | return neutered | |
9545 | } | |
9546 | ||
9547 | HDNode.prototype.sign = function (hash) { | |
9548 | return this.keyPair.sign(hash) | |
9549 | } | |
9550 | ||
9551 | HDNode.prototype.verify = function (hash, signature) { | |
9552 | return this.keyPair.verify(hash, signature) | |
9553 | } | |
9554 | ||
9555 | HDNode.prototype.toBase58 = function (__isPrivate) { | |
9556 | if (__isPrivate !== undefined) throw new TypeError('Unsupported argument in 2.0.0') | |
9557 | ||
9558 | // Version | |
9559 | var network = this.keyPair.network | |
9560 | var version = (!this.isNeutered()) ? network.bip32.private : network.bip32.public | |
9561 | var buffer = Buffer.allocUnsafe(78) | |
9562 | ||
9563 | // 4 bytes: version bytes | |
9564 | buffer.writeUInt32BE(version, 0) | |
9565 | ||
9566 | // 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, .... | |
9567 | buffer.writeUInt8(this.depth, 4) | |
9568 | ||
9569 | // 4 bytes: the fingerprint of the parent's key (0x00000000 if master key) | |
9570 | buffer.writeUInt32BE(this.parentFingerprint, 5) | |
9571 | ||
9572 | // 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized. | |
9573 | // This is encoded in big endian. (0x00000000 if master key) | |
9574 | buffer.writeUInt32BE(this.index, 9) | |
9575 | ||
9576 | // 32 bytes: the chain code | |
9577 | this.chainCode.copy(buffer, 13) | |
9578 | ||
9579 | // 33 bytes: the public key or private key data | |
9580 | if (!this.isNeutered()) { | |
9581 | // 0x00 + k for private keys | |
9582 | buffer.writeUInt8(0, 45) | |
9583 | this.keyPair.d.toBuffer(32).copy(buffer, 46) | |
9584 | ||
9585 | // 33 bytes: the public key | |
9586 | } else { | |
9587 | // X9.62 encoding for public keys | |
9588 | this.keyPair.getPublicKeyBuffer().copy(buffer, 45) | |
9589 | } | |
9590 | ||
9591 | return base58check.encode(buffer) | |
9592 | } | |
9593 | ||
9594 | // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions | |
9595 | HDNode.prototype.derive = function (index) { | |
9596 | typeforce(types.UInt32, index) | |
9597 | ||
9598 | var isHardened = index >= HDNode.HIGHEST_BIT | |
9599 | var data = Buffer.allocUnsafe(37) | |
9600 | ||
9601 | // Hardened child | |
9602 | if (isHardened) { | |
9603 | if (this.isNeutered()) throw new TypeError('Could not derive hardened child key') | |
9604 | ||
9605 | // data = 0x00 || ser256(kpar) || ser32(index) | |
9606 | data[0] = 0x00 | |
9607 | this.keyPair.d.toBuffer(32).copy(data, 1) | |
9608 | data.writeUInt32BE(index, 33) | |
9609 | ||
9610 | // Normal child | |
9611 | } else { | |
9612 | // data = serP(point(kpar)) || ser32(index) | |
9613 | // = serP(Kpar) || ser32(index) | |
9614 | this.keyPair.getPublicKeyBuffer().copy(data, 0) | |
9615 | data.writeUInt32BE(index, 33) | |
9616 | } | |
9617 | ||
9618 | var I = createHmac('sha512', this.chainCode).update(data).digest() | |
9619 | var IL = I.slice(0, 32) | |
9620 | var IR = I.slice(32) | |
9621 | ||
9622 | var pIL = BigInteger.fromBuffer(IL) | |
9623 | ||
9624 | // In case parse256(IL) >= n, proceed with the next value for i | |
9625 | if (pIL.compareTo(curve.n) >= 0) { | |
9626 | return this.derive(index + 1) | |
9627 | } | |
9628 | ||
9629 | // Private parent key -> private child key | |
9630 | var derivedKeyPair | |
9631 | if (!this.isNeutered()) { | |
9632 | // ki = parse256(IL) + kpar (mod n) | |
9633 | var ki = pIL.add(this.keyPair.d).mod(curve.n) | |
9634 | ||
9635 | // In case ki == 0, proceed with the next value for i | |
9636 | if (ki.signum() === 0) { | |
9637 | return this.derive(index + 1) | |
9638 | } | |
9639 | ||
9640 | derivedKeyPair = new ECPair(ki, null, { | |
9641 | network: this.keyPair.network | |
9642 | }) | |
9643 | ||
9644 | // Public parent key -> public child key | |
9645 | } else { | |
9646 | // Ki = point(parse256(IL)) + Kpar | |
9647 | // = G*IL + Kpar | |
9648 | var Ki = curve.G.multiply(pIL).add(this.keyPair.Q) | |
9649 | ||
9650 | // In case Ki is the point at infinity, proceed with the next value for i | |
9651 | if (curve.isInfinity(Ki)) { | |
9652 | return this.derive(index + 1) | |
9653 | } | |
9654 | ||
9655 | derivedKeyPair = new ECPair(null, Ki, { | |
9656 | network: this.keyPair.network | |
9657 | }) | |
9658 | } | |
9659 | ||
9660 | var hd = new HDNode(derivedKeyPair, IR) | |
9661 | hd.depth = this.depth + 1 | |
9662 | hd.index = index | |
9663 | hd.parentFingerprint = this.getFingerprint().readUInt32BE(0) | |
9664 | ||
9665 | return hd | |
9666 | } | |
9667 | ||
9668 | HDNode.prototype.deriveHardened = function (index) { | |
9669 | typeforce(types.UInt31, index) | |
9670 | ||
9671 | // Only derives hardened private keys by default | |
9672 | return this.derive(index + HDNode.HIGHEST_BIT) | |
9673 | } | |
9674 | ||
9675 | // Private === not neutered | |
9676 | // Public === neutered | |
9677 | HDNode.prototype.isNeutered = function () { | |
9678 | return !(this.keyPair.d) | |
9679 | } | |
9680 | ||
9681 | HDNode.prototype.derivePath = function (path) { | |
9682 | typeforce(types.BIP32Path, path) | |
9683 | ||
9684 | var splitPath = path.split('/') | |
9685 | if (splitPath[0] === 'm') { | |
9686 | if (this.parentFingerprint) { | |
9687 | throw new Error('Not a master node') | |
9688 | } | |
9689 | ||
9690 | splitPath = splitPath.slice(1) | |
9691 | } | |
9692 | ||
9693 | return splitPath.reduce(function (prevHd, indexStr) { | |
9694 | var index | |
9695 | if (indexStr.slice(-1) === "'") { | |
9696 | index = parseInt(indexStr.slice(0, -1), 10) | |
9697 | return prevHd.deriveHardened(index) | |
9698 | } else { | |
9699 | index = parseInt(indexStr, 10) | |
9700 | return prevHd.derive(index) | |
9701 | } | |
9702 | }, this) | |
9703 | } | |
9704 | ||
9705 | module.exports = HDNode | |
9706 | ||
9f59e99b IC |
9707 | },{"./crypto":47,"./ecpair":49,"./networks":53,"./types":80,"bigi":39,"bs58check":85,"create-hmac":90,"ecurve":94,"safe-buffer":128,"typeforce":139}],52:[function(require,module,exports){ |
9708 | var script = require('./script') | |
9709 | ||
9710 | var templates = require('./templates') | |
9711 | for (var key in templates) { | |
9712 | script[key] = templates[key] | |
9713 | } | |
9714 | ||
a0091a40 | 9715 | module.exports = { |
9f59e99b IC |
9716 | bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0 |
9717 | ||
a0091a40 IC |
9718 | Block: require('./block'), |
9719 | ECPair: require('./ecpair'), | |
9720 | ECSignature: require('./ecsignature'), | |
9721 | HDNode: require('./hdnode'), | |
9722 | Transaction: require('./transaction'), | |
9723 | TransactionBuilder: require('./transaction_builder'), | |
9724 | ||
9725 | address: require('./address'), | |
a0091a40 IC |
9726 | crypto: require('./crypto'), |
9727 | networks: require('./networks'), | |
9728 | opcodes: require('bitcoin-ops'), | |
9f59e99b | 9729 | script: script |
a0091a40 IC |
9730 | } |
9731 | ||
9f59e99b | 9732 | },{"./address":44,"./block":45,"./bufferutils":46,"./crypto":47,"./ecpair":49,"./ecsignature":50,"./hdnode":51,"./networks":53,"./script":54,"./templates":56,"./transaction":78,"./transaction_builder":79,"bitcoin-ops":42}],53:[function(require,module,exports){ |
a0091a40 IC |
9733 | // https://en.bitcoin.it/wiki/List_of_address_prefixes |
9734 | // Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731 | |
9735 | ||
9736 | module.exports = { | |
9737 | bitcoin: { | |
9738 | messagePrefix: '\x18Bitcoin Signed Message:\n', | |
9f59e99b | 9739 | bech32: 'bc', |
a0091a40 IC |
9740 | bip32: { |
9741 | public: 0x0488b21e, | |
9742 | private: 0x0488ade4 | |
9743 | }, | |
9744 | pubKeyHash: 0x00, | |
9745 | scriptHash: 0x05, | |
9746 | wif: 0x80 | |
9747 | }, | |
9748 | testnet: { | |
9749 | messagePrefix: '\x18Bitcoin Signed Message:\n', | |
9f59e99b | 9750 | bech32: 'tb', |
a0091a40 IC |
9751 | bip32: { |
9752 | public: 0x043587cf, | |
9753 | private: 0x04358394 | |
9754 | }, | |
9755 | pubKeyHash: 0x6f, | |
9756 | scriptHash: 0xc4, | |
9757 | wif: 0xef | |
9758 | }, | |
9759 | litecoin: { | |
9760 | messagePrefix: '\x19Litecoin Signed Message:\n', | |
9761 | bip32: { | |
9762 | public: 0x019da462, | |
9763 | private: 0x019d9cfe | |
9764 | }, | |
9765 | pubKeyHash: 0x30, | |
9766 | scriptHash: 0x32, | |
9767 | wif: 0xb0 | |
9768 | } | |
9769 | } | |
9770 | ||
9f59e99b | 9771 | },{}],54:[function(require,module,exports){ |
a0091a40 IC |
9772 | var Buffer = require('safe-buffer').Buffer |
9773 | var bip66 = require('bip66') | |
9774 | var pushdata = require('pushdata-bitcoin') | |
9775 | var typeforce = require('typeforce') | |
9776 | var types = require('./types') | |
9777 | var scriptNumber = require('./script_number') | |
9778 | ||
9779 | var OPS = require('bitcoin-ops') | |
9780 | var REVERSE_OPS = require('bitcoin-ops/map') | |
9781 | var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 | |
9782 | ||
9783 | function isOPInt (value) { | |
9784 | return types.Number(value) && | |
9785 | ((value === OPS.OP_0) || | |
9786 | (value >= OPS.OP_1 && value <= OPS.OP_16) || | |
9787 | (value === OPS.OP_1NEGATE)) | |
9788 | } | |
9789 | ||
9790 | function isPushOnlyChunk (value) { | |
9791 | return types.Buffer(value) || isOPInt(value) | |
9792 | } | |
9793 | ||
9794 | function isPushOnly (value) { | |
9795 | return types.Array(value) && value.every(isPushOnlyChunk) | |
9796 | } | |
9797 | ||
9f59e99b IC |
9798 | function asMinimalOP (buffer) { |
9799 | if (buffer.length === 0) return OPS.OP_0 | |
9800 | if (buffer.length !== 1) return | |
9801 | if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0] | |
9802 | if (buffer[0] === 0x81) return OPS.OP_1NEGATE | |
9803 | } | |
9804 | ||
a0091a40 IC |
9805 | function compile (chunks) { |
9806 | // TODO: remove me | |
9807 | if (Buffer.isBuffer(chunks)) return chunks | |
9808 | ||
9809 | typeforce(types.Array, chunks) | |
9810 | ||
9811 | var bufferSize = chunks.reduce(function (accum, chunk) { | |
9812 | // data chunk | |
9813 | if (Buffer.isBuffer(chunk)) { | |
9814 | // adhere to BIP62.3, minimal push policy | |
9f59e99b | 9815 | if (chunk.length === 1 && asMinimalOP(chunk) !== undefined) { |
a0091a40 IC |
9816 | return accum + 1 |
9817 | } | |
9818 | ||
9819 | return accum + pushdata.encodingLength(chunk.length) + chunk.length | |
9820 | } | |
9821 | ||
9822 | // opcode | |
9823 | return accum + 1 | |
9824 | }, 0.0) | |
9825 | ||
9826 | var buffer = Buffer.allocUnsafe(bufferSize) | |
9827 | var offset = 0 | |
9828 | ||
9829 | chunks.forEach(function (chunk) { | |
9830 | // data chunk | |
9831 | if (Buffer.isBuffer(chunk)) { | |
9832 | // adhere to BIP62.3, minimal push policy | |
9f59e99b IC |
9833 | var opcode = asMinimalOP(chunk) |
9834 | if (opcode !== undefined) { | |
a0091a40 IC |
9835 | buffer.writeUInt8(opcode, offset) |
9836 | offset += 1 | |
9837 | return | |
9838 | } | |
9839 | ||
a0091a40 | 9840 | offset += pushdata.encode(buffer, chunk.length, offset) |
a0091a40 IC |
9841 | chunk.copy(buffer, offset) |
9842 | offset += chunk.length | |
9843 | ||
9844 | // opcode | |
9845 | } else { | |
9846 | buffer.writeUInt8(chunk, offset) | |
9847 | offset += 1 | |
9848 | } | |
9849 | }) | |
9850 | ||
9851 | if (offset !== buffer.length) throw new Error('Could not decode chunks') | |
9852 | return buffer | |
9853 | } | |
9854 | ||
9855 | function decompile (buffer) { | |
9856 | // TODO: remove me | |
9857 | if (types.Array(buffer)) return buffer | |
9858 | ||
9859 | typeforce(types.Buffer, buffer) | |
9860 | ||
9861 | var chunks = [] | |
9862 | var i = 0 | |
9863 | ||
9864 | while (i < buffer.length) { | |
9865 | var opcode = buffer[i] | |
9866 | ||
9867 | // data chunk | |
9868 | if ((opcode > OPS.OP_0) && (opcode <= OPS.OP_PUSHDATA4)) { | |
9869 | var d = pushdata.decode(buffer, i) | |
9870 | ||
9871 | // did reading a pushDataInt fail? empty script | |
9872 | if (d === null) return [] | |
9873 | i += d.size | |
9874 | ||
9875 | // attempt to read too much data? empty script | |
9876 | if (i + d.number > buffer.length) return [] | |
9877 | ||
9878 | var data = buffer.slice(i, i + d.number) | |
9879 | i += d.number | |
9880 | ||
9f59e99b IC |
9881 | // decompile minimally |
9882 | var op = asMinimalOP(data) | |
9883 | if (op !== undefined) { | |
9884 | chunks.push(op) | |
9885 | } else { | |
9886 | chunks.push(data) | |
9887 | } | |
a0091a40 IC |
9888 | |
9889 | // opcode | |
9890 | } else { | |
9891 | chunks.push(opcode) | |
9892 | ||
9893 | i += 1 | |
9894 | } | |
9895 | } | |
9896 | ||
9897 | return chunks | |
9898 | } | |
9899 | ||
9900 | function toASM (chunks) { | |
9901 | if (Buffer.isBuffer(chunks)) { | |
9902 | chunks = decompile(chunks) | |
9903 | } | |
9904 | ||
9905 | return chunks.map(function (chunk) { | |
9906 | // data? | |
9f59e99b IC |
9907 | if (Buffer.isBuffer(chunk)) { |
9908 | var op = asMinimalOP(chunk) | |
9909 | if (op === undefined) return chunk.toString('hex') | |
9910 | chunk = op | |
9911 | } | |
a0091a40 IC |
9912 | |
9913 | // opcode! | |
9914 | return REVERSE_OPS[chunk] | |
9915 | }).join(' ') | |
9916 | } | |
9917 | ||
9918 | function fromASM (asm) { | |
9919 | typeforce(types.String, asm) | |
9920 | ||
9921 | return compile(asm.split(' ').map(function (chunkStr) { | |
9922 | // opcode? | |
9923 | if (OPS[chunkStr] !== undefined) return OPS[chunkStr] | |
9924 | typeforce(types.Hex, chunkStr) | |
9925 | ||
9926 | // data! | |
9927 | return Buffer.from(chunkStr, 'hex') | |
9928 | })) | |
9929 | } | |
9930 | ||
9931 | function toStack (chunks) { | |
9932 | chunks = decompile(chunks) | |
9933 | typeforce(isPushOnly, chunks) | |
9934 | ||
9935 | return chunks.map(function (op) { | |
9936 | if (Buffer.isBuffer(op)) return op | |
9937 | if (op === OPS.OP_0) return Buffer.allocUnsafe(0) | |
9938 | ||
9939 | return scriptNumber.encode(op - OP_INT_BASE) | |
9940 | }) | |
9941 | } | |
9942 | ||
9943 | function isCanonicalPubKey (buffer) { | |
9944 | if (!Buffer.isBuffer(buffer)) return false | |
9945 | if (buffer.length < 33) return false | |
9946 | ||
9947 | switch (buffer[0]) { | |
9948 | case 0x02: | |
9949 | case 0x03: | |
9950 | return buffer.length === 33 | |
9951 | case 0x04: | |
9952 | return buffer.length === 65 | |
9953 | } | |
9954 | ||
9955 | return false | |
9956 | } | |
9957 | ||
9958 | function isDefinedHashType (hashType) { | |
9959 | var hashTypeMod = hashType & ~0x80 | |
9960 | ||
9961 | // return hashTypeMod > SIGHASH_ALL && hashTypeMod < SIGHASH_SINGLE | |
9962 | return hashTypeMod > 0x00 && hashTypeMod < 0x04 | |
9963 | } | |
9964 | ||
9965 | function isCanonicalSignature (buffer) { | |
9966 | if (!Buffer.isBuffer(buffer)) return false | |
9967 | if (!isDefinedHashType(buffer[buffer.length - 1])) return false | |
9968 | ||
9969 | return bip66.check(buffer.slice(0, -1)) | |
9970 | } | |
9971 | ||
9972 | module.exports = { | |
9973 | compile: compile, | |
9974 | decompile: decompile, | |
9975 | fromASM: fromASM, | |
9976 | toASM: toASM, | |
9977 | toStack: toStack, | |
9978 | ||
9979 | number: require('./script_number'), | |
9980 | ||
9981 | isCanonicalPubKey: isCanonicalPubKey, | |
9982 | isCanonicalSignature: isCanonicalSignature, | |
9983 | isPushOnly: isPushOnly, | |
9984 | isDefinedHashType: isDefinedHashType | |
9985 | } | |
9986 | ||
9f59e99b | 9987 | },{"./script_number":55,"./types":80,"bip66":41,"bitcoin-ops":42,"bitcoin-ops/map":43,"pushdata-bitcoin":125,"safe-buffer":128,"typeforce":139}],55:[function(require,module,exports){ |
a0091a40 IC |
9988 | var Buffer = require('safe-buffer').Buffer |
9989 | ||
9990 | function decode (buffer, maxLength, minimal) { | |
9991 | maxLength = maxLength || 4 | |
9992 | minimal = minimal === undefined ? true : minimal | |
9993 | ||
9994 | var length = buffer.length | |
9995 | if (length === 0) return 0 | |
9996 | if (length > maxLength) throw new TypeError('Script number overflow') | |
9997 | if (minimal) { | |
9998 | if ((buffer[length - 1] & 0x7f) === 0) { | |
9999 | if (length <= 1 || (buffer[length - 2] & 0x80) === 0) throw new Error('Non-minimally encoded script number') | |
10000 | } | |
10001 | } | |
10002 | ||
10003 | // 40-bit | |
10004 | if (length === 5) { | |
10005 | var a = buffer.readUInt32LE(0) | |
10006 | var b = buffer.readUInt8(4) | |
10007 | ||
10008 | if (b & 0x80) return -(((b & ~0x80) * 0x100000000) + a) | |
10009 | return (b * 0x100000000) + a | |
10010 | } | |
10011 | ||
10012 | var result = 0 | |
10013 | ||
10014 | // 32-bit / 24-bit / 16-bit / 8-bit | |
10015 | for (var i = 0; i < length; ++i) { | |
10016 | result |= buffer[i] << (8 * i) | |
10017 | } | |
10018 | ||
10019 | if (buffer[length - 1] & 0x80) return -(result & ~(0x80 << (8 * (length - 1)))) | |
10020 | return result | |
10021 | } | |
10022 | ||
10023 | function scriptNumSize (i) { | |
10024 | return i > 0x7fffffff ? 5 | |
10025 | : i > 0x7fffff ? 4 | |
10026 | : i > 0x7fff ? 3 | |
10027 | : i > 0x7f ? 2 | |
10028 | : i > 0x00 ? 1 | |
10029 | : 0 | |
10030 | } | |
10031 | ||
10032 | function encode (number) { | |
10033 | var value = Math.abs(number) | |
10034 | var size = scriptNumSize(value) | |
10035 | var buffer = Buffer.allocUnsafe(size) | |
10036 | var negative = number < 0 | |
10037 | ||
10038 | for (var i = 0; i < size; ++i) { | |
10039 | buffer.writeUInt8(value & 0xff, i) | |
10040 | value >>= 8 | |
10041 | } | |
10042 | ||
10043 | if (buffer[size - 1] & 0x80) { | |
10044 | buffer.writeUInt8(negative ? 0x80 : 0x00, size - 1) | |
10045 | } else if (negative) { | |
10046 | buffer[size - 1] |= 0x80 | |
10047 | } | |
10048 | ||
10049 | return buffer | |
10050 | } | |
10051 | ||
10052 | module.exports = { | |
10053 | decode: decode, | |
10054 | encode: encode | |
10055 | } | |
10056 | ||
9f59e99b | 10057 | },{"safe-buffer":128}],56:[function(require,module,exports){ |
a0091a40 IC |
10058 | var decompile = require('../script').decompile |
10059 | var multisig = require('./multisig') | |
10060 | var nullData = require('./nulldata') | |
10061 | var pubKey = require('./pubkey') | |
10062 | var pubKeyHash = require('./pubkeyhash') | |
10063 | var scriptHash = require('./scripthash') | |
10064 | var witnessPubKeyHash = require('./witnesspubkeyhash') | |
10065 | var witnessScriptHash = require('./witnessscripthash') | |
10066 | var witnessCommitment = require('./witnesscommitment') | |
10067 | ||
10068 | var types = { | |
10069 | MULTISIG: 'multisig', | |
10070 | NONSTANDARD: 'nonstandard', | |
10071 | NULLDATA: 'nulldata', | |
10072 | P2PK: 'pubkey', | |
10073 | P2PKH: 'pubkeyhash', | |
10074 | P2SH: 'scripthash', | |
10075 | P2WPKH: 'witnesspubkeyhash', | |
10076 | P2WSH: 'witnessscripthash', | |
10077 | WITNESS_COMMITMENT: 'witnesscommitment' | |
10078 | } | |
10079 | ||
10080 | function classifyOutput (script) { | |
10081 | if (witnessPubKeyHash.output.check(script)) return types.P2WPKH | |
10082 | if (witnessScriptHash.output.check(script)) return types.P2WSH | |
10083 | if (pubKeyHash.output.check(script)) return types.P2PKH | |
10084 | if (scriptHash.output.check(script)) return types.P2SH | |
10085 | ||
10086 | // XXX: optimization, below functions .decompile before use | |
10087 | var chunks = decompile(script) | |
10088 | if (multisig.output.check(chunks)) return types.MULTISIG | |
10089 | if (pubKey.output.check(chunks)) return types.P2PK | |
10090 | if (witnessCommitment.output.check(chunks)) return types.WITNESS_COMMITMENT | |
10091 | if (nullData.output.check(chunks)) return types.NULLDATA | |
10092 | ||
10093 | return types.NONSTANDARD | |
10094 | } | |
10095 | ||
10096 | function classifyInput (script, allowIncomplete) { | |
10097 | // XXX: optimization, below functions .decompile before use | |
10098 | var chunks = decompile(script) | |
10099 | ||
10100 | if (pubKeyHash.input.check(chunks)) return types.P2PKH | |
10101 | if (scriptHash.input.check(chunks, allowIncomplete)) return types.P2SH | |
10102 | if (multisig.input.check(chunks, allowIncomplete)) return types.MULTISIG | |
10103 | if (pubKey.input.check(chunks)) return types.P2PK | |
10104 | ||
10105 | return types.NONSTANDARD | |
10106 | } | |
10107 | ||
10108 | function classifyWitness (script, allowIncomplete) { | |
10109 | // XXX: optimization, below functions .decompile before use | |
10110 | var chunks = decompile(script) | |
10111 | ||
10112 | if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH | |
10113 | if (witnessScriptHash.input.check(chunks, allowIncomplete)) return types.P2WSH | |
10114 | ||
10115 | return types.NONSTANDARD | |
10116 | } | |
10117 | ||
10118 | module.exports = { | |
10119 | classifyInput: classifyInput, | |
10120 | classifyOutput: classifyOutput, | |
10121 | classifyWitness: classifyWitness, | |
10122 | multisig: multisig, | |
10123 | nullData: nullData, | |
10124 | pubKey: pubKey, | |
10125 | pubKeyHash: pubKeyHash, | |
10126 | scriptHash: scriptHash, | |
10127 | witnessPubKeyHash: witnessPubKeyHash, | |
10128 | witnessScriptHash: witnessScriptHash, | |
10129 | witnessCommitment: witnessCommitment, | |
10130 | types: types | |
10131 | } | |
10132 | ||
9f59e99b | 10133 | },{"../script":54,"./multisig":57,"./nulldata":60,"./pubkey":61,"./pubkeyhash":64,"./scripthash":67,"./witnesscommitment":70,"./witnesspubkeyhash":72,"./witnessscripthash":75}],57:[function(require,module,exports){ |
a0091a40 IC |
10134 | module.exports = { |
10135 | input: require('./input'), | |
10136 | output: require('./output') | |
10137 | } | |
10138 | ||
9f59e99b | 10139 | },{"./input":58,"./output":59}],58:[function(require,module,exports){ |
a0091a40 IC |
10140 | // OP_0 [signatures ...] |
10141 | ||
10142 | var Buffer = require('safe-buffer').Buffer | |
10143 | var bscript = require('../../script') | |
9f59e99b | 10144 | var p2mso = require('./output') |
a0091a40 IC |
10145 | var typeforce = require('typeforce') |
10146 | var OPS = require('bitcoin-ops') | |
10147 | ||
10148 | function partialSignature (value) { | |
10149 | return value === OPS.OP_0 || bscript.isCanonicalSignature(value) | |
10150 | } | |
10151 | ||
10152 | function check (script, allowIncomplete) { | |
10153 | var chunks = bscript.decompile(script) | |
10154 | if (chunks.length < 2) return false | |
10155 | if (chunks[0] !== OPS.OP_0) return false | |
10156 | ||
10157 | if (allowIncomplete) { | |
10158 | return chunks.slice(1).every(partialSignature) | |
10159 | } | |
10160 | ||
10161 | return chunks.slice(1).every(bscript.isCanonicalSignature) | |
10162 | } | |
10163 | check.toJSON = function () { return 'multisig input' } | |
10164 | ||
10165 | var EMPTY_BUFFER = Buffer.allocUnsafe(0) | |
10166 | ||
10167 | function encodeStack (signatures, scriptPubKey) { | |
10168 | typeforce([partialSignature], signatures) | |
10169 | ||
10170 | if (scriptPubKey) { | |
9f59e99b | 10171 | var scriptData = p2mso.decode(scriptPubKey) |
a0091a40 IC |
10172 | |
10173 | if (signatures.length < scriptData.m) { | |
10174 | throw new TypeError('Not enough signatures provided') | |
10175 | } | |
10176 | ||
10177 | if (signatures.length > scriptData.pubKeys.length) { | |
10178 | throw new TypeError('Too many signatures provided') | |
10179 | } | |
10180 | } | |
10181 | ||
9f59e99b IC |
10182 | return [].concat(EMPTY_BUFFER, signatures.map(function (sig) { |
10183 | if (sig === OPS.OP_0) { | |
10184 | return EMPTY_BUFFER | |
10185 | } | |
10186 | return sig | |
10187 | })) | |
a0091a40 IC |
10188 | } |
10189 | ||
10190 | function encode (signatures, scriptPubKey) { | |
10191 | return bscript.compile(encodeStack(signatures, scriptPubKey)) | |
10192 | } | |
10193 | ||
10194 | function decodeStack (stack, allowIncomplete) { | |
10195 | typeforce(check, stack, allowIncomplete) | |
10196 | return stack.slice(1) | |
10197 | } | |
10198 | ||
10199 | function decode (buffer, allowIncomplete) { | |
10200 | var stack = bscript.decompile(buffer) | |
10201 | return decodeStack(stack, allowIncomplete) | |
10202 | } | |
10203 | ||
10204 | module.exports = { | |
10205 | check: check, | |
10206 | decode: decode, | |
10207 | decodeStack: decodeStack, | |
10208 | encode: encode, | |
10209 | encodeStack: encodeStack | |
10210 | } | |
10211 | ||
9f59e99b | 10212 | },{"../../script":54,"./output":59,"bitcoin-ops":42,"safe-buffer":128,"typeforce":139}],59:[function(require,module,exports){ |
a0091a40 IC |
10213 | // m [pubKeys ...] n OP_CHECKMULTISIG |
10214 | ||
10215 | var bscript = require('../../script') | |
10216 | var types = require('../../types') | |
10217 | var typeforce = require('typeforce') | |
10218 | var OPS = require('bitcoin-ops') | |
10219 | var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 | |
10220 | ||
10221 | function check (script, allowIncomplete) { | |
10222 | var chunks = bscript.decompile(script) | |
10223 | ||
10224 | if (chunks.length < 4) return false | |
10225 | if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) return false | |
10226 | if (!types.Number(chunks[0])) return false | |
10227 | if (!types.Number(chunks[chunks.length - 2])) return false | |
10228 | var m = chunks[0] - OP_INT_BASE | |
10229 | var n = chunks[chunks.length - 2] - OP_INT_BASE | |
10230 | ||
10231 | if (m <= 0) return false | |
10232 | if (n > 16) return false | |
10233 | if (m > n) return false | |
10234 | if (n !== chunks.length - 3) return false | |
10235 | if (allowIncomplete) return true | |
10236 | ||
10237 | var keys = chunks.slice(1, -2) | |
10238 | return keys.every(bscript.isCanonicalPubKey) | |
10239 | } | |
10240 | check.toJSON = function () { return 'multi-sig output' } | |
10241 | ||
10242 | function encode (m, pubKeys) { | |
10243 | typeforce({ | |
10244 | m: types.Number, | |
10245 | pubKeys: [bscript.isCanonicalPubKey] | |
10246 | }, { | |
10247 | m: m, | |
10248 | pubKeys: pubKeys | |
10249 | }) | |
10250 | ||
10251 | var n = pubKeys.length | |
10252 | if (n < m) throw new TypeError('Not enough pubKeys provided') | |
10253 | ||
10254 | return bscript.compile([].concat( | |
10255 | OP_INT_BASE + m, | |
10256 | pubKeys, | |
10257 | OP_INT_BASE + n, | |
10258 | OPS.OP_CHECKMULTISIG | |
10259 | )) | |
10260 | } | |
10261 | ||
10262 | function decode (buffer, allowIncomplete) { | |
10263 | var chunks = bscript.decompile(buffer) | |
10264 | typeforce(check, chunks, allowIncomplete) | |
10265 | ||
10266 | return { | |
10267 | m: chunks[0] - OP_INT_BASE, | |
10268 | pubKeys: chunks.slice(1, -2) | |
10269 | } | |
10270 | } | |
10271 | ||
10272 | module.exports = { | |
10273 | check: check, | |
10274 | decode: decode, | |
10275 | encode: encode | |
10276 | } | |
10277 | ||
9f59e99b | 10278 | },{"../../script":54,"../../types":80,"bitcoin-ops":42,"typeforce":139}],60:[function(require,module,exports){ |
a0091a40 IC |
10279 | // OP_RETURN {data} |
10280 | ||
10281 | var bscript = require('../script') | |
10282 | var types = require('../types') | |
10283 | var typeforce = require('typeforce') | |
10284 | var OPS = require('bitcoin-ops') | |
10285 | ||
10286 | function check (script) { | |
10287 | var buffer = bscript.compile(script) | |
10288 | ||
10289 | return buffer.length > 1 && | |
10290 | buffer[0] === OPS.OP_RETURN | |
10291 | } | |
10292 | check.toJSON = function () { return 'null data output' } | |
10293 | ||
10294 | function encode (data) { | |
10295 | typeforce(types.Buffer, data) | |
10296 | ||
10297 | return bscript.compile([OPS.OP_RETURN, data]) | |
10298 | } | |
10299 | ||
10300 | function decode (buffer) { | |
10301 | typeforce(check, buffer) | |
10302 | ||
10303 | return buffer.slice(2) | |
10304 | } | |
10305 | ||
10306 | module.exports = { | |
10307 | output: { | |
10308 | check: check, | |
10309 | decode: decode, | |
10310 | encode: encode | |
10311 | } | |
10312 | } | |
10313 | ||
9f59e99b IC |
10314 | },{"../script":54,"../types":80,"bitcoin-ops":42,"typeforce":139}],61:[function(require,module,exports){ |
10315 | arguments[4][57][0].apply(exports,arguments) | |
10316 | },{"./input":62,"./output":63,"dup":57}],62:[function(require,module,exports){ | |
a0091a40 IC |
10317 | // {signature} |
10318 | ||
10319 | var bscript = require('../../script') | |
a0091a40 IC |
10320 | var typeforce = require('typeforce') |
10321 | ||
10322 | function check (script) { | |
10323 | var chunks = bscript.decompile(script) | |
10324 | ||
10325 | return chunks.length === 1 && | |
10326 | bscript.isCanonicalSignature(chunks[0]) | |
10327 | } | |
10328 | check.toJSON = function () { return 'pubKey input' } | |
10329 | ||
10330 | function encodeStack (signature) { | |
9f59e99b | 10331 | typeforce(bscript.isCanonicalSignature, signature) |
a0091a40 IC |
10332 | return [signature] |
10333 | } | |
10334 | ||
10335 | function encode (signature) { | |
10336 | return bscript.compile(encodeStack(signature)) | |
10337 | } | |
10338 | ||
10339 | function decodeStack (stack) { | |
10340 | typeforce(check, stack) | |
10341 | return stack[0] | |
10342 | } | |
10343 | ||
10344 | function decode (buffer) { | |
10345 | var stack = bscript.decompile(buffer) | |
10346 | return decodeStack(stack) | |
10347 | } | |
10348 | ||
10349 | module.exports = { | |
10350 | check: check, | |
10351 | decode: decode, | |
10352 | decodeStack: decodeStack, | |
10353 | encode: encode, | |
10354 | encodeStack: encodeStack | |
10355 | } | |
10356 | ||
9f59e99b | 10357 | },{"../../script":54,"typeforce":139}],63:[function(require,module,exports){ |
a0091a40 IC |
10358 | // {pubKey} OP_CHECKSIG |
10359 | ||
10360 | var bscript = require('../../script') | |
10361 | var typeforce = require('typeforce') | |
10362 | var OPS = require('bitcoin-ops') | |
10363 | ||
10364 | function check (script) { | |
10365 | var chunks = bscript.decompile(script) | |
10366 | ||
10367 | return chunks.length === 2 && | |
10368 | bscript.isCanonicalPubKey(chunks[0]) && | |
10369 | chunks[1] === OPS.OP_CHECKSIG | |
10370 | } | |
10371 | check.toJSON = function () { return 'pubKey output' } | |
10372 | ||
10373 | function encode (pubKey) { | |
10374 | typeforce(bscript.isCanonicalPubKey, pubKey) | |
10375 | ||
10376 | return bscript.compile([pubKey, OPS.OP_CHECKSIG]) | |
10377 | } | |
10378 | ||
10379 | function decode (buffer) { | |
10380 | var chunks = bscript.decompile(buffer) | |
10381 | typeforce(check, chunks) | |
10382 | ||
10383 | return chunks[0] | |
10384 | } | |
10385 | ||
10386 | module.exports = { | |
10387 | check: check, | |
10388 | decode: decode, | |
10389 | encode: encode | |
10390 | } | |
10391 | ||
9f59e99b IC |
10392 | },{"../../script":54,"bitcoin-ops":42,"typeforce":139}],64:[function(require,module,exports){ |
10393 | arguments[4][57][0].apply(exports,arguments) | |
10394 | },{"./input":65,"./output":66,"dup":57}],65:[function(require,module,exports){ | |
a0091a40 IC |
10395 | // {signature} {pubKey} |
10396 | ||
10397 | var bscript = require('../../script') | |
a0091a40 IC |
10398 | var typeforce = require('typeforce') |
10399 | ||
10400 | function check (script) { | |
10401 | var chunks = bscript.decompile(script) | |
10402 | ||
10403 | return chunks.length === 2 && | |
10404 | bscript.isCanonicalSignature(chunks[0]) && | |
10405 | bscript.isCanonicalPubKey(chunks[1]) | |
10406 | } | |
10407 | check.toJSON = function () { return 'pubKeyHash input' } | |
10408 | ||
10409 | function encodeStack (signature, pubKey) { | |
10410 | typeforce({ | |
9f59e99b IC |
10411 | signature: bscript.isCanonicalSignature, |
10412 | pubKey: bscript.isCanonicalPubKey | |
a0091a40 | 10413 | }, { |
9f59e99b IC |
10414 | signature: signature, |
10415 | pubKey: pubKey | |
a0091a40 IC |
10416 | }) |
10417 | ||
10418 | return [signature, pubKey] | |
10419 | } | |
10420 | ||
10421 | function encode (signature, pubKey) { | |
10422 | return bscript.compile(encodeStack(signature, pubKey)) | |
10423 | } | |
10424 | ||
10425 | function decodeStack (stack) { | |
10426 | typeforce(check, stack) | |
10427 | ||
10428 | return { | |
10429 | signature: stack[0], | |
10430 | pubKey: stack[1] | |
10431 | } | |
10432 | } | |
10433 | ||
10434 | function decode (buffer) { | |
10435 | var stack = bscript.decompile(buffer) | |
10436 | return decodeStack(stack) | |
10437 | } | |
10438 | ||
10439 | module.exports = { | |
10440 | check: check, | |
10441 | decode: decode, | |
10442 | decodeStack: decodeStack, | |
10443 | encode: encode, | |
10444 | encodeStack: encodeStack | |
10445 | } | |
10446 | ||
9f59e99b | 10447 | },{"../../script":54,"typeforce":139}],66:[function(require,module,exports){ |
a0091a40 IC |
10448 | // OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG |
10449 | ||
10450 | var bscript = require('../../script') | |
10451 | var types = require('../../types') | |
10452 | var typeforce = require('typeforce') | |
10453 | var OPS = require('bitcoin-ops') | |
10454 | ||
10455 | function check (script) { | |
10456 | var buffer = bscript.compile(script) | |
10457 | ||
10458 | return buffer.length === 25 && | |
10459 | buffer[0] === OPS.OP_DUP && | |
10460 | buffer[1] === OPS.OP_HASH160 && | |
10461 | buffer[2] === 0x14 && | |
10462 | buffer[23] === OPS.OP_EQUALVERIFY && | |
10463 | buffer[24] === OPS.OP_CHECKSIG | |
10464 | } | |
10465 | check.toJSON = function () { return 'pubKeyHash output' } | |
10466 | ||
10467 | function encode (pubKeyHash) { | |
10468 | typeforce(types.Hash160bit, pubKeyHash) | |
10469 | ||
10470 | return bscript.compile([ | |
10471 | OPS.OP_DUP, | |
10472 | OPS.OP_HASH160, | |
10473 | pubKeyHash, | |
10474 | OPS.OP_EQUALVERIFY, | |
10475 | OPS.OP_CHECKSIG | |
10476 | ]) | |
10477 | } | |
10478 | ||
10479 | function decode (buffer) { | |
10480 | typeforce(check, buffer) | |
10481 | ||
10482 | return buffer.slice(3, 23) | |
10483 | } | |
10484 | ||
10485 | module.exports = { | |
10486 | check: check, | |
10487 | decode: decode, | |
10488 | encode: encode | |
10489 | } | |
10490 | ||
9f59e99b IC |
10491 | },{"../../script":54,"../../types":80,"bitcoin-ops":42,"typeforce":139}],67:[function(require,module,exports){ |
10492 | arguments[4][57][0].apply(exports,arguments) | |
10493 | },{"./input":68,"./output":69,"dup":57}],68:[function(require,module,exports){ | |
a0091a40 IC |
10494 | // <scriptSig> {serialized scriptPubKey script} |
10495 | ||
10496 | var Buffer = require('safe-buffer').Buffer | |
10497 | var bscript = require('../../script') | |
10498 | var typeforce = require('typeforce') | |
10499 | ||
9f59e99b IC |
10500 | var p2ms = require('../multisig/') |
10501 | var p2pk = require('../pubkey/') | |
10502 | var p2pkh = require('../pubkeyhash/') | |
10503 | var p2wpkho = require('../witnesspubkeyhash/output') | |
10504 | var p2wsho = require('../witnessscripthash/output') | |
10505 | ||
a0091a40 IC |
10506 | function check (script, allowIncomplete) { |
10507 | var chunks = bscript.decompile(script) | |
10508 | if (chunks.length < 1) return false | |
10509 | ||
10510 | var lastChunk = chunks[chunks.length - 1] | |
10511 | if (!Buffer.isBuffer(lastChunk)) return false | |
10512 | ||
10513 | var scriptSigChunks = bscript.decompile(bscript.compile(chunks.slice(0, -1))) | |
10514 | var redeemScriptChunks = bscript.decompile(lastChunk) | |
10515 | ||
10516 | // is redeemScript a valid script? | |
10517 | if (redeemScriptChunks.length === 0) return false | |
10518 | ||
10519 | // is redeemScriptSig push only? | |
10520 | if (!bscript.isPushOnly(scriptSigChunks)) return false | |
10521 | ||
9f59e99b | 10522 | // is witness? |
a0091a40 | 10523 | if (chunks.length === 1) { |
9f59e99b IC |
10524 | return p2wsho.check(redeemScriptChunks) || |
10525 | p2wpkho.check(redeemScriptChunks) | |
a0091a40 | 10526 | } |
9f59e99b IC |
10527 | |
10528 | // match types | |
10529 | if (p2pkh.input.check(scriptSigChunks) && | |
10530 | p2pkh.output.check(redeemScriptChunks)) return true | |
10531 | ||
10532 | if (p2ms.input.check(scriptSigChunks, allowIncomplete) && | |
10533 | p2ms.output.check(redeemScriptChunks)) return true | |
10534 | ||
10535 | if (p2pk.input.check(scriptSigChunks) && | |
10536 | p2pk.output.check(redeemScriptChunks)) return true | |
10537 | ||
10538 | return false | |
a0091a40 IC |
10539 | } |
10540 | check.toJSON = function () { return 'scriptHash input' } | |
10541 | ||
10542 | function encodeStack (redeemScriptStack, redeemScript) { | |
10543 | var serializedScriptPubKey = bscript.compile(redeemScript) | |
10544 | ||
10545 | return [].concat(redeemScriptStack, serializedScriptPubKey) | |
10546 | } | |
10547 | ||
10548 | function encode (redeemScriptSig, redeemScript) { | |
10549 | var redeemScriptStack = bscript.decompile(redeemScriptSig) | |
10550 | ||
10551 | return bscript.compile(encodeStack(redeemScriptStack, redeemScript)) | |
10552 | } | |
10553 | ||
10554 | function decodeStack (stack) { | |
10555 | typeforce(check, stack) | |
10556 | ||
10557 | return { | |
10558 | redeemScriptStack: stack.slice(0, -1), | |
10559 | redeemScript: stack[stack.length - 1] | |
10560 | } | |
10561 | } | |
10562 | ||
10563 | function decode (buffer) { | |
10564 | var stack = bscript.decompile(buffer) | |
10565 | var result = decodeStack(stack) | |
10566 | result.redeemScriptSig = bscript.compile(result.redeemScriptStack) | |
10567 | delete result.redeemScriptStack | |
10568 | return result | |
10569 | } | |
10570 | ||
10571 | module.exports = { | |
10572 | check: check, | |
10573 | decode: decode, | |
10574 | decodeStack: decodeStack, | |
10575 | encode: encode, | |
10576 | encodeStack: encodeStack | |
10577 | } | |
10578 | ||
9f59e99b | 10579 | },{"../../script":54,"../multisig/":57,"../pubkey/":61,"../pubkeyhash/":64,"../witnesspubkeyhash/output":74,"../witnessscripthash/output":77,"safe-buffer":128,"typeforce":139}],69:[function(require,module,exports){ |
a0091a40 IC |
10580 | // OP_HASH160 {scriptHash} OP_EQUAL |
10581 | ||
10582 | var bscript = require('../../script') | |
10583 | var types = require('../../types') | |
10584 | var typeforce = require('typeforce') | |
10585 | var OPS = require('bitcoin-ops') | |
10586 | ||
10587 | function check (script) { | |
10588 | var buffer = bscript.compile(script) | |
10589 | ||
10590 | return buffer.length === 23 && | |
10591 | buffer[0] === OPS.OP_HASH160 && | |
10592 | buffer[1] === 0x14 && | |
10593 | buffer[22] === OPS.OP_EQUAL | |
10594 | } | |
10595 | check.toJSON = function () { return 'scriptHash output' } | |
10596 | ||
10597 | function encode (scriptHash) { | |
10598 | typeforce(types.Hash160bit, scriptHash) | |
10599 | ||
10600 | return bscript.compile([OPS.OP_HASH160, scriptHash, OPS.OP_EQUAL]) | |
10601 | } | |
10602 | ||
10603 | function decode (buffer) { | |
10604 | typeforce(check, buffer) | |
10605 | ||
10606 | return buffer.slice(2, 22) | |
10607 | } | |
10608 | ||
10609 | module.exports = { | |
10610 | check: check, | |
10611 | decode: decode, | |
10612 | encode: encode | |
10613 | } | |
10614 | ||
9f59e99b | 10615 | },{"../../script":54,"../../types":80,"bitcoin-ops":42,"typeforce":139}],70:[function(require,module,exports){ |
a0091a40 IC |
10616 | module.exports = { |
10617 | output: require('./output') | |
10618 | } | |
10619 | ||
9f59e99b | 10620 | },{"./output":71}],71:[function(require,module,exports){ |
a0091a40 IC |
10621 | // OP_RETURN {aa21a9ed} {commitment} |
10622 | ||
10623 | var Buffer = require('safe-buffer').Buffer | |
10624 | var bscript = require('../../script') | |
10625 | var types = require('../../types') | |
10626 | var typeforce = require('typeforce') | |
10627 | var OPS = require('bitcoin-ops') | |
10628 | ||
10629 | var HEADER = Buffer.from('aa21a9ed', 'hex') | |
10630 | ||
10631 | function check (script) { | |
10632 | var buffer = bscript.compile(script) | |
10633 | ||
10634 | return buffer.length > 37 && | |
10635 | buffer[0] === OPS.OP_RETURN && | |
10636 | buffer[1] === 0x24 && | |
10637 | buffer.slice(2, 6).equals(HEADER) | |
10638 | } | |
10639 | ||
10640 | check.toJSON = function () { return 'Witness commitment output' } | |
10641 | ||
10642 | function encode (commitment) { | |
10643 | typeforce(types.Hash256bit, commitment) | |
10644 | ||
10645 | var buffer = Buffer.allocUnsafe(36) | |
10646 | HEADER.copy(buffer, 0) | |
10647 | commitment.copy(buffer, 4) | |
10648 | ||
10649 | return bscript.compile([OPS.OP_RETURN, buffer]) | |
10650 | } | |
10651 | ||
10652 | function decode (buffer) { | |
10653 | typeforce(check, buffer) | |
10654 | ||
10655 | return bscript.decompile(buffer)[1].slice(4, 36) | |
10656 | } | |
10657 | ||
10658 | module.exports = { | |
10659 | check: check, | |
10660 | decode: decode, | |
10661 | encode: encode | |
10662 | } | |
10663 | ||
9f59e99b IC |
10664 | },{"../../script":54,"../../types":80,"bitcoin-ops":42,"safe-buffer":128,"typeforce":139}],72:[function(require,module,exports){ |
10665 | arguments[4][57][0].apply(exports,arguments) | |
10666 | },{"./input":73,"./output":74,"dup":57}],73:[function(require,module,exports){ | |
a0091a40 IC |
10667 | // {signature} {pubKey} |
10668 | ||
9f59e99b IC |
10669 | var bscript = require('../../script') |
10670 | var typeforce = require('typeforce') | |
10671 | ||
10672 | function isCompressedCanonicalPubKey (pubKey) { | |
10673 | return bscript.isCanonicalPubKey(pubKey) && pubKey.length === 33 | |
10674 | } | |
10675 | ||
10676 | function check (script) { | |
10677 | var chunks = bscript.decompile(script) | |
10678 | ||
10679 | return chunks.length === 2 && | |
10680 | bscript.isCanonicalSignature(chunks[0]) && | |
10681 | isCompressedCanonicalPubKey(chunks[1]) | |
10682 | } | |
10683 | check.toJSON = function () { return 'witnessPubKeyHash input' } | |
10684 | ||
10685 | function encodeStack (signature, pubKey) { | |
10686 | typeforce({ | |
10687 | signature: bscript.isCanonicalSignature, | |
10688 | pubKey: isCompressedCanonicalPubKey | |
10689 | }, { | |
10690 | signature: signature, | |
10691 | pubKey: pubKey | |
10692 | }) | |
10693 | ||
10694 | return [signature, pubKey] | |
10695 | } | |
10696 | ||
10697 | function decodeStack (stack) { | |
10698 | typeforce(check, stack) | |
10699 | ||
10700 | return { | |
10701 | signature: stack[0], | |
10702 | pubKey: stack[1] | |
10703 | } | |
10704 | } | |
a0091a40 IC |
10705 | |
10706 | module.exports = { | |
9f59e99b IC |
10707 | check: check, |
10708 | decodeStack: decodeStack, | |
10709 | encodeStack: encodeStack | |
a0091a40 IC |
10710 | } |
10711 | ||
9f59e99b | 10712 | },{"../../script":54,"typeforce":139}],74:[function(require,module,exports){ |
a0091a40 IC |
10713 | // OP_0 {pubKeyHash} |
10714 | ||
10715 | var bscript = require('../../script') | |
10716 | var types = require('../../types') | |
10717 | var typeforce = require('typeforce') | |
10718 | var OPS = require('bitcoin-ops') | |
10719 | ||
10720 | function check (script) { | |
10721 | var buffer = bscript.compile(script) | |
10722 | ||
10723 | return buffer.length === 22 && | |
10724 | buffer[0] === OPS.OP_0 && | |
10725 | buffer[1] === 0x14 | |
10726 | } | |
10727 | check.toJSON = function () { return 'Witness pubKeyHash output' } | |
10728 | ||
10729 | function encode (pubKeyHash) { | |
10730 | typeforce(types.Hash160bit, pubKeyHash) | |
10731 | ||
10732 | return bscript.compile([OPS.OP_0, pubKeyHash]) | |
10733 | } | |
10734 | ||
10735 | function decode (buffer) { | |
10736 | typeforce(check, buffer) | |
10737 | ||
10738 | return buffer.slice(2) | |
10739 | } | |
10740 | ||
10741 | module.exports = { | |
10742 | check: check, | |
10743 | decode: decode, | |
10744 | encode: encode | |
10745 | } | |
10746 | ||
9f59e99b IC |
10747 | },{"../../script":54,"../../types":80,"bitcoin-ops":42,"typeforce":139}],75:[function(require,module,exports){ |
10748 | arguments[4][57][0].apply(exports,arguments) | |
10749 | },{"./input":76,"./output":77,"dup":57}],76:[function(require,module,exports){ | |
10750 | (function (Buffer){ | |
10751 | // <scriptSig> {serialized scriptPubKey script} | |
10752 | ||
10753 | var bscript = require('../../script') | |
10754 | var types = require('../../types') | |
10755 | var typeforce = require('typeforce') | |
10756 | ||
10757 | var p2ms = require('../multisig/') | |
10758 | var p2pk = require('../pubkey/') | |
10759 | var p2pkh = require('../pubkeyhash/') | |
10760 | ||
10761 | function check (chunks, allowIncomplete) { | |
10762 | typeforce(types.Array, chunks) | |
10763 | if (chunks.length < 1) return false | |
10764 | ||
10765 | var witnessScript = chunks[chunks.length - 1] | |
10766 | if (!Buffer.isBuffer(witnessScript)) return false | |
10767 | ||
10768 | var witnessScriptChunks = bscript.decompile(witnessScript) | |
10769 | ||
10770 | // is witnessScript a valid script? | |
10771 | if (witnessScriptChunks.length === 0) return false | |
10772 | ||
10773 | var witnessRawScriptSig = bscript.compile(chunks.slice(0, -1)) | |
10774 | ||
10775 | // match types | |
10776 | if (p2pkh.input.check(witnessRawScriptSig) && | |
10777 | p2pkh.output.check(witnessScriptChunks)) return true | |
10778 | ||
10779 | if (p2ms.input.check(witnessRawScriptSig, allowIncomplete) && | |
10780 | p2ms.output.check(witnessScriptChunks)) return true | |
10781 | ||
10782 | if (p2pk.input.check(witnessRawScriptSig) && | |
10783 | p2pk.output.check(witnessScriptChunks)) return true | |
10784 | ||
10785 | return false | |
10786 | } | |
10787 | check.toJSON = function () { return 'witnessScriptHash input' } | |
10788 | ||
10789 | function encodeStack (witnessData, witnessScript) { | |
10790 | typeforce({ | |
10791 | witnessData: [types.Buffer], | |
10792 | witnessScript: types.Buffer | |
10793 | }, { | |
10794 | witnessData: witnessData, | |
10795 | witnessScript: witnessScript | |
10796 | }) | |
10797 | ||
10798 | return [].concat(witnessData, witnessScript) | |
10799 | } | |
a0091a40 | 10800 | |
9f59e99b IC |
10801 | function decodeStack (chunks) { |
10802 | typeforce(check, chunks) | |
10803 | return { | |
10804 | witnessData: chunks.slice(0, -1), | |
10805 | witnessScript: chunks[chunks.length - 1] | |
10806 | } | |
10807 | } | |
a0091a40 IC |
10808 | |
10809 | module.exports = { | |
9f59e99b IC |
10810 | check: check, |
10811 | decodeStack: decodeStack, | |
10812 | encodeStack: encodeStack | |
a0091a40 IC |
10813 | } |
10814 | ||
9f59e99b IC |
10815 | }).call(this,{"isBuffer":require("../../../../../../../../.nvm/versions/node/v6.0.0/lib/node_modules/browserify/node_modules/is-buffer/index.js")}) |
10816 | },{"../../../../../../../../.nvm/versions/node/v6.0.0/lib/node_modules/browserify/node_modules/is-buffer/index.js":10,"../../script":54,"../../types":80,"../multisig/":57,"../pubkey/":61,"../pubkeyhash/":64,"typeforce":139}],77:[function(require,module,exports){ | |
a0091a40 IC |
10817 | // OP_0 {scriptHash} |
10818 | ||
10819 | var bscript = require('../../script') | |
10820 | var types = require('../../types') | |
10821 | var typeforce = require('typeforce') | |
10822 | var OPS = require('bitcoin-ops') | |
10823 | ||
10824 | function check (script) { | |
10825 | var buffer = bscript.compile(script) | |
10826 | ||
10827 | return buffer.length === 34 && | |
10828 | buffer[0] === OPS.OP_0 && | |
10829 | buffer[1] === 0x20 | |
10830 | } | |
10831 | check.toJSON = function () { return 'Witness scriptHash output' } | |
10832 | ||
10833 | function encode (scriptHash) { | |
10834 | typeforce(types.Hash256bit, scriptHash) | |
10835 | ||
10836 | return bscript.compile([OPS.OP_0, scriptHash]) | |
10837 | } | |
10838 | ||
10839 | function decode (buffer) { | |
10840 | typeforce(check, buffer) | |
10841 | ||
10842 | return buffer.slice(2) | |
10843 | } | |
10844 | ||
10845 | module.exports = { | |
10846 | check: check, | |
10847 | decode: decode, | |
10848 | encode: encode | |
10849 | } | |
10850 | ||
9f59e99b | 10851 | },{"../../script":54,"../../types":80,"bitcoin-ops":42,"typeforce":139}],78:[function(require,module,exports){ |
a0091a40 IC |
10852 | var Buffer = require('safe-buffer').Buffer |
10853 | var bcrypto = require('./crypto') | |
10854 | var bscript = require('./script') | |
10855 | var bufferutils = require('./bufferutils') | |
10856 | var opcodes = require('bitcoin-ops') | |
10857 | var typeforce = require('typeforce') | |
10858 | var types = require('./types') | |
10859 | var varuint = require('varuint-bitcoin') | |
10860 | ||
10861 | function varSliceSize (someScript) { | |
10862 | var length = someScript.length | |
10863 | ||
10864 | return varuint.encodingLength(length) + length | |
10865 | } | |
10866 | ||
10867 | function vectorSize (someVector) { | |
10868 | var length = someVector.length | |
10869 | ||
10870 | return varuint.encodingLength(length) + someVector.reduce(function (sum, witness) { | |
10871 | return sum + varSliceSize(witness) | |
10872 | }, 0) | |
10873 | } | |
10874 | ||
10875 | function Transaction () { | |
10876 | this.version = 1 | |
10877 | this.locktime = 0 | |
10878 | this.ins = [] | |
10879 | this.outs = [] | |
10880 | } | |
10881 | ||
10882 | Transaction.DEFAULT_SEQUENCE = 0xffffffff | |
10883 | Transaction.SIGHASH_ALL = 0x01 | |
10884 | Transaction.SIGHASH_NONE = 0x02 | |
10885 | Transaction.SIGHASH_SINGLE = 0x03 | |
10886 | Transaction.SIGHASH_ANYONECANPAY = 0x80 | |
10887 | Transaction.ADVANCED_TRANSACTION_MARKER = 0x00 | |
10888 | Transaction.ADVANCED_TRANSACTION_FLAG = 0x01 | |
10889 | ||
10890 | var EMPTY_SCRIPT = Buffer.allocUnsafe(0) | |
10891 | var EMPTY_WITNESS = [] | |
10892 | var ZERO = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex') | |
10893 | var ONE = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex') | |
10894 | var VALUE_UINT64_MAX = Buffer.from('ffffffffffffffff', 'hex') | |
10895 | var BLANK_OUTPUT = { | |
10896 | script: EMPTY_SCRIPT, | |
10897 | valueBuffer: VALUE_UINT64_MAX | |
10898 | } | |
10899 | ||
10900 | Transaction.fromBuffer = function (buffer, __noStrict) { | |
10901 | var offset = 0 | |
10902 | function readSlice (n) { | |
10903 | offset += n | |
10904 | return buffer.slice(offset - n, offset) | |
10905 | } | |
10906 | ||
10907 | function readUInt32 () { | |
10908 | var i = buffer.readUInt32LE(offset) | |
10909 | offset += 4 | |
10910 | return i | |
10911 | } | |
10912 | ||
10913 | function readInt32 () { | |
10914 | var i = buffer.readInt32LE(offset) | |
10915 | offset += 4 | |
10916 | return i | |
10917 | } | |
10918 | ||
10919 | function readUInt64 () { | |
10920 | var i = bufferutils.readUInt64LE(buffer, offset) | |
10921 | offset += 8 | |
10922 | return i | |
10923 | } | |
10924 | ||
10925 | function readVarInt () { | |
10926 | var vi = varuint.decode(buffer, offset) | |
10927 | offset += varuint.decode.bytes | |
10928 | return vi | |
10929 | } | |
10930 | ||
10931 | function readVarSlice () { | |
10932 | return readSlice(readVarInt()) | |
10933 | } | |
10934 | ||
10935 | function readVector () { | |
10936 | var count = readVarInt() | |
10937 | var vector = [] | |
10938 | for (var i = 0; i < count; i++) vector.push(readVarSlice()) | |
10939 | return vector | |
10940 | } | |
10941 | ||
10942 | var tx = new Transaction() | |
10943 | tx.version = readInt32() | |
10944 | ||
10945 | var marker = buffer.readUInt8(offset) | |
10946 | var flag = buffer.readUInt8(offset + 1) | |
10947 | ||
10948 | var hasWitnesses = false | |
10949 | if (marker === Transaction.ADVANCED_TRANSACTION_MARKER && | |
10950 | flag === Transaction.ADVANCED_TRANSACTION_FLAG) { | |
10951 | offset += 2 | |
10952 | hasWitnesses = true | |
10953 | } | |
10954 | ||
10955 | var vinLen = readVarInt() | |
10956 | for (var i = 0; i < vinLen; ++i) { | |
10957 | tx.ins.push({ | |
10958 | hash: readSlice(32), | |
10959 | index: readUInt32(), | |
10960 | script: readVarSlice(), | |
10961 | sequence: readUInt32(), | |
10962 | witness: EMPTY_WITNESS | |
10963 | }) | |
10964 | } | |
10965 | ||
10966 | var voutLen = readVarInt() | |
10967 | for (i = 0; i < voutLen; ++i) { | |
10968 | tx.outs.push({ | |
10969 | value: readUInt64(), | |
10970 | script: readVarSlice() | |
10971 | }) | |
10972 | } | |
10973 | ||
10974 | if (hasWitnesses) { | |
10975 | for (i = 0; i < vinLen; ++i) { | |
10976 | tx.ins[i].witness = readVector() | |
10977 | } | |
10978 | ||
10979 | // was this pointless? | |
10980 | if (!tx.hasWitnesses()) throw new Error('Transaction has superfluous witness data') | |
10981 | } | |
10982 | ||
10983 | tx.locktime = readUInt32() | |
10984 | ||
10985 | if (__noStrict) return tx | |
10986 | if (offset !== buffer.length) throw new Error('Transaction has unexpected data') | |
10987 | ||
10988 | return tx | |
10989 | } | |
10990 | ||
10991 | Transaction.fromHex = function (hex) { | |
10992 | return Transaction.fromBuffer(Buffer.from(hex, 'hex')) | |
10993 | } | |
10994 | ||
10995 | Transaction.isCoinbaseHash = function (buffer) { | |
10996 | typeforce(types.Hash256bit, buffer) | |
10997 | for (var i = 0; i < 32; ++i) { | |
10998 | if (buffer[i] !== 0) return false | |
10999 | } | |
11000 | return true | |
11001 | } | |
11002 | ||
11003 | Transaction.prototype.isCoinbase = function () { | |
11004 | return this.ins.length === 1 && Transaction.isCoinbaseHash(this.ins[0].hash) | |
11005 | } | |
11006 | ||
11007 | Transaction.prototype.addInput = function (hash, index, sequence, scriptSig) { | |
11008 | typeforce(types.tuple( | |
11009 | types.Hash256bit, | |
11010 | types.UInt32, | |
11011 | types.maybe(types.UInt32), | |
11012 | types.maybe(types.Buffer) | |
11013 | ), arguments) | |
11014 | ||
11015 | if (types.Null(sequence)) { | |
11016 | sequence = Transaction.DEFAULT_SEQUENCE | |
11017 | } | |
11018 | ||
11019 | // Add the input and return the input's index | |
11020 | return (this.ins.push({ | |
11021 | hash: hash, | |
11022 | index: index, | |
11023 | script: scriptSig || EMPTY_SCRIPT, | |
11024 | sequence: sequence, | |
11025 | witness: EMPTY_WITNESS | |
11026 | }) - 1) | |
11027 | } | |
11028 | ||
11029 | Transaction.prototype.addOutput = function (scriptPubKey, value) { | |
11030 | typeforce(types.tuple(types.Buffer, types.Satoshi), arguments) | |
11031 | ||
11032 | // Add the output and return the output's index | |
11033 | return (this.outs.push({ | |
11034 | script: scriptPubKey, | |
11035 | value: value | |
11036 | }) - 1) | |
11037 | } | |
11038 | ||
11039 | Transaction.prototype.hasWitnesses = function () { | |
11040 | return this.ins.some(function (x) { | |
11041 | return x.witness.length !== 0 | |
11042 | }) | |
11043 | } | |
11044 | ||
11045 | Transaction.prototype.weight = function () { | |
11046 | var base = this.__byteLength(false) | |
11047 | var total = this.__byteLength(true) | |
11048 | return base * 3 + total | |
11049 | } | |
11050 | ||
11051 | Transaction.prototype.virtualSize = function () { | |
11052 | return Math.ceil(this.weight() / 4) | |
11053 | } | |
11054 | ||
11055 | Transaction.prototype.byteLength = function () { | |
11056 | return this.__byteLength(true) | |
11057 | } | |
11058 | ||
11059 | Transaction.prototype.__byteLength = function (__allowWitness) { | |
11060 | var hasWitnesses = __allowWitness && this.hasWitnesses() | |
11061 | ||
11062 | return ( | |
11063 | (hasWitnesses ? 10 : 8) + | |
11064 | varuint.encodingLength(this.ins.length) + | |
11065 | varuint.encodingLength(this.outs.length) + | |
11066 | this.ins.reduce(function (sum, input) { return sum + 40 + varSliceSize(input.script) }, 0) + | |
11067 | this.outs.reduce(function (sum, output) { return sum + 8 + varSliceSize(output.script) }, 0) + | |
11068 | (hasWitnesses ? this.ins.reduce(function (sum, input) { return sum + vectorSize(input.witness) }, 0) : 0) | |
11069 | ) | |
11070 | } | |
11071 | ||
11072 | Transaction.prototype.clone = function () { | |
11073 | var newTx = new Transaction() | |
11074 | newTx.version = this.version | |
11075 | newTx.locktime = this.locktime | |
11076 | ||
11077 | newTx.ins = this.ins.map(function (txIn) { | |
11078 | return { | |
11079 | hash: txIn.hash, | |
11080 | index: txIn.index, | |
11081 | script: txIn.script, | |
11082 | sequence: txIn.sequence, | |
11083 | witness: txIn.witness | |
11084 | } | |
11085 | }) | |
11086 | ||
11087 | newTx.outs = this.outs.map(function (txOut) { | |
11088 | return { | |
11089 | script: txOut.script, | |
11090 | value: txOut.value | |
11091 | } | |
11092 | }) | |
11093 | ||
11094 | return newTx | |
11095 | } | |
11096 | ||
11097 | /** | |
11098 | * Hash transaction for signing a specific input. | |
11099 | * | |
11100 | * Bitcoin uses a different hash for each signed transaction input. | |
11101 | * This method copies the transaction, makes the necessary changes based on the | |
11102 | * hashType, and then hashes the result. | |
11103 | * This hash can then be used to sign the provided transaction input. | |
11104 | */ | |
11105 | Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) { | |
11106 | typeforce(types.tuple(types.UInt32, types.Buffer, /* types.UInt8 */ types.Number), arguments) | |
11107 | ||
11108 | // https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29 | |
11109 | if (inIndex >= this.ins.length) return ONE | |
11110 | ||
11111 | // ignore OP_CODESEPARATOR | |
11112 | var ourScript = bscript.compile(bscript.decompile(prevOutScript).filter(function (x) { | |
11113 | return x !== opcodes.OP_CODESEPARATOR | |
11114 | })) | |
11115 | ||
11116 | var txTmp = this.clone() | |
11117 | ||
11118 | // SIGHASH_NONE: ignore all outputs? (wildcard payee) | |
11119 | if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) { | |
11120 | txTmp.outs = [] | |
11121 | ||
11122 | // ignore sequence numbers (except at inIndex) | |
11123 | txTmp.ins.forEach(function (input, i) { | |
11124 | if (i === inIndex) return | |
11125 | ||
11126 | input.sequence = 0 | |
11127 | }) | |
11128 | ||
11129 | // SIGHASH_SINGLE: ignore all outputs, except at the same index? | |
11130 | } else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) { | |
11131 | // https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60 | |
11132 | if (inIndex >= this.outs.length) return ONE | |
11133 | ||
11134 | // truncate outputs after | |
11135 | txTmp.outs.length = inIndex + 1 | |
11136 | ||
11137 | // "blank" outputs before | |
11138 | for (var i = 0; i < inIndex; i++) { | |
11139 | txTmp.outs[i] = BLANK_OUTPUT | |
11140 | } | |
11141 | ||
11142 | // ignore sequence numbers (except at inIndex) | |
11143 | txTmp.ins.forEach(function (input, y) { | |
11144 | if (y === inIndex) return | |
11145 | ||
11146 | input.sequence = 0 | |
11147 | }) | |
11148 | } | |
11149 | ||
11150 | // SIGHASH_ANYONECANPAY: ignore inputs entirely? | |
11151 | if (hashType & Transaction.SIGHASH_ANYONECANPAY) { | |
11152 | txTmp.ins = [txTmp.ins[inIndex]] | |
11153 | txTmp.ins[0].script = ourScript | |
11154 | ||
11155 | // SIGHASH_ALL: only ignore input scripts | |
11156 | } else { | |
11157 | // "blank" others input scripts | |
11158 | txTmp.ins.forEach(function (input) { input.script = EMPTY_SCRIPT }) | |
11159 | txTmp.ins[inIndex].script = ourScript | |
11160 | } | |
11161 | ||
11162 | // serialize and hash | |
11163 | var buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4) | |
11164 | buffer.writeInt32LE(hashType, buffer.length - 4) | |
11165 | txTmp.__toBuffer(buffer, 0, false) | |
11166 | ||
11167 | return bcrypto.hash256(buffer) | |
11168 | } | |
11169 | ||
11170 | Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value, hashType) { | |
11171 | typeforce(types.tuple(types.UInt32, types.Buffer, types.Satoshi, types.UInt32), arguments) | |
11172 | ||
11173 | var tbuffer, toffset | |
11174 | function writeSlice (slice) { toffset += slice.copy(tbuffer, toffset) } | |
11175 | function writeUInt32 (i) { toffset = tbuffer.writeUInt32LE(i, toffset) } | |
11176 | function writeUInt64 (i) { toffset = bufferutils.writeUInt64LE(tbuffer, i, toffset) } | |
11177 | function writeVarInt (i) { | |
11178 | varuint.encode(i, tbuffer, toffset) | |
11179 | toffset += varuint.encode.bytes | |
11180 | } | |
11181 | function writeVarSlice (slice) { writeVarInt(slice.length); writeSlice(slice) } | |
11182 | ||
11183 | var hashOutputs = ZERO | |
11184 | var hashPrevouts = ZERO | |
11185 | var hashSequence = ZERO | |
11186 | ||
11187 | if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) { | |
11188 | tbuffer = Buffer.allocUnsafe(36 * this.ins.length) | |
11189 | toffset = 0 | |
11190 | ||
11191 | this.ins.forEach(function (txIn) { | |
11192 | writeSlice(txIn.hash) | |
11193 | writeUInt32(txIn.index) | |
11194 | }) | |
11195 | ||
11196 | hashPrevouts = bcrypto.hash256(tbuffer) | |
11197 | } | |
11198 | ||
11199 | if (!(hashType & Transaction.SIGHASH_ANYONECANPAY) && | |
11200 | (hashType & 0x1f) !== Transaction.SIGHASH_SINGLE && | |
11201 | (hashType & 0x1f) !== Transaction.SIGHASH_NONE) { | |
11202 | tbuffer = Buffer.allocUnsafe(4 * this.ins.length) | |
11203 | toffset = 0 | |
11204 | ||
11205 | this.ins.forEach(function (txIn) { | |
11206 | writeUInt32(txIn.sequence) | |
11207 | }) | |
11208 | ||
11209 | hashSequence = bcrypto.hash256(tbuffer) | |
11210 | } | |
11211 | ||
11212 | if ((hashType & 0x1f) !== Transaction.SIGHASH_SINGLE && | |
11213 | (hashType & 0x1f) !== Transaction.SIGHASH_NONE) { | |
11214 | var txOutsSize = this.outs.reduce(function (sum, output) { | |
11215 | return sum + 8 + varSliceSize(output.script) | |
11216 | }, 0) | |
11217 | ||
11218 | tbuffer = Buffer.allocUnsafe(txOutsSize) | |
11219 | toffset = 0 | |
11220 | ||
11221 | this.outs.forEach(function (out) { | |
11222 | writeUInt64(out.value) | |
11223 | writeVarSlice(out.script) | |
11224 | }) | |
11225 | ||
11226 | hashOutputs = bcrypto.hash256(tbuffer) | |
11227 | } else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) { | |
11228 | var output = this.outs[inIndex] | |
11229 | ||
11230 | tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script)) | |
11231 | toffset = 0 | |
11232 | writeUInt64(output.value) | |
11233 | writeVarSlice(output.script) | |
11234 | ||
11235 | hashOutputs = bcrypto.hash256(tbuffer) | |
11236 | } | |
11237 | ||
11238 | tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript)) | |
11239 | toffset = 0 | |
11240 | ||
11241 | var input = this.ins[inIndex] | |
11242 | writeUInt32(this.version) | |
11243 | writeSlice(hashPrevouts) | |
11244 | writeSlice(hashSequence) | |
11245 | writeSlice(input.hash) | |
11246 | writeUInt32(input.index) | |
11247 | writeVarSlice(prevOutScript) | |
11248 | writeUInt64(value) | |
11249 | writeUInt32(input.sequence) | |
11250 | writeSlice(hashOutputs) | |
11251 | writeUInt32(this.locktime) | |
11252 | writeUInt32(hashType) | |
11253 | return bcrypto.hash256(tbuffer) | |
11254 | } | |
11255 | ||
11256 | Transaction.prototype.getHash = function () { | |
11257 | return bcrypto.hash256(this.__toBuffer(undefined, undefined, false)) | |
11258 | } | |
11259 | ||
11260 | Transaction.prototype.getId = function () { | |
11261 | // transaction hash's are displayed in reverse order | |
11262 | return this.getHash().reverse().toString('hex') | |
11263 | } | |
11264 | ||
11265 | Transaction.prototype.toBuffer = function (buffer, initialOffset) { | |
11266 | return this.__toBuffer(buffer, initialOffset, true) | |
11267 | } | |
11268 | ||
11269 | Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitness) { | |
11270 | if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(__allowWitness)) | |
11271 | ||
11272 | var offset = initialOffset || 0 | |
11273 | function writeSlice (slice) { offset += slice.copy(buffer, offset) } | |
11274 | function writeUInt8 (i) { offset = buffer.writeUInt8(i, offset) } | |
11275 | function writeUInt32 (i) { offset = buffer.writeUInt32LE(i, offset) } | |
11276 | function writeInt32 (i) { offset = buffer.writeInt32LE(i, offset) } | |
11277 | function writeUInt64 (i) { offset = bufferutils.writeUInt64LE(buffer, i, offset) } | |
11278 | function writeVarInt (i) { | |
11279 | varuint.encode(i, buffer, offset) | |
11280 | offset += varuint.encode.bytes | |
11281 | } | |
11282 | function writeVarSlice (slice) { writeVarInt(slice.length); writeSlice(slice) } | |
11283 | function writeVector (vector) { writeVarInt(vector.length); vector.forEach(writeVarSlice) } | |
11284 | ||
11285 | writeInt32(this.version) | |
11286 | ||
11287 | var hasWitnesses = __allowWitness && this.hasWitnesses() | |
11288 | ||
11289 | if (hasWitnesses) { | |
11290 | writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER) | |
11291 | writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG) | |
11292 | } | |
11293 | ||
11294 | writeVarInt(this.ins.length) | |
11295 | ||
11296 | this.ins.forEach(function (txIn) { | |
11297 | writeSlice(txIn.hash) | |
11298 | writeUInt32(txIn.index) | |
11299 | writeVarSlice(txIn.script) | |
11300 | writeUInt32(txIn.sequence) | |
11301 | }) | |
11302 | ||
11303 | writeVarInt(this.outs.length) | |
11304 | this.outs.forEach(function (txOut) { | |
11305 | if (!txOut.valueBuffer) { | |
11306 | writeUInt64(txOut.value) | |
11307 | } else { | |
11308 | writeSlice(txOut.valueBuffer) | |
11309 | } | |
11310 | ||
11311 | writeVarSlice(txOut.script) | |
11312 | }) | |
11313 | ||
11314 | if (hasWitnesses) { | |
11315 | this.ins.forEach(function (input) { | |
11316 | writeVector(input.witness) | |
11317 | }) | |
11318 | } | |
11319 | ||
11320 | writeUInt32(this.locktime) | |
11321 | ||
11322 | // avoid slicing unless necessary | |
11323 | if (initialOffset !== undefined) return buffer.slice(initialOffset, offset) | |
11324 | return buffer | |
11325 | } | |
11326 | ||
11327 | Transaction.prototype.toHex = function () { | |
11328 | return this.toBuffer().toString('hex') | |
11329 | } | |
11330 | ||
11331 | Transaction.prototype.setInputScript = function (index, scriptSig) { | |
11332 | typeforce(types.tuple(types.Number, types.Buffer), arguments) | |
11333 | ||
11334 | this.ins[index].script = scriptSig | |
11335 | } | |
11336 | ||
11337 | Transaction.prototype.setWitness = function (index, witness) { | |
11338 | typeforce(types.tuple(types.Number, [types.Buffer]), arguments) | |
11339 | ||
11340 | this.ins[index].witness = witness | |
11341 | } | |
11342 | ||
11343 | module.exports = Transaction | |
11344 | ||
9f59e99b | 11345 | },{"./bufferutils":46,"./crypto":47,"./script":54,"./types":80,"bitcoin-ops":42,"safe-buffer":128,"typeforce":139,"varuint-bitcoin":141}],79:[function(require,module,exports){ |
a0091a40 IC |
11346 | var Buffer = require('safe-buffer').Buffer |
11347 | var baddress = require('./address') | |
11348 | var bcrypto = require('./crypto') | |
11349 | var bscript = require('./script') | |
9f59e99b | 11350 | var btemplates = require('./templates') |
a0091a40 IC |
11351 | var networks = require('./networks') |
11352 | var ops = require('bitcoin-ops') | |
11353 | var typeforce = require('typeforce') | |
11354 | var types = require('./types') | |
9f59e99b IC |
11355 | var scriptTypes = btemplates.types |
11356 | var SIGNABLE = [btemplates.types.P2PKH, btemplates.types.P2PK, btemplates.types.MULTISIG] | |
11357 | var P2SH = SIGNABLE.concat([btemplates.types.P2WPKH, btemplates.types.P2WSH]) | |
a0091a40 IC |
11358 | |
11359 | var ECPair = require('./ecpair') | |
11360 | var ECSignature = require('./ecsignature') | |
11361 | var Transaction = require('./transaction') | |
11362 | ||
9f59e99b IC |
11363 | function supportedType (type) { |
11364 | return SIGNABLE.indexOf(type) !== -1 | |
11365 | } | |
11366 | ||
11367 | function supportedP2SHType (type) { | |
11368 | return P2SH.indexOf(type) !== -1 | |
11369 | } | |
11370 | ||
a0091a40 IC |
11371 | function extractChunks (type, chunks, script) { |
11372 | var pubKeys = [] | |
11373 | var signatures = [] | |
11374 | switch (type) { | |
11375 | case scriptTypes.P2PKH: | |
11376 | // if (redeemScript) throw new Error('Nonstandard... P2SH(P2PKH)') | |
11377 | pubKeys = chunks.slice(1) | |
11378 | signatures = chunks.slice(0, 1) | |
11379 | break | |
11380 | ||
11381 | case scriptTypes.P2PK: | |
9f59e99b | 11382 | pubKeys[0] = script ? btemplates.pubKey.output.decode(script) : undefined |
a0091a40 IC |
11383 | signatures = chunks.slice(0, 1) |
11384 | break | |
11385 | ||
11386 | case scriptTypes.MULTISIG: | |
11387 | if (script) { | |
9f59e99b | 11388 | var multisig = btemplates.multisig.output.decode(script) |
a0091a40 IC |
11389 | pubKeys = multisig.pubKeys |
11390 | } | |
11391 | ||
11392 | signatures = chunks.slice(1).map(function (chunk) { | |
11393 | return chunk.length === 0 ? undefined : chunk | |
11394 | }) | |
11395 | break | |
11396 | } | |
11397 | ||
11398 | return { | |
11399 | pubKeys: pubKeys, | |
11400 | signatures: signatures | |
11401 | } | |
11402 | } | |
11403 | function expandInput (scriptSig, witnessStack) { | |
11404 | if (scriptSig.length === 0 && witnessStack.length === 0) return {} | |
11405 | ||
11406 | var prevOutScript | |
11407 | var prevOutType | |
11408 | var scriptType | |
11409 | var script | |
11410 | var redeemScript | |
11411 | var witnessScript | |
11412 | var witnessScriptType | |
11413 | var redeemScriptType | |
11414 | var witness = false | |
11415 | var p2wsh = false | |
11416 | var p2sh = false | |
11417 | var witnessProgram | |
11418 | var chunks | |
11419 | ||
11420 | var scriptSigChunks = bscript.decompile(scriptSig) | |
9f59e99b | 11421 | var sigType = btemplates.classifyInput(scriptSigChunks, true) |
a0091a40 IC |
11422 | if (sigType === scriptTypes.P2SH) { |
11423 | p2sh = true | |
11424 | redeemScript = scriptSigChunks[scriptSigChunks.length - 1] | |
9f59e99b IC |
11425 | redeemScriptType = btemplates.classifyOutput(redeemScript) |
11426 | prevOutScript = btemplates.scriptHash.output.encode(bcrypto.hash160(redeemScript)) | |
a0091a40 IC |
11427 | prevOutType = scriptTypes.P2SH |
11428 | script = redeemScript | |
11429 | } | |
11430 | ||
9f59e99b | 11431 | var classifyWitness = btemplates.classifyWitness(witnessStack, true) |
a0091a40 IC |
11432 | if (classifyWitness === scriptTypes.P2WSH) { |
11433 | witnessScript = witnessStack[witnessStack.length - 1] | |
9f59e99b | 11434 | witnessScriptType = btemplates.classifyOutput(witnessScript) |
a0091a40 | 11435 | p2wsh = true |
9f59e99b | 11436 | witness = true |
a0091a40 | 11437 | if (scriptSig.length === 0) { |
9f59e99b | 11438 | prevOutScript = btemplates.witnessScriptHash.output.encode(bcrypto.sha256(witnessScript)) |
a0091a40 | 11439 | prevOutType = scriptTypes.P2WSH |
9f59e99b | 11440 | if (redeemScript !== undefined) { |
a0091a40 IC |
11441 | throw new Error('Redeem script given when unnecessary') |
11442 | } | |
11443 | // bare witness | |
11444 | } else { | |
11445 | if (!redeemScript) { | |
11446 | throw new Error('No redeemScript provided for P2WSH, but scriptSig non-empty') | |
11447 | } | |
9f59e99b | 11448 | witnessProgram = btemplates.witnessScriptHash.output.encode(bcrypto.sha256(witnessScript)) |
a0091a40 IC |
11449 | if (!redeemScript.equals(witnessProgram)) { |
11450 | throw new Error('Redeem script didn\'t match witnessScript') | |
11451 | } | |
11452 | } | |
11453 | ||
9f59e99b | 11454 | if (!supportedType(btemplates.classifyOutput(witnessScript))) { |
a0091a40 IC |
11455 | throw new Error('unsupported witness script') |
11456 | } | |
9f59e99b | 11457 | |
a0091a40 IC |
11458 | script = witnessScript |
11459 | scriptType = witnessScriptType | |
11460 | chunks = witnessStack.slice(0, -1) | |
11461 | } else if (classifyWitness === scriptTypes.P2WPKH) { | |
9f59e99b | 11462 | witness = true |
a0091a40 IC |
11463 | var key = witnessStack[witnessStack.length - 1] |
11464 | var keyHash = bcrypto.hash160(key) | |
11465 | if (scriptSig.length === 0) { | |
9f59e99b | 11466 | prevOutScript = btemplates.witnessPubKeyHash.output.encode(keyHash) |
a0091a40 IC |
11467 | prevOutType = scriptTypes.P2WPKH |
11468 | if (typeof redeemScript !== 'undefined') { | |
11469 | throw new Error('Redeem script given when unnecessary') | |
11470 | } | |
11471 | } else { | |
11472 | if (!redeemScript) { | |
11473 | throw new Error('No redeemScript provided for P2WPKH, but scriptSig wasn\'t empty') | |
11474 | } | |
9f59e99b | 11475 | witnessProgram = btemplates.witnessPubKeyHash.output.encode(keyHash) |
a0091a40 IC |
11476 | if (!redeemScript.equals(witnessProgram)) { |
11477 | throw new Error('Redeem script did not have the right witness program') | |
11478 | } | |
11479 | } | |
11480 | ||
11481 | scriptType = scriptTypes.P2PKH | |
11482 | chunks = witnessStack | |
11483 | } else if (redeemScript) { | |
9f59e99b | 11484 | if (!supportedP2SHType(redeemScriptType)) { |
a0091a40 IC |
11485 | throw new Error('Bad redeemscript!') |
11486 | } | |
11487 | ||
11488 | script = redeemScript | |
11489 | scriptType = redeemScriptType | |
11490 | chunks = scriptSigChunks.slice(0, -1) | |
11491 | } else { | |
9f59e99b | 11492 | prevOutType = scriptType = btemplates.classifyInput(scriptSig) |
a0091a40 IC |
11493 | chunks = scriptSigChunks |
11494 | } | |
11495 | ||
11496 | var expanded = extractChunks(scriptType, chunks, script) | |
11497 | ||
11498 | var result = { | |
11499 | pubKeys: expanded.pubKeys, | |
11500 | signatures: expanded.signatures, | |
11501 | prevOutScript: prevOutScript, | |
11502 | prevOutType: prevOutType, | |
11503 | signType: scriptType, | |
11504 | signScript: script, | |
11505 | witness: Boolean(witness) | |
11506 | } | |
11507 | ||
11508 | if (p2sh) { | |
11509 | result.redeemScript = redeemScript | |
11510 | result.redeemScriptType = redeemScriptType | |
11511 | } | |
11512 | ||
11513 | if (p2wsh) { | |
11514 | result.witnessScript = witnessScript | |
11515 | result.witnessScriptType = witnessScriptType | |
11516 | } | |
11517 | ||
11518 | return result | |
11519 | } | |
11520 | ||
11521 | // could be done in expandInput, but requires the original Transaction for hashForSignature | |
11522 | function fixMultisigOrder (input, transaction, vin) { | |
11523 | if (input.redeemScriptType !== scriptTypes.MULTISIG || !input.redeemScript) return | |
11524 | if (input.pubKeys.length === input.signatures.length) return | |
11525 | ||
11526 | var unmatched = input.signatures.concat() | |
11527 | ||
11528 | input.signatures = input.pubKeys.map(function (pubKey) { | |
11529 | var keyPair = ECPair.fromPublicKeyBuffer(pubKey) | |
11530 | var match | |
11531 | ||
11532 | // check for a signature | |
11533 | unmatched.some(function (signature, i) { | |
11534 | // skip if undefined || OP_0 | |
11535 | if (!signature) return false | |
11536 | ||
11537 | // TODO: avoid O(n) hashForSignature | |
11538 | var parsed = ECSignature.parseScriptSignature(signature) | |
11539 | var hash = transaction.hashForSignature(vin, input.redeemScript, parsed.hashType) | |
11540 | ||
11541 | // skip if signature does not match pubKey | |
11542 | if (!keyPair.verify(hash, parsed.signature)) return false | |
11543 | ||
11544 | // remove matched signature from unmatched | |
11545 | unmatched[i] = undefined | |
11546 | match = signature | |
11547 | ||
11548 | return true | |
11549 | }) | |
11550 | ||
11551 | return match | |
11552 | }) | |
11553 | } | |
11554 | ||
11555 | function expandOutput (script, scriptType, ourPubKey) { | |
11556 | typeforce(types.Buffer, script) | |
11557 | ||
11558 | var scriptChunks = bscript.decompile(script) | |
11559 | if (!scriptType) { | |
9f59e99b | 11560 | scriptType = btemplates.classifyOutput(script) |
a0091a40 IC |
11561 | } |
11562 | ||
11563 | var pubKeys = [] | |
11564 | ||
11565 | switch (scriptType) { | |
11566 | // does our hash160(pubKey) match the output scripts? | |
11567 | case scriptTypes.P2PKH: | |
11568 | if (!ourPubKey) break | |
11569 | ||
11570 | var pkh1 = scriptChunks[2] | |
11571 | var pkh2 = bcrypto.hash160(ourPubKey) | |
11572 | if (pkh1.equals(pkh2)) pubKeys = [ourPubKey] | |
11573 | break | |
11574 | ||
11575 | // does our hash160(pubKey) match the output scripts? | |
11576 | case scriptTypes.P2WPKH: | |
11577 | if (!ourPubKey) break | |
11578 | ||
11579 | var wpkh1 = scriptChunks[1] | |
11580 | var wpkh2 = bcrypto.hash160(ourPubKey) | |
11581 | if (wpkh1.equals(wpkh2)) pubKeys = [ourPubKey] | |
11582 | break | |
11583 | ||
11584 | case scriptTypes.P2PK: | |
11585 | pubKeys = scriptChunks.slice(0, 1) | |
11586 | break | |
11587 | ||
11588 | case scriptTypes.MULTISIG: | |
11589 | pubKeys = scriptChunks.slice(1, -2) | |
11590 | break | |
11591 | ||
11592 | default: return { scriptType: scriptType } | |
11593 | } | |
11594 | ||
11595 | return { | |
11596 | pubKeys: pubKeys, | |
11597 | scriptType: scriptType, | |
11598 | signatures: pubKeys.map(function () { return undefined }) | |
11599 | } | |
11600 | } | |
11601 | ||
9f59e99b | 11602 | function checkP2SHInput (input, redeemScriptHash) { |
a0091a40 IC |
11603 | if (input.prevOutType) { |
11604 | if (input.prevOutType !== scriptTypes.P2SH) throw new Error('PrevOutScript must be P2SH') | |
11605 | ||
11606 | var prevOutScriptScriptHash = bscript.decompile(input.prevOutScript)[1] | |
11607 | if (!prevOutScriptScriptHash.equals(redeemScriptHash)) throw new Error('Inconsistent hash160(RedeemScript)') | |
11608 | } | |
11609 | } | |
11610 | ||
11611 | function checkP2WSHInput (input, witnessScriptHash) { | |
11612 | if (input.prevOutType) { | |
11613 | if (input.prevOutType !== scriptTypes.P2WSH) throw new Error('PrevOutScript must be P2WSH') | |
11614 | ||
11615 | var scriptHash = bscript.decompile(input.prevOutScript)[1] | |
11616 | if (!scriptHash.equals(witnessScriptHash)) throw new Error('Inconsistent sha25(WitnessScript)') | |
11617 | } | |
11618 | } | |
11619 | ||
11620 | function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScript) { | |
11621 | var expanded | |
11622 | var prevOutType | |
11623 | var prevOutScript | |
11624 | ||
11625 | var p2sh = false | |
11626 | var p2shType | |
11627 | var redeemScriptHash | |
11628 | ||
11629 | var witness = false | |
11630 | var p2wsh = false | |
11631 | var witnessType | |
11632 | var witnessScriptHash | |
11633 | ||
11634 | var signType | |
11635 | var signScript | |
11636 | ||
11637 | if (redeemScript && witnessScript) { | |
11638 | redeemScriptHash = bcrypto.hash160(redeemScript) | |
11639 | witnessScriptHash = bcrypto.sha256(witnessScript) | |
9f59e99b | 11640 | checkP2SHInput(input, redeemScriptHash) |
a0091a40 | 11641 | |
9f59e99b | 11642 | if (!redeemScript.equals(btemplates.witnessScriptHash.output.encode(witnessScriptHash))) throw new Error('Witness script inconsistent with redeem script') |
a0091a40 IC |
11643 | |
11644 | expanded = expandOutput(witnessScript, undefined, kpPubKey) | |
11645 | if (!expanded.pubKeys) throw new Error('WitnessScript not supported "' + bscript.toASM(redeemScript) + '"') | |
9f59e99b IC |
11646 | |
11647 | prevOutType = btemplates.types.P2SH | |
11648 | prevOutScript = btemplates.scriptHash.output.encode(redeemScriptHash) | |
a0091a40 | 11649 | p2sh = witness = p2wsh = true |
9f59e99b | 11650 | p2shType = btemplates.types.P2WSH |
a0091a40 IC |
11651 | signType = witnessType = expanded.scriptType |
11652 | signScript = witnessScript | |
11653 | } else if (redeemScript) { | |
11654 | redeemScriptHash = bcrypto.hash160(redeemScript) | |
9f59e99b | 11655 | checkP2SHInput(input, redeemScriptHash) |
a0091a40 IC |
11656 | |
11657 | expanded = expandOutput(redeemScript, undefined, kpPubKey) | |
11658 | if (!expanded.pubKeys) throw new Error('RedeemScript not supported "' + bscript.toASM(redeemScript) + '"') | |
11659 | ||
9f59e99b IC |
11660 | prevOutType = btemplates.types.P2SH |
11661 | prevOutScript = btemplates.scriptHash.output.encode(redeemScriptHash) | |
a0091a40 IC |
11662 | p2sh = true |
11663 | signType = p2shType = expanded.scriptType | |
11664 | signScript = redeemScript | |
9f59e99b | 11665 | witness = signType === btemplates.types.P2WPKH |
a0091a40 IC |
11666 | } else if (witnessScript) { |
11667 | witnessScriptHash = bcrypto.sha256(witnessScript) | |
11668 | checkP2WSHInput(input, witnessScriptHash) | |
11669 | ||
11670 | expanded = expandOutput(witnessScript, undefined, kpPubKey) | |
11671 | if (!expanded.pubKeys) throw new Error('WitnessScript not supported "' + bscript.toASM(redeemScript) + '"') | |
11672 | ||
9f59e99b IC |
11673 | prevOutType = btemplates.types.P2WSH |
11674 | prevOutScript = btemplates.witnessScriptHash.output.encode(witnessScriptHash) | |
a0091a40 IC |
11675 | witness = p2wsh = true |
11676 | signType = witnessType = expanded.scriptType | |
11677 | signScript = witnessScript | |
11678 | } else if (input.prevOutType) { | |
11679 | // embedded scripts are not possible without a redeemScript | |
11680 | if (input.prevOutType === scriptTypes.P2SH || | |
11681 | input.prevOutType === scriptTypes.P2WSH) { | |
11682 | throw new Error('PrevOutScript is ' + input.prevOutType + ', requires redeemScript') | |
11683 | } | |
11684 | ||
11685 | prevOutType = input.prevOutType | |
11686 | prevOutScript = input.prevOutScript | |
11687 | expanded = expandOutput(input.prevOutScript, input.prevOutType, kpPubKey) | |
11688 | if (!expanded.pubKeys) return | |
11689 | ||
11690 | witness = (input.prevOutType === scriptTypes.P2WPKH) | |
11691 | signType = prevOutType | |
11692 | signScript = prevOutScript | |
11693 | } else { | |
9f59e99b | 11694 | prevOutScript = btemplates.pubKeyHash.output.encode(bcrypto.hash160(kpPubKey)) |
a0091a40 | 11695 | expanded = expandOutput(prevOutScript, scriptTypes.P2PKH, kpPubKey) |
9f59e99b | 11696 | |
a0091a40 IC |
11697 | prevOutType = scriptTypes.P2PKH |
11698 | witness = false | |
11699 | signType = prevOutType | |
11700 | signScript = prevOutScript | |
11701 | } | |
11702 | ||
a0091a40 | 11703 | if (signType === scriptTypes.P2WPKH) { |
9f59e99b | 11704 | signScript = btemplates.pubKeyHash.output.encode(btemplates.witnessPubKeyHash.output.decode(signScript)) |
a0091a40 IC |
11705 | } |
11706 | ||
11707 | if (p2sh) { | |
11708 | input.redeemScript = redeemScript | |
11709 | input.redeemScriptType = p2shType | |
11710 | } | |
11711 | ||
11712 | if (p2wsh) { | |
11713 | input.witnessScript = witnessScript | |
11714 | input.witnessScriptType = witnessType | |
11715 | } | |
11716 | ||
11717 | input.pubKeys = expanded.pubKeys | |
11718 | input.signatures = expanded.signatures | |
11719 | input.signScript = signScript | |
11720 | input.signType = signType | |
11721 | input.prevOutScript = prevOutScript | |
11722 | input.prevOutType = prevOutType | |
11723 | input.witness = witness | |
11724 | } | |
11725 | ||
11726 | function buildStack (type, signatures, pubKeys, allowIncomplete) { | |
11727 | if (type === scriptTypes.P2PKH) { | |
9f59e99b | 11728 | if (signatures.length === 1 && Buffer.isBuffer(signatures[0]) && pubKeys.length === 1) return btemplates.pubKeyHash.input.encodeStack(signatures[0], pubKeys[0]) |
a0091a40 | 11729 | } else if (type === scriptTypes.P2PK) { |
9f59e99b | 11730 | if (signatures.length === 1 && Buffer.isBuffer(signatures[0])) return btemplates.pubKey.input.encodeStack(signatures[0]) |
a0091a40 IC |
11731 | } else if (type === scriptTypes.MULTISIG) { |
11732 | if (signatures.length > 0) { | |
11733 | signatures = signatures.map(function (signature) { | |
11734 | return signature || ops.OP_0 | |
11735 | }) | |
11736 | if (!allowIncomplete) { | |
11737 | // remove blank signatures | |
11738 | signatures = signatures.filter(function (x) { return x !== ops.OP_0 }) | |
11739 | } | |
11740 | ||
9f59e99b | 11741 | return btemplates.multisig.input.encodeStack(signatures) |
a0091a40 IC |
11742 | } |
11743 | } else { | |
11744 | throw new Error('Not yet supported') | |
11745 | } | |
11746 | ||
11747 | if (!allowIncomplete) throw new Error('Not enough signatures provided') | |
a0091a40 IC |
11748 | return [] |
11749 | } | |
11750 | ||
11751 | function buildInput (input, allowIncomplete) { | |
11752 | var scriptType = input.prevOutType | |
11753 | var sig = [] | |
11754 | var witness = [] | |
9f59e99b IC |
11755 | |
11756 | if (supportedType(scriptType)) { | |
a0091a40 IC |
11757 | sig = buildStack(scriptType, input.signatures, input.pubKeys, allowIncomplete) |
11758 | } | |
11759 | ||
11760 | var p2sh = false | |
9f59e99b | 11761 | if (scriptType === btemplates.types.P2SH) { |
a0091a40 IC |
11762 | // We can remove this error later when we have a guarantee prepareInput |
11763 | // rejects unsignable scripts - it MUST be signable at this point. | |
9f59e99b | 11764 | if (!allowIncomplete && !supportedP2SHType(input.redeemScriptType)) { |
a0091a40 IC |
11765 | throw new Error('Impossible to sign this type') |
11766 | } | |
9f59e99b IC |
11767 | |
11768 | if (supportedType(input.redeemScriptType)) { | |
a0091a40 IC |
11769 | sig = buildStack(input.redeemScriptType, input.signatures, input.pubKeys, allowIncomplete) |
11770 | } | |
9f59e99b | 11771 | |
a0091a40 | 11772 | // If it wasn't SIGNABLE, it's witness, defer to that |
9f59e99b IC |
11773 | if (input.redeemScriptType) { |
11774 | p2sh = true | |
11775 | scriptType = input.redeemScriptType | |
11776 | } | |
a0091a40 IC |
11777 | } |
11778 | ||
9f59e99b | 11779 | switch (scriptType) { |
a0091a40 | 11780 | // P2WPKH is a special case of P2PKH |
9f59e99b IC |
11781 | case btemplates.types.P2WPKH: |
11782 | witness = buildStack(btemplates.types.P2PKH, input.signatures, input.pubKeys, allowIncomplete) | |
11783 | break | |
a0091a40 | 11784 | |
9f59e99b IC |
11785 | case btemplates.types.P2WSH: |
11786 | // We can remove this check later | |
11787 | if (!allowIncomplete && !supportedType(input.witnessScriptType)) { | |
11788 | throw new Error('Impossible to sign this type') | |
11789 | } | |
11790 | ||
11791 | if (supportedType(input.witnessScriptType)) { | |
11792 | witness = buildStack(input.witnessScriptType, input.signatures, input.pubKeys, allowIncomplete) | |
11793 | witness.push(input.witnessScript) | |
11794 | scriptType = input.witnessScriptType | |
11795 | } | |
11796 | ||
11797 | break | |
a0091a40 IC |
11798 | } |
11799 | ||
11800 | // append redeemScript if necessary | |
11801 | if (p2sh) { | |
11802 | sig.push(input.redeemScript) | |
11803 | } | |
11804 | ||
11805 | return { | |
11806 | type: scriptType, | |
11807 | script: bscript.compile(sig), | |
11808 | witness: witness | |
11809 | } | |
11810 | } | |
11811 | ||
11812 | function TransactionBuilder (network, maximumFeeRate) { | |
11813 | this.prevTxMap = {} | |
11814 | this.network = network || networks.bitcoin | |
11815 | ||
11816 | // WARNING: This is __NOT__ to be relied on, its just another potential safety mechanism (safety in-depth) | |
11817 | this.maximumFeeRate = maximumFeeRate || 1000 | |
11818 | ||
11819 | this.inputs = [] | |
11820 | this.tx = new Transaction() | |
11821 | } | |
11822 | ||
11823 | TransactionBuilder.prototype.setLockTime = function (locktime) { | |
11824 | typeforce(types.UInt32, locktime) | |
11825 | ||
11826 | // if any signatures exist, throw | |
11827 | if (this.inputs.some(function (input) { | |
11828 | if (!input.signatures) return false | |
11829 | ||
11830 | return input.signatures.some(function (s) { return s }) | |
11831 | })) { | |
11832 | throw new Error('No, this would invalidate signatures') | |
11833 | } | |
11834 | ||
11835 | this.tx.locktime = locktime | |
11836 | } | |
11837 | ||
11838 | TransactionBuilder.prototype.setVersion = function (version) { | |
11839 | typeforce(types.UInt32, version) | |
11840 | ||
11841 | // XXX: this might eventually become more complex depending on what the versions represent | |
11842 | this.tx.version = version | |
11843 | } | |
11844 | ||
11845 | TransactionBuilder.fromTransaction = function (transaction, network) { | |
11846 | var txb = new TransactionBuilder(network) | |
11847 | ||
11848 | // Copy transaction fields | |
11849 | txb.setVersion(transaction.version) | |
11850 | txb.setLockTime(transaction.locktime) | |
11851 | ||
11852 | // Copy outputs (done first to avoid signature invalidation) | |
11853 | transaction.outs.forEach(function (txOut) { | |
11854 | txb.addOutput(txOut.script, txOut.value) | |
11855 | }) | |
11856 | ||
11857 | // Copy inputs | |
11858 | transaction.ins.forEach(function (txIn) { | |
11859 | txb.__addInputUnsafe(txIn.hash, txIn.index, { | |
11860 | sequence: txIn.sequence, | |
11861 | script: txIn.script, | |
11862 | witness: txIn.witness | |
11863 | }) | |
11864 | }) | |
11865 | ||
11866 | // fix some things not possible through the public API | |
11867 | txb.inputs.forEach(function (input, i) { | |
11868 | fixMultisigOrder(input, transaction, i) | |
11869 | }) | |
11870 | ||
11871 | return txb | |
11872 | } | |
11873 | ||
11874 | TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOutScript) { | |
11875 | if (!this.__canModifyInputs()) { | |
11876 | throw new Error('No, this would invalidate signatures') | |
11877 | } | |
11878 | ||
11879 | var value | |
11880 | ||
11881 | // is it a hex string? | |
11882 | if (typeof txHash === 'string') { | |
11883 | // transaction hashs's are displayed in reverse order, un-reverse it | |
11884 | txHash = Buffer.from(txHash, 'hex').reverse() | |
11885 | ||
11886 | // is it a Transaction object? | |
11887 | } else if (txHash instanceof Transaction) { | |
11888 | var txOut = txHash.outs[vout] | |
11889 | prevOutScript = txOut.script | |
11890 | value = txOut.value | |
11891 | ||
11892 | txHash = txHash.getHash() | |
11893 | } | |
11894 | ||
11895 | return this.__addInputUnsafe(txHash, vout, { | |
11896 | sequence: sequence, | |
11897 | prevOutScript: prevOutScript, | |
11898 | value: value | |
11899 | }) | |
11900 | } | |
11901 | ||
11902 | TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, options) { | |
11903 | if (Transaction.isCoinbaseHash(txHash)) { | |
11904 | throw new Error('coinbase inputs not supported') | |
11905 | } | |
11906 | ||
11907 | var prevTxOut = txHash.toString('hex') + ':' + vout | |
11908 | if (this.prevTxMap[prevTxOut] !== undefined) throw new Error('Duplicate TxOut: ' + prevTxOut) | |
11909 | ||
11910 | var input = {} | |
11911 | ||
11912 | // derive what we can from the scriptSig | |
11913 | if (options.script !== undefined) { | |
11914 | input = expandInput(options.script, options.witness || []) | |
11915 | } | |
11916 | ||
11917 | // if an input value was given, retain it | |
11918 | if (options.value !== undefined) { | |
11919 | input.value = options.value | |
11920 | } | |
11921 | ||
11922 | // derive what we can from the previous transactions output script | |
11923 | if (!input.prevOutScript && options.prevOutScript) { | |
11924 | var prevOutType | |
11925 | ||
11926 | if (!input.pubKeys && !input.signatures) { | |
11927 | var expanded = expandOutput(options.prevOutScript) | |
11928 | ||
11929 | if (expanded.pubKeys) { | |
11930 | input.pubKeys = expanded.pubKeys | |
11931 | input.signatures = expanded.signatures | |
11932 | } | |
11933 | ||
11934 | prevOutType = expanded.scriptType | |
11935 | } | |
11936 | ||
11937 | input.prevOutScript = options.prevOutScript | |
9f59e99b | 11938 | input.prevOutType = prevOutType || btemplates.classifyOutput(options.prevOutScript) |
a0091a40 IC |
11939 | } |
11940 | ||
11941 | var vin = this.tx.addInput(txHash, vout, options.sequence, options.scriptSig) | |
11942 | this.inputs[vin] = input | |
11943 | this.prevTxMap[prevTxOut] = vin | |
a0091a40 IC |
11944 | return vin |
11945 | } | |
11946 | ||
11947 | TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) { | |
11948 | if (!this.__canModifyOutputs()) { | |
11949 | throw new Error('No, this would invalidate signatures') | |
11950 | } | |
11951 | ||
11952 | // Attempt to get a script if it's a base58 address string | |
11953 | if (typeof scriptPubKey === 'string') { | |
11954 | scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network) | |
11955 | } | |
11956 | ||
11957 | return this.tx.addOutput(scriptPubKey, value) | |
11958 | } | |
11959 | ||
11960 | TransactionBuilder.prototype.build = function () { | |
11961 | return this.__build(false) | |
11962 | } | |
11963 | TransactionBuilder.prototype.buildIncomplete = function () { | |
11964 | return this.__build(true) | |
11965 | } | |
11966 | ||
11967 | TransactionBuilder.prototype.__build = function (allowIncomplete) { | |
11968 | if (!allowIncomplete) { | |
11969 | if (!this.tx.ins.length) throw new Error('Transaction has no inputs') | |
11970 | if (!this.tx.outs.length) throw new Error('Transaction has no outputs') | |
11971 | } | |
11972 | ||
11973 | var tx = this.tx.clone() | |
11974 | // Create script signatures from inputs | |
11975 | this.inputs.forEach(function (input, i) { | |
11976 | var scriptType = input.witnessScriptType || input.redeemScriptType || input.prevOutType | |
11977 | if (!scriptType && !allowIncomplete) throw new Error('Transaction is not complete') | |
11978 | var result = buildInput(input, allowIncomplete) | |
11979 | ||
11980 | // skip if no result | |
11981 | if (!allowIncomplete) { | |
9f59e99b | 11982 | if (!supportedType(result.type) && result.type !== btemplates.types.P2WPKH) { |
a0091a40 IC |
11983 | throw new Error(result.type + ' not supported') |
11984 | } | |
11985 | } | |
11986 | ||
11987 | tx.setInputScript(i, result.script) | |
11988 | tx.setWitness(i, result.witness) | |
11989 | }) | |
11990 | ||
11991 | if (!allowIncomplete) { | |
11992 | // do not rely on this, its merely a last resort | |
9f59e99b | 11993 | if (this.__overMaximumFees(tx.virtualSize())) { |
a0091a40 IC |
11994 | throw new Error('Transaction has absurd fees') |
11995 | } | |
11996 | } | |
11997 | ||
11998 | return tx | |
11999 | } | |
12000 | ||
12001 | function canSign (input) { | |
12002 | return input.prevOutScript !== undefined && | |
12003 | input.signScript !== undefined && | |
12004 | input.pubKeys !== undefined && | |
12005 | input.signatures !== undefined && | |
12006 | input.signatures.length === input.pubKeys.length && | |
12007 | input.pubKeys.length > 0 && | |
9f59e99b IC |
12008 | ( |
12009 | input.witness === false || | |
12010 | (input.witness === true && input.value !== undefined) | |
12011 | ) | |
a0091a40 IC |
12012 | } |
12013 | ||
12014 | TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashType, witnessValue, witnessScript) { | |
9f59e99b IC |
12015 | // TODO: remove keyPair.network matching in 4.0.0 |
12016 | if (keyPair.network && keyPair.network !== this.network) throw new TypeError('Inconsistent network') | |
a0091a40 IC |
12017 | if (!this.inputs[vin]) throw new Error('No input at index: ' + vin) |
12018 | hashType = hashType || Transaction.SIGHASH_ALL | |
12019 | ||
12020 | var input = this.inputs[vin] | |
12021 | ||
12022 | // if redeemScript was previously provided, enforce consistency | |
12023 | if (input.redeemScript !== undefined && | |
12024 | redeemScript && | |
12025 | !input.redeemScript.equals(redeemScript)) { | |
12026 | throw new Error('Inconsistent redeemScript') | |
12027 | } | |
12028 | ||
9f59e99b | 12029 | var kpPubKey = keyPair.publicKey || keyPair.getPublicKeyBuffer() |
a0091a40 | 12030 | if (!canSign(input)) { |
9f59e99b IC |
12031 | if (witnessValue !== undefined) { |
12032 | if (input.value !== undefined && input.value !== witnessValue) throw new Error('Input didn\'t match witnessValue') | |
12033 | typeforce(types.Satoshi, witnessValue) | |
12034 | input.value = witnessValue | |
12035 | } | |
12036 | ||
12037 | if (!canSign(input)) prepareInput(input, kpPubKey, redeemScript, witnessValue, witnessScript) | |
a0091a40 IC |
12038 | if (!canSign(input)) throw Error(input.prevOutType + ' not supported') |
12039 | } | |
12040 | ||
12041 | // ready to sign | |
12042 | var signatureHash | |
12043 | if (input.witness) { | |
9f59e99b | 12044 | signatureHash = this.tx.hashForWitnessV0(vin, input.signScript, input.value, hashType) |
a0091a40 IC |
12045 | } else { |
12046 | signatureHash = this.tx.hashForSignature(vin, input.signScript, hashType) | |
12047 | } | |
9f59e99b | 12048 | |
a0091a40 IC |
12049 | // enforce in order signing of public keys |
12050 | var signed = input.pubKeys.some(function (pubKey, i) { | |
12051 | if (!kpPubKey.equals(pubKey)) return false | |
12052 | if (input.signatures[i]) throw new Error('Signature already exists') | |
9f59e99b IC |
12053 | if (kpPubKey.length !== 33 && |
12054 | input.signType === scriptTypes.P2WPKH) throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH') | |
a0091a40 | 12055 | |
9f59e99b IC |
12056 | var signature = keyPair.sign(signatureHash) |
12057 | if (Buffer.isBuffer(signature)) signature = ECSignature.fromRSBuffer(signature) | |
12058 | ||
12059 | input.signatures[i] = signature.toScriptSignature(hashType) | |
a0091a40 IC |
12060 | return true |
12061 | }) | |
12062 | ||
12063 | if (!signed) throw new Error('Key pair cannot sign for this input') | |
12064 | } | |
12065 | ||
12066 | function signatureHashType (buffer) { | |
12067 | return buffer.readUInt8(buffer.length - 1) | |
12068 | } | |
12069 | ||
12070 | TransactionBuilder.prototype.__canModifyInputs = function () { | |
12071 | return this.inputs.every(function (input) { | |
12072 | // any signatures? | |
12073 | if (input.signatures === undefined) return true | |
12074 | ||
12075 | return input.signatures.every(function (signature) { | |
12076 | if (!signature) return true | |
12077 | var hashType = signatureHashType(signature) | |
12078 | ||
12079 | // if SIGHASH_ANYONECANPAY is set, signatures would not | |
12080 | // be invalidated by more inputs | |
12081 | return hashType & Transaction.SIGHASH_ANYONECANPAY | |
12082 | }) | |
12083 | }) | |
12084 | } | |
12085 | ||
12086 | TransactionBuilder.prototype.__canModifyOutputs = function () { | |
12087 | var nInputs = this.tx.ins.length | |
12088 | var nOutputs = this.tx.outs.length | |
12089 | ||
12090 | return this.inputs.every(function (input) { | |
12091 | if (input.signatures === undefined) return true | |
12092 | ||
12093 | return input.signatures.every(function (signature) { | |
12094 | if (!signature) return true | |
12095 | var hashType = signatureHashType(signature) | |
12096 | ||
12097 | var hashTypeMod = hashType & 0x1f | |
12098 | if (hashTypeMod === Transaction.SIGHASH_NONE) return true | |
12099 | if (hashTypeMod === Transaction.SIGHASH_SINGLE) { | |
12100 | // if SIGHASH_SINGLE is set, and nInputs > nOutputs | |
12101 | // some signatures would be invalidated by the addition | |
12102 | // of more outputs | |
12103 | return nInputs <= nOutputs | |
12104 | } | |
12105 | }) | |
12106 | }) | |
12107 | } | |
12108 | ||
12109 | TransactionBuilder.prototype.__overMaximumFees = function (bytes) { | |
12110 | // not all inputs will have .value defined | |
12111 | var incoming = this.inputs.reduce(function (a, x) { return a + (x.value >>> 0) }, 0) | |
12112 | ||
12113 | // but all outputs do, and if we have any input value | |
12114 | // we can immediately determine if the outputs are too small | |
12115 | var outgoing = this.tx.outs.reduce(function (a, x) { return a + x.value }, 0) | |
12116 | var fee = incoming - outgoing | |
12117 | var feeRate = fee / bytes | |
12118 | ||
12119 | return feeRate > this.maximumFeeRate | |
12120 | } | |
12121 | ||
12122 | module.exports = TransactionBuilder | |
12123 | ||
9f59e99b | 12124 | },{"./address":44,"./crypto":47,"./ecpair":49,"./ecsignature":50,"./networks":53,"./script":54,"./templates":56,"./transaction":78,"./types":80,"bitcoin-ops":42,"safe-buffer":128,"typeforce":139}],80:[function(require,module,exports){ |
a0091a40 IC |
12125 | var typeforce = require('typeforce') |
12126 | ||
12127 | var UINT31_MAX = Math.pow(2, 31) - 1 | |
12128 | function UInt31 (value) { | |
12129 | return typeforce.UInt32(value) && value <= UINT31_MAX | |
12130 | } | |
12131 | ||
12132 | function BIP32Path (value) { | |
12133 | return typeforce.String(value) && value.match(/^(m\/)?(\d+'?\/)*\d+'?$/) | |
12134 | } | |
12135 | BIP32Path.toJSON = function () { return 'BIP32 derivation path' } | |
12136 | ||
12137 | var SATOSHI_MAX = 21 * 1e14 | |
12138 | function Satoshi (value) { | |
12139 | return typeforce.UInt53(value) && value <= SATOSHI_MAX | |
12140 | } | |
12141 | ||
12142 | // external dependent types | |
12143 | var BigInt = typeforce.quacksLike('BigInteger') | |
12144 | var ECPoint = typeforce.quacksLike('Point') | |
12145 | ||
12146 | // exposed, external API | |
12147 | var ECSignature = typeforce.compile({ r: BigInt, s: BigInt }) | |
12148 | var Network = typeforce.compile({ | |
12149 | messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String), | |
12150 | bip32: { | |
12151 | public: typeforce.UInt32, | |
12152 | private: typeforce.UInt32 | |
12153 | }, | |
12154 | pubKeyHash: typeforce.UInt8, | |
12155 | scriptHash: typeforce.UInt8, | |
12156 | wif: typeforce.UInt8 | |
12157 | }) | |
12158 | ||
12159 | // extend typeforce types with ours | |
12160 | var types = { | |
12161 | BigInt: BigInt, | |
12162 | BIP32Path: BIP32Path, | |
12163 | Buffer256bit: typeforce.BufferN(32), | |
12164 | ECPoint: ECPoint, | |
12165 | ECSignature: ECSignature, | |
12166 | Hash160bit: typeforce.BufferN(20), | |
12167 | Hash256bit: typeforce.BufferN(32), | |
12168 | Network: Network, | |
12169 | Satoshi: Satoshi, | |
12170 | UInt31: UInt31 | |
12171 | } | |
12172 | ||
12173 | for (var typeName in typeforce) { | |
12174 | types[typeName] = typeforce[typeName] | |
12175 | } | |
12176 | ||
12177 | module.exports = types | |
12178 | ||
9f59e99b IC |
12179 | },{"typeforce":139}],81:[function(require,module,exports){ |
12180 | (function (module, exports) { | |
12181 | 'use strict'; | |
a0091a40 | 12182 | |
9f59e99b IC |
12183 | // Utils |
12184 | function assert (val, msg) { | |
12185 | if (!val) throw new Error(msg || 'Assertion failed'); | |
12186 | } | |
a0091a40 | 12187 | |
9f59e99b IC |
12188 | // Could use `inherits` module, but don't want to move from single file |
12189 | // architecture yet. | |
12190 | function inherits (ctor, superCtor) { | |
12191 | ctor.super_ = superCtor; | |
12192 | var TempCtor = function () {}; | |
12193 | TempCtor.prototype = superCtor.prototype; | |
12194 | ctor.prototype = new TempCtor(); | |
12195 | ctor.prototype.constructor = ctor; | |
12196 | } | |
a0091a40 | 12197 | |
9f59e99b | 12198 | // BN |
a0091a40 | 12199 | |
9f59e99b IC |
12200 | function BN (number, base, endian) { |
12201 | if (BN.isBN(number)) { | |
12202 | return number; | |
12203 | } | |
a0091a40 | 12204 | |
9f59e99b IC |
12205 | this.negative = 0; |
12206 | this.words = null; | |
12207 | this.length = 0; | |
a0091a40 | 12208 | |
9f59e99b IC |
12209 | // Reduction context |
12210 | this.red = null; | |
a0091a40 | 12211 | |
9f59e99b IC |
12212 | if (number !== null) { |
12213 | if (base === 'le' || base === 'be') { | |
12214 | endian = base; | |
12215 | base = 10; | |
12216 | } | |
a0091a40 | 12217 | |
9f59e99b IC |
12218 | this._init(number || 0, base || 10, endian || 'be'); |
12219 | } | |
a0091a40 | 12220 | } |
9f59e99b IC |
12221 | if (typeof module === 'object') { |
12222 | module.exports = BN; | |
12223 | } else { | |
12224 | exports.BN = BN; | |
a0091a40 | 12225 | } |
a0091a40 | 12226 | |
9f59e99b IC |
12227 | BN.BN = BN; |
12228 | BN.wordSize = 26; | |
12229 | ||
12230 | var Buffer; | |
12231 | try { | |
12232 | Buffer = require('buf' + 'fer').Buffer; | |
12233 | } catch (e) { | |
a0091a40 IC |
12234 | } |
12235 | ||
9f59e99b IC |
12236 | BN.isBN = function isBN (num) { |
12237 | if (num instanceof BN) { | |
12238 | return true; | |
12239 | } | |
a0091a40 | 12240 | |
9f59e99b IC |
12241 | return num !== null && typeof num === 'object' && |
12242 | num.constructor.wordSize === BN.wordSize && Array.isArray(num.words); | |
12243 | }; | |
a0091a40 | 12244 | |
9f59e99b IC |
12245 | BN.max = function max (left, right) { |
12246 | if (left.cmp(right) > 0) return left; | |
12247 | return right; | |
12248 | }; | |
a0091a40 | 12249 | |
9f59e99b IC |
12250 | BN.min = function min (left, right) { |
12251 | if (left.cmp(right) < 0) return left; | |
12252 | return right; | |
12253 | }; | |
a0091a40 | 12254 | |
9f59e99b IC |
12255 | BN.prototype._init = function init (number, base, endian) { |
12256 | if (typeof number === 'number') { | |
12257 | return this._initNumber(number, base, endian); | |
12258 | } | |
a0091a40 | 12259 | |
9f59e99b IC |
12260 | if (typeof number === 'object') { |
12261 | return this._initArray(number, base, endian); | |
12262 | } | |
a0091a40 | 12263 | |
9f59e99b IC |
12264 | if (base === 'hex') { |
12265 | base = 16; | |
a0091a40 | 12266 | } |
9f59e99b | 12267 | assert(base === (base | 0) && base >= 2 && base <= 36); |
a0091a40 | 12268 | |
9f59e99b IC |
12269 | number = number.toString().replace(/\s+/g, ''); |
12270 | var start = 0; | |
12271 | if (number[0] === '-') { | |
12272 | start++; | |
12273 | } | |
a0091a40 | 12274 | |
9f59e99b IC |
12275 | if (base === 16) { |
12276 | this._parseHex(number, start); | |
12277 | } else { | |
12278 | this._parseBase(number, base, start); | |
12279 | } | |
a0091a40 | 12280 | |
9f59e99b IC |
12281 | if (number[0] === '-') { |
12282 | this.negative = 1; | |
12283 | } | |
a0091a40 | 12284 | |
9f59e99b | 12285 | this.strip(); |
a0091a40 | 12286 | |
9f59e99b | 12287 | if (endian !== 'le') return; |
a0091a40 | 12288 | |
9f59e99b IC |
12289 | this._initArray(this.toArray(), base, endian); |
12290 | }; | |
a0091a40 | 12291 | |
9f59e99b IC |
12292 | BN.prototype._initNumber = function _initNumber (number, base, endian) { |
12293 | if (number < 0) { | |
12294 | this.negative = 1; | |
12295 | number = -number; | |
12296 | } | |
12297 | if (number < 0x4000000) { | |
12298 | this.words = [ number & 0x3ffffff ]; | |
12299 | this.length = 1; | |
12300 | } else if (number < 0x10000000000000) { | |
12301 | this.words = [ | |
12302 | number & 0x3ffffff, | |
12303 | (number / 0x4000000) & 0x3ffffff | |
12304 | ]; | |
12305 | this.length = 2; | |
12306 | } else { | |
12307 | assert(number < 0x20000000000000); // 2 ^ 53 (unsafe) | |
12308 | this.words = [ | |
12309 | number & 0x3ffffff, | |
12310 | (number / 0x4000000) & 0x3ffffff, | |
12311 | 1 | |
12312 | ]; | |
12313 | this.length = 3; | |
12314 | } | |
a0091a40 | 12315 | |
9f59e99b | 12316 | if (endian !== 'le') return; |
a0091a40 | 12317 | |
9f59e99b IC |
12318 | // Reverse the bytes |
12319 | this._initArray(this.toArray(), base, endian); | |
12320 | }; | |
a0091a40 | 12321 | |
9f59e99b IC |
12322 | BN.prototype._initArray = function _initArray (number, base, endian) { |
12323 | // Perhaps a Uint8Array | |
12324 | assert(typeof number.length === 'number'); | |
12325 | if (number.length <= 0) { | |
12326 | this.words = [ 0 ]; | |
12327 | this.length = 1; | |
12328 | return this; | |
12329 | } | |
a0091a40 | 12330 | |
9f59e99b IC |
12331 | this.length = Math.ceil(number.length / 3); |
12332 | this.words = new Array(this.length); | |
12333 | for (var i = 0; i < this.length; i++) { | |
12334 | this.words[i] = 0; | |
12335 | } | |
a0091a40 | 12336 | |
9f59e99b IC |
12337 | var j, w; |
12338 | var off = 0; | |
12339 | if (endian === 'be') { | |
12340 | for (i = number.length - 1, j = 0; i >= 0; i -= 3) { | |
12341 | w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16); | |
12342 | this.words[j] |= (w << off) & 0x3ffffff; | |
12343 | this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; | |
12344 | off += 24; | |
12345 | if (off >= 26) { | |
12346 | off -= 26; | |
12347 | j++; | |
12348 | } | |
12349 | } | |
12350 | } else if (endian === 'le') { | |
12351 | for (i = 0, j = 0; i < number.length; i += 3) { | |
12352 | w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16); | |
12353 | this.words[j] |= (w << off) & 0x3ffffff; | |
12354 | this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff; | |
12355 | off += 24; | |
12356 | if (off >= 26) { | |
12357 | off -= 26; | |
12358 | j++; | |
12359 | } | |
12360 | } | |
12361 | } | |
12362 | return this.strip(); | |
12363 | }; | |
a0091a40 | 12364 | |
9f59e99b IC |
12365 | function parseHex (str, start, end) { |
12366 | var r = 0; | |
12367 | var len = Math.min(str.length, end); | |
12368 | for (var i = start; i < len; i++) { | |
12369 | var c = str.charCodeAt(i) - 48; | |
a0091a40 | 12370 | |
9f59e99b | 12371 | r <<= 4; |
a0091a40 | 12372 | |
9f59e99b IC |
12373 | // 'a' - 'f' |
12374 | if (c >= 49 && c <= 54) { | |
12375 | r |= c - 49 + 0xa; | |
a0091a40 | 12376 | |
9f59e99b IC |
12377 | // 'A' - 'F' |
12378 | } else if (c >= 17 && c <= 22) { | |
12379 | r |= c - 17 + 0xa; | |
a0091a40 | 12380 | |
9f59e99b IC |
12381 | // '0' - '9' |
12382 | } else { | |
12383 | r |= c & 0xf; | |
12384 | } | |
12385 | } | |
12386 | return r; | |
12387 | } | |
a0091a40 | 12388 | |
9f59e99b IC |
12389 | BN.prototype._parseHex = function _parseHex (number, start) { |
12390 | // Create possibly bigger array to ensure that it fits the number | |
12391 | this.length = Math.ceil((number.length - start) / 6); | |
12392 | this.words = new Array(this.length); | |
12393 | for (var i = 0; i < this.length; i++) { | |
12394 | this.words[i] = 0; | |
12395 | } | |
a0091a40 | 12396 | |
9f59e99b IC |
12397 | var j, w; |
12398 | // Scan 24-bit chunks and add them to the number | |
12399 | var off = 0; | |
12400 | for (i = number.length - 6, j = 0; i >= start; i -= 6) { | |
12401 | w = parseHex(number, i, i + 6); | |
12402 | this.words[j] |= (w << off) & 0x3ffffff; | |
12403 | // NOTE: `0x3fffff` is intentional here, 26bits max shift + 24bit hex limb | |
12404 | this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; | |
12405 | off += 24; | |
12406 | if (off >= 26) { | |
12407 | off -= 26; | |
12408 | j++; | |
12409 | } | |
12410 | } | |
12411 | if (i + 6 !== start) { | |
12412 | w = parseHex(number, start, i + 6); | |
12413 | this.words[j] |= (w << off) & 0x3ffffff; | |
12414 | this.words[j + 1] |= w >>> (26 - off) & 0x3fffff; | |
12415 | } | |
12416 | this.strip(); | |
12417 | }; | |
a0091a40 | 12418 | |
9f59e99b IC |
12419 | function parseBase (str, start, end, mul) { |
12420 | var r = 0; | |
12421 | var len = Math.min(str.length, end); | |
12422 | for (var i = start; i < len; i++) { | |
12423 | var c = str.charCodeAt(i) - 48; | |
a0091a40 | 12424 | |
9f59e99b | 12425 | r *= mul; |
a0091a40 | 12426 | |
9f59e99b IC |
12427 | // 'a' |
12428 | if (c >= 49) { | |
12429 | r += c - 49 + 0xa; | |
a0091a40 | 12430 | |
9f59e99b IC |
12431 | // 'A' |
12432 | } else if (c >= 17) { | |
12433 | r += c - 17 + 0xa; | |
a0091a40 | 12434 | |
9f59e99b IC |
12435 | // '0' - '9' |
12436 | } else { | |
12437 | r += c; | |
12438 | } | |
12439 | } | |
12440 | return r; | |
a0091a40 IC |
12441 | } |
12442 | ||
9f59e99b IC |
12443 | BN.prototype._parseBase = function _parseBase (number, base, start) { |
12444 | // Initialize as zero | |
12445 | this.words = [ 0 ]; | |
12446 | this.length = 1; | |
a0091a40 | 12447 | |
9f59e99b IC |
12448 | // Find length of limb in base |
12449 | for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) { | |
12450 | limbLen++; | |
12451 | } | |
12452 | limbLen--; | |
12453 | limbPow = (limbPow / base) | 0; | |
a0091a40 | 12454 | |
9f59e99b IC |
12455 | var total = number.length - start; |
12456 | var mod = total % limbLen; | |
12457 | var end = Math.min(total, total - mod) + start; | |
a0091a40 | 12458 | |
9f59e99b IC |
12459 | var word = 0; |
12460 | for (var i = start; i < end; i += limbLen) { | |
12461 | word = parseBase(number, i, i + limbLen, base); | |
a0091a40 | 12462 | |
9f59e99b IC |
12463 | this.imuln(limbPow); |
12464 | if (this.words[0] + word < 0x4000000) { | |
12465 | this.words[0] += word; | |
12466 | } else { | |
12467 | this._iaddn(word); | |
12468 | } | |
12469 | } | |
a0091a40 | 12470 | |
9f59e99b IC |
12471 | if (mod !== 0) { |
12472 | var pow = 1; | |
12473 | word = parseBase(number, i, number.length, base); | |
a0091a40 | 12474 | |
9f59e99b IC |
12475 | for (i = 0; i < mod; i++) { |
12476 | pow *= base; | |
12477 | } | |
a0091a40 | 12478 | |
9f59e99b IC |
12479 | this.imuln(pow); |
12480 | if (this.words[0] + word < 0x4000000) { | |
12481 | this.words[0] += word; | |
12482 | } else { | |
12483 | this._iaddn(word); | |
12484 | } | |
12485 | } | |
12486 | }; | |
a0091a40 | 12487 | |
9f59e99b IC |
12488 | BN.prototype.copy = function copy (dest) { |
12489 | dest.words = new Array(this.length); | |
12490 | for (var i = 0; i < this.length; i++) { | |
12491 | dest.words[i] = this.words[i]; | |
12492 | } | |
12493 | dest.length = this.length; | |
12494 | dest.negative = this.negative; | |
12495 | dest.red = this.red; | |
12496 | }; | |
a0091a40 | 12497 | |
9f59e99b IC |
12498 | BN.prototype.clone = function clone () { |
12499 | var r = new BN(null); | |
12500 | this.copy(r); | |
12501 | return r; | |
12502 | }; | |
12503 | ||
12504 | BN.prototype._expand = function _expand (size) { | |
12505 | while (this.length < size) { | |
12506 | this.words[this.length++] = 0; | |
12507 | } | |
12508 | return this; | |
12509 | }; | |
12510 | ||
12511 | // Remove leading `0` from `this` | |
12512 | BN.prototype.strip = function strip () { | |
12513 | while (this.length > 1 && this.words[this.length - 1] === 0) { | |
12514 | this.length--; | |
12515 | } | |
12516 | return this._normSign(); | |
12517 | }; | |
12518 | ||
12519 | BN.prototype._normSign = function _normSign () { | |
12520 | // -0 = 0 | |
12521 | if (this.length === 1 && this.words[0] === 0) { | |
12522 | this.negative = 0; | |
12523 | } | |
12524 | return this; | |
12525 | }; | |
12526 | ||
12527 | BN.prototype.inspect = function inspect () { | |
12528 | return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>'; | |
12529 | }; | |
12530 | ||
12531 | /* | |
12532 | ||
12533 | var zeros = []; | |
12534 | var groupSizes = []; | |
12535 | var groupBases = []; | |
12536 | ||
12537 | var s = ''; | |
12538 | var i = -1; | |
12539 | while (++i < BN.wordSize) { | |
12540 | zeros[i] = s; | |
12541 | s += '0'; | |
12542 | } | |
12543 | groupSizes[0] = 0; | |
12544 | groupSizes[1] = 0; | |
12545 | groupBases[0] = 0; | |
12546 | groupBases[1] = 0; | |
12547 | var base = 2 - 1; | |
12548 | while (++base < 36 + 1) { | |
12549 | var groupSize = 0; | |
12550 | var groupBase = 1; | |
12551 | while (groupBase < (1 << BN.wordSize) / base) { | |
12552 | groupBase *= base; | |
12553 | groupSize += 1; | |
12554 | } | |
12555 | groupSizes[base] = groupSize; | |
12556 | groupBases[base] = groupBase; | |
12557 | } | |
12558 | ||
12559 | */ | |
12560 | ||
12561 | var zeros = [ | |
12562 | '', | |
12563 | '0', | |
12564 | '00', | |
12565 | '000', | |
12566 | '0000', | |
12567 | '00000', | |
12568 | '000000', | |
12569 | '0000000', | |
12570 | '00000000', | |
12571 | '000000000', | |
12572 | '0000000000', | |
12573 | '00000000000', | |
12574 | '000000000000', | |
12575 | '0000000000000', | |
12576 | '00000000000000', | |
12577 | '000000000000000', | |
12578 | '0000000000000000', | |
12579 | '00000000000000000', | |
12580 | '000000000000000000', | |
12581 | '0000000000000000000', | |
12582 | '00000000000000000000', | |
12583 | '000000000000000000000', | |
12584 | '0000000000000000000000', | |
12585 | '00000000000000000000000', | |
12586 | '000000000000000000000000', | |
12587 | '0000000000000000000000000' | |
12588 | ]; | |
12589 | ||
12590 | var groupSizes = [ | |
12591 | 0, 0, | |
12592 | 25, 16, 12, 11, 10, 9, 8, | |
12593 | 8, 7, 7, 7, 7, 6, 6, | |
12594 | 6, 6, 6, 6, 6, 5, 5, | |
12595 | 5, 5, 5, 5, 5, 5, 5, | |
12596 | 5, 5, 5, 5, 5, 5, 5 | |
12597 | ]; | |
12598 | ||
12599 | var groupBases = [ | |
12600 | 0, 0, | |
12601 | 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216, | |
12602 | 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625, | |
12603 | 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632, | |
12604 | 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149, | |
12605 | 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176 | |
12606 | ]; | |
12607 | ||
12608 | BN.prototype.toString = function toString (base, padding) { | |
12609 | base = base || 10; | |
12610 | padding = padding | 0 || 1; | |
12611 | ||
12612 | var out; | |
12613 | if (base === 16 || base === 'hex') { | |
12614 | out = ''; | |
12615 | var off = 0; | |
12616 | var carry = 0; | |
12617 | for (var i = 0; i < this.length; i++) { | |
12618 | var w = this.words[i]; | |
12619 | var word = (((w << off) | carry) & 0xffffff).toString(16); | |
12620 | carry = (w >>> (24 - off)) & 0xffffff; | |
12621 | if (carry !== 0 || i !== this.length - 1) { | |
12622 | out = zeros[6 - word.length] + word + out; | |
12623 | } else { | |
12624 | out = word + out; | |
12625 | } | |
12626 | off += 2; | |
12627 | if (off >= 26) { | |
12628 | off -= 26; | |
12629 | i--; | |
12630 | } | |
12631 | } | |
12632 | if (carry !== 0) { | |
12633 | out = carry.toString(16) + out; | |
12634 | } | |
12635 | while (out.length % padding !== 0) { | |
12636 | out = '0' + out; | |
12637 | } | |
12638 | if (this.negative !== 0) { | |
12639 | out = '-' + out; | |
12640 | } | |
12641 | return out; | |
12642 | } | |
12643 | ||
12644 | if (base === (base | 0) && base >= 2 && base <= 36) { | |
12645 | // var groupSize = Math.floor(BN.wordSize * Math.LN2 / Math.log(base)); | |
12646 | var groupSize = groupSizes[base]; | |
12647 | // var groupBase = Math.pow(base, groupSize); | |
12648 | var groupBase = groupBases[base]; | |
12649 | out = ''; | |
12650 | var c = this.clone(); | |
12651 | c.negative = 0; | |
12652 | while (!c.isZero()) { | |
12653 | var r = c.modn(groupBase).toString(base); | |
12654 | c = c.idivn(groupBase); | |
12655 | ||
12656 | if (!c.isZero()) { | |
12657 | out = zeros[groupSize - r.length] + r + out; | |
12658 | } else { | |
12659 | out = r + out; | |
12660 | } | |
12661 | } | |
12662 | if (this.isZero()) { | |
12663 | out = '0' + out; | |
12664 | } | |
12665 | while (out.length % padding !== 0) { | |
12666 | out = '0' + out; | |
12667 | } | |
12668 | if (this.negative !== 0) { | |
12669 | out = '-' + out; | |
12670 | } | |
12671 | return out; | |
12672 | } | |
12673 | ||
12674 | assert(false, 'Base should be between 2 and 36'); | |
12675 | }; | |
12676 | ||
12677 | BN.prototype.toNumber = function toNumber () { | |
12678 | var ret = this.words[0]; | |
12679 | if (this.length === 2) { | |
12680 | ret += this.words[1] * 0x4000000; | |
12681 | } else if (this.length === 3 && this.words[2] === 0x01) { | |
12682 | // NOTE: at this stage it is known that the top bit is set | |
12683 | ret += 0x10000000000000 + (this.words[1] * 0x4000000); | |
12684 | } else if (this.length > 2) { | |
12685 | assert(false, 'Number can only safely store up to 53 bits'); | |
12686 | } | |
12687 | return (this.negative !== 0) ? -ret : ret; | |
12688 | }; | |
12689 | ||
12690 | BN.prototype.toJSON = function toJSON () { | |
12691 | return this.toString(16); | |
12692 | }; | |
12693 | ||
12694 | BN.prototype.toBuffer = function toBuffer (endian, length) { | |
12695 | assert(typeof Buffer !== 'undefined'); | |
12696 | return this.toArrayLike(Buffer, endian, length); | |
12697 | }; | |
12698 | ||
12699 | BN.prototype.toArray = function toArray (endian, length) { | |
12700 | return this.toArrayLike(Array, endian, length); | |
12701 | }; | |
12702 | ||
12703 | BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) { | |
12704 | var byteLength = this.byteLength(); | |
12705 | var reqLength = length || Math.max(1, byteLength); | |
12706 | assert(byteLength <= reqLength, 'byte array longer than desired length'); | |
12707 | assert(reqLength > 0, 'Requested array length <= 0'); | |
12708 | ||
12709 | this.strip(); | |
12710 | var littleEndian = endian === 'le'; | |
12711 | var res = new ArrayType(reqLength); | |
12712 | ||
12713 | var b, i; | |
12714 | var q = this.clone(); | |
12715 | if (!littleEndian) { | |
12716 | // Assume big-endian | |
12717 | for (i = 0; i < reqLength - byteLength; i++) { | |
12718 | res[i] = 0; | |
12719 | } | |
12720 | ||
12721 | for (i = 0; !q.isZero(); i++) { | |
12722 | b = q.andln(0xff); | |
12723 | q.iushrn(8); | |
12724 | ||
12725 | res[reqLength - i - 1] = b; | |
12726 | } | |
12727 | } else { | |
12728 | for (i = 0; !q.isZero(); i++) { | |
12729 | b = q.andln(0xff); | |
12730 | q.iushrn(8); | |
12731 | ||
12732 | res[i] = b; | |
12733 | } | |
12734 | ||
12735 | for (; i < reqLength; i++) { | |
12736 | res[i] = 0; | |
12737 | } | |
12738 | } | |
12739 | ||
12740 | return res; | |
12741 | }; | |
12742 | ||
12743 | if (Math.clz32) { | |
12744 | BN.prototype._countBits = function _countBits (w) { | |
12745 | return 32 - Math.clz32(w); | |
12746 | }; | |
12747 | } else { | |
12748 | BN.prototype._countBits = function _countBits (w) { | |
12749 | var t = w; | |
12750 | var r = 0; | |
12751 | if (t >= 0x1000) { | |
12752 | r += 13; | |
12753 | t >>>= 13; | |
12754 | } | |
12755 | if (t >= 0x40) { | |
12756 | r += 7; | |
12757 | t >>>= 7; | |
12758 | } | |
12759 | if (t >= 0x8) { | |
12760 | r += 4; | |
12761 | t >>>= 4; | |
12762 | } | |
12763 | if (t >= 0x02) { | |
12764 | r += 2; | |
12765 | t >>>= 2; | |
12766 | } | |
12767 | return r + t; | |
12768 | }; | |
12769 | } | |
12770 | ||
12771 | BN.prototype._zeroBits = function _zeroBits (w) { | |
12772 | // Short-cut | |
12773 | if (w === 0) return 26; | |
12774 | ||
12775 | var t = w; | |
12776 | var r = 0; | |
12777 | if ((t & 0x1fff) === 0) { | |
12778 | r += 13; | |
12779 | t >>>= 13; | |
12780 | } | |
12781 | if ((t & 0x7f) === 0) { | |
12782 | r += 7; | |
12783 | t >>>= 7; | |
12784 | } | |
12785 | if ((t & 0xf) === 0) { | |
12786 | r += 4; | |
12787 | t >>>= 4; | |
12788 | } | |
12789 | if ((t & 0x3) === 0) { | |
12790 | r += 2; | |
12791 | t >>>= 2; | |
12792 | } | |
12793 | if ((t & 0x1) === 0) { | |
12794 | r++; | |
12795 | } | |
12796 | return r; | |
12797 | }; | |
12798 | ||
12799 | // Return number of used bits in a BN | |
12800 | BN.prototype.bitLength = function bitLength () { | |
12801 | var w = this.words[this.length - 1]; | |
12802 | var hi = this._countBits(w); | |
12803 | return (this.length - 1) * 26 + hi; | |
12804 | }; | |
12805 | ||
12806 | function toBitArray (num) { | |
12807 | var w = new Array(num.bitLength()); | |
12808 | ||
12809 | for (var bit = 0; bit < w.length; bit++) { | |
12810 | var off = (bit / 26) | 0; | |
12811 | var wbit = bit % 26; | |
12812 | ||
12813 | w[bit] = (num.words[off] & (1 << wbit)) >>> wbit; | |
12814 | } | |
12815 | ||
12816 | return w; | |
12817 | } | |
12818 | ||
12819 | // Number of trailing zero bits | |
12820 | BN.prototype.zeroBits = function zeroBits () { | |
12821 | if (this.isZero()) return 0; | |
12822 | ||
12823 | var r = 0; | |
12824 | for (var i = 0; i < this.length; i++) { | |
12825 | var b = this._zeroBits(this.words[i]); | |
12826 | r += b; | |
12827 | if (b !== 26) break; | |
12828 | } | |
12829 | return r; | |
12830 | }; | |
12831 | ||
12832 | BN.prototype.byteLength = function byteLength () { | |
12833 | return Math.ceil(this.bitLength() / 8); | |
12834 | }; | |
12835 | ||
12836 | BN.prototype.toTwos = function toTwos (width) { | |
12837 | if (this.negative !== 0) { | |
12838 | return this.abs().inotn(width).iaddn(1); | |
12839 | } | |
12840 | return this.clone(); | |
12841 | }; | |
12842 | ||
12843 | BN.prototype.fromTwos = function fromTwos (width) { | |
12844 | if (this.testn(width - 1)) { | |
12845 | return this.notn(width).iaddn(1).ineg(); | |
12846 | } | |
12847 | return this.clone(); | |
12848 | }; | |
12849 | ||
12850 | BN.prototype.isNeg = function isNeg () { | |
12851 | return this.negative !== 0; | |
12852 | }; | |
12853 | ||
12854 | // Return negative clone of `this` | |
12855 | BN.prototype.neg = function neg () { | |
12856 | return this.clone().ineg(); | |
12857 | }; | |
12858 | ||
12859 | BN.prototype.ineg = function ineg () { | |
12860 | if (!this.isZero()) { | |
12861 | this.negative ^= 1; | |
12862 | } | |
12863 | ||
12864 | return this; | |
12865 | }; | |
12866 | ||
12867 | // Or `num` with `this` in-place | |
12868 | BN.prototype.iuor = function iuor (num) { | |
12869 | while (this.length < num.length) { | |
12870 | this.words[this.length++] = 0; | |
12871 | } | |
12872 | ||
12873 | for (var i = 0; i < num.length; i++) { | |
12874 | this.words[i] = this.words[i] | num.words[i]; | |
12875 | } | |
12876 | ||
12877 | return this.strip(); | |
12878 | }; | |
12879 | ||
12880 | BN.prototype.ior = function ior (num) { | |
12881 | assert((this.negative | num.negative) === 0); | |
12882 | return this.iuor(num); | |
12883 | }; | |
12884 | ||
12885 | // Or `num` with `this` | |
12886 | BN.prototype.or = function or (num) { | |
12887 | if (this.length > num.length) return this.clone().ior(num); | |
12888 | return num.clone().ior(this); | |
12889 | }; | |
12890 | ||
12891 | BN.prototype.uor = function uor (num) { | |
12892 | if (this.length > num.length) return this.clone().iuor(num); | |
12893 | return num.clone().iuor(this); | |
12894 | }; | |
12895 | ||
12896 | // And `num` with `this` in-place | |
12897 | BN.prototype.iuand = function iuand (num) { | |
12898 | // b = min-length(num, this) | |
12899 | var b; | |
12900 | if (this.length > num.length) { | |
12901 | b = num; | |
12902 | } else { | |
12903 | b = this; | |
12904 | } | |
12905 | ||
12906 | for (var i = 0; i < b.length; i++) { | |
12907 | this.words[i] = this.words[i] & num.words[i]; | |
12908 | } | |
12909 | ||
12910 | this.length = b.length; | |
12911 | ||
12912 | return this.strip(); | |
12913 | }; | |
12914 | ||
12915 | BN.prototype.iand = function iand (num) { | |
12916 | assert((this.negative | num.negative) === 0); | |
12917 | return this.iuand(num); | |
12918 | }; | |
12919 | ||
12920 | // And `num` with `this` | |
12921 | BN.prototype.and = function and (num) { | |
12922 | if (this.length > num.length) return this.clone().iand(num); | |
12923 | return num.clone().iand(this); | |
12924 | }; | |
12925 | ||
12926 | BN.prototype.uand = function uand (num) { | |
12927 | if (this.length > num.length) return this.clone().iuand(num); | |
12928 | return num.clone().iuand(this); | |
12929 | }; | |
12930 | ||
12931 | // Xor `num` with `this` in-place | |
12932 | BN.prototype.iuxor = function iuxor (num) { | |
12933 | // a.length > b.length | |
12934 | var a; | |
12935 | var b; | |
12936 | if (this.length > num.length) { | |
12937 | a = this; | |
12938 | b = num; | |
12939 | } else { | |
12940 | a = num; | |
12941 | b = this; | |
12942 | } | |
12943 | ||
12944 | for (var i = 0; i < b.length; i++) { | |
12945 | this.words[i] = a.words[i] ^ b.words[i]; | |
12946 | } | |
12947 | ||
12948 | if (this !== a) { | |
12949 | for (; i < a.length; i++) { | |
12950 | this.words[i] = a.words[i]; | |
12951 | } | |
12952 | } | |
12953 | ||
12954 | this.length = a.length; | |
12955 | ||
12956 | return this.strip(); | |
12957 | }; | |
12958 | ||
12959 | BN.prototype.ixor = function ixor (num) { | |
12960 | assert((this.negative | num.negative) === 0); | |
12961 | return this.iuxor(num); | |
12962 | }; | |
12963 | ||
12964 | // Xor `num` with `this` | |
12965 | BN.prototype.xor = function xor (num) { | |
12966 | if (this.length > num.length) return this.clone().ixor(num); | |
12967 | return num.clone().ixor(this); | |
12968 | }; | |
12969 | ||
12970 | BN.prototype.uxor = function uxor (num) { | |
12971 | if (this.length > num.length) return this.clone().iuxor(num); | |
12972 | return num.clone().iuxor(this); | |
12973 | }; | |
12974 | ||
12975 | // Not ``this`` with ``width`` bitwidth | |
12976 | BN.prototype.inotn = function inotn (width) { | |
12977 | assert(typeof width === 'number' && width >= 0); | |
12978 | ||
12979 | var bytesNeeded = Math.ceil(width / 26) | 0; | |
12980 | var bitsLeft = width % 26; | |
12981 | ||
12982 | // Extend the buffer with leading zeroes | |
12983 | this._expand(bytesNeeded); | |
12984 | ||
12985 | if (bitsLeft > 0) { | |
12986 | bytesNeeded--; | |
12987 | } | |
12988 | ||
12989 | // Handle complete words | |
12990 | for (var i = 0; i < bytesNeeded; i++) { | |
12991 | this.words[i] = ~this.words[i] & 0x3ffffff; | |
12992 | } | |
12993 | ||
12994 | // Handle the residue | |
12995 | if (bitsLeft > 0) { | |
12996 | this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft)); | |
12997 | } | |
12998 | ||
12999 | // And remove leading zeroes | |
13000 | return this.strip(); | |
13001 | }; | |
13002 | ||
13003 | BN.prototype.notn = function notn (width) { | |
13004 | return this.clone().inotn(width); | |
13005 | }; | |
13006 | ||
13007 | // Set `bit` of `this` | |
13008 | BN.prototype.setn = function setn (bit, val) { | |
13009 | assert(typeof bit === 'number' && bit >= 0); | |
13010 | ||
13011 | var off = (bit / 26) | 0; | |
13012 | var wbit = bit % 26; | |
13013 | ||
13014 | this._expand(off + 1); | |
13015 | ||
13016 | if (val) { | |
13017 | this.words[off] = this.words[off] | (1 << wbit); | |
13018 | } else { | |
13019 | this.words[off] = this.words[off] & ~(1 << wbit); | |
13020 | } | |
13021 | ||
13022 | return this.strip(); | |
13023 | }; | |
13024 | ||
13025 | // Add `num` to `this` in-place | |
13026 | BN.prototype.iadd = function iadd (num) { | |
13027 | var r; | |
13028 | ||
13029 | // negative + positive | |
13030 | if (this.negative !== 0 && num.negative === 0) { | |
13031 | this.negative = 0; | |
13032 | r = this.isub(num); | |
13033 | this.negative ^= 1; | |
13034 | return this._normSign(); | |
13035 | ||
13036 | // positive + negative | |
13037 | } else if (this.negative === 0 && num.negative !== 0) { | |
13038 | num.negative = 0; | |
13039 | r = this.isub(num); | |
13040 | num.negative = 1; | |
13041 | return r._normSign(); | |
13042 | } | |
13043 | ||
13044 | // a.length > b.length | |
13045 | var a, b; | |
13046 | if (this.length > num.length) { | |
13047 | a = this; | |
13048 | b = num; | |
13049 | } else { | |
13050 | a = num; | |
13051 | b = this; | |
13052 | } | |
13053 | ||
13054 | var carry = 0; | |
13055 | for (var i = 0; i < b.length; i++) { | |
13056 | r = (a.words[i] | 0) + (b.words[i] | 0) + carry; | |
13057 | this.words[i] = r & 0x3ffffff; | |
13058 | carry = r >>> 26; | |
13059 | } | |
13060 | for (; carry !== 0 && i < a.length; i++) { | |
13061 | r = (a.words[i] | 0) + carry; | |
13062 | this.words[i] = r & 0x3ffffff; | |
13063 | carry = r >>> 26; | |
13064 | } | |
13065 | ||
13066 | this.length = a.length; | |
13067 | if (carry !== 0) { | |
13068 | this.words[this.length] = carry; | |
13069 | this.length++; | |
13070 | // Copy the rest of the words | |
13071 | } else if (a !== this) { | |
13072 | for (; i < a.length; i++) { | |
13073 | this.words[i] = a.words[i]; | |
13074 | } | |
13075 | } | |
13076 | ||
13077 | return this; | |
13078 | }; | |
13079 | ||
13080 | // Add `num` to `this` | |
13081 | BN.prototype.add = function add (num) { | |
13082 | var res; | |
13083 | if (num.negative !== 0 && this.negative === 0) { | |
13084 | num.negative = 0; | |
13085 | res = this.sub(num); | |
13086 | num.negative ^= 1; | |
13087 | return res; | |
13088 | } else if (num.negative === 0 && this.negative !== 0) { | |
13089 | this.negative = 0; | |
13090 | res = num.sub(this); | |
13091 | this.negative = 1; | |
13092 | return res; | |
13093 | } | |
13094 | ||
13095 | if (this.length > num.length) return this.clone().iadd(num); | |
13096 | ||
13097 | return num.clone().iadd(this); | |
13098 | }; | |
13099 | ||
13100 | // Subtract `num` from `this` in-place | |
13101 | BN.prototype.isub = function isub (num) { | |
13102 | // this - (-num) = this + num | |
13103 | if (num.negative !== 0) { | |
13104 | num.negative = 0; | |
13105 | var r = this.iadd(num); | |
13106 | num.negative = 1; | |
13107 | return r._normSign(); | |
13108 | ||
13109 | // -this - num = -(this + num) | |
13110 | } else if (this.negative !== 0) { | |
13111 | this.negative = 0; | |
13112 | this.iadd(num); | |
13113 | this.negative = 1; | |
13114 | return this._normSign(); | |
13115 | } | |
13116 | ||
13117 | // At this point both numbers are positive | |
13118 | var cmp = this.cmp(num); | |
13119 | ||
13120 | // Optimization - zeroify | |
13121 | if (cmp === 0) { | |
13122 | this.negative = 0; | |
13123 | this.length = 1; | |
13124 | this.words[0] = 0; | |
13125 | return this; | |
13126 | } | |
13127 | ||
13128 | // a > b | |
13129 | var a, b; | |
13130 | if (cmp > 0) { | |
13131 | a = this; | |
13132 | b = num; | |
13133 | } else { | |
13134 | a = num; | |
13135 | b = this; | |
13136 | } | |
13137 | ||
13138 | var carry = 0; | |
13139 | for (var i = 0; i < b.length; i++) { | |
13140 | r = (a.words[i] | 0) - (b.words[i] | 0) + carry; | |
13141 | carry = r >> 26; | |
13142 | this.words[i] = r & 0x3ffffff; | |
13143 | } | |
13144 | for (; carry !== 0 && i < a.length; i++) { | |
13145 | r = (a.words[i] | 0) + carry; | |
13146 | carry = r >> 26; | |
13147 | this.words[i] = r & 0x3ffffff; | |
13148 | } | |
13149 | ||
13150 | // Copy rest of the words | |
13151 | if (carry === 0 && i < a.length && a !== this) { | |
13152 | for (; i < a.length; i++) { | |
13153 | this.words[i] = a.words[i]; | |
13154 | } | |
13155 | } | |
13156 | ||
13157 | this.length = Math.max(this.length, i); | |
13158 | ||
13159 | if (a !== this) { | |
13160 | this.negative = 1; | |
13161 | } | |
13162 | ||
13163 | return this.strip(); | |
13164 | }; | |
13165 | ||
13166 | // Subtract `num` from `this` | |
13167 | BN.prototype.sub = function sub (num) { | |
13168 | return this.clone().isub(num); | |
13169 | }; | |
13170 | ||
13171 | function smallMulTo (self, num, out) { | |
13172 | out.negative = num.negative ^ self.negative; | |
13173 | var len = (self.length + num.length) | 0; | |
13174 | out.length = len; | |
13175 | len = (len - 1) | 0; | |
13176 | ||
13177 | // Peel one iteration (compiler can't do it, because of code complexity) | |
13178 | var a = self.words[0] | 0; | |
13179 | var b = num.words[0] | 0; | |
13180 | var r = a * b; | |
13181 | ||
13182 | var lo = r & 0x3ffffff; | |
13183 | var carry = (r / 0x4000000) | 0; | |
13184 | out.words[0] = lo; | |
13185 | ||
13186 | for (var k = 1; k < len; k++) { | |
13187 | // Sum all words with the same `i + j = k` and accumulate `ncarry`, | |
13188 | // note that ncarry could be >= 0x3ffffff | |
13189 | var ncarry = carry >>> 26; | |
13190 | var rword = carry & 0x3ffffff; | |
13191 | var maxJ = Math.min(k, num.length - 1); | |
13192 | for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { | |
13193 | var i = (k - j) | 0; | |
13194 | a = self.words[i] | 0; | |
13195 | b = num.words[j] | 0; | |
13196 | r = a * b + rword; | |
13197 | ncarry += (r / 0x4000000) | 0; | |
13198 | rword = r & 0x3ffffff; | |
13199 | } | |
13200 | out.words[k] = rword | 0; | |
13201 | carry = ncarry | 0; | |
13202 | } | |
13203 | if (carry !== 0) { | |
13204 | out.words[k] = carry | 0; | |
13205 | } else { | |
13206 | out.length--; | |
13207 | } | |
13208 | ||
13209 | return out.strip(); | |
13210 | } | |
13211 | ||
13212 | // TODO(indutny): it may be reasonable to omit it for users who don't need | |
13213 | // to work with 256-bit numbers, otherwise it gives 20% improvement for 256-bit | |
13214 | // multiplication (like elliptic secp256k1). | |
13215 | var comb10MulTo = function comb10MulTo (self, num, out) { | |
13216 | var a = self.words; | |
13217 | var b = num.words; | |
13218 | var o = out.words; | |
13219 | var c = 0; | |
13220 | var lo; | |
13221 | var mid; | |
13222 | var hi; | |
13223 | var a0 = a[0] | 0; | |
13224 | var al0 = a0 & 0x1fff; | |
13225 | var ah0 = a0 >>> 13; | |
13226 | var a1 = a[1] | 0; | |
13227 | var al1 = a1 & 0x1fff; | |
13228 | var ah1 = a1 >>> 13; | |
13229 | var a2 = a[2] | 0; | |
13230 | var al2 = a2 & 0x1fff; | |
13231 | var ah2 = a2 >>> 13; | |
13232 | var a3 = a[3] | 0; | |
13233 | var al3 = a3 & 0x1fff; | |
13234 | var ah3 = a3 >>> 13; | |
13235 | var a4 = a[4] | 0; | |
13236 | var al4 = a4 & 0x1fff; | |
13237 | var ah4 = a4 >>> 13; | |
13238 | var a5 = a[5] | 0; | |
13239 | var al5 = a5 & 0x1fff; | |
13240 | var ah5 = a5 >>> 13; | |
13241 | var a6 = a[6] | 0; | |
13242 | var al6 = a6 & 0x1fff; | |
13243 | var ah6 = a6 >>> 13; | |
13244 | var a7 = a[7] | 0; | |
13245 | var al7 = a7 & 0x1fff; | |
13246 | var ah7 = a7 >>> 13; | |
13247 | var a8 = a[8] | 0; | |
13248 | var al8 = a8 & 0x1fff; | |
13249 | var ah8 = a8 >>> 13; | |
13250 | var a9 = a[9] | 0; | |
13251 | var al9 = a9 & 0x1fff; | |
13252 | var ah9 = a9 >>> 13; | |
13253 | var b0 = b[0] | 0; | |
13254 | var bl0 = b0 & 0x1fff; | |
13255 | var bh0 = b0 >>> 13; | |
13256 | var b1 = b[1] | 0; | |
13257 | var bl1 = b1 & 0x1fff; | |
13258 | var bh1 = b1 >>> 13; | |
13259 | var b2 = b[2] | 0; | |
13260 | var bl2 = b2 & 0x1fff; | |
13261 | var bh2 = b2 >>> 13; | |
13262 | var b3 = b[3] | 0; | |
13263 | var bl3 = b3 & 0x1fff; | |
13264 | var bh3 = b3 >>> 13; | |
13265 | var b4 = b[4] | 0; | |
13266 | var bl4 = b4 & 0x1fff; | |
13267 | var bh4 = b4 >>> 13; | |
13268 | var b5 = b[5] | 0; | |
13269 | var bl5 = b5 & 0x1fff; | |
13270 | var bh5 = b5 >>> 13; | |
13271 | var b6 = b[6] | 0; | |
13272 | var bl6 = b6 & 0x1fff; | |
13273 | var bh6 = b6 >>> 13; | |
13274 | var b7 = b[7] | 0; | |
13275 | var bl7 = b7 & 0x1fff; | |
13276 | var bh7 = b7 >>> 13; | |
13277 | var b8 = b[8] | 0; | |
13278 | var bl8 = b8 & 0x1fff; | |
13279 | var bh8 = b8 >>> 13; | |
13280 | var b9 = b[9] | 0; | |
13281 | var bl9 = b9 & 0x1fff; | |
13282 | var bh9 = b9 >>> 13; | |
13283 | ||
13284 | out.negative = self.negative ^ num.negative; | |
13285 | out.length = 19; | |
13286 | /* k = 0 */ | |
13287 | lo = Math.imul(al0, bl0); | |
13288 | mid = Math.imul(al0, bh0); | |
13289 | mid = (mid + Math.imul(ah0, bl0)) | 0; | |
13290 | hi = Math.imul(ah0, bh0); | |
13291 | var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13292 | c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0; | |
13293 | w0 &= 0x3ffffff; | |
13294 | /* k = 1 */ | |
13295 | lo = Math.imul(al1, bl0); | |
13296 | mid = Math.imul(al1, bh0); | |
13297 | mid = (mid + Math.imul(ah1, bl0)) | 0; | |
13298 | hi = Math.imul(ah1, bh0); | |
13299 | lo = (lo + Math.imul(al0, bl1)) | 0; | |
13300 | mid = (mid + Math.imul(al0, bh1)) | 0; | |
13301 | mid = (mid + Math.imul(ah0, bl1)) | 0; | |
13302 | hi = (hi + Math.imul(ah0, bh1)) | 0; | |
13303 | var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13304 | c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0; | |
13305 | w1 &= 0x3ffffff; | |
13306 | /* k = 2 */ | |
13307 | lo = Math.imul(al2, bl0); | |
13308 | mid = Math.imul(al2, bh0); | |
13309 | mid = (mid + Math.imul(ah2, bl0)) | 0; | |
13310 | hi = Math.imul(ah2, bh0); | |
13311 | lo = (lo + Math.imul(al1, bl1)) | 0; | |
13312 | mid = (mid + Math.imul(al1, bh1)) | 0; | |
13313 | mid = (mid + Math.imul(ah1, bl1)) | 0; | |
13314 | hi = (hi + Math.imul(ah1, bh1)) | 0; | |
13315 | lo = (lo + Math.imul(al0, bl2)) | 0; | |
13316 | mid = (mid + Math.imul(al0, bh2)) | 0; | |
13317 | mid = (mid + Math.imul(ah0, bl2)) | 0; | |
13318 | hi = (hi + Math.imul(ah0, bh2)) | 0; | |
13319 | var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13320 | c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0; | |
13321 | w2 &= 0x3ffffff; | |
13322 | /* k = 3 */ | |
13323 | lo = Math.imul(al3, bl0); | |
13324 | mid = Math.imul(al3, bh0); | |
13325 | mid = (mid + Math.imul(ah3, bl0)) | 0; | |
13326 | hi = Math.imul(ah3, bh0); | |
13327 | lo = (lo + Math.imul(al2, bl1)) | 0; | |
13328 | mid = (mid + Math.imul(al2, bh1)) | 0; | |
13329 | mid = (mid + Math.imul(ah2, bl1)) | 0; | |
13330 | hi = (hi + Math.imul(ah2, bh1)) | 0; | |
13331 | lo = (lo + Math.imul(al1, bl2)) | 0; | |
13332 | mid = (mid + Math.imul(al1, bh2)) | 0; | |
13333 | mid = (mid + Math.imul(ah1, bl2)) | 0; | |
13334 | hi = (hi + Math.imul(ah1, bh2)) | 0; | |
13335 | lo = (lo + Math.imul(al0, bl3)) | 0; | |
13336 | mid = (mid + Math.imul(al0, bh3)) | 0; | |
13337 | mid = (mid + Math.imul(ah0, bl3)) | 0; | |
13338 | hi = (hi + Math.imul(ah0, bh3)) | 0; | |
13339 | var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13340 | c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0; | |
13341 | w3 &= 0x3ffffff; | |
13342 | /* k = 4 */ | |
13343 | lo = Math.imul(al4, bl0); | |
13344 | mid = Math.imul(al4, bh0); | |
13345 | mid = (mid + Math.imul(ah4, bl0)) | 0; | |
13346 | hi = Math.imul(ah4, bh0); | |
13347 | lo = (lo + Math.imul(al3, bl1)) | 0; | |
13348 | mid = (mid + Math.imul(al3, bh1)) | 0; | |
13349 | mid = (mid + Math.imul(ah3, bl1)) | 0; | |
13350 | hi = (hi + Math.imul(ah3, bh1)) | 0; | |
13351 | lo = (lo + Math.imul(al2, bl2)) | 0; | |
13352 | mid = (mid + Math.imul(al2, bh2)) | 0; | |
13353 | mid = (mid + Math.imul(ah2, bl2)) | 0; | |
13354 | hi = (hi + Math.imul(ah2, bh2)) | 0; | |
13355 | lo = (lo + Math.imul(al1, bl3)) | 0; | |
13356 | mid = (mid + Math.imul(al1, bh3)) | 0; | |
13357 | mid = (mid + Math.imul(ah1, bl3)) | 0; | |
13358 | hi = (hi + Math.imul(ah1, bh3)) | 0; | |
13359 | lo = (lo + Math.imul(al0, bl4)) | 0; | |
13360 | mid = (mid + Math.imul(al0, bh4)) | 0; | |
13361 | mid = (mid + Math.imul(ah0, bl4)) | 0; | |
13362 | hi = (hi + Math.imul(ah0, bh4)) | 0; | |
13363 | var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13364 | c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0; | |
13365 | w4 &= 0x3ffffff; | |
13366 | /* k = 5 */ | |
13367 | lo = Math.imul(al5, bl0); | |
13368 | mid = Math.imul(al5, bh0); | |
13369 | mid = (mid + Math.imul(ah5, bl0)) | 0; | |
13370 | hi = Math.imul(ah5, bh0); | |
13371 | lo = (lo + Math.imul(al4, bl1)) | 0; | |
13372 | mid = (mid + Math.imul(al4, bh1)) | 0; | |
13373 | mid = (mid + Math.imul(ah4, bl1)) | 0; | |
13374 | hi = (hi + Math.imul(ah4, bh1)) | 0; | |
13375 | lo = (lo + Math.imul(al3, bl2)) | 0; | |
13376 | mid = (mid + Math.imul(al3, bh2)) | 0; | |
13377 | mid = (mid + Math.imul(ah3, bl2)) | 0; | |
13378 | hi = (hi + Math.imul(ah3, bh2)) | 0; | |
13379 | lo = (lo + Math.imul(al2, bl3)) | 0; | |
13380 | mid = (mid + Math.imul(al2, bh3)) | 0; | |
13381 | mid = (mid + Math.imul(ah2, bl3)) | 0; | |
13382 | hi = (hi + Math.imul(ah2, bh3)) | 0; | |
13383 | lo = (lo + Math.imul(al1, bl4)) | 0; | |
13384 | mid = (mid + Math.imul(al1, bh4)) | 0; | |
13385 | mid = (mid + Math.imul(ah1, bl4)) | 0; | |
13386 | hi = (hi + Math.imul(ah1, bh4)) | 0; | |
13387 | lo = (lo + Math.imul(al0, bl5)) | 0; | |
13388 | mid = (mid + Math.imul(al0, bh5)) | 0; | |
13389 | mid = (mid + Math.imul(ah0, bl5)) | 0; | |
13390 | hi = (hi + Math.imul(ah0, bh5)) | 0; | |
13391 | var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13392 | c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0; | |
13393 | w5 &= 0x3ffffff; | |
13394 | /* k = 6 */ | |
13395 | lo = Math.imul(al6, bl0); | |
13396 | mid = Math.imul(al6, bh0); | |
13397 | mid = (mid + Math.imul(ah6, bl0)) | 0; | |
13398 | hi = Math.imul(ah6, bh0); | |
13399 | lo = (lo + Math.imul(al5, bl1)) | 0; | |
13400 | mid = (mid + Math.imul(al5, bh1)) | 0; | |
13401 | mid = (mid + Math.imul(ah5, bl1)) | 0; | |
13402 | hi = (hi + Math.imul(ah5, bh1)) | 0; | |
13403 | lo = (lo + Math.imul(al4, bl2)) | 0; | |
13404 | mid = (mid + Math.imul(al4, bh2)) | 0; | |
13405 | mid = (mid + Math.imul(ah4, bl2)) | 0; | |
13406 | hi = (hi + Math.imul(ah4, bh2)) | 0; | |
13407 | lo = (lo + Math.imul(al3, bl3)) | 0; | |
13408 | mid = (mid + Math.imul(al3, bh3)) | 0; | |
13409 | mid = (mid + Math.imul(ah3, bl3)) | 0; | |
13410 | hi = (hi + Math.imul(ah3, bh3)) | 0; | |
13411 | lo = (lo + Math.imul(al2, bl4)) | 0; | |
13412 | mid = (mid + Math.imul(al2, bh4)) | 0; | |
13413 | mid = (mid + Math.imul(ah2, bl4)) | 0; | |
13414 | hi = (hi + Math.imul(ah2, bh4)) | 0; | |
13415 | lo = (lo + Math.imul(al1, bl5)) | 0; | |
13416 | mid = (mid + Math.imul(al1, bh5)) | 0; | |
13417 | mid = (mid + Math.imul(ah1, bl5)) | 0; | |
13418 | hi = (hi + Math.imul(ah1, bh5)) | 0; | |
13419 | lo = (lo + Math.imul(al0, bl6)) | 0; | |
13420 | mid = (mid + Math.imul(al0, bh6)) | 0; | |
13421 | mid = (mid + Math.imul(ah0, bl6)) | 0; | |
13422 | hi = (hi + Math.imul(ah0, bh6)) | 0; | |
13423 | var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13424 | c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0; | |
13425 | w6 &= 0x3ffffff; | |
13426 | /* k = 7 */ | |
13427 | lo = Math.imul(al7, bl0); | |
13428 | mid = Math.imul(al7, bh0); | |
13429 | mid = (mid + Math.imul(ah7, bl0)) | 0; | |
13430 | hi = Math.imul(ah7, bh0); | |
13431 | lo = (lo + Math.imul(al6, bl1)) | 0; | |
13432 | mid = (mid + Math.imul(al6, bh1)) | 0; | |
13433 | mid = (mid + Math.imul(ah6, bl1)) | 0; | |
13434 | hi = (hi + Math.imul(ah6, bh1)) | 0; | |
13435 | lo = (lo + Math.imul(al5, bl2)) | 0; | |
13436 | mid = (mid + Math.imul(al5, bh2)) | 0; | |
13437 | mid = (mid + Math.imul(ah5, bl2)) | 0; | |
13438 | hi = (hi + Math.imul(ah5, bh2)) | 0; | |
13439 | lo = (lo + Math.imul(al4, bl3)) | 0; | |
13440 | mid = (mid + Math.imul(al4, bh3)) | 0; | |
13441 | mid = (mid + Math.imul(ah4, bl3)) | 0; | |
13442 | hi = (hi + Math.imul(ah4, bh3)) | 0; | |
13443 | lo = (lo + Math.imul(al3, bl4)) | 0; | |
13444 | mid = (mid + Math.imul(al3, bh4)) | 0; | |
13445 | mid = (mid + Math.imul(ah3, bl4)) | 0; | |
13446 | hi = (hi + Math.imul(ah3, bh4)) | 0; | |
13447 | lo = (lo + Math.imul(al2, bl5)) | 0; | |
13448 | mid = (mid + Math.imul(al2, bh5)) | 0; | |
13449 | mid = (mid + Math.imul(ah2, bl5)) | 0; | |
13450 | hi = (hi + Math.imul(ah2, bh5)) | 0; | |
13451 | lo = (lo + Math.imul(al1, bl6)) | 0; | |
13452 | mid = (mid + Math.imul(al1, bh6)) | 0; | |
13453 | mid = (mid + Math.imul(ah1, bl6)) | 0; | |
13454 | hi = (hi + Math.imul(ah1, bh6)) | 0; | |
13455 | lo = (lo + Math.imul(al0, bl7)) | 0; | |
13456 | mid = (mid + Math.imul(al0, bh7)) | 0; | |
13457 | mid = (mid + Math.imul(ah0, bl7)) | 0; | |
13458 | hi = (hi + Math.imul(ah0, bh7)) | 0; | |
13459 | var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13460 | c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0; | |
13461 | w7 &= 0x3ffffff; | |
13462 | /* k = 8 */ | |
13463 | lo = Math.imul(al8, bl0); | |
13464 | mid = Math.imul(al8, bh0); | |
13465 | mid = (mid + Math.imul(ah8, bl0)) | 0; | |
13466 | hi = Math.imul(ah8, bh0); | |
13467 | lo = (lo + Math.imul(al7, bl1)) | 0; | |
13468 | mid = (mid + Math.imul(al7, bh1)) | 0; | |
13469 | mid = (mid + Math.imul(ah7, bl1)) | 0; | |
13470 | hi = (hi + Math.imul(ah7, bh1)) | 0; | |
13471 | lo = (lo + Math.imul(al6, bl2)) | 0; | |
13472 | mid = (mid + Math.imul(al6, bh2)) | 0; | |
13473 | mid = (mid + Math.imul(ah6, bl2)) | 0; | |
13474 | hi = (hi + Math.imul(ah6, bh2)) | 0; | |
13475 | lo = (lo + Math.imul(al5, bl3)) | 0; | |
13476 | mid = (mid + Math.imul(al5, bh3)) | 0; | |
13477 | mid = (mid + Math.imul(ah5, bl3)) | 0; | |
13478 | hi = (hi + Math.imul(ah5, bh3)) | 0; | |
13479 | lo = (lo + Math.imul(al4, bl4)) | 0; | |
13480 | mid = (mid + Math.imul(al4, bh4)) | 0; | |
13481 | mid = (mid + Math.imul(ah4, bl4)) | 0; | |
13482 | hi = (hi + Math.imul(ah4, bh4)) | 0; | |
13483 | lo = (lo + Math.imul(al3, bl5)) | 0; | |
13484 | mid = (mid + Math.imul(al3, bh5)) | 0; | |
13485 | mid = (mid + Math.imul(ah3, bl5)) | 0; | |
13486 | hi = (hi + Math.imul(ah3, bh5)) | 0; | |
13487 | lo = (lo + Math.imul(al2, bl6)) | 0; | |
13488 | mid = (mid + Math.imul(al2, bh6)) | 0; | |
13489 | mid = (mid + Math.imul(ah2, bl6)) | 0; | |
13490 | hi = (hi + Math.imul(ah2, bh6)) | 0; | |
13491 | lo = (lo + Math.imul(al1, bl7)) | 0; | |
13492 | mid = (mid + Math.imul(al1, bh7)) | 0; | |
13493 | mid = (mid + Math.imul(ah1, bl7)) | 0; | |
13494 | hi = (hi + Math.imul(ah1, bh7)) | 0; | |
13495 | lo = (lo + Math.imul(al0, bl8)) | 0; | |
13496 | mid = (mid + Math.imul(al0, bh8)) | 0; | |
13497 | mid = (mid + Math.imul(ah0, bl8)) | 0; | |
13498 | hi = (hi + Math.imul(ah0, bh8)) | 0; | |
13499 | var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13500 | c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0; | |
13501 | w8 &= 0x3ffffff; | |
13502 | /* k = 9 */ | |
13503 | lo = Math.imul(al9, bl0); | |
13504 | mid = Math.imul(al9, bh0); | |
13505 | mid = (mid + Math.imul(ah9, bl0)) | 0; | |
13506 | hi = Math.imul(ah9, bh0); | |
13507 | lo = (lo + Math.imul(al8, bl1)) | 0; | |
13508 | mid = (mid + Math.imul(al8, bh1)) | 0; | |
13509 | mid = (mid + Math.imul(ah8, bl1)) | 0; | |
13510 | hi = (hi + Math.imul(ah8, bh1)) | 0; | |
13511 | lo = (lo + Math.imul(al7, bl2)) | 0; | |
13512 | mid = (mid + Math.imul(al7, bh2)) | 0; | |
13513 | mid = (mid + Math.imul(ah7, bl2)) | 0; | |
13514 | hi = (hi + Math.imul(ah7, bh2)) | 0; | |
13515 | lo = (lo + Math.imul(al6, bl3)) | 0; | |
13516 | mid = (mid + Math.imul(al6, bh3)) | 0; | |
13517 | mid = (mid + Math.imul(ah6, bl3)) | 0; | |
13518 | hi = (hi + Math.imul(ah6, bh3)) | 0; | |
13519 | lo = (lo + Math.imul(al5, bl4)) | 0; | |
13520 | mid = (mid + Math.imul(al5, bh4)) | 0; | |
13521 | mid = (mid + Math.imul(ah5, bl4)) | 0; | |
13522 | hi = (hi + Math.imul(ah5, bh4)) | 0; | |
13523 | lo = (lo + Math.imul(al4, bl5)) | 0; | |
13524 | mid = (mid + Math.imul(al4, bh5)) | 0; | |
13525 | mid = (mid + Math.imul(ah4, bl5)) | 0; | |
13526 | hi = (hi + Math.imul(ah4, bh5)) | 0; | |
13527 | lo = (lo + Math.imul(al3, bl6)) | 0; | |
13528 | mid = (mid + Math.imul(al3, bh6)) | 0; | |
13529 | mid = (mid + Math.imul(ah3, bl6)) | 0; | |
13530 | hi = (hi + Math.imul(ah3, bh6)) | 0; | |
13531 | lo = (lo + Math.imul(al2, bl7)) | 0; | |
13532 | mid = (mid + Math.imul(al2, bh7)) | 0; | |
13533 | mid = (mid + Math.imul(ah2, bl7)) | 0; | |
13534 | hi = (hi + Math.imul(ah2, bh7)) | 0; | |
13535 | lo = (lo + Math.imul(al1, bl8)) | 0; | |
13536 | mid = (mid + Math.imul(al1, bh8)) | 0; | |
13537 | mid = (mid + Math.imul(ah1, bl8)) | 0; | |
13538 | hi = (hi + Math.imul(ah1, bh8)) | 0; | |
13539 | lo = (lo + Math.imul(al0, bl9)) | 0; | |
13540 | mid = (mid + Math.imul(al0, bh9)) | 0; | |
13541 | mid = (mid + Math.imul(ah0, bl9)) | 0; | |
13542 | hi = (hi + Math.imul(ah0, bh9)) | 0; | |
13543 | var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13544 | c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0; | |
13545 | w9 &= 0x3ffffff; | |
13546 | /* k = 10 */ | |
13547 | lo = Math.imul(al9, bl1); | |
13548 | mid = Math.imul(al9, bh1); | |
13549 | mid = (mid + Math.imul(ah9, bl1)) | 0; | |
13550 | hi = Math.imul(ah9, bh1); | |
13551 | lo = (lo + Math.imul(al8, bl2)) | 0; | |
13552 | mid = (mid + Math.imul(al8, bh2)) | 0; | |
13553 | mid = (mid + Math.imul(ah8, bl2)) | 0; | |
13554 | hi = (hi + Math.imul(ah8, bh2)) | 0; | |
13555 | lo = (lo + Math.imul(al7, bl3)) | 0; | |
13556 | mid = (mid + Math.imul(al7, bh3)) | 0; | |
13557 | mid = (mid + Math.imul(ah7, bl3)) | 0; | |
13558 | hi = (hi + Math.imul(ah7, bh3)) | 0; | |
13559 | lo = (lo + Math.imul(al6, bl4)) | 0; | |
13560 | mid = (mid + Math.imul(al6, bh4)) | 0; | |
13561 | mid = (mid + Math.imul(ah6, bl4)) | 0; | |
13562 | hi = (hi + Math.imul(ah6, bh4)) | 0; | |
13563 | lo = (lo + Math.imul(al5, bl5)) | 0; | |
13564 | mid = (mid + Math.imul(al5, bh5)) | 0; | |
13565 | mid = (mid + Math.imul(ah5, bl5)) | 0; | |
13566 | hi = (hi + Math.imul(ah5, bh5)) | 0; | |
13567 | lo = (lo + Math.imul(al4, bl6)) | 0; | |
13568 | mid = (mid + Math.imul(al4, bh6)) | 0; | |
13569 | mid = (mid + Math.imul(ah4, bl6)) | 0; | |
13570 | hi = (hi + Math.imul(ah4, bh6)) | 0; | |
13571 | lo = (lo + Math.imul(al3, bl7)) | 0; | |
13572 | mid = (mid + Math.imul(al3, bh7)) | 0; | |
13573 | mid = (mid + Math.imul(ah3, bl7)) | 0; | |
13574 | hi = (hi + Math.imul(ah3, bh7)) | 0; | |
13575 | lo = (lo + Math.imul(al2, bl8)) | 0; | |
13576 | mid = (mid + Math.imul(al2, bh8)) | 0; | |
13577 | mid = (mid + Math.imul(ah2, bl8)) | 0; | |
13578 | hi = (hi + Math.imul(ah2, bh8)) | 0; | |
13579 | lo = (lo + Math.imul(al1, bl9)) | 0; | |
13580 | mid = (mid + Math.imul(al1, bh9)) | 0; | |
13581 | mid = (mid + Math.imul(ah1, bl9)) | 0; | |
13582 | hi = (hi + Math.imul(ah1, bh9)) | 0; | |
13583 | var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13584 | c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0; | |
13585 | w10 &= 0x3ffffff; | |
13586 | /* k = 11 */ | |
13587 | lo = Math.imul(al9, bl2); | |
13588 | mid = Math.imul(al9, bh2); | |
13589 | mid = (mid + Math.imul(ah9, bl2)) | 0; | |
13590 | hi = Math.imul(ah9, bh2); | |
13591 | lo = (lo + Math.imul(al8, bl3)) | 0; | |
13592 | mid = (mid + Math.imul(al8, bh3)) | 0; | |
13593 | mid = (mid + Math.imul(ah8, bl3)) | 0; | |
13594 | hi = (hi + Math.imul(ah8, bh3)) | 0; | |
13595 | lo = (lo + Math.imul(al7, bl4)) | 0; | |
13596 | mid = (mid + Math.imul(al7, bh4)) | 0; | |
13597 | mid = (mid + Math.imul(ah7, bl4)) | 0; | |
13598 | hi = (hi + Math.imul(ah7, bh4)) | 0; | |
13599 | lo = (lo + Math.imul(al6, bl5)) | 0; | |
13600 | mid = (mid + Math.imul(al6, bh5)) | 0; | |
13601 | mid = (mid + Math.imul(ah6, bl5)) | 0; | |
13602 | hi = (hi + Math.imul(ah6, bh5)) | 0; | |
13603 | lo = (lo + Math.imul(al5, bl6)) | 0; | |
13604 | mid = (mid + Math.imul(al5, bh6)) | 0; | |
13605 | mid = (mid + Math.imul(ah5, bl6)) | 0; | |
13606 | hi = (hi + Math.imul(ah5, bh6)) | 0; | |
13607 | lo = (lo + Math.imul(al4, bl7)) | 0; | |
13608 | mid = (mid + Math.imul(al4, bh7)) | 0; | |
13609 | mid = (mid + Math.imul(ah4, bl7)) | 0; | |
13610 | hi = (hi + Math.imul(ah4, bh7)) | 0; | |
13611 | lo = (lo + Math.imul(al3, bl8)) | 0; | |
13612 | mid = (mid + Math.imul(al3, bh8)) | 0; | |
13613 | mid = (mid + Math.imul(ah3, bl8)) | 0; | |
13614 | hi = (hi + Math.imul(ah3, bh8)) | 0; | |
13615 | lo = (lo + Math.imul(al2, bl9)) | 0; | |
13616 | mid = (mid + Math.imul(al2, bh9)) | 0; | |
13617 | mid = (mid + Math.imul(ah2, bl9)) | 0; | |
13618 | hi = (hi + Math.imul(ah2, bh9)) | 0; | |
13619 | var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13620 | c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0; | |
13621 | w11 &= 0x3ffffff; | |
13622 | /* k = 12 */ | |
13623 | lo = Math.imul(al9, bl3); | |
13624 | mid = Math.imul(al9, bh3); | |
13625 | mid = (mid + Math.imul(ah9, bl3)) | 0; | |
13626 | hi = Math.imul(ah9, bh3); | |
13627 | lo = (lo + Math.imul(al8, bl4)) | 0; | |
13628 | mid = (mid + Math.imul(al8, bh4)) | 0; | |
13629 | mid = (mid + Math.imul(ah8, bl4)) | 0; | |
13630 | hi = (hi + Math.imul(ah8, bh4)) | 0; | |
13631 | lo = (lo + Math.imul(al7, bl5)) | 0; | |
13632 | mid = (mid + Math.imul(al7, bh5)) | 0; | |
13633 | mid = (mid + Math.imul(ah7, bl5)) | 0; | |
13634 | hi = (hi + Math.imul(ah7, bh5)) | 0; | |
13635 | lo = (lo + Math.imul(al6, bl6)) | 0; | |
13636 | mid = (mid + Math.imul(al6, bh6)) | 0; | |
13637 | mid = (mid + Math.imul(ah6, bl6)) | 0; | |
13638 | hi = (hi + Math.imul(ah6, bh6)) | 0; | |
13639 | lo = (lo + Math.imul(al5, bl7)) | 0; | |
13640 | mid = (mid + Math.imul(al5, bh7)) | 0; | |
13641 | mid = (mid + Math.imul(ah5, bl7)) | 0; | |
13642 | hi = (hi + Math.imul(ah5, bh7)) | 0; | |
13643 | lo = (lo + Math.imul(al4, bl8)) | 0; | |
13644 | mid = (mid + Math.imul(al4, bh8)) | 0; | |
13645 | mid = (mid + Math.imul(ah4, bl8)) | 0; | |
13646 | hi = (hi + Math.imul(ah4, bh8)) | 0; | |
13647 | lo = (lo + Math.imul(al3, bl9)) | 0; | |
13648 | mid = (mid + Math.imul(al3, bh9)) | 0; | |
13649 | mid = (mid + Math.imul(ah3, bl9)) | 0; | |
13650 | hi = (hi + Math.imul(ah3, bh9)) | 0; | |
13651 | var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13652 | c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0; | |
13653 | w12 &= 0x3ffffff; | |
13654 | /* k = 13 */ | |
13655 | lo = Math.imul(al9, bl4); | |
13656 | mid = Math.imul(al9, bh4); | |
13657 | mid = (mid + Math.imul(ah9, bl4)) | 0; | |
13658 | hi = Math.imul(ah9, bh4); | |
13659 | lo = (lo + Math.imul(al8, bl5)) | 0; | |
13660 | mid = (mid + Math.imul(al8, bh5)) | 0; | |
13661 | mid = (mid + Math.imul(ah8, bl5)) | 0; | |
13662 | hi = (hi + Math.imul(ah8, bh5)) | 0; | |
13663 | lo = (lo + Math.imul(al7, bl6)) | 0; | |
13664 | mid = (mid + Math.imul(al7, bh6)) | 0; | |
13665 | mid = (mid + Math.imul(ah7, bl6)) | 0; | |
13666 | hi = (hi + Math.imul(ah7, bh6)) | 0; | |
13667 | lo = (lo + Math.imul(al6, bl7)) | 0; | |
13668 | mid = (mid + Math.imul(al6, bh7)) | 0; | |
13669 | mid = (mid + Math.imul(ah6, bl7)) | 0; | |
13670 | hi = (hi + Math.imul(ah6, bh7)) | 0; | |
13671 | lo = (lo + Math.imul(al5, bl8)) | 0; | |
13672 | mid = (mid + Math.imul(al5, bh8)) | 0; | |
13673 | mid = (mid + Math.imul(ah5, bl8)) | 0; | |
13674 | hi = (hi + Math.imul(ah5, bh8)) | 0; | |
13675 | lo = (lo + Math.imul(al4, bl9)) | 0; | |
13676 | mid = (mid + Math.imul(al4, bh9)) | 0; | |
13677 | mid = (mid + Math.imul(ah4, bl9)) | 0; | |
13678 | hi = (hi + Math.imul(ah4, bh9)) | 0; | |
13679 | var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13680 | c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0; | |
13681 | w13 &= 0x3ffffff; | |
13682 | /* k = 14 */ | |
13683 | lo = Math.imul(al9, bl5); | |
13684 | mid = Math.imul(al9, bh5); | |
13685 | mid = (mid + Math.imul(ah9, bl5)) | 0; | |
13686 | hi = Math.imul(ah9, bh5); | |
13687 | lo = (lo + Math.imul(al8, bl6)) | 0; | |
13688 | mid = (mid + Math.imul(al8, bh6)) | 0; | |
13689 | mid = (mid + Math.imul(ah8, bl6)) | 0; | |
13690 | hi = (hi + Math.imul(ah8, bh6)) | 0; | |
13691 | lo = (lo + Math.imul(al7, bl7)) | 0; | |
13692 | mid = (mid + Math.imul(al7, bh7)) | 0; | |
13693 | mid = (mid + Math.imul(ah7, bl7)) | 0; | |
13694 | hi = (hi + Math.imul(ah7, bh7)) | 0; | |
13695 | lo = (lo + Math.imul(al6, bl8)) | 0; | |
13696 | mid = (mid + Math.imul(al6, bh8)) | 0; | |
13697 | mid = (mid + Math.imul(ah6, bl8)) | 0; | |
13698 | hi = (hi + Math.imul(ah6, bh8)) | 0; | |
13699 | lo = (lo + Math.imul(al5, bl9)) | 0; | |
13700 | mid = (mid + Math.imul(al5, bh9)) | 0; | |
13701 | mid = (mid + Math.imul(ah5, bl9)) | 0; | |
13702 | hi = (hi + Math.imul(ah5, bh9)) | 0; | |
13703 | var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13704 | c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0; | |
13705 | w14 &= 0x3ffffff; | |
13706 | /* k = 15 */ | |
13707 | lo = Math.imul(al9, bl6); | |
13708 | mid = Math.imul(al9, bh6); | |
13709 | mid = (mid + Math.imul(ah9, bl6)) | 0; | |
13710 | hi = Math.imul(ah9, bh6); | |
13711 | lo = (lo + Math.imul(al8, bl7)) | 0; | |
13712 | mid = (mid + Math.imul(al8, bh7)) | 0; | |
13713 | mid = (mid + Math.imul(ah8, bl7)) | 0; | |
13714 | hi = (hi + Math.imul(ah8, bh7)) | 0; | |
13715 | lo = (lo + Math.imul(al7, bl8)) | 0; | |
13716 | mid = (mid + Math.imul(al7, bh8)) | 0; | |
13717 | mid = (mid + Math.imul(ah7, bl8)) | 0; | |
13718 | hi = (hi + Math.imul(ah7, bh8)) | 0; | |
13719 | lo = (lo + Math.imul(al6, bl9)) | 0; | |
13720 | mid = (mid + Math.imul(al6, bh9)) | 0; | |
13721 | mid = (mid + Math.imul(ah6, bl9)) | 0; | |
13722 | hi = (hi + Math.imul(ah6, bh9)) | 0; | |
13723 | var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13724 | c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0; | |
13725 | w15 &= 0x3ffffff; | |
13726 | /* k = 16 */ | |
13727 | lo = Math.imul(al9, bl7); | |
13728 | mid = Math.imul(al9, bh7); | |
13729 | mid = (mid + Math.imul(ah9, bl7)) | 0; | |
13730 | hi = Math.imul(ah9, bh7); | |
13731 | lo = (lo + Math.imul(al8, bl8)) | 0; | |
13732 | mid = (mid + Math.imul(al8, bh8)) | 0; | |
13733 | mid = (mid + Math.imul(ah8, bl8)) | 0; | |
13734 | hi = (hi + Math.imul(ah8, bh8)) | 0; | |
13735 | lo = (lo + Math.imul(al7, bl9)) | 0; | |
13736 | mid = (mid + Math.imul(al7, bh9)) | 0; | |
13737 | mid = (mid + Math.imul(ah7, bl9)) | 0; | |
13738 | hi = (hi + Math.imul(ah7, bh9)) | 0; | |
13739 | var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13740 | c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0; | |
13741 | w16 &= 0x3ffffff; | |
13742 | /* k = 17 */ | |
13743 | lo = Math.imul(al9, bl8); | |
13744 | mid = Math.imul(al9, bh8); | |
13745 | mid = (mid + Math.imul(ah9, bl8)) | 0; | |
13746 | hi = Math.imul(ah9, bh8); | |
13747 | lo = (lo + Math.imul(al8, bl9)) | 0; | |
13748 | mid = (mid + Math.imul(al8, bh9)) | 0; | |
13749 | mid = (mid + Math.imul(ah8, bl9)) | 0; | |
13750 | hi = (hi + Math.imul(ah8, bh9)) | 0; | |
13751 | var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13752 | c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0; | |
13753 | w17 &= 0x3ffffff; | |
13754 | /* k = 18 */ | |
13755 | lo = Math.imul(al9, bl9); | |
13756 | mid = Math.imul(al9, bh9); | |
13757 | mid = (mid + Math.imul(ah9, bl9)) | 0; | |
13758 | hi = Math.imul(ah9, bh9); | |
13759 | var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0; | |
13760 | c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0; | |
13761 | w18 &= 0x3ffffff; | |
13762 | o[0] = w0; | |
13763 | o[1] = w1; | |
13764 | o[2] = w2; | |
13765 | o[3] = w3; | |
13766 | o[4] = w4; | |
13767 | o[5] = w5; | |
13768 | o[6] = w6; | |
13769 | o[7] = w7; | |
13770 | o[8] = w8; | |
13771 | o[9] = w9; | |
13772 | o[10] = w10; | |
13773 | o[11] = w11; | |
13774 | o[12] = w12; | |
13775 | o[13] = w13; | |
13776 | o[14] = w14; | |
13777 | o[15] = w15; | |
13778 | o[16] = w16; | |
13779 | o[17] = w17; | |
13780 | o[18] = w18; | |
13781 | if (c !== 0) { | |
13782 | o[19] = c; | |
13783 | out.length++; | |
13784 | } | |
13785 | return out; | |
13786 | }; | |
13787 | ||
13788 | // Polyfill comb | |
13789 | if (!Math.imul) { | |
13790 | comb10MulTo = smallMulTo; | |
13791 | } | |
13792 | ||
13793 | function bigMulTo (self, num, out) { | |
13794 | out.negative = num.negative ^ self.negative; | |
13795 | out.length = self.length + num.length; | |
13796 | ||
13797 | var carry = 0; | |
13798 | var hncarry = 0; | |
13799 | for (var k = 0; k < out.length - 1; k++) { | |
13800 | // Sum all words with the same `i + j = k` and accumulate `ncarry`, | |
13801 | // note that ncarry could be >= 0x3ffffff | |
13802 | var ncarry = hncarry; | |
13803 | hncarry = 0; | |
13804 | var rword = carry & 0x3ffffff; | |
13805 | var maxJ = Math.min(k, num.length - 1); | |
13806 | for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) { | |
13807 | var i = k - j; | |
13808 | var a = self.words[i] | 0; | |
13809 | var b = num.words[j] | 0; | |
13810 | var r = a * b; | |
13811 | ||
13812 | var lo = r & 0x3ffffff; | |
13813 | ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0; | |
13814 | lo = (lo + rword) | 0; | |
13815 | rword = lo & 0x3ffffff; | |
13816 | ncarry = (ncarry + (lo >>> 26)) | 0; | |
13817 | ||
13818 | hncarry += ncarry >>> 26; | |
13819 | ncarry &= 0x3ffffff; | |
13820 | } | |
13821 | out.words[k] = rword; | |
13822 | carry = ncarry; | |
13823 | ncarry = hncarry; | |
13824 | } | |
13825 | if (carry !== 0) { | |
13826 | out.words[k] = carry; | |
13827 | } else { | |
13828 | out.length--; | |
13829 | } | |
13830 | ||
13831 | return out.strip(); | |
13832 | } | |
13833 | ||
13834 | function jumboMulTo (self, num, out) { | |
13835 | var fftm = new FFTM(); | |
13836 | return fftm.mulp(self, num, out); | |
13837 | } | |
13838 | ||
13839 | BN.prototype.mulTo = function mulTo (num, out) { | |
13840 | var res; | |
13841 | var len = this.length + num.length; | |
13842 | if (this.length === 10 && num.length === 10) { | |
13843 | res = comb10MulTo(this, num, out); | |
13844 | } else if (len < 63) { | |
13845 | res = smallMulTo(this, num, out); | |
13846 | } else if (len < 1024) { | |
13847 | res = bigMulTo(this, num, out); | |
13848 | } else { | |
13849 | res = jumboMulTo(this, num, out); | |
13850 | } | |
13851 | ||
13852 | return res; | |
13853 | }; | |
13854 | ||
13855 | // Cooley-Tukey algorithm for FFT | |
13856 | // slightly revisited to rely on looping instead of recursion | |
13857 | ||
13858 | function FFTM (x, y) { | |
13859 | this.x = x; | |
13860 | this.y = y; | |
13861 | } | |
13862 | ||
13863 | FFTM.prototype.makeRBT = function makeRBT (N) { | |
13864 | var t = new Array(N); | |
13865 | var l = BN.prototype._countBits(N) - 1; | |
13866 | for (var i = 0; i < N; i++) { | |
13867 | t[i] = this.revBin(i, l, N); | |
13868 | } | |
13869 | ||
13870 | return t; | |
13871 | }; | |
13872 | ||
13873 | // Returns binary-reversed representation of `x` | |
13874 | FFTM.prototype.revBin = function revBin (x, l, N) { | |
13875 | if (x === 0 || x === N - 1) return x; | |
13876 | ||
13877 | var rb = 0; | |
13878 | for (var i = 0; i < l; i++) { | |
13879 | rb |= (x & 1) << (l - i - 1); | |
13880 | x >>= 1; | |
13881 | } | |
13882 | ||
13883 | return rb; | |
13884 | }; | |
13885 | ||
13886 | // Performs "tweedling" phase, therefore 'emulating' | |
13887 | // behaviour of the recursive algorithm | |
13888 | FFTM.prototype.permute = function permute (rbt, rws, iws, rtws, itws, N) { | |
13889 | for (var i = 0; i < N; i++) { | |
13890 | rtws[i] = rws[rbt[i]]; | |
13891 | itws[i] = iws[rbt[i]]; | |
13892 | } | |
13893 | }; | |
13894 | ||
13895 | FFTM.prototype.transform = function transform (rws, iws, rtws, itws, N, rbt) { | |
13896 | this.permute(rbt, rws, iws, rtws, itws, N); | |
13897 | ||
13898 | for (var s = 1; s < N; s <<= 1) { | |
13899 | var l = s << 1; | |
13900 | ||
13901 | var rtwdf = Math.cos(2 * Math.PI / l); | |
13902 | var itwdf = Math.sin(2 * Math.PI / l); | |
13903 | ||
13904 | for (var p = 0; p < N; p += l) { | |
13905 | var rtwdf_ = rtwdf; | |
13906 | var itwdf_ = itwdf; | |
13907 | ||
13908 | for (var j = 0; j < s; j++) { | |
13909 | var re = rtws[p + j]; | |
13910 | var ie = itws[p + j]; | |
13911 | ||
13912 | var ro = rtws[p + j + s]; | |
13913 | var io = itws[p + j + s]; | |
13914 | ||
13915 | var rx = rtwdf_ * ro - itwdf_ * io; | |
13916 | ||
13917 | io = rtwdf_ * io + itwdf_ * ro; | |
13918 | ro = rx; | |
13919 | ||
13920 | rtws[p + j] = re + ro; | |
13921 | itws[p + j] = ie + io; | |
13922 | ||
13923 | rtws[p + j + s] = re - ro; | |
13924 | itws[p + j + s] = ie - io; | |
13925 | ||
13926 | /* jshint maxdepth : false */ | |
13927 | if (j !== l) { | |
13928 | rx = rtwdf * rtwdf_ - itwdf * itwdf_; | |
13929 | ||
13930 | itwdf_ = rtwdf * itwdf_ + itwdf * rtwdf_; | |
13931 | rtwdf_ = rx; | |
13932 | } | |
13933 | } | |
13934 | } | |
13935 | } | |
13936 | }; | |
13937 | ||
13938 | FFTM.prototype.guessLen13b = function guessLen13b (n, m) { | |
13939 | var N = Math.max(m, n) | 1; | |
13940 | var odd = N & 1; | |
13941 | var i = 0; | |
13942 | for (N = N / 2 | 0; N; N = N >>> 1) { | |
13943 | i++; | |
13944 | } | |
13945 | ||
13946 | return 1 << i + 1 + odd; | |
13947 | }; | |
13948 | ||
13949 | FFTM.prototype.conjugate = function conjugate (rws, iws, N) { | |
13950 | if (N <= 1) return; | |
13951 | ||
13952 | for (var i = 0; i < N / 2; i++) { | |
13953 | var t = rws[i]; | |
13954 | ||
13955 | rws[i] = rws[N - i - 1]; | |
13956 | rws[N - i - 1] = t; | |
13957 | ||
13958 | t = iws[i]; | |
13959 | ||
13960 | iws[i] = -iws[N - i - 1]; | |
13961 | iws[N - i - 1] = -t; | |
13962 | } | |
13963 | }; | |
13964 | ||
13965 | FFTM.prototype.normalize13b = function normalize13b (ws, N) { | |
13966 | var carry = 0; | |
13967 | for (var i = 0; i < N / 2; i++) { | |
13968 | var w = Math.round(ws[2 * i + 1] / N) * 0x2000 + | |
13969 | Math.round(ws[2 * i] / N) + | |
13970 | carry; | |
13971 | ||
13972 | ws[i] = w & 0x3ffffff; | |
13973 | ||
13974 | if (w < 0x4000000) { | |
13975 | carry = 0; | |
13976 | } else { | |
13977 | carry = w / 0x4000000 | 0; | |
13978 | } | |
13979 | } | |
13980 | ||
13981 | return ws; | |
13982 | }; | |
13983 | ||
13984 | FFTM.prototype.convert13b = function convert13b (ws, len, rws, N) { | |
13985 | var carry = 0; | |
13986 | for (var i = 0; i < len; i++) { | |
13987 | carry = carry + (ws[i] | 0); | |
13988 | ||
13989 | rws[2 * i] = carry & 0x1fff; carry = carry >>> 13; | |
13990 | rws[2 * i + 1] = carry & 0x1fff; carry = carry >>> 13; | |
13991 | } | |
13992 | ||
13993 | // Pad with zeroes | |
13994 | for (i = 2 * len; i < N; ++i) { | |
13995 | rws[i] = 0; | |
13996 | } | |
13997 | ||
13998 | assert(carry === 0); | |
13999 | assert((carry & ~0x1fff) === 0); | |
14000 | }; | |
14001 | ||
14002 | FFTM.prototype.stub = function stub (N) { | |
14003 | var ph = new Array(N); | |
14004 | for (var i = 0; i < N; i++) { | |
14005 | ph[i] = 0; | |
14006 | } | |
14007 | ||
14008 | return ph; | |
14009 | }; | |
14010 | ||
14011 | FFTM.prototype.mulp = function mulp (x, y, out) { | |
14012 | var N = 2 * this.guessLen13b(x.length, y.length); | |
14013 | ||
14014 | var rbt = this.makeRBT(N); | |
14015 | ||
14016 | var _ = this.stub(N); | |
14017 | ||
14018 | var rws = new Array(N); | |
14019 | var rwst = new Array(N); | |
14020 | var iwst = new Array(N); | |
14021 | ||
14022 | var nrws = new Array(N); | |
14023 | var nrwst = new Array(N); | |
14024 | var niwst = new Array(N); | |
14025 | ||
14026 | var rmws = out.words; | |
14027 | rmws.length = N; | |
14028 | ||
14029 | this.convert13b(x.words, x.length, rws, N); | |
14030 | this.convert13b(y.words, y.length, nrws, N); | |
14031 | ||
14032 | this.transform(rws, _, rwst, iwst, N, rbt); | |
14033 | this.transform(nrws, _, nrwst, niwst, N, rbt); | |
14034 | ||
14035 | for (var i = 0; i < N; i++) { | |
14036 | var rx = rwst[i] * nrwst[i] - iwst[i] * niwst[i]; | |
14037 | iwst[i] = rwst[i] * niwst[i] + iwst[i] * nrwst[i]; | |
14038 | rwst[i] = rx; | |
14039 | } | |
14040 | ||
14041 | this.conjugate(rwst, iwst, N); | |
14042 | this.transform(rwst, iwst, rmws, _, N, rbt); | |
14043 | this.conjugate(rmws, _, N); | |
14044 | this.normalize13b(rmws, N); | |
14045 | ||
14046 | out.negative = x.negative ^ y.negative; | |
14047 | out.length = x.length + y.length; | |
14048 | return out.strip(); | |
14049 | }; | |
14050 | ||
14051 | // Multiply `this` by `num` | |
14052 | BN.prototype.mul = function mul (num) { | |
14053 | var out = new BN(null); | |
14054 | out.words = new Array(this.length + num.length); | |
14055 | return this.mulTo(num, out); | |
14056 | }; | |
14057 | ||
14058 | // Multiply employing FFT | |
14059 | BN.prototype.mulf = function mulf (num) { | |
14060 | var out = new BN(null); | |
14061 | out.words = new Array(this.length + num.length); | |
14062 | return jumboMulTo(this, num, out); | |
14063 | }; | |
14064 | ||
14065 | // In-place Multiplication | |
14066 | BN.prototype.imul = function imul (num) { | |
14067 | return this.clone().mulTo(num, this); | |
14068 | }; | |
14069 | ||
14070 | BN.prototype.imuln = function imuln (num) { | |
14071 | assert(typeof num === 'number'); | |
14072 | assert(num < 0x4000000); | |
14073 | ||
14074 | // Carry | |
14075 | var carry = 0; | |
14076 | for (var i = 0; i < this.length; i++) { | |
14077 | var w = (this.words[i] | 0) * num; | |
14078 | var lo = (w & 0x3ffffff) + (carry & 0x3ffffff); | |
14079 | carry >>= 26; | |
14080 | carry += (w / 0x4000000) | 0; | |
14081 | // NOTE: lo is 27bit maximum | |
14082 | carry += lo >>> 26; | |
14083 | this.words[i] = lo & 0x3ffffff; | |
14084 | } | |
14085 | ||
14086 | if (carry !== 0) { | |
14087 | this.words[i] = carry; | |
14088 | this.length++; | |
14089 | } | |
14090 | ||
14091 | return this; | |
14092 | }; | |
14093 | ||
14094 | BN.prototype.muln = function muln (num) { | |
14095 | return this.clone().imuln(num); | |
14096 | }; | |
14097 | ||
14098 | // `this` * `this` | |
14099 | BN.prototype.sqr = function sqr () { | |
14100 | return this.mul(this); | |
14101 | }; | |
14102 | ||
14103 | // `this` * `this` in-place | |
14104 | BN.prototype.isqr = function isqr () { | |
14105 | return this.imul(this.clone()); | |
14106 | }; | |
14107 | ||
14108 | // Math.pow(`this`, `num`) | |
14109 | BN.prototype.pow = function pow (num) { | |
14110 | var w = toBitArray(num); | |
14111 | if (w.length === 0) return new BN(1); | |
14112 | ||
14113 | // Skip leading zeroes | |
14114 | var res = this; | |
14115 | for (var i = 0; i < w.length; i++, res = res.sqr()) { | |
14116 | if (w[i] !== 0) break; | |
14117 | } | |
14118 | ||
14119 | if (++i < w.length) { | |
14120 | for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) { | |
14121 | if (w[i] === 0) continue; | |
14122 | ||
14123 | res = res.mul(q); | |
14124 | } | |
14125 | } | |
14126 | ||
14127 | return res; | |
14128 | }; | |
14129 | ||
14130 | // Shift-left in-place | |
14131 | BN.prototype.iushln = function iushln (bits) { | |
14132 | assert(typeof bits === 'number' && bits >= 0); | |
14133 | var r = bits % 26; | |
14134 | var s = (bits - r) / 26; | |
14135 | var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r); | |
14136 | var i; | |
14137 | ||
14138 | if (r !== 0) { | |
14139 | var carry = 0; | |
14140 | ||
14141 | for (i = 0; i < this.length; i++) { | |
14142 | var newCarry = this.words[i] & carryMask; | |
14143 | var c = ((this.words[i] | 0) - newCarry) << r; | |
14144 | this.words[i] = c | carry; | |
14145 | carry = newCarry >>> (26 - r); | |
14146 | } | |
14147 | ||
14148 | if (carry) { | |
14149 | this.words[i] = carry; | |
14150 | this.length++; | |
14151 | } | |
14152 | } | |
14153 | ||
14154 | if (s !== 0) { | |
14155 | for (i = this.length - 1; i >= 0; i--) { | |
14156 | this.words[i + s] = this.words[i]; | |
14157 | } | |
14158 | ||
14159 | for (i = 0; i < s; i++) { | |
14160 | this.words[i] = 0; | |
14161 | } | |
14162 | ||
14163 | this.length += s; | |
14164 | } | |
14165 | ||
14166 | return this.strip(); | |
14167 | }; | |
14168 | ||
14169 | BN.prototype.ishln = function ishln (bits) { | |
14170 | // TODO(indutny): implement me | |
14171 | assert(this.negative === 0); | |
14172 | return this.iushln(bits); | |
14173 | }; | |
14174 | ||
14175 | // Shift-right in-place | |
14176 | // NOTE: `hint` is a lowest bit before trailing zeroes | |
14177 | // NOTE: if `extended` is present - it will be filled with destroyed bits | |
14178 | BN.prototype.iushrn = function iushrn (bits, hint, extended) { | |
14179 | assert(typeof bits === 'number' && bits >= 0); | |
14180 | var h; | |
14181 | if (hint) { | |
14182 | h = (hint - (hint % 26)) / 26; | |
14183 | } else { | |
14184 | h = 0; | |
14185 | } | |
14186 | ||
14187 | var r = bits % 26; | |
14188 | var s = Math.min((bits - r) / 26, this.length); | |
14189 | var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); | |
14190 | var maskedWords = extended; | |
14191 | ||
14192 | h -= s; | |
14193 | h = Math.max(0, h); | |
14194 | ||
14195 | // Extended mode, copy masked part | |
14196 | if (maskedWords) { | |
14197 | for (var i = 0; i < s; i++) { | |
14198 | maskedWords.words[i] = this.words[i]; | |
14199 | } | |
14200 | maskedWords.length = s; | |
14201 | } | |
14202 | ||
14203 | if (s === 0) { | |
14204 | // No-op, we should not move anything at all | |
14205 | } else if (this.length > s) { | |
14206 | this.length -= s; | |
14207 | for (i = 0; i < this.length; i++) { | |
14208 | this.words[i] = this.words[i + s]; | |
14209 | } | |
14210 | } else { | |
14211 | this.words[0] = 0; | |
14212 | this.length = 1; | |
14213 | } | |
14214 | ||
14215 | var carry = 0; | |
14216 | for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) { | |
14217 | var word = this.words[i] | 0; | |
14218 | this.words[i] = (carry << (26 - r)) | (word >>> r); | |
14219 | carry = word & mask; | |
14220 | } | |
14221 | ||
14222 | // Push carried bits as a mask | |
14223 | if (maskedWords && carry !== 0) { | |
14224 | maskedWords.words[maskedWords.length++] = carry; | |
14225 | } | |
14226 | ||
14227 | if (this.length === 0) { | |
14228 | this.words[0] = 0; | |
14229 | this.length = 1; | |
14230 | } | |
14231 | ||
14232 | return this.strip(); | |
14233 | }; | |
14234 | ||
14235 | BN.prototype.ishrn = function ishrn (bits, hint, extended) { | |
14236 | // TODO(indutny): implement me | |
14237 | assert(this.negative === 0); | |
14238 | return this.iushrn(bits, hint, extended); | |
14239 | }; | |
14240 | ||
14241 | // Shift-left | |
14242 | BN.prototype.shln = function shln (bits) { | |
14243 | return this.clone().ishln(bits); | |
14244 | }; | |
14245 | ||
14246 | BN.prototype.ushln = function ushln (bits) { | |
14247 | return this.clone().iushln(bits); | |
14248 | }; | |
14249 | ||
14250 | // Shift-right | |
14251 | BN.prototype.shrn = function shrn (bits) { | |
14252 | return this.clone().ishrn(bits); | |
14253 | }; | |
14254 | ||
14255 | BN.prototype.ushrn = function ushrn (bits) { | |
14256 | return this.clone().iushrn(bits); | |
14257 | }; | |
14258 | ||
14259 | // Test if n bit is set | |
14260 | BN.prototype.testn = function testn (bit) { | |
14261 | assert(typeof bit === 'number' && bit >= 0); | |
14262 | var r = bit % 26; | |
14263 | var s = (bit - r) / 26; | |
14264 | var q = 1 << r; | |
14265 | ||
14266 | // Fast case: bit is much higher than all existing words | |
14267 | if (this.length <= s) return false; | |
14268 | ||
14269 | // Check bit and return | |
14270 | var w = this.words[s]; | |
14271 | ||
14272 | return !!(w & q); | |
14273 | }; | |
14274 | ||
14275 | // Return only lowers bits of number (in-place) | |
14276 | BN.prototype.imaskn = function imaskn (bits) { | |
14277 | assert(typeof bits === 'number' && bits >= 0); | |
14278 | var r = bits % 26; | |
14279 | var s = (bits - r) / 26; | |
14280 | ||
14281 | assert(this.negative === 0, 'imaskn works only with positive numbers'); | |
14282 | ||
14283 | if (this.length <= s) { | |
14284 | return this; | |
14285 | } | |
14286 | ||
14287 | if (r !== 0) { | |
14288 | s++; | |
14289 | } | |
14290 | this.length = Math.min(s, this.length); | |
14291 | ||
14292 | if (r !== 0) { | |
14293 | var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r); | |
14294 | this.words[this.length - 1] &= mask; | |
14295 | } | |
14296 | ||
14297 | return this.strip(); | |
14298 | }; | |
14299 | ||
14300 | // Return only lowers bits of number | |
14301 | BN.prototype.maskn = function maskn (bits) { | |
14302 | return this.clone().imaskn(bits); | |
14303 | }; | |
14304 | ||
14305 | // Add plain number `num` to `this` | |
14306 | BN.prototype.iaddn = function iaddn (num) { | |
14307 | assert(typeof num === 'number'); | |
14308 | assert(num < 0x4000000); | |
14309 | if (num < 0) return this.isubn(-num); | |
14310 | ||
14311 | // Possible sign change | |
14312 | if (this.negative !== 0) { | |
14313 | if (this.length === 1 && (this.words[0] | 0) < num) { | |
14314 | this.words[0] = num - (this.words[0] | 0); | |
14315 | this.negative = 0; | |
14316 | return this; | |
14317 | } | |
14318 | ||
14319 | this.negative = 0; | |
14320 | this.isubn(num); | |
14321 | this.negative = 1; | |
14322 | return this; | |
14323 | } | |
14324 | ||
14325 | // Add without checks | |
14326 | return this._iaddn(num); | |
14327 | }; | |
14328 | ||
14329 | BN.prototype._iaddn = function _iaddn (num) { | |
14330 | this.words[0] += num; | |
14331 | ||
14332 | // Carry | |
14333 | for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) { | |
14334 | this.words[i] -= 0x4000000; | |
14335 | if (i === this.length - 1) { | |
14336 | this.words[i + 1] = 1; | |
14337 | } else { | |
14338 | this.words[i + 1]++; | |
14339 | } | |
14340 | } | |
14341 | this.length = Math.max(this.length, i + 1); | |
14342 | ||
14343 | return this; | |
14344 | }; | |
14345 | ||
14346 | // Subtract plain number `num` from `this` | |
14347 | BN.prototype.isubn = function isubn (num) { | |
14348 | assert(typeof num === 'number'); | |
14349 | assert(num < 0x4000000); | |
14350 | if (num < 0) return this.iaddn(-num); | |
14351 | ||
14352 | if (this.negative !== 0) { | |
14353 | this.negative = 0; | |
14354 | this.iaddn(num); | |
14355 | this.negative = 1; | |
14356 | return this; | |
14357 | } | |
14358 | ||
14359 | this.words[0] -= num; | |
14360 | ||
14361 | if (this.length === 1 && this.words[0] < 0) { | |
14362 | this.words[0] = -this.words[0]; | |
14363 | this.negative = 1; | |
14364 | } else { | |
14365 | // Carry | |
14366 | for (var i = 0; i < this.length && this.words[i] < 0; i++) { | |
14367 | this.words[i] += 0x4000000; | |
14368 | this.words[i + 1] -= 1; | |
14369 | } | |
14370 | } | |
14371 | ||
14372 | return this.strip(); | |
14373 | }; | |
14374 | ||
14375 | BN.prototype.addn = function addn (num) { | |
14376 | return this.clone().iaddn(num); | |
14377 | }; | |
14378 | ||
14379 | BN.prototype.subn = function subn (num) { | |
14380 | return this.clone().isubn(num); | |
14381 | }; | |
14382 | ||
14383 | BN.prototype.iabs = function iabs () { | |
14384 | this.negative = 0; | |
14385 | ||
14386 | return this; | |
14387 | }; | |
14388 | ||
14389 | BN.prototype.abs = function abs () { | |
14390 | return this.clone().iabs(); | |
14391 | }; | |
14392 | ||
14393 | BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) { | |
14394 | var len = num.length + shift; | |
14395 | var i; | |
14396 | ||
14397 | this._expand(len); | |
14398 | ||
14399 | var w; | |
14400 | var carry = 0; | |
14401 | for (i = 0; i < num.length; i++) { | |
14402 | w = (this.words[i + shift] | 0) + carry; | |
14403 | var right = (num.words[i] | 0) * mul; | |
14404 | w -= right & 0x3ffffff; | |
14405 | carry = (w >> 26) - ((right / 0x4000000) | 0); | |
14406 | this.words[i + shift] = w & 0x3ffffff; | |
14407 | } | |
14408 | for (; i < this.length - shift; i++) { | |
14409 | w = (this.words[i + shift] | 0) + carry; | |
14410 | carry = w >> 26; | |
14411 | this.words[i + shift] = w & 0x3ffffff; | |
14412 | } | |
14413 | ||
14414 | if (carry === 0) return this.strip(); | |
14415 | ||
14416 | // Subtraction overflow | |
14417 | assert(carry === -1); | |
14418 | carry = 0; | |
14419 | for (i = 0; i < this.length; i++) { | |
14420 | w = -(this.words[i] | 0) + carry; | |
14421 | carry = w >> 26; | |
14422 | this.words[i] = w & 0x3ffffff; | |
14423 | } | |
14424 | this.negative = 1; | |
14425 | ||
14426 | return this.strip(); | |
14427 | }; | |
14428 | ||
14429 | BN.prototype._wordDiv = function _wordDiv (num, mode) { | |
14430 | var shift = this.length - num.length; | |
14431 | ||
14432 | var a = this.clone(); | |
14433 | var b = num; | |
14434 | ||
14435 | // Normalize | |
14436 | var bhi = b.words[b.length - 1] | 0; | |
14437 | var bhiBits = this._countBits(bhi); | |
14438 | shift = 26 - bhiBits; | |
14439 | if (shift !== 0) { | |
14440 | b = b.ushln(shift); | |
14441 | a.iushln(shift); | |
14442 | bhi = b.words[b.length - 1] | 0; | |
14443 | } | |
14444 | ||
14445 | // Initialize quotient | |
14446 | var m = a.length - b.length; | |
14447 | var q; | |
14448 | ||
14449 | if (mode !== 'mod') { | |
14450 | q = new BN(null); | |
14451 | q.length = m + 1; | |
14452 | q.words = new Array(q.length); | |
14453 | for (var i = 0; i < q.length; i++) { | |
14454 | q.words[i] = 0; | |
14455 | } | |
14456 | } | |
14457 | ||
14458 | var diff = a.clone()._ishlnsubmul(b, 1, m); | |
14459 | if (diff.negative === 0) { | |
14460 | a = diff; | |
14461 | if (q) { | |
14462 | q.words[m] = 1; | |
14463 | } | |
14464 | } | |
14465 | ||
14466 | for (var j = m - 1; j >= 0; j--) { | |
14467 | var qj = (a.words[b.length + j] | 0) * 0x4000000 + | |
14468 | (a.words[b.length + j - 1] | 0); | |
14469 | ||
14470 | // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max | |
14471 | // (0x7ffffff) | |
14472 | qj = Math.min((qj / bhi) | 0, 0x3ffffff); | |
14473 | ||
14474 | a._ishlnsubmul(b, qj, j); | |
14475 | while (a.negative !== 0) { | |
14476 | qj--; | |
14477 | a.negative = 0; | |
14478 | a._ishlnsubmul(b, 1, j); | |
14479 | if (!a.isZero()) { | |
14480 | a.negative ^= 1; | |
14481 | } | |
14482 | } | |
14483 | if (q) { | |
14484 | q.words[j] = qj; | |
14485 | } | |
14486 | } | |
14487 | if (q) { | |
14488 | q.strip(); | |
14489 | } | |
14490 | a.strip(); | |
14491 | ||
14492 | // Denormalize | |
14493 | if (mode !== 'div' && shift !== 0) { | |
14494 | a.iushrn(shift); | |
14495 | } | |
14496 | ||
14497 | return { | |
14498 | div: q || null, | |
14499 | mod: a | |
14500 | }; | |
14501 | }; | |
14502 | ||
14503 | // NOTE: 1) `mode` can be set to `mod` to request mod only, | |
14504 | // to `div` to request div only, or be absent to | |
14505 | // request both div & mod | |
14506 | // 2) `positive` is true if unsigned mod is requested | |
14507 | BN.prototype.divmod = function divmod (num, mode, positive) { | |
14508 | assert(!num.isZero()); | |
14509 | ||
14510 | if (this.isZero()) { | |
14511 | return { | |
14512 | div: new BN(0), | |
14513 | mod: new BN(0) | |
14514 | }; | |
14515 | } | |
14516 | ||
14517 | var div, mod, res; | |
14518 | if (this.negative !== 0 && num.negative === 0) { | |
14519 | res = this.neg().divmod(num, mode); | |
14520 | ||
14521 | if (mode !== 'mod') { | |
14522 | div = res.div.neg(); | |
14523 | } | |
14524 | ||
14525 | if (mode !== 'div') { | |
14526 | mod = res.mod.neg(); | |
14527 | if (positive && mod.negative !== 0) { | |
14528 | mod.iadd(num); | |
14529 | } | |
14530 | } | |
14531 | ||
14532 | return { | |
14533 | div: div, | |
14534 | mod: mod | |
14535 | }; | |
14536 | } | |
14537 | ||
14538 | if (this.negative === 0 && num.negative !== 0) { | |
14539 | res = this.divmod(num.neg(), mode); | |
14540 | ||
14541 | if (mode !== 'mod') { | |
14542 | div = res.div.neg(); | |
14543 | } | |
14544 | ||
14545 | return { | |
14546 | div: div, | |
14547 | mod: res.mod | |
14548 | }; | |
14549 | } | |
14550 | ||
14551 | if ((this.negative & num.negative) !== 0) { | |
14552 | res = this.neg().divmod(num.neg(), mode); | |
14553 | ||
14554 | if (mode !== 'div') { | |
14555 | mod = res.mod.neg(); | |
14556 | if (positive && mod.negative !== 0) { | |
14557 | mod.isub(num); | |
14558 | } | |
14559 | } | |
14560 | ||
14561 | return { | |
14562 | div: res.div, | |
14563 | mod: mod | |
14564 | }; | |
14565 | } | |
14566 | ||
14567 | // Both numbers are positive at this point | |
14568 | ||
14569 | // Strip both numbers to approximate shift value | |
14570 | if (num.length > this.length || this.cmp(num) < 0) { | |
14571 | return { | |
14572 | div: new BN(0), | |
14573 | mod: this | |
14574 | }; | |
14575 | } | |
14576 | ||
14577 | // Very short reduction | |
14578 | if (num.length === 1) { | |
14579 | if (mode === 'div') { | |
14580 | return { | |
14581 | div: this.divn(num.words[0]), | |
14582 | mod: null | |
14583 | }; | |
14584 | } | |
14585 | ||
14586 | if (mode === 'mod') { | |
14587 | return { | |
14588 | div: null, | |
14589 | mod: new BN(this.modn(num.words[0])) | |
14590 | }; | |
14591 | } | |
14592 | ||
14593 | return { | |
14594 | div: this.divn(num.words[0]), | |
14595 | mod: new BN(this.modn(num.words[0])) | |
14596 | }; | |
14597 | } | |
14598 | ||
14599 | return this._wordDiv(num, mode); | |
14600 | }; | |
14601 | ||
14602 | // Find `this` / `num` | |
14603 | BN.prototype.div = function div (num) { | |
14604 | return this.divmod(num, 'div', false).div; | |
14605 | }; | |
14606 | ||
14607 | // Find `this` % `num` | |
14608 | BN.prototype.mod = function mod (num) { | |
14609 | return this.divmod(num, 'mod', false).mod; | |
14610 | }; | |
14611 | ||
14612 | BN.prototype.umod = function umod (num) { | |
14613 | return this.divmod(num, 'mod', true).mod; | |
14614 | }; | |
14615 | ||
14616 | // Find Round(`this` / `num`) | |
14617 | BN.prototype.divRound = function divRound (num) { | |
14618 | var dm = this.divmod(num); | |
14619 | ||
14620 | // Fast case - exact division | |
14621 | if (dm.mod.isZero()) return dm.div; | |
14622 | ||
14623 | var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod; | |
14624 | ||
14625 | var half = num.ushrn(1); | |
14626 | var r2 = num.andln(1); | |
14627 | var cmp = mod.cmp(half); | |
14628 | ||
14629 | // Round down | |
14630 | if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div; | |
14631 | ||
14632 | // Round up | |
14633 | return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1); | |
14634 | }; | |
14635 | ||
14636 | BN.prototype.modn = function modn (num) { | |
14637 | assert(num <= 0x3ffffff); | |
14638 | var p = (1 << 26) % num; | |
14639 | ||
14640 | var acc = 0; | |
14641 | for (var i = this.length - 1; i >= 0; i--) { | |
14642 | acc = (p * acc + (this.words[i] | 0)) % num; | |
14643 | } | |
14644 | ||
14645 | return acc; | |
14646 | }; | |
14647 | ||
14648 | // In-place division by number | |
14649 | BN.prototype.idivn = function idivn (num) { | |
14650 | assert(num <= 0x3ffffff); | |
14651 | ||
14652 | var carry = 0; | |
14653 | for (var i = this.length - 1; i >= 0; i--) { | |
14654 | var w = (this.words[i] | 0) + carry * 0x4000000; | |
14655 | this.words[i] = (w / num) | 0; | |
14656 | carry = w % num; | |
14657 | } | |
14658 | ||
14659 | return this.strip(); | |
14660 | }; | |
14661 | ||
14662 | BN.prototype.divn = function divn (num) { | |
14663 | return this.clone().idivn(num); | |
14664 | }; | |
14665 | ||
14666 | BN.prototype.egcd = function egcd (p) { | |
14667 | assert(p.negative === 0); | |
14668 | assert(!p.isZero()); | |
14669 | ||
14670 | var x = this; | |
14671 | var y = p.clone(); | |
14672 | ||
14673 | if (x.negative !== 0) { | |
14674 | x = x.umod(p); | |
14675 | } else { | |
14676 | x = x.clone(); | |
14677 | } | |
14678 | ||
14679 | // A * x + B * y = x | |
14680 | var A = new BN(1); | |
14681 | var B = new BN(0); | |
14682 | ||
14683 | // C * x + D * y = y | |
14684 | var C = new BN(0); | |
14685 | var D = new BN(1); | |
14686 | ||
14687 | var g = 0; | |
14688 | ||
14689 | while (x.isEven() && y.isEven()) { | |
14690 | x.iushrn(1); | |
14691 | y.iushrn(1); | |
14692 | ++g; | |
14693 | } | |
14694 | ||
14695 | var yp = y.clone(); | |
14696 | var xp = x.clone(); | |
14697 | ||
14698 | while (!x.isZero()) { | |
14699 | for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1); | |
14700 | if (i > 0) { | |
14701 | x.iushrn(i); | |
14702 | while (i-- > 0) { | |
14703 | if (A.isOdd() || B.isOdd()) { | |
14704 | A.iadd(yp); | |
14705 | B.isub(xp); | |
14706 | } | |
14707 | ||
14708 | A.iushrn(1); | |
14709 | B.iushrn(1); | |
14710 | } | |
14711 | } | |
14712 | ||
14713 | for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); | |
14714 | if (j > 0) { | |
14715 | y.iushrn(j); | |
14716 | while (j-- > 0) { | |
14717 | if (C.isOdd() || D.isOdd()) { | |
14718 | C.iadd(yp); | |
14719 | D.isub(xp); | |
14720 | } | |
14721 | ||
14722 | C.iushrn(1); | |
14723 | D.iushrn(1); | |
14724 | } | |
14725 | } | |
14726 | ||
14727 | if (x.cmp(y) >= 0) { | |
14728 | x.isub(y); | |
14729 | A.isub(C); | |
14730 | B.isub(D); | |
14731 | } else { | |
14732 | y.isub(x); | |
14733 | C.isub(A); | |
14734 | D.isub(B); | |
14735 | } | |
14736 | } | |
14737 | ||
14738 | return { | |
14739 | a: C, | |
14740 | b: D, | |
14741 | gcd: y.iushln(g) | |
14742 | }; | |
14743 | }; | |
14744 | ||
14745 | // This is reduced incarnation of the binary EEA | |
14746 | // above, designated to invert members of the | |
14747 | // _prime_ fields F(p) at a maximal speed | |
14748 | BN.prototype._invmp = function _invmp (p) { | |
14749 | assert(p.negative === 0); | |
14750 | assert(!p.isZero()); | |
14751 | ||
14752 | var a = this; | |
14753 | var b = p.clone(); | |
14754 | ||
14755 | if (a.negative !== 0) { | |
14756 | a = a.umod(p); | |
14757 | } else { | |
14758 | a = a.clone(); | |
14759 | } | |
14760 | ||
14761 | var x1 = new BN(1); | |
14762 | var x2 = new BN(0); | |
14763 | ||
14764 | var delta = b.clone(); | |
14765 | ||
14766 | while (a.cmpn(1) > 0 && b.cmpn(1) > 0) { | |
14767 | for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1); | |
14768 | if (i > 0) { | |
14769 | a.iushrn(i); | |
14770 | while (i-- > 0) { | |
14771 | if (x1.isOdd()) { | |
14772 | x1.iadd(delta); | |
14773 | } | |
14774 | ||
14775 | x1.iushrn(1); | |
14776 | } | |
14777 | } | |
14778 | ||
14779 | for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1); | |
14780 | if (j > 0) { | |
14781 | b.iushrn(j); | |
14782 | while (j-- > 0) { | |
14783 | if (x2.isOdd()) { | |
14784 | x2.iadd(delta); | |
14785 | } | |
14786 | ||
14787 | x2.iushrn(1); | |
14788 | } | |
14789 | } | |
14790 | ||
14791 | if (a.cmp(b) >= 0) { | |
14792 | a.isub(b); | |
14793 | x1.isub(x2); | |
14794 | } else { | |
14795 | b.isub(a); | |
14796 | x2.isub(x1); | |
14797 | } | |
14798 | } | |
14799 | ||
14800 | var res; | |
14801 | if (a.cmpn(1) === 0) { | |
14802 | res = x1; | |
14803 | } else { | |
14804 | res = x2; | |
14805 | } | |
14806 | ||
14807 | if (res.cmpn(0) < 0) { | |
14808 | res.iadd(p); | |
14809 | } | |
14810 | ||
14811 | return res; | |
14812 | }; | |
14813 | ||
14814 | BN.prototype.gcd = function gcd (num) { | |
14815 | if (this.isZero()) return num.abs(); | |
14816 | if (num.isZero()) return this.abs(); | |
14817 | ||
14818 | var a = this.clone(); | |
14819 | var b = num.clone(); | |
14820 | a.negative = 0; | |
14821 | b.negative = 0; | |
14822 | ||
14823 | // Remove common factor of two | |
14824 | for (var shift = 0; a.isEven() && b.isEven(); shift++) { | |
14825 | a.iushrn(1); | |
14826 | b.iushrn(1); | |
14827 | } | |
14828 | ||
14829 | do { | |
14830 | while (a.isEven()) { | |
14831 | a.iushrn(1); | |
14832 | } | |
14833 | while (b.isEven()) { | |
14834 | b.iushrn(1); | |
14835 | } | |
14836 | ||
14837 | var r = a.cmp(b); | |
14838 | if (r < 0) { | |
14839 | // Swap `a` and `b` to make `a` always bigger than `b` | |
14840 | var t = a; | |
14841 | a = b; | |
14842 | b = t; | |
14843 | } else if (r === 0 || b.cmpn(1) === 0) { | |
14844 | break; | |
14845 | } | |
14846 | ||
14847 | a.isub(b); | |
14848 | } while (true); | |
14849 | ||
14850 | return b.iushln(shift); | |
14851 | }; | |
14852 | ||
14853 | // Invert number in the field F(num) | |
14854 | BN.prototype.invm = function invm (num) { | |
14855 | return this.egcd(num).a.umod(num); | |
14856 | }; | |
14857 | ||
14858 | BN.prototype.isEven = function isEven () { | |
14859 | return (this.words[0] & 1) === 0; | |
14860 | }; | |
14861 | ||
14862 | BN.prototype.isOdd = function isOdd () { | |
14863 | return (this.words[0] & 1) === 1; | |
14864 | }; | |
14865 | ||
14866 | // And first word and num | |
14867 | BN.prototype.andln = function andln (num) { | |
14868 | return this.words[0] & num; | |
14869 | }; | |
14870 | ||
14871 | // Increment at the bit position in-line | |
14872 | BN.prototype.bincn = function bincn (bit) { | |
14873 | assert(typeof bit === 'number'); | |
14874 | var r = bit % 26; | |
14875 | var s = (bit - r) / 26; | |
14876 | var q = 1 << r; | |
14877 | ||
14878 | // Fast case: bit is much higher than all existing words | |
14879 | if (this.length <= s) { | |
14880 | this._expand(s + 1); | |
14881 | this.words[s] |= q; | |
14882 | return this; | |
14883 | } | |
14884 | ||
14885 | // Add bit and propagate, if needed | |
14886 | var carry = q; | |
14887 | for (var i = s; carry !== 0 && i < this.length; i++) { | |
14888 | var w = this.words[i] | 0; | |
14889 | w += carry; | |
14890 | carry = w >>> 26; | |
14891 | w &= 0x3ffffff; | |
14892 | this.words[i] = w; | |
14893 | } | |
14894 | if (carry !== 0) { | |
14895 | this.words[i] = carry; | |
14896 | this.length++; | |
14897 | } | |
14898 | return this; | |
14899 | }; | |
14900 | ||
14901 | BN.prototype.isZero = function isZero () { | |
14902 | return this.length === 1 && this.words[0] === 0; | |
14903 | }; | |
14904 | ||
14905 | BN.prototype.cmpn = function cmpn (num) { | |
14906 | var negative = num < 0; | |
14907 | ||
14908 | if (this.negative !== 0 && !negative) return -1; | |
14909 | if (this.negative === 0 && negative) return 1; | |
14910 | ||
14911 | this.strip(); | |
14912 | ||
14913 | var res; | |
14914 | if (this.length > 1) { | |
14915 | res = 1; | |
14916 | } else { | |
14917 | if (negative) { | |
14918 | num = -num; | |
14919 | } | |
14920 | ||
14921 | assert(num <= 0x3ffffff, 'Number is too big'); | |
14922 | ||
14923 | var w = this.words[0] | 0; | |
14924 | res = w === num ? 0 : w < num ? -1 : 1; | |
14925 | } | |
14926 | if (this.negative !== 0) return -res | 0; | |
14927 | return res; | |
14928 | }; | |
14929 | ||
14930 | // Compare two numbers and return: | |
14931 | // 1 - if `this` > `num` | |
14932 | // 0 - if `this` == `num` | |
14933 | // -1 - if `this` < `num` | |
14934 | BN.prototype.cmp = function cmp (num) { | |
14935 | if (this.negative !== 0 && num.negative === 0) return -1; | |
14936 | if (this.negative === 0 && num.negative !== 0) return 1; | |
14937 | ||
14938 | var res = this.ucmp(num); | |
14939 | if (this.negative !== 0) return -res | 0; | |
14940 | return res; | |
14941 | }; | |
14942 | ||
14943 | // Unsigned comparison | |
14944 | BN.prototype.ucmp = function ucmp (num) { | |
14945 | // At this point both numbers have the same sign | |
14946 | if (this.length > num.length) return 1; | |
14947 | if (this.length < num.length) return -1; | |
14948 | ||
14949 | var res = 0; | |
14950 | for (var i = this.length - 1; i >= 0; i--) { | |
14951 | var a = this.words[i] | 0; | |
14952 | var b = num.words[i] | 0; | |
14953 | ||
14954 | if (a === b) continue; | |
14955 | if (a < b) { | |
14956 | res = -1; | |
14957 | } else if (a > b) { | |
14958 | res = 1; | |
14959 | } | |
14960 | break; | |
14961 | } | |
14962 | return res; | |
14963 | }; | |
14964 | ||
14965 | BN.prototype.gtn = function gtn (num) { | |
14966 | return this.cmpn(num) === 1; | |
14967 | }; | |
14968 | ||
14969 | BN.prototype.gt = function gt (num) { | |
14970 | return this.cmp(num) === 1; | |
14971 | }; | |
14972 | ||
14973 | BN.prototype.gten = function gten (num) { | |
14974 | return this.cmpn(num) >= 0; | |
14975 | }; | |
14976 | ||
14977 | BN.prototype.gte = function gte (num) { | |
14978 | return this.cmp(num) >= 0; | |
14979 | }; | |
14980 | ||
14981 | BN.prototype.ltn = function ltn (num) { | |
14982 | return this.cmpn(num) === -1; | |
14983 | }; | |
14984 | ||
14985 | BN.prototype.lt = function lt (num) { | |
14986 | return this.cmp(num) === -1; | |
14987 | }; | |
14988 | ||
14989 | BN.prototype.lten = function lten (num) { | |
14990 | return this.cmpn(num) <= 0; | |
14991 | }; | |
14992 | ||
14993 | BN.prototype.lte = function lte (num) { | |
14994 | return this.cmp(num) <= 0; | |
14995 | }; | |
14996 | ||
14997 | BN.prototype.eqn = function eqn (num) { | |
14998 | return this.cmpn(num) === 0; | |
14999 | }; | |
15000 | ||
15001 | BN.prototype.eq = function eq (num) { | |
15002 | return this.cmp(num) === 0; | |
15003 | }; | |
15004 | ||
15005 | // | |
15006 | // A reduce context, could be using montgomery or something better, depending | |
15007 | // on the `m` itself. | |
15008 | // | |
15009 | BN.red = function red (num) { | |
15010 | return new Red(num); | |
15011 | }; | |
15012 | ||
15013 | BN.prototype.toRed = function toRed (ctx) { | |
15014 | assert(!this.red, 'Already a number in reduction context'); | |
15015 | assert(this.negative === 0, 'red works only with positives'); | |
15016 | return ctx.convertTo(this)._forceRed(ctx); | |
15017 | }; | |
15018 | ||
15019 | BN.prototype.fromRed = function fromRed () { | |
15020 | assert(this.red, 'fromRed works only with numbers in reduction context'); | |
15021 | return this.red.convertFrom(this); | |
15022 | }; | |
15023 | ||
15024 | BN.prototype._forceRed = function _forceRed (ctx) { | |
15025 | this.red = ctx; | |
15026 | return this; | |
15027 | }; | |
15028 | ||
15029 | BN.prototype.forceRed = function forceRed (ctx) { | |
15030 | assert(!this.red, 'Already a number in reduction context'); | |
15031 | return this._forceRed(ctx); | |
15032 | }; | |
15033 | ||
15034 | BN.prototype.redAdd = function redAdd (num) { | |
15035 | assert(this.red, 'redAdd works only with red numbers'); | |
15036 | return this.red.add(this, num); | |
15037 | }; | |
15038 | ||
15039 | BN.prototype.redIAdd = function redIAdd (num) { | |
15040 | assert(this.red, 'redIAdd works only with red numbers'); | |
15041 | return this.red.iadd(this, num); | |
15042 | }; | |
15043 | ||
15044 | BN.prototype.redSub = function redSub (num) { | |
15045 | assert(this.red, 'redSub works only with red numbers'); | |
15046 | return this.red.sub(this, num); | |
15047 | }; | |
15048 | ||
15049 | BN.prototype.redISub = function redISub (num) { | |
15050 | assert(this.red, 'redISub works only with red numbers'); | |
15051 | return this.red.isub(this, num); | |
15052 | }; | |
15053 | ||
15054 | BN.prototype.redShl = function redShl (num) { | |
15055 | assert(this.red, 'redShl works only with red numbers'); | |
15056 | return this.red.shl(this, num); | |
15057 | }; | |
15058 | ||
15059 | BN.prototype.redMul = function redMul (num) { | |
15060 | assert(this.red, 'redMul works only with red numbers'); | |
15061 | this.red._verify2(this, num); | |
15062 | return this.red.mul(this, num); | |
15063 | }; | |
15064 | ||
15065 | BN.prototype.redIMul = function redIMul (num) { | |
15066 | assert(this.red, 'redMul works only with red numbers'); | |
15067 | this.red._verify2(this, num); | |
15068 | return this.red.imul(this, num); | |
15069 | }; | |
15070 | ||
15071 | BN.prototype.redSqr = function redSqr () { | |
15072 | assert(this.red, 'redSqr works only with red numbers'); | |
15073 | this.red._verify1(this); | |
15074 | return this.red.sqr(this); | |
15075 | }; | |
15076 | ||
15077 | BN.prototype.redISqr = function redISqr () { | |
15078 | assert(this.red, 'redISqr works only with red numbers'); | |
15079 | this.red._verify1(this); | |
15080 | return this.red.isqr(this); | |
15081 | }; | |
15082 | ||
15083 | // Square root over p | |
15084 | BN.prototype.redSqrt = function redSqrt () { | |
15085 | assert(this.red, 'redSqrt works only with red numbers'); | |
15086 | this.red._verify1(this); | |
15087 | return this.red.sqrt(this); | |
15088 | }; | |
15089 | ||
15090 | BN.prototype.redInvm = function redInvm () { | |
15091 | assert(this.red, 'redInvm works only with red numbers'); | |
15092 | this.red._verify1(this); | |
15093 | return this.red.invm(this); | |
15094 | }; | |
15095 | ||
15096 | // Return negative clone of `this` % `red modulo` | |
15097 | BN.prototype.redNeg = function redNeg () { | |
15098 | assert(this.red, 'redNeg works only with red numbers'); | |
15099 | this.red._verify1(this); | |
15100 | return this.red.neg(this); | |
15101 | }; | |
15102 | ||
15103 | BN.prototype.redPow = function redPow (num) { | |
15104 | assert(this.red && !num.red, 'redPow(normalNum)'); | |
15105 | this.red._verify1(this); | |
15106 | return this.red.pow(this, num); | |
15107 | }; | |
15108 | ||
15109 | // Prime numbers with efficient reduction | |
15110 | var primes = { | |
15111 | k256: null, | |
15112 | p224: null, | |
15113 | p192: null, | |
15114 | p25519: null | |
15115 | }; | |
15116 | ||
15117 | // Pseudo-Mersenne prime | |
15118 | function MPrime (name, p) { | |
15119 | // P = 2 ^ N - K | |
15120 | this.name = name; | |
15121 | this.p = new BN(p, 16); | |
15122 | this.n = this.p.bitLength(); | |
15123 | this.k = new BN(1).iushln(this.n).isub(this.p); | |
15124 | ||
15125 | this.tmp = this._tmp(); | |
15126 | } | |
15127 | ||
15128 | MPrime.prototype._tmp = function _tmp () { | |
15129 | var tmp = new BN(null); | |
15130 | tmp.words = new Array(Math.ceil(this.n / 13)); | |
15131 | return tmp; | |
15132 | }; | |
15133 | ||
15134 | MPrime.prototype.ireduce = function ireduce (num) { | |
15135 | // Assumes that `num` is less than `P^2` | |
15136 | // num = HI * (2 ^ N - K) + HI * K + LO = HI * K + LO (mod P) | |
15137 | var r = num; | |
15138 | var rlen; | |
15139 | ||
15140 | do { | |
15141 | this.split(r, this.tmp); | |
15142 | r = this.imulK(r); | |
15143 | r = r.iadd(this.tmp); | |
15144 | rlen = r.bitLength(); | |
15145 | } while (rlen > this.n); | |
15146 | ||
15147 | var cmp = rlen < this.n ? -1 : r.ucmp(this.p); | |
15148 | if (cmp === 0) { | |
15149 | r.words[0] = 0; | |
15150 | r.length = 1; | |
15151 | } else if (cmp > 0) { | |
15152 | r.isub(this.p); | |
15153 | } else { | |
15154 | r.strip(); | |
15155 | } | |
15156 | ||
15157 | return r; | |
15158 | }; | |
15159 | ||
15160 | MPrime.prototype.split = function split (input, out) { | |
15161 | input.iushrn(this.n, 0, out); | |
15162 | }; | |
15163 | ||
15164 | MPrime.prototype.imulK = function imulK (num) { | |
15165 | return num.imul(this.k); | |
15166 | }; | |
15167 | ||
15168 | function K256 () { | |
15169 | MPrime.call( | |
15170 | this, | |
15171 | 'k256', | |
15172 | 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'); | |
15173 | } | |
15174 | inherits(K256, MPrime); | |
15175 | ||
15176 | K256.prototype.split = function split (input, output) { | |
15177 | // 256 = 9 * 26 + 22 | |
15178 | var mask = 0x3fffff; | |
15179 | ||
15180 | var outLen = Math.min(input.length, 9); | |
15181 | for (var i = 0; i < outLen; i++) { | |
15182 | output.words[i] = input.words[i]; | |
15183 | } | |
15184 | output.length = outLen; | |
15185 | ||
15186 | if (input.length <= 9) { | |
15187 | input.words[0] = 0; | |
15188 | input.length = 1; | |
15189 | return; | |
15190 | } | |
15191 | ||
15192 | // Shift by 9 limbs | |
15193 | var prev = input.words[9]; | |
15194 | output.words[output.length++] = prev & mask; | |
15195 | ||
15196 | for (i = 10; i < input.length; i++) { | |
15197 | var next = input.words[i] | 0; | |
15198 | input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22); | |
15199 | prev = next; | |
15200 | } | |
15201 | prev >>>= 22; | |
15202 | input.words[i - 10] = prev; | |
15203 | if (prev === 0 && input.length > 10) { | |
15204 | input.length -= 10; | |
15205 | } else { | |
15206 | input.length -= 9; | |
15207 | } | |
15208 | }; | |
15209 | ||
15210 | K256.prototype.imulK = function imulK (num) { | |
15211 | // K = 0x1000003d1 = [ 0x40, 0x3d1 ] | |
15212 | num.words[num.length] = 0; | |
15213 | num.words[num.length + 1] = 0; | |
15214 | num.length += 2; | |
15215 | ||
15216 | // bounded at: 0x40 * 0x3ffffff + 0x3d0 = 0x100000390 | |
15217 | var lo = 0; | |
15218 | for (var i = 0; i < num.length; i++) { | |
15219 | var w = num.words[i] | 0; | |
15220 | lo += w * 0x3d1; | |
15221 | num.words[i] = lo & 0x3ffffff; | |
15222 | lo = w * 0x40 + ((lo / 0x4000000) | 0); | |
15223 | } | |
15224 | ||
15225 | // Fast length reduction | |
15226 | if (num.words[num.length - 1] === 0) { | |
15227 | num.length--; | |
15228 | if (num.words[num.length - 1] === 0) { | |
15229 | num.length--; | |
15230 | } | |
15231 | } | |
15232 | return num; | |
15233 | }; | |
15234 | ||
15235 | function P224 () { | |
15236 | MPrime.call( | |
15237 | this, | |
15238 | 'p224', | |
15239 | 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001'); | |
15240 | } | |
15241 | inherits(P224, MPrime); | |
15242 | ||
15243 | function P192 () { | |
15244 | MPrime.call( | |
15245 | this, | |
15246 | 'p192', | |
15247 | 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff'); | |
15248 | } | |
15249 | inherits(P192, MPrime); | |
15250 | ||
15251 | function P25519 () { | |
15252 | // 2 ^ 255 - 19 | |
15253 | MPrime.call( | |
15254 | this, | |
15255 | '25519', | |
15256 | '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed'); | |
15257 | } | |
15258 | inherits(P25519, MPrime); | |
15259 | ||
15260 | P25519.prototype.imulK = function imulK (num) { | |
15261 | // K = 0x13 | |
15262 | var carry = 0; | |
15263 | for (var i = 0; i < num.length; i++) { | |
15264 | var hi = (num.words[i] | 0) * 0x13 + carry; | |
15265 | var lo = hi & 0x3ffffff; | |
15266 | hi >>>= 26; | |
15267 | ||
15268 | num.words[i] = lo; | |
15269 | carry = hi; | |
15270 | } | |
15271 | if (carry !== 0) { | |
15272 | num.words[num.length++] = carry; | |
15273 | } | |
15274 | return num; | |
15275 | }; | |
15276 | ||
15277 | // Exported mostly for testing purposes, use plain name instead | |
15278 | BN._prime = function prime (name) { | |
15279 | // Cached version of prime | |
15280 | if (primes[name]) return primes[name]; | |
15281 | ||
15282 | var prime; | |
15283 | if (name === 'k256') { | |
15284 | prime = new K256(); | |
15285 | } else if (name === 'p224') { | |
15286 | prime = new P224(); | |
15287 | } else if (name === 'p192') { | |
15288 | prime = new P192(); | |
15289 | } else if (name === 'p25519') { | |
15290 | prime = new P25519(); | |
15291 | } else { | |
15292 | throw new Error('Unknown prime ' + name); | |
15293 | } | |
15294 | primes[name] = prime; | |
15295 | ||
15296 | return prime; | |
15297 | }; | |
15298 | ||
15299 | // | |
15300 | // Base reduction engine | |
15301 | // | |
15302 | function Red (m) { | |
15303 | if (typeof m === 'string') { | |
15304 | var prime = BN._prime(m); | |
15305 | this.m = prime.p; | |
15306 | this.prime = prime; | |
15307 | } else { | |
15308 | assert(m.gtn(1), 'modulus must be greater than 1'); | |
15309 | this.m = m; | |
15310 | this.prime = null; | |
15311 | } | |
15312 | } | |
15313 | ||
15314 | Red.prototype._verify1 = function _verify1 (a) { | |
15315 | assert(a.negative === 0, 'red works only with positives'); | |
15316 | assert(a.red, 'red works only with red numbers'); | |
15317 | }; | |
15318 | ||
15319 | Red.prototype._verify2 = function _verify2 (a, b) { | |
15320 | assert((a.negative | b.negative) === 0, 'red works only with positives'); | |
15321 | assert(a.red && a.red === b.red, | |
15322 | 'red works only with red numbers'); | |
15323 | }; | |
15324 | ||
15325 | Red.prototype.imod = function imod (a) { | |
15326 | if (this.prime) return this.prime.ireduce(a)._forceRed(this); | |
15327 | return a.umod(this.m)._forceRed(this); | |
15328 | }; | |
15329 | ||
15330 | Red.prototype.neg = function neg (a) { | |
15331 | if (a.isZero()) { | |
15332 | return a.clone(); | |
15333 | } | |
15334 | ||
15335 | return this.m.sub(a)._forceRed(this); | |
15336 | }; | |
15337 | ||
15338 | Red.prototype.add = function add (a, b) { | |
15339 | this._verify2(a, b); | |
15340 | ||
15341 | var res = a.add(b); | |
15342 | if (res.cmp(this.m) >= 0) { | |
15343 | res.isub(this.m); | |
15344 | } | |
15345 | return res._forceRed(this); | |
15346 | }; | |
15347 | ||
15348 | Red.prototype.iadd = function iadd (a, b) { | |
15349 | this._verify2(a, b); | |
15350 | ||
15351 | var res = a.iadd(b); | |
15352 | if (res.cmp(this.m) >= 0) { | |
15353 | res.isub(this.m); | |
15354 | } | |
15355 | return res; | |
15356 | }; | |
15357 | ||
15358 | Red.prototype.sub = function sub (a, b) { | |
15359 | this._verify2(a, b); | |
15360 | ||
15361 | var res = a.sub(b); | |
15362 | if (res.cmpn(0) < 0) { | |
15363 | res.iadd(this.m); | |
15364 | } | |
15365 | return res._forceRed(this); | |
15366 | }; | |
15367 | ||
15368 | Red.prototype.isub = function isub (a, b) { | |
15369 | this._verify2(a, b); | |
15370 | ||
15371 | var res = a.isub(b); | |
15372 | if (res.cmpn(0) < 0) { | |
15373 | res.iadd(this.m); | |
15374 | } | |
15375 | return res; | |
15376 | }; | |
15377 | ||
15378 | Red.prototype.shl = function shl (a, num) { | |
15379 | this._verify1(a); | |
15380 | return this.imod(a.ushln(num)); | |
15381 | }; | |
15382 | ||
15383 | Red.prototype.imul = function imul (a, b) { | |
15384 | this._verify2(a, b); | |
15385 | return this.imod(a.imul(b)); | |
15386 | }; | |
15387 | ||
15388 | Red.prototype.mul = function mul (a, b) { | |
15389 | this._verify2(a, b); | |
15390 | return this.imod(a.mul(b)); | |
15391 | }; | |
15392 | ||
15393 | Red.prototype.isqr = function isqr (a) { | |
15394 | return this.imul(a, a.clone()); | |
15395 | }; | |
15396 | ||
15397 | Red.prototype.sqr = function sqr (a) { | |
15398 | return this.mul(a, a); | |
15399 | }; | |
15400 | ||
15401 | Red.prototype.sqrt = function sqrt (a) { | |
15402 | if (a.isZero()) return a.clone(); | |
15403 | ||
15404 | var mod3 = this.m.andln(3); | |
15405 | assert(mod3 % 2 === 1); | |
15406 | ||
15407 | // Fast case | |
15408 | if (mod3 === 3) { | |
15409 | var pow = this.m.add(new BN(1)).iushrn(2); | |
15410 | return this.pow(a, pow); | |
15411 | } | |
15412 | ||
15413 | // Tonelli-Shanks algorithm (Totally unoptimized and slow) | |
15414 | // | |
15415 | // Find Q and S, that Q * 2 ^ S = (P - 1) | |
15416 | var q = this.m.subn(1); | |
15417 | var s = 0; | |
15418 | while (!q.isZero() && q.andln(1) === 0) { | |
15419 | s++; | |
15420 | q.iushrn(1); | |
15421 | } | |
15422 | assert(!q.isZero()); | |
15423 | ||
15424 | var one = new BN(1).toRed(this); | |
15425 | var nOne = one.redNeg(); | |
15426 | ||
15427 | // Find quadratic non-residue | |
15428 | // NOTE: Max is such because of generalized Riemann hypothesis. | |
15429 | var lpow = this.m.subn(1).iushrn(1); | |
15430 | var z = this.m.bitLength(); | |
15431 | z = new BN(2 * z * z).toRed(this); | |
15432 | ||
15433 | while (this.pow(z, lpow).cmp(nOne) !== 0) { | |
15434 | z.redIAdd(nOne); | |
15435 | } | |
15436 | ||
15437 | var c = this.pow(z, q); | |
15438 | var r = this.pow(a, q.addn(1).iushrn(1)); | |
15439 | var t = this.pow(a, q); | |
15440 | var m = s; | |
15441 | while (t.cmp(one) !== 0) { | |
15442 | var tmp = t; | |
15443 | for (var i = 0; tmp.cmp(one) !== 0; i++) { | |
15444 | tmp = tmp.redSqr(); | |
15445 | } | |
15446 | assert(i < m); | |
15447 | var b = this.pow(c, new BN(1).iushln(m - i - 1)); | |
15448 | ||
15449 | r = r.redMul(b); | |
15450 | c = b.redSqr(); | |
15451 | t = t.redMul(c); | |
15452 | m = i; | |
15453 | } | |
15454 | ||
15455 | return r; | |
15456 | }; | |
15457 | ||
15458 | Red.prototype.invm = function invm (a) { | |
15459 | var inv = a._invmp(this.m); | |
15460 | if (inv.negative !== 0) { | |
15461 | inv.negative = 0; | |
15462 | return this.imod(inv).redNeg(); | |
15463 | } else { | |
15464 | return this.imod(inv); | |
15465 | } | |
15466 | }; | |
15467 | ||
15468 | Red.prototype.pow = function pow (a, num) { | |
15469 | if (num.isZero()) return new BN(1); | |
15470 | if (num.cmpn(1) === 0) return a.clone(); | |
15471 | ||
15472 | var windowSize = 4; | |
15473 | var wnd = new Array(1 << windowSize); | |
15474 | wnd[0] = new BN(1).toRed(this); | |
15475 | wnd[1] = a; | |
15476 | for (var i = 2; i < wnd.length; i++) { | |
15477 | wnd[i] = this.mul(wnd[i - 1], a); | |
15478 | } | |
15479 | ||
15480 | var res = wnd[0]; | |
15481 | var current = 0; | |
15482 | var currentLen = 0; | |
15483 | var start = num.bitLength() % 26; | |
15484 | if (start === 0) { | |
15485 | start = 26; | |
15486 | } | |
15487 | ||
15488 | for (i = num.length - 1; i >= 0; i--) { | |
15489 | var word = num.words[i]; | |
15490 | for (var j = start - 1; j >= 0; j--) { | |
15491 | var bit = (word >> j) & 1; | |
15492 | if (res !== wnd[0]) { | |
15493 | res = this.sqr(res); | |
15494 | } | |
15495 | ||
15496 | if (bit === 0 && current === 0) { | |
15497 | currentLen = 0; | |
15498 | continue; | |
15499 | } | |
15500 | ||
15501 | current <<= 1; | |
15502 | current |= bit; | |
15503 | currentLen++; | |
15504 | if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue; | |
15505 | ||
15506 | res = this.mul(res, wnd[current]); | |
15507 | currentLen = 0; | |
15508 | current = 0; | |
15509 | } | |
15510 | start = 26; | |
15511 | } | |
15512 | ||
15513 | return res; | |
15514 | }; | |
15515 | ||
15516 | Red.prototype.convertTo = function convertTo (num) { | |
15517 | var r = num.umod(this.m); | |
15518 | ||
15519 | return r === num ? r.clone() : r; | |
15520 | }; | |
15521 | ||
15522 | Red.prototype.convertFrom = function convertFrom (num) { | |
15523 | var res = num.clone(); | |
15524 | res.red = null; | |
15525 | return res; | |
15526 | }; | |
15527 | ||
15528 | // | |
15529 | // Montgomery method engine | |
15530 | // | |
15531 | ||
15532 | BN.mont = function mont (num) { | |
15533 | return new Mont(num); | |
15534 | }; | |
15535 | ||
15536 | function Mont (m) { | |
15537 | Red.call(this, m); | |
15538 | ||
15539 | this.shift = this.m.bitLength(); | |
15540 | if (this.shift % 26 !== 0) { | |
15541 | this.shift += 26 - (this.shift % 26); | |
15542 | } | |
15543 | ||
15544 | this.r = new BN(1).iushln(this.shift); | |
15545 | this.r2 = this.imod(this.r.sqr()); | |
15546 | this.rinv = this.r._invmp(this.m); | |
15547 | ||
15548 | this.minv = this.rinv.mul(this.r).isubn(1).div(this.m); | |
15549 | this.minv = this.minv.umod(this.r); | |
15550 | this.minv = this.r.sub(this.minv); | |
15551 | } | |
15552 | inherits(Mont, Red); | |
15553 | ||
15554 | Mont.prototype.convertTo = function convertTo (num) { | |
15555 | return this.imod(num.ushln(this.shift)); | |
15556 | }; | |
15557 | ||
15558 | Mont.prototype.convertFrom = function convertFrom (num) { | |
15559 | var r = this.imod(num.mul(this.rinv)); | |
15560 | r.red = null; | |
15561 | return r; | |
15562 | }; | |
15563 | ||
15564 | Mont.prototype.imul = function imul (a, b) { | |
15565 | if (a.isZero() || b.isZero()) { | |
15566 | a.words[0] = 0; | |
15567 | a.length = 1; | |
15568 | return a; | |
15569 | } | |
15570 | ||
15571 | var t = a.imul(b); | |
15572 | var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); | |
15573 | var u = t.isub(c).iushrn(this.shift); | |
15574 | var res = u; | |
15575 | ||
15576 | if (u.cmp(this.m) >= 0) { | |
15577 | res = u.isub(this.m); | |
15578 | } else if (u.cmpn(0) < 0) { | |
15579 | res = u.iadd(this.m); | |
15580 | } | |
15581 | ||
15582 | return res._forceRed(this); | |
15583 | }; | |
15584 | ||
15585 | Mont.prototype.mul = function mul (a, b) { | |
15586 | if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this); | |
15587 | ||
15588 | var t = a.mul(b); | |
15589 | var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m); | |
15590 | var u = t.isub(c).iushrn(this.shift); | |
15591 | var res = u; | |
15592 | if (u.cmp(this.m) >= 0) { | |
15593 | res = u.isub(this.m); | |
15594 | } else if (u.cmpn(0) < 0) { | |
15595 | res = u.iadd(this.m); | |
15596 | } | |
15597 | ||
15598 | return res._forceRed(this); | |
15599 | }; | |
15600 | ||
15601 | Mont.prototype.invm = function invm (a) { | |
15602 | // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R | |
15603 | var res = this.imod(a._invmp(this.m).mul(this.r2)); | |
15604 | return res._forceRed(this); | |
15605 | }; | |
15606 | })(typeof module === 'undefined' || module, this); | |
15607 | ||
15608 | },{}],82:[function(require,module,exports){ | |
15609 | var r; | |
15610 | ||
15611 | module.exports = function rand(len) { | |
15612 | if (!r) | |
15613 | r = new Rand(null); | |
15614 | ||
15615 | return r.generate(len); | |
15616 | }; | |
15617 | ||
15618 | function Rand(rand) { | |
15619 | this.rand = rand; | |
15620 | } | |
15621 | module.exports.Rand = Rand; | |
15622 | ||
15623 | Rand.prototype.generate = function generate(len) { | |
15624 | return this._rand(len); | |
15625 | }; | |
15626 | ||
15627 | // Emulate crypto API using randy | |
15628 | Rand.prototype._rand = function _rand(n) { | |
15629 | if (this.rand.getBytes) | |
15630 | return this.rand.getBytes(n); | |
15631 | ||
15632 | var res = new Uint8Array(n); | |
15633 | for (var i = 0; i < res.length; i++) | |
15634 | res[i] = this.rand.getByte(); | |
15635 | return res; | |
15636 | }; | |
15637 | ||
15638 | if (typeof self === 'object') { | |
15639 | if (self.crypto && self.crypto.getRandomValues) { | |
15640 | // Modern browsers | |
15641 | Rand.prototype._rand = function _rand(n) { | |
15642 | var arr = new Uint8Array(n); | |
15643 | self.crypto.getRandomValues(arr); | |
15644 | return arr; | |
15645 | }; | |
15646 | } else if (self.msCrypto && self.msCrypto.getRandomValues) { | |
15647 | // IE | |
15648 | Rand.prototype._rand = function _rand(n) { | |
15649 | var arr = new Uint8Array(n); | |
15650 | self.msCrypto.getRandomValues(arr); | |
15651 | return arr; | |
15652 | }; | |
15653 | ||
15654 | // Safari's WebWorkers do not have `crypto` | |
15655 | } else if (typeof window === 'object') { | |
15656 | // Old junk | |
15657 | Rand.prototype._rand = function() { | |
15658 | throw new Error('Not implemented yet'); | |
15659 | }; | |
15660 | } | |
15661 | } else { | |
15662 | // Node.js or Web worker with no crypto support | |
15663 | try { | |
15664 | var crypto = require('crypto'); | |
15665 | if (typeof crypto.randomBytes !== 'function') | |
15666 | throw new Error('Not supported'); | |
15667 | ||
15668 | Rand.prototype._rand = function _rand(n) { | |
15669 | return crypto.randomBytes(n); | |
15670 | }; | |
15671 | } catch (e) { | |
15672 | } | |
15673 | } | |
15674 | ||
15675 | },{"crypto":3}],83:[function(require,module,exports){ | |
15676 | var basex = require('base-x') | |
15677 | var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | |
15678 | ||
15679 | module.exports = basex(ALPHABET) | |
15680 | ||
15681 | },{"base-x":35}],84:[function(require,module,exports){ | |
15682 | (function (Buffer){ | |
15683 | 'use strict' | |
15684 | ||
15685 | var base58 = require('bs58') | |
15686 | ||
15687 | module.exports = function (checksumFn) { | |
15688 | // Encode a buffer as a base58-check encoded string | |
15689 | function encode (payload) { | |
15690 | var checksum = checksumFn(payload) | |
15691 | ||
15692 | return base58.encode(Buffer.concat([ | |
15693 | payload, | |
15694 | checksum | |
15695 | ], payload.length + 4)) | |
15696 | } | |
15697 | ||
15698 | function decodeRaw (buffer) { | |
15699 | var payload = buffer.slice(0, -4) | |
15700 | var checksum = buffer.slice(-4) | |
15701 | var newChecksum = checksumFn(payload) | |
15702 | ||
15703 | if (checksum[0] ^ newChecksum[0] | | |
15704 | checksum[1] ^ newChecksum[1] | | |
15705 | checksum[2] ^ newChecksum[2] | | |
15706 | checksum[3] ^ newChecksum[3]) return | |
15707 | ||
15708 | return payload | |
15709 | } | |
15710 | ||
15711 | // Decode a base58-check encoded string to a buffer, no result if checksum is wrong | |
15712 | function decodeUnsafe (string) { | |
15713 | var buffer = base58.decodeUnsafe(string) | |
15714 | if (!buffer) return | |
15715 | ||
15716 | return decodeRaw(buffer) | |
15717 | } | |
15718 | ||
15719 | function decode (string) { | |
15720 | var buffer = base58.decode(string) | |
15721 | var payload = decodeRaw(buffer, checksumFn) | |
15722 | if (!payload) throw new Error('Invalid checksum') | |
15723 | return payload | |
15724 | } | |
15725 | ||
15726 | return { | |
15727 | encode: encode, | |
15728 | decode: decode, | |
15729 | decodeUnsafe: decodeUnsafe | |
15730 | } | |
15731 | } | |
15732 | ||
15733 | }).call(this,require("buffer").Buffer) | |
15734 | },{"bs58":83,"buffer":5}],85:[function(require,module,exports){ | |
15735 | 'use strict' | |
15736 | ||
15737 | var createHash = require('create-hash') | |
15738 | var bs58checkBase = require('./base') | |
15739 | ||
15740 | // SHA256(SHA256(buffer)) | |
15741 | function sha256x2 (buffer) { | |
15742 | var tmp = createHash('sha256').update(buffer).digest() | |
15743 | return createHash('sha256').update(tmp).digest() | |
15744 | } | |
15745 | ||
15746 | module.exports = bs58checkBase(sha256x2) | |
15747 | ||
15748 | },{"./base":84,"create-hash":87}],86:[function(require,module,exports){ | |
15749 | var Buffer = require('safe-buffer').Buffer | |
15750 | var Transform = require('stream').Transform | |
15751 | var StringDecoder = require('string_decoder').StringDecoder | |
15752 | var inherits = require('inherits') | |
15753 | ||
15754 | function CipherBase (hashMode) { | |
15755 | Transform.call(this) | |
15756 | this.hashMode = typeof hashMode === 'string' | |
15757 | if (this.hashMode) { | |
15758 | this[hashMode] = this._finalOrDigest | |
15759 | } else { | |
15760 | this.final = this._finalOrDigest | |
15761 | } | |
15762 | if (this._final) { | |
15763 | this.__final = this._final | |
15764 | this._final = null | |
15765 | } | |
15766 | this._decoder = null | |
15767 | this._encoding = null | |
15768 | } | |
15769 | inherits(CipherBase, Transform) | |
15770 | ||
15771 | CipherBase.prototype.update = function (data, inputEnc, outputEnc) { | |
15772 | if (typeof data === 'string') { | |
15773 | data = Buffer.from(data, inputEnc) | |
15774 | } | |
15775 | ||
15776 | var outData = this._update(data) | |
15777 | if (this.hashMode) return this | |
15778 | ||
15779 | if (outputEnc) { | |
15780 | outData = this._toString(outData, outputEnc) | |
15781 | } | |
15782 | ||
15783 | return outData | |
15784 | } | |
15785 | ||
15786 | CipherBase.prototype.setAutoPadding = function () {} | |
15787 | CipherBase.prototype.getAuthTag = function () { | |
15788 | throw new Error('trying to get auth tag in unsupported state') | |
15789 | } | |
15790 | ||
15791 | CipherBase.prototype.setAuthTag = function () { | |
15792 | throw new Error('trying to set auth tag in unsupported state') | |
15793 | } | |
15794 | ||
15795 | CipherBase.prototype.setAAD = function () { | |
15796 | throw new Error('trying to set aad in unsupported state') | |
15797 | } | |
15798 | ||
15799 | CipherBase.prototype._transform = function (data, _, next) { | |
15800 | var err | |
15801 | try { | |
15802 | if (this.hashMode) { | |
15803 | this._update(data) | |
15804 | } else { | |
15805 | this.push(this._update(data)) | |
15806 | } | |
15807 | } catch (e) { | |
15808 | err = e | |
15809 | } finally { | |
15810 | next(err) | |
15811 | } | |
15812 | } | |
15813 | CipherBase.prototype._flush = function (done) { | |
15814 | var err | |
15815 | try { | |
15816 | this.push(this.__final()) | |
15817 | } catch (e) { | |
15818 | err = e | |
15819 | } | |
15820 | ||
15821 | done(err) | |
15822 | } | |
15823 | CipherBase.prototype._finalOrDigest = function (outputEnc) { | |
15824 | var outData = this.__final() || Buffer.alloc(0) | |
15825 | if (outputEnc) { | |
15826 | outData = this._toString(outData, outputEnc, true) | |
15827 | } | |
15828 | return outData | |
15829 | } | |
15830 | ||
15831 | CipherBase.prototype._toString = function (value, enc, fin) { | |
15832 | if (!this._decoder) { | |
15833 | this._decoder = new StringDecoder(enc) | |
15834 | this._encoding = enc | |
15835 | } | |
15836 | ||
15837 | if (this._encoding !== enc) throw new Error('can\'t switch encodings') | |
15838 | ||
15839 | var out = this._decoder.write(value) | |
15840 | if (fin) { | |
15841 | out += this._decoder.end() | |
15842 | } | |
15843 | ||
15844 | return out | |
15845 | } | |
15846 | ||
15847 | module.exports = CipherBase | |
15848 | ||
15849 | },{"inherits":121,"safe-buffer":128,"stream":28,"string_decoder":29}],87:[function(require,module,exports){ | |
15850 | (function (Buffer){ | |
15851 | 'use strict' | |
15852 | var inherits = require('inherits') | |
15853 | var md5 = require('./md5') | |
15854 | var RIPEMD160 = require('ripemd160') | |
15855 | var sha = require('sha.js') | |
15856 | ||
15857 | var Base = require('cipher-base') | |
15858 | ||
15859 | function HashNoConstructor (hash) { | |
15860 | Base.call(this, 'digest') | |
15861 | ||
15862 | this._hash = hash | |
15863 | this.buffers = [] | |
15864 | } | |
15865 | ||
15866 | inherits(HashNoConstructor, Base) | |
15867 | ||
15868 | HashNoConstructor.prototype._update = function (data) { | |
15869 | this.buffers.push(data) | |
15870 | } | |
15871 | ||
15872 | HashNoConstructor.prototype._final = function () { | |
15873 | var buf = Buffer.concat(this.buffers) | |
15874 | var r = this._hash(buf) | |
15875 | this.buffers = null | |
15876 | ||
15877 | return r | |
15878 | } | |
15879 | ||
15880 | function Hash (hash) { | |
15881 | Base.call(this, 'digest') | |
15882 | ||
15883 | this._hash = hash | |
15884 | } | |
15885 | ||
15886 | inherits(Hash, Base) | |
15887 | ||
15888 | Hash.prototype._update = function (data) { | |
15889 | this._hash.update(data) | |
15890 | } | |
15891 | ||
15892 | Hash.prototype._final = function () { | |
15893 | return this._hash.digest() | |
15894 | } | |
15895 | ||
15896 | module.exports = function createHash (alg) { | |
15897 | alg = alg.toLowerCase() | |
15898 | if (alg === 'md5') return new HashNoConstructor(md5) | |
15899 | if (alg === 'rmd160' || alg === 'ripemd160') return new Hash(new RIPEMD160()) | |
15900 | ||
15901 | return new Hash(sha(alg)) | |
15902 | } | |
15903 | ||
15904 | }).call(this,require("buffer").Buffer) | |
15905 | },{"./md5":89,"buffer":5,"cipher-base":86,"inherits":121,"ripemd160":127,"sha.js":130}],88:[function(require,module,exports){ | |
15906 | (function (Buffer){ | |
15907 | 'use strict' | |
15908 | var intSize = 4 | |
15909 | var zeroBuffer = new Buffer(intSize) | |
15910 | zeroBuffer.fill(0) | |
15911 | ||
15912 | var charSize = 8 | |
15913 | var hashSize = 16 | |
15914 | ||
15915 | function toArray (buf) { | |
15916 | if ((buf.length % intSize) !== 0) { | |
15917 | var len = buf.length + (intSize - (buf.length % intSize)) | |
15918 | buf = Buffer.concat([buf, zeroBuffer], len) | |
15919 | } | |
15920 | ||
15921 | var arr = new Array(buf.length >>> 2) | |
15922 | for (var i = 0, j = 0; i < buf.length; i += intSize, j++) { | |
15923 | arr[j] = buf.readInt32LE(i) | |
15924 | } | |
15925 | ||
15926 | return arr | |
15927 | } | |
15928 | ||
15929 | module.exports = function hash (buf, fn) { | |
15930 | var arr = fn(toArray(buf), buf.length * charSize) | |
15931 | buf = new Buffer(hashSize) | |
15932 | for (var i = 0; i < arr.length; i++) { | |
15933 | buf.writeInt32LE(arr[i], i << 2, true) | |
15934 | } | |
15935 | return buf | |
15936 | } | |
15937 | ||
15938 | }).call(this,require("buffer").Buffer) | |
15939 | },{"buffer":5}],89:[function(require,module,exports){ | |
15940 | 'use strict' | |
15941 | /* | |
15942 | * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message | |
15943 | * Digest Algorithm, as defined in RFC 1321. | |
15944 | * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. | |
15945 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet | |
15946 | * Distributed under the BSD License | |
15947 | * See http://pajhome.org.uk/crypt/md5 for more info. | |
15948 | */ | |
15949 | ||
15950 | var makeHash = require('./make-hash') | |
15951 | ||
15952 | /* | |
15953 | * Calculate the MD5 of an array of little-endian words, and a bit length | |
15954 | */ | |
15955 | function core_md5 (x, len) { | |
15956 | /* append padding */ | |
15957 | x[len >> 5] |= 0x80 << ((len) % 32) | |
15958 | x[(((len + 64) >>> 9) << 4) + 14] = len | |
15959 | ||
15960 | var a = 1732584193 | |
15961 | var b = -271733879 | |
15962 | var c = -1732584194 | |
15963 | var d = 271733878 | |
15964 | ||
15965 | for (var i = 0; i < x.length; i += 16) { | |
15966 | var olda = a | |
15967 | var oldb = b | |
15968 | var oldc = c | |
15969 | var oldd = d | |
15970 | ||
15971 | a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936) | |
15972 | d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586) | |
15973 | c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819) | |
15974 | b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330) | |
15975 | a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897) | |
15976 | d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426) | |
15977 | c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341) | |
15978 | b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983) | |
15979 | a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416) | |
15980 | d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417) | |
15981 | c = md5_ff(c, d, a, b, x[i + 10], 17, -42063) | |
15982 | b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162) | |
15983 | a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682) | |
15984 | d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101) | |
15985 | c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290) | |
15986 | b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329) | |
15987 | ||
15988 | a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510) | |
a0091a40 IC |
15989 | d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632) |
15990 | c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713) | |
15991 | b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302) | |
15992 | a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691) | |
15993 | d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083) | |
15994 | c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335) | |
15995 | b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848) | |
15996 | a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438) | |
15997 | d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690) | |
15998 | c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961) | |
15999 | b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501) | |
16000 | a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467) | |
16001 | d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784) | |
16002 | c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473) | |
16003 | b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734) | |
16004 | ||
9f59e99b IC |
16005 | a = md5_hh(a, b, c, d, x[i + 5], 4, -378558) |
16006 | d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463) | |
16007 | c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562) | |
16008 | b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556) | |
16009 | a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060) | |
16010 | d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353) | |
16011 | c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632) | |
16012 | b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640) | |
16013 | a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174) | |
16014 | d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222) | |
16015 | c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979) | |
16016 | b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189) | |
16017 | a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487) | |
16018 | d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835) | |
16019 | c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520) | |
16020 | b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651) | |
16021 | ||
16022 | a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844) | |
16023 | d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415) | |
16024 | c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905) | |
16025 | b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055) | |
16026 | a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571) | |
16027 | d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606) | |
16028 | c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523) | |
16029 | b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799) | |
16030 | a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359) | |
16031 | d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744) | |
16032 | c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380) | |
16033 | b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649) | |
16034 | a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070) | |
16035 | d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379) | |
16036 | c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259) | |
16037 | b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551) | |
16038 | ||
16039 | a = safe_add(a, olda) | |
16040 | b = safe_add(b, oldb) | |
16041 | c = safe_add(c, oldc) | |
16042 | d = safe_add(d, oldd) | |
16043 | } | |
16044 | ||
16045 | return [a, b, c, d] | |
16046 | } | |
16047 | ||
16048 | /* | |
16049 | * These functions implement the four basic operations the algorithm uses. | |
16050 | */ | |
16051 | function md5_cmn (q, a, b, x, s, t) { | |
16052 | return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b) | |
16053 | } | |
16054 | ||
16055 | function md5_ff (a, b, c, d, x, s, t) { | |
16056 | return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t) | |
16057 | } | |
16058 | ||
16059 | function md5_gg (a, b, c, d, x, s, t) { | |
16060 | return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t) | |
16061 | } | |
16062 | ||
16063 | function md5_hh (a, b, c, d, x, s, t) { | |
16064 | return md5_cmn(b ^ c ^ d, a, b, x, s, t) | |
16065 | } | |
16066 | ||
16067 | function md5_ii (a, b, c, d, x, s, t) { | |
16068 | return md5_cmn(c ^ (b | (~d)), a, b, x, s, t) | |
16069 | } | |
16070 | ||
16071 | /* | |
16072 | * Add integers, wrapping at 2^32. This uses 16-bit operations internally | |
16073 | * to work around bugs in some JS interpreters. | |
16074 | */ | |
16075 | function safe_add (x, y) { | |
16076 | var lsw = (x & 0xFFFF) + (y & 0xFFFF) | |
16077 | var msw = (x >> 16) + (y >> 16) + (lsw >> 16) | |
16078 | return (msw << 16) | (lsw & 0xFFFF) | |
16079 | } | |
16080 | ||
16081 | /* | |
16082 | * Bitwise rotate a 32-bit number to the left. | |
16083 | */ | |
16084 | function bit_rol (num, cnt) { | |
16085 | return (num << cnt) | (num >>> (32 - cnt)) | |
16086 | } | |
16087 | ||
16088 | module.exports = function md5 (buf) { | |
16089 | return makeHash(buf, core_md5) | |
16090 | } | |
16091 | ||
16092 | },{"./make-hash":88}],90:[function(require,module,exports){ | |
16093 | 'use strict' | |
16094 | var inherits = require('inherits') | |
16095 | var Legacy = require('./legacy') | |
16096 | var Base = require('cipher-base') | |
16097 | var Buffer = require('safe-buffer').Buffer | |
16098 | var md5 = require('create-hash/md5') | |
16099 | var RIPEMD160 = require('ripemd160') | |
16100 | ||
16101 | var sha = require('sha.js') | |
16102 | ||
16103 | var ZEROS = Buffer.alloc(128) | |
16104 | ||
16105 | function Hmac (alg, key) { | |
16106 | Base.call(this, 'digest') | |
16107 | if (typeof key === 'string') { | |
16108 | key = Buffer.from(key) | |
16109 | } | |
16110 | ||
16111 | var blocksize = (alg === 'sha512' || alg === 'sha384') ? 128 : 64 | |
16112 | ||
16113 | this._alg = alg | |
16114 | this._key = key | |
16115 | if (key.length > blocksize) { | |
16116 | var hash = alg === 'rmd160' ? new RIPEMD160() : sha(alg) | |
16117 | key = hash.update(key).digest() | |
16118 | } else if (key.length < blocksize) { | |
16119 | key = Buffer.concat([key, ZEROS], blocksize) | |
16120 | } | |
16121 | ||
16122 | var ipad = this._ipad = Buffer.allocUnsafe(blocksize) | |
16123 | var opad = this._opad = Buffer.allocUnsafe(blocksize) | |
16124 | ||
16125 | for (var i = 0; i < blocksize; i++) { | |
16126 | ipad[i] = key[i] ^ 0x36 | |
16127 | opad[i] = key[i] ^ 0x5C | |
16128 | } | |
16129 | this._hash = alg === 'rmd160' ? new RIPEMD160() : sha(alg) | |
16130 | this._hash.update(ipad) | |
16131 | } | |
16132 | ||
16133 | inherits(Hmac, Base) | |
16134 | ||
16135 | Hmac.prototype._update = function (data) { | |
16136 | this._hash.update(data) | |
16137 | } | |
16138 | ||
16139 | Hmac.prototype._final = function () { | |
16140 | var h = this._hash.digest() | |
16141 | var hash = this._alg === 'rmd160' ? new RIPEMD160() : sha(this._alg) | |
16142 | return hash.update(this._opad).update(h).digest() | |
16143 | } | |
16144 | ||
16145 | module.exports = function createHmac (alg, key) { | |
16146 | alg = alg.toLowerCase() | |
16147 | if (alg === 'rmd160' || alg === 'ripemd160') { | |
16148 | return new Hmac('rmd160', key) | |
16149 | } | |
16150 | if (alg === 'md5') { | |
16151 | return new Legacy(md5, key) | |
16152 | } | |
16153 | return new Hmac(alg, key) | |
16154 | } | |
16155 | ||
16156 | },{"./legacy":91,"cipher-base":86,"create-hash/md5":89,"inherits":121,"ripemd160":127,"safe-buffer":128,"sha.js":130}],91:[function(require,module,exports){ | |
16157 | 'use strict' | |
16158 | var inherits = require('inherits') | |
16159 | var Buffer = require('safe-buffer').Buffer | |
16160 | ||
16161 | var Base = require('cipher-base') | |
16162 | ||
16163 | var ZEROS = Buffer.alloc(128) | |
16164 | var blocksize = 64 | |
16165 | ||
16166 | function Hmac (alg, key) { | |
16167 | Base.call(this, 'digest') | |
16168 | if (typeof key === 'string') { | |
16169 | key = Buffer.from(key) | |
16170 | } | |
16171 | ||
16172 | this._alg = alg | |
16173 | this._key = key | |
16174 | ||
16175 | if (key.length > blocksize) { | |
16176 | key = alg(key) | |
16177 | } else if (key.length < blocksize) { | |
16178 | key = Buffer.concat([key, ZEROS], blocksize) | |
16179 | } | |
16180 | ||
16181 | var ipad = this._ipad = Buffer.allocUnsafe(blocksize) | |
16182 | var opad = this._opad = Buffer.allocUnsafe(blocksize) | |
16183 | ||
16184 | for (var i = 0; i < blocksize; i++) { | |
16185 | ipad[i] = key[i] ^ 0x36 | |
16186 | opad[i] = key[i] ^ 0x5C | |
16187 | } | |
16188 | ||
16189 | this._hash = [ipad] | |
16190 | } | |
16191 | ||
16192 | inherits(Hmac, Base) | |
16193 | ||
16194 | Hmac.prototype._update = function (data) { | |
16195 | this._hash.push(data) | |
16196 | } | |
16197 | ||
16198 | Hmac.prototype._final = function () { | |
16199 | var h = this._alg(Buffer.concat(this._hash)) | |
16200 | return this._alg(Buffer.concat([this._opad, h])) | |
16201 | } | |
16202 | module.exports = Hmac | |
16203 | ||
16204 | },{"cipher-base":86,"inherits":121,"safe-buffer":128}],92:[function(require,module,exports){ | |
16205 | var assert = require('assert') | |
16206 | var BigInteger = require('bigi') | |
16207 | ||
16208 | var Point = require('./point') | |
16209 | ||
16210 | function Curve (p, a, b, Gx, Gy, n, h) { | |
16211 | this.p = p | |
16212 | this.a = a | |
16213 | this.b = b | |
16214 | this.G = Point.fromAffine(this, Gx, Gy) | |
16215 | this.n = n | |
16216 | this.h = h | |
16217 | ||
16218 | this.infinity = new Point(this, null, null, BigInteger.ZERO) | |
16219 | ||
16220 | // result caching | |
16221 | this.pOverFour = p.add(BigInteger.ONE).shiftRight(2) | |
16222 | ||
16223 | // determine size of p in bytes | |
16224 | this.pLength = Math.floor((this.p.bitLength() + 7) / 8) | |
16225 | } | |
16226 | ||
16227 | Curve.prototype.pointFromX = function (isOdd, x) { | |
16228 | var alpha = x.pow(3).add(this.a.multiply(x)).add(this.b).mod(this.p) | |
16229 | var beta = alpha.modPow(this.pOverFour, this.p) // XXX: not compatible with all curves | |
16230 | ||
16231 | var y = beta | |
16232 | if (beta.isEven() ^ !isOdd) { | |
16233 | y = this.p.subtract(y) // -y % p | |
16234 | } | |
16235 | ||
16236 | return Point.fromAffine(this, x, y) | |
16237 | } | |
16238 | ||
16239 | Curve.prototype.isInfinity = function (Q) { | |
16240 | if (Q === this.infinity) return true | |
16241 | ||
16242 | return Q.z.signum() === 0 && Q.y.signum() !== 0 | |
16243 | } | |
16244 | ||
16245 | Curve.prototype.isOnCurve = function (Q) { | |
16246 | if (this.isInfinity(Q)) return true | |
16247 | ||
16248 | var x = Q.affineX | |
16249 | var y = Q.affineY | |
16250 | var a = this.a | |
16251 | var b = this.b | |
16252 | var p = this.p | |
16253 | ||
16254 | // Check that xQ and yQ are integers in the interval [0, p - 1] | |
16255 | if (x.signum() < 0 || x.compareTo(p) >= 0) return false | |
16256 | if (y.signum() < 0 || y.compareTo(p) >= 0) return false | |
16257 | ||
16258 | // and check that y^2 = x^3 + ax + b (mod p) | |
16259 | var lhs = y.square().mod(p) | |
16260 | var rhs = x.pow(3).add(a.multiply(x)).add(b).mod(p) | |
16261 | return lhs.equals(rhs) | |
16262 | } | |
16263 | ||
16264 | /** | |
16265 | * Validate an elliptic curve point. | |
16266 | * | |
16267 | * See SEC 1, section 3.2.2.1: Elliptic Curve Public Key Validation Primitive | |
16268 | */ | |
16269 | Curve.prototype.validate = function (Q) { | |
16270 | // Check Q != O | |
16271 | assert(!this.isInfinity(Q), 'Point is at infinity') | |
16272 | assert(this.isOnCurve(Q), 'Point is not on the curve') | |
16273 | ||
16274 | // Check nQ = O (where Q is a scalar multiple of G) | |
16275 | var nQ = Q.multiply(this.n) | |
16276 | assert(this.isInfinity(nQ), 'Point is not a scalar multiple of G') | |
16277 | ||
16278 | return true | |
16279 | } | |
16280 | ||
16281 | module.exports = Curve | |
16282 | ||
16283 | },{"./point":96,"assert":1,"bigi":39}],93:[function(require,module,exports){ | |
16284 | module.exports={ | |
16285 | "secp128r1": { | |
16286 | "p": "fffffffdffffffffffffffffffffffff", | |
16287 | "a": "fffffffdfffffffffffffffffffffffc", | |
16288 | "b": "e87579c11079f43dd824993c2cee5ed3", | |
16289 | "n": "fffffffe0000000075a30d1b9038a115", | |
16290 | "h": "01", | |
16291 | "Gx": "161ff7528b899b2d0c28607ca52c5b86", | |
16292 | "Gy": "cf5ac8395bafeb13c02da292dded7a83" | |
16293 | }, | |
16294 | "secp160k1": { | |
16295 | "p": "fffffffffffffffffffffffffffffffeffffac73", | |
16296 | "a": "00", | |
16297 | "b": "07", | |
16298 | "n": "0100000000000000000001b8fa16dfab9aca16b6b3", | |
16299 | "h": "01", | |
16300 | "Gx": "3b4c382ce37aa192a4019e763036f4f5dd4d7ebb", | |
16301 | "Gy": "938cf935318fdced6bc28286531733c3f03c4fee" | |
16302 | }, | |
16303 | "secp160r1": { | |
16304 | "p": "ffffffffffffffffffffffffffffffff7fffffff", | |
16305 | "a": "ffffffffffffffffffffffffffffffff7ffffffc", | |
16306 | "b": "1c97befc54bd7a8b65acf89f81d4d4adc565fa45", | |
16307 | "n": "0100000000000000000001f4c8f927aed3ca752257", | |
16308 | "h": "01", | |
16309 | "Gx": "4a96b5688ef573284664698968c38bb913cbfc82", | |
16310 | "Gy": "23a628553168947d59dcc912042351377ac5fb32" | |
16311 | }, | |
16312 | "secp192k1": { | |
16313 | "p": "fffffffffffffffffffffffffffffffffffffffeffffee37", | |
16314 | "a": "00", | |
16315 | "b": "03", | |
16316 | "n": "fffffffffffffffffffffffe26f2fc170f69466a74defd8d", | |
16317 | "h": "01", | |
16318 | "Gx": "db4ff10ec057e9ae26b07d0280b7f4341da5d1b1eae06c7d", | |
16319 | "Gy": "9b2f2f6d9c5628a7844163d015be86344082aa88d95e2f9d" | |
16320 | }, | |
16321 | "secp192r1": { | |
16322 | "p": "fffffffffffffffffffffffffffffffeffffffffffffffff", | |
16323 | "a": "fffffffffffffffffffffffffffffffefffffffffffffffc", | |
16324 | "b": "64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", | |
16325 | "n": "ffffffffffffffffffffffff99def836146bc9b1b4d22831", | |
16326 | "h": "01", | |
16327 | "Gx": "188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012", | |
16328 | "Gy": "07192b95ffc8da78631011ed6b24cdd573f977a11e794811" | |
16329 | }, | |
16330 | "secp256k1": { | |
16331 | "p": "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", | |
16332 | "a": "00", | |
16333 | "b": "07", | |
16334 | "n": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", | |
16335 | "h": "01", | |
16336 | "Gx": "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", | |
16337 | "Gy": "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8" | |
16338 | }, | |
16339 | "secp256r1": { | |
16340 | "p": "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", | |
16341 | "a": "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", | |
16342 | "b": "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", | |
16343 | "n": "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", | |
16344 | "h": "01", | |
16345 | "Gx": "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", | |
16346 | "Gy": "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5" | |
16347 | } | |
16348 | } | |
16349 | ||
16350 | },{}],94:[function(require,module,exports){ | |
16351 | var Point = require('./point') | |
16352 | var Curve = require('./curve') | |
16353 | ||
16354 | var getCurveByName = require('./names') | |
16355 | ||
16356 | module.exports = { | |
16357 | Curve: Curve, | |
16358 | Point: Point, | |
16359 | getCurveByName: getCurveByName | |
16360 | } | |
16361 | ||
16362 | },{"./curve":92,"./names":95,"./point":96}],95:[function(require,module,exports){ | |
16363 | var BigInteger = require('bigi') | |
16364 | ||
16365 | var curves = require('./curves.json') | |
16366 | var Curve = require('./curve') | |
16367 | ||
16368 | function getCurveByName (name) { | |
16369 | var curve = curves[name] | |
16370 | if (!curve) return null | |
16371 | ||
16372 | var p = new BigInteger(curve.p, 16) | |
16373 | var a = new BigInteger(curve.a, 16) | |
16374 | var b = new BigInteger(curve.b, 16) | |
16375 | var n = new BigInteger(curve.n, 16) | |
16376 | var h = new BigInteger(curve.h, 16) | |
16377 | var Gx = new BigInteger(curve.Gx, 16) | |
16378 | var Gy = new BigInteger(curve.Gy, 16) | |
16379 | ||
16380 | return new Curve(p, a, b, Gx, Gy, n, h) | |
16381 | } | |
16382 | ||
16383 | module.exports = getCurveByName | |
16384 | ||
16385 | },{"./curve":92,"./curves.json":93,"bigi":39}],96:[function(require,module,exports){ | |
16386 | (function (Buffer){ | |
16387 | var assert = require('assert') | |
16388 | var BigInteger = require('bigi') | |
16389 | ||
16390 | var THREE = BigInteger.valueOf(3) | |
16391 | ||
16392 | function Point (curve, x, y, z) { | |
16393 | assert.notStrictEqual(z, undefined, 'Missing Z coordinate') | |
16394 | ||
16395 | this.curve = curve | |
16396 | this.x = x | |
16397 | this.y = y | |
16398 | this.z = z | |
16399 | this._zInv = null | |
16400 | ||
16401 | this.compressed = true | |
16402 | } | |
16403 | ||
16404 | Object.defineProperty(Point.prototype, 'zInv', { | |
16405 | get: function () { | |
16406 | if (this._zInv === null) { | |
16407 | this._zInv = this.z.modInverse(this.curve.p) | |
16408 | } | |
16409 | ||
16410 | return this._zInv | |
16411 | } | |
16412 | }) | |
16413 | ||
16414 | Object.defineProperty(Point.prototype, 'affineX', { | |
16415 | get: function () { | |
16416 | return this.x.multiply(this.zInv).mod(this.curve.p) | |
16417 | } | |
16418 | }) | |
16419 | ||
16420 | Object.defineProperty(Point.prototype, 'affineY', { | |
16421 | get: function () { | |
16422 | return this.y.multiply(this.zInv).mod(this.curve.p) | |
16423 | } | |
16424 | }) | |
16425 | ||
16426 | Point.fromAffine = function (curve, x, y) { | |
16427 | return new Point(curve, x, y, BigInteger.ONE) | |
16428 | } | |
16429 | ||
16430 | Point.prototype.equals = function (other) { | |
16431 | if (other === this) return true | |
16432 | if (this.curve.isInfinity(this)) return this.curve.isInfinity(other) | |
16433 | if (this.curve.isInfinity(other)) return this.curve.isInfinity(this) | |
16434 | ||
16435 | // u = Y2 * Z1 - Y1 * Z2 | |
16436 | var u = other.y.multiply(this.z).subtract(this.y.multiply(other.z)).mod(this.curve.p) | |
16437 | ||
16438 | if (u.signum() !== 0) return false | |
16439 | ||
16440 | // v = X2 * Z1 - X1 * Z2 | |
16441 | var v = other.x.multiply(this.z).subtract(this.x.multiply(other.z)).mod(this.curve.p) | |
16442 | ||
16443 | return v.signum() === 0 | |
16444 | } | |
16445 | ||
16446 | Point.prototype.negate = function () { | |
16447 | var y = this.curve.p.subtract(this.y) | |
16448 | ||
16449 | return new Point(this.curve, this.x, y, this.z) | |
16450 | } | |
16451 | ||
16452 | Point.prototype.add = function (b) { | |
16453 | if (this.curve.isInfinity(this)) return b | |
16454 | if (this.curve.isInfinity(b)) return this | |
16455 | ||
16456 | var x1 = this.x | |
16457 | var y1 = this.y | |
16458 | var x2 = b.x | |
16459 | var y2 = b.y | |
16460 | ||
16461 | // u = Y2 * Z1 - Y1 * Z2 | |
16462 | var u = y2.multiply(this.z).subtract(y1.multiply(b.z)).mod(this.curve.p) | |
16463 | // v = X2 * Z1 - X1 * Z2 | |
16464 | var v = x2.multiply(this.z).subtract(x1.multiply(b.z)).mod(this.curve.p) | |
16465 | ||
16466 | if (v.signum() === 0) { | |
16467 | if (u.signum() === 0) { | |
16468 | return this.twice() // this == b, so double | |
16469 | } | |
16470 | ||
16471 | return this.curve.infinity // this = -b, so infinity | |
16472 | } | |
16473 | ||
16474 | var v2 = v.square() | |
16475 | var v3 = v2.multiply(v) | |
16476 | var x1v2 = x1.multiply(v2) | |
16477 | var zu2 = u.square().multiply(this.z) | |
16478 | ||
16479 | // x3 = v * (z2 * (z1 * u^2 - 2 * x1 * v^2) - v^3) | |
16480 | var x3 = zu2.subtract(x1v2.shiftLeft(1)).multiply(b.z).subtract(v3).multiply(v).mod(this.curve.p) | |
16481 | // y3 = z2 * (3 * x1 * u * v^2 - y1 * v^3 - z1 * u^3) + u * v^3 | |
16482 | var y3 = x1v2.multiply(THREE).multiply(u).subtract(y1.multiply(v3)).subtract(zu2.multiply(u)).multiply(b.z).add(u.multiply(v3)).mod(this.curve.p) | |
16483 | // z3 = v^3 * z1 * z2 | |
16484 | var z3 = v3.multiply(this.z).multiply(b.z).mod(this.curve.p) | |
16485 | ||
16486 | return new Point(this.curve, x3, y3, z3) | |
16487 | } | |
16488 | ||
16489 | Point.prototype.twice = function () { | |
16490 | if (this.curve.isInfinity(this)) return this | |
16491 | if (this.y.signum() === 0) return this.curve.infinity | |
16492 | ||
16493 | var x1 = this.x | |
16494 | var y1 = this.y | |
16495 | ||
16496 | var y1z1 = y1.multiply(this.z).mod(this.curve.p) | |
16497 | var y1sqz1 = y1z1.multiply(y1).mod(this.curve.p) | |
16498 | var a = this.curve.a | |
16499 | ||
16500 | // w = 3 * x1^2 + a * z1^2 | |
16501 | var w = x1.square().multiply(THREE) | |
16502 | ||
16503 | if (a.signum() !== 0) { | |
16504 | w = w.add(this.z.square().multiply(a)) | |
16505 | } | |
16506 | ||
16507 | w = w.mod(this.curve.p) | |
16508 | // x3 = 2 * y1 * z1 * (w^2 - 8 * x1 * y1^2 * z1) | |
16509 | var x3 = w.square().subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.p) | |
16510 | // y3 = 4 * y1^2 * z1 * (3 * w * x1 - 2 * y1^2 * z1) - w^3 | |
16511 | var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.pow(3)).mod(this.curve.p) | |
16512 | // z3 = 8 * (y1 * z1)^3 | |
16513 | var z3 = y1z1.pow(3).shiftLeft(3).mod(this.curve.p) | |
16514 | ||
16515 | return new Point(this.curve, x3, y3, z3) | |
16516 | } | |
16517 | ||
16518 | // Simple NAF (Non-Adjacent Form) multiplication algorithm | |
16519 | // TODO: modularize the multiplication algorithm | |
16520 | Point.prototype.multiply = function (k) { | |
16521 | if (this.curve.isInfinity(this)) return this | |
16522 | if (k.signum() === 0) return this.curve.infinity | |
16523 | ||
16524 | var e = k | |
16525 | var h = e.multiply(THREE) | |
16526 | ||
16527 | var neg = this.negate() | |
16528 | var R = this | |
16529 | ||
16530 | for (var i = h.bitLength() - 2; i > 0; --i) { | |
16531 | var hBit = h.testBit(i) | |
16532 | var eBit = e.testBit(i) | |
16533 | ||
16534 | R = R.twice() | |
16535 | ||
16536 | if (hBit !== eBit) { | |
16537 | R = R.add(hBit ? this : neg) | |
16538 | } | |
16539 | } | |
16540 | ||
16541 | return R | |
16542 | } | |
16543 | ||
16544 | // Compute this*j + x*k (simultaneous multiplication) | |
16545 | Point.prototype.multiplyTwo = function (j, x, k) { | |
16546 | var i = Math.max(j.bitLength(), k.bitLength()) - 1 | |
16547 | var R = this.curve.infinity | |
16548 | var both = this.add(x) | |
16549 | ||
16550 | while (i >= 0) { | |
16551 | var jBit = j.testBit(i) | |
16552 | var kBit = k.testBit(i) | |
16553 | ||
16554 | R = R.twice() | |
16555 | ||
16556 | if (jBit) { | |
16557 | if (kBit) { | |
16558 | R = R.add(both) | |
16559 | } else { | |
16560 | R = R.add(this) | |
16561 | } | |
16562 | } else if (kBit) { | |
16563 | R = R.add(x) | |
16564 | } | |
16565 | --i | |
16566 | } | |
16567 | ||
16568 | return R | |
16569 | } | |
16570 | ||
16571 | Point.prototype.getEncoded = function (compressed) { | |
16572 | if (compressed == null) compressed = this.compressed | |
16573 | if (this.curve.isInfinity(this)) return new Buffer('00', 'hex') // Infinity point encoded is simply '00' | |
16574 | ||
16575 | var x = this.affineX | |
16576 | var y = this.affineY | |
16577 | var byteLength = this.curve.pLength | |
16578 | var buffer | |
16579 | ||
16580 | // 0x02/0x03 | X | |
16581 | if (compressed) { | |
16582 | buffer = new Buffer(1 + byteLength) | |
16583 | buffer.writeUInt8(y.isEven() ? 0x02 : 0x03, 0) | |
16584 | ||
16585 | // 0x04 | X | Y | |
16586 | } else { | |
16587 | buffer = new Buffer(1 + byteLength + byteLength) | |
16588 | buffer.writeUInt8(0x04, 0) | |
16589 | ||
16590 | y.toBuffer(byteLength).copy(buffer, 1 + byteLength) | |
16591 | } | |
16592 | ||
16593 | x.toBuffer(byteLength).copy(buffer, 1) | |
16594 | ||
16595 | return buffer | |
16596 | } | |
16597 | ||
16598 | Point.decodeFrom = function (curve, buffer) { | |
16599 | var type = buffer.readUInt8(0) | |
16600 | var compressed = (type !== 4) | |
16601 | ||
16602 | var byteLength = Math.floor((curve.p.bitLength() + 7) / 8) | |
16603 | var x = BigInteger.fromBuffer(buffer.slice(1, 1 + byteLength)) | |
16604 | ||
16605 | var Q | |
16606 | if (compressed) { | |
16607 | assert.equal(buffer.length, byteLength + 1, 'Invalid sequence length') | |
16608 | assert(type === 0x02 || type === 0x03, 'Invalid sequence tag') | |
16609 | ||
16610 | var isOdd = (type === 0x03) | |
16611 | Q = curve.pointFromX(isOdd, x) | |
16612 | } else { | |
16613 | assert.equal(buffer.length, 1 + byteLength + byteLength, 'Invalid sequence length') | |
16614 | ||
16615 | var y = BigInteger.fromBuffer(buffer.slice(1 + byteLength)) | |
16616 | Q = Point.fromAffine(curve, x, y) | |
16617 | } | |
16618 | ||
16619 | Q.compressed = compressed | |
16620 | return Q | |
16621 | } | |
16622 | ||
16623 | Point.prototype.toString = function () { | |
16624 | if (this.curve.isInfinity(this)) return '(INFINITY)' | |
16625 | ||
16626 | return '(' + this.affineX.toString() + ',' + this.affineY.toString() + ')' | |
16627 | } | |
16628 | ||
16629 | module.exports = Point | |
16630 | ||
16631 | }).call(this,require("buffer").Buffer) | |
16632 | },{"assert":1,"bigi":39,"buffer":5}],97:[function(require,module,exports){ | |
16633 | 'use strict'; | |
16634 | ||
16635 | var elliptic = exports; | |
16636 | ||
16637 | elliptic.version = require('../package.json').version; | |
16638 | elliptic.utils = require('./elliptic/utils'); | |
16639 | elliptic.rand = require('brorand'); | |
16640 | elliptic.curve = require('./elliptic/curve'); | |
16641 | elliptic.curves = require('./elliptic/curves'); | |
16642 | ||
16643 | // Protocols | |
16644 | elliptic.ec = require('./elliptic/ec'); | |
16645 | elliptic.eddsa = require('./elliptic/eddsa'); | |
16646 | ||
16647 | },{"../package.json":112,"./elliptic/curve":100,"./elliptic/curves":103,"./elliptic/ec":104,"./elliptic/eddsa":107,"./elliptic/utils":111,"brorand":82}],98:[function(require,module,exports){ | |
16648 | 'use strict'; | |
16649 | ||
16650 | var BN = require('bn.js'); | |
16651 | var elliptic = require('../../elliptic'); | |
16652 | var utils = elliptic.utils; | |
16653 | var getNAF = utils.getNAF; | |
16654 | var getJSF = utils.getJSF; | |
16655 | var assert = utils.assert; | |
16656 | ||
16657 | function BaseCurve(type, conf) { | |
16658 | this.type = type; | |
16659 | this.p = new BN(conf.p, 16); | |
16660 | ||
16661 | // Use Montgomery, when there is no fast reduction for the prime | |
16662 | this.red = conf.prime ? BN.red(conf.prime) : BN.mont(this.p); | |
16663 | ||
16664 | // Useful for many curves | |
16665 | this.zero = new BN(0).toRed(this.red); | |
16666 | this.one = new BN(1).toRed(this.red); | |
16667 | this.two = new BN(2).toRed(this.red); | |
16668 | ||
16669 | // Curve configuration, optional | |
16670 | this.n = conf.n && new BN(conf.n, 16); | |
16671 | this.g = conf.g && this.pointFromJSON(conf.g, conf.gRed); | |
16672 | ||
16673 | // Temporary arrays | |
16674 | this._wnafT1 = new Array(4); | |
16675 | this._wnafT2 = new Array(4); | |
16676 | this._wnafT3 = new Array(4); | |
16677 | this._wnafT4 = new Array(4); | |
16678 | ||
16679 | // Generalized Greg Maxwell's trick | |
16680 | var adjustCount = this.n && this.p.div(this.n); | |
16681 | if (!adjustCount || adjustCount.cmpn(100) > 0) { | |
16682 | this.redN = null; | |
16683 | } else { | |
16684 | this._maxwellTrick = true; | |
16685 | this.redN = this.n.toRed(this.red); | |
16686 | } | |
16687 | } | |
16688 | module.exports = BaseCurve; | |
16689 | ||
16690 | BaseCurve.prototype.point = function point() { | |
16691 | throw new Error('Not implemented'); | |
16692 | }; | |
16693 | ||
16694 | BaseCurve.prototype.validate = function validate() { | |
16695 | throw new Error('Not implemented'); | |
16696 | }; | |
16697 | ||
16698 | BaseCurve.prototype._fixedNafMul = function _fixedNafMul(p, k) { | |
16699 | assert(p.precomputed); | |
16700 | var doubles = p._getDoubles(); | |
16701 | ||
16702 | var naf = getNAF(k, 1); | |
16703 | var I = (1 << (doubles.step + 1)) - (doubles.step % 2 === 0 ? 2 : 1); | |
16704 | I /= 3; | |
16705 | ||
16706 | // Translate into more windowed form | |
16707 | var repr = []; | |
16708 | for (var j = 0; j < naf.length; j += doubles.step) { | |
16709 | var nafW = 0; | |
16710 | for (var k = j + doubles.step - 1; k >= j; k--) | |
16711 | nafW = (nafW << 1) + naf[k]; | |
16712 | repr.push(nafW); | |
16713 | } | |
16714 | ||
16715 | var a = this.jpoint(null, null, null); | |
16716 | var b = this.jpoint(null, null, null); | |
16717 | for (var i = I; i > 0; i--) { | |
16718 | for (var j = 0; j < repr.length; j++) { | |
16719 | var nafW = repr[j]; | |
16720 | if (nafW === i) | |
16721 | b = b.mixedAdd(doubles.points[j]); | |
16722 | else if (nafW === -i) | |
16723 | b = b.mixedAdd(doubles.points[j].neg()); | |
16724 | } | |
16725 | a = a.add(b); | |
16726 | } | |
16727 | return a.toP(); | |
16728 | }; | |
16729 | ||
16730 | BaseCurve.prototype._wnafMul = function _wnafMul(p, k) { | |
16731 | var w = 4; | |
16732 | ||
16733 | // Precompute window | |
16734 | var nafPoints = p._getNAFPoints(w); | |
16735 | w = nafPoints.wnd; | |
16736 | var wnd = nafPoints.points; | |
16737 | ||
16738 | // Get NAF form | |
16739 | var naf = getNAF(k, w); | |
16740 | ||
16741 | // Add `this`*(N+1) for every w-NAF index | |
16742 | var acc = this.jpoint(null, null, null); | |
16743 | for (var i = naf.length - 1; i >= 0; i--) { | |
16744 | // Count zeroes | |
16745 | for (var k = 0; i >= 0 && naf[i] === 0; i--) | |
16746 | k++; | |
16747 | if (i >= 0) | |
16748 | k++; | |
16749 | acc = acc.dblp(k); | |
16750 | ||
16751 | if (i < 0) | |
16752 | break; | |
16753 | var z = naf[i]; | |
16754 | assert(z !== 0); | |
16755 | if (p.type === 'affine') { | |
16756 | // J +- P | |
16757 | if (z > 0) | |
16758 | acc = acc.mixedAdd(wnd[(z - 1) >> 1]); | |
16759 | else | |
16760 | acc = acc.mixedAdd(wnd[(-z - 1) >> 1].neg()); | |
16761 | } else { | |
16762 | // J +- J | |
16763 | if (z > 0) | |
16764 | acc = acc.add(wnd[(z - 1) >> 1]); | |
16765 | else | |
16766 | acc = acc.add(wnd[(-z - 1) >> 1].neg()); | |
16767 | } | |
16768 | } | |
16769 | return p.type === 'affine' ? acc.toP() : acc; | |
16770 | }; | |
16771 | ||
16772 | BaseCurve.prototype._wnafMulAdd = function _wnafMulAdd(defW, | |
16773 | points, | |
16774 | coeffs, | |
16775 | len, | |
16776 | jacobianResult) { | |
16777 | var wndWidth = this._wnafT1; | |
16778 | var wnd = this._wnafT2; | |
16779 | var naf = this._wnafT3; | |
16780 | ||
16781 | // Fill all arrays | |
16782 | var max = 0; | |
16783 | for (var i = 0; i < len; i++) { | |
16784 | var p = points[i]; | |
16785 | var nafPoints = p._getNAFPoints(defW); | |
16786 | wndWidth[i] = nafPoints.wnd; | |
16787 | wnd[i] = nafPoints.points; | |
16788 | } | |
16789 | ||
16790 | // Comb small window NAFs | |
16791 | for (var i = len - 1; i >= 1; i -= 2) { | |
16792 | var a = i - 1; | |
16793 | var b = i; | |
16794 | if (wndWidth[a] !== 1 || wndWidth[b] !== 1) { | |
16795 | naf[a] = getNAF(coeffs[a], wndWidth[a]); | |
16796 | naf[b] = getNAF(coeffs[b], wndWidth[b]); | |
16797 | max = Math.max(naf[a].length, max); | |
16798 | max = Math.max(naf[b].length, max); | |
16799 | continue; | |
16800 | } | |
16801 | ||
16802 | var comb = [ | |
16803 | points[a], /* 1 */ | |
16804 | null, /* 3 */ | |
16805 | null, /* 5 */ | |
16806 | points[b] /* 7 */ | |
16807 | ]; | |
16808 | ||
16809 | // Try to avoid Projective points, if possible | |
16810 | if (points[a].y.cmp(points[b].y) === 0) { | |
16811 | comb[1] = points[a].add(points[b]); | |
16812 | comb[2] = points[a].toJ().mixedAdd(points[b].neg()); | |
16813 | } else if (points[a].y.cmp(points[b].y.redNeg()) === 0) { | |
16814 | comb[1] = points[a].toJ().mixedAdd(points[b]); | |
16815 | comb[2] = points[a].add(points[b].neg()); | |
16816 | } else { | |
16817 | comb[1] = points[a].toJ().mixedAdd(points[b]); | |
16818 | comb[2] = points[a].toJ().mixedAdd(points[b].neg()); | |
16819 | } | |
16820 | ||
16821 | var index = [ | |
16822 | -3, /* -1 -1 */ | |
16823 | -1, /* -1 0 */ | |
16824 | -5, /* -1 1 */ | |
16825 | -7, /* 0 -1 */ | |
16826 | 0, /* 0 0 */ | |
16827 | 7, /* 0 1 */ | |
16828 | 5, /* 1 -1 */ | |
16829 | 1, /* 1 0 */ | |
16830 | 3 /* 1 1 */ | |
16831 | ]; | |
16832 | ||
16833 | var jsf = getJSF(coeffs[a], coeffs[b]); | |
16834 | max = Math.max(jsf[0].length, max); | |
16835 | naf[a] = new Array(max); | |
16836 | naf[b] = new Array(max); | |
16837 | for (var j = 0; j < max; j++) { | |
16838 | var ja = jsf[0][j] | 0; | |
16839 | var jb = jsf[1][j] | 0; | |
16840 | ||
16841 | naf[a][j] = index[(ja + 1) * 3 + (jb + 1)]; | |
16842 | naf[b][j] = 0; | |
16843 | wnd[a] = comb; | |
16844 | } | |
16845 | } | |
16846 | ||
16847 | var acc = this.jpoint(null, null, null); | |
16848 | var tmp = this._wnafT4; | |
16849 | for (var i = max; i >= 0; i--) { | |
16850 | var k = 0; | |
16851 | ||
16852 | while (i >= 0) { | |
16853 | var zero = true; | |
16854 | for (var j = 0; j < len; j++) { | |
16855 | tmp[j] = naf[j][i] | 0; | |
16856 | if (tmp[j] !== 0) | |
16857 | zero = false; | |
16858 | } | |
16859 | if (!zero) | |
16860 | break; | |
16861 | k++; | |
16862 | i--; | |
16863 | } | |
16864 | if (i >= 0) | |
16865 | k++; | |
16866 | acc = acc.dblp(k); | |
16867 | if (i < 0) | |
16868 | break; | |
16869 | ||
16870 | for (var j = 0; j < len; j++) { | |
16871 | var z = tmp[j]; | |
16872 | var p; | |
16873 | if (z === 0) | |
16874 | continue; | |
16875 | else if (z > 0) | |
16876 | p = wnd[j][(z - 1) >> 1]; | |
16877 | else if (z < 0) | |
16878 | p = wnd[j][(-z - 1) >> 1].neg(); | |
16879 | ||
16880 | if (p.type === 'affine') | |
16881 | acc = acc.mixedAdd(p); | |
16882 | else | |
16883 | acc = acc.add(p); | |
16884 | } | |
16885 | } | |
16886 | // Zeroify references | |
16887 | for (var i = 0; i < len; i++) | |
16888 | wnd[i] = null; | |
16889 | ||
16890 | if (jacobianResult) | |
16891 | return acc; | |
16892 | else | |
16893 | return acc.toP(); | |
16894 | }; | |
16895 | ||
16896 | function BasePoint(curve, type) { | |
16897 | this.curve = curve; | |
16898 | this.type = type; | |
16899 | this.precomputed = null; | |
16900 | } | |
16901 | BaseCurve.BasePoint = BasePoint; | |
16902 | ||
16903 | BasePoint.prototype.eq = function eq(/*other*/) { | |
16904 | throw new Error('Not implemented'); | |
16905 | }; | |
16906 | ||
16907 | BasePoint.prototype.validate = function validate() { | |
16908 | return this.curve.validate(this); | |
16909 | }; | |
16910 | ||
16911 | BaseCurve.prototype.decodePoint = function decodePoint(bytes, enc) { | |
16912 | bytes = utils.toArray(bytes, enc); | |
16913 | ||
16914 | var len = this.p.byteLength(); | |
16915 | ||
16916 | // uncompressed, hybrid-odd, hybrid-even | |
16917 | if ((bytes[0] === 0x04 || bytes[0] === 0x06 || bytes[0] === 0x07) && | |
16918 | bytes.length - 1 === 2 * len) { | |
16919 | if (bytes[0] === 0x06) | |
16920 | assert(bytes[bytes.length - 1] % 2 === 0); | |
16921 | else if (bytes[0] === 0x07) | |
16922 | assert(bytes[bytes.length - 1] % 2 === 1); | |
16923 | ||
16924 | var res = this.point(bytes.slice(1, 1 + len), | |
16925 | bytes.slice(1 + len, 1 + 2 * len)); | |
16926 | ||
16927 | return res; | |
16928 | } else if ((bytes[0] === 0x02 || bytes[0] === 0x03) && | |
16929 | bytes.length - 1 === len) { | |
16930 | return this.pointFromX(bytes.slice(1, 1 + len), bytes[0] === 0x03); | |
16931 | } | |
16932 | throw new Error('Unknown point format'); | |
16933 | }; | |
16934 | ||
16935 | BasePoint.prototype.encodeCompressed = function encodeCompressed(enc) { | |
16936 | return this.encode(enc, true); | |
16937 | }; | |
16938 | ||
16939 | BasePoint.prototype._encode = function _encode(compact) { | |
16940 | var len = this.curve.p.byteLength(); | |
16941 | var x = this.getX().toArray('be', len); | |
16942 | ||
16943 | if (compact) | |
16944 | return [ this.getY().isEven() ? 0x02 : 0x03 ].concat(x); | |
16945 | ||
16946 | return [ 0x04 ].concat(x, this.getY().toArray('be', len)) ; | |
16947 | }; | |
16948 | ||
16949 | BasePoint.prototype.encode = function encode(enc, compact) { | |
16950 | return utils.encode(this._encode(compact), enc); | |
16951 | }; | |
16952 | ||
16953 | BasePoint.prototype.precompute = function precompute(power) { | |
16954 | if (this.precomputed) | |
16955 | return this; | |
16956 | ||
16957 | var precomputed = { | |
16958 | doubles: null, | |
16959 | naf: null, | |
16960 | beta: null | |
16961 | }; | |
16962 | precomputed.naf = this._getNAFPoints(8); | |
16963 | precomputed.doubles = this._getDoubles(4, power); | |
16964 | precomputed.beta = this._getBeta(); | |
16965 | this.precomputed = precomputed; | |
16966 | ||
16967 | return this; | |
16968 | }; | |
16969 | ||
16970 | BasePoint.prototype._hasDoubles = function _hasDoubles(k) { | |
16971 | if (!this.precomputed) | |
16972 | return false; | |
16973 | ||
16974 | var doubles = this.precomputed.doubles; | |
16975 | if (!doubles) | |
16976 | return false; | |
16977 | ||
16978 | return doubles.points.length >= Math.ceil((k.bitLength() + 1) / doubles.step); | |
16979 | }; | |
16980 | ||
16981 | BasePoint.prototype._getDoubles = function _getDoubles(step, power) { | |
16982 | if (this.precomputed && this.precomputed.doubles) | |
16983 | return this.precomputed.doubles; | |
16984 | ||
16985 | var doubles = [ this ]; | |
16986 | var acc = this; | |
16987 | for (var i = 0; i < power; i += step) { | |
16988 | for (var j = 0; j < step; j++) | |
16989 | acc = acc.dbl(); | |
16990 | doubles.push(acc); | |
16991 | } | |
16992 | return { | |
16993 | step: step, | |
16994 | points: doubles | |
16995 | }; | |
16996 | }; | |
16997 | ||
16998 | BasePoint.prototype._getNAFPoints = function _getNAFPoints(wnd) { | |
16999 | if (this.precomputed && this.precomputed.naf) | |
17000 | return this.precomputed.naf; | |
17001 | ||
17002 | var res = [ this ]; | |
17003 | var max = (1 << wnd) - 1; | |
17004 | var dbl = max === 1 ? null : this.dbl(); | |
17005 | for (var i = 1; i < max; i++) | |
17006 | res[i] = res[i - 1].add(dbl); | |
17007 | return { | |
17008 | wnd: wnd, | |
17009 | points: res | |
17010 | }; | |
17011 | }; | |
17012 | ||
17013 | BasePoint.prototype._getBeta = function _getBeta() { | |
17014 | return null; | |
17015 | }; | |
17016 | ||
17017 | BasePoint.prototype.dblp = function dblp(k) { | |
17018 | var r = this; | |
17019 | for (var i = 0; i < k; i++) | |
17020 | r = r.dbl(); | |
17021 | return r; | |
17022 | }; | |
17023 | ||
17024 | },{"../../elliptic":97,"bn.js":81}],99:[function(require,module,exports){ | |
17025 | 'use strict'; | |
17026 | ||
17027 | var curve = require('../curve'); | |
17028 | var elliptic = require('../../elliptic'); | |
17029 | var BN = require('bn.js'); | |
17030 | var inherits = require('inherits'); | |
17031 | var Base = curve.base; | |
17032 | ||
17033 | var assert = elliptic.utils.assert; | |
17034 | ||
17035 | function EdwardsCurve(conf) { | |
17036 | // NOTE: Important as we are creating point in Base.call() | |
17037 | this.twisted = (conf.a | 0) !== 1; | |
17038 | this.mOneA = this.twisted && (conf.a | 0) === -1; | |
17039 | this.extended = this.mOneA; | |
17040 | ||
17041 | Base.call(this, 'edwards', conf); | |
17042 | ||
17043 | this.a = new BN(conf.a, 16).umod(this.red.m); | |
17044 | this.a = this.a.toRed(this.red); | |
17045 | this.c = new BN(conf.c, 16).toRed(this.red); | |
17046 | this.c2 = this.c.redSqr(); | |
17047 | this.d = new BN(conf.d, 16).toRed(this.red); | |
17048 | this.dd = this.d.redAdd(this.d); | |
17049 | ||
17050 | assert(!this.twisted || this.c.fromRed().cmpn(1) === 0); | |
17051 | this.oneC = (conf.c | 0) === 1; | |
17052 | } | |
17053 | inherits(EdwardsCurve, Base); | |
17054 | module.exports = EdwardsCurve; | |
17055 | ||
17056 | EdwardsCurve.prototype._mulA = function _mulA(num) { | |
17057 | if (this.mOneA) | |
17058 | return num.redNeg(); | |
17059 | else | |
17060 | return this.a.redMul(num); | |
17061 | }; | |
17062 | ||
17063 | EdwardsCurve.prototype._mulC = function _mulC(num) { | |
17064 | if (this.oneC) | |
17065 | return num; | |
17066 | else | |
17067 | return this.c.redMul(num); | |
17068 | }; | |
17069 | ||
17070 | // Just for compatibility with Short curve | |
17071 | EdwardsCurve.prototype.jpoint = function jpoint(x, y, z, t) { | |
17072 | return this.point(x, y, z, t); | |
17073 | }; | |
17074 | ||
17075 | EdwardsCurve.prototype.pointFromX = function pointFromX(x, odd) { | |
17076 | x = new BN(x, 16); | |
17077 | if (!x.red) | |
17078 | x = x.toRed(this.red); | |
17079 | ||
17080 | var x2 = x.redSqr(); | |
17081 | var rhs = this.c2.redSub(this.a.redMul(x2)); | |
17082 | var lhs = this.one.redSub(this.c2.redMul(this.d).redMul(x2)); | |
17083 | ||
17084 | var y2 = rhs.redMul(lhs.redInvm()); | |
17085 | var y = y2.redSqrt(); | |
17086 | if (y.redSqr().redSub(y2).cmp(this.zero) !== 0) | |
17087 | throw new Error('invalid point'); | |
17088 | ||
17089 | var isOdd = y.fromRed().isOdd(); | |
17090 | if (odd && !isOdd || !odd && isOdd) | |
17091 | y = y.redNeg(); | |
17092 | ||
17093 | return this.point(x, y); | |
17094 | }; | |
17095 | ||
17096 | EdwardsCurve.prototype.pointFromY = function pointFromY(y, odd) { | |
17097 | y = new BN(y, 16); | |
17098 | if (!y.red) | |
17099 | y = y.toRed(this.red); | |
17100 | ||
17101 | // x^2 = (y^2 - 1) / (d y^2 + 1) | |
17102 | var y2 = y.redSqr(); | |
17103 | var lhs = y2.redSub(this.one); | |
17104 | var rhs = y2.redMul(this.d).redAdd(this.one); | |
17105 | var x2 = lhs.redMul(rhs.redInvm()); | |
17106 | ||
17107 | if (x2.cmp(this.zero) === 0) { | |
17108 | if (odd) | |
17109 | throw new Error('invalid point'); | |
17110 | else | |
17111 | return this.point(this.zero, y); | |
17112 | } | |
17113 | ||
17114 | var x = x2.redSqrt(); | |
17115 | if (x.redSqr().redSub(x2).cmp(this.zero) !== 0) | |
17116 | throw new Error('invalid point'); | |
17117 | ||
17118 | if (x.isOdd() !== odd) | |
17119 | x = x.redNeg(); | |
17120 | ||
17121 | return this.point(x, y); | |
17122 | }; | |
17123 | ||
17124 | EdwardsCurve.prototype.validate = function validate(point) { | |
17125 | if (point.isInfinity()) | |
17126 | return true; | |
17127 | ||
17128 | // Curve: A * X^2 + Y^2 = C^2 * (1 + D * X^2 * Y^2) | |
17129 | point.normalize(); | |
17130 | ||
17131 | var x2 = point.x.redSqr(); | |
17132 | var y2 = point.y.redSqr(); | |
17133 | var lhs = x2.redMul(this.a).redAdd(y2); | |
17134 | var rhs = this.c2.redMul(this.one.redAdd(this.d.redMul(x2).redMul(y2))); | |
17135 | ||
17136 | return lhs.cmp(rhs) === 0; | |
17137 | }; | |
17138 | ||
17139 | function Point(curve, x, y, z, t) { | |
17140 | Base.BasePoint.call(this, curve, 'projective'); | |
17141 | if (x === null && y === null && z === null) { | |
17142 | this.x = this.curve.zero; | |
17143 | this.y = this.curve.one; | |
17144 | this.z = this.curve.one; | |
17145 | this.t = this.curve.zero; | |
17146 | this.zOne = true; | |
17147 | } else { | |
17148 | this.x = new BN(x, 16); | |
17149 | this.y = new BN(y, 16); | |
17150 | this.z = z ? new BN(z, 16) : this.curve.one; | |
17151 | this.t = t && new BN(t, 16); | |
17152 | if (!this.x.red) | |
17153 | this.x = this.x.toRed(this.curve.red); | |
17154 | if (!this.y.red) | |
17155 | this.y = this.y.toRed(this.curve.red); | |
17156 | if (!this.z.red) | |
17157 | this.z = this.z.toRed(this.curve.red); | |
17158 | if (this.t && !this.t.red) | |
17159 | this.t = this.t.toRed(this.curve.red); | |
17160 | this.zOne = this.z === this.curve.one; | |
17161 | ||
17162 | // Use extended coordinates | |
17163 | if (this.curve.extended && !this.t) { | |
17164 | this.t = this.x.redMul(this.y); | |
17165 | if (!this.zOne) | |
17166 | this.t = this.t.redMul(this.z.redInvm()); | |
17167 | } | |
17168 | } | |
17169 | } | |
17170 | inherits(Point, Base.BasePoint); | |
17171 | ||
17172 | EdwardsCurve.prototype.pointFromJSON = function pointFromJSON(obj) { | |
17173 | return Point.fromJSON(this, obj); | |
17174 | }; | |
17175 | ||
17176 | EdwardsCurve.prototype.point = function point(x, y, z, t) { | |
17177 | return new Point(this, x, y, z, t); | |
17178 | }; | |
17179 | ||
17180 | Point.fromJSON = function fromJSON(curve, obj) { | |
17181 | return new Point(curve, obj[0], obj[1], obj[2]); | |
17182 | }; | |
17183 | ||
17184 | Point.prototype.inspect = function inspect() { | |
17185 | if (this.isInfinity()) | |
17186 | return '<EC Point Infinity>'; | |
17187 | return '<EC Point x: ' + this.x.fromRed().toString(16, 2) + | |
17188 | ' y: ' + this.y.fromRed().toString(16, 2) + | |
17189 | ' z: ' + this.z.fromRed().toString(16, 2) + '>'; | |
17190 | }; | |
17191 | ||
17192 | Point.prototype.isInfinity = function isInfinity() { | |
17193 | // XXX This code assumes that zero is always zero in red | |
17194 | return this.x.cmpn(0) === 0 && | |
17195 | this.y.cmp(this.z) === 0; | |
17196 | }; | |
17197 | ||
17198 | Point.prototype._extDbl = function _extDbl() { | |
17199 | // hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html | |
17200 | // #doubling-dbl-2008-hwcd | |
17201 | // 4M + 4S | |
17202 | ||
17203 | // A = X1^2 | |
17204 | var a = this.x.redSqr(); | |
17205 | // B = Y1^2 | |
17206 | var b = this.y.redSqr(); | |
17207 | // C = 2 * Z1^2 | |
17208 | var c = this.z.redSqr(); | |
17209 | c = c.redIAdd(c); | |
17210 | // D = a * A | |
17211 | var d = this.curve._mulA(a); | |
17212 | // E = (X1 + Y1)^2 - A - B | |
17213 | var e = this.x.redAdd(this.y).redSqr().redISub(a).redISub(b); | |
17214 | // G = D + B | |
17215 | var g = d.redAdd(b); | |
17216 | // F = G - C | |
17217 | var f = g.redSub(c); | |
17218 | // H = D - B | |
17219 | var h = d.redSub(b); | |
17220 | // X3 = E * F | |
17221 | var nx = e.redMul(f); | |
17222 | // Y3 = G * H | |
17223 | var ny = g.redMul(h); | |
17224 | // T3 = E * H | |
17225 | var nt = e.redMul(h); | |
17226 | // Z3 = F * G | |
17227 | var nz = f.redMul(g); | |
17228 | return this.curve.point(nx, ny, nz, nt); | |
17229 | }; | |
17230 | ||
17231 | Point.prototype._projDbl = function _projDbl() { | |
17232 | // hyperelliptic.org/EFD/g1p/auto-twisted-projective.html | |
17233 | // #doubling-dbl-2008-bbjlp | |
17234 | // #doubling-dbl-2007-bl | |
17235 | // and others | |
17236 | // Generally 3M + 4S or 2M + 4S | |
17237 | ||
17238 | // B = (X1 + Y1)^2 | |
17239 | var b = this.x.redAdd(this.y).redSqr(); | |
17240 | // C = X1^2 | |
17241 | var c = this.x.redSqr(); | |
17242 | // D = Y1^2 | |
17243 | var d = this.y.redSqr(); | |
17244 | ||
17245 | var nx; | |
17246 | var ny; | |
17247 | var nz; | |
17248 | if (this.curve.twisted) { | |
17249 | // E = a * C | |
17250 | var e = this.curve._mulA(c); | |
17251 | // F = E + D | |
17252 | var f = e.redAdd(d); | |
17253 | if (this.zOne) { | |
17254 | // X3 = (B - C - D) * (F - 2) | |
17255 | nx = b.redSub(c).redSub(d).redMul(f.redSub(this.curve.two)); | |
17256 | // Y3 = F * (E - D) | |
17257 | ny = f.redMul(e.redSub(d)); | |
17258 | // Z3 = F^2 - 2 * F | |
17259 | nz = f.redSqr().redSub(f).redSub(f); | |
17260 | } else { | |
17261 | // H = Z1^2 | |
17262 | var h = this.z.redSqr(); | |
17263 | // J = F - 2 * H | |
17264 | var j = f.redSub(h).redISub(h); | |
17265 | // X3 = (B-C-D)*J | |
17266 | nx = b.redSub(c).redISub(d).redMul(j); | |
17267 | // Y3 = F * (E - D) | |
17268 | ny = f.redMul(e.redSub(d)); | |
17269 | // Z3 = F * J | |
17270 | nz = f.redMul(j); | |
17271 | } | |
17272 | } else { | |
17273 | // E = C + D | |
17274 | var e = c.redAdd(d); | |
17275 | // H = (c * Z1)^2 | |
17276 | var h = this.curve._mulC(this.c.redMul(this.z)).redSqr(); | |
17277 | // J = E - 2 * H | |
17278 | var j = e.redSub(h).redSub(h); | |
17279 | // X3 = c * (B - E) * J | |
17280 | nx = this.curve._mulC(b.redISub(e)).redMul(j); | |
17281 | // Y3 = c * E * (C - D) | |
17282 | ny = this.curve._mulC(e).redMul(c.redISub(d)); | |
17283 | // Z3 = E * J | |
17284 | nz = e.redMul(j); | |
17285 | } | |
17286 | return this.curve.point(nx, ny, nz); | |
17287 | }; | |
17288 | ||
17289 | Point.prototype.dbl = function dbl() { | |
17290 | if (this.isInfinity()) | |
17291 | return this; | |
17292 | ||
17293 | // Double in extended coordinates | |
17294 | if (this.curve.extended) | |
17295 | return this._extDbl(); | |
17296 | else | |
17297 | return this._projDbl(); | |
17298 | }; | |
17299 | ||
17300 | Point.prototype._extAdd = function _extAdd(p) { | |
17301 | // hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html | |
17302 | // #addition-add-2008-hwcd-3 | |
17303 | // 8M | |
17304 | ||
17305 | // A = (Y1 - X1) * (Y2 - X2) | |
17306 | var a = this.y.redSub(this.x).redMul(p.y.redSub(p.x)); | |
17307 | // B = (Y1 + X1) * (Y2 + X2) | |
17308 | var b = this.y.redAdd(this.x).redMul(p.y.redAdd(p.x)); | |
17309 | // C = T1 * k * T2 | |
17310 | var c = this.t.redMul(this.curve.dd).redMul(p.t); | |
17311 | // D = Z1 * 2 * Z2 | |
17312 | var d = this.z.redMul(p.z.redAdd(p.z)); | |
17313 | // E = B - A | |
17314 | var e = b.redSub(a); | |
17315 | // F = D - C | |
17316 | var f = d.redSub(c); | |
17317 | // G = D + C | |
17318 | var g = d.redAdd(c); | |
17319 | // H = B + A | |
17320 | var h = b.redAdd(a); | |
17321 | // X3 = E * F | |
17322 | var nx = e.redMul(f); | |
17323 | // Y3 = G * H | |
17324 | var ny = g.redMul(h); | |
17325 | // T3 = E * H | |
17326 | var nt = e.redMul(h); | |
17327 | // Z3 = F * G | |
17328 | var nz = f.redMul(g); | |
17329 | return this.curve.point(nx, ny, nz, nt); | |
17330 | }; | |
17331 | ||
17332 | Point.prototype._projAdd = function _projAdd(p) { | |
17333 | // hyperelliptic.org/EFD/g1p/auto-twisted-projective.html | |
17334 | // #addition-add-2008-bbjlp | |
17335 | // #addition-add-2007-bl | |
17336 | // 10M + 1S | |
17337 | ||
17338 | // A = Z1 * Z2 | |
17339 | var a = this.z.redMul(p.z); | |
17340 | // B = A^2 | |
17341 | var b = a.redSqr(); | |
17342 | // C = X1 * X2 | |
17343 | var c = this.x.redMul(p.x); | |
17344 | // D = Y1 * Y2 | |
17345 | var d = this.y.redMul(p.y); | |
17346 | // E = d * C * D | |
17347 | var e = this.curve.d.redMul(c).redMul(d); | |
17348 | // F = B - E | |
17349 | var f = b.redSub(e); | |
17350 | // G = B + E | |
17351 | var g = b.redAdd(e); | |
17352 | // X3 = A * F * ((X1 + Y1) * (X2 + Y2) - C - D) | |
17353 | var tmp = this.x.redAdd(this.y).redMul(p.x.redAdd(p.y)).redISub(c).redISub(d); | |
17354 | var nx = a.redMul(f).redMul(tmp); | |
17355 | var ny; | |
17356 | var nz; | |
17357 | if (this.curve.twisted) { | |
17358 | // Y3 = A * G * (D - a * C) | |
17359 | ny = a.redMul(g).redMul(d.redSub(this.curve._mulA(c))); | |
17360 | // Z3 = F * G | |
17361 | nz = f.redMul(g); | |
17362 | } else { | |
17363 | // Y3 = A * G * (D - C) | |
17364 | ny = a.redMul(g).redMul(d.redSub(c)); | |
17365 | // Z3 = c * F * G | |
17366 | nz = this.curve._mulC(f).redMul(g); | |
17367 | } | |
17368 | return this.curve.point(nx, ny, nz); | |
17369 | }; | |
17370 | ||
17371 | Point.prototype.add = function add(p) { | |
17372 | if (this.isInfinity()) | |
17373 | return p; | |
17374 | if (p.isInfinity()) | |
17375 | return this; | |
17376 | ||
17377 | if (this.curve.extended) | |
17378 | return this._extAdd(p); | |
17379 | else | |
17380 | return this._projAdd(p); | |
17381 | }; | |
17382 | ||
17383 | Point.prototype.mul = function mul(k) { | |
17384 | if (this._hasDoubles(k)) | |
17385 | return this.curve._fixedNafMul(this, k); | |
17386 | else | |
17387 | return this.curve._wnafMul(this, k); | |
17388 | }; | |
17389 | ||
17390 | Point.prototype.mulAdd = function mulAdd(k1, p, k2) { | |
17391 | return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, false); | |
17392 | }; | |
17393 | ||
17394 | Point.prototype.jmulAdd = function jmulAdd(k1, p, k2) { | |
17395 | return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, true); | |
17396 | }; | |
17397 | ||
17398 | Point.prototype.normalize = function normalize() { | |
17399 | if (this.zOne) | |
17400 | return this; | |
17401 | ||
17402 | // Normalize coordinates | |
17403 | var zi = this.z.redInvm(); | |
17404 | this.x = this.x.redMul(zi); | |
17405 | this.y = this.y.redMul(zi); | |
17406 | if (this.t) | |
17407 | this.t = this.t.redMul(zi); | |
17408 | this.z = this.curve.one; | |
17409 | this.zOne = true; | |
17410 | return this; | |
17411 | }; | |
17412 | ||
17413 | Point.prototype.neg = function neg() { | |
17414 | return this.curve.point(this.x.redNeg(), | |
17415 | this.y, | |
17416 | this.z, | |
17417 | this.t && this.t.redNeg()); | |
17418 | }; | |
17419 | ||
17420 | Point.prototype.getX = function getX() { | |
17421 | this.normalize(); | |
17422 | return this.x.fromRed(); | |
17423 | }; | |
17424 | ||
17425 | Point.prototype.getY = function getY() { | |
17426 | this.normalize(); | |
17427 | return this.y.fromRed(); | |
17428 | }; | |
17429 | ||
17430 | Point.prototype.eq = function eq(other) { | |
17431 | return this === other || | |
17432 | this.getX().cmp(other.getX()) === 0 && | |
17433 | this.getY().cmp(other.getY()) === 0; | |
17434 | }; | |
17435 | ||
17436 | Point.prototype.eqXToP = function eqXToP(x) { | |
17437 | var rx = x.toRed(this.curve.red).redMul(this.z); | |
17438 | if (this.x.cmp(rx) === 0) | |
17439 | return true; | |
17440 | ||
17441 | var xc = x.clone(); | |
17442 | var t = this.curve.redN.redMul(this.z); | |
17443 | for (;;) { | |
17444 | xc.iadd(this.curve.n); | |
17445 | if (xc.cmp(this.curve.p) >= 0) | |
17446 | return false; | |
17447 | ||
17448 | rx.redIAdd(t); | |
17449 | if (this.x.cmp(rx) === 0) | |
17450 | return true; | |
17451 | } | |
17452 | return false; | |
17453 | }; | |
17454 | ||
17455 | // Compatibility with BaseCurve | |
17456 | Point.prototype.toP = Point.prototype.normalize; | |
17457 | Point.prototype.mixedAdd = Point.prototype.add; | |
17458 | ||
17459 | },{"../../elliptic":97,"../curve":100,"bn.js":81,"inherits":121}],100:[function(require,module,exports){ | |
17460 | 'use strict'; | |
17461 | ||
17462 | var curve = exports; | |
17463 | ||
17464 | curve.base = require('./base'); | |
17465 | curve.short = require('./short'); | |
17466 | curve.mont = require('./mont'); | |
17467 | curve.edwards = require('./edwards'); | |
17468 | ||
17469 | },{"./base":98,"./edwards":99,"./mont":101,"./short":102}],101:[function(require,module,exports){ | |
17470 | 'use strict'; | |
17471 | ||
17472 | var curve = require('../curve'); | |
17473 | var BN = require('bn.js'); | |
17474 | var inherits = require('inherits'); | |
17475 | var Base = curve.base; | |
17476 | ||
17477 | var elliptic = require('../../elliptic'); | |
17478 | var utils = elliptic.utils; | |
17479 | ||
17480 | function MontCurve(conf) { | |
17481 | Base.call(this, 'mont', conf); | |
17482 | ||
17483 | this.a = new BN(conf.a, 16).toRed(this.red); | |
17484 | this.b = new BN(conf.b, 16).toRed(this.red); | |
17485 | this.i4 = new BN(4).toRed(this.red).redInvm(); | |
17486 | this.two = new BN(2).toRed(this.red); | |
17487 | this.a24 = this.i4.redMul(this.a.redAdd(this.two)); | |
17488 | } | |
17489 | inherits(MontCurve, Base); | |
17490 | module.exports = MontCurve; | |
17491 | ||
17492 | MontCurve.prototype.validate = function validate(point) { | |
17493 | var x = point.normalize().x; | |
17494 | var x2 = x.redSqr(); | |
17495 | var rhs = x2.redMul(x).redAdd(x2.redMul(this.a)).redAdd(x); | |
17496 | var y = rhs.redSqrt(); | |
17497 | ||
17498 | return y.redSqr().cmp(rhs) === 0; | |
17499 | }; | |
17500 | ||
17501 | function Point(curve, x, z) { | |
17502 | Base.BasePoint.call(this, curve, 'projective'); | |
17503 | if (x === null && z === null) { | |
17504 | this.x = this.curve.one; | |
17505 | this.z = this.curve.zero; | |
17506 | } else { | |
17507 | this.x = new BN(x, 16); | |
17508 | this.z = new BN(z, 16); | |
17509 | if (!this.x.red) | |
17510 | this.x = this.x.toRed(this.curve.red); | |
17511 | if (!this.z.red) | |
17512 | this.z = this.z.toRed(this.curve.red); | |
17513 | } | |
17514 | } | |
17515 | inherits(Point, Base.BasePoint); | |
17516 | ||
17517 | MontCurve.prototype.decodePoint = function decodePoint(bytes, enc) { | |
17518 | return this.point(utils.toArray(bytes, enc), 1); | |
17519 | }; | |
17520 | ||
17521 | MontCurve.prototype.point = function point(x, z) { | |
17522 | return new Point(this, x, z); | |
17523 | }; | |
17524 | ||
17525 | MontCurve.prototype.pointFromJSON = function pointFromJSON(obj) { | |
17526 | return Point.fromJSON(this, obj); | |
17527 | }; | |
17528 | ||
17529 | Point.prototype.precompute = function precompute() { | |
17530 | // No-op | |
17531 | }; | |
17532 | ||
17533 | Point.prototype._encode = function _encode() { | |
17534 | return this.getX().toArray('be', this.curve.p.byteLength()); | |
17535 | }; | |
17536 | ||
17537 | Point.fromJSON = function fromJSON(curve, obj) { | |
17538 | return new Point(curve, obj[0], obj[1] || curve.one); | |
17539 | }; | |
17540 | ||
17541 | Point.prototype.inspect = function inspect() { | |
17542 | if (this.isInfinity()) | |
17543 | return '<EC Point Infinity>'; | |
17544 | return '<EC Point x: ' + this.x.fromRed().toString(16, 2) + | |
17545 | ' z: ' + this.z.fromRed().toString(16, 2) + '>'; | |
17546 | }; | |
17547 | ||
17548 | Point.prototype.isInfinity = function isInfinity() { | |
17549 | // XXX This code assumes that zero is always zero in red | |
17550 | return this.z.cmpn(0) === 0; | |
17551 | }; | |
17552 | ||
17553 | Point.prototype.dbl = function dbl() { | |
17554 | // http://hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#doubling-dbl-1987-m-3 | |
17555 | // 2M + 2S + 4A | |
17556 | ||
17557 | // A = X1 + Z1 | |
17558 | var a = this.x.redAdd(this.z); | |
17559 | // AA = A^2 | |
17560 | var aa = a.redSqr(); | |
17561 | // B = X1 - Z1 | |
17562 | var b = this.x.redSub(this.z); | |
17563 | // BB = B^2 | |
17564 | var bb = b.redSqr(); | |
17565 | // C = AA - BB | |
17566 | var c = aa.redSub(bb); | |
17567 | // X3 = AA * BB | |
17568 | var nx = aa.redMul(bb); | |
17569 | // Z3 = C * (BB + A24 * C) | |
17570 | var nz = c.redMul(bb.redAdd(this.curve.a24.redMul(c))); | |
17571 | return this.curve.point(nx, nz); | |
17572 | }; | |
17573 | ||
17574 | Point.prototype.add = function add() { | |
17575 | throw new Error('Not supported on Montgomery curve'); | |
17576 | }; | |
17577 | ||
17578 | Point.prototype.diffAdd = function diffAdd(p, diff) { | |
17579 | // http://hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#diffadd-dadd-1987-m-3 | |
17580 | // 4M + 2S + 6A | |
17581 | ||
17582 | // A = X2 + Z2 | |
17583 | var a = this.x.redAdd(this.z); | |
17584 | // B = X2 - Z2 | |
17585 | var b = this.x.redSub(this.z); | |
17586 | // C = X3 + Z3 | |
17587 | var c = p.x.redAdd(p.z); | |
17588 | // D = X3 - Z3 | |
17589 | var d = p.x.redSub(p.z); | |
17590 | // DA = D * A | |
17591 | var da = d.redMul(a); | |
17592 | // CB = C * B | |
17593 | var cb = c.redMul(b); | |
17594 | // X5 = Z1 * (DA + CB)^2 | |
17595 | var nx = diff.z.redMul(da.redAdd(cb).redSqr()); | |
17596 | // Z5 = X1 * (DA - CB)^2 | |
17597 | var nz = diff.x.redMul(da.redISub(cb).redSqr()); | |
17598 | return this.curve.point(nx, nz); | |
17599 | }; | |
17600 | ||
17601 | Point.prototype.mul = function mul(k) { | |
17602 | var t = k.clone(); | |
17603 | var a = this; // (N / 2) * Q + Q | |
17604 | var b = this.curve.point(null, null); // (N / 2) * Q | |
17605 | var c = this; // Q | |
17606 | ||
17607 | for (var bits = []; t.cmpn(0) !== 0; t.iushrn(1)) | |
17608 | bits.push(t.andln(1)); | |
17609 | ||
17610 | for (var i = bits.length - 1; i >= 0; i--) { | |
17611 | if (bits[i] === 0) { | |
17612 | // N * Q + Q = ((N / 2) * Q + Q)) + (N / 2) * Q | |
17613 | a = a.diffAdd(b, c); | |
17614 | // N * Q = 2 * ((N / 2) * Q + Q)) | |
17615 | b = b.dbl(); | |
17616 | } else { | |
17617 | // N * Q = ((N / 2) * Q + Q) + ((N / 2) * Q) | |
17618 | b = a.diffAdd(b, c); | |
17619 | // N * Q + Q = 2 * ((N / 2) * Q + Q) | |
17620 | a = a.dbl(); | |
17621 | } | |
17622 | } | |
17623 | return b; | |
17624 | }; | |
17625 | ||
17626 | Point.prototype.mulAdd = function mulAdd() { | |
17627 | throw new Error('Not supported on Montgomery curve'); | |
17628 | }; | |
17629 | ||
17630 | Point.prototype.jumlAdd = function jumlAdd() { | |
17631 | throw new Error('Not supported on Montgomery curve'); | |
17632 | }; | |
17633 | ||
17634 | Point.prototype.eq = function eq(other) { | |
17635 | return this.getX().cmp(other.getX()) === 0; | |
17636 | }; | |
17637 | ||
17638 | Point.prototype.normalize = function normalize() { | |
17639 | this.x = this.x.redMul(this.z.redInvm()); | |
17640 | this.z = this.curve.one; | |
17641 | return this; | |
17642 | }; | |
17643 | ||
17644 | Point.prototype.getX = function getX() { | |
17645 | // Normalize coordinates | |
17646 | this.normalize(); | |
17647 | ||
17648 | return this.x.fromRed(); | |
17649 | }; | |
17650 | ||
17651 | },{"../../elliptic":97,"../curve":100,"bn.js":81,"inherits":121}],102:[function(require,module,exports){ | |
17652 | 'use strict'; | |
17653 | ||
17654 | var curve = require('../curve'); | |
17655 | var elliptic = require('../../elliptic'); | |
17656 | var BN = require('bn.js'); | |
17657 | var inherits = require('inherits'); | |
17658 | var Base = curve.base; | |
17659 | ||
17660 | var assert = elliptic.utils.assert; | |
17661 | ||
17662 | function ShortCurve(conf) { | |
17663 | Base.call(this, 'short', conf); | |
17664 | ||
17665 | this.a = new BN(conf.a, 16).toRed(this.red); | |
17666 | this.b = new BN(conf.b, 16).toRed(this.red); | |
17667 | this.tinv = this.two.redInvm(); | |
17668 | ||
17669 | this.zeroA = this.a.fromRed().cmpn(0) === 0; | |
17670 | this.threeA = this.a.fromRed().sub(this.p).cmpn(-3) === 0; | |
17671 | ||
17672 | // If the curve is endomorphic, precalculate beta and lambda | |
17673 | this.endo = this._getEndomorphism(conf); | |
17674 | this._endoWnafT1 = new Array(4); | |
17675 | this._endoWnafT2 = new Array(4); | |
17676 | } | |
17677 | inherits(ShortCurve, Base); | |
17678 | module.exports = ShortCurve; | |
17679 | ||
17680 | ShortCurve.prototype._getEndomorphism = function _getEndomorphism(conf) { | |
17681 | // No efficient endomorphism | |
17682 | if (!this.zeroA || !this.g || !this.n || this.p.modn(3) !== 1) | |
17683 | return; | |
17684 | ||
17685 | // Compute beta and lambda, that lambda * P = (beta * Px; Py) | |
17686 | var beta; | |
17687 | var lambda; | |
17688 | if (conf.beta) { | |
17689 | beta = new BN(conf.beta, 16).toRed(this.red); | |
17690 | } else { | |
17691 | var betas = this._getEndoRoots(this.p); | |
17692 | // Choose the smallest beta | |
17693 | beta = betas[0].cmp(betas[1]) < 0 ? betas[0] : betas[1]; | |
17694 | beta = beta.toRed(this.red); | |
17695 | } | |
17696 | if (conf.lambda) { | |
17697 | lambda = new BN(conf.lambda, 16); | |
17698 | } else { | |
17699 | // Choose the lambda that is matching selected beta | |
17700 | var lambdas = this._getEndoRoots(this.n); | |
17701 | if (this.g.mul(lambdas[0]).x.cmp(this.g.x.redMul(beta)) === 0) { | |
17702 | lambda = lambdas[0]; | |
17703 | } else { | |
17704 | lambda = lambdas[1]; | |
17705 | assert(this.g.mul(lambda).x.cmp(this.g.x.redMul(beta)) === 0); | |
17706 | } | |
17707 | } | |
17708 | ||
17709 | // Get basis vectors, used for balanced length-two representation | |
17710 | var basis; | |
17711 | if (conf.basis) { | |
17712 | basis = conf.basis.map(function(vec) { | |
17713 | return { | |
17714 | a: new BN(vec.a, 16), | |
17715 | b: new BN(vec.b, 16) | |
17716 | }; | |
17717 | }); | |
17718 | } else { | |
17719 | basis = this._getEndoBasis(lambda); | |
17720 | } | |
17721 | ||
17722 | return { | |
17723 | beta: beta, | |
17724 | lambda: lambda, | |
17725 | basis: basis | |
17726 | }; | |
17727 | }; | |
17728 | ||
17729 | ShortCurve.prototype._getEndoRoots = function _getEndoRoots(num) { | |
17730 | // Find roots of for x^2 + x + 1 in F | |
17731 | // Root = (-1 +- Sqrt(-3)) / 2 | |
17732 | // | |
17733 | var red = num === this.p ? this.red : BN.mont(num); | |
17734 | var tinv = new BN(2).toRed(red).redInvm(); | |
17735 | var ntinv = tinv.redNeg(); | |
17736 | ||
17737 | var s = new BN(3).toRed(red).redNeg().redSqrt().redMul(tinv); | |
17738 | ||
17739 | var l1 = ntinv.redAdd(s).fromRed(); | |
17740 | var l2 = ntinv.redSub(s).fromRed(); | |
17741 | return [ l1, l2 ]; | |
17742 | }; | |
17743 | ||
17744 | ShortCurve.prototype._getEndoBasis = function _getEndoBasis(lambda) { | |
17745 | // aprxSqrt >= sqrt(this.n) | |
17746 | var aprxSqrt = this.n.ushrn(Math.floor(this.n.bitLength() / 2)); | |
17747 | ||
17748 | // 3.74 | |
17749 | // Run EGCD, until r(L + 1) < aprxSqrt | |
17750 | var u = lambda; | |
17751 | var v = this.n.clone(); | |
17752 | var x1 = new BN(1); | |
17753 | var y1 = new BN(0); | |
17754 | var x2 = new BN(0); | |
17755 | var y2 = new BN(1); | |
17756 | ||
17757 | // NOTE: all vectors are roots of: a + b * lambda = 0 (mod n) | |
17758 | var a0; | |
17759 | var b0; | |
17760 | // First vector | |
17761 | var a1; | |
17762 | var b1; | |
17763 | // Second vector | |
17764 | var a2; | |
17765 | var b2; | |
17766 | ||
17767 | var prevR; | |
17768 | var i = 0; | |
17769 | var r; | |
17770 | var x; | |
17771 | while (u.cmpn(0) !== 0) { | |
17772 | var q = v.div(u); | |
17773 | r = v.sub(q.mul(u)); | |
17774 | x = x2.sub(q.mul(x1)); | |
17775 | var y = y2.sub(q.mul(y1)); | |
17776 | ||
17777 | if (!a1 && r.cmp(aprxSqrt) < 0) { | |
17778 | a0 = prevR.neg(); | |
17779 | b0 = x1; | |
17780 | a1 = r.neg(); | |
17781 | b1 = x; | |
17782 | } else if (a1 && ++i === 2) { | |
17783 | break; | |
17784 | } | |
17785 | prevR = r; | |
17786 | ||
17787 | v = u; | |
17788 | u = r; | |
17789 | x2 = x1; | |
17790 | x1 = x; | |
17791 | y2 = y1; | |
17792 | y1 = y; | |
17793 | } | |
17794 | a2 = r.neg(); | |
17795 | b2 = x; | |
17796 | ||
17797 | var len1 = a1.sqr().add(b1.sqr()); | |
17798 | var len2 = a2.sqr().add(b2.sqr()); | |
17799 | if (len2.cmp(len1) >= 0) { | |
17800 | a2 = a0; | |
17801 | b2 = b0; | |
17802 | } | |
17803 | ||
17804 | // Normalize signs | |
17805 | if (a1.negative) { | |
17806 | a1 = a1.neg(); | |
17807 | b1 = b1.neg(); | |
17808 | } | |
17809 | if (a2.negative) { | |
17810 | a2 = a2.neg(); | |
17811 | b2 = b2.neg(); | |
17812 | } | |
17813 | ||
17814 | return [ | |
17815 | { a: a1, b: b1 }, | |
17816 | { a: a2, b: b2 } | |
17817 | ]; | |
17818 | }; | |
17819 | ||
17820 | ShortCurve.prototype._endoSplit = function _endoSplit(k) { | |
17821 | var basis = this.endo.basis; | |
17822 | var v1 = basis[0]; | |
17823 | var v2 = basis[1]; | |
17824 | ||
17825 | var c1 = v2.b.mul(k).divRound(this.n); | |
17826 | var c2 = v1.b.neg().mul(k).divRound(this.n); | |
17827 | ||
17828 | var p1 = c1.mul(v1.a); | |
17829 | var p2 = c2.mul(v2.a); | |
17830 | var q1 = c1.mul(v1.b); | |
17831 | var q2 = c2.mul(v2.b); | |
17832 | ||
17833 | // Calculate answer | |
17834 | var k1 = k.sub(p1).sub(p2); | |
17835 | var k2 = q1.add(q2).neg(); | |
17836 | return { k1: k1, k2: k2 }; | |
17837 | }; | |
17838 | ||
17839 | ShortCurve.prototype.pointFromX = function pointFromX(x, odd) { | |
17840 | x = new BN(x, 16); | |
17841 | if (!x.red) | |
17842 | x = x.toRed(this.red); | |
17843 | ||
17844 | var y2 = x.redSqr().redMul(x).redIAdd(x.redMul(this.a)).redIAdd(this.b); | |
17845 | var y = y2.redSqrt(); | |
17846 | if (y.redSqr().redSub(y2).cmp(this.zero) !== 0) | |
17847 | throw new Error('invalid point'); | |
17848 | ||
17849 | // XXX Is there any way to tell if the number is odd without converting it | |
17850 | // to non-red form? | |
17851 | var isOdd = y.fromRed().isOdd(); | |
17852 | if (odd && !isOdd || !odd && isOdd) | |
17853 | y = y.redNeg(); | |
17854 | ||
17855 | return this.point(x, y); | |
17856 | }; | |
17857 | ||
17858 | ShortCurve.prototype.validate = function validate(point) { | |
17859 | if (point.inf) | |
17860 | return true; | |
17861 | ||
17862 | var x = point.x; | |
17863 | var y = point.y; | |
17864 | ||
17865 | var ax = this.a.redMul(x); | |
17866 | var rhs = x.redSqr().redMul(x).redIAdd(ax).redIAdd(this.b); | |
17867 | return y.redSqr().redISub(rhs).cmpn(0) === 0; | |
17868 | }; | |
17869 | ||
17870 | ShortCurve.prototype._endoWnafMulAdd = | |
17871 | function _endoWnafMulAdd(points, coeffs, jacobianResult) { | |
17872 | var npoints = this._endoWnafT1; | |
17873 | var ncoeffs = this._endoWnafT2; | |
17874 | for (var i = 0; i < points.length; i++) { | |
17875 | var split = this._endoSplit(coeffs[i]); | |
17876 | var p = points[i]; | |
17877 | var beta = p._getBeta(); | |
17878 | ||
17879 | if (split.k1.negative) { | |
17880 | split.k1.ineg(); | |
17881 | p = p.neg(true); | |
17882 | } | |
17883 | if (split.k2.negative) { | |
17884 | split.k2.ineg(); | |
17885 | beta = beta.neg(true); | |
17886 | } | |
17887 | ||
17888 | npoints[i * 2] = p; | |
17889 | npoints[i * 2 + 1] = beta; | |
17890 | ncoeffs[i * 2] = split.k1; | |
17891 | ncoeffs[i * 2 + 1] = split.k2; | |
17892 | } | |
17893 | var res = this._wnafMulAdd(1, npoints, ncoeffs, i * 2, jacobianResult); | |
17894 | ||
17895 | // Clean-up references to points and coefficients | |
17896 | for (var j = 0; j < i * 2; j++) { | |
17897 | npoints[j] = null; | |
17898 | ncoeffs[j] = null; | |
17899 | } | |
17900 | return res; | |
17901 | }; | |
17902 | ||
17903 | function Point(curve, x, y, isRed) { | |
17904 | Base.BasePoint.call(this, curve, 'affine'); | |
17905 | if (x === null && y === null) { | |
17906 | this.x = null; | |
17907 | this.y = null; | |
17908 | this.inf = true; | |
17909 | } else { | |
17910 | this.x = new BN(x, 16); | |
17911 | this.y = new BN(y, 16); | |
17912 | // Force redgomery representation when loading from JSON | |
17913 | if (isRed) { | |
17914 | this.x.forceRed(this.curve.red); | |
17915 | this.y.forceRed(this.curve.red); | |
17916 | } | |
17917 | if (!this.x.red) | |
17918 | this.x = this.x.toRed(this.curve.red); | |
17919 | if (!this.y.red) | |
17920 | this.y = this.y.toRed(this.curve.red); | |
17921 | this.inf = false; | |
17922 | } | |
17923 | } | |
17924 | inherits(Point, Base.BasePoint); | |
17925 | ||
17926 | ShortCurve.prototype.point = function point(x, y, isRed) { | |
17927 | return new Point(this, x, y, isRed); | |
17928 | }; | |
17929 | ||
17930 | ShortCurve.prototype.pointFromJSON = function pointFromJSON(obj, red) { | |
17931 | return Point.fromJSON(this, obj, red); | |
17932 | }; | |
17933 | ||
17934 | Point.prototype._getBeta = function _getBeta() { | |
17935 | if (!this.curve.endo) | |
17936 | return; | |
17937 | ||
17938 | var pre = this.precomputed; | |
17939 | if (pre && pre.beta) | |
17940 | return pre.beta; | |
17941 | ||
17942 | var beta = this.curve.point(this.x.redMul(this.curve.endo.beta), this.y); | |
17943 | if (pre) { | |
17944 | var curve = this.curve; | |
17945 | var endoMul = function(p) { | |
17946 | return curve.point(p.x.redMul(curve.endo.beta), p.y); | |
17947 | }; | |
17948 | pre.beta = beta; | |
17949 | beta.precomputed = { | |
17950 | beta: null, | |
17951 | naf: pre.naf && { | |
17952 | wnd: pre.naf.wnd, | |
17953 | points: pre.naf.points.map(endoMul) | |
17954 | }, | |
17955 | doubles: pre.doubles && { | |
17956 | step: pre.doubles.step, | |
17957 | points: pre.doubles.points.map(endoMul) | |
17958 | } | |
17959 | }; | |
17960 | } | |
17961 | return beta; | |
17962 | }; | |
17963 | ||
17964 | Point.prototype.toJSON = function toJSON() { | |
17965 | if (!this.precomputed) | |
17966 | return [ this.x, this.y ]; | |
17967 | ||
17968 | return [ this.x, this.y, this.precomputed && { | |
17969 | doubles: this.precomputed.doubles && { | |
17970 | step: this.precomputed.doubles.step, | |
17971 | points: this.precomputed.doubles.points.slice(1) | |
17972 | }, | |
17973 | naf: this.precomputed.naf && { | |
17974 | wnd: this.precomputed.naf.wnd, | |
17975 | points: this.precomputed.naf.points.slice(1) | |
17976 | } | |
17977 | } ]; | |
17978 | }; | |
17979 | ||
17980 | Point.fromJSON = function fromJSON(curve, obj, red) { | |
17981 | if (typeof obj === 'string') | |
17982 | obj = JSON.parse(obj); | |
17983 | var res = curve.point(obj[0], obj[1], red); | |
17984 | if (!obj[2]) | |
17985 | return res; | |
17986 | ||
17987 | function obj2point(obj) { | |
17988 | return curve.point(obj[0], obj[1], red); | |
17989 | } | |
17990 | ||
17991 | var pre = obj[2]; | |
17992 | res.precomputed = { | |
17993 | beta: null, | |
17994 | doubles: pre.doubles && { | |
17995 | step: pre.doubles.step, | |
17996 | points: [ res ].concat(pre.doubles.points.map(obj2point)) | |
17997 | }, | |
17998 | naf: pre.naf && { | |
17999 | wnd: pre.naf.wnd, | |
18000 | points: [ res ].concat(pre.naf.points.map(obj2point)) | |
18001 | } | |
18002 | }; | |
18003 | return res; | |
18004 | }; | |
18005 | ||
18006 | Point.prototype.inspect = function inspect() { | |
18007 | if (this.isInfinity()) | |
18008 | return '<EC Point Infinity>'; | |
18009 | return '<EC Point x: ' + this.x.fromRed().toString(16, 2) + | |
18010 | ' y: ' + this.y.fromRed().toString(16, 2) + '>'; | |
18011 | }; | |
18012 | ||
18013 | Point.prototype.isInfinity = function isInfinity() { | |
18014 | return this.inf; | |
18015 | }; | |
18016 | ||
18017 | Point.prototype.add = function add(p) { | |
18018 | // O + P = P | |
18019 | if (this.inf) | |
18020 | return p; | |
18021 | ||
18022 | // P + O = P | |
18023 | if (p.inf) | |
18024 | return this; | |
18025 | ||
18026 | // P + P = 2P | |
18027 | if (this.eq(p)) | |
18028 | return this.dbl(); | |
18029 | ||
18030 | // P + (-P) = O | |
18031 | if (this.neg().eq(p)) | |
18032 | return this.curve.point(null, null); | |
18033 | ||
18034 | // P + Q = O | |
18035 | if (this.x.cmp(p.x) === 0) | |
18036 | return this.curve.point(null, null); | |
18037 | ||
18038 | var c = this.y.redSub(p.y); | |
18039 | if (c.cmpn(0) !== 0) | |
18040 | c = c.redMul(this.x.redSub(p.x).redInvm()); | |
18041 | var nx = c.redSqr().redISub(this.x).redISub(p.x); | |
18042 | var ny = c.redMul(this.x.redSub(nx)).redISub(this.y); | |
18043 | return this.curve.point(nx, ny); | |
18044 | }; | |
18045 | ||
18046 | Point.prototype.dbl = function dbl() { | |
18047 | if (this.inf) | |
18048 | return this; | |
18049 | ||
18050 | // 2P = O | |
18051 | var ys1 = this.y.redAdd(this.y); | |
18052 | if (ys1.cmpn(0) === 0) | |
18053 | return this.curve.point(null, null); | |
18054 | ||
18055 | var a = this.curve.a; | |
18056 | ||
18057 | var x2 = this.x.redSqr(); | |
18058 | var dyinv = ys1.redInvm(); | |
18059 | var c = x2.redAdd(x2).redIAdd(x2).redIAdd(a).redMul(dyinv); | |
18060 | ||
18061 | var nx = c.redSqr().redISub(this.x.redAdd(this.x)); | |
18062 | var ny = c.redMul(this.x.redSub(nx)).redISub(this.y); | |
18063 | return this.curve.point(nx, ny); | |
18064 | }; | |
18065 | ||
18066 | Point.prototype.getX = function getX() { | |
18067 | return this.x.fromRed(); | |
18068 | }; | |
18069 | ||
18070 | Point.prototype.getY = function getY() { | |
18071 | return this.y.fromRed(); | |
18072 | }; | |
18073 | ||
18074 | Point.prototype.mul = function mul(k) { | |
18075 | k = new BN(k, 16); | |
18076 | ||
18077 | if (this._hasDoubles(k)) | |
18078 | return this.curve._fixedNafMul(this, k); | |
18079 | else if (this.curve.endo) | |
18080 | return this.curve._endoWnafMulAdd([ this ], [ k ]); | |
18081 | else | |
18082 | return this.curve._wnafMul(this, k); | |
18083 | }; | |
18084 | ||
18085 | Point.prototype.mulAdd = function mulAdd(k1, p2, k2) { | |
18086 | var points = [ this, p2 ]; | |
18087 | var coeffs = [ k1, k2 ]; | |
18088 | if (this.curve.endo) | |
18089 | return this.curve._endoWnafMulAdd(points, coeffs); | |
18090 | else | |
18091 | return this.curve._wnafMulAdd(1, points, coeffs, 2); | |
18092 | }; | |
18093 | ||
18094 | Point.prototype.jmulAdd = function jmulAdd(k1, p2, k2) { | |
18095 | var points = [ this, p2 ]; | |
18096 | var coeffs = [ k1, k2 ]; | |
18097 | if (this.curve.endo) | |
18098 | return this.curve._endoWnafMulAdd(points, coeffs, true); | |
18099 | else | |
18100 | return this.curve._wnafMulAdd(1, points, coeffs, 2, true); | |
18101 | }; | |
18102 | ||
18103 | Point.prototype.eq = function eq(p) { | |
18104 | return this === p || | |
18105 | this.inf === p.inf && | |
18106 | (this.inf || this.x.cmp(p.x) === 0 && this.y.cmp(p.y) === 0); | |
18107 | }; | |
18108 | ||
18109 | Point.prototype.neg = function neg(_precompute) { | |
18110 | if (this.inf) | |
18111 | return this; | |
18112 | ||
18113 | var res = this.curve.point(this.x, this.y.redNeg()); | |
18114 | if (_precompute && this.precomputed) { | |
18115 | var pre = this.precomputed; | |
18116 | var negate = function(p) { | |
18117 | return p.neg(); | |
18118 | }; | |
18119 | res.precomputed = { | |
18120 | naf: pre.naf && { | |
18121 | wnd: pre.naf.wnd, | |
18122 | points: pre.naf.points.map(negate) | |
18123 | }, | |
18124 | doubles: pre.doubles && { | |
18125 | step: pre.doubles.step, | |
18126 | points: pre.doubles.points.map(negate) | |
18127 | } | |
18128 | }; | |
18129 | } | |
18130 | return res; | |
18131 | }; | |
18132 | ||
18133 | Point.prototype.toJ = function toJ() { | |
18134 | if (this.inf) | |
18135 | return this.curve.jpoint(null, null, null); | |
18136 | ||
18137 | var res = this.curve.jpoint(this.x, this.y, this.curve.one); | |
18138 | return res; | |
18139 | }; | |
18140 | ||
18141 | function JPoint(curve, x, y, z) { | |
18142 | Base.BasePoint.call(this, curve, 'jacobian'); | |
18143 | if (x === null && y === null && z === null) { | |
18144 | this.x = this.curve.one; | |
18145 | this.y = this.curve.one; | |
18146 | this.z = new BN(0); | |
18147 | } else { | |
18148 | this.x = new BN(x, 16); | |
18149 | this.y = new BN(y, 16); | |
18150 | this.z = new BN(z, 16); | |
18151 | } | |
18152 | if (!this.x.red) | |
18153 | this.x = this.x.toRed(this.curve.red); | |
18154 | if (!this.y.red) | |
18155 | this.y = this.y.toRed(this.curve.red); | |
18156 | if (!this.z.red) | |
18157 | this.z = this.z.toRed(this.curve.red); | |
18158 | ||
18159 | this.zOne = this.z === this.curve.one; | |
18160 | } | |
18161 | inherits(JPoint, Base.BasePoint); | |
18162 | ||
18163 | ShortCurve.prototype.jpoint = function jpoint(x, y, z) { | |
18164 | return new JPoint(this, x, y, z); | |
18165 | }; | |
18166 | ||
18167 | JPoint.prototype.toP = function toP() { | |
18168 | if (this.isInfinity()) | |
18169 | return this.curve.point(null, null); | |
18170 | ||
18171 | var zinv = this.z.redInvm(); | |
18172 | var zinv2 = zinv.redSqr(); | |
18173 | var ax = this.x.redMul(zinv2); | |
18174 | var ay = this.y.redMul(zinv2).redMul(zinv); | |
18175 | ||
18176 | return this.curve.point(ax, ay); | |
18177 | }; | |
18178 | ||
18179 | JPoint.prototype.neg = function neg() { | |
18180 | return this.curve.jpoint(this.x, this.y.redNeg(), this.z); | |
18181 | }; | |
18182 | ||
18183 | JPoint.prototype.add = function add(p) { | |
18184 | // O + P = P | |
18185 | if (this.isInfinity()) | |
18186 | return p; | |
18187 | ||
18188 | // P + O = P | |
18189 | if (p.isInfinity()) | |
18190 | return this; | |
18191 | ||
18192 | // 12M + 4S + 7A | |
18193 | var pz2 = p.z.redSqr(); | |
18194 | var z2 = this.z.redSqr(); | |
18195 | var u1 = this.x.redMul(pz2); | |
18196 | var u2 = p.x.redMul(z2); | |
18197 | var s1 = this.y.redMul(pz2.redMul(p.z)); | |
18198 | var s2 = p.y.redMul(z2.redMul(this.z)); | |
18199 | ||
18200 | var h = u1.redSub(u2); | |
18201 | var r = s1.redSub(s2); | |
18202 | if (h.cmpn(0) === 0) { | |
18203 | if (r.cmpn(0) !== 0) | |
18204 | return this.curve.jpoint(null, null, null); | |
18205 | else | |
18206 | return this.dbl(); | |
18207 | } | |
18208 | ||
18209 | var h2 = h.redSqr(); | |
18210 | var h3 = h2.redMul(h); | |
18211 | var v = u1.redMul(h2); | |
18212 | ||
18213 | var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v); | |
18214 | var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3)); | |
18215 | var nz = this.z.redMul(p.z).redMul(h); | |
18216 | ||
18217 | return this.curve.jpoint(nx, ny, nz); | |
18218 | }; | |
18219 | ||
18220 | JPoint.prototype.mixedAdd = function mixedAdd(p) { | |
18221 | // O + P = P | |
18222 | if (this.isInfinity()) | |
18223 | return p.toJ(); | |
18224 | ||
18225 | // P + O = P | |
18226 | if (p.isInfinity()) | |
18227 | return this; | |
18228 | ||
18229 | // 8M + 3S + 7A | |
18230 | var z2 = this.z.redSqr(); | |
18231 | var u1 = this.x; | |
18232 | var u2 = p.x.redMul(z2); | |
18233 | var s1 = this.y; | |
18234 | var s2 = p.y.redMul(z2).redMul(this.z); | |
18235 | ||
18236 | var h = u1.redSub(u2); | |
18237 | var r = s1.redSub(s2); | |
18238 | if (h.cmpn(0) === 0) { | |
18239 | if (r.cmpn(0) !== 0) | |
18240 | return this.curve.jpoint(null, null, null); | |
18241 | else | |
18242 | return this.dbl(); | |
18243 | } | |
18244 | ||
18245 | var h2 = h.redSqr(); | |
18246 | var h3 = h2.redMul(h); | |
18247 | var v = u1.redMul(h2); | |
18248 | ||
18249 | var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v); | |
18250 | var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3)); | |
18251 | var nz = this.z.redMul(h); | |
18252 | ||
18253 | return this.curve.jpoint(nx, ny, nz); | |
18254 | }; | |
18255 | ||
18256 | JPoint.prototype.dblp = function dblp(pow) { | |
18257 | if (pow === 0) | |
18258 | return this; | |
18259 | if (this.isInfinity()) | |
18260 | return this; | |
18261 | if (!pow) | |
18262 | return this.dbl(); | |
18263 | ||
18264 | if (this.curve.zeroA || this.curve.threeA) { | |
18265 | var r = this; | |
18266 | for (var i = 0; i < pow; i++) | |
18267 | r = r.dbl(); | |
18268 | return r; | |
18269 | } | |
18270 | ||
18271 | // 1M + 2S + 1A + N * (4S + 5M + 8A) | |
18272 | // N = 1 => 6M + 6S + 9A | |
18273 | var a = this.curve.a; | |
18274 | var tinv = this.curve.tinv; | |
18275 | ||
18276 | var jx = this.x; | |
18277 | var jy = this.y; | |
18278 | var jz = this.z; | |
18279 | var jz4 = jz.redSqr().redSqr(); | |
18280 | ||
18281 | // Reuse results | |
18282 | var jyd = jy.redAdd(jy); | |
18283 | for (var i = 0; i < pow; i++) { | |
18284 | var jx2 = jx.redSqr(); | |
18285 | var jyd2 = jyd.redSqr(); | |
18286 | var jyd4 = jyd2.redSqr(); | |
18287 | var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4)); | |
18288 | ||
18289 | var t1 = jx.redMul(jyd2); | |
18290 | var nx = c.redSqr().redISub(t1.redAdd(t1)); | |
18291 | var t2 = t1.redISub(nx); | |
18292 | var dny = c.redMul(t2); | |
18293 | dny = dny.redIAdd(dny).redISub(jyd4); | |
18294 | var nz = jyd.redMul(jz); | |
18295 | if (i + 1 < pow) | |
18296 | jz4 = jz4.redMul(jyd4); | |
18297 | ||
18298 | jx = nx; | |
18299 | jz = nz; | |
18300 | jyd = dny; | |
18301 | } | |
18302 | ||
18303 | return this.curve.jpoint(jx, jyd.redMul(tinv), jz); | |
18304 | }; | |
18305 | ||
18306 | JPoint.prototype.dbl = function dbl() { | |
18307 | if (this.isInfinity()) | |
18308 | return this; | |
18309 | ||
18310 | if (this.curve.zeroA) | |
18311 | return this._zeroDbl(); | |
18312 | else if (this.curve.threeA) | |
18313 | return this._threeDbl(); | |
18314 | else | |
18315 | return this._dbl(); | |
18316 | }; | |
18317 | ||
18318 | JPoint.prototype._zeroDbl = function _zeroDbl() { | |
18319 | var nx; | |
18320 | var ny; | |
18321 | var nz; | |
18322 | // Z = 1 | |
18323 | if (this.zOne) { | |
18324 | // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html | |
18325 | // #doubling-mdbl-2007-bl | |
18326 | // 1M + 5S + 14A | |
18327 | ||
18328 | // XX = X1^2 | |
18329 | var xx = this.x.redSqr(); | |
18330 | // YY = Y1^2 | |
18331 | var yy = this.y.redSqr(); | |
18332 | // YYYY = YY^2 | |
18333 | var yyyy = yy.redSqr(); | |
18334 | // S = 2 * ((X1 + YY)^2 - XX - YYYY) | |
18335 | var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy); | |
18336 | s = s.redIAdd(s); | |
18337 | // M = 3 * XX + a; a = 0 | |
18338 | var m = xx.redAdd(xx).redIAdd(xx); | |
18339 | // T = M ^ 2 - 2*S | |
18340 | var t = m.redSqr().redISub(s).redISub(s); | |
18341 | ||
18342 | // 8 * YYYY | |
18343 | var yyyy8 = yyyy.redIAdd(yyyy); | |
18344 | yyyy8 = yyyy8.redIAdd(yyyy8); | |
18345 | yyyy8 = yyyy8.redIAdd(yyyy8); | |
18346 | ||
18347 | // X3 = T | |
18348 | nx = t; | |
18349 | // Y3 = M * (S - T) - 8 * YYYY | |
18350 | ny = m.redMul(s.redISub(t)).redISub(yyyy8); | |
18351 | // Z3 = 2*Y1 | |
18352 | nz = this.y.redAdd(this.y); | |
18353 | } else { | |
18354 | // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html | |
18355 | // #doubling-dbl-2009-l | |
18356 | // 2M + 5S + 13A | |
18357 | ||
18358 | // A = X1^2 | |
18359 | var a = this.x.redSqr(); | |
18360 | // B = Y1^2 | |
18361 | var b = this.y.redSqr(); | |
18362 | // C = B^2 | |
18363 | var c = b.redSqr(); | |
18364 | // D = 2 * ((X1 + B)^2 - A - C) | |
18365 | var d = this.x.redAdd(b).redSqr().redISub(a).redISub(c); | |
18366 | d = d.redIAdd(d); | |
18367 | // E = 3 * A | |
18368 | var e = a.redAdd(a).redIAdd(a); | |
18369 | // F = E^2 | |
18370 | var f = e.redSqr(); | |
18371 | ||
18372 | // 8 * C | |
18373 | var c8 = c.redIAdd(c); | |
18374 | c8 = c8.redIAdd(c8); | |
18375 | c8 = c8.redIAdd(c8); | |
18376 | ||
18377 | // X3 = F - 2 * D | |
18378 | nx = f.redISub(d).redISub(d); | |
18379 | // Y3 = E * (D - X3) - 8 * C | |
18380 | ny = e.redMul(d.redISub(nx)).redISub(c8); | |
18381 | // Z3 = 2 * Y1 * Z1 | |
18382 | nz = this.y.redMul(this.z); | |
18383 | nz = nz.redIAdd(nz); | |
18384 | } | |
18385 | ||
18386 | return this.curve.jpoint(nx, ny, nz); | |
18387 | }; | |
18388 | ||
18389 | JPoint.prototype._threeDbl = function _threeDbl() { | |
18390 | var nx; | |
18391 | var ny; | |
18392 | var nz; | |
18393 | // Z = 1 | |
18394 | if (this.zOne) { | |
18395 | // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html | |
18396 | // #doubling-mdbl-2007-bl | |
18397 | // 1M + 5S + 15A | |
18398 | ||
18399 | // XX = X1^2 | |
18400 | var xx = this.x.redSqr(); | |
18401 | // YY = Y1^2 | |
18402 | var yy = this.y.redSqr(); | |
18403 | // YYYY = YY^2 | |
18404 | var yyyy = yy.redSqr(); | |
18405 | // S = 2 * ((X1 + YY)^2 - XX - YYYY) | |
18406 | var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy); | |
18407 | s = s.redIAdd(s); | |
18408 | // M = 3 * XX + a | |
18409 | var m = xx.redAdd(xx).redIAdd(xx).redIAdd(this.curve.a); | |
18410 | // T = M^2 - 2 * S | |
18411 | var t = m.redSqr().redISub(s).redISub(s); | |
18412 | // X3 = T | |
18413 | nx = t; | |
18414 | // Y3 = M * (S - T) - 8 * YYYY | |
18415 | var yyyy8 = yyyy.redIAdd(yyyy); | |
18416 | yyyy8 = yyyy8.redIAdd(yyyy8); | |
18417 | yyyy8 = yyyy8.redIAdd(yyyy8); | |
18418 | ny = m.redMul(s.redISub(t)).redISub(yyyy8); | |
18419 | // Z3 = 2 * Y1 | |
18420 | nz = this.y.redAdd(this.y); | |
18421 | } else { | |
18422 | // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b | |
18423 | // 3M + 5S | |
18424 | ||
18425 | // delta = Z1^2 | |
18426 | var delta = this.z.redSqr(); | |
18427 | // gamma = Y1^2 | |
18428 | var gamma = this.y.redSqr(); | |
18429 | // beta = X1 * gamma | |
18430 | var beta = this.x.redMul(gamma); | |
18431 | // alpha = 3 * (X1 - delta) * (X1 + delta) | |
18432 | var alpha = this.x.redSub(delta).redMul(this.x.redAdd(delta)); | |
18433 | alpha = alpha.redAdd(alpha).redIAdd(alpha); | |
18434 | // X3 = alpha^2 - 8 * beta | |
18435 | var beta4 = beta.redIAdd(beta); | |
18436 | beta4 = beta4.redIAdd(beta4); | |
18437 | var beta8 = beta4.redAdd(beta4); | |
18438 | nx = alpha.redSqr().redISub(beta8); | |
18439 | // Z3 = (Y1 + Z1)^2 - gamma - delta | |
18440 | nz = this.y.redAdd(this.z).redSqr().redISub(gamma).redISub(delta); | |
18441 | // Y3 = alpha * (4 * beta - X3) - 8 * gamma^2 | |
18442 | var ggamma8 = gamma.redSqr(); | |
18443 | ggamma8 = ggamma8.redIAdd(ggamma8); | |
18444 | ggamma8 = ggamma8.redIAdd(ggamma8); | |
18445 | ggamma8 = ggamma8.redIAdd(ggamma8); | |
18446 | ny = alpha.redMul(beta4.redISub(nx)).redISub(ggamma8); | |
18447 | } | |
18448 | ||
18449 | return this.curve.jpoint(nx, ny, nz); | |
18450 | }; | |
18451 | ||
18452 | JPoint.prototype._dbl = function _dbl() { | |
18453 | var a = this.curve.a; | |
18454 | ||
18455 | // 4M + 6S + 10A | |
18456 | var jx = this.x; | |
18457 | var jy = this.y; | |
18458 | var jz = this.z; | |
18459 | var jz4 = jz.redSqr().redSqr(); | |
18460 | ||
18461 | var jx2 = jx.redSqr(); | |
18462 | var jy2 = jy.redSqr(); | |
18463 | ||
18464 | var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4)); | |
18465 | ||
18466 | var jxd4 = jx.redAdd(jx); | |
18467 | jxd4 = jxd4.redIAdd(jxd4); | |
18468 | var t1 = jxd4.redMul(jy2); | |
18469 | var nx = c.redSqr().redISub(t1.redAdd(t1)); | |
18470 | var t2 = t1.redISub(nx); | |
18471 | ||
18472 | var jyd8 = jy2.redSqr(); | |
18473 | jyd8 = jyd8.redIAdd(jyd8); | |
18474 | jyd8 = jyd8.redIAdd(jyd8); | |
18475 | jyd8 = jyd8.redIAdd(jyd8); | |
18476 | var ny = c.redMul(t2).redISub(jyd8); | |
18477 | var nz = jy.redAdd(jy).redMul(jz); | |
18478 | ||
18479 | return this.curve.jpoint(nx, ny, nz); | |
18480 | }; | |
18481 | ||
18482 | JPoint.prototype.trpl = function trpl() { | |
18483 | if (!this.curve.zeroA) | |
18484 | return this.dbl().add(this); | |
18485 | ||
18486 | // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#tripling-tpl-2007-bl | |
18487 | // 5M + 10S + ... | |
18488 | ||
18489 | // XX = X1^2 | |
18490 | var xx = this.x.redSqr(); | |
18491 | // YY = Y1^2 | |
18492 | var yy = this.y.redSqr(); | |
18493 | // ZZ = Z1^2 | |
18494 | var zz = this.z.redSqr(); | |
18495 | // YYYY = YY^2 | |
18496 | var yyyy = yy.redSqr(); | |
18497 | // M = 3 * XX + a * ZZ2; a = 0 | |
18498 | var m = xx.redAdd(xx).redIAdd(xx); | |
18499 | // MM = M^2 | |
18500 | var mm = m.redSqr(); | |
18501 | // E = 6 * ((X1 + YY)^2 - XX - YYYY) - MM | |
18502 | var e = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy); | |
18503 | e = e.redIAdd(e); | |
18504 | e = e.redAdd(e).redIAdd(e); | |
18505 | e = e.redISub(mm); | |
18506 | // EE = E^2 | |
18507 | var ee = e.redSqr(); | |
18508 | // T = 16*YYYY | |
18509 | var t = yyyy.redIAdd(yyyy); | |
18510 | t = t.redIAdd(t); | |
18511 | t = t.redIAdd(t); | |
18512 | t = t.redIAdd(t); | |
18513 | // U = (M + E)^2 - MM - EE - T | |
18514 | var u = m.redIAdd(e).redSqr().redISub(mm).redISub(ee).redISub(t); | |
18515 | // X3 = 4 * (X1 * EE - 4 * YY * U) | |
18516 | var yyu4 = yy.redMul(u); | |
18517 | yyu4 = yyu4.redIAdd(yyu4); | |
18518 | yyu4 = yyu4.redIAdd(yyu4); | |
18519 | var nx = this.x.redMul(ee).redISub(yyu4); | |
18520 | nx = nx.redIAdd(nx); | |
18521 | nx = nx.redIAdd(nx); | |
18522 | // Y3 = 8 * Y1 * (U * (T - U) - E * EE) | |
18523 | var ny = this.y.redMul(u.redMul(t.redISub(u)).redISub(e.redMul(ee))); | |
18524 | ny = ny.redIAdd(ny); | |
18525 | ny = ny.redIAdd(ny); | |
18526 | ny = ny.redIAdd(ny); | |
18527 | // Z3 = (Z1 + E)^2 - ZZ - EE | |
18528 | var nz = this.z.redAdd(e).redSqr().redISub(zz).redISub(ee); | |
18529 | ||
18530 | return this.curve.jpoint(nx, ny, nz); | |
18531 | }; | |
18532 | ||
18533 | JPoint.prototype.mul = function mul(k, kbase) { | |
18534 | k = new BN(k, kbase); | |
18535 | ||
18536 | return this.curve._wnafMul(this, k); | |
18537 | }; | |
18538 | ||
18539 | JPoint.prototype.eq = function eq(p) { | |
18540 | if (p.type === 'affine') | |
18541 | return this.eq(p.toJ()); | |
18542 | ||
18543 | if (this === p) | |
18544 | return true; | |
18545 | ||
18546 | // x1 * z2^2 == x2 * z1^2 | |
18547 | var z2 = this.z.redSqr(); | |
18548 | var pz2 = p.z.redSqr(); | |
18549 | if (this.x.redMul(pz2).redISub(p.x.redMul(z2)).cmpn(0) !== 0) | |
18550 | return false; | |
18551 | ||
18552 | // y1 * z2^3 == y2 * z1^3 | |
18553 | var z3 = z2.redMul(this.z); | |
18554 | var pz3 = pz2.redMul(p.z); | |
18555 | return this.y.redMul(pz3).redISub(p.y.redMul(z3)).cmpn(0) === 0; | |
18556 | }; | |
18557 | ||
18558 | JPoint.prototype.eqXToP = function eqXToP(x) { | |
18559 | var zs = this.z.redSqr(); | |
18560 | var rx = x.toRed(this.curve.red).redMul(zs); | |
18561 | if (this.x.cmp(rx) === 0) | |
18562 | return true; | |
18563 | ||
18564 | var xc = x.clone(); | |
18565 | var t = this.curve.redN.redMul(zs); | |
18566 | for (;;) { | |
18567 | xc.iadd(this.curve.n); | |
18568 | if (xc.cmp(this.curve.p) >= 0) | |
18569 | return false; | |
18570 | ||
18571 | rx.redIAdd(t); | |
18572 | if (this.x.cmp(rx) === 0) | |
18573 | return true; | |
18574 | } | |
18575 | return false; | |
18576 | }; | |
18577 | ||
18578 | JPoint.prototype.inspect = function inspect() { | |
18579 | if (this.isInfinity()) | |
18580 | return '<EC JPoint Infinity>'; | |
18581 | return '<EC JPoint x: ' + this.x.toString(16, 2) + | |
18582 | ' y: ' + this.y.toString(16, 2) + | |
18583 | ' z: ' + this.z.toString(16, 2) + '>'; | |
18584 | }; | |
18585 | ||
18586 | JPoint.prototype.isInfinity = function isInfinity() { | |
18587 | // XXX This code assumes that zero is always zero in red | |
18588 | return this.z.cmpn(0) === 0; | |
18589 | }; | |
18590 | ||
18591 | },{"../../elliptic":97,"../curve":100,"bn.js":81,"inherits":121}],103:[function(require,module,exports){ | |
18592 | 'use strict'; | |
18593 | ||
18594 | var curves = exports; | |
18595 | ||
18596 | var hash = require('hash.js'); | |
18597 | var elliptic = require('../elliptic'); | |
18598 | ||
18599 | var assert = elliptic.utils.assert; | |
18600 | ||
18601 | function PresetCurve(options) { | |
18602 | if (options.type === 'short') | |
18603 | this.curve = new elliptic.curve.short(options); | |
18604 | else if (options.type === 'edwards') | |
18605 | this.curve = new elliptic.curve.edwards(options); | |
18606 | else | |
18607 | this.curve = new elliptic.curve.mont(options); | |
18608 | this.g = this.curve.g; | |
18609 | this.n = this.curve.n; | |
18610 | this.hash = options.hash; | |
18611 | ||
18612 | assert(this.g.validate(), 'Invalid curve'); | |
18613 | assert(this.g.mul(this.n).isInfinity(), 'Invalid curve, G*N != O'); | |
18614 | } | |
18615 | curves.PresetCurve = PresetCurve; | |
18616 | ||
18617 | function defineCurve(name, options) { | |
18618 | Object.defineProperty(curves, name, { | |
18619 | configurable: true, | |
18620 | enumerable: true, | |
18621 | get: function() { | |
18622 | var curve = new PresetCurve(options); | |
18623 | Object.defineProperty(curves, name, { | |
18624 | configurable: true, | |
18625 | enumerable: true, | |
18626 | value: curve | |
18627 | }); | |
18628 | return curve; | |
18629 | } | |
18630 | }); | |
18631 | } | |
18632 | ||
18633 | defineCurve('p192', { | |
18634 | type: 'short', | |
18635 | prime: 'p192', | |
18636 | p: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff', | |
18637 | a: 'ffffffff ffffffff ffffffff fffffffe ffffffff fffffffc', | |
18638 | b: '64210519 e59c80e7 0fa7e9ab 72243049 feb8deec c146b9b1', | |
18639 | n: 'ffffffff ffffffff ffffffff 99def836 146bc9b1 b4d22831', | |
18640 | hash: hash.sha256, | |
18641 | gRed: false, | |
18642 | g: [ | |
18643 | '188da80e b03090f6 7cbf20eb 43a18800 f4ff0afd 82ff1012', | |
18644 | '07192b95 ffc8da78 631011ed 6b24cdd5 73f977a1 1e794811' | |
18645 | ] | |
18646 | }); | |
18647 | ||
18648 | defineCurve('p224', { | |
18649 | type: 'short', | |
18650 | prime: 'p224', | |
18651 | p: 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001', | |
18652 | a: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff fffffffe', | |
18653 | b: 'b4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4', | |
18654 | n: 'ffffffff ffffffff ffffffff ffff16a2 e0b8f03e 13dd2945 5c5c2a3d', | |
18655 | hash: hash.sha256, | |
18656 | gRed: false, | |
18657 | g: [ | |
18658 | 'b70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21', | |
18659 | 'bd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34' | |
18660 | ] | |
18661 | }); | |
18662 | ||
18663 | defineCurve('p256', { | |
18664 | type: 'short', | |
18665 | prime: null, | |
18666 | p: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff', | |
18667 | a: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff fffffffc', | |
18668 | b: '5ac635d8 aa3a93e7 b3ebbd55 769886bc 651d06b0 cc53b0f6 3bce3c3e 27d2604b', | |
18669 | n: 'ffffffff 00000000 ffffffff ffffffff bce6faad a7179e84 f3b9cac2 fc632551', | |
18670 | hash: hash.sha256, | |
18671 | gRed: false, | |
18672 | g: [ | |
18673 | '6b17d1f2 e12c4247 f8bce6e5 63a440f2 77037d81 2deb33a0 f4a13945 d898c296', | |
18674 | '4fe342e2 fe1a7f9b 8ee7eb4a 7c0f9e16 2bce3357 6b315ece cbb64068 37bf51f5' | |
18675 | ] | |
18676 | }); | |
18677 | ||
18678 | defineCurve('p384', { | |
18679 | type: 'short', | |
18680 | prime: null, | |
18681 | p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' + | |
18682 | 'fffffffe ffffffff 00000000 00000000 ffffffff', | |
18683 | a: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' + | |
18684 | 'fffffffe ffffffff 00000000 00000000 fffffffc', | |
18685 | b: 'b3312fa7 e23ee7e4 988e056b e3f82d19 181d9c6e fe814112 0314088f ' + | |
18686 | '5013875a c656398d 8a2ed19d 2a85c8ed d3ec2aef', | |
18687 | n: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff c7634d81 ' + | |
18688 | 'f4372ddf 581a0db2 48b0a77a ecec196a ccc52973', | |
18689 | hash: hash.sha384, | |
18690 | gRed: false, | |
18691 | g: [ | |
18692 | 'aa87ca22 be8b0537 8eb1c71e f320ad74 6e1d3b62 8ba79b98 59f741e0 82542a38 ' + | |
18693 | '5502f25d bf55296c 3a545e38 72760ab7', | |
18694 | '3617de4a 96262c6f 5d9e98bf 9292dc29 f8f41dbd 289a147c e9da3113 b5f0b8c0 ' + | |
18695 | '0a60b1ce 1d7e819d 7a431d7c 90ea0e5f' | |
18696 | ] | |
18697 | }); | |
18698 | ||
18699 | defineCurve('p521', { | |
18700 | type: 'short', | |
18701 | prime: null, | |
18702 | p: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' + | |
18703 | 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' + | |
18704 | 'ffffffff ffffffff ffffffff ffffffff ffffffff', | |
18705 | a: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' + | |
18706 | 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' + | |
18707 | 'ffffffff ffffffff ffffffff ffffffff fffffffc', | |
18708 | b: '00000051 953eb961 8e1c9a1f 929a21a0 b68540ee a2da725b ' + | |
18709 | '99b315f3 b8b48991 8ef109e1 56193951 ec7e937b 1652c0bd ' + | |
18710 | '3bb1bf07 3573df88 3d2c34f1 ef451fd4 6b503f00', | |
18711 | n: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' + | |
18712 | 'ffffffff ffffffff fffffffa 51868783 bf2f966b 7fcc0148 ' + | |
18713 | 'f709a5d0 3bb5c9b8 899c47ae bb6fb71e 91386409', | |
18714 | hash: hash.sha512, | |
18715 | gRed: false, | |
18716 | g: [ | |
18717 | '000000c6 858e06b7 0404e9cd 9e3ecb66 2395b442 9c648139 ' + | |
18718 | '053fb521 f828af60 6b4d3dba a14b5e77 efe75928 fe1dc127 ' + | |
18719 | 'a2ffa8de 3348b3c1 856a429b f97e7e31 c2e5bd66', | |
18720 | '00000118 39296a78 9a3bc004 5c8a5fb4 2c7d1bd9 98f54449 ' + | |
18721 | '579b4468 17afbd17 273e662c 97ee7299 5ef42640 c550b901 ' + | |
18722 | '3fad0761 353c7086 a272c240 88be9476 9fd16650' | |
18723 | ] | |
18724 | }); | |
18725 | ||
18726 | defineCurve('curve25519', { | |
18727 | type: 'mont', | |
18728 | prime: 'p25519', | |
18729 | p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed', | |
18730 | a: '76d06', | |
18731 | b: '1', | |
18732 | n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed', | |
18733 | hash: hash.sha256, | |
18734 | gRed: false, | |
18735 | g: [ | |
18736 | '9' | |
18737 | ] | |
18738 | }); | |
18739 | ||
18740 | defineCurve('ed25519', { | |
18741 | type: 'edwards', | |
18742 | prime: 'p25519', | |
18743 | p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed', | |
18744 | a: '-1', | |
18745 | c: '1', | |
18746 | // -121665 * (121666^(-1)) (mod P) | |
18747 | d: '52036cee2b6ffe73 8cc740797779e898 00700a4d4141d8ab 75eb4dca135978a3', | |
18748 | n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed', | |
18749 | hash: hash.sha256, | |
18750 | gRed: false, | |
18751 | g: [ | |
18752 | '216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a', | |
18753 | ||
18754 | // 4/5 | |
18755 | '6666666666666666666666666666666666666666666666666666666666666658' | |
18756 | ] | |
18757 | }); | |
18758 | ||
18759 | var pre; | |
18760 | try { | |
18761 | pre = require('./precomputed/secp256k1'); | |
18762 | } catch (e) { | |
18763 | pre = undefined; | |
18764 | } | |
18765 | ||
18766 | defineCurve('secp256k1', { | |
18767 | type: 'short', | |
18768 | prime: 'k256', | |
18769 | p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f', | |
18770 | a: '0', | |
18771 | b: '7', | |
18772 | n: 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141', | |
18773 | h: '1', | |
18774 | hash: hash.sha256, | |
18775 | ||
18776 | // Precomputed endomorphism | |
18777 | beta: '7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee', | |
18778 | lambda: '5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72', | |
18779 | basis: [ | |
18780 | { | |
18781 | a: '3086d221a7d46bcde86c90e49284eb15', | |
18782 | b: '-e4437ed6010e88286f547fa90abfe4c3' | |
18783 | }, | |
18784 | { | |
18785 | a: '114ca50f7a8e2f3f657c1108d9d44cfd8', | |
18786 | b: '3086d221a7d46bcde86c90e49284eb15' | |
18787 | } | |
18788 | ], | |
18789 | ||
18790 | gRed: false, | |
18791 | g: [ | |
18792 | '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', | |
18793 | '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8', | |
18794 | pre | |
18795 | ] | |
18796 | }); | |
18797 | ||
18798 | },{"../elliptic":97,"./precomputed/secp256k1":110,"hash.js":114}],104:[function(require,module,exports){ | |
18799 | 'use strict'; | |
18800 | ||
18801 | var BN = require('bn.js'); | |
18802 | var HmacDRBG = require('hmac-drbg'); | |
18803 | var elliptic = require('../../elliptic'); | |
18804 | var utils = elliptic.utils; | |
18805 | var assert = utils.assert; | |
18806 | ||
18807 | var KeyPair = require('./key'); | |
18808 | var Signature = require('./signature'); | |
18809 | ||
18810 | function EC(options) { | |
18811 | if (!(this instanceof EC)) | |
18812 | return new EC(options); | |
18813 | ||
18814 | // Shortcut `elliptic.ec(curve-name)` | |
18815 | if (typeof options === 'string') { | |
18816 | assert(elliptic.curves.hasOwnProperty(options), 'Unknown curve ' + options); | |
18817 | ||
18818 | options = elliptic.curves[options]; | |
18819 | } | |
18820 | ||
18821 | // Shortcut for `elliptic.ec(elliptic.curves.curveName)` | |
18822 | if (options instanceof elliptic.curves.PresetCurve) | |
18823 | options = { curve: options }; | |
18824 | ||
18825 | this.curve = options.curve.curve; | |
18826 | this.n = this.curve.n; | |
18827 | this.nh = this.n.ushrn(1); | |
18828 | this.g = this.curve.g; | |
18829 | ||
18830 | // Point on curve | |
18831 | this.g = options.curve.g; | |
18832 | this.g.precompute(options.curve.n.bitLength() + 1); | |
18833 | ||
18834 | // Hash for function for DRBG | |
18835 | this.hash = options.hash || options.curve.hash; | |
18836 | } | |
18837 | module.exports = EC; | |
18838 | ||
18839 | EC.prototype.keyPair = function keyPair(options) { | |
18840 | return new KeyPair(this, options); | |
18841 | }; | |
18842 | ||
18843 | EC.prototype.keyFromPrivate = function keyFromPrivate(priv, enc) { | |
18844 | return KeyPair.fromPrivate(this, priv, enc); | |
18845 | }; | |
18846 | ||
18847 | EC.prototype.keyFromPublic = function keyFromPublic(pub, enc) { | |
18848 | return KeyPair.fromPublic(this, pub, enc); | |
18849 | }; | |
18850 | ||
18851 | EC.prototype.genKeyPair = function genKeyPair(options) { | |
18852 | if (!options) | |
18853 | options = {}; | |
18854 | ||
18855 | // Instantiate Hmac_DRBG | |
18856 | var drbg = new HmacDRBG({ | |
18857 | hash: this.hash, | |
18858 | pers: options.pers, | |
18859 | persEnc: options.persEnc || 'utf8', | |
18860 | entropy: options.entropy || elliptic.rand(this.hash.hmacStrength), | |
18861 | entropyEnc: options.entropy && options.entropyEnc || 'utf8', | |
18862 | nonce: this.n.toArray() | |
18863 | }); | |
18864 | ||
18865 | var bytes = this.n.byteLength(); | |
18866 | var ns2 = this.n.sub(new BN(2)); | |
18867 | do { | |
18868 | var priv = new BN(drbg.generate(bytes)); | |
18869 | if (priv.cmp(ns2) > 0) | |
18870 | continue; | |
18871 | ||
18872 | priv.iaddn(1); | |
18873 | return this.keyFromPrivate(priv); | |
18874 | } while (true); | |
18875 | }; | |
18876 | ||
18877 | EC.prototype._truncateToN = function truncateToN(msg, truncOnly) { | |
18878 | var delta = msg.byteLength() * 8 - this.n.bitLength(); | |
18879 | if (delta > 0) | |
18880 | msg = msg.ushrn(delta); | |
18881 | if (!truncOnly && msg.cmp(this.n) >= 0) | |
18882 | return msg.sub(this.n); | |
18883 | else | |
18884 | return msg; | |
18885 | }; | |
18886 | ||
18887 | EC.prototype.sign = function sign(msg, key, enc, options) { | |
18888 | if (typeof enc === 'object') { | |
18889 | options = enc; | |
18890 | enc = null; | |
18891 | } | |
18892 | if (!options) | |
18893 | options = {}; | |
18894 | ||
18895 | key = this.keyFromPrivate(key, enc); | |
18896 | msg = this._truncateToN(new BN(msg, 16)); | |
18897 | ||
18898 | // Zero-extend key to provide enough entropy | |
18899 | var bytes = this.n.byteLength(); | |
18900 | var bkey = key.getPrivate().toArray('be', bytes); | |
18901 | ||
18902 | // Zero-extend nonce to have the same byte size as N | |
18903 | var nonce = msg.toArray('be', bytes); | |
18904 | ||
18905 | // Instantiate Hmac_DRBG | |
18906 | var drbg = new HmacDRBG({ | |
18907 | hash: this.hash, | |
18908 | entropy: bkey, | |
18909 | nonce: nonce, | |
18910 | pers: options.pers, | |
18911 | persEnc: options.persEnc || 'utf8' | |
18912 | }); | |
18913 | ||
18914 | // Number of bytes to generate | |
18915 | var ns1 = this.n.sub(new BN(1)); | |
18916 | ||
18917 | for (var iter = 0; true; iter++) { | |
18918 | var k = options.k ? | |
18919 | options.k(iter) : | |
18920 | new BN(drbg.generate(this.n.byteLength())); | |
18921 | k = this._truncateToN(k, true); | |
18922 | if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0) | |
18923 | continue; | |
18924 | ||
18925 | var kp = this.g.mul(k); | |
18926 | if (kp.isInfinity()) | |
18927 | continue; | |
18928 | ||
18929 | var kpX = kp.getX(); | |
18930 | var r = kpX.umod(this.n); | |
18931 | if (r.cmpn(0) === 0) | |
18932 | continue; | |
18933 | ||
18934 | var s = k.invm(this.n).mul(r.mul(key.getPrivate()).iadd(msg)); | |
18935 | s = s.umod(this.n); | |
18936 | if (s.cmpn(0) === 0) | |
18937 | continue; | |
18938 | ||
18939 | var recoveryParam = (kp.getY().isOdd() ? 1 : 0) | | |
18940 | (kpX.cmp(r) !== 0 ? 2 : 0); | |
18941 | ||
18942 | // Use complement of `s`, if it is > `n / 2` | |
18943 | if (options.canonical && s.cmp(this.nh) > 0) { | |
18944 | s = this.n.sub(s); | |
18945 | recoveryParam ^= 1; | |
18946 | } | |
18947 | ||
18948 | return new Signature({ r: r, s: s, recoveryParam: recoveryParam }); | |
18949 | } | |
18950 | }; | |
18951 | ||
18952 | EC.prototype.verify = function verify(msg, signature, key, enc) { | |
18953 | msg = this._truncateToN(new BN(msg, 16)); | |
18954 | key = this.keyFromPublic(key, enc); | |
18955 | signature = new Signature(signature, 'hex'); | |
18956 | ||
18957 | // Perform primitive values validation | |
18958 | var r = signature.r; | |
18959 | var s = signature.s; | |
18960 | if (r.cmpn(1) < 0 || r.cmp(this.n) >= 0) | |
18961 | return false; | |
18962 | if (s.cmpn(1) < 0 || s.cmp(this.n) >= 0) | |
18963 | return false; | |
18964 | ||
18965 | // Validate signature | |
18966 | var sinv = s.invm(this.n); | |
18967 | var u1 = sinv.mul(msg).umod(this.n); | |
18968 | var u2 = sinv.mul(r).umod(this.n); | |
18969 | ||
18970 | if (!this.curve._maxwellTrick) { | |
18971 | var p = this.g.mulAdd(u1, key.getPublic(), u2); | |
18972 | if (p.isInfinity()) | |
18973 | return false; | |
18974 | ||
18975 | return p.getX().umod(this.n).cmp(r) === 0; | |
18976 | } | |
18977 | ||
18978 | // NOTE: Greg Maxwell's trick, inspired by: | |
18979 | // https://git.io/vad3K | |
18980 | ||
18981 | var p = this.g.jmulAdd(u1, key.getPublic(), u2); | |
18982 | if (p.isInfinity()) | |
18983 | return false; | |
18984 | ||
18985 | // Compare `p.x` of Jacobian point with `r`, | |
18986 | // this will do `p.x == r * p.z^2` instead of multiplying `p.x` by the | |
18987 | // inverse of `p.z^2` | |
18988 | return p.eqXToP(r); | |
18989 | }; | |
18990 | ||
18991 | EC.prototype.recoverPubKey = function(msg, signature, j, enc) { | |
18992 | assert((3 & j) === j, 'The recovery param is more than two bits'); | |
18993 | signature = new Signature(signature, enc); | |
18994 | ||
18995 | var n = this.n; | |
18996 | var e = new BN(msg); | |
18997 | var r = signature.r; | |
18998 | var s = signature.s; | |
18999 | ||
19000 | // A set LSB signifies that the y-coordinate is odd | |
19001 | var isYOdd = j & 1; | |
19002 | var isSecondKey = j >> 1; | |
19003 | if (r.cmp(this.curve.p.umod(this.curve.n)) >= 0 && isSecondKey) | |
19004 | throw new Error('Unable to find sencond key candinate'); | |
19005 | ||
19006 | // 1.1. Let x = r + jn. | |
19007 | if (isSecondKey) | |
19008 | r = this.curve.pointFromX(r.add(this.curve.n), isYOdd); | |
19009 | else | |
19010 | r = this.curve.pointFromX(r, isYOdd); | |
19011 | ||
19012 | var rInv = signature.r.invm(n); | |
19013 | var s1 = n.sub(e).mul(rInv).umod(n); | |
19014 | var s2 = s.mul(rInv).umod(n); | |
19015 | ||
19016 | // 1.6.1 Compute Q = r^-1 (sR - eG) | |
19017 | // Q = r^-1 (sR + -eG) | |
19018 | return this.g.mulAdd(s1, r, s2); | |
19019 | }; | |
19020 | ||
19021 | EC.prototype.getKeyRecoveryParam = function(e, signature, Q, enc) { | |
19022 | signature = new Signature(signature, enc); | |
19023 | if (signature.recoveryParam !== null) | |
19024 | return signature.recoveryParam; | |
19025 | ||
19026 | for (var i = 0; i < 4; i++) { | |
19027 | var Qprime; | |
19028 | try { | |
19029 | Qprime = this.recoverPubKey(e, signature, i); | |
19030 | } catch (e) { | |
19031 | continue; | |
19032 | } | |
19033 | ||
19034 | if (Qprime.eq(Q)) | |
19035 | return i; | |
19036 | } | |
19037 | throw new Error('Unable to find valid recovery factor'); | |
19038 | }; | |
19039 | ||
19040 | },{"../../elliptic":97,"./key":105,"./signature":106,"bn.js":81,"hmac-drbg":120}],105:[function(require,module,exports){ | |
19041 | 'use strict'; | |
19042 | ||
19043 | var BN = require('bn.js'); | |
19044 | var elliptic = require('../../elliptic'); | |
19045 | var utils = elliptic.utils; | |
19046 | var assert = utils.assert; | |
19047 | ||
19048 | function KeyPair(ec, options) { | |
19049 | this.ec = ec; | |
19050 | this.priv = null; | |
19051 | this.pub = null; | |
19052 | ||
19053 | // KeyPair(ec, { priv: ..., pub: ... }) | |
19054 | if (options.priv) | |
19055 | this._importPrivate(options.priv, options.privEnc); | |
19056 | if (options.pub) | |
19057 | this._importPublic(options.pub, options.pubEnc); | |
19058 | } | |
19059 | module.exports = KeyPair; | |
19060 | ||
19061 | KeyPair.fromPublic = function fromPublic(ec, pub, enc) { | |
19062 | if (pub instanceof KeyPair) | |
19063 | return pub; | |
19064 | ||
19065 | return new KeyPair(ec, { | |
19066 | pub: pub, | |
19067 | pubEnc: enc | |
19068 | }); | |
19069 | }; | |
19070 | ||
19071 | KeyPair.fromPrivate = function fromPrivate(ec, priv, enc) { | |
19072 | if (priv instanceof KeyPair) | |
19073 | return priv; | |
19074 | ||
19075 | return new KeyPair(ec, { | |
19076 | priv: priv, | |
19077 | privEnc: enc | |
19078 | }); | |
19079 | }; | |
19080 | ||
19081 | KeyPair.prototype.validate = function validate() { | |
19082 | var pub = this.getPublic(); | |
19083 | ||
19084 | if (pub.isInfinity()) | |
19085 | return { result: false, reason: 'Invalid public key' }; | |
19086 | if (!pub.validate()) | |
19087 | return { result: false, reason: 'Public key is not a point' }; | |
19088 | if (!pub.mul(this.ec.curve.n).isInfinity()) | |
19089 | return { result: false, reason: 'Public key * N != O' }; | |
19090 | ||
19091 | return { result: true, reason: null }; | |
19092 | }; | |
19093 | ||
19094 | KeyPair.prototype.getPublic = function getPublic(compact, enc) { | |
19095 | // compact is optional argument | |
19096 | if (typeof compact === 'string') { | |
19097 | enc = compact; | |
19098 | compact = null; | |
19099 | } | |
19100 | ||
19101 | if (!this.pub) | |
19102 | this.pub = this.ec.g.mul(this.priv); | |
19103 | ||
19104 | if (!enc) | |
19105 | return this.pub; | |
19106 | ||
19107 | return this.pub.encode(enc, compact); | |
19108 | }; | |
19109 | ||
19110 | KeyPair.prototype.getPrivate = function getPrivate(enc) { | |
19111 | if (enc === 'hex') | |
19112 | return this.priv.toString(16, 2); | |
19113 | else | |
19114 | return this.priv; | |
19115 | }; | |
19116 | ||
19117 | KeyPair.prototype._importPrivate = function _importPrivate(key, enc) { | |
19118 | this.priv = new BN(key, enc || 16); | |
19119 | ||
19120 | // Ensure that the priv won't be bigger than n, otherwise we may fail | |
19121 | // in fixed multiplication method | |
19122 | this.priv = this.priv.umod(this.ec.curve.n); | |
19123 | }; | |
19124 | ||
19125 | KeyPair.prototype._importPublic = function _importPublic(key, enc) { | |
19126 | if (key.x || key.y) { | |
19127 | // Montgomery points only have an `x` coordinate. | |
19128 | // Weierstrass/Edwards points on the other hand have both `x` and | |
19129 | // `y` coordinates. | |
19130 | if (this.ec.curve.type === 'mont') { | |
19131 | assert(key.x, 'Need x coordinate'); | |
19132 | } else if (this.ec.curve.type === 'short' || | |
19133 | this.ec.curve.type === 'edwards') { | |
19134 | assert(key.x && key.y, 'Need both x and y coordinate'); | |
19135 | } | |
19136 | this.pub = this.ec.curve.point(key.x, key.y); | |
19137 | return; | |
19138 | } | |
19139 | this.pub = this.ec.curve.decodePoint(key, enc); | |
19140 | }; | |
19141 | ||
19142 | // ECDH | |
19143 | KeyPair.prototype.derive = function derive(pub) { | |
19144 | return pub.mul(this.priv).getX(); | |
19145 | }; | |
19146 | ||
19147 | // ECDSA | |
19148 | KeyPair.prototype.sign = function sign(msg, enc, options) { | |
19149 | return this.ec.sign(msg, this, enc, options); | |
19150 | }; | |
19151 | ||
19152 | KeyPair.prototype.verify = function verify(msg, signature) { | |
19153 | return this.ec.verify(msg, signature, this); | |
19154 | }; | |
19155 | ||
19156 | KeyPair.prototype.inspect = function inspect() { | |
19157 | return '<Key priv: ' + (this.priv && this.priv.toString(16, 2)) + | |
19158 | ' pub: ' + (this.pub && this.pub.inspect()) + ' >'; | |
19159 | }; | |
19160 | ||
19161 | },{"../../elliptic":97,"bn.js":81}],106:[function(require,module,exports){ | |
19162 | 'use strict'; | |
19163 | ||
19164 | var BN = require('bn.js'); | |
19165 | ||
19166 | var elliptic = require('../../elliptic'); | |
19167 | var utils = elliptic.utils; | |
19168 | var assert = utils.assert; | |
19169 | ||
19170 | function Signature(options, enc) { | |
19171 | if (options instanceof Signature) | |
19172 | return options; | |
19173 | ||
19174 | if (this._importDER(options, enc)) | |
19175 | return; | |
19176 | ||
19177 | assert(options.r && options.s, 'Signature without r or s'); | |
19178 | this.r = new BN(options.r, 16); | |
19179 | this.s = new BN(options.s, 16); | |
19180 | if (options.recoveryParam === undefined) | |
19181 | this.recoveryParam = null; | |
19182 | else | |
19183 | this.recoveryParam = options.recoveryParam; | |
19184 | } | |
19185 | module.exports = Signature; | |
19186 | ||
19187 | function Position() { | |
19188 | this.place = 0; | |
19189 | } | |
19190 | ||
19191 | function getLength(buf, p) { | |
19192 | var initial = buf[p.place++]; | |
19193 | if (!(initial & 0x80)) { | |
19194 | return initial; | |
19195 | } | |
19196 | var octetLen = initial & 0xf; | |
19197 | var val = 0; | |
19198 | for (var i = 0, off = p.place; i < octetLen; i++, off++) { | |
19199 | val <<= 8; | |
19200 | val |= buf[off]; | |
19201 | } | |
19202 | p.place = off; | |
19203 | return val; | |
19204 | } | |
19205 | ||
19206 | function rmPadding(buf) { | |
19207 | var i = 0; | |
19208 | var len = buf.length - 1; | |
19209 | while (!buf[i] && !(buf[i + 1] & 0x80) && i < len) { | |
19210 | i++; | |
19211 | } | |
19212 | if (i === 0) { | |
19213 | return buf; | |
19214 | } | |
19215 | return buf.slice(i); | |
19216 | } | |
19217 | ||
19218 | Signature.prototype._importDER = function _importDER(data, enc) { | |
19219 | data = utils.toArray(data, enc); | |
19220 | var p = new Position(); | |
19221 | if (data[p.place++] !== 0x30) { | |
19222 | return false; | |
19223 | } | |
19224 | var len = getLength(data, p); | |
19225 | if ((len + p.place) !== data.length) { | |
19226 | return false; | |
19227 | } | |
19228 | if (data[p.place++] !== 0x02) { | |
19229 | return false; | |
19230 | } | |
19231 | var rlen = getLength(data, p); | |
19232 | var r = data.slice(p.place, rlen + p.place); | |
19233 | p.place += rlen; | |
19234 | if (data[p.place++] !== 0x02) { | |
19235 | return false; | |
19236 | } | |
19237 | var slen = getLength(data, p); | |
19238 | if (data.length !== slen + p.place) { | |
19239 | return false; | |
19240 | } | |
19241 | var s = data.slice(p.place, slen + p.place); | |
19242 | if (r[0] === 0 && (r[1] & 0x80)) { | |
19243 | r = r.slice(1); | |
19244 | } | |
19245 | if (s[0] === 0 && (s[1] & 0x80)) { | |
19246 | s = s.slice(1); | |
19247 | } | |
19248 | ||
19249 | this.r = new BN(r); | |
19250 | this.s = new BN(s); | |
19251 | this.recoveryParam = null; | |
19252 | ||
19253 | return true; | |
19254 | }; | |
19255 | ||
19256 | function constructLength(arr, len) { | |
19257 | if (len < 0x80) { | |
19258 | arr.push(len); | |
19259 | return; | |
19260 | } | |
19261 | var octets = 1 + (Math.log(len) / Math.LN2 >>> 3); | |
19262 | arr.push(octets | 0x80); | |
19263 | while (--octets) { | |
19264 | arr.push((len >>> (octets << 3)) & 0xff); | |
19265 | } | |
19266 | arr.push(len); | |
19267 | } | |
19268 | ||
19269 | Signature.prototype.toDER = function toDER(enc) { | |
19270 | var r = this.r.toArray(); | |
19271 | var s = this.s.toArray(); | |
19272 | ||
19273 | // Pad values | |
19274 | if (r[0] & 0x80) | |
19275 | r = [ 0 ].concat(r); | |
19276 | // Pad values | |
19277 | if (s[0] & 0x80) | |
19278 | s = [ 0 ].concat(s); | |
19279 | ||
19280 | r = rmPadding(r); | |
19281 | s = rmPadding(s); | |
19282 | ||
19283 | while (!s[0] && !(s[1] & 0x80)) { | |
19284 | s = s.slice(1); | |
19285 | } | |
19286 | var arr = [ 0x02 ]; | |
19287 | constructLength(arr, r.length); | |
19288 | arr = arr.concat(r); | |
19289 | arr.push(0x02); | |
19290 | constructLength(arr, s.length); | |
19291 | var backHalf = arr.concat(s); | |
19292 | var res = [ 0x30 ]; | |
19293 | constructLength(res, backHalf.length); | |
19294 | res = res.concat(backHalf); | |
19295 | return utils.encode(res, enc); | |
19296 | }; | |
19297 | ||
19298 | },{"../../elliptic":97,"bn.js":81}],107:[function(require,module,exports){ | |
19299 | 'use strict'; | |
19300 | ||
19301 | var hash = require('hash.js'); | |
19302 | var elliptic = require('../../elliptic'); | |
19303 | var utils = elliptic.utils; | |
19304 | var assert = utils.assert; | |
19305 | var parseBytes = utils.parseBytes; | |
19306 | var KeyPair = require('./key'); | |
19307 | var Signature = require('./signature'); | |
19308 | ||
19309 | function EDDSA(curve) { | |
19310 | assert(curve === 'ed25519', 'only tested with ed25519 so far'); | |
19311 | ||
19312 | if (!(this instanceof EDDSA)) | |
19313 | return new EDDSA(curve); | |
19314 | ||
19315 | var curve = elliptic.curves[curve].curve; | |
19316 | this.curve = curve; | |
19317 | this.g = curve.g; | |
19318 | this.g.precompute(curve.n.bitLength() + 1); | |
19319 | ||
19320 | this.pointClass = curve.point().constructor; | |
19321 | this.encodingLength = Math.ceil(curve.n.bitLength() / 8); | |
19322 | this.hash = hash.sha512; | |
19323 | } | |
19324 | ||
19325 | module.exports = EDDSA; | |
19326 | ||
19327 | /** | |
19328 | * @param {Array|String} message - message bytes | |
19329 | * @param {Array|String|KeyPair} secret - secret bytes or a keypair | |
19330 | * @returns {Signature} - signature | |
19331 | */ | |
19332 | EDDSA.prototype.sign = function sign(message, secret) { | |
19333 | message = parseBytes(message); | |
19334 | var key = this.keyFromSecret(secret); | |
19335 | var r = this.hashInt(key.messagePrefix(), message); | |
19336 | var R = this.g.mul(r); | |
19337 | var Rencoded = this.encodePoint(R); | |
19338 | var s_ = this.hashInt(Rencoded, key.pubBytes(), message) | |
19339 | .mul(key.priv()); | |
19340 | var S = r.add(s_).umod(this.curve.n); | |
19341 | return this.makeSignature({ R: R, S: S, Rencoded: Rencoded }); | |
19342 | }; | |
19343 | ||
19344 | /** | |
19345 | * @param {Array} message - message bytes | |
19346 | * @param {Array|String|Signature} sig - sig bytes | |
19347 | * @param {Array|String|Point|KeyPair} pub - public key | |
19348 | * @returns {Boolean} - true if public key matches sig of message | |
19349 | */ | |
19350 | EDDSA.prototype.verify = function verify(message, sig, pub) { | |
19351 | message = parseBytes(message); | |
19352 | sig = this.makeSignature(sig); | |
19353 | var key = this.keyFromPublic(pub); | |
19354 | var h = this.hashInt(sig.Rencoded(), key.pubBytes(), message); | |
19355 | var SG = this.g.mul(sig.S()); | |
19356 | var RplusAh = sig.R().add(key.pub().mul(h)); | |
19357 | return RplusAh.eq(SG); | |
19358 | }; | |
19359 | ||
19360 | EDDSA.prototype.hashInt = function hashInt() { | |
19361 | var hash = this.hash(); | |
19362 | for (var i = 0; i < arguments.length; i++) | |
19363 | hash.update(arguments[i]); | |
19364 | return utils.intFromLE(hash.digest()).umod(this.curve.n); | |
19365 | }; | |
19366 | ||
19367 | EDDSA.prototype.keyFromPublic = function keyFromPublic(pub) { | |
19368 | return KeyPair.fromPublic(this, pub); | |
19369 | }; | |
19370 | ||
19371 | EDDSA.prototype.keyFromSecret = function keyFromSecret(secret) { | |
19372 | return KeyPair.fromSecret(this, secret); | |
19373 | }; | |
19374 | ||
19375 | EDDSA.prototype.makeSignature = function makeSignature(sig) { | |
19376 | if (sig instanceof Signature) | |
19377 | return sig; | |
19378 | return new Signature(this, sig); | |
19379 | }; | |
19380 | ||
19381 | /** | |
19382 | * * https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-03#section-5.2 | |
19383 | * | |
19384 | * EDDSA defines methods for encoding and decoding points and integers. These are | |
19385 | * helper convenience methods, that pass along to utility functions implied | |
19386 | * parameters. | |
19387 | * | |
19388 | */ | |
19389 | EDDSA.prototype.encodePoint = function encodePoint(point) { | |
19390 | var enc = point.getY().toArray('le', this.encodingLength); | |
19391 | enc[this.encodingLength - 1] |= point.getX().isOdd() ? 0x80 : 0; | |
19392 | return enc; | |
19393 | }; | |
19394 | ||
19395 | EDDSA.prototype.decodePoint = function decodePoint(bytes) { | |
19396 | bytes = utils.parseBytes(bytes); | |
19397 | ||
19398 | var lastIx = bytes.length - 1; | |
19399 | var normed = bytes.slice(0, lastIx).concat(bytes[lastIx] & ~0x80); | |
19400 | var xIsOdd = (bytes[lastIx] & 0x80) !== 0; | |
19401 | ||
19402 | var y = utils.intFromLE(normed); | |
19403 | return this.curve.pointFromY(y, xIsOdd); | |
19404 | }; | |
19405 | ||
19406 | EDDSA.prototype.encodeInt = function encodeInt(num) { | |
19407 | return num.toArray('le', this.encodingLength); | |
19408 | }; | |
19409 | ||
19410 | EDDSA.prototype.decodeInt = function decodeInt(bytes) { | |
19411 | return utils.intFromLE(bytes); | |
19412 | }; | |
19413 | ||
19414 | EDDSA.prototype.isPoint = function isPoint(val) { | |
19415 | return val instanceof this.pointClass; | |
19416 | }; | |
19417 | ||
19418 | },{"../../elliptic":97,"./key":108,"./signature":109,"hash.js":114}],108:[function(require,module,exports){ | |
19419 | 'use strict'; | |
19420 | ||
19421 | var elliptic = require('../../elliptic'); | |
19422 | var utils = elliptic.utils; | |
19423 | var assert = utils.assert; | |
19424 | var parseBytes = utils.parseBytes; | |
19425 | var cachedProperty = utils.cachedProperty; | |
19426 | ||
19427 | /** | |
19428 | * @param {EDDSA} eddsa - instance | |
19429 | * @param {Object} params - public/private key parameters | |
19430 | * | |
19431 | * @param {Array<Byte>} [params.secret] - secret seed bytes | |
19432 | * @param {Point} [params.pub] - public key point (aka `A` in eddsa terms) | |
19433 | * @param {Array<Byte>} [params.pub] - public key point encoded as bytes | |
19434 | * | |
19435 | */ | |
19436 | function KeyPair(eddsa, params) { | |
19437 | this.eddsa = eddsa; | |
19438 | this._secret = parseBytes(params.secret); | |
19439 | if (eddsa.isPoint(params.pub)) | |
19440 | this._pub = params.pub; | |
19441 | else | |
19442 | this._pubBytes = parseBytes(params.pub); | |
19443 | } | |
19444 | ||
19445 | KeyPair.fromPublic = function fromPublic(eddsa, pub) { | |
19446 | if (pub instanceof KeyPair) | |
19447 | return pub; | |
19448 | return new KeyPair(eddsa, { pub: pub }); | |
19449 | }; | |
19450 | ||
19451 | KeyPair.fromSecret = function fromSecret(eddsa, secret) { | |
19452 | if (secret instanceof KeyPair) | |
19453 | return secret; | |
19454 | return new KeyPair(eddsa, { secret: secret }); | |
19455 | }; | |
19456 | ||
19457 | KeyPair.prototype.secret = function secret() { | |
19458 | return this._secret; | |
19459 | }; | |
19460 | ||
19461 | cachedProperty(KeyPair, 'pubBytes', function pubBytes() { | |
19462 | return this.eddsa.encodePoint(this.pub()); | |
19463 | }); | |
19464 | ||
19465 | cachedProperty(KeyPair, 'pub', function pub() { | |
19466 | if (this._pubBytes) | |
19467 | return this.eddsa.decodePoint(this._pubBytes); | |
19468 | return this.eddsa.g.mul(this.priv()); | |
19469 | }); | |
19470 | ||
19471 | cachedProperty(KeyPair, 'privBytes', function privBytes() { | |
19472 | var eddsa = this.eddsa; | |
19473 | var hash = this.hash(); | |
19474 | var lastIx = eddsa.encodingLength - 1; | |
19475 | ||
19476 | var a = hash.slice(0, eddsa.encodingLength); | |
19477 | a[0] &= 248; | |
19478 | a[lastIx] &= 127; | |
19479 | a[lastIx] |= 64; | |
19480 | ||
19481 | return a; | |
19482 | }); | |
19483 | ||
19484 | cachedProperty(KeyPair, 'priv', function priv() { | |
19485 | return this.eddsa.decodeInt(this.privBytes()); | |
19486 | }); | |
19487 | ||
19488 | cachedProperty(KeyPair, 'hash', function hash() { | |
19489 | return this.eddsa.hash().update(this.secret()).digest(); | |
19490 | }); | |
19491 | ||
19492 | cachedProperty(KeyPair, 'messagePrefix', function messagePrefix() { | |
19493 | return this.hash().slice(this.eddsa.encodingLength); | |
19494 | }); | |
19495 | ||
19496 | KeyPair.prototype.sign = function sign(message) { | |
19497 | assert(this._secret, 'KeyPair can only verify'); | |
19498 | return this.eddsa.sign(message, this); | |
19499 | }; | |
19500 | ||
19501 | KeyPair.prototype.verify = function verify(message, sig) { | |
19502 | return this.eddsa.verify(message, sig, this); | |
19503 | }; | |
19504 | ||
19505 | KeyPair.prototype.getSecret = function getSecret(enc) { | |
19506 | assert(this._secret, 'KeyPair is public only'); | |
19507 | return utils.encode(this.secret(), enc); | |
19508 | }; | |
a0091a40 | 19509 | |
9f59e99b IC |
19510 | KeyPair.prototype.getPublic = function getPublic(enc) { |
19511 | return utils.encode(this.pubBytes(), enc); | |
19512 | }; | |
a0091a40 | 19513 | |
9f59e99b | 19514 | module.exports = KeyPair; |
a0091a40 | 19515 | |
9f59e99b IC |
19516 | },{"../../elliptic":97}],109:[function(require,module,exports){ |
19517 | 'use strict'; | |
a0091a40 | 19518 | |
9f59e99b IC |
19519 | var BN = require('bn.js'); |
19520 | var elliptic = require('../../elliptic'); | |
19521 | var utils = elliptic.utils; | |
19522 | var assert = utils.assert; | |
19523 | var cachedProperty = utils.cachedProperty; | |
19524 | var parseBytes = utils.parseBytes; | |
a0091a40 | 19525 | |
9f59e99b IC |
19526 | /** |
19527 | * @param {EDDSA} eddsa - eddsa instance | |
19528 | * @param {Array<Bytes>|Object} sig - | |
19529 | * @param {Array<Bytes>|Point} [sig.R] - R point as Point or bytes | |
19530 | * @param {Array<Bytes>|bn} [sig.S] - S scalar as bn or bytes | |
19531 | * @param {Array<Bytes>} [sig.Rencoded] - R point encoded | |
19532 | * @param {Array<Bytes>} [sig.Sencoded] - S scalar encoded | |
19533 | */ | |
19534 | function Signature(eddsa, sig) { | |
19535 | this.eddsa = eddsa; | |
a0091a40 | 19536 | |
9f59e99b IC |
19537 | if (typeof sig !== 'object') |
19538 | sig = parseBytes(sig); | |
a0091a40 | 19539 | |
9f59e99b IC |
19540 | if (Array.isArray(sig)) { |
19541 | sig = { | |
19542 | R: sig.slice(0, eddsa.encodingLength), | |
19543 | S: sig.slice(eddsa.encodingLength) | |
19544 | }; | |
19545 | } | |
a0091a40 | 19546 | |
9f59e99b | 19547 | assert(sig.R && sig.S, 'Signature without R or S'); |
a0091a40 | 19548 | |
9f59e99b IC |
19549 | if (eddsa.isPoint(sig.R)) |
19550 | this._R = sig.R; | |
19551 | if (sig.S instanceof BN) | |
19552 | this._S = sig.S; | |
a0091a40 | 19553 | |
9f59e99b IC |
19554 | this._Rencoded = Array.isArray(sig.R) ? sig.R : sig.Rencoded; |
19555 | this._Sencoded = Array.isArray(sig.S) ? sig.S : sig.Sencoded; | |
a0091a40 IC |
19556 | } |
19557 | ||
9f59e99b IC |
19558 | cachedProperty(Signature, 'S', function S() { |
19559 | return this.eddsa.decodeInt(this.Sencoded()); | |
19560 | }); | |
a0091a40 | 19561 | |
9f59e99b IC |
19562 | cachedProperty(Signature, 'R', function R() { |
19563 | return this.eddsa.decodePoint(this.Rencoded()); | |
19564 | }); | |
a0091a40 | 19565 | |
9f59e99b IC |
19566 | cachedProperty(Signature, 'Rencoded', function Rencoded() { |
19567 | return this.eddsa.encodePoint(this.R()); | |
19568 | }); | |
a0091a40 | 19569 | |
9f59e99b IC |
19570 | cachedProperty(Signature, 'Sencoded', function Sencoded() { |
19571 | return this.eddsa.encodeInt(this.S()); | |
19572 | }); | |
a0091a40 | 19573 | |
9f59e99b IC |
19574 | Signature.prototype.toBytes = function toBytes() { |
19575 | return this.Rencoded().concat(this.Sencoded()); | |
19576 | }; | |
a0091a40 | 19577 | |
9f59e99b IC |
19578 | Signature.prototype.toHex = function toHex() { |
19579 | return utils.encode(this.toBytes(), 'hex').toUpperCase(); | |
19580 | }; | |
a0091a40 | 19581 | |
9f59e99b IC |
19582 | module.exports = Signature; |
19583 | ||
19584 | },{"../../elliptic":97,"bn.js":81}],110:[function(require,module,exports){ | |
19585 | module.exports = { | |
19586 | doubles: { | |
19587 | step: 4, | |
19588 | points: [ | |
19589 | [ | |
19590 | 'e60fce93b59e9ec53011aabc21c23e97b2a31369b87a5ae9c44ee89e2a6dec0a', | |
19591 | 'f7e3507399e595929db99f34f57937101296891e44d23f0be1f32cce69616821' | |
19592 | ], | |
19593 | [ | |
19594 | '8282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508', | |
19595 | '11f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26caf' | |
19596 | ], | |
19597 | [ | |
19598 | '175e159f728b865a72f99cc6c6fc846de0b93833fd2222ed73fce5b551e5b739', | |
19599 | 'd3506e0d9e3c79eba4ef97a51ff71f5eacb5955add24345c6efa6ffee9fed695' | |
19600 | ], | |
19601 | [ | |
19602 | '363d90d447b00c9c99ceac05b6262ee053441c7e55552ffe526bad8f83ff4640', | |
19603 | '4e273adfc732221953b445397f3363145b9a89008199ecb62003c7f3bee9de9' | |
19604 | ], | |
19605 | [ | |
19606 | '8b4b5f165df3c2be8c6244b5b745638843e4a781a15bcd1b69f79a55dffdf80c', | |
19607 | '4aad0a6f68d308b4b3fbd7813ab0da04f9e336546162ee56b3eff0c65fd4fd36' | |
19608 | ], | |
19609 | [ | |
19610 | '723cbaa6e5db996d6bf771c00bd548c7b700dbffa6c0e77bcb6115925232fcda', | |
19611 | '96e867b5595cc498a921137488824d6e2660a0653779494801dc069d9eb39f5f' | |
19612 | ], | |
19613 | [ | |
19614 | 'eebfa4d493bebf98ba5feec812c2d3b50947961237a919839a533eca0e7dd7fa', | |
19615 | '5d9a8ca3970ef0f269ee7edaf178089d9ae4cdc3a711f712ddfd4fdae1de8999' | |
19616 | ], | |
19617 | [ | |
19618 | '100f44da696e71672791d0a09b7bde459f1215a29b3c03bfefd7835b39a48db0', | |
19619 | 'cdd9e13192a00b772ec8f3300c090666b7ff4a18ff5195ac0fbd5cd62bc65a09' | |
19620 | ], | |
19621 | [ | |
19622 | 'e1031be262c7ed1b1dc9227a4a04c017a77f8d4464f3b3852c8acde6e534fd2d', | |
19623 | '9d7061928940405e6bb6a4176597535af292dd419e1ced79a44f18f29456a00d' | |
19624 | ], | |
19625 | [ | |
19626 | 'feea6cae46d55b530ac2839f143bd7ec5cf8b266a41d6af52d5e688d9094696d', | |
19627 | 'e57c6b6c97dce1bab06e4e12bf3ecd5c981c8957cc41442d3155debf18090088' | |
19628 | ], | |
19629 | [ | |
19630 | 'da67a91d91049cdcb367be4be6ffca3cfeed657d808583de33fa978bc1ec6cb1', | |
19631 | '9bacaa35481642bc41f463f7ec9780e5dec7adc508f740a17e9ea8e27a68be1d' | |
19632 | ], | |
19633 | [ | |
19634 | '53904faa0b334cdda6e000935ef22151ec08d0f7bb11069f57545ccc1a37b7c0', | |
19635 | '5bc087d0bc80106d88c9eccac20d3c1c13999981e14434699dcb096b022771c8' | |
19636 | ], | |
19637 | [ | |
19638 | '8e7bcd0bd35983a7719cca7764ca906779b53a043a9b8bcaeff959f43ad86047', | |
19639 | '10b7770b2a3da4b3940310420ca9514579e88e2e47fd68b3ea10047e8460372a' | |
19640 | ], | |
19641 | [ | |
19642 | '385eed34c1cdff21e6d0818689b81bde71a7f4f18397e6690a841e1599c43862', | |
19643 | '283bebc3e8ea23f56701de19e9ebf4576b304eec2086dc8cc0458fe5542e5453' | |
19644 | ], | |
19645 | [ | |
19646 | '6f9d9b803ecf191637c73a4413dfa180fddf84a5947fbc9c606ed86c3fac3a7', | |
19647 | '7c80c68e603059ba69b8e2a30e45c4d47ea4dd2f5c281002d86890603a842160' | |
19648 | ], | |
19649 | [ | |
19650 | '3322d401243c4e2582a2147c104d6ecbf774d163db0f5e5313b7e0e742d0e6bd', | |
19651 | '56e70797e9664ef5bfb019bc4ddaf9b72805f63ea2873af624f3a2e96c28b2a0' | |
19652 | ], | |
19653 | [ | |
19654 | '85672c7d2de0b7da2bd1770d89665868741b3f9af7643397721d74d28134ab83', | |
19655 | '7c481b9b5b43b2eb6374049bfa62c2e5e77f17fcc5298f44c8e3094f790313a6' | |
19656 | ], | |
19657 | [ | |
19658 | '948bf809b1988a46b06c9f1919413b10f9226c60f668832ffd959af60c82a0a', | |
19659 | '53a562856dcb6646dc6b74c5d1c3418c6d4dff08c97cd2bed4cb7f88d8c8e589' | |
19660 | ], | |
19661 | [ | |
19662 | '6260ce7f461801c34f067ce0f02873a8f1b0e44dfc69752accecd819f38fd8e8', | |
19663 | 'bc2da82b6fa5b571a7f09049776a1ef7ecd292238051c198c1a84e95b2b4ae17' | |
19664 | ], | |
19665 | [ | |
19666 | 'e5037de0afc1d8d43d8348414bbf4103043ec8f575bfdc432953cc8d2037fa2d', | |
19667 | '4571534baa94d3b5f9f98d09fb990bddbd5f5b03ec481f10e0e5dc841d755bda' | |
19668 | ], | |
19669 | [ | |
19670 | 'e06372b0f4a207adf5ea905e8f1771b4e7e8dbd1c6a6c5b725866a0ae4fce725', | |
19671 | '7a908974bce18cfe12a27bb2ad5a488cd7484a7787104870b27034f94eee31dd' | |
19672 | ], | |
19673 | [ | |
19674 | '213c7a715cd5d45358d0bbf9dc0ce02204b10bdde2a3f58540ad6908d0559754', | |
19675 | '4b6dad0b5ae462507013ad06245ba190bb4850f5f36a7eeddff2c27534b458f2' | |
19676 | ], | |
19677 | [ | |
19678 | '4e7c272a7af4b34e8dbb9352a5419a87e2838c70adc62cddf0cc3a3b08fbd53c', | |
19679 | '17749c766c9d0b18e16fd09f6def681b530b9614bff7dd33e0b3941817dcaae6' | |
19680 | ], | |
19681 | [ | |
19682 | 'fea74e3dbe778b1b10f238ad61686aa5c76e3db2be43057632427e2840fb27b6', | |
19683 | '6e0568db9b0b13297cf674deccb6af93126b596b973f7b77701d3db7f23cb96f' | |
19684 | ], | |
19685 | [ | |
19686 | '76e64113f677cf0e10a2570d599968d31544e179b760432952c02a4417bdde39', | |
19687 | 'c90ddf8dee4e95cf577066d70681f0d35e2a33d2b56d2032b4b1752d1901ac01' | |
19688 | ], | |
19689 | [ | |
19690 | 'c738c56b03b2abe1e8281baa743f8f9a8f7cc643df26cbee3ab150242bcbb891', | |
19691 | '893fb578951ad2537f718f2eacbfbbbb82314eef7880cfe917e735d9699a84c3' | |
19692 | ], | |
19693 | [ | |
19694 | 'd895626548b65b81e264c7637c972877d1d72e5f3a925014372e9f6588f6c14b', | |
19695 | 'febfaa38f2bc7eae728ec60818c340eb03428d632bb067e179363ed75d7d991f' | |
19696 | ], | |
19697 | [ | |
19698 | 'b8da94032a957518eb0f6433571e8761ceffc73693e84edd49150a564f676e03', | |
19699 | '2804dfa44805a1e4d7c99cc9762808b092cc584d95ff3b511488e4e74efdf6e7' | |
19700 | ], | |
19701 | [ | |
19702 | 'e80fea14441fb33a7d8adab9475d7fab2019effb5156a792f1a11778e3c0df5d', | |
19703 | 'eed1de7f638e00771e89768ca3ca94472d155e80af322ea9fcb4291b6ac9ec78' | |
19704 | ], | |
19705 | [ | |
19706 | 'a301697bdfcd704313ba48e51d567543f2a182031efd6915ddc07bbcc4e16070', | |
19707 | '7370f91cfb67e4f5081809fa25d40f9b1735dbf7c0a11a130c0d1a041e177ea1' | |
19708 | ], | |
19709 | [ | |
19710 | '90ad85b389d6b936463f9d0512678de208cc330b11307fffab7ac63e3fb04ed4', | |
19711 | 'e507a3620a38261affdcbd9427222b839aefabe1582894d991d4d48cb6ef150' | |
19712 | ], | |
19713 | [ | |
19714 | '8f68b9d2f63b5f339239c1ad981f162ee88c5678723ea3351b7b444c9ec4c0da', | |
19715 | '662a9f2dba063986de1d90c2b6be215dbbea2cfe95510bfdf23cbf79501fff82' | |
19716 | ], | |
19717 | [ | |
19718 | 'e4f3fb0176af85d65ff99ff9198c36091f48e86503681e3e6686fd5053231e11', | |
19719 | '1e63633ad0ef4f1c1661a6d0ea02b7286cc7e74ec951d1c9822c38576feb73bc' | |
19720 | ], | |
19721 | [ | |
19722 | '8c00fa9b18ebf331eb961537a45a4266c7034f2f0d4e1d0716fb6eae20eae29e', | |
19723 | 'efa47267fea521a1a9dc343a3736c974c2fadafa81e36c54e7d2a4c66702414b' | |
19724 | ], | |
19725 | [ | |
19726 | 'e7a26ce69dd4829f3e10cec0a9e98ed3143d084f308b92c0997fddfc60cb3e41', | |
19727 | '2a758e300fa7984b471b006a1aafbb18d0a6b2c0420e83e20e8a9421cf2cfd51' | |
19728 | ], | |
19729 | [ | |
19730 | 'b6459e0ee3662ec8d23540c223bcbdc571cbcb967d79424f3cf29eb3de6b80ef', | |
19731 | '67c876d06f3e06de1dadf16e5661db3c4b3ae6d48e35b2ff30bf0b61a71ba45' | |
19732 | ], | |
19733 | [ | |
19734 | 'd68a80c8280bb840793234aa118f06231d6f1fc67e73c5a5deda0f5b496943e8', | |
19735 | 'db8ba9fff4b586d00c4b1f9177b0e28b5b0e7b8f7845295a294c84266b133120' | |
19736 | ], | |
19737 | [ | |
19738 | '324aed7df65c804252dc0270907a30b09612aeb973449cea4095980fc28d3d5d', | |
19739 | '648a365774b61f2ff130c0c35aec1f4f19213b0c7e332843967224af96ab7c84' | |
19740 | ], | |
19741 | [ | |
19742 | '4df9c14919cde61f6d51dfdbe5fee5dceec4143ba8d1ca888e8bd373fd054c96', | |
19743 | '35ec51092d8728050974c23a1d85d4b5d506cdc288490192ebac06cad10d5d' | |
19744 | ], | |
19745 | [ | |
19746 | '9c3919a84a474870faed8a9c1cc66021523489054d7f0308cbfc99c8ac1f98cd', | |
19747 | 'ddb84f0f4a4ddd57584f044bf260e641905326f76c64c8e6be7e5e03d4fc599d' | |
19748 | ], | |
19749 | [ | |
19750 | '6057170b1dd12fdf8de05f281d8e06bb91e1493a8b91d4cc5a21382120a959e5', | |
19751 | '9a1af0b26a6a4807add9a2daf71df262465152bc3ee24c65e899be932385a2a8' | |
19752 | ], | |
19753 | [ | |
19754 | 'a576df8e23a08411421439a4518da31880cef0fba7d4df12b1a6973eecb94266', | |
19755 | '40a6bf20e76640b2c92b97afe58cd82c432e10a7f514d9f3ee8be11ae1b28ec8' | |
19756 | ], | |
19757 | [ | |
19758 | '7778a78c28dec3e30a05fe9629de8c38bb30d1f5cf9a3a208f763889be58ad71', | |
19759 | '34626d9ab5a5b22ff7098e12f2ff580087b38411ff24ac563b513fc1fd9f43ac' | |
19760 | ], | |
19761 | [ | |
19762 | '928955ee637a84463729fd30e7afd2ed5f96274e5ad7e5cb09eda9c06d903ac', | |
19763 | 'c25621003d3f42a827b78a13093a95eeac3d26efa8a8d83fc5180e935bcd091f' | |
19764 | ], | |
19765 | [ | |
19766 | '85d0fef3ec6db109399064f3a0e3b2855645b4a907ad354527aae75163d82751', | |
19767 | '1f03648413a38c0be29d496e582cf5663e8751e96877331582c237a24eb1f962' | |
19768 | ], | |
19769 | [ | |
19770 | 'ff2b0dce97eece97c1c9b6041798b85dfdfb6d8882da20308f5404824526087e', | |
19771 | '493d13fef524ba188af4c4dc54d07936c7b7ed6fb90e2ceb2c951e01f0c29907' | |
19772 | ], | |
19773 | [ | |
19774 | '827fbbe4b1e880ea9ed2b2e6301b212b57f1ee148cd6dd28780e5e2cf856e241', | |
19775 | 'c60f9c923c727b0b71bef2c67d1d12687ff7a63186903166d605b68baec293ec' | |
19776 | ], | |
19777 | [ | |
19778 | 'eaa649f21f51bdbae7be4ae34ce6e5217a58fdce7f47f9aa7f3b58fa2120e2b3', | |
19779 | 'be3279ed5bbbb03ac69a80f89879aa5a01a6b965f13f7e59d47a5305ba5ad93d' | |
19780 | ], | |
19781 | [ | |
19782 | 'e4a42d43c5cf169d9391df6decf42ee541b6d8f0c9a137401e23632dda34d24f', | |
19783 | '4d9f92e716d1c73526fc99ccfb8ad34ce886eedfa8d8e4f13a7f7131deba9414' | |
19784 | ], | |
19785 | [ | |
19786 | '1ec80fef360cbdd954160fadab352b6b92b53576a88fea4947173b9d4300bf19', | |
19787 | 'aeefe93756b5340d2f3a4958a7abbf5e0146e77f6295a07b671cdc1cc107cefd' | |
19788 | ], | |
19789 | [ | |
19790 | '146a778c04670c2f91b00af4680dfa8bce3490717d58ba889ddb5928366642be', | |
19791 | 'b318e0ec3354028add669827f9d4b2870aaa971d2f7e5ed1d0b297483d83efd0' | |
19792 | ], | |
19793 | [ | |
19794 | 'fa50c0f61d22e5f07e3acebb1aa07b128d0012209a28b9776d76a8793180eef9', | |
19795 | '6b84c6922397eba9b72cd2872281a68a5e683293a57a213b38cd8d7d3f4f2811' | |
19796 | ], | |
19797 | [ | |
19798 | 'da1d61d0ca721a11b1a5bf6b7d88e8421a288ab5d5bba5220e53d32b5f067ec2', | |
19799 | '8157f55a7c99306c79c0766161c91e2966a73899d279b48a655fba0f1ad836f1' | |
19800 | ], | |
19801 | [ | |
19802 | 'a8e282ff0c9706907215ff98e8fd416615311de0446f1e062a73b0610d064e13', | |
19803 | '7f97355b8db81c09abfb7f3c5b2515888b679a3e50dd6bd6cef7c73111f4cc0c' | |
19804 | ], | |
19805 | [ | |
19806 | '174a53b9c9a285872d39e56e6913cab15d59b1fa512508c022f382de8319497c', | |
19807 | 'ccc9dc37abfc9c1657b4155f2c47f9e6646b3a1d8cb9854383da13ac079afa73' | |
19808 | ], | |
19809 | [ | |
19810 | '959396981943785c3d3e57edf5018cdbe039e730e4918b3d884fdff09475b7ba', | |
19811 | '2e7e552888c331dd8ba0386a4b9cd6849c653f64c8709385e9b8abf87524f2fd' | |
19812 | ], | |
19813 | [ | |
19814 | 'd2a63a50ae401e56d645a1153b109a8fcca0a43d561fba2dbb51340c9d82b151', | |
19815 | 'e82d86fb6443fcb7565aee58b2948220a70f750af484ca52d4142174dcf89405' | |
19816 | ], | |
19817 | [ | |
19818 | '64587e2335471eb890ee7896d7cfdc866bacbdbd3839317b3436f9b45617e073', | |
19819 | 'd99fcdd5bf6902e2ae96dd6447c299a185b90a39133aeab358299e5e9faf6589' | |
19820 | ], | |
19821 | [ | |
19822 | '8481bde0e4e4d885b3a546d3e549de042f0aa6cea250e7fd358d6c86dd45e458', | |
19823 | '38ee7b8cba5404dd84a25bf39cecb2ca900a79c42b262e556d64b1b59779057e' | |
19824 | ], | |
19825 | [ | |
19826 | '13464a57a78102aa62b6979ae817f4637ffcfed3c4b1ce30bcd6303f6caf666b', | |
19827 | '69be159004614580ef7e433453ccb0ca48f300a81d0942e13f495a907f6ecc27' | |
19828 | ], | |
19829 | [ | |
19830 | 'bc4a9df5b713fe2e9aef430bcc1dc97a0cd9ccede2f28588cada3a0d2d83f366', | |
19831 | 'd3a81ca6e785c06383937adf4b798caa6e8a9fbfa547b16d758d666581f33c1' | |
19832 | ], | |
19833 | [ | |
19834 | '8c28a97bf8298bc0d23d8c749452a32e694b65e30a9472a3954ab30fe5324caa', | |
19835 | '40a30463a3305193378fedf31f7cc0eb7ae784f0451cb9459e71dc73cbef9482' | |
19836 | ], | |
19837 | [ | |
19838 | '8ea9666139527a8c1dd94ce4f071fd23c8b350c5a4bb33748c4ba111faccae0', | |
19839 | '620efabbc8ee2782e24e7c0cfb95c5d735b783be9cf0f8e955af34a30e62b945' | |
19840 | ], | |
19841 | [ | |
19842 | 'dd3625faef5ba06074669716bbd3788d89bdde815959968092f76cc4eb9a9787', | |
19843 | '7a188fa3520e30d461da2501045731ca941461982883395937f68d00c644a573' | |
19844 | ], | |
19845 | [ | |
19846 | 'f710d79d9eb962297e4f6232b40e8f7feb2bc63814614d692c12de752408221e', | |
19847 | 'ea98e67232d3b3295d3b535532115ccac8612c721851617526ae47a9c77bfc82' | |
19848 | ] | |
19849 | ] | |
19850 | }, | |
19851 | naf: { | |
19852 | wnd: 7, | |
19853 | points: [ | |
19854 | [ | |
19855 | 'f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9', | |
19856 | '388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672' | |
19857 | ], | |
19858 | [ | |
19859 | '2f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4', | |
19860 | 'd8ac222636e5e3d6d4dba9dda6c9c426f788271bab0d6840dca87d3aa6ac62d6' | |
19861 | ], | |
19862 | [ | |
19863 | '5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc', | |
19864 | '6aebca40ba255960a3178d6d861a54dba813d0b813fde7b5a5082628087264da' | |
19865 | ], | |
19866 | [ | |
19867 | 'acd484e2f0c7f65309ad178a9f559abde09796974c57e714c35f110dfc27ccbe', | |
19868 | 'cc338921b0a7d9fd64380971763b61e9add888a4375f8e0f05cc262ac64f9c37' | |
19869 | ], | |
19870 | [ | |
19871 | '774ae7f858a9411e5ef4246b70c65aac5649980be5c17891bbec17895da008cb', | |
19872 | 'd984a032eb6b5e190243dd56d7b7b365372db1e2dff9d6a8301d74c9c953c61b' | |
19873 | ], | |
19874 | [ | |
19875 | 'f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8', | |
19876 | 'ab0902e8d880a89758212eb65cdaf473a1a06da521fa91f29b5cb52db03ed81' | |
19877 | ], | |
19878 | [ | |
19879 | 'd7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e', | |
19880 | '581e2872a86c72a683842ec228cc6defea40af2bd896d3a5c504dc9ff6a26b58' | |
19881 | ], | |
19882 | [ | |
19883 | 'defdea4cdb677750a420fee807eacf21eb9898ae79b9768766e4faa04a2d4a34', | |
19884 | '4211ab0694635168e997b0ead2a93daeced1f4a04a95c0f6cfb199f69e56eb77' | |
19885 | ], | |
19886 | [ | |
19887 | '2b4ea0a797a443d293ef5cff444f4979f06acfebd7e86d277475656138385b6c', | |
19888 | '85e89bc037945d93b343083b5a1c86131a01f60c50269763b570c854e5c09b7a' | |
19889 | ], | |
19890 | [ | |
19891 | '352bbf4a4cdd12564f93fa332ce333301d9ad40271f8107181340aef25be59d5', | |
19892 | '321eb4075348f534d59c18259dda3e1f4a1b3b2e71b1039c67bd3d8bcf81998c' | |
19893 | ], | |
19894 | [ | |
19895 | '2fa2104d6b38d11b0230010559879124e42ab8dfeff5ff29dc9cdadd4ecacc3f', | |
19896 | '2de1068295dd865b64569335bd5dd80181d70ecfc882648423ba76b532b7d67' | |
19897 | ], | |
19898 | [ | |
19899 | '9248279b09b4d68dab21a9b066edda83263c3d84e09572e269ca0cd7f5453714', | |
19900 | '73016f7bf234aade5d1aa71bdea2b1ff3fc0de2a887912ffe54a32ce97cb3402' | |
19901 | ], | |
19902 | [ | |
19903 | 'daed4f2be3a8bf278e70132fb0beb7522f570e144bf615c07e996d443dee8729', | |
19904 | 'a69dce4a7d6c98e8d4a1aca87ef8d7003f83c230f3afa726ab40e52290be1c55' | |
19905 | ], | |
19906 | [ | |
19907 | 'c44d12c7065d812e8acf28d7cbb19f9011ecd9e9fdf281b0e6a3b5e87d22e7db', | |
19908 | '2119a460ce326cdc76c45926c982fdac0e106e861edf61c5a039063f0e0e6482' | |
19909 | ], | |
19910 | [ | |
19911 | '6a245bf6dc698504c89a20cfded60853152b695336c28063b61c65cbd269e6b4', | |
19912 | 'e022cf42c2bd4a708b3f5126f16a24ad8b33ba48d0423b6efd5e6348100d8a82' | |
19913 | ], | |
19914 | [ | |
19915 | '1697ffa6fd9de627c077e3d2fe541084ce13300b0bec1146f95ae57f0d0bd6a5', | |
19916 | 'b9c398f186806f5d27561506e4557433a2cf15009e498ae7adee9d63d01b2396' | |
19917 | ], | |
19918 | [ | |
19919 | '605bdb019981718b986d0f07e834cb0d9deb8360ffb7f61df982345ef27a7479', | |
19920 | '2972d2de4f8d20681a78d93ec96fe23c26bfae84fb14db43b01e1e9056b8c49' | |
19921 | ], | |
19922 | [ | |
19923 | '62d14dab4150bf497402fdc45a215e10dcb01c354959b10cfe31c7e9d87ff33d', | |
19924 | '80fc06bd8cc5b01098088a1950eed0db01aa132967ab472235f5642483b25eaf' | |
19925 | ], | |
19926 | [ | |
19927 | '80c60ad0040f27dade5b4b06c408e56b2c50e9f56b9b8b425e555c2f86308b6f', | |
19928 | '1c38303f1cc5c30f26e66bad7fe72f70a65eed4cbe7024eb1aa01f56430bd57a' | |
19929 | ], | |
19930 | [ | |
19931 | '7a9375ad6167ad54aa74c6348cc54d344cc5dc9487d847049d5eabb0fa03c8fb', | |
19932 | 'd0e3fa9eca8726909559e0d79269046bdc59ea10c70ce2b02d499ec224dc7f7' | |
19933 | ], | |
19934 | [ | |
19935 | 'd528ecd9b696b54c907a9ed045447a79bb408ec39b68df504bb51f459bc3ffc9', | |
19936 | 'eecf41253136e5f99966f21881fd656ebc4345405c520dbc063465b521409933' | |
19937 | ], | |
19938 | [ | |
19939 | '49370a4b5f43412ea25f514e8ecdad05266115e4a7ecb1387231808f8b45963', | |
19940 | '758f3f41afd6ed428b3081b0512fd62a54c3f3afbb5b6764b653052a12949c9a' | |
19941 | ], | |
19942 | [ | |
19943 | '77f230936ee88cbbd73df930d64702ef881d811e0e1498e2f1c13eb1fc345d74', | |
19944 | '958ef42a7886b6400a08266e9ba1b37896c95330d97077cbbe8eb3c7671c60d6' | |
19945 | ], | |
19946 | [ | |
19947 | 'f2dac991cc4ce4b9ea44887e5c7c0bce58c80074ab9d4dbaeb28531b7739f530', | |
19948 | 'e0dedc9b3b2f8dad4da1f32dec2531df9eb5fbeb0598e4fd1a117dba703a3c37' | |
19949 | ], | |
19950 | [ | |
19951 | '463b3d9f662621fb1b4be8fbbe2520125a216cdfc9dae3debcba4850c690d45b', | |
19952 | '5ed430d78c296c3543114306dd8622d7c622e27c970a1de31cb377b01af7307e' | |
19953 | ], | |
19954 | [ | |
19955 | 'f16f804244e46e2a09232d4aff3b59976b98fac14328a2d1a32496b49998f247', | |
19956 | 'cedabd9b82203f7e13d206fcdf4e33d92a6c53c26e5cce26d6579962c4e31df6' | |
19957 | ], | |
19958 | [ | |
19959 | 'caf754272dc84563b0352b7a14311af55d245315ace27c65369e15f7151d41d1', | |
19960 | 'cb474660ef35f5f2a41b643fa5e460575f4fa9b7962232a5c32f908318a04476' | |
19961 | ], | |
19962 | [ | |
19963 | '2600ca4b282cb986f85d0f1709979d8b44a09c07cb86d7c124497bc86f082120', | |
19964 | '4119b88753c15bd6a693b03fcddbb45d5ac6be74ab5f0ef44b0be9475a7e4b40' | |
19965 | ], | |
19966 | [ | |
19967 | '7635ca72d7e8432c338ec53cd12220bc01c48685e24f7dc8c602a7746998e435', | |
19968 | '91b649609489d613d1d5e590f78e6d74ecfc061d57048bad9e76f302c5b9c61' | |
19969 | ], | |
19970 | [ | |
19971 | '754e3239f325570cdbbf4a87deee8a66b7f2b33479d468fbc1a50743bf56cc18', | |
19972 | '673fb86e5bda30fb3cd0ed304ea49a023ee33d0197a695d0c5d98093c536683' | |
19973 | ], | |
19974 | [ | |
19975 | 'e3e6bd1071a1e96aff57859c82d570f0330800661d1c952f9fe2694691d9b9e8', | |
19976 | '59c9e0bba394e76f40c0aa58379a3cb6a5a2283993e90c4167002af4920e37f5' | |
19977 | ], | |
19978 | [ | |
19979 | '186b483d056a033826ae73d88f732985c4ccb1f32ba35f4b4cc47fdcf04aa6eb', | |
19980 | '3b952d32c67cf77e2e17446e204180ab21fb8090895138b4a4a797f86e80888b' | |
19981 | ], | |
19982 | [ | |
19983 | 'df9d70a6b9876ce544c98561f4be4f725442e6d2b737d9c91a8321724ce0963f', | |
19984 | '55eb2dafd84d6ccd5f862b785dc39d4ab157222720ef9da217b8c45cf2ba2417' | |
19985 | ], | |
19986 | [ | |
19987 | '5edd5cc23c51e87a497ca815d5dce0f8ab52554f849ed8995de64c5f34ce7143', | |
19988 | 'efae9c8dbc14130661e8cec030c89ad0c13c66c0d17a2905cdc706ab7399a868' | |
19989 | ], | |
19990 | [ | |
19991 | '290798c2b6476830da12fe02287e9e777aa3fba1c355b17a722d362f84614fba', | |
19992 | 'e38da76dcd440621988d00bcf79af25d5b29c094db2a23146d003afd41943e7a' | |
19993 | ], | |
19994 | [ | |
19995 | 'af3c423a95d9f5b3054754efa150ac39cd29552fe360257362dfdecef4053b45', | |
19996 | 'f98a3fd831eb2b749a93b0e6f35cfb40c8cd5aa667a15581bc2feded498fd9c6' | |
19997 | ], | |
19998 | [ | |
19999 | '766dbb24d134e745cccaa28c99bf274906bb66b26dcf98df8d2fed50d884249a', | |
20000 | '744b1152eacbe5e38dcc887980da38b897584a65fa06cedd2c924f97cbac5996' | |
20001 | ], | |
20002 | [ | |
20003 | '59dbf46f8c94759ba21277c33784f41645f7b44f6c596a58ce92e666191abe3e', | |
20004 | 'c534ad44175fbc300f4ea6ce648309a042ce739a7919798cd85e216c4a307f6e' | |
20005 | ], | |
20006 | [ | |
20007 | 'f13ada95103c4537305e691e74e9a4a8dd647e711a95e73cb62dc6018cfd87b8', | |
20008 | 'e13817b44ee14de663bf4bc808341f326949e21a6a75c2570778419bdaf5733d' | |
20009 | ], | |
20010 | [ | |
20011 | '7754b4fa0e8aced06d4167a2c59cca4cda1869c06ebadfb6488550015a88522c', | |
20012 | '30e93e864e669d82224b967c3020b8fa8d1e4e350b6cbcc537a48b57841163a2' | |
20013 | ], | |
20014 | [ | |
20015 | '948dcadf5990e048aa3874d46abef9d701858f95de8041d2a6828c99e2262519', | |
20016 | 'e491a42537f6e597d5d28a3224b1bc25df9154efbd2ef1d2cbba2cae5347d57e' | |
20017 | ], | |
20018 | [ | |
20019 | '7962414450c76c1689c7b48f8202ec37fb224cf5ac0bfa1570328a8a3d7c77ab', | |
20020 | '100b610ec4ffb4760d5c1fc133ef6f6b12507a051f04ac5760afa5b29db83437' | |
20021 | ], | |
20022 | [ | |
20023 | '3514087834964b54b15b160644d915485a16977225b8847bb0dd085137ec47ca', | |
20024 | 'ef0afbb2056205448e1652c48e8127fc6039e77c15c2378b7e7d15a0de293311' | |
20025 | ], | |
20026 | [ | |
20027 | 'd3cc30ad6b483e4bc79ce2c9dd8bc54993e947eb8df787b442943d3f7b527eaf', | |
20028 | '8b378a22d827278d89c5e9be8f9508ae3c2ad46290358630afb34db04eede0a4' | |
20029 | ], | |
20030 | [ | |
20031 | '1624d84780732860ce1c78fcbfefe08b2b29823db913f6493975ba0ff4847610', | |
20032 | '68651cf9b6da903e0914448c6cd9d4ca896878f5282be4c8cc06e2a404078575' | |
20033 | ], | |
20034 | [ | |
20035 | '733ce80da955a8a26902c95633e62a985192474b5af207da6df7b4fd5fc61cd4', | |
20036 | 'f5435a2bd2badf7d485a4d8b8db9fcce3e1ef8e0201e4578c54673bc1dc5ea1d' | |
20037 | ], | |
20038 | [ | |
20039 | '15d9441254945064cf1a1c33bbd3b49f8966c5092171e699ef258dfab81c045c', | |
20040 | 'd56eb30b69463e7234f5137b73b84177434800bacebfc685fc37bbe9efe4070d' | |
20041 | ], | |
20042 | [ | |
20043 | 'a1d0fcf2ec9de675b612136e5ce70d271c21417c9d2b8aaaac138599d0717940', | |
20044 | 'edd77f50bcb5a3cab2e90737309667f2641462a54070f3d519212d39c197a629' | |
20045 | ], | |
20046 | [ | |
20047 | 'e22fbe15c0af8ccc5780c0735f84dbe9a790badee8245c06c7ca37331cb36980', | |
20048 | 'a855babad5cd60c88b430a69f53a1a7a38289154964799be43d06d77d31da06' | |
20049 | ], | |
20050 | [ | |
20051 | '311091dd9860e8e20ee13473c1155f5f69635e394704eaa74009452246cfa9b3', | |
20052 | '66db656f87d1f04fffd1f04788c06830871ec5a64feee685bd80f0b1286d8374' | |
20053 | ], | |
20054 | [ | |
20055 | '34c1fd04d301be89b31c0442d3e6ac24883928b45a9340781867d4232ec2dbdf', | |
20056 | '9414685e97b1b5954bd46f730174136d57f1ceeb487443dc5321857ba73abee' | |
20057 | ], | |
20058 | [ | |
20059 | 'f219ea5d6b54701c1c14de5b557eb42a8d13f3abbcd08affcc2a5e6b049b8d63', | |
20060 | '4cb95957e83d40b0f73af4544cccf6b1f4b08d3c07b27fb8d8c2962a400766d1' | |
20061 | ], | |
20062 | [ | |
20063 | 'd7b8740f74a8fbaab1f683db8f45de26543a5490bca627087236912469a0b448', | |
20064 | 'fa77968128d9c92ee1010f337ad4717eff15db5ed3c049b3411e0315eaa4593b' | |
20065 | ], | |
20066 | [ | |
20067 | '32d31c222f8f6f0ef86f7c98d3a3335ead5bcd32abdd94289fe4d3091aa824bf', | |
20068 | '5f3032f5892156e39ccd3d7915b9e1da2e6dac9e6f26e961118d14b8462e1661' | |
20069 | ], | |
20070 | [ | |
20071 | '7461f371914ab32671045a155d9831ea8793d77cd59592c4340f86cbc18347b5', | |
20072 | '8ec0ba238b96bec0cbdddcae0aa442542eee1ff50c986ea6b39847b3cc092ff6' | |
20073 | ], | |
20074 | [ | |
20075 | 'ee079adb1df1860074356a25aa38206a6d716b2c3e67453d287698bad7b2b2d6', | |
20076 | '8dc2412aafe3be5c4c5f37e0ecc5f9f6a446989af04c4e25ebaac479ec1c8c1e' | |
20077 | ], | |
20078 | [ | |
20079 | '16ec93e447ec83f0467b18302ee620f7e65de331874c9dc72bfd8616ba9da6b5', | |
20080 | '5e4631150e62fb40d0e8c2a7ca5804a39d58186a50e497139626778e25b0674d' | |
20081 | ], | |
20082 | [ | |
20083 | 'eaa5f980c245f6f038978290afa70b6bd8855897f98b6aa485b96065d537bd99', | |
20084 | 'f65f5d3e292c2e0819a528391c994624d784869d7e6ea67fb18041024edc07dc' | |
20085 | ], | |
20086 | [ | |
20087 | '78c9407544ac132692ee1910a02439958ae04877151342ea96c4b6b35a49f51', | |
20088 | 'f3e0319169eb9b85d5404795539a5e68fa1fbd583c064d2462b675f194a3ddb4' | |
20089 | ], | |
20090 | [ | |
20091 | '494f4be219a1a77016dcd838431aea0001cdc8ae7a6fc688726578d9702857a5', | |
20092 | '42242a969283a5f339ba7f075e36ba2af925ce30d767ed6e55f4b031880d562c' | |
20093 | ], | |
20094 | [ | |
20095 | 'a598a8030da6d86c6bc7f2f5144ea549d28211ea58faa70ebf4c1e665c1fe9b5', | |
20096 | '204b5d6f84822c307e4b4a7140737aec23fc63b65b35f86a10026dbd2d864e6b' | |
20097 | ], | |
20098 | [ | |
20099 | 'c41916365abb2b5d09192f5f2dbeafec208f020f12570a184dbadc3e58595997', | |
20100 | '4f14351d0087efa49d245b328984989d5caf9450f34bfc0ed16e96b58fa9913' | |
20101 | ], | |
20102 | [ | |
20103 | '841d6063a586fa475a724604da03bc5b92a2e0d2e0a36acfe4c73a5514742881', | |
20104 | '73867f59c0659e81904f9a1c7543698e62562d6744c169ce7a36de01a8d6154' | |
20105 | ], | |
20106 | [ | |
20107 | '5e95bb399a6971d376026947f89bde2f282b33810928be4ded112ac4d70e20d5', | |
20108 | '39f23f366809085beebfc71181313775a99c9aed7d8ba38b161384c746012865' | |
20109 | ], | |
20110 | [ | |
20111 | '36e4641a53948fd476c39f8a99fd974e5ec07564b5315d8bf99471bca0ef2f66', | |
20112 | 'd2424b1b1abe4eb8164227b085c9aa9456ea13493fd563e06fd51cf5694c78fc' | |
20113 | ], | |
20114 | [ | |
20115 | '336581ea7bfbbb290c191a2f507a41cf5643842170e914faeab27c2c579f726', | |
20116 | 'ead12168595fe1be99252129b6e56b3391f7ab1410cd1e0ef3dcdcabd2fda224' | |
20117 | ], | |
20118 | [ | |
20119 | '8ab89816dadfd6b6a1f2634fcf00ec8403781025ed6890c4849742706bd43ede', | |
20120 | '6fdcef09f2f6d0a044e654aef624136f503d459c3e89845858a47a9129cdd24e' | |
20121 | ], | |
20122 | [ | |
20123 | '1e33f1a746c9c5778133344d9299fcaa20b0938e8acff2544bb40284b8c5fb94', | |
20124 | '60660257dd11b3aa9c8ed618d24edff2306d320f1d03010e33a7d2057f3b3b6' | |
20125 | ], | |
20126 | [ | |
20127 | '85b7c1dcb3cec1b7ee7f30ded79dd20a0ed1f4cc18cbcfcfa410361fd8f08f31', | |
20128 | '3d98a9cdd026dd43f39048f25a8847f4fcafad1895d7a633c6fed3c35e999511' | |
20129 | ], | |
20130 | [ | |
20131 | '29df9fbd8d9e46509275f4b125d6d45d7fbe9a3b878a7af872a2800661ac5f51', | |
20132 | 'b4c4fe99c775a606e2d8862179139ffda61dc861c019e55cd2876eb2a27d84b' | |
20133 | ], | |
20134 | [ | |
20135 | 'a0b1cae06b0a847a3fea6e671aaf8adfdfe58ca2f768105c8082b2e449fce252', | |
20136 | 'ae434102edde0958ec4b19d917a6a28e6b72da1834aff0e650f049503a296cf2' | |
20137 | ], | |
20138 | [ | |
20139 | '4e8ceafb9b3e9a136dc7ff67e840295b499dfb3b2133e4ba113f2e4c0e121e5', | |
20140 | 'cf2174118c8b6d7a4b48f6d534ce5c79422c086a63460502b827ce62a326683c' | |
20141 | ], | |
20142 | [ | |
20143 | 'd24a44e047e19b6f5afb81c7ca2f69080a5076689a010919f42725c2b789a33b', | |
20144 | '6fb8d5591b466f8fc63db50f1c0f1c69013f996887b8244d2cdec417afea8fa3' | |
20145 | ], | |
20146 | [ | |
20147 | 'ea01606a7a6c9cdd249fdfcfacb99584001edd28abbab77b5104e98e8e3b35d4', | |
20148 | '322af4908c7312b0cfbfe369f7a7b3cdb7d4494bc2823700cfd652188a3ea98d' | |
20149 | ], | |
20150 | [ | |
20151 | 'af8addbf2b661c8a6c6328655eb96651252007d8c5ea31be4ad196de8ce2131f', | |
20152 | '6749e67c029b85f52a034eafd096836b2520818680e26ac8f3dfbcdb71749700' | |
20153 | ], | |
20154 | [ | |
20155 | 'e3ae1974566ca06cc516d47e0fb165a674a3dabcfca15e722f0e3450f45889', | |
20156 | '2aeabe7e4531510116217f07bf4d07300de97e4874f81f533420a72eeb0bd6a4' | |
20157 | ], | |
20158 | [ | |
20159 | '591ee355313d99721cf6993ffed1e3e301993ff3ed258802075ea8ced397e246', | |
20160 | 'b0ea558a113c30bea60fc4775460c7901ff0b053d25ca2bdeee98f1a4be5d196' | |
20161 | ], | |
20162 | [ | |
20163 | '11396d55fda54c49f19aa97318d8da61fa8584e47b084945077cf03255b52984', | |
20164 | '998c74a8cd45ac01289d5833a7beb4744ff536b01b257be4c5767bea93ea57a4' | |
20165 | ], | |
20166 | [ | |
20167 | '3c5d2a1ba39c5a1790000738c9e0c40b8dcdfd5468754b6405540157e017aa7a', | |
20168 | 'b2284279995a34e2f9d4de7396fc18b80f9b8b9fdd270f6661f79ca4c81bd257' | |
20169 | ], | |
20170 | [ | |
20171 | 'cc8704b8a60a0defa3a99a7299f2e9c3fbc395afb04ac078425ef8a1793cc030', | |
20172 | 'bdd46039feed17881d1e0862db347f8cf395b74fc4bcdc4e940b74e3ac1f1b13' | |
20173 | ], | |
20174 | [ | |
20175 | 'c533e4f7ea8555aacd9777ac5cad29b97dd4defccc53ee7ea204119b2889b197', | |
20176 | '6f0a256bc5efdf429a2fb6242f1a43a2d9b925bb4a4b3a26bb8e0f45eb596096' | |
20177 | ], | |
20178 | [ | |
20179 | 'c14f8f2ccb27d6f109f6d08d03cc96a69ba8c34eec07bbcf566d48e33da6593', | |
20180 | 'c359d6923bb398f7fd4473e16fe1c28475b740dd098075e6c0e8649113dc3a38' | |
20181 | ], | |
20182 | [ | |
20183 | 'a6cbc3046bc6a450bac24789fa17115a4c9739ed75f8f21ce441f72e0b90e6ef', | |
20184 | '21ae7f4680e889bb130619e2c0f95a360ceb573c70603139862afd617fa9b9f' | |
20185 | ], | |
20186 | [ | |
20187 | '347d6d9a02c48927ebfb86c1359b1caf130a3c0267d11ce6344b39f99d43cc38', | |
20188 | '60ea7f61a353524d1c987f6ecec92f086d565ab687870cb12689ff1e31c74448' | |
20189 | ], | |
20190 | [ | |
20191 | 'da6545d2181db8d983f7dcb375ef5866d47c67b1bf31c8cf855ef7437b72656a', | |
20192 | '49b96715ab6878a79e78f07ce5680c5d6673051b4935bd897fea824b77dc208a' | |
20193 | ], | |
20194 | [ | |
20195 | 'c40747cc9d012cb1a13b8148309c6de7ec25d6945d657146b9d5994b8feb1111', | |
20196 | '5ca560753be2a12fc6de6caf2cb489565db936156b9514e1bb5e83037e0fa2d4' | |
20197 | ], | |
20198 | [ | |
20199 | '4e42c8ec82c99798ccf3a610be870e78338c7f713348bd34c8203ef4037f3502', | |
20200 | '7571d74ee5e0fb92a7a8b33a07783341a5492144cc54bcc40a94473693606437' | |
20201 | ], | |
20202 | [ | |
20203 | '3775ab7089bc6af823aba2e1af70b236d251cadb0c86743287522a1b3b0dedea', | |
20204 | 'be52d107bcfa09d8bcb9736a828cfa7fac8db17bf7a76a2c42ad961409018cf7' | |
20205 | ], | |
20206 | [ | |
20207 | 'cee31cbf7e34ec379d94fb814d3d775ad954595d1314ba8846959e3e82f74e26', | |
20208 | '8fd64a14c06b589c26b947ae2bcf6bfa0149ef0be14ed4d80f448a01c43b1c6d' | |
20209 | ], | |
20210 | [ | |
20211 | 'b4f9eaea09b6917619f6ea6a4eb5464efddb58fd45b1ebefcdc1a01d08b47986', | |
20212 | '39e5c9925b5a54b07433a4f18c61726f8bb131c012ca542eb24a8ac07200682a' | |
20213 | ], | |
20214 | [ | |
20215 | 'd4263dfc3d2df923a0179a48966d30ce84e2515afc3dccc1b77907792ebcc60e', | |
20216 | '62dfaf07a0f78feb30e30d6295853ce189e127760ad6cf7fae164e122a208d54' | |
20217 | ], | |
20218 | [ | |
20219 | '48457524820fa65a4f8d35eb6930857c0032acc0a4a2de422233eeda897612c4', | |
20220 | '25a748ab367979d98733c38a1fa1c2e7dc6cc07db2d60a9ae7a76aaa49bd0f77' | |
20221 | ], | |
20222 | [ | |
20223 | 'dfeeef1881101f2cb11644f3a2afdfc2045e19919152923f367a1767c11cceda', | |
20224 | 'ecfb7056cf1de042f9420bab396793c0c390bde74b4bbdff16a83ae09a9a7517' | |
20225 | ], | |
20226 | [ | |
20227 | '6d7ef6b17543f8373c573f44e1f389835d89bcbc6062ced36c82df83b8fae859', | |
20228 | 'cd450ec335438986dfefa10c57fea9bcc521a0959b2d80bbf74b190dca712d10' | |
20229 | ], | |
20230 | [ | |
20231 | 'e75605d59102a5a2684500d3b991f2e3f3c88b93225547035af25af66e04541f', | |
20232 | 'f5c54754a8f71ee540b9b48728473e314f729ac5308b06938360990e2bfad125' | |
20233 | ], | |
20234 | [ | |
20235 | 'eb98660f4c4dfaa06a2be453d5020bc99a0c2e60abe388457dd43fefb1ed620c', | |
20236 | '6cb9a8876d9cb8520609af3add26cd20a0a7cd8a9411131ce85f44100099223e' | |
20237 | ], | |
20238 | [ | |
20239 | '13e87b027d8514d35939f2e6892b19922154596941888336dc3563e3b8dba942', | |
20240 | 'fef5a3c68059a6dec5d624114bf1e91aac2b9da568d6abeb2570d55646b8adf1' | |
20241 | ], | |
20242 | [ | |
20243 | 'ee163026e9fd6fe017c38f06a5be6fc125424b371ce2708e7bf4491691e5764a', | |
20244 | '1acb250f255dd61c43d94ccc670d0f58f49ae3fa15b96623e5430da0ad6c62b2' | |
20245 | ], | |
20246 | [ | |
20247 | 'b268f5ef9ad51e4d78de3a750c2dc89b1e626d43505867999932e5db33af3d80', | |
20248 | '5f310d4b3c99b9ebb19f77d41c1dee018cf0d34fd4191614003e945a1216e423' | |
20249 | ], | |
20250 | [ | |
20251 | 'ff07f3118a9df035e9fad85eb6c7bfe42b02f01ca99ceea3bf7ffdba93c4750d', | |
20252 | '438136d603e858a3a5c440c38eccbaddc1d2942114e2eddd4740d098ced1f0d8' | |
20253 | ], | |
20254 | [ | |
20255 | '8d8b9855c7c052a34146fd20ffb658bea4b9f69e0d825ebec16e8c3ce2b526a1', | |
20256 | 'cdb559eedc2d79f926baf44fb84ea4d44bcf50fee51d7ceb30e2e7f463036758' | |
20257 | ], | |
20258 | [ | |
20259 | '52db0b5384dfbf05bfa9d472d7ae26dfe4b851ceca91b1eba54263180da32b63', | |
20260 | 'c3b997d050ee5d423ebaf66a6db9f57b3180c902875679de924b69d84a7b375' | |
20261 | ], | |
20262 | [ | |
20263 | 'e62f9490d3d51da6395efd24e80919cc7d0f29c3f3fa48c6fff543becbd43352', | |
20264 | '6d89ad7ba4876b0b22c2ca280c682862f342c8591f1daf5170e07bfd9ccafa7d' | |
20265 | ], | |
20266 | [ | |
20267 | '7f30ea2476b399b4957509c88f77d0191afa2ff5cb7b14fd6d8e7d65aaab1193', | |
20268 | 'ca5ef7d4b231c94c3b15389a5f6311e9daff7bb67b103e9880ef4bff637acaec' | |
20269 | ], | |
20270 | [ | |
20271 | '5098ff1e1d9f14fb46a210fada6c903fef0fb7b4a1dd1d9ac60a0361800b7a00', | |
20272 | '9731141d81fc8f8084d37c6e7542006b3ee1b40d60dfe5362a5b132fd17ddc0' | |
20273 | ], | |
20274 | [ | |
20275 | '32b78c7de9ee512a72895be6b9cbefa6e2f3c4ccce445c96b9f2c81e2778ad58', | |
20276 | 'ee1849f513df71e32efc3896ee28260c73bb80547ae2275ba497237794c8753c' | |
20277 | ], | |
20278 | [ | |
20279 | 'e2cb74fddc8e9fbcd076eef2a7c72b0ce37d50f08269dfc074b581550547a4f7', | |
20280 | 'd3aa2ed71c9dd2247a62df062736eb0baddea9e36122d2be8641abcb005cc4a4' | |
20281 | ], | |
20282 | [ | |
20283 | '8438447566d4d7bedadc299496ab357426009a35f235cb141be0d99cd10ae3a8', | |
20284 | 'c4e1020916980a4da5d01ac5e6ad330734ef0d7906631c4f2390426b2edd791f' | |
20285 | ], | |
20286 | [ | |
20287 | '4162d488b89402039b584c6fc6c308870587d9c46f660b878ab65c82c711d67e', | |
20288 | '67163e903236289f776f22c25fb8a3afc1732f2b84b4e95dbda47ae5a0852649' | |
20289 | ], | |
20290 | [ | |
20291 | '3fad3fa84caf0f34f0f89bfd2dcf54fc175d767aec3e50684f3ba4a4bf5f683d', | |
20292 | 'cd1bc7cb6cc407bb2f0ca647c718a730cf71872e7d0d2a53fa20efcdfe61826' | |
20293 | ], | |
20294 | [ | |
20295 | '674f2600a3007a00568c1a7ce05d0816c1fb84bf1370798f1c69532faeb1a86b', | |
20296 | '299d21f9413f33b3edf43b257004580b70db57da0b182259e09eecc69e0d38a5' | |
20297 | ], | |
20298 | [ | |
20299 | 'd32f4da54ade74abb81b815ad1fb3b263d82d6c692714bcff87d29bd5ee9f08f', | |
20300 | 'f9429e738b8e53b968e99016c059707782e14f4535359d582fc416910b3eea87' | |
20301 | ], | |
20302 | [ | |
20303 | '30e4e670435385556e593657135845d36fbb6931f72b08cb1ed954f1e3ce3ff6', | |
20304 | '462f9bce619898638499350113bbc9b10a878d35da70740dc695a559eb88db7b' | |
20305 | ], | |
20306 | [ | |
20307 | 'be2062003c51cc3004682904330e4dee7f3dcd10b01e580bf1971b04d4cad297', | |
20308 | '62188bc49d61e5428573d48a74e1c655b1c61090905682a0d5558ed72dccb9bc' | |
20309 | ], | |
20310 | [ | |
20311 | '93144423ace3451ed29e0fb9ac2af211cb6e84a601df5993c419859fff5df04a', | |
20312 | '7c10dfb164c3425f5c71a3f9d7992038f1065224f72bb9d1d902a6d13037b47c' | |
20313 | ], | |
20314 | [ | |
20315 | 'b015f8044f5fcbdcf21ca26d6c34fb8197829205c7b7d2a7cb66418c157b112c', | |
20316 | 'ab8c1e086d04e813744a655b2df8d5f83b3cdc6faa3088c1d3aea1454e3a1d5f' | |
20317 | ], | |
20318 | [ | |
20319 | 'd5e9e1da649d97d89e4868117a465a3a4f8a18de57a140d36b3f2af341a21b52', | |
20320 | '4cb04437f391ed73111a13cc1d4dd0db1693465c2240480d8955e8592f27447a' | |
20321 | ], | |
20322 | [ | |
20323 | 'd3ae41047dd7ca065dbf8ed77b992439983005cd72e16d6f996a5316d36966bb', | |
20324 | 'bd1aeb21ad22ebb22a10f0303417c6d964f8cdd7df0aca614b10dc14d125ac46' | |
20325 | ], | |
20326 | [ | |
20327 | '463e2763d885f958fc66cdd22800f0a487197d0a82e377b49f80af87c897b065', | |
20328 | 'bfefacdb0e5d0fd7df3a311a94de062b26b80c61fbc97508b79992671ef7ca7f' | |
20329 | ], | |
20330 | [ | |
20331 | '7985fdfd127c0567c6f53ec1bb63ec3158e597c40bfe747c83cddfc910641917', | |
20332 | '603c12daf3d9862ef2b25fe1de289aed24ed291e0ec6708703a5bd567f32ed03' | |
20333 | ], | |
20334 | [ | |
20335 | '74a1ad6b5f76e39db2dd249410eac7f99e74c59cb83d2d0ed5ff1543da7703e9', | |
20336 | 'cc6157ef18c9c63cd6193d83631bbea0093e0968942e8c33d5737fd790e0db08' | |
20337 | ], | |
20338 | [ | |
20339 | '30682a50703375f602d416664ba19b7fc9bab42c72747463a71d0896b22f6da3', | |
20340 | '553e04f6b018b4fa6c8f39e7f311d3176290d0e0f19ca73f17714d9977a22ff8' | |
20341 | ], | |
20342 | [ | |
20343 | '9e2158f0d7c0d5f26c3791efefa79597654e7a2b2464f52b1ee6c1347769ef57', | |
20344 | '712fcdd1b9053f09003a3481fa7762e9ffd7c8ef35a38509e2fbf2629008373' | |
20345 | ], | |
20346 | [ | |
20347 | '176e26989a43c9cfeba4029c202538c28172e566e3c4fce7322857f3be327d66', | |
20348 | 'ed8cc9d04b29eb877d270b4878dc43c19aefd31f4eee09ee7b47834c1fa4b1c3' | |
20349 | ], | |
20350 | [ | |
20351 | '75d46efea3771e6e68abb89a13ad747ecf1892393dfc4f1b7004788c50374da8', | |
20352 | '9852390a99507679fd0b86fd2b39a868d7efc22151346e1a3ca4726586a6bed8' | |
20353 | ], | |
20354 | [ | |
20355 | '809a20c67d64900ffb698c4c825f6d5f2310fb0451c869345b7319f645605721', | |
20356 | '9e994980d9917e22b76b061927fa04143d096ccc54963e6a5ebfa5f3f8e286c1' | |
20357 | ], | |
20358 | [ | |
20359 | '1b38903a43f7f114ed4500b4eac7083fdefece1cf29c63528d563446f972c180', | |
20360 | '4036edc931a60ae889353f77fd53de4a2708b26b6f5da72ad3394119daf408f9' | |
20361 | ] | |
20362 | ] | |
a0091a40 | 20363 | } |
9f59e99b | 20364 | }; |
a0091a40 | 20365 | |
9f59e99b IC |
20366 | },{}],111:[function(require,module,exports){ |
20367 | 'use strict'; | |
a0091a40 | 20368 | |
9f59e99b IC |
20369 | var utils = exports; |
20370 | var BN = require('bn.js'); | |
20371 | var minAssert = require('minimalistic-assert'); | |
20372 | var minUtils = require('minimalistic-crypto-utils'); | |
20373 | ||
20374 | utils.assert = minAssert; | |
20375 | utils.toArray = minUtils.toArray; | |
20376 | utils.zero2 = minUtils.zero2; | |
20377 | utils.toHex = minUtils.toHex; | |
20378 | utils.encode = minUtils.encode; | |
20379 | ||
20380 | // Represent num in a w-NAF form | |
20381 | function getNAF(num, w) { | |
20382 | var naf = []; | |
20383 | var ws = 1 << (w + 1); | |
20384 | var k = num.clone(); | |
20385 | while (k.cmpn(1) >= 0) { | |
20386 | var z; | |
20387 | if (k.isOdd()) { | |
20388 | var mod = k.andln(ws - 1); | |
20389 | if (mod > (ws >> 1) - 1) | |
20390 | z = (ws >> 1) - mod; | |
20391 | else | |
20392 | z = mod; | |
20393 | k.isubn(z); | |
20394 | } else { | |
20395 | z = 0; | |
20396 | } | |
20397 | naf.push(z); | |
20398 | ||
20399 | // Optimization, shift by word if possible | |
20400 | var shift = (k.cmpn(0) !== 0 && k.andln(ws - 1) === 0) ? (w + 1) : 1; | |
20401 | for (var i = 1; i < shift; i++) | |
20402 | naf.push(0); | |
20403 | k.iushrn(shift); | |
20404 | } | |
20405 | ||
20406 | return naf; | |
20407 | } | |
20408 | utils.getNAF = getNAF; | |
20409 | ||
20410 | // Represent k1, k2 in a Joint Sparse Form | |
20411 | function getJSF(k1, k2) { | |
20412 | var jsf = [ | |
20413 | [], | |
20414 | [] | |
20415 | ]; | |
20416 | ||
20417 | k1 = k1.clone(); | |
20418 | k2 = k2.clone(); | |
20419 | var d1 = 0; | |
20420 | var d2 = 0; | |
20421 | while (k1.cmpn(-d1) > 0 || k2.cmpn(-d2) > 0) { | |
20422 | ||
20423 | // First phase | |
20424 | var m14 = (k1.andln(3) + d1) & 3; | |
20425 | var m24 = (k2.andln(3) + d2) & 3; | |
20426 | if (m14 === 3) | |
20427 | m14 = -1; | |
20428 | if (m24 === 3) | |
20429 | m24 = -1; | |
20430 | var u1; | |
20431 | if ((m14 & 1) === 0) { | |
20432 | u1 = 0; | |
20433 | } else { | |
20434 | var m8 = (k1.andln(7) + d1) & 7; | |
20435 | if ((m8 === 3 || m8 === 5) && m24 === 2) | |
20436 | u1 = -m14; | |
20437 | else | |
20438 | u1 = m14; | |
20439 | } | |
20440 | jsf[0].push(u1); | |
20441 | ||
20442 | var u2; | |
20443 | if ((m24 & 1) === 0) { | |
20444 | u2 = 0; | |
20445 | } else { | |
20446 | var m8 = (k2.andln(7) + d2) & 7; | |
20447 | if ((m8 === 3 || m8 === 5) && m14 === 2) | |
20448 | u2 = -m24; | |
20449 | else | |
20450 | u2 = m24; | |
20451 | } | |
20452 | jsf[1].push(u2); | |
20453 | ||
20454 | // Second phase | |
20455 | if (2 * d1 === u1 + 1) | |
20456 | d1 = 1 - d1; | |
20457 | if (2 * d2 === u2 + 1) | |
20458 | d2 = 1 - d2; | |
20459 | k1.iushrn(1); | |
20460 | k2.iushrn(1); | |
a0091a40 | 20461 | } |
9f59e99b IC |
20462 | |
20463 | return jsf; | |
a0091a40 | 20464 | } |
9f59e99b | 20465 | utils.getJSF = getJSF; |
a0091a40 | 20466 | |
9f59e99b IC |
20467 | function cachedProperty(obj, name, computer) { |
20468 | var key = '_' + name; | |
20469 | obj.prototype[name] = function cachedProperty() { | |
20470 | return this[key] !== undefined ? this[key] : | |
20471 | this[key] = computer.call(this); | |
20472 | }; | |
20473 | } | |
20474 | utils.cachedProperty = cachedProperty; | |
a0091a40 | 20475 | |
9f59e99b IC |
20476 | function parseBytes(bytes) { |
20477 | return typeof bytes === 'string' ? utils.toArray(bytes, 'hex') : | |
20478 | bytes; | |
a0091a40 | 20479 | } |
9f59e99b | 20480 | utils.parseBytes = parseBytes; |
a0091a40 | 20481 | |
9f59e99b IC |
20482 | function intFromLE(bytes) { |
20483 | return new BN(bytes, 'hex', 'le'); | |
a0091a40 | 20484 | } |
9f59e99b | 20485 | utils.intFromLE = intFromLE; |
a0091a40 | 20486 | |
9f59e99b IC |
20487 | |
20488 | },{"bn.js":81,"minimalistic-assert":123,"minimalistic-crypto-utils":124}],112:[function(require,module,exports){ | |
20489 | module.exports={ | |
20490 | "_args": [ | |
20491 | [ | |
20492 | "elliptic", | |
20493 | "/home/ian/git/bitcoin/bitcoinjs-lib-browser" | |
20494 | ] | |
20495 | ], | |
20496 | "_from": "elliptic@latest", | |
20497 | "_id": "elliptic@6.4.0", | |
20498 | "_inCache": true, | |
20499 | "_installable": true, | |
20500 | "_location": "/elliptic", | |
20501 | "_nodeVersion": "7.0.0", | |
20502 | "_npmOperationalInternal": { | |
20503 | "host": "packages-18-east.internal.npmjs.com", | |
20504 | "tmp": "tmp/elliptic-6.4.0.tgz_1487798866428_0.30510620190761983" | |
20505 | }, | |
20506 | "_npmUser": { | |
20507 | "email": "fedor@indutny.com", | |
20508 | "name": "indutny" | |
20509 | }, | |
20510 | "_npmVersion": "3.10.8", | |
20511 | "_phantomChildren": {}, | |
20512 | "_requested": { | |
20513 | "name": "elliptic", | |
20514 | "raw": "elliptic", | |
20515 | "rawSpec": "", | |
20516 | "scope": null, | |
20517 | "spec": "latest", | |
20518 | "type": "tag" | |
20519 | }, | |
20520 | "_requiredBy": [ | |
20521 | "#USER" | |
20522 | ], | |
20523 | "_resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.4.0.tgz", | |
20524 | "_shasum": "cac9af8762c85836187003c8dfe193e5e2eae5df", | |
20525 | "_shrinkwrap": null, | |
20526 | "_spec": "elliptic", | |
20527 | "_where": "/home/ian/git/bitcoin/bitcoinjs-lib-browser", | |
20528 | "author": { | |
20529 | "email": "fedor@indutny.com", | |
20530 | "name": "Fedor Indutny" | |
20531 | }, | |
20532 | "bugs": { | |
20533 | "url": "https://github.com/indutny/elliptic/issues" | |
20534 | }, | |
20535 | "dependencies": { | |
20536 | "bn.js": "^4.4.0", | |
20537 | "brorand": "^1.0.1", | |
20538 | "hash.js": "^1.0.0", | |
20539 | "hmac-drbg": "^1.0.0", | |
20540 | "inherits": "^2.0.1", | |
20541 | "minimalistic-assert": "^1.0.0", | |
20542 | "minimalistic-crypto-utils": "^1.0.0" | |
20543 | }, | |
20544 | "description": "EC cryptography", | |
20545 | "devDependencies": { | |
20546 | "brfs": "^1.4.3", | |
20547 | "coveralls": "^2.11.3", | |
20548 | "grunt": "^0.4.5", | |
20549 | "grunt-browserify": "^5.0.0", | |
20550 | "grunt-cli": "^1.2.0", | |
20551 | "grunt-contrib-connect": "^1.0.0", | |
20552 | "grunt-contrib-copy": "^1.0.0", | |
20553 | "grunt-contrib-uglify": "^1.0.1", | |
20554 | "grunt-mocha-istanbul": "^3.0.1", | |
20555 | "grunt-saucelabs": "^8.6.2", | |
20556 | "istanbul": "^0.4.2", | |
20557 | "jscs": "^2.9.0", | |
20558 | "jshint": "^2.6.0", | |
20559 | "mocha": "^2.1.0" | |
20560 | }, | |
20561 | "directories": {}, | |
20562 | "dist": { | |
20563 | "shasum": "cac9af8762c85836187003c8dfe193e5e2eae5df", | |
20564 | "tarball": "https://registry.npmjs.org/elliptic/-/elliptic-6.4.0.tgz" | |
20565 | }, | |
20566 | "files": [ | |
20567 | "lib" | |
20568 | ], | |
20569 | "gitHead": "6b0d2b76caae91471649c8e21f0b1d3ba0f96090", | |
20570 | "homepage": "https://github.com/indutny/elliptic", | |
20571 | "keywords": [ | |
20572 | "EC", | |
20573 | "Elliptic", | |
20574 | "curve", | |
20575 | "Cryptography" | |
20576 | ], | |
20577 | "license": "MIT", | |
20578 | "main": "lib/elliptic.js", | |
20579 | "maintainers": [ | |
20580 | { | |
20581 | "email": "fedor@indutny.com", | |
20582 | "name": "indutny" | |
20583 | } | |
20584 | ], | |
20585 | "name": "elliptic", | |
20586 | "optionalDependencies": {}, | |
20587 | "readme": "ERROR: No README data found!", | |
20588 | "repository": { | |
20589 | "type": "git", | |
20590 | "url": "git+ssh://git@github.com/indutny/elliptic.git" | |
20591 | }, | |
20592 | "scripts": { | |
20593 | "jscs": "jscs benchmarks/*.js lib/*.js lib/**/*.js lib/**/**/*.js test/index.js", | |
20594 | "jshint": "jscs benchmarks/*.js lib/*.js lib/**/*.js lib/**/**/*.js test/index.js", | |
20595 | "lint": "npm run jscs && npm run jshint", | |
20596 | "test": "npm run lint && npm run unit", | |
20597 | "unit": "istanbul test _mocha --reporter=spec test/index.js", | |
20598 | "version": "grunt dist && git add dist/" | |
20599 | }, | |
20600 | "version": "6.4.0" | |
a0091a40 IC |
20601 | } |
20602 | ||
9f59e99b IC |
20603 | },{}],113:[function(require,module,exports){ |
20604 | (function (Buffer){ | |
a0091a40 | 20605 | 'use strict' |
9f59e99b | 20606 | var Transform = require('stream').Transform |
a0091a40 | 20607 | var inherits = require('inherits') |
a0091a40 | 20608 | |
9f59e99b IC |
20609 | function HashBase (blockSize) { |
20610 | Transform.call(this) | |
a0091a40 | 20611 | |
9f59e99b IC |
20612 | this._block = new Buffer(blockSize) |
20613 | this._blockSize = blockSize | |
20614 | this._blockOffset = 0 | |
20615 | this._length = [0, 0, 0, 0] | |
a0091a40 | 20616 | |
9f59e99b IC |
20617 | this._finalized = false |
20618 | } | |
20619 | ||
20620 | inherits(HashBase, Transform) | |
20621 | ||
20622 | HashBase.prototype._transform = function (chunk, encoding, callback) { | |
20623 | var error = null | |
20624 | try { | |
20625 | if (encoding !== 'buffer') chunk = new Buffer(chunk, encoding) | |
20626 | this.update(chunk) | |
20627 | } catch (err) { | |
20628 | error = err | |
a0091a40 IC |
20629 | } |
20630 | ||
9f59e99b IC |
20631 | callback(error) |
20632 | } | |
a0091a40 | 20633 | |
9f59e99b IC |
20634 | HashBase.prototype._flush = function (callback) { |
20635 | var error = null | |
20636 | try { | |
20637 | this.push(this._digest()) | |
20638 | } catch (err) { | |
20639 | error = err | |
a0091a40 IC |
20640 | } |
20641 | ||
9f59e99b IC |
20642 | callback(error) |
20643 | } | |
a0091a40 | 20644 | |
9f59e99b IC |
20645 | HashBase.prototype.update = function (data, encoding) { |
20646 | if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer') | |
20647 | if (this._finalized) throw new Error('Digest already called') | |
20648 | if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding || 'binary') | |
20649 | ||
20650 | // consume data | |
20651 | var block = this._block | |
20652 | var offset = 0 | |
20653 | while (this._blockOffset + data.length - offset >= this._blockSize) { | |
20654 | for (var i = this._blockOffset; i < this._blockSize;) block[i++] = data[offset++] | |
20655 | this._update() | |
20656 | this._blockOffset = 0 | |
a0091a40 | 20657 | } |
9f59e99b | 20658 | while (offset < data.length) block[this._blockOffset++] = data[offset++] |
a0091a40 | 20659 | |
9f59e99b IC |
20660 | // update length |
20661 | for (var j = 0, carry = data.length * 8; carry > 0; ++j) { | |
20662 | this._length[j] += carry | |
20663 | carry = (this._length[j] / 0x0100000000) | 0 | |
20664 | if (carry > 0) this._length[j] -= 0x0100000000 * carry | |
20665 | } | |
20666 | ||
20667 | return this | |
20668 | } | |
20669 | ||
20670 | HashBase.prototype._update = function (data) { | |
20671 | throw new Error('_update is not implemented') | |
a0091a40 IC |
20672 | } |
20673 | ||
9f59e99b IC |
20674 | HashBase.prototype.digest = function (encoding) { |
20675 | if (this._finalized) throw new Error('Digest already called') | |
20676 | this._finalized = true | |
a0091a40 | 20677 | |
9f59e99b IC |
20678 | var digest = this._digest() |
20679 | if (encoding !== undefined) digest = digest.toString(encoding) | |
20680 | return digest | |
a0091a40 IC |
20681 | } |
20682 | ||
9f59e99b IC |
20683 | HashBase.prototype._digest = function () { |
20684 | throw new Error('_digest is not implemented') | |
a0091a40 | 20685 | } |
a0091a40 | 20686 | |
9f59e99b | 20687 | module.exports = HashBase |
a0091a40 | 20688 | |
9f59e99b IC |
20689 | }).call(this,require("buffer").Buffer) |
20690 | },{"buffer":5,"inherits":121,"stream":28}],114:[function(require,module,exports){ | |
20691 | var hash = exports; | |
20692 | ||
20693 | hash.utils = require('./hash/utils'); | |
20694 | hash.common = require('./hash/common'); | |
20695 | hash.sha = require('./hash/sha'); | |
20696 | hash.ripemd = require('./hash/ripemd'); | |
20697 | hash.hmac = require('./hash/hmac'); | |
20698 | ||
20699 | // Proxy hash functions to the main object | |
20700 | hash.sha1 = hash.sha.sha1; | |
20701 | hash.sha256 = hash.sha.sha256; | |
20702 | hash.sha224 = hash.sha.sha224; | |
20703 | hash.sha384 = hash.sha.sha384; | |
20704 | hash.sha512 = hash.sha.sha512; | |
20705 | hash.ripemd160 = hash.ripemd.ripemd160; | |
20706 | ||
20707 | },{"./hash/common":115,"./hash/hmac":116,"./hash/ripemd":117,"./hash/sha":118,"./hash/utils":119}],115:[function(require,module,exports){ | |
20708 | var hash = require('../hash'); | |
20709 | var utils = hash.utils; | |
20710 | var assert = utils.assert; | |
20711 | ||
20712 | function BlockHash() { | |
20713 | this.pending = null; | |
20714 | this.pendingTotal = 0; | |
20715 | this.blockSize = this.constructor.blockSize; | |
20716 | this.outSize = this.constructor.outSize; | |
20717 | this.hmacStrength = this.constructor.hmacStrength; | |
20718 | this.padLength = this.constructor.padLength / 8; | |
20719 | this.endian = 'big'; | |
20720 | ||
20721 | this._delta8 = this.blockSize / 8; | |
20722 | this._delta32 = this.blockSize / 32; | |
20723 | } | |
20724 | exports.BlockHash = BlockHash; | |
20725 | ||
20726 | BlockHash.prototype.update = function update(msg, enc) { | |
20727 | // Convert message to array, pad it, and join into 32bit blocks | |
20728 | msg = utils.toArray(msg, enc); | |
20729 | if (!this.pending) | |
20730 | this.pending = msg; | |
20731 | else | |
20732 | this.pending = this.pending.concat(msg); | |
20733 | this.pendingTotal += msg.length; | |
a0091a40 | 20734 | |
9f59e99b IC |
20735 | // Enough data, try updating |
20736 | if (this.pending.length >= this._delta8) { | |
20737 | msg = this.pending; | |
a0091a40 | 20738 | |
9f59e99b IC |
20739 | // Process pending data in blocks |
20740 | var r = msg.length % this._delta8; | |
20741 | this.pending = msg.slice(msg.length - r, msg.length); | |
20742 | if (this.pending.length === 0) | |
20743 | this.pending = null; | |
a0091a40 | 20744 | |
9f59e99b IC |
20745 | msg = utils.join32(msg, 0, msg.length - r, this.endian); |
20746 | for (var i = 0; i < msg.length; i += this._delta32) | |
20747 | this._update(msg, i, i + this._delta32); | |
20748 | } | |
a0091a40 | 20749 | |
9f59e99b IC |
20750 | return this; |
20751 | }; | |
a0091a40 | 20752 | |
9f59e99b IC |
20753 | BlockHash.prototype.digest = function digest(enc) { |
20754 | this.update(this._pad()); | |
20755 | assert(this.pending === null); | |
a0091a40 | 20756 | |
9f59e99b IC |
20757 | return this._digest(enc); |
20758 | }; | |
20759 | ||
20760 | BlockHash.prototype._pad = function pad() { | |
20761 | var len = this.pendingTotal; | |
20762 | var bytes = this._delta8; | |
20763 | var k = bytes - ((len + this.padLength) % bytes); | |
20764 | var res = new Array(k + this.padLength); | |
20765 | res[0] = 0x80; | |
20766 | for (var i = 1; i < k; i++) | |
20767 | res[i] = 0; | |
20768 | ||
20769 | // Append length | |
20770 | len <<= 3; | |
20771 | if (this.endian === 'big') { | |
20772 | for (var t = 8; t < this.padLength; t++) | |
20773 | res[i++] = 0; | |
20774 | ||
20775 | res[i++] = 0; | |
20776 | res[i++] = 0; | |
20777 | res[i++] = 0; | |
20778 | res[i++] = 0; | |
20779 | res[i++] = (len >>> 24) & 0xff; | |
20780 | res[i++] = (len >>> 16) & 0xff; | |
20781 | res[i++] = (len >>> 8) & 0xff; | |
20782 | res[i++] = len & 0xff; | |
20783 | } else { | |
20784 | res[i++] = len & 0xff; | |
20785 | res[i++] = (len >>> 8) & 0xff; | |
20786 | res[i++] = (len >>> 16) & 0xff; | |
20787 | res[i++] = (len >>> 24) & 0xff; | |
20788 | res[i++] = 0; | |
20789 | res[i++] = 0; | |
20790 | res[i++] = 0; | |
20791 | res[i++] = 0; | |
20792 | ||
20793 | for (var t = 8; t < this.padLength; t++) | |
20794 | res[i++] = 0; | |
a0091a40 IC |
20795 | } |
20796 | ||
9f59e99b IC |
20797 | return res; |
20798 | }; | |
a0091a40 | 20799 | |
9f59e99b IC |
20800 | },{"../hash":114}],116:[function(require,module,exports){ |
20801 | var hmac = exports; | |
a0091a40 | 20802 | |
9f59e99b IC |
20803 | var hash = require('../hash'); |
20804 | var utils = hash.utils; | |
20805 | var assert = utils.assert; | |
20806 | ||
20807 | function Hmac(hash, key, enc) { | |
20808 | if (!(this instanceof Hmac)) | |
20809 | return new Hmac(hash, key, enc); | |
20810 | this.Hash = hash; | |
20811 | this.blockSize = hash.blockSize / 8; | |
20812 | this.outSize = hash.outSize / 8; | |
20813 | this.inner = null; | |
20814 | this.outer = null; | |
20815 | ||
20816 | this._init(utils.toArray(key, enc)); | |
a0091a40 | 20817 | } |
9f59e99b | 20818 | module.exports = Hmac; |
a0091a40 | 20819 | |
9f59e99b IC |
20820 | Hmac.prototype._init = function init(key) { |
20821 | // Shorten key, if needed | |
20822 | if (key.length > this.blockSize) | |
20823 | key = new this.Hash().update(key).digest(); | |
20824 | assert(key.length <= this.blockSize); | |
a0091a40 | 20825 | |
9f59e99b IC |
20826 | // Add padding to key |
20827 | for (var i = key.length; i < this.blockSize; i++) | |
20828 | key.push(0); | |
a0091a40 | 20829 | |
9f59e99b IC |
20830 | for (var i = 0; i < key.length; i++) |
20831 | key[i] ^= 0x36; | |
20832 | this.inner = new this.Hash().update(key); | |
a0091a40 | 20833 | |
9f59e99b IC |
20834 | // 0x36 ^ 0x5c = 0x6a |
20835 | for (var i = 0; i < key.length; i++) | |
20836 | key[i] ^= 0x6a; | |
20837 | this.outer = new this.Hash().update(key); | |
20838 | }; | |
a0091a40 | 20839 | |
9f59e99b IC |
20840 | Hmac.prototype.update = function update(msg, enc) { |
20841 | this.inner.update(msg, enc); | |
20842 | return this; | |
20843 | }; | |
a0091a40 | 20844 | |
9f59e99b IC |
20845 | Hmac.prototype.digest = function digest(enc) { |
20846 | this.outer.update(this.inner.digest()); | |
20847 | return this.outer.digest(enc); | |
20848 | }; | |
a0091a40 | 20849 | |
9f59e99b IC |
20850 | },{"../hash":114}],117:[function(require,module,exports){ |
20851 | var hash = require('../hash'); | |
20852 | var utils = hash.utils; | |
20853 | ||
20854 | var rotl32 = utils.rotl32; | |
20855 | var sum32 = utils.sum32; | |
20856 | var sum32_3 = utils.sum32_3; | |
20857 | var sum32_4 = utils.sum32_4; | |
20858 | var BlockHash = hash.common.BlockHash; | |
20859 | ||
20860 | function RIPEMD160() { | |
20861 | if (!(this instanceof RIPEMD160)) | |
20862 | return new RIPEMD160(); | |
20863 | ||
20864 | BlockHash.call(this); | |
20865 | ||
20866 | this.h = [ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 ]; | |
20867 | this.endian = 'little'; | |
20868 | } | |
20869 | utils.inherits(RIPEMD160, BlockHash); | |
20870 | exports.ripemd160 = RIPEMD160; | |
20871 | ||
20872 | RIPEMD160.blockSize = 512; | |
20873 | RIPEMD160.outSize = 160; | |
20874 | RIPEMD160.hmacStrength = 192; | |
20875 | RIPEMD160.padLength = 64; | |
20876 | ||
20877 | RIPEMD160.prototype._update = function update(msg, start) { | |
20878 | var A = this.h[0]; | |
20879 | var B = this.h[1]; | |
20880 | var C = this.h[2]; | |
20881 | var D = this.h[3]; | |
20882 | var E = this.h[4]; | |
20883 | var Ah = A; | |
20884 | var Bh = B; | |
20885 | var Ch = C; | |
20886 | var Dh = D; | |
20887 | var Eh = E; | |
20888 | for (var j = 0; j < 80; j++) { | |
20889 | var T = sum32( | |
20890 | rotl32( | |
20891 | sum32_4(A, f(j, B, C, D), msg[r[j] + start], K(j)), | |
20892 | s[j]), | |
20893 | E); | |
20894 | A = E; | |
20895 | E = D; | |
20896 | D = rotl32(C, 10); | |
20897 | C = B; | |
20898 | B = T; | |
20899 | T = sum32( | |
20900 | rotl32( | |
20901 | sum32_4(Ah, f(79 - j, Bh, Ch, Dh), msg[rh[j] + start], Kh(j)), | |
20902 | sh[j]), | |
20903 | Eh); | |
20904 | Ah = Eh; | |
20905 | Eh = Dh; | |
20906 | Dh = rotl32(Ch, 10); | |
20907 | Ch = Bh; | |
20908 | Bh = T; | |
20909 | } | |
20910 | T = sum32_3(this.h[1], C, Dh); | |
20911 | this.h[1] = sum32_3(this.h[2], D, Eh); | |
20912 | this.h[2] = sum32_3(this.h[3], E, Ah); | |
20913 | this.h[3] = sum32_3(this.h[4], A, Bh); | |
20914 | this.h[4] = sum32_3(this.h[0], B, Ch); | |
20915 | this.h[0] = T; | |
20916 | }; | |
a0091a40 | 20917 | |
9f59e99b IC |
20918 | RIPEMD160.prototype._digest = function digest(enc) { |
20919 | if (enc === 'hex') | |
20920 | return utils.toHex32(this.h, 'little'); | |
20921 | else | |
20922 | return utils.split32(this.h, 'little'); | |
20923 | }; | |
a0091a40 | 20924 | |
9f59e99b IC |
20925 | function f(j, x, y, z) { |
20926 | if (j <= 15) | |
20927 | return x ^ y ^ z; | |
20928 | else if (j <= 31) | |
20929 | return (x & y) | ((~x) & z); | |
20930 | else if (j <= 47) | |
20931 | return (x | (~y)) ^ z; | |
20932 | else if (j <= 63) | |
20933 | return (x & z) | (y & (~z)); | |
20934 | else | |
20935 | return x ^ (y | (~z)); | |
20936 | } | |
20937 | ||
20938 | function K(j) { | |
20939 | if (j <= 15) | |
20940 | return 0x00000000; | |
20941 | else if (j <= 31) | |
20942 | return 0x5a827999; | |
20943 | else if (j <= 47) | |
20944 | return 0x6ed9eba1; | |
20945 | else if (j <= 63) | |
20946 | return 0x8f1bbcdc; | |
20947 | else | |
20948 | return 0xa953fd4e; | |
20949 | } | |
20950 | ||
20951 | function Kh(j) { | |
20952 | if (j <= 15) | |
20953 | return 0x50a28be6; | |
20954 | else if (j <= 31) | |
20955 | return 0x5c4dd124; | |
20956 | else if (j <= 47) | |
20957 | return 0x6d703ef3; | |
20958 | else if (j <= 63) | |
20959 | return 0x7a6d76e9; | |
20960 | else | |
20961 | return 0x00000000; | |
20962 | } | |
20963 | ||
20964 | var r = [ | |
20965 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
20966 | 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, | |
20967 | 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, | |
20968 | 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, | |
20969 | 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 | |
20970 | ]; | |
20971 | ||
20972 | var rh = [ | |
20973 | 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, | |
20974 | 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, | |
20975 | 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, | |
20976 | 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, | |
20977 | 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 | |
20978 | ]; | |
20979 | ||
20980 | var s = [ | |
20981 | 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, | |
20982 | 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, | |
20983 | 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, | |
20984 | 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, | |
20985 | 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 | |
20986 | ]; | |
20987 | ||
20988 | var sh = [ | |
20989 | 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, | |
20990 | 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, | |
20991 | 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, | |
20992 | 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, | |
20993 | 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 | |
20994 | ]; | |
20995 | ||
20996 | },{"../hash":114}],118:[function(require,module,exports){ | |
20997 | var hash = require('../hash'); | |
20998 | var utils = hash.utils; | |
20999 | var assert = utils.assert; | |
21000 | ||
21001 | var rotr32 = utils.rotr32; | |
21002 | var rotl32 = utils.rotl32; | |
21003 | var sum32 = utils.sum32; | |
21004 | var sum32_4 = utils.sum32_4; | |
21005 | var sum32_5 = utils.sum32_5; | |
21006 | var rotr64_hi = utils.rotr64_hi; | |
21007 | var rotr64_lo = utils.rotr64_lo; | |
21008 | var shr64_hi = utils.shr64_hi; | |
21009 | var shr64_lo = utils.shr64_lo; | |
21010 | var sum64 = utils.sum64; | |
21011 | var sum64_hi = utils.sum64_hi; | |
21012 | var sum64_lo = utils.sum64_lo; | |
21013 | var sum64_4_hi = utils.sum64_4_hi; | |
21014 | var sum64_4_lo = utils.sum64_4_lo; | |
21015 | var sum64_5_hi = utils.sum64_5_hi; | |
21016 | var sum64_5_lo = utils.sum64_5_lo; | |
21017 | var BlockHash = hash.common.BlockHash; | |
21018 | ||
21019 | var sha256_K = [ | |
21020 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, | |
21021 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, | |
21022 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, | |
21023 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, | |
21024 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, | |
21025 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, | |
21026 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, | |
21027 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, | |
21028 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, | |
21029 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, | |
21030 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, | |
21031 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, | |
21032 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, | |
21033 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, | |
21034 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, | |
21035 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 | |
21036 | ]; | |
21037 | ||
21038 | var sha512_K = [ | |
21039 | 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, | |
21040 | 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc, | |
21041 | 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019, | |
21042 | 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118, | |
21043 | 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe, | |
21044 | 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2, | |
21045 | 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1, | |
21046 | 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694, | |
21047 | 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3, | |
21048 | 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65, | |
21049 | 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483, | |
21050 | 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5, | |
21051 | 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210, | |
21052 | 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4, | |
21053 | 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725, | |
21054 | 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70, | |
21055 | 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926, | |
21056 | 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df, | |
21057 | 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8, | |
21058 | 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b, | |
21059 | 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001, | |
21060 | 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30, | |
21061 | 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910, | |
21062 | 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8, | |
21063 | 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53, | |
21064 | 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8, | |
21065 | 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb, | |
21066 | 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3, | |
21067 | 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60, | |
21068 | 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec, | |
21069 | 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9, | |
21070 | 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b, | |
21071 | 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207, | |
21072 | 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178, | |
21073 | 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6, | |
21074 | 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b, | |
21075 | 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, | |
21076 | 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c, | |
21077 | 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a, | |
21078 | 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817 | |
21079 | ]; | |
21080 | ||
21081 | var sha1_K = [ | |
21082 | 0x5A827999, 0x6ED9EBA1, | |
21083 | 0x8F1BBCDC, 0xCA62C1D6 | |
21084 | ]; | |
21085 | ||
21086 | function SHA256() { | |
21087 | if (!(this instanceof SHA256)) | |
21088 | return new SHA256(); | |
21089 | ||
21090 | BlockHash.call(this); | |
21091 | this.h = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, | |
21092 | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ]; | |
21093 | this.k = sha256_K; | |
21094 | this.W = new Array(64); | |
21095 | } | |
21096 | utils.inherits(SHA256, BlockHash); | |
21097 | exports.sha256 = SHA256; | |
21098 | ||
21099 | SHA256.blockSize = 512; | |
21100 | SHA256.outSize = 256; | |
21101 | SHA256.hmacStrength = 192; | |
21102 | SHA256.padLength = 64; | |
21103 | ||
21104 | SHA256.prototype._update = function _update(msg, start) { | |
21105 | var W = this.W; | |
21106 | ||
21107 | for (var i = 0; i < 16; i++) | |
21108 | W[i] = msg[start + i]; | |
21109 | for (; i < W.length; i++) | |
21110 | W[i] = sum32_4(g1_256(W[i - 2]), W[i - 7], g0_256(W[i - 15]), W[i - 16]); | |
21111 | ||
21112 | var a = this.h[0]; | |
21113 | var b = this.h[1]; | |
21114 | var c = this.h[2]; | |
21115 | var d = this.h[3]; | |
21116 | var e = this.h[4]; | |
21117 | var f = this.h[5]; | |
21118 | var g = this.h[6]; | |
21119 | var h = this.h[7]; | |
21120 | ||
21121 | assert(this.k.length === W.length); | |
21122 | for (var i = 0; i < W.length; i++) { | |
21123 | var T1 = sum32_5(h, s1_256(e), ch32(e, f, g), this.k[i], W[i]); | |
21124 | var T2 = sum32(s0_256(a), maj32(a, b, c)); | |
21125 | h = g; | |
21126 | g = f; | |
21127 | f = e; | |
21128 | e = sum32(d, T1); | |
21129 | d = c; | |
21130 | c = b; | |
21131 | b = a; | |
21132 | a = sum32(T1, T2); | |
21133 | } | |
21134 | ||
21135 | this.h[0] = sum32(this.h[0], a); | |
21136 | this.h[1] = sum32(this.h[1], b); | |
21137 | this.h[2] = sum32(this.h[2], c); | |
21138 | this.h[3] = sum32(this.h[3], d); | |
21139 | this.h[4] = sum32(this.h[4], e); | |
21140 | this.h[5] = sum32(this.h[5], f); | |
21141 | this.h[6] = sum32(this.h[6], g); | |
21142 | this.h[7] = sum32(this.h[7], h); | |
21143 | }; | |
a0091a40 | 21144 | |
9f59e99b IC |
21145 | SHA256.prototype._digest = function digest(enc) { |
21146 | if (enc === 'hex') | |
21147 | return utils.toHex32(this.h, 'big'); | |
21148 | else | |
21149 | return utils.split32(this.h, 'big'); | |
21150 | }; | |
a0091a40 | 21151 | |
9f59e99b IC |
21152 | function SHA224() { |
21153 | if (!(this instanceof SHA224)) | |
21154 | return new SHA224(); | |
a0091a40 | 21155 | |
9f59e99b IC |
21156 | SHA256.call(this); |
21157 | this.h = [ 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, | |
21158 | 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 ]; | |
a0091a40 | 21159 | } |
9f59e99b IC |
21160 | utils.inherits(SHA224, SHA256); |
21161 | exports.sha224 = SHA224; | |
a0091a40 | 21162 | |
9f59e99b IC |
21163 | SHA224.blockSize = 512; |
21164 | SHA224.outSize = 224; | |
21165 | SHA224.hmacStrength = 192; | |
21166 | SHA224.padLength = 64; | |
a0091a40 | 21167 | |
9f59e99b IC |
21168 | SHA224.prototype._digest = function digest(enc) { |
21169 | // Just truncate output | |
21170 | if (enc === 'hex') | |
21171 | return utils.toHex32(this.h.slice(0, 7), 'big'); | |
21172 | else | |
21173 | return utils.split32(this.h.slice(0, 7), 'big'); | |
21174 | }; | |
a0091a40 | 21175 | |
9f59e99b IC |
21176 | function SHA512() { |
21177 | if (!(this instanceof SHA512)) | |
21178 | return new SHA512(); | |
21179 | ||
21180 | BlockHash.call(this); | |
21181 | this.h = [ 0x6a09e667, 0xf3bcc908, | |
21182 | 0xbb67ae85, 0x84caa73b, | |
21183 | 0x3c6ef372, 0xfe94f82b, | |
21184 | 0xa54ff53a, 0x5f1d36f1, | |
21185 | 0x510e527f, 0xade682d1, | |
21186 | 0x9b05688c, 0x2b3e6c1f, | |
21187 | 0x1f83d9ab, 0xfb41bd6b, | |
21188 | 0x5be0cd19, 0x137e2179 ]; | |
21189 | this.k = sha512_K; | |
21190 | this.W = new Array(160); | |
21191 | } | |
21192 | utils.inherits(SHA512, BlockHash); | |
21193 | exports.sha512 = SHA512; | |
21194 | ||
21195 | SHA512.blockSize = 1024; | |
21196 | SHA512.outSize = 512; | |
21197 | SHA512.hmacStrength = 192; | |
21198 | SHA512.padLength = 128; | |
21199 | ||
21200 | SHA512.prototype._prepareBlock = function _prepareBlock(msg, start) { | |
21201 | var W = this.W; | |
21202 | ||
21203 | // 32 x 32bit words | |
21204 | for (var i = 0; i < 32; i++) | |
21205 | W[i] = msg[start + i]; | |
21206 | for (; i < W.length; i += 2) { | |
21207 | var c0_hi = g1_512_hi(W[i - 4], W[i - 3]); // i - 2 | |
21208 | var c0_lo = g1_512_lo(W[i - 4], W[i - 3]); | |
21209 | var c1_hi = W[i - 14]; // i - 7 | |
21210 | var c1_lo = W[i - 13]; | |
21211 | var c2_hi = g0_512_hi(W[i - 30], W[i - 29]); // i - 15 | |
21212 | var c2_lo = g0_512_lo(W[i - 30], W[i - 29]); | |
21213 | var c3_hi = W[i - 32]; // i - 16 | |
21214 | var c3_lo = W[i - 31]; | |
21215 | ||
21216 | W[i] = sum64_4_hi(c0_hi, c0_lo, | |
21217 | c1_hi, c1_lo, | |
21218 | c2_hi, c2_lo, | |
21219 | c3_hi, c3_lo); | |
21220 | W[i + 1] = sum64_4_lo(c0_hi, c0_lo, | |
21221 | c1_hi, c1_lo, | |
21222 | c2_hi, c2_lo, | |
21223 | c3_hi, c3_lo); | |
21224 | } | |
21225 | }; | |
a0091a40 | 21226 | |
9f59e99b IC |
21227 | SHA512.prototype._update = function _update(msg, start) { |
21228 | this._prepareBlock(msg, start); | |
21229 | ||
21230 | var W = this.W; | |
21231 | ||
21232 | var ah = this.h[0]; | |
21233 | var al = this.h[1]; | |
21234 | var bh = this.h[2]; | |
21235 | var bl = this.h[3]; | |
21236 | var ch = this.h[4]; | |
21237 | var cl = this.h[5]; | |
21238 | var dh = this.h[6]; | |
21239 | var dl = this.h[7]; | |
21240 | var eh = this.h[8]; | |
21241 | var el = this.h[9]; | |
21242 | var fh = this.h[10]; | |
21243 | var fl = this.h[11]; | |
21244 | var gh = this.h[12]; | |
21245 | var gl = this.h[13]; | |
21246 | var hh = this.h[14]; | |
21247 | var hl = this.h[15]; | |
21248 | ||
21249 | assert(this.k.length === W.length); | |
21250 | for (var i = 0; i < W.length; i += 2) { | |
21251 | var c0_hi = hh; | |
21252 | var c0_lo = hl; | |
21253 | var c1_hi = s1_512_hi(eh, el); | |
21254 | var c1_lo = s1_512_lo(eh, el); | |
21255 | var c2_hi = ch64_hi(eh, el, fh, fl, gh, gl); | |
21256 | var c2_lo = ch64_lo(eh, el, fh, fl, gh, gl); | |
21257 | var c3_hi = this.k[i]; | |
21258 | var c3_lo = this.k[i + 1]; | |
21259 | var c4_hi = W[i]; | |
21260 | var c4_lo = W[i + 1]; | |
21261 | ||
21262 | var T1_hi = sum64_5_hi(c0_hi, c0_lo, | |
21263 | c1_hi, c1_lo, | |
21264 | c2_hi, c2_lo, | |
21265 | c3_hi, c3_lo, | |
21266 | c4_hi, c4_lo); | |
21267 | var T1_lo = sum64_5_lo(c0_hi, c0_lo, | |
21268 | c1_hi, c1_lo, | |
21269 | c2_hi, c2_lo, | |
21270 | c3_hi, c3_lo, | |
21271 | c4_hi, c4_lo); | |
21272 | ||
21273 | var c0_hi = s0_512_hi(ah, al); | |
21274 | var c0_lo = s0_512_lo(ah, al); | |
21275 | var c1_hi = maj64_hi(ah, al, bh, bl, ch, cl); | |
21276 | var c1_lo = maj64_lo(ah, al, bh, bl, ch, cl); | |
21277 | ||
21278 | var T2_hi = sum64_hi(c0_hi, c0_lo, c1_hi, c1_lo); | |
21279 | var T2_lo = sum64_lo(c0_hi, c0_lo, c1_hi, c1_lo); | |
21280 | ||
21281 | hh = gh; | |
21282 | hl = gl; | |
21283 | ||
21284 | gh = fh; | |
21285 | gl = fl; | |
21286 | ||
21287 | fh = eh; | |
21288 | fl = el; | |
21289 | ||
21290 | eh = sum64_hi(dh, dl, T1_hi, T1_lo); | |
21291 | el = sum64_lo(dl, dl, T1_hi, T1_lo); | |
21292 | ||
21293 | dh = ch; | |
21294 | dl = cl; | |
21295 | ||
21296 | ch = bh; | |
21297 | cl = bl; | |
21298 | ||
21299 | bh = ah; | |
21300 | bl = al; | |
21301 | ||
21302 | ah = sum64_hi(T1_hi, T1_lo, T2_hi, T2_lo); | |
21303 | al = sum64_lo(T1_hi, T1_lo, T2_hi, T2_lo); | |
21304 | } | |
21305 | ||
21306 | sum64(this.h, 0, ah, al); | |
21307 | sum64(this.h, 2, bh, bl); | |
21308 | sum64(this.h, 4, ch, cl); | |
21309 | sum64(this.h, 6, dh, dl); | |
21310 | sum64(this.h, 8, eh, el); | |
21311 | sum64(this.h, 10, fh, fl); | |
21312 | sum64(this.h, 12, gh, gl); | |
21313 | sum64(this.h, 14, hh, hl); | |
21314 | }; | |
a0091a40 | 21315 | |
9f59e99b IC |
21316 | SHA512.prototype._digest = function digest(enc) { |
21317 | if (enc === 'hex') | |
21318 | return utils.toHex32(this.h, 'big'); | |
21319 | else | |
21320 | return utils.split32(this.h, 'big'); | |
21321 | }; | |
a0091a40 | 21322 | |
9f59e99b IC |
21323 | function SHA384() { |
21324 | if (!(this instanceof SHA384)) | |
21325 | return new SHA384(); | |
21326 | ||
21327 | SHA512.call(this); | |
21328 | this.h = [ 0xcbbb9d5d, 0xc1059ed8, | |
21329 | 0x629a292a, 0x367cd507, | |
21330 | 0x9159015a, 0x3070dd17, | |
21331 | 0x152fecd8, 0xf70e5939, | |
21332 | 0x67332667, 0xffc00b31, | |
21333 | 0x8eb44a87, 0x68581511, | |
21334 | 0xdb0c2e0d, 0x64f98fa7, | |
21335 | 0x47b5481d, 0xbefa4fa4 ]; | |
21336 | } | |
21337 | utils.inherits(SHA384, SHA512); | |
21338 | exports.sha384 = SHA384; | |
21339 | ||
21340 | SHA384.blockSize = 1024; | |
21341 | SHA384.outSize = 384; | |
21342 | SHA384.hmacStrength = 192; | |
21343 | SHA384.padLength = 128; | |
21344 | ||
21345 | SHA384.prototype._digest = function digest(enc) { | |
21346 | if (enc === 'hex') | |
21347 | return utils.toHex32(this.h.slice(0, 12), 'big'); | |
21348 | else | |
21349 | return utils.split32(this.h.slice(0, 12), 'big'); | |
21350 | }; | |
a0091a40 | 21351 | |
9f59e99b IC |
21352 | function SHA1() { |
21353 | if (!(this instanceof SHA1)) | |
21354 | return new SHA1(); | |
a0091a40 | 21355 | |
9f59e99b IC |
21356 | BlockHash.call(this); |
21357 | this.h = [ 0x67452301, 0xefcdab89, 0x98badcfe, | |
21358 | 0x10325476, 0xc3d2e1f0 ]; | |
21359 | this.W = new Array(80); | |
21360 | } | |
a0091a40 | 21361 | |
9f59e99b IC |
21362 | utils.inherits(SHA1, BlockHash); |
21363 | exports.sha1 = SHA1; | |
a0091a40 | 21364 | |
9f59e99b IC |
21365 | SHA1.blockSize = 512; |
21366 | SHA1.outSize = 160; | |
21367 | SHA1.hmacStrength = 80; | |
21368 | SHA1.padLength = 64; | |
a0091a40 | 21369 | |
9f59e99b IC |
21370 | SHA1.prototype._update = function _update(msg, start) { |
21371 | var W = this.W; | |
a0091a40 | 21372 | |
9f59e99b IC |
21373 | for (var i = 0; i < 16; i++) |
21374 | W[i] = msg[start + i]; | |
a0091a40 | 21375 | |
9f59e99b IC |
21376 | for(; i < W.length; i++) |
21377 | W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1); | |
a0091a40 | 21378 | |
9f59e99b IC |
21379 | var a = this.h[0]; |
21380 | var b = this.h[1]; | |
21381 | var c = this.h[2]; | |
21382 | var d = this.h[3]; | |
21383 | var e = this.h[4]; | |
a0091a40 | 21384 | |
9f59e99b IC |
21385 | for (var i = 0; i < W.length; i++) { |
21386 | var s = ~~(i / 20); | |
21387 | var t = sum32_5(rotl32(a, 5), ft_1(s, b, c, d), e, W[i], sha1_K[s]); | |
21388 | e = d; | |
21389 | d = c; | |
21390 | c = rotl32(b, 30); | |
21391 | b = a; | |
21392 | a = t; | |
a0091a40 | 21393 | } |
a0091a40 | 21394 | |
9f59e99b IC |
21395 | this.h[0] = sum32(this.h[0], a); |
21396 | this.h[1] = sum32(this.h[1], b); | |
21397 | this.h[2] = sum32(this.h[2], c); | |
21398 | this.h[3] = sum32(this.h[3], d); | |
21399 | this.h[4] = sum32(this.h[4], e); | |
21400 | }; | |
21401 | ||
21402 | SHA1.prototype._digest = function digest(enc) { | |
21403 | if (enc === 'hex') | |
21404 | return utils.toHex32(this.h, 'big'); | |
21405 | else | |
21406 | return utils.split32(this.h, 'big'); | |
21407 | }; | |
21408 | ||
21409 | function ch32(x, y, z) { | |
21410 | return (x & y) ^ ((~x) & z); | |
a0091a40 IC |
21411 | } |
21412 | ||
9f59e99b IC |
21413 | function maj32(x, y, z) { |
21414 | return (x & y) ^ (x & z) ^ (y & z); | |
21415 | } | |
a0091a40 | 21416 | |
9f59e99b IC |
21417 | function p32(x, y, z) { |
21418 | return x ^ y ^ z; | |
21419 | } | |
a0091a40 | 21420 | |
9f59e99b IC |
21421 | function s0_256(x) { |
21422 | return rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22); | |
21423 | } | |
a0091a40 | 21424 | |
9f59e99b IC |
21425 | function s1_256(x) { |
21426 | return rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25); | |
21427 | } | |
a0091a40 | 21428 | |
9f59e99b IC |
21429 | function g0_256(x) { |
21430 | return rotr32(x, 7) ^ rotr32(x, 18) ^ (x >>> 3); | |
a0091a40 IC |
21431 | } |
21432 | ||
9f59e99b IC |
21433 | function g1_256(x) { |
21434 | return rotr32(x, 17) ^ rotr32(x, 19) ^ (x >>> 10); | |
21435 | } | |
a0091a40 | 21436 | |
9f59e99b IC |
21437 | function ft_1(s, x, y, z) { |
21438 | if (s === 0) | |
21439 | return ch32(x, y, z); | |
21440 | if (s === 1 || s === 3) | |
21441 | return p32(x, y, z); | |
21442 | if (s === 2) | |
21443 | return maj32(x, y, z); | |
a0091a40 IC |
21444 | } |
21445 | ||
9f59e99b IC |
21446 | function ch64_hi(xh, xl, yh, yl, zh, zl) { |
21447 | var r = (xh & yh) ^ ((~xh) & zh); | |
21448 | if (r < 0) | |
21449 | r += 0x100000000; | |
21450 | return r; | |
21451 | } | |
a0091a40 | 21452 | |
9f59e99b IC |
21453 | function ch64_lo(xh, xl, yh, yl, zh, zl) { |
21454 | var r = (xl & yl) ^ ((~xl) & zl); | |
21455 | if (r < 0) | |
21456 | r += 0x100000000; | |
21457 | return r; | |
21458 | } | |
a0091a40 | 21459 | |
9f59e99b IC |
21460 | function maj64_hi(xh, xl, yh, yl, zh, zl) { |
21461 | var r = (xh & yh) ^ (xh & zh) ^ (yh & zh); | |
21462 | if (r < 0) | |
21463 | r += 0x100000000; | |
21464 | return r; | |
21465 | } | |
a0091a40 | 21466 | |
9f59e99b IC |
21467 | function maj64_lo(xh, xl, yh, yl, zh, zl) { |
21468 | var r = (xl & yl) ^ (xl & zl) ^ (yl & zl); | |
21469 | if (r < 0) | |
21470 | r += 0x100000000; | |
21471 | return r; | |
21472 | } | |
a0091a40 | 21473 | |
9f59e99b IC |
21474 | function s0_512_hi(xh, xl) { |
21475 | var c0_hi = rotr64_hi(xh, xl, 28); | |
21476 | var c1_hi = rotr64_hi(xl, xh, 2); // 34 | |
21477 | var c2_hi = rotr64_hi(xl, xh, 7); // 39 | |
a0091a40 | 21478 | |
9f59e99b IC |
21479 | var r = c0_hi ^ c1_hi ^ c2_hi; |
21480 | if (r < 0) | |
21481 | r += 0x100000000; | |
21482 | return r; | |
21483 | } | |
a0091a40 | 21484 | |
9f59e99b IC |
21485 | function s0_512_lo(xh, xl) { |
21486 | var c0_lo = rotr64_lo(xh, xl, 28); | |
21487 | var c1_lo = rotr64_lo(xl, xh, 2); // 34 | |
21488 | var c2_lo = rotr64_lo(xl, xh, 7); // 39 | |
a0091a40 | 21489 | |
9f59e99b IC |
21490 | var r = c0_lo ^ c1_lo ^ c2_lo; |
21491 | if (r < 0) | |
21492 | r += 0x100000000; | |
21493 | return r; | |
a0091a40 IC |
21494 | } |
21495 | ||
9f59e99b IC |
21496 | function s1_512_hi(xh, xl) { |
21497 | var c0_hi = rotr64_hi(xh, xl, 14); | |
21498 | var c1_hi = rotr64_hi(xh, xl, 18); | |
21499 | var c2_hi = rotr64_hi(xl, xh, 9); // 41 | |
a0091a40 | 21500 | |
9f59e99b IC |
21501 | var r = c0_hi ^ c1_hi ^ c2_hi; |
21502 | if (r < 0) | |
21503 | r += 0x100000000; | |
21504 | return r; | |
21505 | } | |
a0091a40 | 21506 | |
9f59e99b IC |
21507 | function s1_512_lo(xh, xl) { |
21508 | var c0_lo = rotr64_lo(xh, xl, 14); | |
21509 | var c1_lo = rotr64_lo(xh, xl, 18); | |
21510 | var c2_lo = rotr64_lo(xl, xh, 9); // 41 | |
a0091a40 | 21511 | |
9f59e99b IC |
21512 | var r = c0_lo ^ c1_lo ^ c2_lo; |
21513 | if (r < 0) | |
21514 | r += 0x100000000; | |
21515 | return r; | |
21516 | } | |
a0091a40 | 21517 | |
9f59e99b IC |
21518 | function g0_512_hi(xh, xl) { |
21519 | var c0_hi = rotr64_hi(xh, xl, 1); | |
21520 | var c1_hi = rotr64_hi(xh, xl, 8); | |
21521 | var c2_hi = shr64_hi(xh, xl, 7); | |
a0091a40 | 21522 | |
9f59e99b IC |
21523 | var r = c0_hi ^ c1_hi ^ c2_hi; |
21524 | if (r < 0) | |
21525 | r += 0x100000000; | |
21526 | return r; | |
21527 | } | |
a0091a40 | 21528 | |
9f59e99b IC |
21529 | function g0_512_lo(xh, xl) { |
21530 | var c0_lo = rotr64_lo(xh, xl, 1); | |
21531 | var c1_lo = rotr64_lo(xh, xl, 8); | |
21532 | var c2_lo = shr64_lo(xh, xl, 7); | |
21533 | ||
21534 | var r = c0_lo ^ c1_lo ^ c2_lo; | |
21535 | if (r < 0) | |
21536 | r += 0x100000000; | |
21537 | return r; | |
a0091a40 IC |
21538 | } |
21539 | ||
9f59e99b IC |
21540 | function g1_512_hi(xh, xl) { |
21541 | var c0_hi = rotr64_hi(xh, xl, 19); | |
21542 | var c1_hi = rotr64_hi(xl, xh, 29); // 61 | |
21543 | var c2_hi = shr64_hi(xh, xl, 6); | |
a0091a40 | 21544 | |
9f59e99b IC |
21545 | var r = c0_hi ^ c1_hi ^ c2_hi; |
21546 | if (r < 0) | |
21547 | r += 0x100000000; | |
21548 | return r; | |
21549 | } | |
a0091a40 | 21550 | |
9f59e99b IC |
21551 | function g1_512_lo(xh, xl) { |
21552 | var c0_lo = rotr64_lo(xh, xl, 19); | |
21553 | var c1_lo = rotr64_lo(xl, xh, 29); // 61 | |
21554 | var c2_lo = shr64_lo(xh, xl, 6); | |
a0091a40 | 21555 | |
9f59e99b IC |
21556 | var r = c0_lo ^ c1_lo ^ c2_lo; |
21557 | if (r < 0) | |
21558 | r += 0x100000000; | |
21559 | return r; | |
21560 | } | |
a0091a40 | 21561 | |
9f59e99b IC |
21562 | },{"../hash":114}],119:[function(require,module,exports){ |
21563 | var utils = exports; | |
21564 | var inherits = require('inherits'); | |
a0091a40 | 21565 | |
9f59e99b IC |
21566 | function toArray(msg, enc) { |
21567 | if (Array.isArray(msg)) | |
21568 | return msg.slice(); | |
21569 | if (!msg) | |
21570 | return []; | |
21571 | var res = []; | |
21572 | if (typeof msg === 'string') { | |
21573 | if (!enc) { | |
21574 | for (var i = 0; i < msg.length; i++) { | |
21575 | var c = msg.charCodeAt(i); | |
21576 | var hi = c >> 8; | |
21577 | var lo = c & 0xff; | |
21578 | if (hi) | |
21579 | res.push(hi, lo); | |
21580 | else | |
21581 | res.push(lo); | |
21582 | } | |
21583 | } else if (enc === 'hex') { | |
21584 | msg = msg.replace(/[^a-z0-9]+/ig, ''); | |
21585 | if (msg.length % 2 !== 0) | |
21586 | msg = '0' + msg; | |
21587 | for (var i = 0; i < msg.length; i += 2) | |
21588 | res.push(parseInt(msg[i] + msg[i + 1], 16)); | |
a0091a40 | 21589 | } |
9f59e99b IC |
21590 | } else { |
21591 | for (var i = 0; i < msg.length; i++) | |
21592 | res[i] = msg[i] | 0; | |
a0091a40 | 21593 | } |
9f59e99b | 21594 | return res; |
a0091a40 | 21595 | } |
9f59e99b | 21596 | utils.toArray = toArray; |
a0091a40 | 21597 | |
9f59e99b IC |
21598 | function toHex(msg) { |
21599 | var res = ''; | |
21600 | for (var i = 0; i < msg.length; i++) | |
21601 | res += zero2(msg[i].toString(16)); | |
21602 | return res; | |
21603 | } | |
21604 | utils.toHex = toHex; | |
a0091a40 | 21605 | |
9f59e99b IC |
21606 | function htonl(w) { |
21607 | var res = (w >>> 24) | | |
21608 | ((w >>> 8) & 0xff00) | | |
21609 | ((w << 8) & 0xff0000) | | |
21610 | ((w & 0xff) << 24); | |
21611 | return res >>> 0; | |
21612 | } | |
21613 | utils.htonl = htonl; | |
a0091a40 | 21614 | |
9f59e99b IC |
21615 | function toHex32(msg, endian) { |
21616 | var res = ''; | |
21617 | for (var i = 0; i < msg.length; i++) { | |
21618 | var w = msg[i]; | |
21619 | if (endian === 'little') | |
21620 | w = htonl(w); | |
21621 | res += zero8(w.toString(16)); | |
21622 | } | |
21623 | return res; | |
21624 | } | |
21625 | utils.toHex32 = toHex32; | |
a0091a40 | 21626 | |
9f59e99b IC |
21627 | function zero2(word) { |
21628 | if (word.length === 1) | |
21629 | return '0' + word; | |
21630 | else | |
21631 | return word; | |
21632 | } | |
21633 | utils.zero2 = zero2; | |
21634 | ||
21635 | function zero8(word) { | |
21636 | if (word.length === 7) | |
21637 | return '0' + word; | |
21638 | else if (word.length === 6) | |
21639 | return '00' + word; | |
21640 | else if (word.length === 5) | |
21641 | return '000' + word; | |
21642 | else if (word.length === 4) | |
21643 | return '0000' + word; | |
21644 | else if (word.length === 3) | |
21645 | return '00000' + word; | |
21646 | else if (word.length === 2) | |
21647 | return '000000' + word; | |
21648 | else if (word.length === 1) | |
21649 | return '0000000' + word; | |
21650 | else | |
21651 | return word; | |
21652 | } | |
21653 | utils.zero8 = zero8; | |
21654 | ||
21655 | function join32(msg, start, end, endian) { | |
21656 | var len = end - start; | |
21657 | assert(len % 4 === 0); | |
21658 | var res = new Array(len / 4); | |
21659 | for (var i = 0, k = start; i < res.length; i++, k += 4) { | |
21660 | var w; | |
21661 | if (endian === 'big') | |
21662 | w = (msg[k] << 24) | (msg[k + 1] << 16) | (msg[k + 2] << 8) | msg[k + 3]; | |
21663 | else | |
21664 | w = (msg[k + 3] << 24) | (msg[k + 2] << 16) | (msg[k + 1] << 8) | msg[k]; | |
21665 | res[i] = w >>> 0; | |
21666 | } | |
21667 | return res; | |
21668 | } | |
21669 | utils.join32 = join32; | |
21670 | ||
21671 | function split32(msg, endian) { | |
21672 | var res = new Array(msg.length * 4); | |
21673 | for (var i = 0, k = 0; i < msg.length; i++, k += 4) { | |
21674 | var m = msg[i]; | |
21675 | if (endian === 'big') { | |
21676 | res[k] = m >>> 24; | |
21677 | res[k + 1] = (m >>> 16) & 0xff; | |
21678 | res[k + 2] = (m >>> 8) & 0xff; | |
21679 | res[k + 3] = m & 0xff; | |
21680 | } else { | |
21681 | res[k + 3] = m >>> 24; | |
21682 | res[k + 2] = (m >>> 16) & 0xff; | |
21683 | res[k + 1] = (m >>> 8) & 0xff; | |
21684 | res[k] = m & 0xff; | |
a0091a40 | 21685 | } |
a0091a40 | 21686 | } |
9f59e99b | 21687 | return res; |
a0091a40 | 21688 | } |
9f59e99b | 21689 | utils.split32 = split32; |
a0091a40 | 21690 | |
9f59e99b IC |
21691 | function rotr32(w, b) { |
21692 | return (w >>> b) | (w << (32 - b)); | |
21693 | } | |
21694 | utils.rotr32 = rotr32; | |
a0091a40 | 21695 | |
9f59e99b IC |
21696 | function rotl32(w, b) { |
21697 | return (w << b) | (w >>> (32 - b)); | |
21698 | } | |
21699 | utils.rotl32 = rotl32; | |
a0091a40 | 21700 | |
9f59e99b IC |
21701 | function sum32(a, b) { |
21702 | return (a + b) >>> 0; | |
21703 | } | |
21704 | utils.sum32 = sum32; | |
a0091a40 | 21705 | |
9f59e99b IC |
21706 | function sum32_3(a, b, c) { |
21707 | return (a + b + c) >>> 0; | |
21708 | } | |
21709 | utils.sum32_3 = sum32_3; | |
a0091a40 | 21710 | |
9f59e99b IC |
21711 | function sum32_4(a, b, c, d) { |
21712 | return (a + b + c + d) >>> 0; | |
21713 | } | |
21714 | utils.sum32_4 = sum32_4; | |
a0091a40 | 21715 | |
9f59e99b IC |
21716 | function sum32_5(a, b, c, d, e) { |
21717 | return (a + b + c + d + e) >>> 0; | |
21718 | } | |
21719 | utils.sum32_5 = sum32_5; | |
a0091a40 | 21720 | |
9f59e99b IC |
21721 | function assert(cond, msg) { |
21722 | if (!cond) | |
21723 | throw new Error(msg || 'Assertion failed'); | |
a0091a40 | 21724 | } |
9f59e99b | 21725 | utils.assert = assert; |
a0091a40 | 21726 | |
9f59e99b | 21727 | utils.inherits = inherits; |
a0091a40 | 21728 | |
9f59e99b IC |
21729 | function sum64(buf, pos, ah, al) { |
21730 | var bh = buf[pos]; | |
21731 | var bl = buf[pos + 1]; | |
a0091a40 | 21732 | |
9f59e99b IC |
21733 | var lo = (al + bl) >>> 0; |
21734 | var hi = (lo < al ? 1 : 0) + ah + bh; | |
21735 | buf[pos] = hi >>> 0; | |
21736 | buf[pos + 1] = lo; | |
21737 | } | |
21738 | exports.sum64 = sum64; | |
a0091a40 | 21739 | |
9f59e99b IC |
21740 | function sum64_hi(ah, al, bh, bl) { |
21741 | var lo = (al + bl) >>> 0; | |
21742 | var hi = (lo < al ? 1 : 0) + ah + bh; | |
21743 | return hi >>> 0; | |
21744 | }; | |
21745 | exports.sum64_hi = sum64_hi; | |
a0091a40 | 21746 | |
9f59e99b IC |
21747 | function sum64_lo(ah, al, bh, bl) { |
21748 | var lo = al + bl; | |
21749 | return lo >>> 0; | |
21750 | }; | |
21751 | exports.sum64_lo = sum64_lo; | |
21752 | ||
21753 | function sum64_4_hi(ah, al, bh, bl, ch, cl, dh, dl) { | |
21754 | var carry = 0; | |
21755 | var lo = al; | |
21756 | lo = (lo + bl) >>> 0; | |
21757 | carry += lo < al ? 1 : 0; | |
21758 | lo = (lo + cl) >>> 0; | |
21759 | carry += lo < cl ? 1 : 0; | |
21760 | lo = (lo + dl) >>> 0; | |
21761 | carry += lo < dl ? 1 : 0; | |
21762 | ||
21763 | var hi = ah + bh + ch + dh + carry; | |
21764 | return hi >>> 0; | |
21765 | }; | |
21766 | exports.sum64_4_hi = sum64_4_hi; | |
a0091a40 | 21767 | |
9f59e99b IC |
21768 | function sum64_4_lo(ah, al, bh, bl, ch, cl, dh, dl) { |
21769 | var lo = al + bl + cl + dl; | |
21770 | return lo >>> 0; | |
21771 | }; | |
21772 | exports.sum64_4_lo = sum64_4_lo; | |
21773 | ||
21774 | function sum64_5_hi(ah, al, bh, bl, ch, cl, dh, dl, eh, el) { | |
21775 | var carry = 0; | |
21776 | var lo = al; | |
21777 | lo = (lo + bl) >>> 0; | |
21778 | carry += lo < al ? 1 : 0; | |
21779 | lo = (lo + cl) >>> 0; | |
21780 | carry += lo < cl ? 1 : 0; | |
21781 | lo = (lo + dl) >>> 0; | |
21782 | carry += lo < dl ? 1 : 0; | |
21783 | lo = (lo + el) >>> 0; | |
21784 | carry += lo < el ? 1 : 0; | |
21785 | ||
21786 | var hi = ah + bh + ch + dh + eh + carry; | |
21787 | return hi >>> 0; | |
21788 | }; | |
21789 | exports.sum64_5_hi = sum64_5_hi; | |
a0091a40 | 21790 | |
9f59e99b IC |
21791 | function sum64_5_lo(ah, al, bh, bl, ch, cl, dh, dl, eh, el) { |
21792 | var lo = al + bl + cl + dl + el; | |
a0091a40 | 21793 | |
9f59e99b IC |
21794 | return lo >>> 0; |
21795 | }; | |
21796 | exports.sum64_5_lo = sum64_5_lo; | |
a0091a40 | 21797 | |
9f59e99b IC |
21798 | function rotr64_hi(ah, al, num) { |
21799 | var r = (al << (32 - num)) | (ah >>> num); | |
21800 | return r >>> 0; | |
21801 | }; | |
21802 | exports.rotr64_hi = rotr64_hi; | |
a0091a40 | 21803 | |
9f59e99b IC |
21804 | function rotr64_lo(ah, al, num) { |
21805 | var r = (ah << (32 - num)) | (al >>> num); | |
21806 | return r >>> 0; | |
21807 | }; | |
21808 | exports.rotr64_lo = rotr64_lo; | |
a0091a40 | 21809 | |
9f59e99b IC |
21810 | function shr64_hi(ah, al, num) { |
21811 | return ah >>> num; | |
21812 | }; | |
21813 | exports.shr64_hi = shr64_hi; | |
a0091a40 | 21814 | |
9f59e99b IC |
21815 | function shr64_lo(ah, al, num) { |
21816 | var r = (ah << (32 - num)) | (al >>> num); | |
21817 | return r >>> 0; | |
21818 | }; | |
21819 | exports.shr64_lo = shr64_lo; | |
a0091a40 | 21820 | |
9f59e99b IC |
21821 | },{"inherits":121}],120:[function(require,module,exports){ |
21822 | 'use strict'; | |
a0091a40 | 21823 | |
9f59e99b IC |
21824 | var hash = require('hash.js'); |
21825 | var utils = require('minimalistic-crypto-utils'); | |
21826 | var assert = require('minimalistic-assert'); | |
a0091a40 | 21827 | |
9f59e99b IC |
21828 | function HmacDRBG(options) { |
21829 | if (!(this instanceof HmacDRBG)) | |
21830 | return new HmacDRBG(options); | |
21831 | this.hash = options.hash; | |
21832 | this.predResist = !!options.predResist; | |
a0091a40 | 21833 | |
9f59e99b IC |
21834 | this.outLen = this.hash.outSize; |
21835 | this.minEntropy = options.minEntropy || this.hash.hmacStrength; | |
a0091a40 | 21836 | |
9f59e99b IC |
21837 | this._reseed = null; |
21838 | this.reseedInterval = null; | |
21839 | this.K = null; | |
21840 | this.V = null; | |
a0091a40 | 21841 | |
9f59e99b IC |
21842 | var entropy = utils.toArray(options.entropy, options.entropyEnc || 'hex'); |
21843 | var nonce = utils.toArray(options.nonce, options.nonceEnc || 'hex'); | |
21844 | var pers = utils.toArray(options.pers, options.persEnc || 'hex'); | |
21845 | assert(entropy.length >= (this.minEntropy / 8), | |
21846 | 'Not enough entropy. Minimum is: ' + this.minEntropy + ' bits'); | |
21847 | this._init(entropy, nonce, pers); | |
a0091a40 | 21848 | } |
9f59e99b | 21849 | module.exports = HmacDRBG; |
a0091a40 | 21850 | |
9f59e99b IC |
21851 | HmacDRBG.prototype._init = function init(entropy, nonce, pers) { |
21852 | var seed = entropy.concat(nonce).concat(pers); | |
a0091a40 | 21853 | |
9f59e99b IC |
21854 | this.K = new Array(this.outLen / 8); |
21855 | this.V = new Array(this.outLen / 8); | |
21856 | for (var i = 0; i < this.V.length; i++) { | |
21857 | this.K[i] = 0x00; | |
21858 | this.V[i] = 0x01; | |
a0091a40 | 21859 | } |
a0091a40 | 21860 | |
9f59e99b IC |
21861 | this._update(seed); |
21862 | this._reseed = 1; | |
21863 | this.reseedInterval = 0x1000000000000; // 2^48 | |
21864 | }; | |
21865 | ||
21866 | HmacDRBG.prototype._hmac = function hmac() { | |
21867 | return new hash.hmac(this.hash, this.K); | |
21868 | }; | |
21869 | ||
21870 | HmacDRBG.prototype._update = function update(seed) { | |
21871 | var kmac = this._hmac() | |
21872 | .update(this.V) | |
21873 | .update([ 0x00 ]); | |
21874 | if (seed) | |
21875 | kmac = kmac.update(seed); | |
21876 | this.K = kmac.digest(); | |
21877 | this.V = this._hmac().update(this.V).digest(); | |
21878 | if (!seed) | |
21879 | return; | |
21880 | ||
21881 | this.K = this._hmac() | |
21882 | .update(this.V) | |
21883 | .update([ 0x01 ]) | |
21884 | .update(seed) | |
21885 | .digest(); | |
21886 | this.V = this._hmac().update(this.V).digest(); | |
21887 | }; | |
21888 | ||
21889 | HmacDRBG.prototype.reseed = function reseed(entropy, entropyEnc, add, addEnc) { | |
21890 | // Optional entropy enc | |
21891 | if (typeof entropyEnc !== 'string') { | |
21892 | addEnc = add; | |
21893 | add = entropyEnc; | |
21894 | entropyEnc = null; | |
a0091a40 IC |
21895 | } |
21896 | ||
9f59e99b IC |
21897 | entropy = utils.toArray(entropy, entropyEnc); |
21898 | add = utils.toArray(add, addEnc); | |
a0091a40 | 21899 | |
9f59e99b IC |
21900 | assert(entropy.length >= (this.minEntropy / 8), |
21901 | 'Not enough entropy. Minimum is: ' + this.minEntropy + ' bits'); | |
a0091a40 | 21902 | |
9f59e99b IC |
21903 | this._update(entropy.concat(add || [])); |
21904 | this._reseed = 1; | |
21905 | }; | |
a0091a40 | 21906 | |
9f59e99b IC |
21907 | HmacDRBG.prototype.generate = function generate(len, enc, add, addEnc) { |
21908 | if (this._reseed > this.reseedInterval) | |
21909 | throw new Error('Reseed is required'); | |
a0091a40 | 21910 | |
9f59e99b IC |
21911 | // Optional encoding |
21912 | if (typeof enc !== 'string') { | |
21913 | addEnc = add; | |
21914 | add = enc; | |
21915 | enc = null; | |
21916 | } | |
a0091a40 | 21917 | |
9f59e99b IC |
21918 | // Optional additional data |
21919 | if (add) { | |
21920 | add = utils.toArray(add, addEnc || 'hex'); | |
21921 | this._update(add); | |
21922 | } | |
a0091a40 | 21923 | |
9f59e99b IC |
21924 | var temp = []; |
21925 | while (temp.length < len) { | |
21926 | this.V = this._hmac().update(this.V).digest(); | |
21927 | temp = temp.concat(this.V); | |
21928 | } | |
21929 | ||
21930 | var res = temp.slice(0, len); | |
21931 | this._update(add); | |
21932 | this._reseed++; | |
21933 | return utils.encode(res, enc); | |
21934 | }; | |
21935 | ||
21936 | },{"hash.js":114,"minimalistic-assert":123,"minimalistic-crypto-utils":124}],121:[function(require,module,exports){ | |
21937 | arguments[4][9][0].apply(exports,arguments) | |
21938 | },{"dup":9}],122:[function(require,module,exports){ | |
a0091a40 IC |
21939 | (function (Buffer){ |
21940 | // constant-space merkle root calculation algorithm | |
21941 | module.exports = function fastRoot (values, digestFn) { | |
21942 | if (!Array.isArray(values)) throw TypeError('Expected values Array') | |
21943 | if (typeof digestFn !== 'function') throw TypeError('Expected digest Function') | |
21944 | ||
21945 | var length = values.length | |
21946 | var results = values.concat() | |
21947 | ||
21948 | while (length > 1) { | |
21949 | var j = 0 | |
21950 | ||
21951 | for (var i = 0; i < length; i += 2, ++j) { | |
21952 | var left = results[i] | |
21953 | var right = i + 1 === length ? left : results[i + 1] | |
21954 | var data = Buffer.concat([left, right]) | |
21955 | ||
21956 | results[j] = digestFn(data) | |
21957 | } | |
21958 | ||
21959 | length = j | |
21960 | } | |
21961 | ||
21962 | return results[0] | |
21963 | } | |
21964 | ||
21965 | }).call(this,require("buffer").Buffer) | |
9f59e99b IC |
21966 | },{"buffer":5}],123:[function(require,module,exports){ |
21967 | module.exports = assert; | |
21968 | ||
21969 | function assert(val, msg) { | |
21970 | if (!val) | |
21971 | throw new Error(msg || 'Assertion failed'); | |
21972 | } | |
21973 | ||
21974 | assert.equal = function assertEqual(l, r, msg) { | |
21975 | if (l != r) | |
21976 | throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r)); | |
21977 | }; | |
21978 | ||
21979 | },{}],124:[function(require,module,exports){ | |
21980 | 'use strict'; | |
21981 | ||
21982 | var utils = exports; | |
21983 | ||
21984 | function toArray(msg, enc) { | |
21985 | if (Array.isArray(msg)) | |
21986 | return msg.slice(); | |
21987 | if (!msg) | |
21988 | return []; | |
21989 | var res = []; | |
21990 | if (typeof msg !== 'string') { | |
21991 | for (var i = 0; i < msg.length; i++) | |
21992 | res[i] = msg[i] | 0; | |
21993 | return res; | |
21994 | } | |
21995 | if (enc === 'hex') { | |
21996 | msg = msg.replace(/[^a-z0-9]+/ig, ''); | |
21997 | if (msg.length % 2 !== 0) | |
21998 | msg = '0' + msg; | |
21999 | for (var i = 0; i < msg.length; i += 2) | |
22000 | res.push(parseInt(msg[i] + msg[i + 1], 16)); | |
22001 | } else { | |
22002 | for (var i = 0; i < msg.length; i++) { | |
22003 | var c = msg.charCodeAt(i); | |
22004 | var hi = c >> 8; | |
22005 | var lo = c & 0xff; | |
22006 | if (hi) | |
22007 | res.push(hi, lo); | |
22008 | else | |
22009 | res.push(lo); | |
22010 | } | |
22011 | } | |
22012 | return res; | |
22013 | } | |
22014 | utils.toArray = toArray; | |
22015 | ||
22016 | function zero2(word) { | |
22017 | if (word.length === 1) | |
22018 | return '0' + word; | |
22019 | else | |
22020 | return word; | |
22021 | } | |
22022 | utils.zero2 = zero2; | |
22023 | ||
22024 | function toHex(msg) { | |
22025 | var res = ''; | |
22026 | for (var i = 0; i < msg.length; i++) | |
22027 | res += zero2(msg[i].toString(16)); | |
22028 | return res; | |
22029 | } | |
22030 | utils.toHex = toHex; | |
22031 | ||
22032 | utils.encode = function encode(arr, enc) { | |
22033 | if (enc === 'hex') | |
22034 | return toHex(arr); | |
22035 | else | |
22036 | return arr; | |
22037 | }; | |
22038 | ||
22039 | },{}],125:[function(require,module,exports){ | |
a0091a40 IC |
22040 | var OPS = require('bitcoin-ops') |
22041 | ||
22042 | function encodingLength (i) { | |
22043 | return i < OPS.OP_PUSHDATA1 ? 1 | |
22044 | : i <= 0xff ? 2 | |
22045 | : i <= 0xffff ? 3 | |
22046 | : 5 | |
22047 | } | |
22048 | ||
22049 | function encode (buffer, number, offset) { | |
22050 | var size = encodingLength(number) | |
22051 | ||
22052 | // ~6 bit | |
22053 | if (size === 1) { | |
22054 | buffer.writeUInt8(number, offset) | |
22055 | ||
22056 | // 8 bit | |
22057 | } else if (size === 2) { | |
22058 | buffer.writeUInt8(OPS.OP_PUSHDATA1, offset) | |
22059 | buffer.writeUInt8(number, offset + 1) | |
22060 | ||
22061 | // 16 bit | |
22062 | } else if (size === 3) { | |
22063 | buffer.writeUInt8(OPS.OP_PUSHDATA2, offset) | |
22064 | buffer.writeUInt16LE(number, offset + 1) | |
22065 | ||
22066 | // 32 bit | |
22067 | } else { | |
22068 | buffer.writeUInt8(OPS.OP_PUSHDATA4, offset) | |
22069 | buffer.writeUInt32LE(number, offset + 1) | |
22070 | } | |
22071 | ||
22072 | return size | |
22073 | } | |
22074 | ||
22075 | function decode (buffer, offset) { | |
22076 | var opcode = buffer.readUInt8(offset) | |
22077 | var number, size | |
22078 | ||
22079 | // ~6 bit | |
22080 | if (opcode < OPS.OP_PUSHDATA1) { | |
22081 | number = opcode | |
22082 | size = 1 | |
22083 | ||
22084 | // 8 bit | |
22085 | } else if (opcode === OPS.OP_PUSHDATA1) { | |
22086 | if (offset + 2 > buffer.length) return null | |
22087 | number = buffer.readUInt8(offset + 1) | |
22088 | size = 2 | |
22089 | ||
22090 | // 16 bit | |
22091 | } else if (opcode === OPS.OP_PUSHDATA2) { | |
22092 | if (offset + 3 > buffer.length) return null | |
22093 | number = buffer.readUInt16LE(offset + 1) | |
22094 | size = 3 | |
22095 | ||
22096 | // 32 bit | |
22097 | } else { | |
22098 | if (offset + 5 > buffer.length) return null | |
22099 | if (opcode !== OPS.OP_PUSHDATA4) throw new Error('Unexpected opcode') | |
22100 | ||
22101 | number = buffer.readUInt32LE(offset + 1) | |
22102 | size = 5 | |
22103 | } | |
22104 | ||
22105 | return { | |
22106 | opcode: opcode, | |
22107 | number: number, | |
22108 | size: size | |
22109 | } | |
22110 | } | |
22111 | ||
22112 | module.exports = { | |
22113 | encodingLength: encodingLength, | |
22114 | encode: encode, | |
22115 | decode: decode | |
22116 | } | |
22117 | ||
9f59e99b | 22118 | },{"bitcoin-ops":42}],126:[function(require,module,exports){ |
a0091a40 IC |
22119 | (function (process,global){ |
22120 | 'use strict' | |
22121 | ||
22122 | function oldBrowser () { | |
22123 | throw new Error('secure random number generation not supported by this browser\nuse chrome, FireFox or Internet Explorer 11') | |
22124 | } | |
22125 | ||
22126 | var Buffer = require('safe-buffer').Buffer | |
22127 | var crypto = global.crypto || global.msCrypto | |
22128 | ||
22129 | if (crypto && crypto.getRandomValues) { | |
22130 | module.exports = randomBytes | |
22131 | } else { | |
22132 | module.exports = oldBrowser | |
22133 | } | |
22134 | ||
22135 | function randomBytes (size, cb) { | |
22136 | // phantomjs needs to throw | |
22137 | if (size > 65536) throw new Error('requested too many random bytes') | |
22138 | // in case browserify isn't using the Uint8Array version | |
22139 | var rawBytes = new global.Uint8Array(size) | |
22140 | ||
22141 | // This will not work in older browsers. | |
22142 | // See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues | |
22143 | if (size > 0) { // getRandomValues fails on IE if size == 0 | |
22144 | crypto.getRandomValues(rawBytes) | |
22145 | } | |
22146 | ||
22147 | // XXX: phantomjs doesn't like a buffer being passed here | |
22148 | var bytes = Buffer.from(rawBytes.buffer) | |
22149 | ||
22150 | if (typeof cb === 'function') { | |
22151 | return process.nextTick(function () { | |
22152 | cb(null, bytes) | |
22153 | }) | |
22154 | } | |
22155 | ||
22156 | return bytes | |
22157 | } | |
22158 | ||
22159 | }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | |
9f59e99b | 22160 | },{"_process":13,"safe-buffer":128}],127:[function(require,module,exports){ |
a0091a40 IC |
22161 | (function (Buffer){ |
22162 | 'use strict' | |
22163 | var inherits = require('inherits') | |
22164 | var HashBase = require('hash-base') | |
22165 | ||
22166 | function RIPEMD160 () { | |
22167 | HashBase.call(this, 64) | |
22168 | ||
22169 | // state | |
22170 | this._a = 0x67452301 | |
22171 | this._b = 0xefcdab89 | |
22172 | this._c = 0x98badcfe | |
22173 | this._d = 0x10325476 | |
22174 | this._e = 0xc3d2e1f0 | |
22175 | } | |
22176 | ||
22177 | inherits(RIPEMD160, HashBase) | |
22178 | ||
22179 | RIPEMD160.prototype._update = function () { | |
22180 | var m = new Array(16) | |
22181 | for (var i = 0; i < 16; ++i) m[i] = this._block.readInt32LE(i * 4) | |
22182 | ||
22183 | var al = this._a | |
22184 | var bl = this._b | |
22185 | var cl = this._c | |
22186 | var dl = this._d | |
22187 | var el = this._e | |
22188 | ||
22189 | // Mj = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 | |
22190 | // K = 0x00000000 | |
22191 | // Sj = 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8 | |
22192 | al = fn1(al, bl, cl, dl, el, m[0], 0x00000000, 11); cl = rotl(cl, 10) | |
22193 | el = fn1(el, al, bl, cl, dl, m[1], 0x00000000, 14); bl = rotl(bl, 10) | |
22194 | dl = fn1(dl, el, al, bl, cl, m[2], 0x00000000, 15); al = rotl(al, 10) | |
22195 | cl = fn1(cl, dl, el, al, bl, m[3], 0x00000000, 12); el = rotl(el, 10) | |
22196 | bl = fn1(bl, cl, dl, el, al, m[4], 0x00000000, 5); dl = rotl(dl, 10) | |
22197 | al = fn1(al, bl, cl, dl, el, m[5], 0x00000000, 8); cl = rotl(cl, 10) | |
22198 | el = fn1(el, al, bl, cl, dl, m[6], 0x00000000, 7); bl = rotl(bl, 10) | |
22199 | dl = fn1(dl, el, al, bl, cl, m[7], 0x00000000, 9); al = rotl(al, 10) | |
22200 | cl = fn1(cl, dl, el, al, bl, m[8], 0x00000000, 11); el = rotl(el, 10) | |
22201 | bl = fn1(bl, cl, dl, el, al, m[9], 0x00000000, 13); dl = rotl(dl, 10) | |
22202 | al = fn1(al, bl, cl, dl, el, m[10], 0x00000000, 14); cl = rotl(cl, 10) | |
22203 | el = fn1(el, al, bl, cl, dl, m[11], 0x00000000, 15); bl = rotl(bl, 10) | |
22204 | dl = fn1(dl, el, al, bl, cl, m[12], 0x00000000, 6); al = rotl(al, 10) | |
22205 | cl = fn1(cl, dl, el, al, bl, m[13], 0x00000000, 7); el = rotl(el, 10) | |
22206 | bl = fn1(bl, cl, dl, el, al, m[14], 0x00000000, 9); dl = rotl(dl, 10) | |
22207 | al = fn1(al, bl, cl, dl, el, m[15], 0x00000000, 8); cl = rotl(cl, 10) | |
22208 | ||
22209 | // Mj = 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8 | |
22210 | // K = 0x5a827999 | |
22211 | // Sj = 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12 | |
22212 | el = fn2(el, al, bl, cl, dl, m[7], 0x5a827999, 7); bl = rotl(bl, 10) | |
22213 | dl = fn2(dl, el, al, bl, cl, m[4], 0x5a827999, 6); al = rotl(al, 10) | |
22214 | cl = fn2(cl, dl, el, al, bl, m[13], 0x5a827999, 8); el = rotl(el, 10) | |
22215 | bl = fn2(bl, cl, dl, el, al, m[1], 0x5a827999, 13); dl = rotl(dl, 10) | |
22216 | al = fn2(al, bl, cl, dl, el, m[10], 0x5a827999, 11); cl = rotl(cl, 10) | |
22217 | el = fn2(el, al, bl, cl, dl, m[6], 0x5a827999, 9); bl = rotl(bl, 10) | |
22218 | dl = fn2(dl, el, al, bl, cl, m[15], 0x5a827999, 7); al = rotl(al, 10) | |
22219 | cl = fn2(cl, dl, el, al, bl, m[3], 0x5a827999, 15); el = rotl(el, 10) | |
22220 | bl = fn2(bl, cl, dl, el, al, m[12], 0x5a827999, 7); dl = rotl(dl, 10) | |
22221 | al = fn2(al, bl, cl, dl, el, m[0], 0x5a827999, 12); cl = rotl(cl, 10) | |
22222 | el = fn2(el, al, bl, cl, dl, m[9], 0x5a827999, 15); bl = rotl(bl, 10) | |
22223 | dl = fn2(dl, el, al, bl, cl, m[5], 0x5a827999, 9); al = rotl(al, 10) | |
22224 | cl = fn2(cl, dl, el, al, bl, m[2], 0x5a827999, 11); el = rotl(el, 10) | |
22225 | bl = fn2(bl, cl, dl, el, al, m[14], 0x5a827999, 7); dl = rotl(dl, 10) | |
22226 | al = fn2(al, bl, cl, dl, el, m[11], 0x5a827999, 13); cl = rotl(cl, 10) | |
22227 | el = fn2(el, al, bl, cl, dl, m[8], 0x5a827999, 12); bl = rotl(bl, 10) | |
22228 | ||
22229 | // Mj = 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12 | |
22230 | // K = 0x6ed9eba1 | |
22231 | // Sj = 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5 | |
22232 | dl = fn3(dl, el, al, bl, cl, m[3], 0x6ed9eba1, 11); al = rotl(al, 10) | |
22233 | cl = fn3(cl, dl, el, al, bl, m[10], 0x6ed9eba1, 13); el = rotl(el, 10) | |
22234 | bl = fn3(bl, cl, dl, el, al, m[14], 0x6ed9eba1, 6); dl = rotl(dl, 10) | |
22235 | al = fn3(al, bl, cl, dl, el, m[4], 0x6ed9eba1, 7); cl = rotl(cl, 10) | |
22236 | el = fn3(el, al, bl, cl, dl, m[9], 0x6ed9eba1, 14); bl = rotl(bl, 10) | |
22237 | dl = fn3(dl, el, al, bl, cl, m[15], 0x6ed9eba1, 9); al = rotl(al, 10) | |
22238 | cl = fn3(cl, dl, el, al, bl, m[8], 0x6ed9eba1, 13); el = rotl(el, 10) | |
22239 | bl = fn3(bl, cl, dl, el, al, m[1], 0x6ed9eba1, 15); dl = rotl(dl, 10) | |
22240 | al = fn3(al, bl, cl, dl, el, m[2], 0x6ed9eba1, 14); cl = rotl(cl, 10) | |
22241 | el = fn3(el, al, bl, cl, dl, m[7], 0x6ed9eba1, 8); bl = rotl(bl, 10) | |
22242 | dl = fn3(dl, el, al, bl, cl, m[0], 0x6ed9eba1, 13); al = rotl(al, 10) | |
22243 | cl = fn3(cl, dl, el, al, bl, m[6], 0x6ed9eba1, 6); el = rotl(el, 10) | |
22244 | bl = fn3(bl, cl, dl, el, al, m[13], 0x6ed9eba1, 5); dl = rotl(dl, 10) | |
22245 | al = fn3(al, bl, cl, dl, el, m[11], 0x6ed9eba1, 12); cl = rotl(cl, 10) | |
22246 | el = fn3(el, al, bl, cl, dl, m[5], 0x6ed9eba1, 7); bl = rotl(bl, 10) | |
22247 | dl = fn3(dl, el, al, bl, cl, m[12], 0x6ed9eba1, 5); al = rotl(al, 10) | |
22248 | ||
22249 | // Mj = 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2 | |
22250 | // K = 0x8f1bbcdc | |
22251 | // Sj = 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12 | |
22252 | cl = fn4(cl, dl, el, al, bl, m[1], 0x8f1bbcdc, 11); el = rotl(el, 10) | |
22253 | bl = fn4(bl, cl, dl, el, al, m[9], 0x8f1bbcdc, 12); dl = rotl(dl, 10) | |
22254 | al = fn4(al, bl, cl, dl, el, m[11], 0x8f1bbcdc, 14); cl = rotl(cl, 10) | |
22255 | el = fn4(el, al, bl, cl, dl, m[10], 0x8f1bbcdc, 15); bl = rotl(bl, 10) | |
22256 | dl = fn4(dl, el, al, bl, cl, m[0], 0x8f1bbcdc, 14); al = rotl(al, 10) | |
22257 | cl = fn4(cl, dl, el, al, bl, m[8], 0x8f1bbcdc, 15); el = rotl(el, 10) | |
22258 | bl = fn4(bl, cl, dl, el, al, m[12], 0x8f1bbcdc, 9); dl = rotl(dl, 10) | |
22259 | al = fn4(al, bl, cl, dl, el, m[4], 0x8f1bbcdc, 8); cl = rotl(cl, 10) | |
22260 | el = fn4(el, al, bl, cl, dl, m[13], 0x8f1bbcdc, 9); bl = rotl(bl, 10) | |
22261 | dl = fn4(dl, el, al, bl, cl, m[3], 0x8f1bbcdc, 14); al = rotl(al, 10) | |
22262 | cl = fn4(cl, dl, el, al, bl, m[7], 0x8f1bbcdc, 5); el = rotl(el, 10) | |
22263 | bl = fn4(bl, cl, dl, el, al, m[15], 0x8f1bbcdc, 6); dl = rotl(dl, 10) | |
22264 | al = fn4(al, bl, cl, dl, el, m[14], 0x8f1bbcdc, 8); cl = rotl(cl, 10) | |
22265 | el = fn4(el, al, bl, cl, dl, m[5], 0x8f1bbcdc, 6); bl = rotl(bl, 10) | |
22266 | dl = fn4(dl, el, al, bl, cl, m[6], 0x8f1bbcdc, 5); al = rotl(al, 10) | |
22267 | cl = fn4(cl, dl, el, al, bl, m[2], 0x8f1bbcdc, 12); el = rotl(el, 10) | |
22268 | ||
22269 | // Mj = 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 | |
22270 | // K = 0xa953fd4e | |
22271 | // Sj = 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 | |
22272 | bl = fn5(bl, cl, dl, el, al, m[4], 0xa953fd4e, 9); dl = rotl(dl, 10) | |
22273 | al = fn5(al, bl, cl, dl, el, m[0], 0xa953fd4e, 15); cl = rotl(cl, 10) | |
22274 | el = fn5(el, al, bl, cl, dl, m[5], 0xa953fd4e, 5); bl = rotl(bl, 10) | |
22275 | dl = fn5(dl, el, al, bl, cl, m[9], 0xa953fd4e, 11); al = rotl(al, 10) | |
22276 | cl = fn5(cl, dl, el, al, bl, m[7], 0xa953fd4e, 6); el = rotl(el, 10) | |
22277 | bl = fn5(bl, cl, dl, el, al, m[12], 0xa953fd4e, 8); dl = rotl(dl, 10) | |
22278 | al = fn5(al, bl, cl, dl, el, m[2], 0xa953fd4e, 13); cl = rotl(cl, 10) | |
22279 | el = fn5(el, al, bl, cl, dl, m[10], 0xa953fd4e, 12); bl = rotl(bl, 10) | |
22280 | dl = fn5(dl, el, al, bl, cl, m[14], 0xa953fd4e, 5); al = rotl(al, 10) | |
22281 | cl = fn5(cl, dl, el, al, bl, m[1], 0xa953fd4e, 12); el = rotl(el, 10) | |
22282 | bl = fn5(bl, cl, dl, el, al, m[3], 0xa953fd4e, 13); dl = rotl(dl, 10) | |
22283 | al = fn5(al, bl, cl, dl, el, m[8], 0xa953fd4e, 14); cl = rotl(cl, 10) | |
22284 | el = fn5(el, al, bl, cl, dl, m[11], 0xa953fd4e, 11); bl = rotl(bl, 10) | |
22285 | dl = fn5(dl, el, al, bl, cl, m[6], 0xa953fd4e, 8); al = rotl(al, 10) | |
22286 | cl = fn5(cl, dl, el, al, bl, m[15], 0xa953fd4e, 5); el = rotl(el, 10) | |
22287 | bl = fn5(bl, cl, dl, el, al, m[13], 0xa953fd4e, 6); dl = rotl(dl, 10) | |
22288 | ||
22289 | var ar = this._a | |
22290 | var br = this._b | |
22291 | var cr = this._c | |
22292 | var dr = this._d | |
22293 | var er = this._e | |
22294 | ||
22295 | // M'j = 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12 | |
22296 | // K' = 0x50a28be6 | |
22297 | // S'j = 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6 | |
22298 | ar = fn5(ar, br, cr, dr, er, m[5], 0x50a28be6, 8); cr = rotl(cr, 10) | |
22299 | er = fn5(er, ar, br, cr, dr, m[14], 0x50a28be6, 9); br = rotl(br, 10) | |
22300 | dr = fn5(dr, er, ar, br, cr, m[7], 0x50a28be6, 9); ar = rotl(ar, 10) | |
22301 | cr = fn5(cr, dr, er, ar, br, m[0], 0x50a28be6, 11); er = rotl(er, 10) | |
22302 | br = fn5(br, cr, dr, er, ar, m[9], 0x50a28be6, 13); dr = rotl(dr, 10) | |
22303 | ar = fn5(ar, br, cr, dr, er, m[2], 0x50a28be6, 15); cr = rotl(cr, 10) | |
22304 | er = fn5(er, ar, br, cr, dr, m[11], 0x50a28be6, 15); br = rotl(br, 10) | |
22305 | dr = fn5(dr, er, ar, br, cr, m[4], 0x50a28be6, 5); ar = rotl(ar, 10) | |
22306 | cr = fn5(cr, dr, er, ar, br, m[13], 0x50a28be6, 7); er = rotl(er, 10) | |
22307 | br = fn5(br, cr, dr, er, ar, m[6], 0x50a28be6, 7); dr = rotl(dr, 10) | |
22308 | ar = fn5(ar, br, cr, dr, er, m[15], 0x50a28be6, 8); cr = rotl(cr, 10) | |
22309 | er = fn5(er, ar, br, cr, dr, m[8], 0x50a28be6, 11); br = rotl(br, 10) | |
22310 | dr = fn5(dr, er, ar, br, cr, m[1], 0x50a28be6, 14); ar = rotl(ar, 10) | |
22311 | cr = fn5(cr, dr, er, ar, br, m[10], 0x50a28be6, 14); er = rotl(er, 10) | |
22312 | br = fn5(br, cr, dr, er, ar, m[3], 0x50a28be6, 12); dr = rotl(dr, 10) | |
22313 | ar = fn5(ar, br, cr, dr, er, m[12], 0x50a28be6, 6); cr = rotl(cr, 10) | |
22314 | ||
22315 | // M'j = 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2 | |
22316 | // K' = 0x5c4dd124 | |
22317 | // S'j = 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11 | |
22318 | er = fn4(er, ar, br, cr, dr, m[6], 0x5c4dd124, 9); br = rotl(br, 10) | |
22319 | dr = fn4(dr, er, ar, br, cr, m[11], 0x5c4dd124, 13); ar = rotl(ar, 10) | |
22320 | cr = fn4(cr, dr, er, ar, br, m[3], 0x5c4dd124, 15); er = rotl(er, 10) | |
22321 | br = fn4(br, cr, dr, er, ar, m[7], 0x5c4dd124, 7); dr = rotl(dr, 10) | |
22322 | ar = fn4(ar, br, cr, dr, er, m[0], 0x5c4dd124, 12); cr = rotl(cr, 10) | |
22323 | er = fn4(er, ar, br, cr, dr, m[13], 0x5c4dd124, 8); br = rotl(br, 10) | |
22324 | dr = fn4(dr, er, ar, br, cr, m[5], 0x5c4dd124, 9); ar = rotl(ar, 10) | |
22325 | cr = fn4(cr, dr, er, ar, br, m[10], 0x5c4dd124, 11); er = rotl(er, 10) | |
22326 | br = fn4(br, cr, dr, er, ar, m[14], 0x5c4dd124, 7); dr = rotl(dr, 10) | |
22327 | ar = fn4(ar, br, cr, dr, er, m[15], 0x5c4dd124, 7); cr = rotl(cr, 10) | |
22328 | er = fn4(er, ar, br, cr, dr, m[8], 0x5c4dd124, 12); br = rotl(br, 10) | |
22329 | dr = fn4(dr, er, ar, br, cr, m[12], 0x5c4dd124, 7); ar = rotl(ar, 10) | |
22330 | cr = fn4(cr, dr, er, ar, br, m[4], 0x5c4dd124, 6); er = rotl(er, 10) | |
22331 | br = fn4(br, cr, dr, er, ar, m[9], 0x5c4dd124, 15); dr = rotl(dr, 10) | |
22332 | ar = fn4(ar, br, cr, dr, er, m[1], 0x5c4dd124, 13); cr = rotl(cr, 10) | |
22333 | er = fn4(er, ar, br, cr, dr, m[2], 0x5c4dd124, 11); br = rotl(br, 10) | |
22334 | ||
22335 | // M'j = 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13 | |
22336 | // K' = 0x6d703ef3 | |
22337 | // S'j = 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5 | |
22338 | dr = fn3(dr, er, ar, br, cr, m[15], 0x6d703ef3, 9); ar = rotl(ar, 10) | |
22339 | cr = fn3(cr, dr, er, ar, br, m[5], 0x6d703ef3, 7); er = rotl(er, 10) | |
22340 | br = fn3(br, cr, dr, er, ar, m[1], 0x6d703ef3, 15); dr = rotl(dr, 10) | |
22341 | ar = fn3(ar, br, cr, dr, er, m[3], 0x6d703ef3, 11); cr = rotl(cr, 10) | |
22342 | er = fn3(er, ar, br, cr, dr, m[7], 0x6d703ef3, 8); br = rotl(br, 10) | |
22343 | dr = fn3(dr, er, ar, br, cr, m[14], 0x6d703ef3, 6); ar = rotl(ar, 10) | |
22344 | cr = fn3(cr, dr, er, ar, br, m[6], 0x6d703ef3, 6); er = rotl(er, 10) | |
22345 | br = fn3(br, cr, dr, er, ar, m[9], 0x6d703ef3, 14); dr = rotl(dr, 10) | |
22346 | ar = fn3(ar, br, cr, dr, er, m[11], 0x6d703ef3, 12); cr = rotl(cr, 10) | |
22347 | er = fn3(er, ar, br, cr, dr, m[8], 0x6d703ef3, 13); br = rotl(br, 10) | |
22348 | dr = fn3(dr, er, ar, br, cr, m[12], 0x6d703ef3, 5); ar = rotl(ar, 10) | |
22349 | cr = fn3(cr, dr, er, ar, br, m[2], 0x6d703ef3, 14); er = rotl(er, 10) | |
22350 | br = fn3(br, cr, dr, er, ar, m[10], 0x6d703ef3, 13); dr = rotl(dr, 10) | |
22351 | ar = fn3(ar, br, cr, dr, er, m[0], 0x6d703ef3, 13); cr = rotl(cr, 10) | |
22352 | er = fn3(er, ar, br, cr, dr, m[4], 0x6d703ef3, 7); br = rotl(br, 10) | |
22353 | dr = fn3(dr, er, ar, br, cr, m[13], 0x6d703ef3, 5); ar = rotl(ar, 10) | |
22354 | ||
22355 | // M'j = 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14 | |
22356 | // K' = 0x7a6d76e9 | |
22357 | // S'j = 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8 | |
22358 | cr = fn2(cr, dr, er, ar, br, m[8], 0x7a6d76e9, 15); er = rotl(er, 10) | |
22359 | br = fn2(br, cr, dr, er, ar, m[6], 0x7a6d76e9, 5); dr = rotl(dr, 10) | |
22360 | ar = fn2(ar, br, cr, dr, er, m[4], 0x7a6d76e9, 8); cr = rotl(cr, 10) | |
22361 | er = fn2(er, ar, br, cr, dr, m[1], 0x7a6d76e9, 11); br = rotl(br, 10) | |
22362 | dr = fn2(dr, er, ar, br, cr, m[3], 0x7a6d76e9, 14); ar = rotl(ar, 10) | |
22363 | cr = fn2(cr, dr, er, ar, br, m[11], 0x7a6d76e9, 14); er = rotl(er, 10) | |
22364 | br = fn2(br, cr, dr, er, ar, m[15], 0x7a6d76e9, 6); dr = rotl(dr, 10) | |
22365 | ar = fn2(ar, br, cr, dr, er, m[0], 0x7a6d76e9, 14); cr = rotl(cr, 10) | |
22366 | er = fn2(er, ar, br, cr, dr, m[5], 0x7a6d76e9, 6); br = rotl(br, 10) | |
22367 | dr = fn2(dr, er, ar, br, cr, m[12], 0x7a6d76e9, 9); ar = rotl(ar, 10) | |
22368 | cr = fn2(cr, dr, er, ar, br, m[2], 0x7a6d76e9, 12); er = rotl(er, 10) | |
22369 | br = fn2(br, cr, dr, er, ar, m[13], 0x7a6d76e9, 9); dr = rotl(dr, 10) | |
22370 | ar = fn2(ar, br, cr, dr, er, m[9], 0x7a6d76e9, 12); cr = rotl(cr, 10) | |
22371 | er = fn2(er, ar, br, cr, dr, m[7], 0x7a6d76e9, 5); br = rotl(br, 10) | |
22372 | dr = fn2(dr, er, ar, br, cr, m[10], 0x7a6d76e9, 15); ar = rotl(ar, 10) | |
22373 | cr = fn2(cr, dr, er, ar, br, m[14], 0x7a6d76e9, 8); er = rotl(er, 10) | |
22374 | ||
22375 | // M'j = 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 | |
22376 | // K' = 0x00000000 | |
22377 | // S'j = 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 | |
22378 | br = fn1(br, cr, dr, er, ar, m[12], 0x00000000, 8); dr = rotl(dr, 10) | |
22379 | ar = fn1(ar, br, cr, dr, er, m[15], 0x00000000, 5); cr = rotl(cr, 10) | |
22380 | er = fn1(er, ar, br, cr, dr, m[10], 0x00000000, 12); br = rotl(br, 10) | |
22381 | dr = fn1(dr, er, ar, br, cr, m[4], 0x00000000, 9); ar = rotl(ar, 10) | |
22382 | cr = fn1(cr, dr, er, ar, br, m[1], 0x00000000, 12); er = rotl(er, 10) | |
22383 | br = fn1(br, cr, dr, er, ar, m[5], 0x00000000, 5); dr = rotl(dr, 10) | |
22384 | ar = fn1(ar, br, cr, dr, er, m[8], 0x00000000, 14); cr = rotl(cr, 10) | |
22385 | er = fn1(er, ar, br, cr, dr, m[7], 0x00000000, 6); br = rotl(br, 10) | |
22386 | dr = fn1(dr, er, ar, br, cr, m[6], 0x00000000, 8); ar = rotl(ar, 10) | |
22387 | cr = fn1(cr, dr, er, ar, br, m[2], 0x00000000, 13); er = rotl(er, 10) | |
22388 | br = fn1(br, cr, dr, er, ar, m[13], 0x00000000, 6); dr = rotl(dr, 10) | |
22389 | ar = fn1(ar, br, cr, dr, er, m[14], 0x00000000, 5); cr = rotl(cr, 10) | |
22390 | er = fn1(er, ar, br, cr, dr, m[0], 0x00000000, 15); br = rotl(br, 10) | |
22391 | dr = fn1(dr, er, ar, br, cr, m[3], 0x00000000, 13); ar = rotl(ar, 10) | |
22392 | cr = fn1(cr, dr, er, ar, br, m[9], 0x00000000, 11); er = rotl(er, 10) | |
22393 | br = fn1(br, cr, dr, er, ar, m[11], 0x00000000, 11); dr = rotl(dr, 10) | |
22394 | ||
22395 | // change state | |
22396 | var t = (this._b + cl + dr) | 0 | |
22397 | this._b = (this._c + dl + er) | 0 | |
22398 | this._c = (this._d + el + ar) | 0 | |
22399 | this._d = (this._e + al + br) | 0 | |
22400 | this._e = (this._a + bl + cr) | 0 | |
22401 | this._a = t | |
22402 | } | |
22403 | ||
22404 | RIPEMD160.prototype._digest = function () { | |
22405 | // create padding and handle blocks | |
22406 | this._block[this._blockOffset++] = 0x80 | |
22407 | if (this._blockOffset > 56) { | |
22408 | this._block.fill(0, this._blockOffset, 64) | |
22409 | this._update() | |
22410 | this._blockOffset = 0 | |
22411 | } | |
22412 | ||
22413 | this._block.fill(0, this._blockOffset, 56) | |
22414 | this._block.writeUInt32LE(this._length[0], 56) | |
22415 | this._block.writeUInt32LE(this._length[1], 60) | |
22416 | this._update() | |
22417 | ||
22418 | // produce result | |
22419 | var buffer = new Buffer(20) | |
22420 | buffer.writeInt32LE(this._a, 0) | |
22421 | buffer.writeInt32LE(this._b, 4) | |
22422 | buffer.writeInt32LE(this._c, 8) | |
22423 | buffer.writeInt32LE(this._d, 12) | |
22424 | buffer.writeInt32LE(this._e, 16) | |
22425 | return buffer | |
22426 | } | |
22427 | ||
22428 | function rotl (x, n) { | |
22429 | return (x << n) | (x >>> (32 - n)) | |
22430 | } | |
22431 | ||
22432 | function fn1 (a, b, c, d, e, m, k, s) { | |
22433 | return (rotl((a + (b ^ c ^ d) + m + k) | 0, s) + e) | 0 | |
22434 | } | |
22435 | ||
22436 | function fn2 (a, b, c, d, e, m, k, s) { | |
22437 | return (rotl((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + e) | 0 | |
22438 | } | |
22439 | ||
22440 | function fn3 (a, b, c, d, e, m, k, s) { | |
22441 | return (rotl((a + ((b | (~c)) ^ d) + m + k) | 0, s) + e) | 0 | |
22442 | } | |
22443 | ||
22444 | function fn4 (a, b, c, d, e, m, k, s) { | |
22445 | return (rotl((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + e) | 0 | |
22446 | } | |
22447 | ||
22448 | function fn5 (a, b, c, d, e, m, k, s) { | |
22449 | return (rotl((a + (b ^ (c | (~d))) + m + k) | 0, s) + e) | 0 | |
22450 | } | |
22451 | ||
22452 | module.exports = RIPEMD160 | |
22453 | ||
22454 | }).call(this,require("buffer").Buffer) | |
9f59e99b IC |
22455 | },{"buffer":5,"hash-base":113,"inherits":121}],128:[function(require,module,exports){ |
22456 | /* eslint-disable node/no-deprecated-api */ | |
22457 | var buffer = require('buffer') | |
22458 | var Buffer = buffer.Buffer | |
22459 | ||
22460 | // alternative to using Object.keys for old browsers | |
22461 | function copyProps (src, dst) { | |
22462 | for (var key in src) { | |
22463 | dst[key] = src[key] | |
22464 | } | |
22465 | } | |
22466 | if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { | |
22467 | module.exports = buffer | |
22468 | } else { | |
22469 | // Copy properties from require('buffer') | |
22470 | copyProps(buffer, exports) | |
22471 | exports.Buffer = SafeBuffer | |
22472 | } | |
22473 | ||
22474 | function SafeBuffer (arg, encodingOrOffset, length) { | |
22475 | return Buffer(arg, encodingOrOffset, length) | |
22476 | } | |
22477 | ||
22478 | // Copy static methods from Buffer | |
22479 | copyProps(Buffer, SafeBuffer) | |
22480 | ||
22481 | SafeBuffer.from = function (arg, encodingOrOffset, length) { | |
22482 | if (typeof arg === 'number') { | |
22483 | throw new TypeError('Argument must not be a number') | |
22484 | } | |
22485 | return Buffer(arg, encodingOrOffset, length) | |
22486 | } | |
22487 | ||
22488 | SafeBuffer.alloc = function (size, fill, encoding) { | |
22489 | if (typeof size !== 'number') { | |
22490 | throw new TypeError('Argument must be a number') | |
22491 | } | |
22492 | var buf = Buffer(size) | |
22493 | if (fill !== undefined) { | |
22494 | if (typeof encoding === 'string') { | |
22495 | buf.fill(fill, encoding) | |
22496 | } else { | |
22497 | buf.fill(fill) | |
22498 | } | |
22499 | } else { | |
22500 | buf.fill(0) | |
22501 | } | |
22502 | return buf | |
22503 | } | |
22504 | ||
22505 | SafeBuffer.allocUnsafe = function (size) { | |
22506 | if (typeof size !== 'number') { | |
22507 | throw new TypeError('Argument must be a number') | |
22508 | } | |
22509 | return Buffer(size) | |
22510 | } | |
22511 | ||
22512 | SafeBuffer.allocUnsafeSlow = function (size) { | |
22513 | if (typeof size !== 'number') { | |
22514 | throw new TypeError('Argument must be a number') | |
22515 | } | |
22516 | return buffer.SlowBuffer(size) | |
22517 | } | |
22518 | ||
22519 | },{"buffer":5}],129:[function(require,module,exports){ | |
22520 | var Buffer = require('safe-buffer').Buffer | |
22521 | ||
a0091a40 IC |
22522 | // prototype class for hash functions |
22523 | function Hash (blockSize, finalSize) { | |
9f59e99b | 22524 | this._block = Buffer.alloc(blockSize) |
a0091a40 IC |
22525 | this._finalSize = finalSize |
22526 | this._blockSize = blockSize | |
22527 | this._len = 0 | |
a0091a40 IC |
22528 | } |
22529 | ||
22530 | Hash.prototype.update = function (data, enc) { | |
22531 | if (typeof data === 'string') { | |
22532 | enc = enc || 'utf8' | |
9f59e99b | 22533 | data = Buffer.from(data, enc) |
a0091a40 IC |
22534 | } |
22535 | ||
9f59e99b IC |
22536 | var block = this._block |
22537 | var blockSize = this._blockSize | |
22538 | var length = data.length | |
22539 | var accum = this._len | |
a0091a40 | 22540 | |
9f59e99b IC |
22541 | for (var offset = 0; offset < length;) { |
22542 | var assigned = accum % blockSize | |
22543 | var remainder = Math.min(length - offset, blockSize - assigned) | |
a0091a40 | 22544 | |
9f59e99b IC |
22545 | for (var i = 0; i < remainder; i++) { |
22546 | block[assigned + i] = data[offset + i] | |
a0091a40 IC |
22547 | } |
22548 | ||
9f59e99b IC |
22549 | accum += remainder |
22550 | offset += remainder | |
a0091a40 | 22551 | |
9f59e99b IC |
22552 | if ((accum % blockSize) === 0) { |
22553 | this._update(block) | |
a0091a40 IC |
22554 | } |
22555 | } | |
a0091a40 | 22556 | |
9f59e99b | 22557 | this._len += length |
a0091a40 IC |
22558 | return this |
22559 | } | |
22560 | ||
22561 | Hash.prototype.digest = function (enc) { | |
9f59e99b | 22562 | var rem = this._len % this._blockSize |
a0091a40 | 22563 | |
9f59e99b | 22564 | this._block[rem] = 0x80 |
a0091a40 | 22565 | |
9f59e99b IC |
22566 | // zero (rem + 1) trailing bits, where (rem + 1) is the smallest |
22567 | // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize | |
22568 | this._block.fill(0, rem + 1) | |
a0091a40 | 22569 | |
9f59e99b | 22570 | if (rem >= this._finalSize) { |
a0091a40 IC |
22571 | this._update(this._block) |
22572 | this._block.fill(0) | |
22573 | } | |
22574 | ||
9f59e99b IC |
22575 | var bits = this._len * 8 |
22576 | ||
22577 | // uint32 | |
22578 | if (bits <= 0xffffffff) { | |
22579 | this._block.writeUInt32BE(bits, this._blockSize - 4) | |
22580 | ||
22581 | // uint64 | |
22582 | } else { | |
22583 | var lowBits = bits & 0xffffffff | |
22584 | var highBits = (bits - lowBits) / 0x100000000 | |
22585 | ||
22586 | this._block.writeUInt32BE(highBits, this._blockSize - 8) | |
22587 | this._block.writeUInt32BE(lowBits, this._blockSize - 4) | |
22588 | } | |
a0091a40 | 22589 | |
9f59e99b IC |
22590 | this._update(this._block) |
22591 | var hash = this._hash() | |
a0091a40 IC |
22592 | |
22593 | return enc ? hash.toString(enc) : hash | |
22594 | } | |
22595 | ||
22596 | Hash.prototype._update = function () { | |
22597 | throw new Error('_update must be implemented by subclass') | |
22598 | } | |
22599 | ||
22600 | module.exports = Hash | |
22601 | ||
9f59e99b | 22602 | },{"safe-buffer":128}],130:[function(require,module,exports){ |
a0091a40 IC |
22603 | var exports = module.exports = function SHA (algorithm) { |
22604 | algorithm = algorithm.toLowerCase() | |
22605 | ||
22606 | var Algorithm = exports[algorithm] | |
22607 | if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)') | |
22608 | ||
22609 | return new Algorithm() | |
22610 | } | |
22611 | ||
22612 | exports.sha = require('./sha') | |
22613 | exports.sha1 = require('./sha1') | |
22614 | exports.sha224 = require('./sha224') | |
22615 | exports.sha256 = require('./sha256') | |
22616 | exports.sha384 = require('./sha384') | |
22617 | exports.sha512 = require('./sha512') | |
22618 | ||
9f59e99b | 22619 | },{"./sha":131,"./sha1":132,"./sha224":133,"./sha256":134,"./sha384":135,"./sha512":136}],131:[function(require,module,exports){ |
a0091a40 IC |
22620 | /* |
22621 | * A JavaScript implementation of the Secure Hash Algorithm, SHA-0, as defined | |
22622 | * in FIPS PUB 180-1 | |
22623 | * This source code is derived from sha1.js of the same repository. | |
22624 | * The difference between SHA-0 and SHA-1 is just a bitwise rotate left | |
22625 | * operation was added. | |
22626 | */ | |
22627 | ||
22628 | var inherits = require('inherits') | |
22629 | var Hash = require('./hash') | |
9f59e99b | 22630 | var Buffer = require('safe-buffer').Buffer |
a0091a40 IC |
22631 | |
22632 | var K = [ | |
22633 | 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0 | |
22634 | ] | |
22635 | ||
22636 | var W = new Array(80) | |
22637 | ||
22638 | function Sha () { | |
22639 | this.init() | |
22640 | this._w = W | |
22641 | ||
22642 | Hash.call(this, 64, 56) | |
22643 | } | |
22644 | ||
22645 | inherits(Sha, Hash) | |
22646 | ||
22647 | Sha.prototype.init = function () { | |
22648 | this._a = 0x67452301 | |
22649 | this._b = 0xefcdab89 | |
22650 | this._c = 0x98badcfe | |
22651 | this._d = 0x10325476 | |
22652 | this._e = 0xc3d2e1f0 | |
22653 | ||
22654 | return this | |
22655 | } | |
22656 | ||
22657 | function rotl5 (num) { | |
22658 | return (num << 5) | (num >>> 27) | |
22659 | } | |
22660 | ||
22661 | function rotl30 (num) { | |
22662 | return (num << 30) | (num >>> 2) | |
22663 | } | |
22664 | ||
22665 | function ft (s, b, c, d) { | |
22666 | if (s === 0) return (b & c) | ((~b) & d) | |
22667 | if (s === 2) return (b & c) | (b & d) | (c & d) | |
22668 | return b ^ c ^ d | |
22669 | } | |
22670 | ||
22671 | Sha.prototype._update = function (M) { | |
22672 | var W = this._w | |
22673 | ||
22674 | var a = this._a | 0 | |
22675 | var b = this._b | 0 | |
22676 | var c = this._c | 0 | |
22677 | var d = this._d | 0 | |
22678 | var e = this._e | 0 | |
22679 | ||
22680 | for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4) | |
22681 | for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16] | |
22682 | ||
22683 | for (var j = 0; j < 80; ++j) { | |
22684 | var s = ~~(j / 20) | |
22685 | var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0 | |
22686 | ||
22687 | e = d | |
22688 | d = c | |
22689 | c = rotl30(b) | |
22690 | b = a | |
22691 | a = t | |
22692 | } | |
22693 | ||
22694 | this._a = (a + this._a) | 0 | |
22695 | this._b = (b + this._b) | 0 | |
22696 | this._c = (c + this._c) | 0 | |
22697 | this._d = (d + this._d) | 0 | |
22698 | this._e = (e + this._e) | 0 | |
22699 | } | |
22700 | ||
22701 | Sha.prototype._hash = function () { | |
9f59e99b | 22702 | var H = Buffer.allocUnsafe(20) |
a0091a40 IC |
22703 | |
22704 | H.writeInt32BE(this._a | 0, 0) | |
22705 | H.writeInt32BE(this._b | 0, 4) | |
22706 | H.writeInt32BE(this._c | 0, 8) | |
22707 | H.writeInt32BE(this._d | 0, 12) | |
22708 | H.writeInt32BE(this._e | 0, 16) | |
22709 | ||
22710 | return H | |
22711 | } | |
22712 | ||
22713 | module.exports = Sha | |
22714 | ||
9f59e99b | 22715 | },{"./hash":129,"inherits":121,"safe-buffer":128}],132:[function(require,module,exports){ |
a0091a40 IC |
22716 | /* |
22717 | * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined | |
22718 | * in FIPS PUB 180-1 | |
22719 | * Version 2.1a Copyright Paul Johnston 2000 - 2002. | |
22720 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet | |
22721 | * Distributed under the BSD License | |
22722 | * See http://pajhome.org.uk/crypt/md5 for details. | |
22723 | */ | |
22724 | ||
22725 | var inherits = require('inherits') | |
22726 | var Hash = require('./hash') | |
9f59e99b | 22727 | var Buffer = require('safe-buffer').Buffer |
a0091a40 IC |
22728 | |
22729 | var K = [ | |
22730 | 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0 | |
22731 | ] | |
22732 | ||
22733 | var W = new Array(80) | |
22734 | ||
22735 | function Sha1 () { | |
22736 | this.init() | |
22737 | this._w = W | |
22738 | ||
22739 | Hash.call(this, 64, 56) | |
22740 | } | |
22741 | ||
22742 | inherits(Sha1, Hash) | |
22743 | ||
22744 | Sha1.prototype.init = function () { | |
22745 | this._a = 0x67452301 | |
22746 | this._b = 0xefcdab89 | |
22747 | this._c = 0x98badcfe | |
22748 | this._d = 0x10325476 | |
22749 | this._e = 0xc3d2e1f0 | |
22750 | ||
22751 | return this | |
22752 | } | |
22753 | ||
22754 | function rotl1 (num) { | |
22755 | return (num << 1) | (num >>> 31) | |
22756 | } | |
22757 | ||
22758 | function rotl5 (num) { | |
22759 | return (num << 5) | (num >>> 27) | |
22760 | } | |
22761 | ||
22762 | function rotl30 (num) { | |
22763 | return (num << 30) | (num >>> 2) | |
22764 | } | |
22765 | ||
22766 | function ft (s, b, c, d) { | |
22767 | if (s === 0) return (b & c) | ((~b) & d) | |
22768 | if (s === 2) return (b & c) | (b & d) | (c & d) | |
22769 | return b ^ c ^ d | |
22770 | } | |
22771 | ||
22772 | Sha1.prototype._update = function (M) { | |
22773 | var W = this._w | |
22774 | ||
22775 | var a = this._a | 0 | |
22776 | var b = this._b | 0 | |
22777 | var c = this._c | 0 | |
22778 | var d = this._d | 0 | |
22779 | var e = this._e | 0 | |
22780 | ||
22781 | for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4) | |
22782 | for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]) | |
22783 | ||
22784 | for (var j = 0; j < 80; ++j) { | |
22785 | var s = ~~(j / 20) | |
22786 | var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0 | |
22787 | ||
22788 | e = d | |
22789 | d = c | |
22790 | c = rotl30(b) | |
22791 | b = a | |
22792 | a = t | |
22793 | } | |
22794 | ||
22795 | this._a = (a + this._a) | 0 | |
22796 | this._b = (b + this._b) | 0 | |
22797 | this._c = (c + this._c) | 0 | |
22798 | this._d = (d + this._d) | 0 | |
22799 | this._e = (e + this._e) | 0 | |
22800 | } | |
22801 | ||
22802 | Sha1.prototype._hash = function () { | |
9f59e99b | 22803 | var H = Buffer.allocUnsafe(20) |
a0091a40 IC |
22804 | |
22805 | H.writeInt32BE(this._a | 0, 0) | |
22806 | H.writeInt32BE(this._b | 0, 4) | |
22807 | H.writeInt32BE(this._c | 0, 8) | |
22808 | H.writeInt32BE(this._d | 0, 12) | |
22809 | H.writeInt32BE(this._e | 0, 16) | |
22810 | ||
22811 | return H | |
22812 | } | |
22813 | ||
22814 | module.exports = Sha1 | |
22815 | ||
9f59e99b | 22816 | },{"./hash":129,"inherits":121,"safe-buffer":128}],133:[function(require,module,exports){ |
a0091a40 IC |
22817 | /** |
22818 | * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined | |
22819 | * in FIPS 180-2 | |
22820 | * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009. | |
22821 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet | |
22822 | * | |
22823 | */ | |
22824 | ||
22825 | var inherits = require('inherits') | |
22826 | var Sha256 = require('./sha256') | |
22827 | var Hash = require('./hash') | |
9f59e99b | 22828 | var Buffer = require('safe-buffer').Buffer |
a0091a40 IC |
22829 | |
22830 | var W = new Array(64) | |
22831 | ||
22832 | function Sha224 () { | |
22833 | this.init() | |
22834 | ||
22835 | this._w = W // new Array(64) | |
22836 | ||
22837 | Hash.call(this, 64, 56) | |
22838 | } | |
22839 | ||
22840 | inherits(Sha224, Sha256) | |
22841 | ||
22842 | Sha224.prototype.init = function () { | |
22843 | this._a = 0xc1059ed8 | |
22844 | this._b = 0x367cd507 | |
22845 | this._c = 0x3070dd17 | |
22846 | this._d = 0xf70e5939 | |
22847 | this._e = 0xffc00b31 | |
22848 | this._f = 0x68581511 | |
22849 | this._g = 0x64f98fa7 | |
22850 | this._h = 0xbefa4fa4 | |
22851 | ||
22852 | return this | |
22853 | } | |
22854 | ||
22855 | Sha224.prototype._hash = function () { | |
9f59e99b | 22856 | var H = Buffer.allocUnsafe(28) |
a0091a40 IC |
22857 | |
22858 | H.writeInt32BE(this._a, 0) | |
22859 | H.writeInt32BE(this._b, 4) | |
22860 | H.writeInt32BE(this._c, 8) | |
22861 | H.writeInt32BE(this._d, 12) | |
22862 | H.writeInt32BE(this._e, 16) | |
22863 | H.writeInt32BE(this._f, 20) | |
22864 | H.writeInt32BE(this._g, 24) | |
22865 | ||
22866 | return H | |
22867 | } | |
22868 | ||
22869 | module.exports = Sha224 | |
22870 | ||
9f59e99b | 22871 | },{"./hash":129,"./sha256":134,"inherits":121,"safe-buffer":128}],134:[function(require,module,exports){ |
a0091a40 IC |
22872 | /** |
22873 | * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined | |
22874 | * in FIPS 180-2 | |
22875 | * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009. | |
22876 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet | |
22877 | * | |
22878 | */ | |
22879 | ||
22880 | var inherits = require('inherits') | |
22881 | var Hash = require('./hash') | |
9f59e99b | 22882 | var Buffer = require('safe-buffer').Buffer |
a0091a40 IC |
22883 | |
22884 | var K = [ | |
22885 | 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, | |
22886 | 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, | |
22887 | 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, | |
22888 | 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, | |
22889 | 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, | |
22890 | 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, | |
22891 | 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, | |
22892 | 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, | |
22893 | 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, | |
22894 | 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, | |
22895 | 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, | |
22896 | 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, | |
22897 | 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, | |
22898 | 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, | |
22899 | 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, | |
22900 | 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2 | |
22901 | ] | |
22902 | ||
22903 | var W = new Array(64) | |
22904 | ||
22905 | function Sha256 () { | |
22906 | this.init() | |
22907 | ||
22908 | this._w = W // new Array(64) | |
22909 | ||
22910 | Hash.call(this, 64, 56) | |
22911 | } | |
22912 | ||
22913 | inherits(Sha256, Hash) | |
22914 | ||
22915 | Sha256.prototype.init = function () { | |
22916 | this._a = 0x6a09e667 | |
22917 | this._b = 0xbb67ae85 | |
22918 | this._c = 0x3c6ef372 | |
22919 | this._d = 0xa54ff53a | |
22920 | this._e = 0x510e527f | |
22921 | this._f = 0x9b05688c | |
22922 | this._g = 0x1f83d9ab | |
22923 | this._h = 0x5be0cd19 | |
22924 | ||
22925 | return this | |
22926 | } | |
22927 | ||
22928 | function ch (x, y, z) { | |
22929 | return z ^ (x & (y ^ z)) | |
22930 | } | |
22931 | ||
22932 | function maj (x, y, z) { | |
22933 | return (x & y) | (z & (x | y)) | |
22934 | } | |
22935 | ||
22936 | function sigma0 (x) { | |
22937 | return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10) | |
22938 | } | |
22939 | ||
22940 | function sigma1 (x) { | |
22941 | return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7) | |
22942 | } | |
22943 | ||
22944 | function gamma0 (x) { | |
22945 | return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3) | |
22946 | } | |
22947 | ||
22948 | function gamma1 (x) { | |
22949 | return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10) | |
22950 | } | |
22951 | ||
22952 | Sha256.prototype._update = function (M) { | |
22953 | var W = this._w | |
22954 | ||
22955 | var a = this._a | 0 | |
22956 | var b = this._b | 0 | |
22957 | var c = this._c | 0 | |
22958 | var d = this._d | 0 | |
22959 | var e = this._e | 0 | |
22960 | var f = this._f | 0 | |
22961 | var g = this._g | 0 | |
22962 | var h = this._h | 0 | |
22963 | ||
22964 | for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4) | |
22965 | for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0 | |
22966 | ||
22967 | for (var j = 0; j < 64; ++j) { | |
22968 | var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0 | |
22969 | var T2 = (sigma0(a) + maj(a, b, c)) | 0 | |
22970 | ||
22971 | h = g | |
22972 | g = f | |
22973 | f = e | |
22974 | e = (d + T1) | 0 | |
22975 | d = c | |
22976 | c = b | |
22977 | b = a | |
22978 | a = (T1 + T2) | 0 | |
22979 | } | |
22980 | ||
22981 | this._a = (a + this._a) | 0 | |
22982 | this._b = (b + this._b) | 0 | |
22983 | this._c = (c + this._c) | 0 | |
22984 | this._d = (d + this._d) | 0 | |
22985 | this._e = (e + this._e) | 0 | |
22986 | this._f = (f + this._f) | 0 | |
22987 | this._g = (g + this._g) | 0 | |
22988 | this._h = (h + this._h) | 0 | |
22989 | } | |
22990 | ||
22991 | Sha256.prototype._hash = function () { | |
9f59e99b | 22992 | var H = Buffer.allocUnsafe(32) |
a0091a40 IC |
22993 | |
22994 | H.writeInt32BE(this._a, 0) | |
22995 | H.writeInt32BE(this._b, 4) | |
22996 | H.writeInt32BE(this._c, 8) | |
22997 | H.writeInt32BE(this._d, 12) | |
22998 | H.writeInt32BE(this._e, 16) | |
22999 | H.writeInt32BE(this._f, 20) | |
23000 | H.writeInt32BE(this._g, 24) | |
23001 | H.writeInt32BE(this._h, 28) | |
23002 | ||
23003 | return H | |
23004 | } | |
23005 | ||
23006 | module.exports = Sha256 | |
23007 | ||
9f59e99b | 23008 | },{"./hash":129,"inherits":121,"safe-buffer":128}],135:[function(require,module,exports){ |
a0091a40 IC |
23009 | var inherits = require('inherits') |
23010 | var SHA512 = require('./sha512') | |
23011 | var Hash = require('./hash') | |
9f59e99b | 23012 | var Buffer = require('safe-buffer').Buffer |
a0091a40 IC |
23013 | |
23014 | var W = new Array(160) | |
23015 | ||
23016 | function Sha384 () { | |
23017 | this.init() | |
23018 | this._w = W | |
23019 | ||
23020 | Hash.call(this, 128, 112) | |
23021 | } | |
23022 | ||
23023 | inherits(Sha384, SHA512) | |
23024 | ||
23025 | Sha384.prototype.init = function () { | |
23026 | this._ah = 0xcbbb9d5d | |
23027 | this._bh = 0x629a292a | |
23028 | this._ch = 0x9159015a | |
23029 | this._dh = 0x152fecd8 | |
23030 | this._eh = 0x67332667 | |
23031 | this._fh = 0x8eb44a87 | |
23032 | this._gh = 0xdb0c2e0d | |
23033 | this._hh = 0x47b5481d | |
23034 | ||
23035 | this._al = 0xc1059ed8 | |
23036 | this._bl = 0x367cd507 | |
23037 | this._cl = 0x3070dd17 | |
23038 | this._dl = 0xf70e5939 | |
23039 | this._el = 0xffc00b31 | |
23040 | this._fl = 0x68581511 | |
23041 | this._gl = 0x64f98fa7 | |
23042 | this._hl = 0xbefa4fa4 | |
23043 | ||
23044 | return this | |
23045 | } | |
23046 | ||
23047 | Sha384.prototype._hash = function () { | |
9f59e99b | 23048 | var H = Buffer.allocUnsafe(48) |
a0091a40 IC |
23049 | |
23050 | function writeInt64BE (h, l, offset) { | |
23051 | H.writeInt32BE(h, offset) | |
23052 | H.writeInt32BE(l, offset + 4) | |
23053 | } | |
23054 | ||
23055 | writeInt64BE(this._ah, this._al, 0) | |
23056 | writeInt64BE(this._bh, this._bl, 8) | |
23057 | writeInt64BE(this._ch, this._cl, 16) | |
23058 | writeInt64BE(this._dh, this._dl, 24) | |
23059 | writeInt64BE(this._eh, this._el, 32) | |
23060 | writeInt64BE(this._fh, this._fl, 40) | |
23061 | ||
23062 | return H | |
23063 | } | |
23064 | ||
23065 | module.exports = Sha384 | |
23066 | ||
9f59e99b | 23067 | },{"./hash":129,"./sha512":136,"inherits":121,"safe-buffer":128}],136:[function(require,module,exports){ |
a0091a40 IC |
23068 | var inherits = require('inherits') |
23069 | var Hash = require('./hash') | |
9f59e99b | 23070 | var Buffer = require('safe-buffer').Buffer |
a0091a40 IC |
23071 | |
23072 | var K = [ | |
23073 | 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, | |
23074 | 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc, | |
23075 | 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019, | |
23076 | 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118, | |
23077 | 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe, | |
23078 | 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2, | |
23079 | 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1, | |
23080 | 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694, | |
23081 | 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3, | |
23082 | 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65, | |
23083 | 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483, | |
23084 | 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5, | |
23085 | 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210, | |
23086 | 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4, | |
23087 | 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725, | |
23088 | 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70, | |
23089 | 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926, | |
23090 | 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df, | |
23091 | 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8, | |
23092 | 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b, | |
23093 | 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001, | |
23094 | 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30, | |
23095 | 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910, | |
23096 | 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8, | |
23097 | 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53, | |
23098 | 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8, | |
23099 | 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb, | |
23100 | 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3, | |
23101 | 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60, | |
23102 | 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec, | |
23103 | 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9, | |
23104 | 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b, | |
23105 | 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207, | |
23106 | 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178, | |
23107 | 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6, | |
23108 | 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b, | |
23109 | 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, | |
23110 | 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c, | |
23111 | 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a, | |
23112 | 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817 | |
23113 | ] | |
23114 | ||
23115 | var W = new Array(160) | |
23116 | ||
23117 | function Sha512 () { | |
23118 | this.init() | |
23119 | this._w = W | |
23120 | ||
23121 | Hash.call(this, 128, 112) | |
23122 | } | |
23123 | ||
23124 | inherits(Sha512, Hash) | |
23125 | ||
23126 | Sha512.prototype.init = function () { | |
23127 | this._ah = 0x6a09e667 | |
23128 | this._bh = 0xbb67ae85 | |
23129 | this._ch = 0x3c6ef372 | |
23130 | this._dh = 0xa54ff53a | |
23131 | this._eh = 0x510e527f | |
23132 | this._fh = 0x9b05688c | |
23133 | this._gh = 0x1f83d9ab | |
23134 | this._hh = 0x5be0cd19 | |
23135 | ||
23136 | this._al = 0xf3bcc908 | |
23137 | this._bl = 0x84caa73b | |
23138 | this._cl = 0xfe94f82b | |
23139 | this._dl = 0x5f1d36f1 | |
23140 | this._el = 0xade682d1 | |
23141 | this._fl = 0x2b3e6c1f | |
23142 | this._gl = 0xfb41bd6b | |
23143 | this._hl = 0x137e2179 | |
23144 | ||
23145 | return this | |
23146 | } | |
23147 | ||
23148 | function Ch (x, y, z) { | |
23149 | return z ^ (x & (y ^ z)) | |
23150 | } | |
23151 | ||
23152 | function maj (x, y, z) { | |
23153 | return (x & y) | (z & (x | y)) | |
23154 | } | |
23155 | ||
23156 | function sigma0 (x, xl) { | |
23157 | return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25) | |
23158 | } | |
23159 | ||
23160 | function sigma1 (x, xl) { | |
23161 | return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23) | |
23162 | } | |
23163 | ||
23164 | function Gamma0 (x, xl) { | |
23165 | return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7) | |
23166 | } | |
23167 | ||
23168 | function Gamma0l (x, xl) { | |
23169 | return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25) | |
23170 | } | |
23171 | ||
23172 | function Gamma1 (x, xl) { | |
23173 | return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6) | |
23174 | } | |
23175 | ||
23176 | function Gamma1l (x, xl) { | |
23177 | return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26) | |
23178 | } | |
23179 | ||
23180 | function getCarry (a, b) { | |
23181 | return (a >>> 0) < (b >>> 0) ? 1 : 0 | |
23182 | } | |
23183 | ||
23184 | Sha512.prototype._update = function (M) { | |
23185 | var W = this._w | |
23186 | ||
23187 | var ah = this._ah | 0 | |
23188 | var bh = this._bh | 0 | |
23189 | var ch = this._ch | 0 | |
23190 | var dh = this._dh | 0 | |
23191 | var eh = this._eh | 0 | |
23192 | var fh = this._fh | 0 | |
23193 | var gh = this._gh | 0 | |
23194 | var hh = this._hh | 0 | |
23195 | ||
23196 | var al = this._al | 0 | |
23197 | var bl = this._bl | 0 | |
23198 | var cl = this._cl | 0 | |
23199 | var dl = this._dl | 0 | |
23200 | var el = this._el | 0 | |
23201 | var fl = this._fl | 0 | |
23202 | var gl = this._gl | 0 | |
23203 | var hl = this._hl | 0 | |
23204 | ||
23205 | for (var i = 0; i < 32; i += 2) { | |
23206 | W[i] = M.readInt32BE(i * 4) | |
23207 | W[i + 1] = M.readInt32BE(i * 4 + 4) | |
23208 | } | |
23209 | for (; i < 160; i += 2) { | |
23210 | var xh = W[i - 15 * 2] | |
23211 | var xl = W[i - 15 * 2 + 1] | |
23212 | var gamma0 = Gamma0(xh, xl) | |
23213 | var gamma0l = Gamma0l(xl, xh) | |
23214 | ||
23215 | xh = W[i - 2 * 2] | |
23216 | xl = W[i - 2 * 2 + 1] | |
23217 | var gamma1 = Gamma1(xh, xl) | |
23218 | var gamma1l = Gamma1l(xl, xh) | |
23219 | ||
23220 | // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16] | |
23221 | var Wi7h = W[i - 7 * 2] | |
23222 | var Wi7l = W[i - 7 * 2 + 1] | |
23223 | ||
23224 | var Wi16h = W[i - 16 * 2] | |
23225 | var Wi16l = W[i - 16 * 2 + 1] | |
23226 | ||
23227 | var Wil = (gamma0l + Wi7l) | 0 | |
23228 | var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0 | |
23229 | Wil = (Wil + gamma1l) | 0 | |
23230 | Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0 | |
23231 | Wil = (Wil + Wi16l) | 0 | |
23232 | Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0 | |
23233 | ||
23234 | W[i] = Wih | |
23235 | W[i + 1] = Wil | |
23236 | } | |
23237 | ||
23238 | for (var j = 0; j < 160; j += 2) { | |
23239 | Wih = W[j] | |
23240 | Wil = W[j + 1] | |
23241 | ||
23242 | var majh = maj(ah, bh, ch) | |
23243 | var majl = maj(al, bl, cl) | |
23244 | ||
23245 | var sigma0h = sigma0(ah, al) | |
23246 | var sigma0l = sigma0(al, ah) | |
23247 | var sigma1h = sigma1(eh, el) | |
23248 | var sigma1l = sigma1(el, eh) | |
23249 | ||
23250 | // t1 = h + sigma1 + ch + K[j] + W[j] | |
23251 | var Kih = K[j] | |
23252 | var Kil = K[j + 1] | |
23253 | ||
23254 | var chh = Ch(eh, fh, gh) | |
23255 | var chl = Ch(el, fl, gl) | |
23256 | ||
23257 | var t1l = (hl + sigma1l) | 0 | |
23258 | var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0 | |
23259 | t1l = (t1l + chl) | 0 | |
23260 | t1h = (t1h + chh + getCarry(t1l, chl)) | 0 | |
23261 | t1l = (t1l + Kil) | 0 | |
23262 | t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0 | |
23263 | t1l = (t1l + Wil) | 0 | |
23264 | t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0 | |
23265 | ||
23266 | // t2 = sigma0 + maj | |
23267 | var t2l = (sigma0l + majl) | 0 | |
23268 | var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0 | |
23269 | ||
23270 | hh = gh | |
23271 | hl = gl | |
23272 | gh = fh | |
23273 | gl = fl | |
23274 | fh = eh | |
23275 | fl = el | |
23276 | el = (dl + t1l) | 0 | |
23277 | eh = (dh + t1h + getCarry(el, dl)) | 0 | |
23278 | dh = ch | |
23279 | dl = cl | |
23280 | ch = bh | |
23281 | cl = bl | |
23282 | bh = ah | |
23283 | bl = al | |
23284 | al = (t1l + t2l) | 0 | |
23285 | ah = (t1h + t2h + getCarry(al, t1l)) | 0 | |
23286 | } | |
23287 | ||
23288 | this._al = (this._al + al) | 0 | |
23289 | this._bl = (this._bl + bl) | 0 | |
23290 | this._cl = (this._cl + cl) | 0 | |
23291 | this._dl = (this._dl + dl) | 0 | |
23292 | this._el = (this._el + el) | 0 | |
23293 | this._fl = (this._fl + fl) | 0 | |
23294 | this._gl = (this._gl + gl) | 0 | |
23295 | this._hl = (this._hl + hl) | 0 | |
23296 | ||
23297 | this._ah = (this._ah + ah + getCarry(this._al, al)) | 0 | |
23298 | this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0 | |
23299 | this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0 | |
23300 | this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0 | |
23301 | this._eh = (this._eh + eh + getCarry(this._el, el)) | 0 | |
23302 | this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0 | |
23303 | this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0 | |
23304 | this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0 | |
23305 | } | |
23306 | ||
23307 | Sha512.prototype._hash = function () { | |
9f59e99b | 23308 | var H = Buffer.allocUnsafe(64) |
a0091a40 IC |
23309 | |
23310 | function writeInt64BE (h, l, offset) { | |
23311 | H.writeInt32BE(h, offset) | |
23312 | H.writeInt32BE(l, offset + 4) | |
23313 | } | |
23314 | ||
23315 | writeInt64BE(this._ah, this._al, 0) | |
23316 | writeInt64BE(this._bh, this._bl, 8) | |
23317 | writeInt64BE(this._ch, this._cl, 16) | |
23318 | writeInt64BE(this._dh, this._dl, 24) | |
23319 | writeInt64BE(this._eh, this._el, 32) | |
23320 | writeInt64BE(this._fh, this._fl, 40) | |
23321 | writeInt64BE(this._gh, this._gl, 48) | |
23322 | writeInt64BE(this._hh, this._hl, 56) | |
23323 | ||
23324 | return H | |
23325 | } | |
23326 | ||
23327 | module.exports = Sha512 | |
23328 | ||
9f59e99b | 23329 | },{"./hash":129,"inherits":121,"safe-buffer":128}],137:[function(require,module,exports){ |
a0091a40 IC |
23330 | var native = require('./native') |
23331 | ||
a0091a40 IC |
23332 | function getTypeName (fn) { |
23333 | return fn.name || fn.toString().match(/function (.*?)\s*\(/)[1] | |
23334 | } | |
23335 | ||
23336 | function getValueTypeName (value) { | |
23337 | return native.Nil(value) ? '' : getTypeName(value.constructor) | |
23338 | } | |
23339 | ||
23340 | function getValue (value) { | |
23341 | if (native.Function(value)) return '' | |
23342 | if (native.String(value)) return JSON.stringify(value) | |
23343 | if (value && native.Object(value)) return '' | |
23344 | return value | |
23345 | } | |
23346 | ||
23347 | function tfJSON (type) { | |
23348 | if (native.Function(type)) return type.toJSON ? type.toJSON() : getTypeName(type) | |
23349 | if (native.Array(type)) return 'Array' | |
23350 | if (type && native.Object(type)) return 'Object' | |
23351 | ||
23352 | return type !== undefined ? type : '' | |
23353 | } | |
23354 | ||
23355 | function tfErrorString (type, value, valueTypeName) { | |
23356 | var valueJson = getValue(value) | |
23357 | ||
23358 | return 'Expected ' + tfJSON(type) + ', got' + | |
23359 | (valueTypeName !== '' ? ' ' + valueTypeName : '') + | |
23360 | (valueJson !== '' ? ' ' + valueJson : '') | |
23361 | } | |
23362 | ||
9f59e99b IC |
23363 | function TfTypeError (type, value, valueTypeName) { |
23364 | valueTypeName = valueTypeName || getValueTypeName(value) | |
23365 | this.message = tfErrorString(type, value, valueTypeName) | |
23366 | ||
23367 | Error.captureStackTrace(this, TfTypeError) | |
23368 | this.__type = type | |
23369 | this.__value = value | |
23370 | this.__valueTypeName = valueTypeName | |
23371 | } | |
23372 | ||
23373 | TfTypeError.prototype = Object.create(Error.prototype) | |
23374 | TfTypeError.prototype.constructor = TfTypeError | |
23375 | ||
a0091a40 IC |
23376 | function tfPropertyErrorString (type, label, name, value, valueTypeName) { |
23377 | var description = '" of type ' | |
23378 | if (label === 'key') description = '" with key type ' | |
23379 | ||
23380 | return tfErrorString('property "' + tfJSON(name) + description + tfJSON(type), value, valueTypeName) | |
23381 | } | |
23382 | ||
9f59e99b IC |
23383 | function TfPropertyTypeError (type, property, label, value, valueTypeName) { |
23384 | if (type) { | |
23385 | valueTypeName = valueTypeName || getValueTypeName(value) | |
23386 | this.message = tfPropertyErrorString(type, label, property, value, valueTypeName) | |
23387 | } else { | |
23388 | this.message = 'Unexpected property "' + property + '"' | |
23389 | } | |
23390 | ||
23391 | Error.captureStackTrace(this, TfTypeError) | |
23392 | this.__label = label | |
23393 | this.__property = property | |
23394 | this.__type = type | |
23395 | this.__value = value | |
23396 | this.__valueTypeName = valueTypeName | |
23397 | } | |
23398 | ||
23399 | TfPropertyTypeError.prototype = Object.create(Error.prototype) | |
23400 | TfPropertyTypeError.prototype.constructor = TfTypeError | |
23401 | ||
23402 | function tfCustomError (expected, actual) { | |
23403 | return new TfTypeError(expected, {}, actual) | |
23404 | } | |
23405 | ||
23406 | function tfSubError (e, property, label) { | |
23407 | // sub child? | |
23408 | if (e instanceof TfPropertyTypeError) { | |
23409 | property = property + '.' + e.__property | |
23410 | ||
23411 | e = new TfPropertyTypeError( | |
23412 | e.__type, property, e.__label, e.__value, e.__valueTypeName | |
23413 | ) | |
23414 | ||
23415 | // child? | |
23416 | } else if (e instanceof TfTypeError) { | |
23417 | e = new TfPropertyTypeError( | |
23418 | e.__type, property, label, e.__value, e.__valueTypeName | |
23419 | ) | |
23420 | } | |
23421 | ||
23422 | Error.captureStackTrace(e) | |
23423 | return e | |
23424 | } | |
23425 | ||
a0091a40 IC |
23426 | module.exports = { |
23427 | TfTypeError: TfTypeError, | |
23428 | TfPropertyTypeError: TfPropertyTypeError, | |
23429 | tfCustomError: tfCustomError, | |
23430 | tfSubError: tfSubError, | |
23431 | tfJSON: tfJSON, | |
23432 | getValueTypeName: getValueTypeName | |
23433 | } | |
23434 | ||
9f59e99b | 23435 | },{"./native":140}],138:[function(require,module,exports){ |
a0091a40 IC |
23436 | (function (Buffer){ |
23437 | var NATIVE = require('./native') | |
23438 | var ERRORS = require('./errors') | |
23439 | ||
23440 | function _Buffer (value) { | |
23441 | return Buffer.isBuffer(value) | |
23442 | } | |
23443 | ||
23444 | function Hex (value) { | |
23445 | return typeof value === 'string' && /^([0-9a-f]{2})+$/i.test(value) | |
23446 | } | |
23447 | ||
23448 | function _LengthN (type, length) { | |
23449 | var name = type.toJSON() | |
23450 | ||
23451 | function Length (value) { | |
23452 | if (!type(value)) return false | |
23453 | if (value.length === length) return true | |
23454 | ||
23455 | throw ERRORS.tfCustomError(name + '(Length: ' + length + ')', name + '(Length: ' + value.length + ')') | |
23456 | } | |
23457 | Length.toJSON = function () { return name } | |
23458 | ||
23459 | return Length | |
23460 | } | |
23461 | ||
23462 | var _ArrayN = _LengthN.bind(null, NATIVE.Array) | |
23463 | var _BufferN = _LengthN.bind(null, _Buffer) | |
23464 | var _HexN = _LengthN.bind(null, Hex) | |
9f59e99b | 23465 | var _StringN = _LengthN.bind(null, NATIVE.String) |
a0091a40 IC |
23466 | |
23467 | var UINT53_MAX = Math.pow(2, 53) - 1 | |
23468 | ||
23469 | function Finite (value) { | |
23470 | return typeof value === 'number' && isFinite(value) | |
23471 | } | |
23472 | function Int8 (value) { return ((value << 24) >> 24) === value } | |
23473 | function Int16 (value) { return ((value << 16) >> 16) === value } | |
23474 | function Int32 (value) { return (value | 0) === value } | |
23475 | function UInt8 (value) { return (value & 0xff) === value } | |
23476 | function UInt16 (value) { return (value & 0xffff) === value } | |
23477 | function UInt32 (value) { return (value >>> 0) === value } | |
23478 | function UInt53 (value) { | |
23479 | return typeof value === 'number' && | |
23480 | value >= 0 && | |
23481 | value <= UINT53_MAX && | |
23482 | Math.floor(value) === value | |
23483 | } | |
23484 | ||
23485 | var types = { | |
23486 | ArrayN: _ArrayN, | |
23487 | Buffer: _Buffer, | |
23488 | BufferN: _BufferN, | |
23489 | Finite: Finite, | |
23490 | Hex: Hex, | |
23491 | HexN: _HexN, | |
23492 | Int8: Int8, | |
23493 | Int16: Int16, | |
23494 | Int32: Int32, | |
9f59e99b | 23495 | StringN: _StringN, |
a0091a40 IC |
23496 | UInt8: UInt8, |
23497 | UInt16: UInt16, | |
23498 | UInt32: UInt32, | |
23499 | UInt53: UInt53 | |
23500 | } | |
23501 | ||
23502 | for (var typeName in types) { | |
23503 | types[typeName].toJSON = function (t) { | |
23504 | return t | |
23505 | }.bind(null, typeName) | |
23506 | } | |
23507 | ||
23508 | module.exports = types | |
23509 | ||
23510 | }).call(this,{"isBuffer":require("../../../../../.nvm/versions/node/v6.0.0/lib/node_modules/browserify/node_modules/is-buffer/index.js")}) | |
9f59e99b | 23511 | },{"../../../../../.nvm/versions/node/v6.0.0/lib/node_modules/browserify/node_modules/is-buffer/index.js":10,"./errors":137,"./native":140}],139:[function(require,module,exports){ |
a0091a40 IC |
23512 | var ERRORS = require('./errors') |
23513 | var NATIVE = require('./native') | |
23514 | ||
23515 | // short-hand | |
23516 | var tfJSON = ERRORS.tfJSON | |
23517 | var TfTypeError = ERRORS.TfTypeError | |
23518 | var TfPropertyTypeError = ERRORS.TfPropertyTypeError | |
23519 | var tfSubError = ERRORS.tfSubError | |
23520 | var getValueTypeName = ERRORS.getValueTypeName | |
23521 | ||
23522 | var TYPES = { | |
23523 | arrayOf: function arrayOf (type) { | |
23524 | type = compile(type) | |
23525 | ||
23526 | function _arrayOf (array, strict) { | |
23527 | if (!NATIVE.Array(array)) return false | |
9f59e99b | 23528 | if (NATIVE.Nil(array)) return false |
a0091a40 IC |
23529 | |
23530 | return array.every(function (value, i) { | |
23531 | try { | |
23532 | return typeforce(type, value, strict) | |
23533 | } catch (e) { | |
23534 | throw tfSubError(e, i) | |
23535 | } | |
23536 | }) | |
23537 | } | |
23538 | _arrayOf.toJSON = function () { return '[' + tfJSON(type) + ']' } | |
23539 | ||
23540 | return _arrayOf | |
23541 | }, | |
23542 | ||
23543 | maybe: function maybe (type) { | |
23544 | type = compile(type) | |
23545 | ||
23546 | function _maybe (value, strict) { | |
23547 | return NATIVE.Nil(value) || type(value, strict, maybe) | |
23548 | } | |
23549 | _maybe.toJSON = function () { return '?' + tfJSON(type) } | |
23550 | ||
23551 | return _maybe | |
23552 | }, | |
23553 | ||
23554 | map: function map (propertyType, propertyKeyType) { | |
23555 | propertyType = compile(propertyType) | |
23556 | if (propertyKeyType) propertyKeyType = compile(propertyKeyType) | |
23557 | ||
23558 | function _map (value, strict) { | |
9f59e99b IC |
23559 | if (!NATIVE.Object(value)) return false |
23560 | if (NATIVE.Nil(value)) return false | |
a0091a40 IC |
23561 | |
23562 | for (var propertyName in value) { | |
23563 | try { | |
23564 | if (propertyKeyType) { | |
23565 | typeforce(propertyKeyType, propertyName, strict) | |
23566 | } | |
23567 | } catch (e) { | |
23568 | throw tfSubError(e, propertyName, 'key') | |
23569 | } | |
23570 | ||
23571 | try { | |
23572 | var propertyValue = value[propertyName] | |
23573 | typeforce(propertyType, propertyValue, strict) | |
23574 | } catch (e) { | |
23575 | throw tfSubError(e, propertyName) | |
23576 | } | |
23577 | } | |
23578 | ||
23579 | return true | |
23580 | } | |
23581 | ||
23582 | if (propertyKeyType) { | |
23583 | _map.toJSON = function () { | |
23584 | return '{' + tfJSON(propertyKeyType) + ': ' + tfJSON(propertyType) + '}' | |
23585 | } | |
23586 | } else { | |
23587 | _map.toJSON = function () { return '{' + tfJSON(propertyType) + '}' } | |
23588 | } | |
23589 | ||
23590 | return _map | |
23591 | }, | |
23592 | ||
23593 | object: function object (uncompiled) { | |
23594 | var type = {} | |
23595 | ||
23596 | for (var typePropertyName in uncompiled) { | |
23597 | type[typePropertyName] = compile(uncompiled[typePropertyName]) | |
23598 | } | |
23599 | ||
23600 | function _object (value, strict) { | |
23601 | if (!NATIVE.Object(value)) return false | |
23602 | if (NATIVE.Nil(value)) return false | |
23603 | ||
23604 | var propertyName | |
23605 | ||
23606 | try { | |
23607 | for (propertyName in type) { | |
23608 | var propertyType = type[propertyName] | |
23609 | var propertyValue = value[propertyName] | |
23610 | ||
23611 | typeforce(propertyType, propertyValue, strict) | |
23612 | } | |
23613 | } catch (e) { | |
23614 | throw tfSubError(e, propertyName) | |
23615 | } | |
23616 | ||
23617 | if (strict) { | |
23618 | for (propertyName in value) { | |
23619 | if (type[propertyName]) continue | |
23620 | ||
23621 | throw new TfPropertyTypeError(undefined, propertyName) | |
23622 | } | |
23623 | } | |
23624 | ||
23625 | return true | |
23626 | } | |
23627 | _object.toJSON = function () { return tfJSON(type) } | |
23628 | ||
23629 | return _object | |
23630 | }, | |
23631 | ||
23632 | oneOf: function oneOf () { | |
23633 | var types = [].slice.call(arguments).map(compile) | |
23634 | ||
23635 | function _oneOf (value, strict) { | |
23636 | return types.some(function (type) { | |
23637 | try { | |
23638 | return typeforce(type, value, strict) | |
23639 | } catch (e) { | |
23640 | return false | |
23641 | } | |
23642 | }) | |
23643 | } | |
23644 | _oneOf.toJSON = function () { return types.map(tfJSON).join('|') } | |
23645 | ||
23646 | return _oneOf | |
23647 | }, | |
23648 | ||
23649 | quacksLike: function quacksLike (type) { | |
23650 | function _quacksLike (value) { | |
23651 | return type === getValueTypeName(value) | |
23652 | } | |
23653 | _quacksLike.toJSON = function () { return type } | |
23654 | ||
23655 | return _quacksLike | |
23656 | }, | |
23657 | ||
23658 | tuple: function tuple () { | |
23659 | var types = [].slice.call(arguments).map(compile) | |
23660 | ||
23661 | function _tuple (values, strict) { | |
9f59e99b IC |
23662 | if (NATIVE.Nil(values)) return false |
23663 | if (NATIVE.Nil(values.length)) return false | |
23664 | if (strict && (values.length !== types.length)) return false | |
23665 | ||
a0091a40 IC |
23666 | return types.every(function (type, i) { |
23667 | try { | |
23668 | return typeforce(type, values[i], strict) | |
23669 | } catch (e) { | |
23670 | throw tfSubError(e, i) | |
23671 | } | |
9f59e99b | 23672 | }) |
a0091a40 IC |
23673 | } |
23674 | _tuple.toJSON = function () { return '(' + types.map(tfJSON).join(', ') + ')' } | |
23675 | ||
23676 | return _tuple | |
23677 | }, | |
23678 | ||
23679 | value: function value (expected) { | |
23680 | function _value (actual) { | |
23681 | return actual === expected | |
23682 | } | |
23683 | _value.toJSON = function () { return expected } | |
23684 | ||
23685 | return _value | |
23686 | } | |
23687 | } | |
23688 | ||
23689 | function compile (type) { | |
23690 | if (NATIVE.String(type)) { | |
9f59e99b | 23691 | if (type[0] === '?') return TYPES.maybe(type.slice(1)) |
a0091a40 IC |
23692 | |
23693 | return NATIVE[type] || TYPES.quacksLike(type) | |
23694 | } else if (type && NATIVE.Object(type)) { | |
9f59e99b | 23695 | if (NATIVE.Array(type)) return TYPES.arrayOf(type[0]) |
a0091a40 IC |
23696 | |
23697 | return TYPES.object(type) | |
23698 | } else if (NATIVE.Function(type)) { | |
23699 | return type | |
23700 | } | |
23701 | ||
23702 | return TYPES.value(type) | |
23703 | } | |
23704 | ||
23705 | function typeforce (type, value, strict, surrogate) { | |
23706 | if (NATIVE.Function(type)) { | |
23707 | if (type(value, strict)) return true | |
23708 | ||
23709 | throw new TfTypeError(surrogate || type, value) | |
23710 | } | |
23711 | ||
23712 | // JIT | |
23713 | return typeforce(compile(type), value, strict) | |
23714 | } | |
23715 | ||
23716 | // assign types to typeforce function | |
23717 | for (var typeName in NATIVE) { | |
23718 | typeforce[typeName] = NATIVE[typeName] | |
23719 | } | |
23720 | ||
23721 | for (typeName in TYPES) { | |
23722 | typeforce[typeName] = TYPES[typeName] | |
23723 | } | |
23724 | ||
23725 | var EXTRA = require('./extra') | |
23726 | for (typeName in EXTRA) { | |
23727 | typeforce[typeName] = EXTRA[typeName] | |
23728 | } | |
23729 | ||
23730 | // async wrapper | |
23731 | function __async (type, value, strict, callback) { | |
23732 | // default to falsy strict if using shorthand overload | |
23733 | if (typeof strict === 'function') return __async(type, value, false, strict) | |
23734 | ||
23735 | try { | |
23736 | typeforce(type, value, strict) | |
23737 | } catch (e) { | |
23738 | return callback(e) | |
23739 | } | |
23740 | ||
23741 | callback() | |
23742 | } | |
23743 | ||
23744 | typeforce.async = __async | |
23745 | typeforce.compile = compile | |
23746 | typeforce.TfTypeError = TfTypeError | |
23747 | typeforce.TfPropertyTypeError = TfPropertyTypeError | |
23748 | ||
23749 | module.exports = typeforce | |
23750 | ||
9f59e99b | 23751 | },{"./errors":137,"./extra":138,"./native":140}],140:[function(require,module,exports){ |
a0091a40 IC |
23752 | var types = { |
23753 | Array: function (value) { return value !== null && value !== undefined && value.constructor === Array }, | |
23754 | Boolean: function (value) { return typeof value === 'boolean' }, | |
23755 | Function: function (value) { return typeof value === 'function' }, | |
23756 | Nil: function (value) { return value === undefined || value === null }, | |
23757 | Number: function (value) { return typeof value === 'number' }, | |
23758 | Object: function (value) { return typeof value === 'object' }, | |
23759 | String: function (value) { return typeof value === 'string' }, | |
23760 | '': function () { return true } | |
23761 | } | |
23762 | ||
23763 | // TODO: deprecate | |
23764 | types.Null = types.Nil | |
23765 | ||
23766 | for (var typeName in types) { | |
23767 | types[typeName].toJSON = function (t) { | |
23768 | return t | |
23769 | }.bind(null, typeName) | |
23770 | } | |
23771 | ||
23772 | module.exports = types | |
23773 | ||
9f59e99b | 23774 | },{}],141:[function(require,module,exports){ |
a0091a40 IC |
23775 | (function (Buffer){ |
23776 | 'use strict' | |
23777 | ||
23778 | // Number.MAX_SAFE_INTEGER | |
23779 | var MAX_SAFE_INTEGER = 9007199254740991 | |
23780 | ||
23781 | function checkUInt53 (n) { | |
23782 | if (n < 0 || n > MAX_SAFE_INTEGER || n % 1 !== 0) throw new RangeError('value out of range') | |
23783 | } | |
23784 | ||
23785 | function encode (number, buffer, offset) { | |
23786 | checkUInt53(number) | |
23787 | ||
23788 | if (!buffer) buffer = new Buffer(encodingLength(number)) | |
23789 | if (!Buffer.isBuffer(buffer)) throw new TypeError('buffer must be a Buffer instance') | |
23790 | if (!offset) offset = 0 | |
23791 | ||
23792 | // 8 bit | |
23793 | if (number < 0xfd) { | |
23794 | buffer.writeUInt8(number, offset) | |
23795 | encode.bytes = 1 | |
23796 | ||
23797 | // 16 bit | |
23798 | } else if (number <= 0xffff) { | |
23799 | buffer.writeUInt8(0xfd, offset) | |
23800 | buffer.writeUInt16LE(number, offset + 1) | |
23801 | encode.bytes = 3 | |
23802 | ||
23803 | // 32 bit | |
23804 | } else if (number <= 0xffffffff) { | |
23805 | buffer.writeUInt8(0xfe, offset) | |
23806 | buffer.writeUInt32LE(number, offset + 1) | |
23807 | encode.bytes = 5 | |
23808 | ||
23809 | // 64 bit | |
23810 | } else { | |
23811 | buffer.writeUInt8(0xff, offset) | |
23812 | buffer.writeUInt32LE(number >>> 0, offset + 1) | |
23813 | buffer.writeUInt32LE((number / 0x100000000) | 0, offset + 5) | |
23814 | encode.bytes = 9 | |
23815 | } | |
23816 | ||
23817 | return buffer | |
23818 | } | |
23819 | ||
23820 | function decode (buffer, offset) { | |
23821 | if (!Buffer.isBuffer(buffer)) throw new TypeError('buffer must be a Buffer instance') | |
23822 | if (!offset) offset = 0 | |
23823 | ||
23824 | var first = buffer.readUInt8(offset) | |
23825 | ||
23826 | // 8 bit | |
23827 | if (first < 0xfd) { | |
23828 | decode.bytes = 1 | |
23829 | return first | |
23830 | ||
23831 | // 16 bit | |
23832 | } else if (first === 0xfd) { | |
23833 | decode.bytes = 3 | |
23834 | return buffer.readUInt16LE(offset + 1) | |
23835 | ||
23836 | // 32 bit | |
23837 | } else if (first === 0xfe) { | |
23838 | decode.bytes = 5 | |
23839 | return buffer.readUInt32LE(offset + 1) | |
23840 | ||
23841 | // 64 bit | |
23842 | } else { | |
23843 | decode.bytes = 9 | |
23844 | var lo = buffer.readUInt32LE(offset + 1) | |
23845 | var hi = buffer.readUInt32LE(offset + 5) | |
23846 | var number = hi * 0x0100000000 + lo | |
23847 | checkUInt53(number) | |
23848 | ||
23849 | return number | |
23850 | } | |
23851 | } | |
23852 | ||
23853 | function encodingLength (number) { | |
23854 | checkUInt53(number) | |
23855 | ||
23856 | return ( | |
23857 | number < 0xfd ? 1 | |
23858 | : number <= 0xffff ? 3 | |
23859 | : number <= 0xffffffff ? 5 | |
23860 | : 9 | |
23861 | ) | |
23862 | } | |
23863 | ||
23864 | module.exports = { encode: encode, decode: decode, encodingLength: encodingLength } | |
23865 | ||
23866 | }).call(this,require("buffer").Buffer) | |
9f59e99b | 23867 | },{"buffer":5}],142:[function(require,module,exports){ |
a0091a40 IC |
23868 | (function (Buffer){ |
23869 | var bs58check = require('bs58check') | |
23870 | ||
23871 | function decodeRaw (buffer, version) { | |
23872 | // check version only if defined | |
23873 | if (version !== undefined && buffer[0] !== version) throw new Error('Invalid network version') | |
23874 | ||
23875 | // uncompressed | |
23876 | if (buffer.length === 33) { | |
23877 | return { | |
23878 | version: buffer[0], | |
23879 | privateKey: buffer.slice(1, 33), | |
23880 | compressed: false | |
23881 | } | |
23882 | } | |
23883 | ||
23884 | // invalid length | |
23885 | if (buffer.length !== 34) throw new Error('Invalid WIF length') | |
23886 | ||
23887 | // invalid compression flag | |
23888 | if (buffer[33] !== 0x01) throw new Error('Invalid compression flag') | |
23889 | ||
23890 | return { | |
23891 | version: buffer[0], | |
23892 | privateKey: buffer.slice(1, 33), | |
23893 | compressed: true | |
23894 | } | |
23895 | } | |
23896 | ||
23897 | function encodeRaw (version, privateKey, compressed) { | |
23898 | var result = new Buffer(compressed ? 34 : 33) | |
23899 | ||
23900 | result.writeUInt8(version, 0) | |
23901 | privateKey.copy(result, 1) | |
23902 | ||
23903 | if (compressed) { | |
23904 | result[33] = 0x01 | |
23905 | } | |
23906 | ||
23907 | return result | |
23908 | } | |
23909 | ||
23910 | function decode (string, version) { | |
23911 | return decodeRaw(bs58check.decode(string), version) | |
23912 | } | |
23913 | ||
23914 | function encode (version, privateKey, compressed) { | |
23915 | if (typeof version === 'number') return bs58check.encode(encodeRaw(version, privateKey, compressed)) | |
23916 | ||
23917 | return bs58check.encode( | |
23918 | encodeRaw( | |
23919 | version.version, | |
23920 | version.privateKey, | |
23921 | version.compressed | |
23922 | ) | |
23923 | ) | |
23924 | } | |
23925 | ||
23926 | module.exports = { | |
23927 | decode: decode, | |
23928 | decodeRaw: decodeRaw, | |
23929 | encode: encode, | |
23930 | encodeRaw: encodeRaw | |
23931 | } | |
23932 | ||
23933 | }).call(this,require("buffer").Buffer) | |
9f59e99b IC |
23934 | },{"bs58check":85,"buffer":5}]},{},[34])(34) |
23935 | }); |