@bsv/sdk 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/README.md +25 -3
  2. package/package.json +9 -5
  3. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -40
  4. package/.github/ISSUE_TEMPLATE/discussion.md +0 -24
  5. package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +0 -23
  6. package/CHANGELOG.md +0 -72
  7. package/CONTRIBUTING.md +0 -85
  8. package/ROADMAP.md +0 -3
  9. package/docs/getting-started/COMMONJS.md +0 -94
  10. package/docs/getting-started/REACT-TS.md +0 -131
  11. package/docs/getting-started/TS-NODE.md +0 -106
  12. package/docs/getting-started/VUE.md +0 -103
  13. package/jest.config.js +0 -6
  14. package/mod.ts +0 -8
  15. package/src/compat/BSM.ts +0 -51
  16. package/src/compat/ECIES.ts +0 -557
  17. package/src/compat/HD.ts +0 -348
  18. package/src/compat/Mnemonic.ts +0 -295
  19. package/src/compat/__tests/BSM.test.ts +0 -38
  20. package/src/compat/__tests/ECIES.test.ts +0 -90
  21. package/src/compat/__tests/HD.test.ts +0 -405
  22. package/src/compat/__tests/Mnemonic.test.ts +0 -177
  23. package/src/compat/__tests/Mnemonic.vectors.ts +0 -172
  24. package/src/compat/bip-39-wordlist-en.ts +0 -2053
  25. package/src/compat/index.ts +0 -4
  26. package/src/messages/EncryptedMessage.ts +0 -70
  27. package/src/messages/SignedMessage.ts +0 -87
  28. package/src/messages/__tests/EncryptedMessage.test.ts +0 -36
  29. package/src/messages/__tests/SignedMessage.test.ts +0 -53
  30. package/src/messages/index.ts +0 -2
  31. package/src/primitives/AESGCM.ts +0 -479
  32. package/src/primitives/BasePoint.ts +0 -21
  33. package/src/primitives/BigNumber.ts +0 -4619
  34. package/src/primitives/Curve.ts +0 -1163
  35. package/src/primitives/DRBG.ts +0 -102
  36. package/src/primitives/ECDSA.ts +0 -164
  37. package/src/primitives/Hash.ts +0 -1420
  38. package/src/primitives/JacobianPoint.ts +0 -410
  39. package/src/primitives/K256.ts +0 -116
  40. package/src/primitives/Mersenne.ts +0 -123
  41. package/src/primitives/MontgomoryMethod.ts +0 -160
  42. package/src/primitives/Point.ts +0 -852
  43. package/src/primitives/PrivateKey.ts +0 -195
  44. package/src/primitives/PublicKey.ts +0 -154
  45. package/src/primitives/Random.ts +0 -55
  46. package/src/primitives/ReductionContext.ts +0 -528
  47. package/src/primitives/Signature.ts +0 -235
  48. package/src/primitives/SymmetricKey.ts +0 -75
  49. package/src/primitives/TransactionSignature.ts +0 -189
  50. package/src/primitives/__tests/AESGCM.test.ts +0 -338
  51. package/src/primitives/__tests/BRC42.private.vectors.ts +0 -33
  52. package/src/primitives/__tests/BRC42.public.vectors.ts +0 -33
  53. package/src/primitives/__tests/BigNumber.arithmatic.test.ts +0 -572
  54. package/src/primitives/__tests/BigNumber.binary.test.ts +0 -203
  55. package/src/primitives/__tests/BigNumber.constructor.test.ts +0 -176
  56. package/src/primitives/__tests/BigNumber.dhGroup.test.ts +0 -18
  57. package/src/primitives/__tests/BigNumber.fixtures.ts +0 -264
  58. package/src/primitives/__tests/BigNumber.serializers.test.ts +0 -157
  59. package/src/primitives/__tests/BigNumber.utils.test.ts +0 -347
  60. package/src/primitives/__tests/Curve.unit.test.ts +0 -192
  61. package/src/primitives/__tests/DRBG.test.ts +0 -18
  62. package/src/primitives/__tests/DRBG.vectors.ts +0 -167
  63. package/src/primitives/__tests/ECDH.test.ts +0 -31
  64. package/src/primitives/__tests/ECDSA.test.ts +0 -58
  65. package/src/primitives/__tests/HMAC.test.ts +0 -59
  66. package/src/primitives/__tests/Hash.test.ts +0 -121
  67. package/src/primitives/__tests/PBKDF2.vectors.ts +0 -119
  68. package/src/primitives/__tests/PrivateKey.test.ts +0 -17
  69. package/src/primitives/__tests/PublicKey.test.ts +0 -66
  70. package/src/primitives/__tests/Random.test.ts +0 -14
  71. package/src/primitives/__tests/Reader.test.ts +0 -296
  72. package/src/primitives/__tests/ReductionContext.test.ts +0 -279
  73. package/src/primitives/__tests/SymmetricKey.test.ts +0 -58
  74. package/src/primitives/__tests/SymmetricKey.vectors.ts +0 -40
  75. package/src/primitives/__tests/Writer.test.ts +0 -198
  76. package/src/primitives/__tests/sighash.vectors.ts +0 -3503
  77. package/src/primitives/__tests/utils.test.ts +0 -108
  78. package/src/primitives/index.ts +0 -8
  79. package/src/primitives/utils.ts +0 -665
  80. package/src/script/LockingScript.ts +0 -30
  81. package/src/script/OP.ts +0 -219
  82. package/src/script/Script.ts +0 -426
  83. package/src/script/ScriptChunk.ts +0 -7
  84. package/src/script/ScriptTemplate.ts +0 -36
  85. package/src/script/Spend.ts +0 -1379
  86. package/src/script/UnlockingScript.ts +0 -30
  87. package/src/script/__tests/Script.test.ts +0 -369
  88. package/src/script/__tests/Spend.test.ts +0 -248
  89. package/src/script/__tests/script.invalid.vectors.ts +0 -925
  90. package/src/script/__tests/script.valid.vectors.ts +0 -1120
  91. package/src/script/__tests/scriptFromVector.ts +0 -42
  92. package/src/script/__tests/spend.valid.vectors.ts +0 -2288
  93. package/src/script/index.ts +0 -7
  94. package/src/script/templates/P2PKH.ts +0 -109
  95. package/src/script/templates/RPuzzle.ts +0 -140
  96. package/src/script/templates/index.ts +0 -2
  97. package/src/transaction/Broadcaster.ts +0 -42
  98. package/src/transaction/ChainTracker.ts +0 -22
  99. package/src/transaction/FeeModel.ts +0 -13
  100. package/src/transaction/MerklePath.ts +0 -259
  101. package/src/transaction/Transaction.ts +0 -602
  102. package/src/transaction/TransactionInput.ts +0 -63
  103. package/src/transaction/TransactionOutput.ts +0 -37
  104. package/src/transaction/__tests/MerklePath.test.ts +0 -181
  105. package/src/transaction/__tests/Transaction.test.ts +0 -413
  106. package/src/transaction/__tests/bigtx.vectors.ts +0 -4
  107. package/src/transaction/__tests/bump.invalid.vectors.ts +0 -8
  108. package/src/transaction/__tests/bump.valid.vectors.ts +0 -4
  109. package/src/transaction/__tests/tx.invalid.vectors.ts +0 -281
  110. package/src/transaction/__tests/tx.valid.vectors.ts +0 -364
  111. package/src/transaction/broadcasters/ARC.ts +0 -106
  112. package/src/transaction/broadcasters/__tests/ARC.test.ts +0 -115
  113. package/src/transaction/broadcasters/index.ts +0 -1
  114. package/src/transaction/fee-models/SatoshisPerKilobyte.ts +0 -71
  115. package/src/transaction/fee-models/index.ts +0 -1
  116. package/src/transaction/index.ts +0 -6
  117. package/ts2md.json +0 -5
  118. package/tsconfig.base.json +0 -26
  119. package/tsconfig.cjs.json +0 -11
  120. package/tsconfig.eslint.json +0 -12
  121. package/tsconfig.esm.json +0 -9
  122. package/tsconfig.json +0 -17
  123. package/tsconfig.types.json +0 -11
@@ -1,4619 +0,0 @@
1
- import ReductionContext from './ReductionContext.js'
2
-
3
- /**
4
- * JavaScript numbers are only precise up to 53 bits. Since Bitcoin relies on
5
- * 256-bit cryptography, this BigNumber class enables operations on larger
6
- * numbers.
7
- *
8
- * @class BigNumber
9
- */
10
- export default class BigNumber {
11
- /**
12
- * @privateinitializer
13
- */
14
- static zeros: string[] = [
15
- '',
16
- '0',
17
- '00',
18
- '000',
19
- '0000',
20
- '00000',
21
- '000000',
22
- '0000000',
23
- '00000000',
24
- '000000000',
25
- '0000000000',
26
- '00000000000',
27
- '000000000000',
28
- '0000000000000',
29
- '00000000000000',
30
- '000000000000000',
31
- '0000000000000000',
32
- '00000000000000000',
33
- '000000000000000000',
34
- '0000000000000000000',
35
- '00000000000000000000',
36
- '000000000000000000000',
37
- '0000000000000000000000',
38
- '00000000000000000000000',
39
- '000000000000000000000000',
40
- '0000000000000000000000000'
41
- ]
42
-
43
- /**
44
- * @privateinitializer
45
- */
46
- static groupSizes: number[] = [
47
- 0, 0,
48
- 25, 16, 12, 11, 10, 9, 8,
49
- 8, 7, 7, 7, 7, 6, 6,
50
- 6, 6, 6, 6, 6, 5, 5,
51
- 5, 5, 5, 5, 5, 5, 5,
52
- 5, 5, 5, 5, 5, 5, 5
53
- ]
54
-
55
- /**
56
- * @privateinitializer
57
- */
58
- static groupBases: number[] = [
59
- 0, 0,
60
- 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216,
61
- 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625,
62
- 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632,
63
- 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149,
64
- 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176
65
- ]
66
-
67
- /**
68
- * The word size of big number chunks.
69
- *
70
- * @property wordSize
71
- *
72
- * @example
73
- * console.log(BigNumber.wordSize); // output: 26
74
- */
75
- static wordSize: number = 26
76
-
77
- /**
78
- * Negative flag. Indicates whether the big number is a negative number.
79
- * - If 0, the number is positive.
80
- * - If 1, the number is negative.
81
- *
82
- * @property negative
83
- *
84
- * @example
85
- * let num = new BigNumber("-10");
86
- * console.log(num.negative); // output: 1
87
- */
88
- negative: number
89
-
90
- /**
91
- * Array of numbers, where each number represents a part of the value of the big number.
92
- *
93
- * @property words
94
- *
95
- * @example
96
- * let num = new BigNumber(50000);
97
- * console.log(num.words); // output: [ 50000 ]
98
- */
99
- words: number[]
100
-
101
- /**
102
- * Length of the words array.
103
- *
104
- * @property length
105
- *
106
- * @example
107
- * let num = new BigNumber(50000);
108
- * console.log(num.length); // output: 1
109
- */
110
- length: number
111
-
112
- /**
113
- * Reduction context of the big number.
114
- *
115
- * @property red
116
- */
117
- red: ReductionContext | null
118
-
119
- /**
120
- * Checks whether a value is an instance of BigNumber. If not, then checks the features of the input to determine potential compatibility. Regular JS numbers fail this check.
121
- *
122
- * @method isBN
123
- * @param num - The value to be checked.
124
- * @returns - Returns a boolean value determining whether or not the checked num parameter is a BigNumber.
125
- *
126
- * @example
127
- * const validNum = new BigNumber(5);
128
- * BigNumber.isBN(validNum); // returns true
129
- *
130
- * const invalidNum = 5;
131
- * BigNumber.isBN(invalidNum); // returns false
132
- */
133
- static isBN (num: any): boolean {
134
- if (num instanceof BigNumber) {
135
- return true
136
- }
137
-
138
- return num !== null && typeof num === 'object' &&
139
- num.constructor.wordSize === BigNumber.wordSize &&
140
- Array.isArray(num.words)
141
- }
142
-
143
- /**
144
- * Returns the bigger value between two BigNumbers
145
- *
146
- * @method max
147
- * @param left - The first BigNumber to be compared.
148
- * @param right - The second BigNumber to be compared.
149
- * @returns - Returns the bigger BigNumber between left and right.
150
- *
151
- * @example
152
- * const bn1 = new BigNumber(5);
153
- * const bn2 = new BigNumber(10);
154
- * BigNumber.max(bn1, bn2); // returns bn2
155
- */
156
- static max (left: BigNumber, right: BigNumber): BigNumber {
157
- if (left.cmp(right) > 0) return left
158
- return right
159
- }
160
-
161
- /**
162
- * Returns the smaller value between two BigNumbers
163
- *
164
- * @method min
165
- * @param left - The first BigNumber to be compared.
166
- * @param right - The second BigNumber to be compared.
167
- * @returns - Returns the smaller value between left and right.
168
- *
169
- * @example
170
- * const bn1 = new BigNumber(5);
171
- * const bn2 = new BigNumber(10);
172
- * BigNumber.min(bn1, bn2); // returns bn1
173
- */
174
- static min (left: BigNumber, right: BigNumber): BigNumber {
175
- if (left.cmp(right) < 0) return left
176
- return right
177
- }
178
-
179
- /**
180
- * @constructor
181
- *
182
- * @param number - The number (various types accepted) to construct a BigNumber from. Default is 0.
183
- *
184
- * @param base - The base of number provided. By default is 10.
185
- *
186
- * @param endian - The endianness provided. By default is 'big endian'.
187
- *
188
- * @example
189
- * import BigNumber from './BigNumber';
190
- * const bn = new BigNumber('123456', 10, 'be');
191
- */
192
- constructor (
193
- number: number | string | number[] = 0,
194
- base: number | 'be' | 'le' | 'hex' = 10,
195
- endian: 'be' | 'le' = 'be'
196
- ) {
197
- this.negative = 0
198
- this.words = []
199
- this.length = 0
200
-
201
- // Reduction context
202
- this.red = null
203
-
204
- if (number !== null) {
205
- if (base === 'le' || base === 'be') {
206
- endian = base
207
- base = 10
208
- }
209
-
210
- if (typeof number === 'number') {
211
- return this.initNumber(number, base, endian)
212
- }
213
-
214
- if (typeof number === 'object') {
215
- return this.initArray(number, endian)
216
- }
217
-
218
- if (base === 'hex') {
219
- base = 16
220
- }
221
- this.assert(base === (base | 0) && base >= 2 && base <= 36)
222
-
223
- number = number.toString().replace(/\s+/g, '')
224
- let start = 0
225
- if (number[0] === '-') {
226
- start++
227
- this.negative = 1
228
- }
229
-
230
- if (start < number.length) {
231
- if (base === 16) {
232
- this.parseHex(number, start, endian)
233
- } else {
234
- this.parseBase(number, base, start)
235
- if (endian === 'le') {
236
- this.initArray(this.toArray(), endian)
237
- }
238
- }
239
- }
240
- }
241
- }
242
-
243
- /**
244
- * Asserts that a certain condition is true. If it is not, throws an error with the provided message.
245
- *
246
- * @method assert
247
- * @private
248
- * @param val - The condition to be checked.
249
- * @param msg - The error message to throw if the condition is not satisfied. Default is 'Assertion failed'.
250
- */
251
- private assert (val: unknown, msg: string = 'Assertion failed'): void {
252
- if (!(val as boolean)) throw new Error(msg)
253
- }
254
-
255
- /**
256
- * Function to initialize a BigNumber from a regular number. It also determines if the number is negative and sets the negative property accordingly.
257
- * If the endianness provided is little endian ('le'), it reverses the bytes.
258
- *
259
- * @method initNumber
260
- * @private
261
- * @param number - The number to initialize the BigNumber from.
262
- * @param base - The base of the number provided.
263
- * @param endian - The endianness ('be' for big-endian, 'le' for little-endian).
264
- * @returns The current BigNumber instance.
265
- */
266
- private initNumber (number, base, endian): BigNumber {
267
- if (number < 0) {
268
- this.negative = 1
269
- number = -number
270
- }
271
- if (number < 0x4000000) {
272
- this.words = [number & 0x3ffffff]
273
- this.length = 1
274
- } else if (number < 0x10000000000000) {
275
- this.words = [
276
- number & 0x3ffffff,
277
- (number / 0x4000000) & 0x3ffffff
278
- ]
279
- this.length = 2
280
- } else {
281
- this.assert(
282
- number < 0x20000000000000,
283
- 'The number is larger than 2 ^ 53 (unsafe)'
284
- )
285
- this.words = [
286
- number & 0x3ffffff,
287
- (number / 0x4000000) & 0x3ffffff,
288
- 1
289
- ]
290
- this.length = 3
291
- }
292
-
293
- if (endian !== 'le') return this
294
-
295
- // Reverse the bytes
296
- this.initArray(this.toArray(), endian)
297
- return this
298
- }
299
-
300
- /**
301
- * Creates a new BigNumber from the provided number array and initializes it based on the base and endian provided.
302
- *
303
- * @method initArray
304
- * @private
305
- * @param number - The array of numbers to initialize the BigNumber from. Each number represents a part of the value of the big number.
306
- * @param endian - The endianness ('be' for big-endian, 'le' for little-endian).
307
- * @return The current BigNumber instance.
308
- */
309
- private initArray (number, endian): BigNumber {
310
- // Perhaps a Uint8Array
311
- this.assert(
312
- typeof number.length === 'number',
313
- 'The number must have a length'
314
- )
315
- if (number.length <= 0) {
316
- this.words = [0]
317
- this.length = 1
318
- return this
319
- }
320
-
321
- this.length = Math.ceil(number.length / 3)
322
- this.words = new Array(this.length)
323
- let i = 0
324
- for (; i < this.length; i++) {
325
- this.words[i] = 0
326
- }
327
-
328
- let j: number, w
329
- let off = 0
330
- if (endian === 'be') {
331
- for (i = number.length - 1, j = 0; i >= 0; i -= 3) {
332
- w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16)
333
- this.words[j] |= (w << off) & 0x3ffffff
334
- this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff
335
- off += 24
336
- if (off >= 26) {
337
- off -= 26
338
- j++
339
- }
340
- }
341
- } else if (endian === 'le') {
342
- for (i = 0, j = 0; i < number.length; i += 3) {
343
- w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16)
344
- this.words[j] |= (w << off) & 0x3ffffff
345
- this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff
346
- off += 24
347
- if (off >= 26) {
348
- off -= 26
349
- j++
350
- }
351
- }
352
- }
353
- return this.strip()
354
- }
355
-
356
- /**
357
- * Function to extract the 4-bit number from a hexadecimal character
358
- *
359
- * @method parseHex4Bits
360
- * @private
361
- * @param string - The string containing the hexadecimal character.
362
- * @param index - The index of the hexadecimal character in the string.
363
- * @return The decimal value corresponding to the hexadecimal character.
364
- */
365
- private parseHex4Bits (string: string, index: number): number {
366
- const c = string.charCodeAt(index)
367
- // '0' - '9'
368
- if (c >= 48 && c <= 57) {
369
- return c - 48
370
- // 'A' - 'F'
371
- } else if (c >= 65 && c <= 70) {
372
- return c - 55
373
- // 'a' - 'f'
374
- } else if (c >= 97 && c <= 102) {
375
- return c - 87
376
- } else {
377
- throw new Error('Invalid character in ' + string)
378
- }
379
- }
380
-
381
- /**
382
- * Function to extract the 8-bit number from two hexadecimal characters
383
- *
384
- * @method parseHexByte
385
- * @private
386
- * @param string - The string containing the hexadecimal characters.
387
- * @param lowerBound - The lower bound of the index to start parsing from.
388
- * @param index - The index of the second hexadecimal character in the string.
389
- * @return The decimal value corresponding to the two hexadecimal characters.
390
- */
391
- private parseHexByte (
392
- string: string, lowerBound: number, index: number
393
- ): number {
394
- let r = this.parseHex4Bits(string, index)
395
- if (index - 1 >= lowerBound) {
396
- r |= this.parseHex4Bits(string, index - 1) << 4
397
- }
398
- return r
399
- }
400
-
401
- /**
402
- * Function to parse and convert a specific string portion into a big number in hexadecimal base.
403
- *
404
- * @method parseHex
405
- * @private
406
- * @param number - The string to parse.
407
- * @param start - The index to start parsing from.
408
- * @param endian - The endianness ('be', 'le').
409
- * @return The current BigNumber instance.
410
- */
411
- private parseHex (number: string, start: number, endian): BigNumber {
412
- // Create possibly bigger array to ensure that it fits the number
413
- this.length = Math.ceil((number.length - start) / 6)
414
- this.words = new Array(this.length)
415
- let i = 0
416
- for (; i < this.length; i++) {
417
- this.words[i] = 0
418
- }
419
-
420
- // 24-bits chunks
421
- let off = 0
422
- let j = 0
423
-
424
- let w
425
- if (endian === 'be') {
426
- for (i = number.length - 1; i >= start; i -= 2) {
427
- w = this.parseHexByte(number, start, i) << off
428
- this.words[j] |= w & 0x3ffffff
429
- if (off >= 18) {
430
- off -= 18
431
- j += 1
432
- this.words[j] |= w >>> 26
433
- } else {
434
- off += 8
435
- }
436
- }
437
- } else {
438
- const parseLength = number.length - start
439
- for (i = parseLength % 2 === 0 ? start + 1 : start; i < number.length; i += 2) {
440
- w = this.parseHexByte(number, start, i) << off
441
- this.words[j] |= w & 0x3ffffff
442
- if (off >= 18) {
443
- off -= 18
444
- j += 1
445
- this.words[j] |= w >>> 26
446
- } else {
447
- off += 8
448
- }
449
- }
450
- }
451
-
452
- return this.strip()
453
- }
454
-
455
- /**
456
- * Function to convert a particular string portion into a base word.
457
- *
458
- * @method parseBaseWord
459
- * @private
460
- * @param str - The string to parse.
461
- * @param start - The index to start parsing from.
462
- * @param end - The index to stop parsing at.
463
- * @param mul - The base to be used for the conversion.
464
- * @return The decimal value of the parsed base word.
465
- */
466
- private parseBaseWord (str, start, end, mul): number {
467
- let r = 0
468
- let b = 0
469
- const len = Math.min(str.length, end)
470
- for (let i = start; i < len; i++) {
471
- const c = str.charCodeAt(i) - 48
472
-
473
- r *= mul
474
-
475
- // 'a'
476
- if (c >= 49) {
477
- b = c - 49 + 0xa
478
-
479
- // 'A'
480
- } else if (c >= 17) {
481
- b = c - 17 + 0xa
482
-
483
- // '0' - '9'
484
- } else {
485
- b = c
486
- }
487
- this.assert(c >= 0 && b < mul, 'Invalid character')
488
- r += b
489
- }
490
- return r
491
- }
492
-
493
- /**
494
- * Function to convert a string into a big number in a specific base.
495
- *
496
- * @method parseBase
497
- * @private
498
- * @param number - The string to be converted into a big number.
499
- * @param base - The base to be used for conversion.
500
- * @param start - The index to start conversion from.
501
- * @return The current BigNumber instance.
502
- */
503
- private parseBase (number: string, base: number, start: number): BigNumber {
504
- // Initialize as zero
505
- this.words = [0]
506
- this.length = 1
507
-
508
- // Find length of limb in base
509
- let limbLen = 0
510
- let limbPow = 1
511
- for (; limbPow <= 0x3ffffff; limbPow *= base) {
512
- limbLen++
513
- }
514
- limbLen--
515
- limbPow = (limbPow / base) | 0
516
-
517
- const total = number.length - start
518
- const mod = total % limbLen
519
- const end = Math.min(total, total - mod) + start
520
-
521
- let word = 0
522
- let i = start
523
- for (; i < end; i += limbLen) {
524
- word = this.parseBaseWord(number, i, i + limbLen, base)
525
-
526
- this.imuln(limbPow)
527
- if (this.words[0] + word < 0x4000000) {
528
- this.words[0] += word
529
- } else {
530
- this._iaddn(word)
531
- }
532
- }
533
-
534
- if (mod !== 0) {
535
- let pow = 1
536
- word = this.parseBaseWord(number, i, number.length, base)
537
-
538
- for (i = 0; i < mod; i++) {
539
- pow *= base
540
- }
541
-
542
- this.imuln(pow)
543
- if (this.words[0] + word < 0x4000000) {
544
- this.words[0] += word
545
- } else {
546
- this._iaddn(word)
547
- }
548
- }
549
-
550
- return this.strip()
551
- }
552
-
553
- /**
554
- * The copy method creates and returns a separate identical copy of the BigNumber.
555
- *
556
- * @method copy
557
- * @param dest - The BigNumber instance that will be made into a copy.
558
- *
559
- * @example
560
- * const bn1 = new BigNumber('123456', 10, 'be');
561
- * const bn2 = new BigNumber();
562
- * bn1.cop(bn2);
563
- * // bn2 is now a BigNumber representing 123456
564
- */
565
- copy (dest: BigNumber): void {
566
- dest.words = new Array(this.length)
567
- for (let i = 0; i < this.length; i++) {
568
- dest.words[i] = this.words[i]
569
- }
570
- dest.length = this.length
571
- dest.negative = this.negative
572
- dest.red = this.red
573
- }
574
-
575
- /**
576
- *
577
- * Directly transfers the attributes of the source BigNumber to the destination BigNumber.
578
- *
579
- * @method move
580
- * @param dest - The BigNumber that attributes will be moved into.
581
- * @param src - The BigNumber that attributes will be moved from.
582
- *
583
- * @example
584
- * const src = new BigNumber('123456', 10, 'be');
585
- * const dest = new BigNumber();
586
- * BigNumber.move(dest, src);
587
- * // dest is now a BigNumber representing 123456
588
- */
589
- static move (dest: BigNumber, src: BigNumber): void {
590
- dest.words = src.words
591
- dest.length = src.length
592
- dest.negative = src.negative
593
- dest.red = src.red
594
- }
595
-
596
- /**
597
- * Creates a copy of the current BigNumber instance.
598
- *
599
- * @method clone
600
- * @returns A new BigNumber instance, identical to the original.
601
- *
602
- * @example
603
- * const bn = new BigNumber('123456', 10, 'be');
604
- * const bnClone = bn.clone();
605
- */
606
- clone (): BigNumber {
607
- const r = new BigNumber()
608
- this.copy(r)
609
- return r
610
- }
611
-
612
- /**
613
- * Increases the BigNumber length up to a certain size and initializes new elements with 0.
614
- *
615
- * @method expand
616
- * @param size - The desired size to grow the BigNumber length.
617
- * @returns The BigNumber instance after expansion.
618
- *
619
- * @example
620
- * const bn = new BigNumber('123456', 10, 'be');
621
- * bn.expand(10);
622
- */
623
- expand (size): BigNumber {
624
- while (this.length < size) {
625
- this.words[this.length++] = 0
626
- }
627
- return this
628
- }
629
-
630
- /**
631
- * Removes leading zeros.
632
- *
633
- * @method strip
634
- * @returns - Returns the BigNumber after stripping leading zeros.
635
- *
636
- * @example
637
- * const bn = new BigNumber('000000", 2, "be");
638
- * bn.strip();
639
- * // bn now represents 0
640
- */
641
- strip (): BigNumber {
642
- while (this.length > 1 && this.words[this.length - 1] === 0) {
643
- this.length--
644
- }
645
- return this.normSign()
646
- }
647
-
648
- /**
649
- * Normalizes the sign of the BigNumber. Changes -0 to 0.
650
- *
651
- * @method normSign
652
- * @returns The normalized BigNumber instance.
653
- *
654
- * @example
655
- * const bn = new BigNumber('-0', 10, 'be');
656
- * bn.normSign();
657
- */
658
- normSign (): BigNumber {
659
- // -0 = 0
660
- if (this.length === 1 && this.words[0] === 0) {
661
- this.negative = 0
662
- }
663
- return this
664
- }
665
-
666
- /**
667
- * Utility for inspecting the current BigNumber instance. Accompanied with a prefix '<BN: ' or '<BN-R: '.
668
- *
669
- * @method inspect
670
- * @returns A string representation to inspect the BigNumber instance.
671
- *
672
- * @example
673
- * const bn = new BigNumber('123456', 10, 'be');
674
- * bn.inspect();
675
- */
676
- inspect (): string {
677
- return (this.red !== null ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>'
678
- }
679
-
680
- /**
681
- * Converts the BigNumber instance to a string representation.
682
- *
683
- * @method toString
684
- * @param base - The base for representing number. Default is 10. Other accepted values are 16 and 'hex'.
685
- * @param padding - Represents the minimum number of digits to represent the BigNumber as a string. Default is 1.
686
- * @throws If base is not between 2 and 36.
687
- * @returns The string representation of the BigNumber instance
688
- *
689
- * @example
690
- * const bn = new BigNumber('123456', 10, 'be');
691
- * bn.toString(16); // Converts the BigNumber to a hexadecimal string.
692
- */
693
- toString (base: number | 'hex' = 10, padding: number = 1): string {
694
- let out: string
695
- if (base === 16 || base === 'hex') {
696
- out = ''
697
- let off = 0
698
- let carry = 0
699
- for (let i = 0; i < this.length; i++) {
700
- const w = this.words[i]
701
- const word = (((w << off) | carry) & 0xffffff).toString(16)
702
- carry = (w >>> (24 - off)) & 0xffffff
703
- off += 2
704
- if (off >= 26) {
705
- off -= 26
706
- i--
707
- }
708
- if (carry !== 0 || i !== this.length - 1) {
709
- out = BigNumber.zeros[6 - word.length] + word + out
710
- } else {
711
- out = word + out
712
- }
713
- }
714
- if (carry !== 0) {
715
- out = carry.toString(16) + out
716
- }
717
- if (padding === 0 && out === '0') {
718
- return ''
719
- }
720
- while (out.length % padding !== 0 && padding !== 0) {
721
- out = '0' + out
722
- }
723
- if (this.negative !== 0) {
724
- out = '-' + out
725
- }
726
- return out
727
- }
728
-
729
- if (base === (base | 0) && base >= 2 && base <= 36) {
730
- const groupSize = BigNumber.groupSizes[base]
731
- const groupBase = BigNumber.groupBases[base]
732
- out = ''
733
- let c = this.clone()
734
- c.negative = 0
735
- while (!c.isZero()) {
736
- const r = c.modrn(groupBase).toString(base)
737
- c = c.idivn(groupBase)
738
-
739
- if (!c.isZero()) {
740
- out = BigNumber.zeros[groupSize - r.length] + r + out
741
- } else {
742
- out = r + out
743
- }
744
- }
745
- if (this.isZero()) {
746
- out = '0' + out
747
- }
748
- while (out.length % padding !== 0) {
749
- out = '0' + out
750
- }
751
- if (this.negative !== 0) {
752
- out = '-' + out
753
- }
754
- return out
755
- }
756
-
757
- throw new Error('Base should be between 2 and 36')
758
- }
759
-
760
- /**
761
- * Converts the BigNumber instance to a JavaScript number.
762
- * Please note that JavaScript numbers are only precise up to 53 bits.
763
- *
764
- * @method toNumber
765
- * @throws If the BigNumber instance cannot be safely stored in a JavaScript number
766
- * @returns The JavaScript number representation of the BigNumber instance.
767
- *
768
- * @example
769
- * const bn = new BigNumber('123456', 10, 'be');
770
- * bn.toNumber();
771
- */
772
- toNumber (): number {
773
- let ret = this.words[0]
774
- if (this.length === 2) {
775
- ret += this.words[1] * 0x4000000
776
- } else if (this.length === 3 && this.words[2] === 0x01) {
777
- // NOTE: at this stage it is known that the top bit is set
778
- ret += 0x10000000000000 + (this.words[1] * 0x4000000)
779
- } else if (this.length > 2) {
780
- throw new Error('Number can only safely store up to 53 bits')
781
- }
782
- return (this.negative !== 0) ? -ret : ret
783
- }
784
-
785
- /**
786
- * Converts the BigNumber instance to a JSON-formatted string.
787
- *
788
- * @method toJSON
789
- * @returns The JSON string representation of the BigNumber instance.
790
- *
791
- * @example
792
- * const bn = new BigNumber('123456', 10, 'be');
793
- * bn.toJSON();
794
- */
795
- toJSON (): string {
796
- return this.toString(16)
797
- }
798
-
799
- /**
800
- * An internal method to format the BigNumber instance into ArrayTypes of Little Endian Type.
801
- * This is a private method.
802
- *
803
- * @method toArrayLikeLE
804
- * @private
805
- * @param res - The resultant ArrayType instance
806
- * @param byteLength - The byte length to define the size of ArrayType
807
- */
808
- private toArrayLikeLE (res, byteLength): void {
809
- let position = 0
810
- let carry = 0
811
-
812
- for (let i = 0, shift = 0; i < this.length; i++) {
813
- const word = (this.words[i] << shift) | carry
814
-
815
- res[position++] = word & 0xff
816
- if (position < res.length) {
817
- res[position++] = (word >> 8) & 0xff
818
- }
819
- if (position < res.length) {
820
- res[position++] = (word >> 16) & 0xff
821
- }
822
-
823
- if (shift === 6) {
824
- if (position < res.length) {
825
- res[position++] = (word >> 24) & 0xff
826
- }
827
- carry = 0
828
- shift = 0
829
- } else {
830
- carry = word >>> 24
831
- shift += 2
832
- }
833
- }
834
-
835
- if (position < res.length) {
836
- res[position++] = carry
837
-
838
- while (position < res.length) {
839
- res[position++] = 0
840
- }
841
- }
842
- }
843
-
844
- /**
845
- * An internal method to format the BigNumber instance into ArrayTypes of Big Endian Type.
846
- * This is a private method.
847
- *
848
- * @method toArrayLikeBE
849
- * @private
850
- * @param res - The resultant ArrayType instance
851
- * @param byteLength - The byte length to define the size of ArrayType
852
- */
853
- private toArrayLikeBE (res, byteLength): void {
854
- let position = res.length - 1
855
- let carry = 0
856
-
857
- for (let i = 0, shift = 0; i < this.length; i++) {
858
- const word = (this.words[i] << shift) | carry
859
-
860
- res[position--] = word & 0xff
861
- if (position >= 0) {
862
- res[position--] = (word >> 8) & 0xff
863
- }
864
- if (position >= 0) {
865
- res[position--] = (word >> 16) & 0xff
866
- }
867
-
868
- if (shift === 6) {
869
- if (position >= 0) {
870
- res[position--] = (word >> 24) & 0xff
871
- }
872
- carry = 0
873
- shift = 0
874
- } else {
875
- carry = word >>> 24
876
- shift += 2
877
- }
878
- }
879
-
880
- if (position >= 0) {
881
- res[position--] = carry
882
-
883
- while (position >= 0) {
884
- res[position--] = 0
885
- }
886
- }
887
- }
888
-
889
- /**
890
- * Converts the BigNumber instance to a JavaScript number array.
891
- *
892
- * @method toArray
893
- * @param endian - The endian for converting BigNumber to array. Default value is 'be'.
894
- * @param length - The length for the resultant array. Default value is undefined.
895
- * @returns The JavaScript array representation of the BigNumber instance.
896
- *
897
- * @example
898
- * const bn = new BigNumber('123456', 10, 'be');
899
- * bn.toArray('be', 8);
900
- */
901
- toArray (endian: 'le' | 'be' = 'be', length?: number): number[] {
902
- this.strip()
903
-
904
- const byteLength = this.byteLength()
905
- const reqLength = length ?? Math.max(1, byteLength)
906
- this.assert(byteLength <= reqLength, 'byte array longer than desired length')
907
- this.assert(reqLength > 0, 'Requested array length <= 0')
908
-
909
- const res = new Array(reqLength)
910
- if (endian === 'le') {
911
- this.toArrayLikeLE(res, byteLength)
912
- } else {
913
- this.toArrayLikeBE(res, byteLength)
914
- }
915
- return res
916
- }
917
-
918
- /**
919
- * A utility method to count the word bits.
920
- * This is a private method.
921
- *
922
- * @method countWordBits
923
- * @private
924
- * @param w - The input number to count the word bits.
925
- * @returns The number of word bits
926
- */
927
- private countWordBits (w: number): number {
928
- if (typeof Math.clz32 === 'function') {
929
- return 32 - Math.clz32(w)
930
- }
931
- let t = w
932
- let r = 0
933
- if (t >= 0x1000) {
934
- r += 13
935
- t >>>= 13
936
- }
937
- if (t >= 0x40) {
938
- r += 7
939
- t >>>= 7
940
- }
941
- if (t >= 0x8) {
942
- r += 4
943
- t >>>= 4
944
- }
945
- if (t >= 0x02) {
946
- r += 2
947
- t >>>= 2
948
- }
949
- return r + t
950
- }
951
-
952
- /**
953
- * A utility method to compute the number of zero bits.
954
- * This is a private method.
955
- *
956
- * @method zeroWordBits
957
- * @private
958
- * @param w - The input number to count the zero bits.
959
- * @returns The number of zero bits
960
- */
961
- private zeroWordBits (w: number): number {
962
- // Short-cut
963
- if (w === 0) return 26
964
-
965
- let t = w
966
- let r = 0
967
- if ((t & 0x1fff) === 0) {
968
- r += 13
969
- t >>>= 13
970
- }
971
- if ((t & 0x7f) === 0) {
972
- r += 7
973
- t >>>= 7
974
- }
975
- if ((t & 0xf) === 0) {
976
- r += 4
977
- t >>>= 4
978
- }
979
- if ((t & 0x3) === 0) {
980
- r += 2
981
- t >>>= 2
982
- }
983
- if ((t & 0x1) === 0) {
984
- r++
985
- }
986
- return r
987
- }
988
-
989
- /**
990
- * Returns the number of used bits in this big number.
991
- *
992
- * @method bitLength
993
- * @returns The number of used bits
994
- */
995
- bitLength (): number {
996
- const w = this.words[this.length - 1]
997
- const hi = this.countWordBits(w)
998
- return (this.length - 1) * 26 + hi
999
- }
1000
-
1001
- /**
1002
- * Convert a big number to a boolean array representing
1003
- * a binary number, where each array index is a bit.
1004
- * @static
1005
- * @method toBitArray
1006
- * @param num - The big number to convert.
1007
- * @returns Returns an array of booleans representing
1008
- * a binary number, with each array index being a bit.
1009
- * @example
1010
- * const BigNumber = require("./BigNumber");
1011
- * const bn = new BigNumber('6'); // binary: 110
1012
- * const bits = BigNumber.toBitArray(bn); // [1,1,0]
1013
- */
1014
- static toBitArray (num: BigNumber): Array<0 | 1> {
1015
- const w = new Array(num.bitLength())
1016
-
1017
- for (let bit = 0; bit < w.length; bit++) {
1018
- const off = (bit / 26) | 0
1019
- const wbit = bit % 26
1020
-
1021
- w[bit] = (num.words[off] >>> wbit) & 0x01
1022
- }
1023
-
1024
- return w
1025
- }
1026
-
1027
- /**
1028
- * Convert this big number to a boolean array representing
1029
- * a binary number, where each array index is a bit.
1030
- * @method toBitArray
1031
- * @returns Returns an array of booleans representing a binary number.
1032
- *
1033
- * @example
1034
- * const BigNumber = require("./BigNumber");
1035
- * const bn = new BigNumber('6'); // binary: 110
1036
- * const bits = bn.toBitArray(); // [ 1, 1, 0 ]
1037
- */
1038
- toBitArray (): Array<0 | 1> {
1039
- return BigNumber.toBitArray(this)
1040
- }
1041
-
1042
- /**
1043
- * Returns the number of trailing zero bits in the big number.
1044
- * @method zeroBits
1045
- * @returns Returns the number of trailing zero bits
1046
- * in the binary representation of the big number.
1047
- * @example
1048
- * const BigNumber = require("./BigNumber");
1049
- * const bn = new BigNumber('8'); // binary: 1000
1050
- * const zeroBits = bn.zeroBits(); // 3
1051
- */
1052
- zeroBits (): number {
1053
- if (this.isZero()) return 0
1054
-
1055
- let r = 0
1056
- for (let i = 0; i < this.length; i++) {
1057
- const b = this.zeroWordBits(this.words[i])
1058
- r += b
1059
- if (b !== 26) break
1060
- }
1061
- return r
1062
- }
1063
-
1064
- /**
1065
- * Get the byte length of the BigNumber
1066
- *
1067
- * @method byteLength
1068
- * @returns Returns the byte length of the big number.
1069
- * @example
1070
- * const BigNumber = require("./BigNumber");
1071
- * const bn = new BigNumber('1234');
1072
- * const byteLen = bn.byteLength();
1073
- */
1074
- byteLength (): number {
1075
- return Math.ceil(this.bitLength() / 8)
1076
- }
1077
-
1078
- /**
1079
- * Converts this big number to two's complement with a specified bit width.
1080
- * @method toTwos
1081
- * @param width - The bit width.
1082
- * @returns Returns the two's complement of the big number.
1083
- *
1084
- * @example
1085
- * const BigNumber = require("./BigNumber");
1086
- * const bn = new BigNumber('-1234');
1087
- * const twosComp = bn.toTwos(16);
1088
- */
1089
- toTwos (width: number): BigNumber {
1090
- if (this.negative !== 0) {
1091
- return this.abs().inotn(width).iaddn(1)
1092
- }
1093
- return this.clone()
1094
- }
1095
-
1096
- /**
1097
- * Converts this big number from two's complement with a specified bit width.
1098
- * @method fromTwos
1099
- * @param width - The bit width.
1100
- * @returns Returns the big number converted from two's complement.
1101
- *
1102
- * @example
1103
- * const BigNumber = require("./BigNumber");
1104
- * const bn = new BigNumber('-1234');
1105
- * const fromTwos = bn.fromTwos(16);
1106
- */
1107
- fromTwos (width: number): BigNumber {
1108
- if (this.testn(width - 1)) {
1109
- return this.notn(width).iaddn(1).ineg()
1110
- }
1111
- return this.clone()
1112
- }
1113
-
1114
- /**
1115
- * Checks if the big number is negative.
1116
- * @method isNeg
1117
- * @returns Returns true if the big number is negative, otherwise false.
1118
- *
1119
- * @example
1120
- * const BigNumber = require("./BigNumber");
1121
- * const bn = new BigNumber('-1234');
1122
- * const isNegative = bn.isNeg(); // true
1123
- */
1124
- isNeg (): boolean {
1125
- return this.negative !== 0
1126
- }
1127
-
1128
- /**
1129
- * Negates the big number and returns a new instance.
1130
- * @method neg
1131
- * @returns Returns a new BigNumber that is the negation of this big number.
1132
- *
1133
- * @example
1134
- * const BigNumber = require("./BigNumber");
1135
- * const bn = new BigNumber('1234');
1136
- * const neg = bn.neg(); // -1234
1137
- */
1138
- neg (): BigNumber {
1139
- return this.clone().ineg()
1140
- }
1141
-
1142
- /**
1143
- * Negates the big number in-place.
1144
- * @method ineg
1145
- * @returns Returns this big number as the negation of itself.
1146
- *
1147
- * @example
1148
- * const BigNumber = require("./BigNumber");
1149
- * const bn = new BigNumber('1234');
1150
- * bn.ineg(); // bn is now -1234
1151
- */
1152
- ineg (): BigNumber {
1153
- if (!this.isZero()) {
1154
- this.negative ^= 1
1155
- }
1156
-
1157
- return this
1158
- }
1159
-
1160
- /**
1161
- * Performs a bitwise OR operation with another BigNumber and stores
1162
- * the result in this BigNumber.
1163
- * @method iuor
1164
- * @param num - The other BigNumber.
1165
- * @returns Returns this BigNumber after performing the bitwise OR operation.
1166
- *
1167
- * @example
1168
- * const BigNumber = require("./BigNumber");
1169
- * const bn1 = new BigNumber('10'); // binary: 1010
1170
- * const bn2 = new(num: BigNumber): BigNumber BigNumber('6'); // binary: 0110
1171
- * bn1.iuor(bn2); // now, bn1 binary: 1110
1172
- */
1173
- iuor (num: BigNumber): BigNumber {
1174
- while (this.length < num.length) {
1175
- this.words[this.length++] = 0
1176
- }
1177
-
1178
- for (let i = 0; i < num.length; i++) {
1179
- this.words[i] = this.words[i] | num.words[i]
1180
- }
1181
-
1182
- return this.strip()
1183
- }
1184
-
1185
- /**
1186
- * Performs a bitwise OR operation with another BigNumber, considering
1187
- * that neither of the numbers can be negative. Stores the result in this BigNumber.
1188
- * @method ior
1189
- * @param num - The other BigNumber.
1190
- * @returns Returns this BigNumber after performing the bitwise OR operation.
1191
- *
1192
- * @example
1193
- * const BigNumber = require("./BigNumber");
1194
- * const bn1 = new BigNumber('10'); // binary: 1010
1195
- * const bn2 = new BigNumber('6'); // binary: 0110
1196
- * bn1.ior(bn2); // now, bn1 binary: 1110
1197
- */
1198
- ior (num: BigNumber): BigNumber {
1199
- this.assert((this.negative | num.negative) === 0)
1200
- return this.iuor(num)
1201
- }
1202
-
1203
- /**
1204
- * Performs a bitwise OR operation on the current instance and given
1205
- * BigNumber and returns a new BigNumber, in such a way that if either
1206
- * the corresponding bit in the first operand or the second operand is
1207
- * 1, then the output is also 1.
1208
- *
1209
- * @method or
1210
- * @param num - The BigNumber to perform the bitwise OR operation with.
1211
- * @returns Returns a new BigNumber resulting from the bitwise OR operation.
1212
- *
1213
- * @example
1214
- * const num1 = new BigNumber('10');
1215
- * const num2 = new BigNumber('20');
1216
- * console.log(num1.or(num2).toString());
1217
- */
1218
- or (num: BigNumber): BigNumber {
1219
- if (this.length > num.length) return this.clone().ior(num)
1220
- return num.clone().ior(this)
1221
- }
1222
-
1223
- /**
1224
- * Performs a bitwise OR operation on the current instance and given
1225
- * BigNumber without considering signed bit(no negative values) and returns a new BigNumber,
1226
- * similar to the `or` method.
1227
- *
1228
- * @method uor
1229
- * @param num - The BigNumber to perform the bitwise OR operation with.
1230
- * @returns Returns a new BigNumber resulting from the bitwise OR operation without sign consideration.
1231
- *
1232
- * @example
1233
- * const num1 = new BigNumber('10');
1234
- * const num2 = new BigNumber('20');
1235
- * console.log(num1.uor(num2).toString());
1236
- */
1237
- uor (num: BigNumber): BigNumber {
1238
- if (this.length > num.length) return this.clone().iuor(num)
1239
- return num.clone().iuor(this)
1240
- }
1241
-
1242
- /**
1243
- * Performs a bitwise AND operation in-place(this method changes the calling object)
1244
- * on the current instance and given BigNumber such that it modifies the current
1245
- * instance and keeps the bits set in the result only if the corresponding bit is set
1246
- * in both operands.
1247
- *
1248
- * @method iuand
1249
- * @param num - The BigNumber to perform the bitwise AND operation with.
1250
- * @returns Returns the current BigNumber instance after performing the bitwise AND operation.
1251
- *
1252
- * @example
1253
- * const num1 = new BigNumber('10');
1254
- * const num2 = new BigNumber('20');
1255
- * console.log(num1.iuand(num2).toString());
1256
- */
1257
- iuand (num: BigNumber): BigNumber {
1258
- const minLength = Math.min(this.length, num.length)
1259
-
1260
- for (let i = 0; i < minLength; i++) {
1261
- this.words[i] = this.words[i] & num.words[i]
1262
- }
1263
-
1264
- this.length = minLength
1265
-
1266
- return this.strip()
1267
- }
1268
-
1269
- /**
1270
- * Performs an in-place operation that does a bitwise AND operation in-place,
1271
- * on the current instance and given BigNumber such that it modifies the current
1272
- * instance only if neither operand is negative. This method is similar to the iuand method but
1273
- * checks for negative values before operation.
1274
- *
1275
- * @method iand
1276
- * @param num - The BigNumber to perform the bitwise AND operation with.
1277
- * @returns Returns the current BigNumber instance after performing the bitwise AND operation.
1278
- *
1279
- * @example
1280
- * const num1 = new BigNumber('10');
1281
- * const num2 = new BigNumber('20');
1282
- * console.log(num1.iand(num2).toString());
1283
- */
1284
- iand (num: BigNumber): BigNumber {
1285
- this.assert((this.negative | num.negative) === 0)
1286
- return this.iuand(num)
1287
- }
1288
-
1289
- /**
1290
- * Performs a bitwise AND operation that returns a new BigNumber, and keeps the bits
1291
- * set in the result only if the corresponding bit is set in both operands.
1292
- *
1293
- * @method and
1294
- * @param num - The BigNumber to perform the bitwise AND operation with.
1295
- * @returns Returns new BigNumber resulting from the bitwise AND operation.
1296
- *
1297
- * @example
1298
- * const num1 = new BigNumber('10');
1299
- * const num2 = new BigNumber('20');
1300
- * console.log(num1.and(num2).toString());
1301
- */
1302
- and (num: BigNumber): BigNumber {
1303
- if (this.length > num.length) return this.clone().iand(num)
1304
- return num.clone().iand(this)
1305
- }
1306
-
1307
- /**
1308
- * Performs a bitwise AND operation without considering signed bit
1309
- * (no negative values) which returns a new BigNumber, similar to the `and` method.
1310
- *
1311
- * @method uand
1312
- * @param num - The BigNumber to perform the bitwise AND operation with.
1313
- * @returns Returns new BigNumber resulting from the bitwise AND operation without sign consideration.
1314
- *
1315
- * @example
1316
- * const num1 = new BigNumber('10');
1317
- * const num2 = new BigNumber('20');
1318
- * console.log(num1.uand(num2).toString());
1319
- */
1320
- uand (num: BigNumber): BigNumber {
1321
- if (this.length > num.length) return this.clone().iuand(num)
1322
- return num.clone().iuand(this)
1323
- }
1324
-
1325
- /**
1326
- * Modifies the current instance by performing a bitwise XOR operation
1327
- * in-place with the provided BigNumber. It keeps the bits set in the result only if the
1328
- * corresponding bits in the operands are different.
1329
- *
1330
- * @method iuxor
1331
- * @param num - The BigNumber to perform the bitwise XOR operation with.
1332
- * @returns Returns the current BigNumber instance after performing the bitwise XOR operation.
1333
- *
1334
- * @example
1335
- * const num1 = new BigNumber('10');
1336
- * const num2 = new BigNumber('20');
1337
- * console.log(num1.iuxor(num2).toString());
1338
- */
1339
- iuxor (num: BigNumber): BigNumber {
1340
- if (this.length > num.length) {
1341
- for (let i = 0; i < num.length; i++) {
1342
- this.words[i] = this.words[i] ^ num.words[i]
1343
- }
1344
- } else {
1345
- for (let i = 0; i < this.length; i++) {
1346
- this.words[i] = this.words[i] ^ num.words[i]
1347
- }
1348
- for (let i = this.length; i < num.length; i++) {
1349
- this.words[i] = num.words[i]
1350
- }
1351
- this.length = num.length
1352
- }
1353
- return this.strip()
1354
- }
1355
-
1356
- /**
1357
- * Performs an in-place operation that does a bitwise XOR operation in-place,
1358
- * on the current instance and given BigNumber such that it modifies the current
1359
- * instance only if neither operand is negative. This method is similar to the iuxor method but
1360
- * checks for negative values before operation.
1361
- *
1362
- * @method ixor
1363
- * @param num - The BigNumber to perform the bitwise XOR operation with.
1364
- * @returns Returns the current BigNumber instance after performing the bitwise XOR operation.
1365
- *
1366
- * @example
1367
- * const num1 = new BigNumber('10');
1368
- * const num2 = new BigNumber('20');
1369
- * console.log(num1.ixor(num2).toString());
1370
- */
1371
- ixor (num: BigNumber): BigNumber {
1372
- this.assert(
1373
- (this.negative | num.negative) === 0,
1374
- 'Neither number can be negative'
1375
- )
1376
- return this.iuxor(num)
1377
- }
1378
-
1379
- /**
1380
- * Performs a bitwise XOR operation which returns a new BigNumber, and keeps the bits
1381
- * set in the result only if the corresponding bits in the operands are different.
1382
- *
1383
- * @method xor
1384
- * @param num - The BigNumber to perform the bitwise XOR operation with.
1385
- * @returns Returns a new BigNumber resulting from the bitwise XOR operation.
1386
- *
1387
- * @example
1388
- * const num1 = new BigNumber('10');
1389
- * const num2 = new BigNumber('20');
1390
- * console.log(num1.xor(num2).toString());
1391
- */
1392
- xor (num: BigNumber): BigNumber {
1393
- if (this.length > num.length) return this.clone().ixor(num)
1394
- return num.clone().ixor(this)
1395
- }
1396
-
1397
- /**
1398
- * Performs an unsigned XOR operation on this BigNumber with the supplied BigNumber. Returns a new BigNumber.
1399
- *
1400
- * @method uxor
1401
- * @param num - The BigNumber with which the unsigned bitwise XOR operation is to be performed.
1402
- * @returns Returns a new BigNumber resulting from the unsigned bitwise XOR operation.
1403
- *
1404
- * @example
1405
- * const num1 = new BigNumber('30');
1406
- * const num2 = new BigNumber('40');
1407
- * console.log(num1.uxor(num2).toString()); // Output will be the result of unsigned XOR operation
1408
- */
1409
- uxor (num: BigNumber): BigNumber {
1410
- if (this.length > num.length) return this.clone().iuxor(num)
1411
- return num.clone().iuxor(this)
1412
- }
1413
-
1414
- /**
1415
- * In-place method that performs a bitwise NOT operation on a BigNumber up to a specified bit width.
1416
- *
1417
- * @method inotn
1418
- * @param width - The number of bits to perform the NOT operation on.
1419
- * @returns Returns the BigNumber after performing the bitwise NOT operation.
1420
- *
1421
- * @example
1422
- * const num = new BigNumber('42');
1423
- * num.inotn(10);
1424
- * console.log(num.toString());
1425
- */
1426
- inotn (width: number): BigNumber {
1427
- this.assert(
1428
- typeof width === 'number' && width >= 0,
1429
- 'The width needs to be a number greater than zero'
1430
- )
1431
-
1432
- let bytesNeeded = Math.ceil(width / 26) | 0
1433
- const bitsLeft = width % 26
1434
-
1435
- // Extend the number with leading zeroes
1436
- this.expand(bytesNeeded)
1437
-
1438
- if (bitsLeft > 0) {
1439
- bytesNeeded--
1440
- }
1441
-
1442
- // Handle complete words
1443
- let i = 0
1444
- for (; i < bytesNeeded; i++) {
1445
- this.words[i] = ~this.words[i] & 0x3ffffff
1446
- }
1447
-
1448
- // Handle the residue
1449
- if (bitsLeft > 0) {
1450
- this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft))
1451
- }
1452
-
1453
- // And remove leading zeroes
1454
- return this.strip()
1455
- }
1456
-
1457
- /**
1458
- * Performs a bitwise NOT operation on a BigNumber up to a specified bit width. Returns a new BigNumber.
1459
- *
1460
- * @method notn
1461
- * @param width - The number of bits to perform the NOT operation on.
1462
- * @returns Returns a new BigNumber resulting from the bitwise NOT operation.
1463
- *
1464
- * @example
1465
- * const num = new BigNumber('42');
1466
- * const notnResult = num.notn(10);
1467
- * console.log(notnResult.toString());
1468
- */
1469
- notn (width: number): BigNumber {
1470
- return this.clone().inotn(width)
1471
- }
1472
-
1473
- /**
1474
- * Set `bit` of `this` BigNumber. The `bit` is a position in the binary representation,
1475
- * and `val` is the value to be set at that position (`0` or `1`).
1476
- *
1477
- * @method setn
1478
- * @param bit - The bit position to set.
1479
- * @param val - The value to set at the bit position.
1480
- * @returns Returns the BigNumber after setting the value at the bit position.
1481
- *
1482
- * @example
1483
- * const num = new BigNumber('42');
1484
- * num.setn(2, 1);
1485
- * console.log(num.toString());
1486
- */
1487
- setn (bit: number, val: 0 | 1 | true | false): BigNumber {
1488
- this.assert(typeof bit === 'number' && bit >= 0)
1489
-
1490
- const off = (bit / 26) | 0
1491
- const wbit = bit % 26
1492
-
1493
- this.expand(off + 1)
1494
-
1495
- if (val === 1 || val === true) {
1496
- this.words[off] = this.words[off] | (1 << wbit)
1497
- } else {
1498
- this.words[off] = this.words[off] & ~(1 << wbit)
1499
- }
1500
-
1501
- return this.strip()
1502
- }
1503
-
1504
- /**
1505
- * Add `num` to `this` BigNumber in-place.
1506
- *
1507
- * @method iadd
1508
- * @param num - The BigNumber to add to `this` BigNumber.
1509
- * @returns Returns the BigNumber after performing the addition.
1510
- *
1511
- * @example
1512
- * const num1 = new BigNumber('10');
1513
- * num1.iadd(new BigNumber('20'));
1514
- * console.log(num1.toString());
1515
- */
1516
- iadd (num: BigNumber): BigNumber {
1517
- let r
1518
-
1519
- // negative + positive
1520
- if (this.negative !== 0 && num.negative === 0) {
1521
- this.negative = 0
1522
- r = this.isub(num)
1523
- this.negative ^= 1
1524
- return this.normSign()
1525
-
1526
- // positive + negative
1527
- } else if (this.negative === 0 && num.negative !== 0) {
1528
- num.negative = 0
1529
- r = this.isub(num)
1530
- num.negative = 1
1531
- return r.normSign()
1532
- }
1533
-
1534
- // a.length > b.length
1535
- let a, b
1536
- if (this.length > num.length) {
1537
- /* eslint-disable @typescript-eslint/no-this-alias */
1538
- a = this
1539
- b = num
1540
- } else {
1541
- a = num
1542
- /* eslint-disable @typescript-eslint/no-this-alias */
1543
- b = this
1544
- }
1545
-
1546
- let carry = 0
1547
- let i = 0
1548
- for (; i < b.length; i++) {
1549
- r = (a.words[i] | 0) + (b.words[i] | 0) + carry
1550
- this.words[i] = r & 0x3ffffff
1551
- carry = r >>> 26
1552
- }
1553
- for (; carry !== 0 && i < a.length; i++) {
1554
- r = (a.words[i] | 0) + carry
1555
- this.words[i] = r & 0x3ffffff
1556
- carry = r >>> 26
1557
- }
1558
-
1559
- this.length = a.length
1560
- if (carry !== 0) {
1561
- this.words[this.length] = carry
1562
- this.length++
1563
- // Copy the rest of the words
1564
- } else if (a !== this) {
1565
- for (; i < a.length; i++) {
1566
- this.words[i] = a.words[i]
1567
- }
1568
- }
1569
-
1570
- return this
1571
- }
1572
-
1573
- /**
1574
- * Add `num` to `this` BigNumber.
1575
- *
1576
- * @method add
1577
- * @param num - The BigNumber to add to `this` BigNumber.
1578
- * @returns Returns a new BigNumber which is the result of the addition.
1579
- *
1580
- * @example
1581
- * const num1 = new BigNumber('10');
1582
- * const addResult = num1.add(new BigNumber('20'));
1583
- * console.log(addResult.toString());
1584
- */
1585
- add (num: BigNumber): BigNumber {
1586
- let res
1587
- if (num.negative !== 0 && this.negative === 0) {
1588
- num.negative = 0
1589
- res = this.sub(num)
1590
- num.negative ^= 1
1591
- return res
1592
- } else if (num.negative === 0 && this.negative !== 0) {
1593
- this.negative = 0
1594
- res = num.sub(this)
1595
- this.negative = 1
1596
- return res
1597
- }
1598
-
1599
- if (this.length > num.length) return this.clone().iadd(num)
1600
-
1601
- return num.clone().iadd(this)
1602
- }
1603
-
1604
- /**
1605
- * Subtract `num` from `this` BigNumber in-place.
1606
- *
1607
- * @method isub
1608
- * @param num - The BigNumber to be subtracted from `this` BigNumber.
1609
- * @returns Returns the BigNumber after performing the subtraction.
1610
- *
1611
- * @example
1612
- * const num1 = new BigNumber('20');
1613
- * num1.isub(new BigNumber('10'));
1614
- * console.log(num1.toString());
1615
- */
1616
- isub (num: BigNumber): BigNumber {
1617
- let r: BigNumber | number
1618
- // this - (-num) = this + num
1619
- if (num.negative !== 0) {
1620
- num.negative = 0
1621
- r = this.iadd(num)
1622
- num.negative = 1
1623
- return r.normSign()
1624
-
1625
- // -this - num = -(this + num)
1626
- } else if (this.negative !== 0) {
1627
- this.negative = 0
1628
- this.iadd(num)
1629
- this.negative = 1
1630
- return this.normSign()
1631
- }
1632
-
1633
- // At this point both numbers are positive
1634
- const cmp = this.cmp(num)
1635
-
1636
- // Optimization - zeroify
1637
- if (cmp === 0) {
1638
- this.negative = 0
1639
- this.length = 1
1640
- this.words[0] = 0
1641
- return this
1642
- }
1643
-
1644
- // a > b
1645
- let a, b
1646
- if (cmp > 0) {
1647
- /* eslint-disable @typescript-eslint/no-this-alias */
1648
- a = this
1649
- b = num
1650
- } else {
1651
- a = num
1652
- /* eslint-disable @typescript-eslint/no-this-alias */
1653
- b = this
1654
- }
1655
-
1656
- let carry = 0
1657
- let i = 0
1658
- for (; i < b.length; i++) {
1659
- r = (a.words[i] | 0) - (b.words[i] | 0) + carry
1660
- carry = r >> 26
1661
- this.words[i] = r & 0x3ffffff
1662
- }
1663
- for (; carry !== 0 && i < a.length; i++) {
1664
- r = (a.words[i] | 0) + carry
1665
- carry = r >> 26
1666
- this.words[i] = r & 0x3ffffff
1667
- }
1668
-
1669
- // Copy rest of the words
1670
- if (carry === 0 && i < a.length && a !== this) {
1671
- for (; i < a.length; i++) {
1672
- this.words[i] = a.words[i]
1673
- }
1674
- }
1675
-
1676
- this.length = Math.max(this.length, i)
1677
-
1678
- if (a !== this) {
1679
- this.negative = 1
1680
- }
1681
-
1682
- return this.strip()
1683
- }
1684
-
1685
- /**
1686
- * Subtract `num` from `this` BigNumber.
1687
- *
1688
- * @method sub
1689
- * @param num - The BigNumber to be subtracted from `this` BigNumber.
1690
- * @returns Returns a new BigNumber which is the result of the subtraction.
1691
- *
1692
- * @example
1693
- * const num1 = new BigNumber('20');
1694
- * const subResult = num1.sub(new BigNumber('10'));
1695
- * console.log(subResult.toString());
1696
- */
1697
- sub (num: BigNumber): BigNumber {
1698
- return this.clone().isub(num)
1699
- }
1700
-
1701
- private smallMulTo (
1702
- self: BigNumber, num: BigNumber, out: BigNumber
1703
- ): BigNumber {
1704
- out.negative = num.negative ^ self.negative
1705
- let len = (self.length + num.length) | 0
1706
- out.length = len
1707
- len = (len - 1) | 0
1708
-
1709
- // Peel one iteration (compiler can't do it, because of code complexity)
1710
- let a = self.words[0] | 0
1711
- let b = num.words[0] | 0
1712
- let r = a * b
1713
-
1714
- const lo = r & 0x3ffffff
1715
- let carry = (r / 0x4000000) | 0
1716
- out.words[0] = lo
1717
-
1718
- let k = 1
1719
- for (; k < len; k++) {
1720
- // Sum all words with the same `i + j = k` and accumulate `ncarry`,
1721
- // note that ncarry could be >= 0x3ffffff
1722
- let ncarry = carry >>> 26
1723
- let rword = carry & 0x3ffffff
1724
- const maxJ = Math.min(k, num.length - 1)
1725
- for (let j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
1726
- const i = (k - j) | 0
1727
- a = self.words[i] | 0
1728
- b = num.words[j] | 0
1729
- r = a * b + rword
1730
- ncarry += (r / 0x4000000) | 0
1731
- rword = r & 0x3ffffff
1732
- }
1733
- out.words[k] = rword | 0
1734
- carry = ncarry | 0
1735
- }
1736
- if (carry !== 0) {
1737
- out.words[k] = carry | 0
1738
- } else {
1739
- out.length--
1740
- }
1741
-
1742
- return out.strip()
1743
- }
1744
-
1745
- comb10MulTo (self: BigNumber, num: BigNumber, out: BigNumber): BigNumber {
1746
- const a = self.words
1747
- const b = num.words
1748
- const o = out.words
1749
- let c: number = 0
1750
- let lo: number
1751
- let mid: number
1752
- let hi: number
1753
- const a0 = a[0] | 0
1754
- const al0 = a0 & 0x1fff
1755
- const ah0 = a0 >>> 13
1756
- const a1 = a[1] | 0
1757
- const al1 = a1 & 0x1fff
1758
- const ah1 = a1 >>> 13
1759
- const a2 = a[2] | 0
1760
- const al2 = a2 & 0x1fff
1761
- const ah2 = a2 >>> 13
1762
- const a3 = a[3] | 0
1763
- const al3 = a3 & 0x1fff
1764
- const ah3 = a3 >>> 13
1765
- const a4 = a[4] | 0
1766
- const al4 = a4 & 0x1fff
1767
- const ah4 = a4 >>> 13
1768
- const a5 = a[5] | 0
1769
- const al5 = a5 & 0x1fff
1770
- const ah5 = a5 >>> 13
1771
- const a6 = a[6] | 0
1772
- const al6 = a6 & 0x1fff
1773
- const ah6 = a6 >>> 13
1774
- const a7 = a[7] | 0
1775
- const al7 = a7 & 0x1fff
1776
- const ah7 = a7 >>> 13
1777
- const a8 = a[8] | 0
1778
- const al8 = a8 & 0x1fff
1779
- const ah8 = a8 >>> 13
1780
- const a9 = a[9] | 0
1781
- const al9 = a9 & 0x1fff
1782
- const ah9 = a9 >>> 13
1783
- const b0 = b[0] | 0
1784
- const bl0 = b0 & 0x1fff
1785
- const bh0 = b0 >>> 13
1786
- const b1 = b[1] | 0
1787
- const bl1 = b1 & 0x1fff
1788
- const bh1 = b1 >>> 13
1789
- const b2 = b[2] | 0
1790
- const bl2 = b2 & 0x1fff
1791
- const bh2 = b2 >>> 13
1792
- const b3 = b[3] | 0
1793
- const bl3 = b3 & 0x1fff
1794
- const bh3 = b3 >>> 13
1795
- const b4 = b[4] | 0
1796
- const bl4 = b4 & 0x1fff
1797
- const bh4 = b4 >>> 13
1798
- const b5 = b[5] | 0
1799
- const bl5 = b5 & 0x1fff
1800
- const bh5 = b5 >>> 13
1801
- const b6 = b[6] | 0
1802
- const bl6 = b6 & 0x1fff
1803
- const bh6 = b6 >>> 13
1804
- const b7 = b[7] | 0
1805
- const bl7 = b7 & 0x1fff
1806
- const bh7 = b7 >>> 13
1807
- const b8 = b[8] | 0
1808
- const bl8 = b8 & 0x1fff
1809
- const bh8 = b8 >>> 13
1810
- const b9 = b[9] | 0
1811
- const bl9 = b9 & 0x1fff
1812
- const bh9 = b9 >>> 13
1813
-
1814
- out.negative = self.negative ^ num.negative
1815
- out.length = 19
1816
- /* k = 0 */
1817
- lo = Math.imul(al0, bl0)
1818
- mid = Math.imul(al0, bh0)
1819
- mid = (mid + Math.imul(ah0, bl0)) | 0
1820
- hi = Math.imul(ah0, bh0)
1821
- let w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
1822
- c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0
1823
- w0 &= 0x3ffffff
1824
- /* k = 1 */
1825
- lo = Math.imul(al1, bl0)
1826
- mid = Math.imul(al1, bh0)
1827
- mid = (mid + Math.imul(ah1, bl0)) | 0
1828
- hi = Math.imul(ah1, bh0)
1829
- lo = (lo + Math.imul(al0, bl1)) | 0
1830
- mid = (mid + Math.imul(al0, bh1)) | 0
1831
- mid = (mid + Math.imul(ah0, bl1)) | 0
1832
- hi = (hi + Math.imul(ah0, bh1)) | 0
1833
- let w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
1834
- c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0
1835
- w1 &= 0x3ffffff
1836
- /* k = 2 */
1837
- lo = Math.imul(al2, bl0)
1838
- mid = Math.imul(al2, bh0)
1839
- mid = (mid + Math.imul(ah2, bl0)) | 0
1840
- hi = Math.imul(ah2, bh0)
1841
- lo = (lo + Math.imul(al1, bl1)) | 0
1842
- mid = (mid + Math.imul(al1, bh1)) | 0
1843
- mid = (mid + Math.imul(ah1, bl1)) | 0
1844
- hi = (hi + Math.imul(ah1, bh1)) | 0
1845
- lo = (lo + Math.imul(al0, bl2)) | 0
1846
- mid = (mid + Math.imul(al0, bh2)) | 0
1847
- mid = (mid + Math.imul(ah0, bl2)) | 0
1848
- hi = (hi + Math.imul(ah0, bh2)) | 0
1849
- let w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
1850
- c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0
1851
- w2 &= 0x3ffffff
1852
- /* k = 3 */
1853
- lo = Math.imul(al3, bl0)
1854
- mid = Math.imul(al3, bh0)
1855
- mid = (mid + Math.imul(ah3, bl0)) | 0
1856
- hi = Math.imul(ah3, bh0)
1857
- lo = (lo + Math.imul(al2, bl1)) | 0
1858
- mid = (mid + Math.imul(al2, bh1)) | 0
1859
- mid = (mid + Math.imul(ah2, bl1)) | 0
1860
- hi = (hi + Math.imul(ah2, bh1)) | 0
1861
- lo = (lo + Math.imul(al1, bl2)) | 0
1862
- mid = (mid + Math.imul(al1, bh2)) | 0
1863
- mid = (mid + Math.imul(ah1, bl2)) | 0
1864
- hi = (hi + Math.imul(ah1, bh2)) | 0
1865
- lo = (lo + Math.imul(al0, bl3)) | 0
1866
- mid = (mid + Math.imul(al0, bh3)) | 0
1867
- mid = (mid + Math.imul(ah0, bl3)) | 0
1868
- hi = (hi + Math.imul(ah0, bh3)) | 0
1869
- let w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
1870
- c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0
1871
- w3 &= 0x3ffffff
1872
- /* k = 4 */
1873
- lo = Math.imul(al4, bl0)
1874
- mid = Math.imul(al4, bh0)
1875
- mid = (mid + Math.imul(ah4, bl0)) | 0
1876
- hi = Math.imul(ah4, bh0)
1877
- lo = (lo + Math.imul(al3, bl1)) | 0
1878
- mid = (mid + Math.imul(al3, bh1)) | 0
1879
- mid = (mid + Math.imul(ah3, bl1)) | 0
1880
- hi = (hi + Math.imul(ah3, bh1)) | 0
1881
- lo = (lo + Math.imul(al2, bl2)) | 0
1882
- mid = (mid + Math.imul(al2, bh2)) | 0
1883
- mid = (mid + Math.imul(ah2, bl2)) | 0
1884
- hi = (hi + Math.imul(ah2, bh2)) | 0
1885
- lo = (lo + Math.imul(al1, bl3)) | 0
1886
- mid = (mid + Math.imul(al1, bh3)) | 0
1887
- mid = (mid + Math.imul(ah1, bl3)) | 0
1888
- hi = (hi + Math.imul(ah1, bh3)) | 0
1889
- lo = (lo + Math.imul(al0, bl4)) | 0
1890
- mid = (mid + Math.imul(al0, bh4)) | 0
1891
- mid = (mid + Math.imul(ah0, bl4)) | 0
1892
- hi = (hi + Math.imul(ah0, bh4)) | 0
1893
- let w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
1894
- c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0
1895
- w4 &= 0x3ffffff
1896
- /* k = 5 */
1897
- lo = Math.imul(al5, bl0)
1898
- mid = Math.imul(al5, bh0)
1899
- mid = (mid + Math.imul(ah5, bl0)) | 0
1900
- hi = Math.imul(ah5, bh0)
1901
- lo = (lo + Math.imul(al4, bl1)) | 0
1902
- mid = (mid + Math.imul(al4, bh1)) | 0
1903
- mid = (mid + Math.imul(ah4, bl1)) | 0
1904
- hi = (hi + Math.imul(ah4, bh1)) | 0
1905
- lo = (lo + Math.imul(al3, bl2)) | 0
1906
- mid = (mid + Math.imul(al3, bh2)) | 0
1907
- mid = (mid + Math.imul(ah3, bl2)) | 0
1908
- hi = (hi + Math.imul(ah3, bh2)) | 0
1909
- lo = (lo + Math.imul(al2, bl3)) | 0
1910
- mid = (mid + Math.imul(al2, bh3)) | 0
1911
- mid = (mid + Math.imul(ah2, bl3)) | 0
1912
- hi = (hi + Math.imul(ah2, bh3)) | 0
1913
- lo = (lo + Math.imul(al1, bl4)) | 0
1914
- mid = (mid + Math.imul(al1, bh4)) | 0
1915
- mid = (mid + Math.imul(ah1, bl4)) | 0
1916
- hi = (hi + Math.imul(ah1, bh4)) | 0
1917
- lo = (lo + Math.imul(al0, bl5)) | 0
1918
- mid = (mid + Math.imul(al0, bh5)) | 0
1919
- mid = (mid + Math.imul(ah0, bl5)) | 0
1920
- hi = (hi + Math.imul(ah0, bh5)) | 0
1921
- let w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
1922
- c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0
1923
- w5 &= 0x3ffffff
1924
- /* k = 6 */
1925
- lo = Math.imul(al6, bl0)
1926
- mid = Math.imul(al6, bh0)
1927
- mid = (mid + Math.imul(ah6, bl0)) | 0
1928
- hi = Math.imul(ah6, bh0)
1929
- lo = (lo + Math.imul(al5, bl1)) | 0
1930
- mid = (mid + Math.imul(al5, bh1)) | 0
1931
- mid = (mid + Math.imul(ah5, bl1)) | 0
1932
- hi = (hi + Math.imul(ah5, bh1)) | 0
1933
- lo = (lo + Math.imul(al4, bl2)) | 0
1934
- mid = (mid + Math.imul(al4, bh2)) | 0
1935
- mid = (mid + Math.imul(ah4, bl2)) | 0
1936
- hi = (hi + Math.imul(ah4, bh2)) | 0
1937
- lo = (lo + Math.imul(al3, bl3)) | 0
1938
- mid = (mid + Math.imul(al3, bh3)) | 0
1939
- mid = (mid + Math.imul(ah3, bl3)) | 0
1940
- hi = (hi + Math.imul(ah3, bh3)) | 0
1941
- lo = (lo + Math.imul(al2, bl4)) | 0
1942
- mid = (mid + Math.imul(al2, bh4)) | 0
1943
- mid = (mid + Math.imul(ah2, bl4)) | 0
1944
- hi = (hi + Math.imul(ah2, bh4)) | 0
1945
- lo = (lo + Math.imul(al1, bl5)) | 0
1946
- mid = (mid + Math.imul(al1, bh5)) | 0
1947
- mid = (mid + Math.imul(ah1, bl5)) | 0
1948
- hi = (hi + Math.imul(ah1, bh5)) | 0
1949
- lo = (lo + Math.imul(al0, bl6)) | 0
1950
- mid = (mid + Math.imul(al0, bh6)) | 0
1951
- mid = (mid + Math.imul(ah0, bl6)) | 0
1952
- hi = (hi + Math.imul(ah0, bh6)) | 0
1953
- let w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
1954
- c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0
1955
- w6 &= 0x3ffffff
1956
- /* k = 7 */
1957
- lo = Math.imul(al7, bl0)
1958
- mid = Math.imul(al7, bh0)
1959
- mid = (mid + Math.imul(ah7, bl0)) | 0
1960
- hi = Math.imul(ah7, bh0)
1961
- lo = (lo + Math.imul(al6, bl1)) | 0
1962
- mid = (mid + Math.imul(al6, bh1)) | 0
1963
- mid = (mid + Math.imul(ah6, bl1)) | 0
1964
- hi = (hi + Math.imul(ah6, bh1)) | 0
1965
- lo = (lo + Math.imul(al5, bl2)) | 0
1966
- mid = (mid + Math.imul(al5, bh2)) | 0
1967
- mid = (mid + Math.imul(ah5, bl2)) | 0
1968
- hi = (hi + Math.imul(ah5, bh2)) | 0
1969
- lo = (lo + Math.imul(al4, bl3)) | 0
1970
- mid = (mid + Math.imul(al4, bh3)) | 0
1971
- mid = (mid + Math.imul(ah4, bl3)) | 0
1972
- hi = (hi + Math.imul(ah4, bh3)) | 0
1973
- lo = (lo + Math.imul(al3, bl4)) | 0
1974
- mid = (mid + Math.imul(al3, bh4)) | 0
1975
- mid = (mid + Math.imul(ah3, bl4)) | 0
1976
- hi = (hi + Math.imul(ah3, bh4)) | 0
1977
- lo = (lo + Math.imul(al2, bl5)) | 0
1978
- mid = (mid + Math.imul(al2, bh5)) | 0
1979
- mid = (mid + Math.imul(ah2, bl5)) | 0
1980
- hi = (hi + Math.imul(ah2, bh5)) | 0
1981
- lo = (lo + Math.imul(al1, bl6)) | 0
1982
- mid = (mid + Math.imul(al1, bh6)) | 0
1983
- mid = (mid + Math.imul(ah1, bl6)) | 0
1984
- hi = (hi + Math.imul(ah1, bh6)) | 0
1985
- lo = (lo + Math.imul(al0, bl7)) | 0
1986
- mid = (mid + Math.imul(al0, bh7)) | 0
1987
- mid = (mid + Math.imul(ah0, bl7)) | 0
1988
- hi = (hi + Math.imul(ah0, bh7)) | 0
1989
- let w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
1990
- c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0
1991
- w7 &= 0x3ffffff
1992
- /* k = 8 */
1993
- lo = Math.imul(al8, bl0)
1994
- mid = Math.imul(al8, bh0)
1995
- mid = (mid + Math.imul(ah8, bl0)) | 0
1996
- hi = Math.imul(ah8, bh0)
1997
- lo = (lo + Math.imul(al7, bl1)) | 0
1998
- mid = (mid + Math.imul(al7, bh1)) | 0
1999
- mid = (mid + Math.imul(ah7, bl1)) | 0
2000
- hi = (hi + Math.imul(ah7, bh1)) | 0
2001
- lo = (lo + Math.imul(al6, bl2)) | 0
2002
- mid = (mid + Math.imul(al6, bh2)) | 0
2003
- mid = (mid + Math.imul(ah6, bl2)) | 0
2004
- hi = (hi + Math.imul(ah6, bh2)) | 0
2005
- lo = (lo + Math.imul(al5, bl3)) | 0
2006
- mid = (mid + Math.imul(al5, bh3)) | 0
2007
- mid = (mid + Math.imul(ah5, bl3)) | 0
2008
- hi = (hi + Math.imul(ah5, bh3)) | 0
2009
- lo = (lo + Math.imul(al4, bl4)) | 0
2010
- mid = (mid + Math.imul(al4, bh4)) | 0
2011
- mid = (mid + Math.imul(ah4, bl4)) | 0
2012
- hi = (hi + Math.imul(ah4, bh4)) | 0
2013
- lo = (lo + Math.imul(al3, bl5)) | 0
2014
- mid = (mid + Math.imul(al3, bh5)) | 0
2015
- mid = (mid + Math.imul(ah3, bl5)) | 0
2016
- hi = (hi + Math.imul(ah3, bh5)) | 0
2017
- lo = (lo + Math.imul(al2, bl6)) | 0
2018
- mid = (mid + Math.imul(al2, bh6)) | 0
2019
- mid = (mid + Math.imul(ah2, bl6)) | 0
2020
- hi = (hi + Math.imul(ah2, bh6)) | 0
2021
- lo = (lo + Math.imul(al1, bl7)) | 0
2022
- mid = (mid + Math.imul(al1, bh7)) | 0
2023
- mid = (mid + Math.imul(ah1, bl7)) | 0
2024
- hi = (hi + Math.imul(ah1, bh7)) | 0
2025
- lo = (lo + Math.imul(al0, bl8)) | 0
2026
- mid = (mid + Math.imul(al0, bh8)) | 0
2027
- mid = (mid + Math.imul(ah0, bl8)) | 0
2028
- hi = (hi + Math.imul(ah0, bh8)) | 0
2029
- let w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2030
- c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0
2031
- w8 &= 0x3ffffff
2032
- /* k = 9 */
2033
- lo = Math.imul(al9, bl0)
2034
- mid = Math.imul(al9, bh0)
2035
- mid = (mid + Math.imul(ah9, bl0)) | 0
2036
- hi = Math.imul(ah9, bh0)
2037
- lo = (lo + Math.imul(al8, bl1)) | 0
2038
- mid = (mid + Math.imul(al8, bh1)) | 0
2039
- mid = (mid + Math.imul(ah8, bl1)) | 0
2040
- hi = (hi + Math.imul(ah8, bh1)) | 0
2041
- lo = (lo + Math.imul(al7, bl2)) | 0
2042
- mid = (mid + Math.imul(al7, bh2)) | 0
2043
- mid = (mid + Math.imul(ah7, bl2)) | 0
2044
- hi = (hi + Math.imul(ah7, bh2)) | 0
2045
- lo = (lo + Math.imul(al6, bl3)) | 0
2046
- mid = (mid + Math.imul(al6, bh3)) | 0
2047
- mid = (mid + Math.imul(ah6, bl3)) | 0
2048
- hi = (hi + Math.imul(ah6, bh3)) | 0
2049
- lo = (lo + Math.imul(al5, bl4)) | 0
2050
- mid = (mid + Math.imul(al5, bh4)) | 0
2051
- mid = (mid + Math.imul(ah5, bl4)) | 0
2052
- hi = (hi + Math.imul(ah5, bh4)) | 0
2053
- lo = (lo + Math.imul(al4, bl5)) | 0
2054
- mid = (mid + Math.imul(al4, bh5)) | 0
2055
- mid = (mid + Math.imul(ah4, bl5)) | 0
2056
- hi = (hi + Math.imul(ah4, bh5)) | 0
2057
- lo = (lo + Math.imul(al3, bl6)) | 0
2058
- mid = (mid + Math.imul(al3, bh6)) | 0
2059
- mid = (mid + Math.imul(ah3, bl6)) | 0
2060
- hi = (hi + Math.imul(ah3, bh6)) | 0
2061
- lo = (lo + Math.imul(al2, bl7)) | 0
2062
- mid = (mid + Math.imul(al2, bh7)) | 0
2063
- mid = (mid + Math.imul(ah2, bl7)) | 0
2064
- hi = (hi + Math.imul(ah2, bh7)) | 0
2065
- lo = (lo + Math.imul(al1, bl8)) | 0
2066
- mid = (mid + Math.imul(al1, bh8)) | 0
2067
- mid = (mid + Math.imul(ah1, bl8)) | 0
2068
- hi = (hi + Math.imul(ah1, bh8)) | 0
2069
- lo = (lo + Math.imul(al0, bl9)) | 0
2070
- mid = (mid + Math.imul(al0, bh9)) | 0
2071
- mid = (mid + Math.imul(ah0, bl9)) | 0
2072
- hi = (hi + Math.imul(ah0, bh9)) | 0
2073
- let w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2074
- c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0
2075
- w9 &= 0x3ffffff
2076
- /* k = 10 */
2077
- lo = Math.imul(al9, bl1)
2078
- mid = Math.imul(al9, bh1)
2079
- mid = (mid + Math.imul(ah9, bl1)) | 0
2080
- hi = Math.imul(ah9, bh1)
2081
- lo = (lo + Math.imul(al8, bl2)) | 0
2082
- mid = (mid + Math.imul(al8, bh2)) | 0
2083
- mid = (mid + Math.imul(ah8, bl2)) | 0
2084
- hi = (hi + Math.imul(ah8, bh2)) | 0
2085
- lo = (lo + Math.imul(al7, bl3)) | 0
2086
- mid = (mid + Math.imul(al7, bh3)) | 0
2087
- mid = (mid + Math.imul(ah7, bl3)) | 0
2088
- hi = (hi + Math.imul(ah7, bh3)) | 0
2089
- lo = (lo + Math.imul(al6, bl4)) | 0
2090
- mid = (mid + Math.imul(al6, bh4)) | 0
2091
- mid = (mid + Math.imul(ah6, bl4)) | 0
2092
- hi = (hi + Math.imul(ah6, bh4)) | 0
2093
- lo = (lo + Math.imul(al5, bl5)) | 0
2094
- mid = (mid + Math.imul(al5, bh5)) | 0
2095
- mid = (mid + Math.imul(ah5, bl5)) | 0
2096
- hi = (hi + Math.imul(ah5, bh5)) | 0
2097
- lo = (lo + Math.imul(al4, bl6)) | 0
2098
- mid = (mid + Math.imul(al4, bh6)) | 0
2099
- mid = (mid + Math.imul(ah4, bl6)) | 0
2100
- hi = (hi + Math.imul(ah4, bh6)) | 0
2101
- lo = (lo + Math.imul(al3, bl7)) | 0
2102
- mid = (mid + Math.imul(al3, bh7)) | 0
2103
- mid = (mid + Math.imul(ah3, bl7)) | 0
2104
- hi = (hi + Math.imul(ah3, bh7)) | 0
2105
- lo = (lo + Math.imul(al2, bl8)) | 0
2106
- mid = (mid + Math.imul(al2, bh8)) | 0
2107
- mid = (mid + Math.imul(ah2, bl8)) | 0
2108
- hi = (hi + Math.imul(ah2, bh8)) | 0
2109
- lo = (lo + Math.imul(al1, bl9)) | 0
2110
- mid = (mid + Math.imul(al1, bh9)) | 0
2111
- mid = (mid + Math.imul(ah1, bl9)) | 0
2112
- hi = (hi + Math.imul(ah1, bh9)) | 0
2113
- let w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2114
- c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0
2115
- w10 &= 0x3ffffff
2116
- /* k = 11 */
2117
- lo = Math.imul(al9, bl2)
2118
- mid = Math.imul(al9, bh2)
2119
- mid = (mid + Math.imul(ah9, bl2)) | 0
2120
- hi = Math.imul(ah9, bh2)
2121
- lo = (lo + Math.imul(al8, bl3)) | 0
2122
- mid = (mid + Math.imul(al8, bh3)) | 0
2123
- mid = (mid + Math.imul(ah8, bl3)) | 0
2124
- hi = (hi + Math.imul(ah8, bh3)) | 0
2125
- lo = (lo + Math.imul(al7, bl4)) | 0
2126
- mid = (mid + Math.imul(al7, bh4)) | 0
2127
- mid = (mid + Math.imul(ah7, bl4)) | 0
2128
- hi = (hi + Math.imul(ah7, bh4)) | 0
2129
- lo = (lo + Math.imul(al6, bl5)) | 0
2130
- mid = (mid + Math.imul(al6, bh5)) | 0
2131
- mid = (mid + Math.imul(ah6, bl5)) | 0
2132
- hi = (hi + Math.imul(ah6, bh5)) | 0
2133
- lo = (lo + Math.imul(al5, bl6)) | 0
2134
- mid = (mid + Math.imul(al5, bh6)) | 0
2135
- mid = (mid + Math.imul(ah5, bl6)) | 0
2136
- hi = (hi + Math.imul(ah5, bh6)) | 0
2137
- lo = (lo + Math.imul(al4, bl7)) | 0
2138
- mid = (mid + Math.imul(al4, bh7)) | 0
2139
- mid = (mid + Math.imul(ah4, bl7)) | 0
2140
- hi = (hi + Math.imul(ah4, bh7)) | 0
2141
- lo = (lo + Math.imul(al3, bl8)) | 0
2142
- mid = (mid + Math.imul(al3, bh8)) | 0
2143
- mid = (mid + Math.imul(ah3, bl8)) | 0
2144
- hi = (hi + Math.imul(ah3, bh8)) | 0
2145
- lo = (lo + Math.imul(al2, bl9)) | 0
2146
- mid = (mid + Math.imul(al2, bh9)) | 0
2147
- mid = (mid + Math.imul(ah2, bl9)) | 0
2148
- hi = (hi + Math.imul(ah2, bh9)) | 0
2149
- let w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2150
- c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0
2151
- w11 &= 0x3ffffff
2152
- /* k = 12 */
2153
- lo = Math.imul(al9, bl3)
2154
- mid = Math.imul(al9, bh3)
2155
- mid = (mid + Math.imul(ah9, bl3)) | 0
2156
- hi = Math.imul(ah9, bh3)
2157
- lo = (lo + Math.imul(al8, bl4)) | 0
2158
- mid = (mid + Math.imul(al8, bh4)) | 0
2159
- mid = (mid + Math.imul(ah8, bl4)) | 0
2160
- hi = (hi + Math.imul(ah8, bh4)) | 0
2161
- lo = (lo + Math.imul(al7, bl5)) | 0
2162
- mid = (mid + Math.imul(al7, bh5)) | 0
2163
- mid = (mid + Math.imul(ah7, bl5)) | 0
2164
- hi = (hi + Math.imul(ah7, bh5)) | 0
2165
- lo = (lo + Math.imul(al6, bl6)) | 0
2166
- mid = (mid + Math.imul(al6, bh6)) | 0
2167
- mid = (mid + Math.imul(ah6, bl6)) | 0
2168
- hi = (hi + Math.imul(ah6, bh6)) | 0
2169
- lo = (lo + Math.imul(al5, bl7)) | 0
2170
- mid = (mid + Math.imul(al5, bh7)) | 0
2171
- mid = (mid + Math.imul(ah5, bl7)) | 0
2172
- hi = (hi + Math.imul(ah5, bh7)) | 0
2173
- lo = (lo + Math.imul(al4, bl8)) | 0
2174
- mid = (mid + Math.imul(al4, bh8)) | 0
2175
- mid = (mid + Math.imul(ah4, bl8)) | 0
2176
- hi = (hi + Math.imul(ah4, bh8)) | 0
2177
- lo = (lo + Math.imul(al3, bl9)) | 0
2178
- mid = (mid + Math.imul(al3, bh9)) | 0
2179
- mid = (mid + Math.imul(ah3, bl9)) | 0
2180
- hi = (hi + Math.imul(ah3, bh9)) | 0
2181
- let w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2182
- c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0
2183
- w12 &= 0x3ffffff
2184
- /* k = 13 */
2185
- lo = Math.imul(al9, bl4)
2186
- mid = Math.imul(al9, bh4)
2187
- mid = (mid + Math.imul(ah9, bl4)) | 0
2188
- hi = Math.imul(ah9, bh4)
2189
- lo = (lo + Math.imul(al8, bl5)) | 0
2190
- mid = (mid + Math.imul(al8, bh5)) | 0
2191
- mid = (mid + Math.imul(ah8, bl5)) | 0
2192
- hi = (hi + Math.imul(ah8, bh5)) | 0
2193
- lo = (lo + Math.imul(al7, bl6)) | 0
2194
- mid = (mid + Math.imul(al7, bh6)) | 0
2195
- mid = (mid + Math.imul(ah7, bl6)) | 0
2196
- hi = (hi + Math.imul(ah7, bh6)) | 0
2197
- lo = (lo + Math.imul(al6, bl7)) | 0
2198
- mid = (mid + Math.imul(al6, bh7)) | 0
2199
- mid = (mid + Math.imul(ah6, bl7)) | 0
2200
- hi = (hi + Math.imul(ah6, bh7)) | 0
2201
- lo = (lo + Math.imul(al5, bl8)) | 0
2202
- mid = (mid + Math.imul(al5, bh8)) | 0
2203
- mid = (mid + Math.imul(ah5, bl8)) | 0
2204
- hi = (hi + Math.imul(ah5, bh8)) | 0
2205
- lo = (lo + Math.imul(al4, bl9)) | 0
2206
- mid = (mid + Math.imul(al4, bh9)) | 0
2207
- mid = (mid + Math.imul(ah4, bl9)) | 0
2208
- hi = (hi + Math.imul(ah4, bh9)) | 0
2209
- let w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2210
- c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0
2211
- w13 &= 0x3ffffff
2212
- /* k = 14 */
2213
- lo = Math.imul(al9, bl5)
2214
- mid = Math.imul(al9, bh5)
2215
- mid = (mid + Math.imul(ah9, bl5)) | 0
2216
- hi = Math.imul(ah9, bh5)
2217
- lo = (lo + Math.imul(al8, bl6)) | 0
2218
- mid = (mid + Math.imul(al8, bh6)) | 0
2219
- mid = (mid + Math.imul(ah8, bl6)) | 0
2220
- hi = (hi + Math.imul(ah8, bh6)) | 0
2221
- lo = (lo + Math.imul(al7, bl7)) | 0
2222
- mid = (mid + Math.imul(al7, bh7)) | 0
2223
- mid = (mid + Math.imul(ah7, bl7)) | 0
2224
- hi = (hi + Math.imul(ah7, bh7)) | 0
2225
- lo = (lo + Math.imul(al6, bl8)) | 0
2226
- mid = (mid + Math.imul(al6, bh8)) | 0
2227
- mid = (mid + Math.imul(ah6, bl8)) | 0
2228
- hi = (hi + Math.imul(ah6, bh8)) | 0
2229
- lo = (lo + Math.imul(al5, bl9)) | 0
2230
- mid = (mid + Math.imul(al5, bh9)) | 0
2231
- mid = (mid + Math.imul(ah5, bl9)) | 0
2232
- hi = (hi + Math.imul(ah5, bh9)) | 0
2233
- let w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2234
- c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0
2235
- w14 &= 0x3ffffff
2236
- /* k = 15 */
2237
- lo = Math.imul(al9, bl6)
2238
- mid = Math.imul(al9, bh6)
2239
- mid = (mid + Math.imul(ah9, bl6)) | 0
2240
- hi = Math.imul(ah9, bh6)
2241
- lo = (lo + Math.imul(al8, bl7)) | 0
2242
- mid = (mid + Math.imul(al8, bh7)) | 0
2243
- mid = (mid + Math.imul(ah8, bl7)) | 0
2244
- hi = (hi + Math.imul(ah8, bh7)) | 0
2245
- lo = (lo + Math.imul(al7, bl8)) | 0
2246
- mid = (mid + Math.imul(al7, bh8)) | 0
2247
- mid = (mid + Math.imul(ah7, bl8)) | 0
2248
- hi = (hi + Math.imul(ah7, bh8)) | 0
2249
- lo = (lo + Math.imul(al6, bl9)) | 0
2250
- mid = (mid + Math.imul(al6, bh9)) | 0
2251
- mid = (mid + Math.imul(ah6, bl9)) | 0
2252
- hi = (hi + Math.imul(ah6, bh9)) | 0
2253
- let w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2254
- c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0
2255
- w15 &= 0x3ffffff
2256
- /* k = 16 */
2257
- lo = Math.imul(al9, bl7)
2258
- mid = Math.imul(al9, bh7)
2259
- mid = (mid + Math.imul(ah9, bl7)) | 0
2260
- hi = Math.imul(ah9, bh7)
2261
- lo = (lo + Math.imul(al8, bl8)) | 0
2262
- mid = (mid + Math.imul(al8, bh8)) | 0
2263
- mid = (mid + Math.imul(ah8, bl8)) | 0
2264
- hi = (hi + Math.imul(ah8, bh8)) | 0
2265
- lo = (lo + Math.imul(al7, bl9)) | 0
2266
- mid = (mid + Math.imul(al7, bh9)) | 0
2267
- mid = (mid + Math.imul(ah7, bl9)) | 0
2268
- hi = (hi + Math.imul(ah7, bh9)) | 0
2269
- let w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2270
- c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0
2271
- w16 &= 0x3ffffff
2272
- /* k = 17 */
2273
- lo = Math.imul(al9, bl8)
2274
- mid = Math.imul(al9, bh8)
2275
- mid = (mid + Math.imul(ah9, bl8)) | 0
2276
- hi = Math.imul(ah9, bh8)
2277
- lo = (lo + Math.imul(al8, bl9)) | 0
2278
- mid = (mid + Math.imul(al8, bh9)) | 0
2279
- mid = (mid + Math.imul(ah8, bl9)) | 0
2280
- hi = (hi + Math.imul(ah8, bh9)) | 0
2281
- let w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2282
- c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0
2283
- w17 &= 0x3ffffff
2284
- /* k = 18 */
2285
- lo = Math.imul(al9, bl9)
2286
- mid = Math.imul(al9, bh9)
2287
- mid = (mid + Math.imul(ah9, bl9)) | 0
2288
- hi = Math.imul(ah9, bh9)
2289
- let w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0
2290
- c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0
2291
- w18 &= 0x3ffffff
2292
- o[0] = w0
2293
- o[1] = w1
2294
- o[2] = w2
2295
- o[3] = w3
2296
- o[4] = w4
2297
- o[5] = w5
2298
- o[6] = w6
2299
- o[7] = w7
2300
- o[8] = w8
2301
- o[9] = w9
2302
- o[10] = w10
2303
- o[11] = w11
2304
- o[12] = w12
2305
- o[13] = w13
2306
- o[14] = w14
2307
- o[15] = w15
2308
- o[16] = w16
2309
- o[17] = w17
2310
- o[18] = w18
2311
- if (c !== 0) {
2312
- o[19] = c
2313
- out.length++
2314
- }
2315
- return out
2316
- }
2317
-
2318
- private bigMulTo (self: BigNumber, num: BigNumber, out: BigNumber): BigNumber {
2319
- out.negative = num.negative ^ self.negative
2320
- out.length = self.length + num.length
2321
-
2322
- let carry = 0
2323
- let hncarry = 0
2324
- let k = 0
2325
- for (; k < out.length - 1; k++) {
2326
- // Sum all words with the same `i + j = k` and accumulate `ncarry`,
2327
- // note that ncarry could be >= 0x3ffffff
2328
- let ncarry = hncarry
2329
- hncarry = 0
2330
- let rword = carry & 0x3ffffff
2331
- const maxJ = Math.min(k, num.length - 1)
2332
- for (let j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
2333
- const i = k - j
2334
- const a = self.words[i] | 0
2335
- const b = num.words[j] | 0
2336
- const r = a * b
2337
-
2338
- let lo = r & 0x3ffffff
2339
- ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0
2340
- lo = (lo + rword) | 0
2341
- rword = lo & 0x3ffffff
2342
- ncarry = (ncarry + (lo >>> 26)) | 0
2343
-
2344
- hncarry += ncarry >>> 26
2345
- ncarry &= 0x3ffffff
2346
- }
2347
- out.words[k] = rword
2348
- carry = ncarry
2349
- ncarry = hncarry
2350
- }
2351
- if (carry !== 0) {
2352
- out.words[k] = carry
2353
- } else {
2354
- out.length--
2355
- }
2356
-
2357
- return out.strip()
2358
- }
2359
-
2360
- /**
2361
- * Performs multiplication between the BigNumber instance and a given BigNumber.
2362
- * It chooses the multiplication method based on the lengths of the numbers to optimize execution time.
2363
- *
2364
- * @method mulTo
2365
- * @param num - The BigNumber multiply with.
2366
- * @param out - The BigNumber where to store the result.
2367
- * @returns The BigNumber resulting from the multiplication operation.
2368
- *
2369
- * @example
2370
- * const bn1 = new BigNumber('12345');
2371
- * const bn2 = new BigNumber('23456');
2372
- * const output = new BigNumber();
2373
- * bn1.mulTo(bn2, output);
2374
- */
2375
- mulTo (num: BigNumber, out: BigNumber): BigNumber {
2376
- let res
2377
- const len = this.length + num.length
2378
- if (this.length === 10 && num.length === 10) {
2379
- res = this.comb10MulTo(this, num, out)
2380
- } else if (len < 63) {
2381
- res = this.smallMulTo(this, num, out)
2382
- } else {
2383
- res = this.bigMulTo(this, num, out)
2384
- }
2385
-
2386
- return res
2387
- }
2388
-
2389
- /**
2390
- * Performs multiplication between the BigNumber instance and a given BigNumber.
2391
- * It creates a new BigNumber to store the result.
2392
- *
2393
- * @method mul
2394
- * @param num - The BigNumber to multiply with.
2395
- * @returns The BigNumber resulting from the multiplication operation.
2396
- *
2397
- * @example
2398
- * const bn1 = new BigNumber('12345');
2399
- * const bn2 = new BigNumber('23456');
2400
- * const result = bn1.mul(bn2);
2401
- */
2402
- mul (num: BigNumber): BigNumber {
2403
- const out = new BigNumber()
2404
- out.words = new Array(this.length + num.length)
2405
- return this.mulTo(num, out)
2406
- }
2407
-
2408
- /**
2409
- * Performs an in-place multiplication of the BigNumber instance by a given BigNumber.
2410
- *
2411
- * @method imul
2412
- * @param num - The BigNumber to multiply with.
2413
- * @returns The BigNumber itself after the multiplication.
2414
- *
2415
- * @example
2416
- * const bn1 = new BigNumber('12345');
2417
- * const bn2 = new BigNumber('23456');
2418
- * bn1.imul(bn2);
2419
- */
2420
- imul (num: BigNumber): BigNumber {
2421
- return this.clone().mulTo(num, this)
2422
- }
2423
-
2424
- /**
2425
- * Performs an in-place multiplication of the BigNumber instance by a number.
2426
- * This method asserts the input to be a number less than 0x4000000 to prevent overflowing.
2427
- * If negavtive number is provided, the resulting BigNumber will be inversely negative.
2428
- *
2429
- * @method imuln
2430
- * @param num - The number to multiply with.
2431
- * @returns The BigNumber itself after the multiplication.
2432
- *
2433
- * @example
2434
- * const bn = new BigNumber('12345');
2435
- * bn.imuln(23456);
2436
- */
2437
- imuln (num: number): BigNumber {
2438
- const isNegNum = num < 0
2439
- if (isNegNum) num = -num
2440
-
2441
- this.assert(typeof num === 'number')
2442
- this.assert(num < 0x4000000)
2443
-
2444
- // Carry
2445
- let carry = 0
2446
- let i = 0
2447
- for (; i < this.length; i++) {
2448
- const w = (this.words[i] | 0) * num
2449
- const lo = (w & 0x3ffffff) + (carry & 0x3ffffff)
2450
- carry >>= 26
2451
- carry += (w / 0x4000000) | 0
2452
- // NOTE: lo is 27bit maximum
2453
- carry += lo >>> 26
2454
- this.words[i] = lo & 0x3ffffff
2455
- }
2456
-
2457
- if (carry !== 0) {
2458
- this.words[i] = carry
2459
- this.length++
2460
- }
2461
-
2462
- return isNegNum ? this.ineg() : this
2463
- }
2464
-
2465
- /**
2466
- * Performs multiplication between the BigNumber instance and a number.
2467
- * It performs the multiplication operation in-place to a cloned BigNumber.
2468
- *
2469
- * @method muln
2470
- * @param num - The number to multiply with.
2471
- * @returns The resulting BigNumber from the multiplication operation.
2472
- *
2473
- * @example
2474
- * const bn = new BigNumber('12345');
2475
- * const result = bn.muln(23456);
2476
- */
2477
- muln (num: number): BigNumber {
2478
- return this.clone().imuln(num)
2479
- }
2480
-
2481
- /**
2482
- * Squares the BigNumber instance.
2483
- *
2484
- * @method sqr
2485
- * @returns The BigNumber squared.
2486
- *
2487
- * @example
2488
- * const bn = new BigNumber('12345');
2489
- * const result = bn.sqr();
2490
- */
2491
- sqr (): BigNumber {
2492
- return this.mul(this)
2493
- }
2494
-
2495
- /**
2496
- * Performs in-place multiplication of the BigNumber instance by itself.
2497
- *
2498
- * @method isqr
2499
- * @returns The result of multiplying the BigNumber instance by itself.
2500
- *
2501
- * @example
2502
- * let myNumber = new BigNumber(4);
2503
- * myNumber.isqr(); // Returns BigNumber of value 16
2504
- */
2505
- isqr (): BigNumber {
2506
- return this.imul(this.clone())
2507
- }
2508
-
2509
- /**
2510
- * Raises the BigNumber instance to the power of the specified BigNumber.
2511
- *
2512
- * @method pow
2513
- * @param num - The exponent to raise the BigNumber instance to.
2514
- * @returns The result of raising the BigNumber instance to the power of num.
2515
- *
2516
- * @example
2517
- * let base = new BigNumber(2);
2518
- * let exponent = new BigNumber(3);
2519
- * base.pow(exponent); // Returns BigNumber of value 8
2520
- */
2521
- pow (num: BigNumber): BigNumber {
2522
- const w = BigNumber.toBitArray(num)
2523
- if (w.length === 0) return new BigNumber(1)
2524
-
2525
- // Skip leading zeroes
2526
- /* eslint-disable @typescript-eslint/no-this-alias */
2527
- let res = this
2528
- let i = 0
2529
- for (; i < w.length; i++, res = res.sqr() as this) {
2530
- if (w[i] !== 0) break
2531
- }
2532
-
2533
- if (++i < w.length) {
2534
- for (let q = res.sqr(); i < w.length; i++, q = q.sqr()) {
2535
- if (w[i] === 0) continue
2536
- res = res.mul(q) as this
2537
- }
2538
- }
2539
-
2540
- return res
2541
- }
2542
-
2543
- /**
2544
- * Performs in-place bitwise left shift operation on the BigNumber instance.
2545
- *
2546
- * @method iushln
2547
- * @param bits - The number of positions to shift.
2548
- * @returns The BigNumber instance after performing the shift operation.
2549
- *
2550
- * @example
2551
- * let myNumber = new BigNumber(4);
2552
- * myNumber.iushln(2); // Returns BigNumber of value 16
2553
- */
2554
- iushln (bits: number): BigNumber {
2555
- this.assert(typeof bits === 'number' && bits >= 0)
2556
- const r = bits % 26
2557
- const s = (bits - r) / 26
2558
- const carryMask = (0x3ffffff >>> (26 - r)) << (26 - r)
2559
- let i: number
2560
-
2561
- if (r !== 0) {
2562
- let carry = 0
2563
-
2564
- for (i = 0; i < this.length; i++) {
2565
- const newCarry = this.words[i] & carryMask
2566
- const c = ((this.words[i] | 0) - newCarry) << r
2567
- this.words[i] = c | carry
2568
- carry = newCarry >>> (26 - r)
2569
- }
2570
-
2571
- if (carry !== 0) {
2572
- this.words[i] = carry
2573
- this.length++
2574
- }
2575
- }
2576
-
2577
- if (s !== 0) {
2578
- for (i = this.length - 1; i >= 0; i--) {
2579
- this.words[i + s] = this.words[i]
2580
- }
2581
-
2582
- for (i = 0; i < s; i++) {
2583
- this.words[i] = 0
2584
- }
2585
-
2586
- this.length += s
2587
- }
2588
-
2589
- return this.strip()
2590
- }
2591
-
2592
- /**
2593
- * Performs an in-place left shift operation on the BigNumber instance only if it is non-negative.
2594
- *
2595
- * @method ishln
2596
- * @param bits - The number of positions to shift.
2597
- * @returns The BigNumber instance after performing the shift operation.
2598
- *
2599
- * @example
2600
- * let myNumber = new BigNumber(4);
2601
- * myNumber.ishln(2); // Returns BigNumber of value 16
2602
- */
2603
- ishln (bits: number): BigNumber {
2604
- this.assert(this.negative === 0)
2605
- return this.iushln(bits)
2606
- }
2607
-
2608
- /**
2609
- * Performs an in-place unsigned bitwise right shift operation on the BigNumber instance.
2610
- *
2611
- * @method iushrn
2612
- * @param bits - The number of positions to shift.
2613
- * @param hint - Lowest bit before trailing zeroes.
2614
- * @param extended - To be filled with the bits that are shifted out.
2615
- * @returns The BigNumber instance after performing the shift operation.
2616
- *
2617
- * @example
2618
- * let myNumber = new BigNumber(16);
2619
- * myNumber.iushrn(2); // Returns BigNumber of value 4
2620
- */
2621
- iushrn (bits: number, hint?: number, extended?: BigNumber): BigNumber {
2622
- this.assert(typeof bits === 'number' && bits >= 0)
2623
- let h
2624
- if (typeof hint === 'number' && hint !== 0) {
2625
- h = (hint - (hint % 26)) / 26
2626
- } else {
2627
- h = 0
2628
- }
2629
-
2630
- const r = bits % 26
2631
- const s = Math.min((bits - r) / 26, this.length)
2632
- const mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r)
2633
- const maskedWords = extended
2634
-
2635
- h -= s
2636
- h = Math.max(0, h)
2637
-
2638
- // Extended mode, copy masked part
2639
- let i = 0
2640
- if (typeof maskedWords !== 'undefined') {
2641
- for (; i < s; i++) {
2642
- maskedWords.words[i] = this.words[i]
2643
- }
2644
- maskedWords.length = s
2645
- }
2646
-
2647
- if (s === 0) {
2648
- // No-op, we should not move anything at all
2649
- } else if (this.length > s) {
2650
- this.length -= s
2651
- for (i = 0; i < this.length; i++) {
2652
- this.words[i] = this.words[i + s]
2653
- }
2654
- } else {
2655
- this.words[0] = 0
2656
- this.length = 1
2657
- }
2658
-
2659
- let carry = 0
2660
- for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) {
2661
- const word = this.words[i] | 0
2662
- this.words[i] = (carry << (26 - r)) | (word >>> r)
2663
- carry = word & mask
2664
- }
2665
-
2666
- // Push carried bits as a mask
2667
- if ((maskedWords != null) && carry !== 0) {
2668
- maskedWords.words[maskedWords.length++] = carry
2669
- }
2670
-
2671
- if (this.length === 0) {
2672
- this.words[0] = 0
2673
- this.length = 1
2674
- }
2675
-
2676
- return this.strip()
2677
- }
2678
-
2679
- /**
2680
- * Performs an in-place right shift operation on the BigNumber instance only if it is non-negative.
2681
- *
2682
- * @method ishrn
2683
- * @param bits - The number of positions to shift.
2684
- * @param hint - Lowest bit before trailing zeroes.
2685
- * @param extended - To be filled with the bits that are shifted out.
2686
- * @returns The BigNumber instance after performing the shift operation.
2687
- *
2688
- * @example
2689
- * let myNumber = new BigNumber(16);
2690
- * myNumber.ishrn(2); // Returns BigNumber of value 4
2691
- */
2692
- ishrn (bits, hint?, extended?): BigNumber {
2693
- this.assert(this.negative === 0)
2694
- return this.iushrn(bits, hint, extended)
2695
- }
2696
-
2697
- /**
2698
- * Performs a bitwise left shift operation on a clone of the BigNumber instance.
2699
- *
2700
- * @method shln
2701
- * @param bits - The number of positions to shift.
2702
- * @returns A new BigNumber, which is the result of the shift operation.
2703
- *
2704
- * @example
2705
- * let myNumber = new BigNumber(4);
2706
- * let shiftedNumber = myNumber.shln(2);
2707
- * console.log(shiftedNumber.toString()); // Outputs "16"
2708
- */
2709
- shln (bits): BigNumber {
2710
- return this.clone().ishln(bits)
2711
- }
2712
-
2713
- /**
2714
- * Performs an unsigned bitwise shift left operation on a clone of the BigNumber instance.
2715
- *
2716
- * @method ushln
2717
- * @param bits - The number of bits to shift.
2718
- * @returns A new BigNumber resulting from the shift operation.
2719
- *
2720
- * @example
2721
- * let myNumber = new BigNumber(4);
2722
- * let shiftedNumber = myNumber.ushln(2);
2723
- * console.log(shiftedNumber.toString()); // Outputs "16"
2724
- */
2725
- ushln (bits): BigNumber {
2726
- return this.clone().iushln(bits)
2727
- }
2728
-
2729
- /**
2730
- * Performs a bitwise right shift operation on a clone of the BigNumber instance.
2731
- *
2732
- * @method shrn
2733
- * @param bits - The number of bits to shift.
2734
- * @returns A new BigNumber resulting from the shift operation.
2735
- *
2736
- * @example
2737
- * let myNumber = new BigNumber(16);
2738
- * let shiftedNumber = myNumber.shrn(3);
2739
- * console.log(shiftedNumber.toString()); // Outputs "2"
2740
- */
2741
- shrn (bits): BigNumber {
2742
- return this.clone().ishrn(bits)
2743
- }
2744
-
2745
- /**
2746
- * Performs an unsigned bitwise shift right operation on a clone of the BigNumber instance.
2747
- *
2748
- * @method ushrn
2749
- * @param bits - The number of bits to shift.
2750
- * @returns A new BigNumber resulting from the shift operation.
2751
- *
2752
- * @example
2753
- * let myNumber = new BigNumber(20);
2754
- * let shiftedNumber = myNumber.ushrn(2);
2755
- * console.log(shiftedNumber.toString()); // Outputs "5"
2756
- */
2757
- ushrn (bits): BigNumber {
2758
- return this.clone().iushrn(bits)
2759
- }
2760
-
2761
- /**
2762
- * Tests if the nth bit of the BigNumber is set.
2763
- *
2764
- * @method testn
2765
- * @param bit - The position of the bit to test.
2766
- * @returns A boolean indicating whether the nth bit is set.
2767
- *
2768
- * @example
2769
- * let myNumber = new BigNumber(10); // 1010 in binary
2770
- * myNumber.testn(1); // Returns true (indicating that the second bit from right is set)
2771
- */
2772
- testn (bit: number): boolean {
2773
- this.assert(typeof bit === 'number' && bit >= 0)
2774
- const r = bit % 26
2775
- const s = (bit - r) / 26
2776
- const q = 1 << r
2777
-
2778
- // Fast case: bit is much higher than all existing words
2779
- if (this.length <= s) return false
2780
-
2781
- // Check bit and return
2782
- const w = this.words[s]
2783
-
2784
- return Boolean(w & q)
2785
- }
2786
-
2787
- /**
2788
- * Performs an in-place operation to keep only the lower bits of the number.
2789
- * @method imaskn
2790
- * @param bits - The number of lower bits to keep.
2791
- * @returns Returns the BigNumber with only the specified lower bits.
2792
- * @throws Will throw an error if bits is not a positive number.
2793
- * @throws Will throw an error if initial BigNumber is negative as imaskn only works with positive numbers.
2794
- * @example
2795
- * const myNumber = new BigNumber(52);
2796
- * myNumber.imaskn(2); // myNumber becomes 0 because lower 2 bits of 52 (110100) are 00.
2797
- */
2798
- imaskn (bits): BigNumber {
2799
- this.assert(typeof bits === 'number' && bits >= 0)
2800
- const r = bits % 26
2801
- let s = (bits - r) / 26
2802
-
2803
- this.assert(this.negative === 0, 'imaskn works only with positive numbers')
2804
-
2805
- if (this.length <= s) {
2806
- return this
2807
- }
2808
-
2809
- if (r !== 0) {
2810
- s++
2811
- }
2812
- this.length = Math.min(s, this.length)
2813
-
2814
- if (r !== 0) {
2815
- const mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r)
2816
- this.words[this.length - 1] &= mask
2817
- }
2818
-
2819
- return this.strip()
2820
- }
2821
-
2822
- /**
2823
- * Returns a new BigNumber that keeps only the lower bits of the original number.
2824
- * @method maskn
2825
- * @param bits - The number of lower bits to keep.
2826
- * @returns Returns a new BigNumber with only the specified lower bits of the original number.
2827
- * @example
2828
- * const myNumber = new BigNumber(52);
2829
- * const newNumber = myNumber.maskn(2); // newNumber becomes 0, myNumber doesn't change.
2830
- */
2831
- maskn (bits): BigNumber {
2832
- return this.clone().imaskn(bits)
2833
- }
2834
-
2835
- /**
2836
- * Performs an in-place addition of a plain number to the BigNumber.
2837
- * @method iaddn
2838
- * @param num - The plain number to add.
2839
- * @returns Returns the BigNumber after the addition.
2840
- * @throws Will throw an error if num is not a number or is larger than 0x4000000.
2841
- * @example
2842
- * const myNumber = new BigNumber(50);
2843
- * myNumber.iaddn(2); // myNumber becomes 52.
2844
- */
2845
- iaddn (num: number): BigNumber {
2846
- this.assert(typeof num === 'number')
2847
- this.assert(num < 0x4000000, 'num is too large')
2848
- if (num < 0) return this.isubn(-num)
2849
-
2850
- // Possible sign change
2851
- if (this.negative !== 0) {
2852
- if (this.length === 1 && (this.words[0] | 0) <= num) {
2853
- this.words[0] = num - (this.words[0] | 0)
2854
- this.negative = 0
2855
- return this
2856
- }
2857
-
2858
- this.negative = 0
2859
- this.isubn(num)
2860
- this.negative = 1
2861
- return this
2862
- }
2863
-
2864
- // Add without checks
2865
- return this._iaddn(num)
2866
- }
2867
-
2868
- /**
2869
- * A helper method for in-place addition, used when there are no sign changes or size checks needed.
2870
- * @private
2871
- * @method _iaddn
2872
- * @param num - The plain number to add.
2873
- * @returns Returns the BigNumber after the addition.
2874
- */
2875
- _iaddn (num: number): BigNumber {
2876
- this.words[0] += num
2877
-
2878
- // Carry
2879
- let i = 0
2880
- for (; i < this.length && this.words[i] >= 0x4000000; i++) {
2881
- this.words[i] -= 0x4000000
2882
- if (i === this.length - 1) {
2883
- this.words[i + 1] = 1
2884
- } else {
2885
- this.words[i + 1]++
2886
- }
2887
- }
2888
- this.length = Math.max(this.length, i + 1)
2889
-
2890
- return this
2891
- }
2892
-
2893
- /**
2894
- * Performs an in-place subtraction of a plain number from the BigNumber.
2895
- * @method isubn
2896
- * @param num - The plain number to subtract.
2897
- * @returns Returns the BigNumber after the subtraction.
2898
- * @throws Will throw an error if num is not a number or is larger than 0x4000000.
2899
- * @example
2900
- * const myNumber = new BigNumber(52);
2901
- * myNumber.isubn(2); // myNumber becomes 50.
2902
- */
2903
- isubn (num: number): BigNumber {
2904
- this.assert(typeof num === 'number')
2905
- this.assert(num < 0x4000000)
2906
- if (num < 0) return this.iaddn(-num)
2907
-
2908
- if (this.negative !== 0) {
2909
- this.negative = 0
2910
- this.iaddn(num)
2911
- this.negative = 1
2912
- return this
2913
- }
2914
-
2915
- this.words[0] -= num
2916
-
2917
- if (this.length === 1 && this.words[0] < 0) {
2918
- this.words[0] = -this.words[0]
2919
- this.negative = 1
2920
- } else {
2921
- // Carry
2922
- for (let i = 0; i < this.length && this.words[i] < 0; i++) {
2923
- this.words[i] += 0x4000000
2924
- this.words[i + 1] -= 1
2925
- }
2926
- }
2927
-
2928
- return this.strip()
2929
- }
2930
-
2931
- /**
2932
- * Returns a new BigNumber that is the result of adding a plain number to the original BigNumber.
2933
- * @method addn
2934
- * @param num - The plain number to add.
2935
- * @returns Returns a new BigNumber which is the sum of the original BigNumber and the plain number.
2936
- * @example
2937
- * const myNumber = new BigNumber(50);
2938
- * const newNumber = myNumber.addn(2); // newNumber becomes 52, myNumber doesn't change.
2939
- */
2940
- addn (num: number): BigNumber {
2941
- return this.clone().iaddn(num)
2942
- }
2943
-
2944
- /**
2945
- * Returns a new BigNumber that is the result of subtracting a plain number from the original BigNumber.
2946
- * @method subn
2947
- * @param num - The plain number to subtract.
2948
- * @returns Returns a new BigNumber which is the difference of the original BigNumber and the plain number.
2949
- * @example
2950
- * const myNumber = new BigNumber(52);
2951
- * const newNumber = myNumber.subn(2); // newNumber becomes 50, myNumber doesn't change.
2952
- */
2953
- subn (num: number): BigNumber {
2954
- return this.clone().isubn(num)
2955
- }
2956
-
2957
- /**
2958
- * Performs an in-place operation to make the BigNumber an absolute value.
2959
- * @method iabs
2960
- * @returns Returns the BigNumber as an absolute value.
2961
- * @example
2962
- * const myNumber = new BigNumber(-50);
2963
- * myNumber.iabs(); // myNumber becomes 50.
2964
- */
2965
- iabs (): BigNumber {
2966
- this.negative = 0
2967
- return this
2968
- }
2969
-
2970
- /**
2971
- * Obtains the absolute value of a BigNumber instance.
2972
- * This operation does not affect the actual object but instead returns a new instance of BigNumber.
2973
- *
2974
- * @method abs
2975
- * @returns a new BigNumber instance with the absolute value of the current instance.
2976
- *
2977
- * @example
2978
- * let negativeNumber = new BigNumber(-10);
2979
- * let absolute = negativeNumber.abs();
2980
- * console.log(absolute.toString()); // Outputs: "10"
2981
- */
2982
- abs (): BigNumber {
2983
- return this.clone().iabs()
2984
- }
2985
-
2986
- /**
2987
- * Perform an in-place shift left, subtract, and multiply operation on a BigNumber instance.
2988
- * This method modifies the existing BigNumber instance.
2989
- *
2990
- * @method _ishlnsubmul
2991
- * @param num - The BigNumber to be operated on.
2992
- * @param mul - The multiplication factor.
2993
- * @param shift - The number of places to shift left.
2994
- * @returns the updated BigNumber instance after performing the in-place shift, subtract, and multiply operations.
2995
- *
2996
- * @example
2997
- * let number = new BigNumber(10);
2998
- * number._ishlnsubmul(new BigNumber(2), 3, 1);
2999
- * console.log(number.toString()); // Outputs result after performing operations
3000
- */
3001
- _ishlnsubmul (num: BigNumber, mul, shift: number): BigNumber {
3002
- const len = num.length + shift
3003
- let i: number
3004
-
3005
- this.expand(len)
3006
-
3007
- let w
3008
- let carry = 0
3009
- for (i = 0; i < num.length; i++) {
3010
- w = (this.words[i + shift] | 0) + carry
3011
- const right = (num.words[i] | 0) * mul
3012
- w -= right & 0x3ffffff
3013
- carry = (w >> 26) - ((right / 0x4000000) | 0)
3014
- this.words[i + shift] = w & 0x3ffffff
3015
- }
3016
- for (; i < this.length - shift; i++) {
3017
- w = (this.words[i + shift] | 0) + carry
3018
- carry = w >> 26
3019
- this.words[i + shift] = w & 0x3ffffff
3020
- }
3021
-
3022
- if (carry === 0) return this.strip()
3023
-
3024
- // Subtraction overflow
3025
- this.assert(carry === -1, 'carry must be -1')
3026
- carry = 0
3027
- for (i = 0; i < this.length; i++) {
3028
- w = -(this.words[i] | 0) + carry
3029
- carry = w >> 26
3030
- this.words[i] = w & 0x3ffffff
3031
- }
3032
- this.negative = 1
3033
-
3034
- return this.strip()
3035
- }
3036
-
3037
- /**
3038
- * Performs a division on a BigNumber instance word-wise.
3039
- *
3040
- * This is a private method and should not be directly accessed.
3041
- *
3042
- * @method wordDiv
3043
- * @private
3044
- * @param num - The BigNumber to divide by.
3045
- * @param mode - Specifies the operation mode as 'mod' for modulus or 'div' for division.
3046
- * @returns Object with division (div) and modulo (mod) results, subject to the 'mode' specified.
3047
- */
3048
- private wordDiv (num: BigNumber, mode): any {
3049
- let shift = this.length - num.length
3050
-
3051
- let a = this.clone()
3052
- let b = num
3053
-
3054
- // Normalize
3055
- let bhi = b.words[b.length - 1] | 0
3056
- const bhiBits = this.countWordBits(bhi)
3057
- shift = 26 - bhiBits
3058
- if (shift !== 0) {
3059
- b = b.ushln(shift)
3060
- a.iushln(shift)
3061
- bhi = b.words[b.length - 1] | 0
3062
- }
3063
-
3064
- // Initialize quotient
3065
- const m = a.length - b.length
3066
- let q: BigNumber | undefined
3067
-
3068
- if (mode !== 'mod') {
3069
- q = new BigNumber()
3070
- q.length = m + 1
3071
- q.words = new Array(q.length)
3072
- for (let i = 0; i < q.length; i++) {
3073
- q.words[i] = 0
3074
- }
3075
- }
3076
-
3077
- const diff = a.clone()._ishlnsubmul(b, 1, m)
3078
- if (diff.negative === 0) {
3079
- a = diff
3080
- if (typeof q !== 'undefined') {
3081
- q.words[m] = 1
3082
- }
3083
- }
3084
-
3085
- for (let j = m - 1; j >= 0; j--) {
3086
- let qj = (a.words[b.length + j] | 0) * 0x4000000 +
3087
- (a.words[b.length + j - 1] | 0)
3088
-
3089
- // NOTE: (qj / bhi) is (0x3ffffff * 0x4000000 + 0x3ffffff) / 0x2000000 max
3090
- // (0x7ffffff)
3091
- qj = Math.min((qj / bhi) | 0, 0x3ffffff)
3092
-
3093
- a._ishlnsubmul(b, qj, j)
3094
- while (a.negative !== 0) {
3095
- qj--
3096
- a.negative = 0
3097
- a._ishlnsubmul(b, 1, j)
3098
- if (!a.isZero()) {
3099
- a.negative ^= 1
3100
- }
3101
- }
3102
- if (typeof q !== 'undefined') {
3103
- q.words[j] = qj
3104
- }
3105
- }
3106
- if (typeof q !== 'undefined') {
3107
- q.strip()
3108
- }
3109
- a.strip()
3110
-
3111
- // Denormalize
3112
- if (mode !== 'div' && shift !== 0) {
3113
- a.iushrn(shift)
3114
- }
3115
-
3116
- return {
3117
- div: q ?? null,
3118
- mod: a
3119
- }
3120
- }
3121
-
3122
- /**
3123
- * Performs division and/or modulus operation on a BigNumber instance depending on the 'mode' parameter.
3124
- * If the mode parameter is not provided, both division and modulus results are returned.
3125
- *
3126
- * @method divmod
3127
- * @param num - The BigNumber to divide by.
3128
- * @param mode - Specifies operation as 'mod' for modulus, 'div' for division, or both if not specified.
3129
- * @param positive - Specifies if unsigned modulus is requested.
3130
- * @returns Object with properties for division (div) and modulo (mod) results.
3131
- *
3132
- * @example
3133
- * let number = new BigNumber(10);
3134
- * let result = number.divmod(new BigNumber(3));
3135
- * console.log(result.div.toString()); // Outputs: "3"
3136
- * console.log(result.mod.toString()); // Outputs: "1"
3137
- */
3138
- divmod (num: BigNumber, mode?: 'div' | 'mod', positive?: boolean): any {
3139
- this.assert(!num.isZero())
3140
-
3141
- if (this.isZero()) {
3142
- return {
3143
- div: new BigNumber(0),
3144
- mod: new BigNumber(0)
3145
- }
3146
- }
3147
-
3148
- let div, mod, res
3149
- if (this.negative !== 0 && num.negative === 0) {
3150
- res = this.neg().divmod(num, mode)
3151
-
3152
- if (mode !== 'mod') {
3153
- div = res.div.neg()
3154
- }
3155
-
3156
- if (mode !== 'div') {
3157
- mod = res.mod.neg()
3158
- if (positive && mod.negative !== 0) {
3159
- mod.iadd(num)
3160
- }
3161
- }
3162
-
3163
- return {
3164
- div,
3165
- mod
3166
- }
3167
- }
3168
-
3169
- if (this.negative === 0 && num.negative !== 0) {
3170
- res = this.divmod(num.neg(), mode)
3171
-
3172
- if (mode !== 'mod') {
3173
- div = res.div.neg()
3174
- }
3175
-
3176
- return {
3177
- div,
3178
- mod: res.mod
3179
- }
3180
- }
3181
-
3182
- if ((this.negative & num.negative) !== 0) {
3183
- res = this.neg().divmod(num.neg(), mode)
3184
-
3185
- if (mode !== 'div') {
3186
- mod = res.mod.neg()
3187
- if (positive && mod.negative !== 0) {
3188
- mod.isub(num)
3189
- }
3190
- }
3191
-
3192
- return {
3193
- div: res.div,
3194
- mod
3195
- }
3196
- }
3197
-
3198
- // Both numbers are positive at this point
3199
-
3200
- // Strip both numbers to approximate shift value
3201
- if (num.length > this.length || this.cmp(num) < 0) {
3202
- return {
3203
- div: new BigNumber(0),
3204
- mod: this
3205
- }
3206
- }
3207
-
3208
- // Very short reduction
3209
- if (num.length === 1) {
3210
- if (mode === 'div') {
3211
- return {
3212
- div: this.divn(num.words[0]),
3213
- mod: null
3214
- }
3215
- }
3216
-
3217
- if (mode === 'mod') {
3218
- return {
3219
- div: null,
3220
- mod: new BigNumber(this.modrn(num.words[0]))
3221
- }
3222
- }
3223
-
3224
- return {
3225
- div: this.divn(num.words[0]),
3226
- mod: new BigNumber(this.modrn(num.words[0]))
3227
- }
3228
- }
3229
-
3230
- return this.wordDiv(num, mode)
3231
- }
3232
-
3233
- /**
3234
- * Divides a BigNumber instance by another BigNumber and returns result. This does not modify the actual object.
3235
- *
3236
- * @method div
3237
- * @param num - The BigNumber to divide by.
3238
- * @returns A new BigNumber instance of the division result.
3239
- *
3240
- * @example
3241
- * let number = new BigNumber(10);
3242
- * let result = number.div(new BigNumber(2));
3243
- * console.log(result.toString()); // Outputs: "5"
3244
- */
3245
- div (num: BigNumber): BigNumber {
3246
- return this.divmod(num, 'div', false).div as BigNumber
3247
- }
3248
-
3249
- /**
3250
- * Returns the remainder after division of one `BigNumber` by another `BigNumber`.
3251
- *
3252
- * @method mod
3253
- * @param num - The divisor `BigNumber`.
3254
- * @returns The remainder `BigNumber` after division.
3255
- *
3256
- * @example
3257
- * const bigNum1 = new BigNumber('100');
3258
- * const bigNum2 = new BigNumber('45');
3259
- * const remainder = bigNum1.mod(bigNum2); // remainder here would be '10'
3260
- */
3261
- mod (num: BigNumber): BigNumber {
3262
- return this.divmod(num, 'mod', false).mod as BigNumber
3263
- }
3264
-
3265
- /**
3266
- * Returns the remainder after unsigned division of one `BigNumber` by another `BigNumber`.
3267
- *
3268
- * @method umod
3269
- * @param num - The divisor `BigNumber`.
3270
- * @returns The remainder `BigNumber` after unsigned division.
3271
- * Note: Here 'unsigned division' means that signs of the numbers are ignored.
3272
- *
3273
- * @example
3274
- * const bigNum1 = new BigNumber('-100');
3275
- * const bigNum2 = new BigNumber('45');
3276
- * const remainder = bigNum1.umod(bigNum2); // remainder here would be '10' as signs are ignored.
3277
- */
3278
- umod (num: BigNumber): BigNumber {
3279
- return this.divmod(num, 'mod', true).mod as BigNumber
3280
- }
3281
-
3282
- /**
3283
- * Returns the rounded quotient after division of one `BigNumber` by another `BigNumber`.
3284
- *
3285
- * @method divRound
3286
- * @param num - The divisor `BigNumber`.
3287
- * @returns The rounded quotient `BigNumber` after division.
3288
- *
3289
- * @example
3290
- * const bigNum1 = new BigNumber('100');
3291
- * const bigNum2 = new BigNumber('45');
3292
- * const quotient = bigNum1.divRound(bigNum2); // quotient here would be '2'
3293
- */
3294
- divRound (num: BigNumber): BigNumber {
3295
- const dm: { div: BigNumber, mod: BigNumber } = (
3296
- this.divmod(num) as unknown as { div: BigNumber, mod: BigNumber }
3297
- )
3298
-
3299
- // Fast case - exact division
3300
- if (dm.mod.isZero()) return dm.div
3301
-
3302
- const mod = dm.div.negative !== 0
3303
- ? dm.mod.isub(num)
3304
- : dm.mod
3305
-
3306
- const half = num.ushrn(1)
3307
- const r2: number = num.andln(1)
3308
- const cmp = mod.cmp(half)
3309
-
3310
- // Round down
3311
- if (cmp < 0 || (r2 === 1 && cmp === 0)) return dm.div
3312
-
3313
- // Round up
3314
- return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1)
3315
- }
3316
-
3317
- /**
3318
- * Returns the remainder after division of a `BigNumber` by a primitive number.
3319
- *
3320
- * @method modrn
3321
- * @param num - The divisor primitive number.
3322
- * @returns The remainder number after division.
3323
- *
3324
- * @example
3325
- * const bigNum = new BigNumber('100');
3326
- * const num = 45;
3327
- * const remainder = bigNum.modrn(num); // remainder here would be '10'
3328
- */
3329
- modrn (num: number): number {
3330
- const isNegNum = num < 0
3331
- if (isNegNum) num = -num
3332
-
3333
- this.assert(num <= 0x3ffffff)
3334
- const p = (1 << 26) % num
3335
-
3336
- let acc = 0
3337
- for (let i = this.length - 1; i >= 0; i--) {
3338
- acc = (p * acc + (this.words[i] | 0)) % num
3339
- }
3340
-
3341
- return isNegNum ? -acc : acc
3342
- }
3343
-
3344
- /**
3345
- * Performs an in-place division of a `BigNumber` by a primitive number.
3346
- *
3347
- * @method idivn
3348
- * @param num - The divisor primitive number.
3349
- * @returns The `BigNumber` itself after being divided.
3350
- * Note: 'in-place' means that this operation modifies the original `BigNumber`.
3351
- *
3352
- * @example
3353
- * const bigNum = new BigNumber('100');
3354
- * const num = 45;
3355
- * bigNum.idivn(num); // the bigNum here directly becomes '2'
3356
- */
3357
- idivn (num: number): BigNumber {
3358
- const isNegNum = num < 0
3359
- if (isNegNum) num = -num
3360
-
3361
- this.assert(num <= 0x3ffffff)
3362
-
3363
- let carry = 0
3364
- for (let i = this.length - 1; i >= 0; i--) {
3365
- const w = (this.words[i] | 0) + carry * 0x4000000
3366
- this.words[i] = (w / num) | 0
3367
- carry = w % num
3368
- }
3369
-
3370
- this.strip()
3371
- return isNegNum ? this.ineg() : this
3372
- }
3373
-
3374
- /**
3375
- * Returns the quotient `BigNumber` after division of one `BigNumber` by a primitive number.
3376
- *
3377
- * @method divn
3378
- * @param num - The divisor primitive number.
3379
- * @returns A new quotient `BigNumber` after division.
3380
- *
3381
- * @example
3382
- * const bigNum = new BigNumber('100');
3383
- * const num = 45;
3384
- * const quotient = bigNum.divn(num); // quotient here would be '2'
3385
- */
3386
- divn (num: number): BigNumber {
3387
- return this.clone().idivn(num)
3388
- }
3389
-
3390
- /**
3391
- * Computes the Extended Euclidean Algorithm for this BigNumber and provided BigNumber `p`.
3392
- * The Extended Euclidean Algorithm is a method to find the GCD (Greatest Common Divisor) and the multiplicative inverse in a modulus field.
3393
- *
3394
- * @method egcd
3395
- * @param p - The `BigNumber` with which the Extended Euclidean Algorithm will be computed.
3396
- * @returns An object `{a: BigNumber, b: BigNumber, gcd: BigNumber}` where `gcd` is the GCD of the numbers, `a` is the coefficient of `this`, and `b` is the coefficient of `p` in Bézout's identity.
3397
- *
3398
- * @example
3399
- * const bigNum1 = new BigNumber('100');
3400
- * const bigNum2 = new BigNumber('45');
3401
- * const result = bigNum1.egcd(bigNum2);
3402
- */
3403
- egcd (p: BigNumber): { a: BigNumber, b: BigNumber, gcd: BigNumber } {
3404
- this.assert(p.negative === 0, 'p must not be negative')
3405
- this.assert(!p.isZero(), 'p must not be zero')
3406
-
3407
- let x = this
3408
- const y = p.clone()
3409
-
3410
- if (x.negative !== 0) {
3411
- x = x.umod(p) as this
3412
- } else {
3413
- x = x.clone() as this
3414
- }
3415
-
3416
- // A * x + B * y = x
3417
- const A = new BigNumber(1)
3418
- const B = new BigNumber(0)
3419
-
3420
- // C * x + D * y = y
3421
- const C = new BigNumber(0)
3422
- const D = new BigNumber(1)
3423
-
3424
- let g = 0
3425
-
3426
- while (x.isEven() && y.isEven()) {
3427
- x.iushrn(1)
3428
- y.iushrn(1)
3429
- ++g
3430
- }
3431
-
3432
- const yp = y.clone()
3433
- const xp = x.clone()
3434
-
3435
- while (!x.isZero()) {
3436
- let i = 0
3437
- let im = 1
3438
- for (; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
3439
- if (i > 0) {
3440
- x.iushrn(i)
3441
- while (i-- > 0) {
3442
- if (A.isOdd() || B.isOdd()) {
3443
- A.iadd(yp)
3444
- B.isub(xp)
3445
- }
3446
-
3447
- A.iushrn(1)
3448
- B.iushrn(1)
3449
- }
3450
- }
3451
-
3452
- let j = 0
3453
- let jm = 1
3454
- for (; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
3455
- if (j > 0) {
3456
- y.iushrn(j)
3457
- while (j-- > 0) {
3458
- if (C.isOdd() || D.isOdd()) {
3459
- C.iadd(yp)
3460
- D.isub(xp)
3461
- }
3462
-
3463
- C.iushrn(1)
3464
- D.iushrn(1)
3465
- }
3466
- }
3467
-
3468
- if (x.cmp(y) >= 0) {
3469
- x.isub(y)
3470
- A.isub(C)
3471
- B.isub(D)
3472
- } else {
3473
- y.isub(x)
3474
- C.isub(A)
3475
- D.isub(B)
3476
- }
3477
- }
3478
-
3479
- return {
3480
- a: C,
3481
- b: D,
3482
- gcd: y.iushln(g)
3483
- }
3484
- }
3485
-
3486
- /**
3487
- * Compute the multiplicative inverse of the current BigNumber in the modulus field specified by `p`.
3488
- * The multiplicative inverse is a number which when multiplied with the current BigNumber gives '1' in the modulus field.
3489
- *
3490
- * @method _invmp
3491
- * @param p - The `BigNumber` specifying the modulus field.
3492
- * @returns The multiplicative inverse `BigNumber` in the modulus field specified by `p`.
3493
- *
3494
- * @example
3495
- * const bigNum = new BigNumber('45');
3496
- * const p = new BigNumber('100');
3497
- * const inverse = bigNum._invmp(p); // inverse here would be a BigNumber such that (inverse*bigNum) % p = '1'
3498
- */
3499
- _invmp (p: BigNumber): BigNumber {
3500
- this.assert(p.negative === 0, 'p must not be negative')
3501
- this.assert(!p.isZero(), 'p must not be zero')
3502
-
3503
- let a = this
3504
- const b = p.clone()
3505
-
3506
- if (a.negative !== 0) {
3507
- a = a.umod(p) as this
3508
- } else {
3509
- a = a.clone() as this
3510
- }
3511
-
3512
- const x1 = new BigNumber(1)
3513
- const x2 = new BigNumber(0)
3514
-
3515
- const delta = b.clone()
3516
-
3517
- while (a.cmpn(1) > 0 && b.cmpn(1) > 0) {
3518
- let i = 0
3519
- let im = 1
3520
- for (; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
3521
- if (i > 0) {
3522
- a.iushrn(i)
3523
- while (i-- > 0) {
3524
- if (x1.isOdd()) {
3525
- x1.iadd(delta)
3526
- }
3527
-
3528
- x1.iushrn(1)
3529
- }
3530
- }
3531
-
3532
- let j = 0
3533
- let jm = 1
3534
- for (; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
3535
- if (j > 0) {
3536
- b.iushrn(j)
3537
- while (j-- > 0) {
3538
- if (x2.isOdd()) {
3539
- x2.iadd(delta)
3540
- }
3541
-
3542
- x2.iushrn(1)
3543
- }
3544
- }
3545
-
3546
- if (a.cmp(b) >= 0) {
3547
- a.isub(b)
3548
- x1.isub(x2)
3549
- } else {
3550
- b.isub(a)
3551
- x2.isub(x1)
3552
- }
3553
- }
3554
-
3555
- let res
3556
- if (a.cmpn(1) === 0) {
3557
- res = x1
3558
- } else {
3559
- res = x2
3560
- }
3561
-
3562
- if (res.cmpn(0) < 0) {
3563
- res.iadd(p)
3564
- }
3565
-
3566
- return res
3567
- }
3568
-
3569
- /**
3570
- * Computes and returns the greatest common divisor (GCD) of this BigNumber and the provided BigNumber.
3571
- *
3572
- * @method gcd
3573
- * @param num - The BigNumber with which to compute the GCD.
3574
- * @returns The GCD of this BigNumber and the provided BigNumber.
3575
- *
3576
- * @example
3577
- * let a = new BigNumber(48);
3578
- * let b = new BigNumber(18);
3579
- * let gcd = a.gcd(b);
3580
- */
3581
- gcd (num: BigNumber): BigNumber {
3582
- if (this.isZero()) return num.abs()
3583
- if (num.isZero()) return this.abs()
3584
-
3585
- let a = this.clone()
3586
- let b = num.clone()
3587
- a.negative = 0
3588
- b.negative = 0
3589
-
3590
- // Remove common factor of two
3591
- let shift = 0
3592
- for (; a.isEven() && b.isEven(); shift++) {
3593
- a.iushrn(1)
3594
- b.iushrn(1)
3595
- }
3596
-
3597
- do {
3598
- while (a.isEven()) {
3599
- a.iushrn(1)
3600
- }
3601
- while (b.isEven()) {
3602
- b.iushrn(1)
3603
- }
3604
-
3605
- const r = a.cmp(b)
3606
- if (r < 0) {
3607
- // Swap `a` and `b` to make `a` always bigger than `b`
3608
- const t = a
3609
- a = b
3610
- b = t
3611
- } else if (r === 0 || b.cmpn(1) === 0) {
3612
- break
3613
- }
3614
-
3615
- a.isub(b)
3616
- } while (true)
3617
-
3618
- return b.iushln(shift)
3619
- }
3620
-
3621
- /**
3622
- * Computes and returns the modular multiplicative inverse of this BigNumber in the field defined by the provided BigNumber.
3623
- *
3624
- * @method invm
3625
- * @param num - The BigNumber that defines the field.
3626
- * @returns The modular multiplicative inverse of this BigNumber.
3627
- *
3628
- * @example
3629
- * let a = new BigNumber(3);
3630
- * let field = new BigNumber(7);
3631
- * let inverse = a.invm(field);
3632
- */
3633
- invm (num: BigNumber): BigNumber {
3634
- return this.egcd(num).a.umod(num)
3635
- }
3636
-
3637
- /**
3638
- * Checks if this BigNumber is even.
3639
- * An even number is an integer which is evenly divisible by two.
3640
- *
3641
- * @method isEven
3642
- * @returns true if this BigNumber is even, else false.
3643
- *
3644
- * @example
3645
- * let a = new BigNumber(4);
3646
- * let isEven = a.isEven(); // true
3647
- */
3648
- isEven (): boolean {
3649
- return (this.words[0] & 1) === 0
3650
- }
3651
-
3652
- /**
3653
- * Checks if this BigNumber is Odd.
3654
- * An odd number is an integer which is not evenly divisible by two.
3655
- *
3656
- * @method isOdd
3657
- * @returns true if this BigNumber is Odd, else false.
3658
- *
3659
- * @example
3660
- * let a = new BigNumber(3);
3661
- * let isOdd = a.isOdd(); // true
3662
- */
3663
- isOdd (): boolean {
3664
- return (this.words[0] & 1) === 1
3665
- }
3666
-
3667
- /**
3668
- * Returns the result of bitwise AND operation between the least significant 26 bits of
3669
- * this BigNumber and the provided number.
3670
- * This method is mostly used to mask-off less significant bits.
3671
- *
3672
- * @method andln
3673
- * @param num - The number to AND with.
3674
- * @returns The result of the AND operation.
3675
- *
3676
- * @example
3677
- * let a = new BigNumber(60);
3678
- * let result = a.andln(13); // 12
3679
- */
3680
- andln (num: number): number {
3681
- return this.words[0] & num
3682
- }
3683
-
3684
- /**
3685
- * Increments the value at the bit position specified by the input parameter.
3686
- *
3687
- * @method bincn
3688
- * @param bit - The bit position to increment at.
3689
- * @returns This BigNumber after incrementing at the specific bit position.
3690
- *
3691
- * @example
3692
- * let a = new BigNumber(5);
3693
- * a.bincn(2); // a = 7
3694
- */
3695
- bincn (bit: number): BigNumber {
3696
- this.assert(typeof bit === 'number')
3697
- const r = bit % 26
3698
- const s = (bit - r) / 26
3699
- const q = 1 << r
3700
-
3701
- // Fast case: bit is much higher than all existing words
3702
- if (this.length <= s) {
3703
- this.expand(s + 1)
3704
- this.words[s] |= q
3705
- return this
3706
- }
3707
-
3708
- // Add bit and propagate, if needed
3709
- let carry = q
3710
- let i = s
3711
- for (; carry !== 0 && i < this.length; i++) {
3712
- let w = this.words[i] | 0
3713
- w += carry
3714
- carry = w >>> 26
3715
- w &= 0x3ffffff
3716
- this.words[i] = w
3717
- }
3718
- if (carry !== 0) {
3719
- this.words[i] = carry
3720
- this.length++
3721
- }
3722
- return this
3723
- }
3724
-
3725
- /**
3726
- * Checks if this BigNumber is Zero.
3727
- * A BigNumber is zero if it only contains one word and that word is 0.
3728
- *
3729
- * @method isZero
3730
- * @returns true if this BigNumber is Zero, else false.
3731
- *
3732
- * @example
3733
- * let a = new BigNumber(0);
3734
- * let isZero = a.isZero(); // true
3735
- */
3736
- isZero (): boolean {
3737
- return this.length === 1 && this.words[0] === 0
3738
- }
3739
-
3740
- /**
3741
- * Compares this BigNumber with the given number.
3742
- * It returns -1 if this BigNumber is less than the number, 0 if they're equal, and 1 if the BigNumber is greater than the number.
3743
- *
3744
- * @method cmpn
3745
- * @param num - The number to compare with.
3746
- * @returns -1, 0, or 1 based on the comparison result.
3747
- *
3748
- * @example
3749
- * let a = new BigNumber(15);
3750
- * let result = a.cmpn(10); // 1
3751
- */
3752
- cmpn (num: number): 1 | 0 | -1 {
3753
- const negative = num < 0
3754
-
3755
- if (this.negative !== 0 && !negative) return -1
3756
- if (this.negative === 0 && negative) return 1
3757
-
3758
- this.strip()
3759
-
3760
- let res: 1 | 0 | -1
3761
- if (this.length > 1) {
3762
- res = 1
3763
- } else {
3764
- if (negative) {
3765
- num = -num
3766
- }
3767
-
3768
- this.assert(num <= 0x3ffffff, 'Number is too big')
3769
-
3770
- const w = this.words[0] | 0
3771
- res = w === num ? 0 : w < num ? -1 : 1
3772
- }
3773
- if (this.negative !== 0) return (-res | 0) as 1 | 0 | -1
3774
- return res
3775
- }
3776
-
3777
- /**
3778
- * Compare this big number with another big number.
3779
- * @method cmp
3780
- * @param num - The big number to compare with.
3781
- * @returns Returns:
3782
- * 1 if this big number is greater,
3783
- * -1 if it's less,
3784
- * 0 if they are equal.
3785
- *
3786
- * @example
3787
- * import BigNumber from './BigNumber';
3788
- * const bn1 = new BigNumber('10');
3789
- * const bn2 = new BigNumber('6');
3790
- * const comparisonResult = bn1.cmp(bn2); // 1 - because 10 is greater than 6
3791
- */
3792
- cmp (num: BigNumber): 1 | 0 | -1 {
3793
- if (this.negative !== 0 && num.negative === 0) return -1
3794
- if (this.negative === 0 && num.negative !== 0) return 1
3795
-
3796
- const res = this.ucmp(num)
3797
- if (this.negative !== 0) return (-res | 0) as 1 | 0 | -1
3798
- return res
3799
- }
3800
-
3801
- /**
3802
- * Performs an unsigned comparison between this BigNumber instance and another.
3803
- *
3804
- * @method ucmp
3805
- * @param num - The BigNumber instance to compare with.
3806
- * @returns Returns 1 if this BigNumber is bigger, -1 if it is smaller, and 0 if they are equal.
3807
- *
3808
- * @example
3809
- * let bigNumber1 = new BigNumber('1234');
3810
- * let bigNumber2 = new BigNumber('2345');
3811
- * let comparisonResult = bigNumber1.ucmp(bigNumber2); // Returns -1
3812
- */
3813
- ucmp (num: BigNumber): 1 | 0 | -1 {
3814
- // At this point both numbers have the same sign
3815
- if (this.length > num.length) return 1
3816
- if (this.length < num.length) return -1
3817
-
3818
- let res: 1 | 0 | -1 = 0
3819
- for (let i = this.length - 1; i >= 0; i--) {
3820
- const a = this.words[i] | 0
3821
- const b = num.words[i] | 0
3822
-
3823
- if (a === b) continue
3824
- if (a < b) {
3825
- res = -1
3826
- } else if (a > b) {
3827
- res = 1
3828
- }
3829
- break
3830
- }
3831
- return res
3832
- }
3833
-
3834
- /**
3835
- * Checks if this BigNumber instance is greater than a number.
3836
- *
3837
- * @method gtn
3838
- * @param num - The number to compare with.
3839
- * @returns Returns true if this BigNumber is greater than the number, false otherwise.
3840
- *
3841
- * @example
3842
- * let bigNumber = new BigNumber('2345');
3843
- * let isGreater = bigNumber.gtn(1234); // Returns true
3844
- */
3845
- gtn (num: number): boolean {
3846
- return this.cmpn(num) === 1
3847
- }
3848
-
3849
- /**
3850
- * Checks if this BigNumber instance is greater than another BigNumber.
3851
- *
3852
- * @method gt
3853
- * @param num - The BigNumber to compare with.
3854
- * @returns Returns true if this BigNumber is greater than the other BigNumber, false otherwise.
3855
- *
3856
- * @example
3857
- * let bigNumber1 = new BigNumber('2345');
3858
- * let bigNumber2 = new BigNumber('1234');
3859
- * let isGreater = bigNumber1.gt(bigNumber2); // Returns true
3860
- */
3861
- gt (num: BigNumber): boolean {
3862
- return this.cmp(num) === 1
3863
- }
3864
-
3865
- /**
3866
- * Checks if this BigNumber instance is greater than or equal to a number.
3867
- *
3868
- * @method gten
3869
- * @param num - The number to compare with.
3870
- * @returns Returns true if this BigNumber is greater than or equal to the number, false otherwise.
3871
- *
3872
- * @example
3873
- * let bigNumber = new BigNumber('1234');
3874
- * let isGreaterOrEqual = bigNumber.gten(1234); // Returns true
3875
- */
3876
- gten (num: number): boolean {
3877
- return this.cmpn(num) >= 0
3878
- }
3879
-
3880
- /**
3881
- * Checks if this BigNumber instance is greater than or equal to another BigNumber.
3882
- *
3883
- * @method gte
3884
- * @param num - The BigNumber to compare with.
3885
- * @returns Returns true if this BigNumber is greater than or equal to the other BigNumber, false otherwise.
3886
- *
3887
- * @example
3888
- * let bigNumber1 = new BigNumber('1234');
3889
- * let bigNumber2 = new BigNumber('1234');
3890
- * let isGreaterOrEqual = bigNumber1.gte(bigNumber2); // Returns true
3891
- */
3892
- gte (num: BigNumber): boolean {
3893
- return this.cmp(num) >= 0
3894
- }
3895
-
3896
- /**
3897
- * Checks if this BigNumber instance is less than a number.
3898
- *
3899
- * @method ltn
3900
- * @param num - The number to compare with.
3901
- * @returns Returns true if this BigNumber is less than the number, false otherwise.
3902
- *
3903
- * @example
3904
- * let bigNumber = new BigNumber('1234');
3905
- * let isLess = bigNumber.ltn(2345); // Returns true
3906
- */
3907
- ltn (num: number): boolean {
3908
- return this.cmpn(num) === -1
3909
- }
3910
-
3911
- /**
3912
- * Checks if this BigNumber instance is less than another BigNumber.
3913
- *
3914
- * @method lt
3915
- * @param num - The BigNumber to compare with.
3916
- * @returns Returns true if this BigNumber is less than the other BigNumber, false otherwise.
3917
- *
3918
- * @example
3919
- * let bigNumber1 = new BigNumber('1234');
3920
- * let bigNumber2 = new BigNumber('2345');
3921
- * let isLess = bigNumber1.lt(bigNumber2); // Returns true
3922
- */
3923
- lt (num: BigNumber): boolean {
3924
- return this.cmp(num) === -1
3925
- }
3926
-
3927
- /**
3928
- * Checks if this BigNumber instance is less than or equal to a number.
3929
- *
3930
- * @method lten
3931
- * @param num - The number to compare with.
3932
- * @returns Returns true if this BigNumber is less than or equal to the number, false otherwise.
3933
- *
3934
- * @example
3935
- * let bigNumber = new BigNumber('2345');
3936
- * let isLessOrEqual = bigNumber.lten(2345); // Returns true
3937
- */
3938
- lten (num: number): boolean {
3939
- return this.cmpn(num) <= 0
3940
- }
3941
-
3942
- /**
3943
- * Checks if this BigNumber instance is less than or equal to another BigNumber.
3944
- *
3945
- * @method lte
3946
- * @param num - The BigNumber to compare with.
3947
- * @returns Returns true if this BigNumber is less than or equal to the other BigNumber, false otherwise.
3948
- *
3949
- * @example
3950
- * let bigNumber1 = new BigNumber('2345');
3951
- * let bigNumber2 = new BigNumber('2345');
3952
- * let isLessOrEqual = bigNumber1.lte(bigNumber2); // Returns true
3953
- */
3954
- lte (num: BigNumber): boolean {
3955
- return this.cmp(num) <= 0
3956
- }
3957
-
3958
- /**
3959
- * Checks if this BigNumber instance is equal to a number.
3960
- *
3961
- * @method eqn
3962
- * @param num - The number to compare with.
3963
- * @returns Returns true if this BigNumber is equal to the number, false otherwise.
3964
- *
3965
- * @example
3966
- * let bigNumber = new BigNumber('1234');
3967
- * let isEqual = bigNumber.eqn(1234); // Returns true
3968
- */
3969
- eqn (num: number): boolean {
3970
- return this.cmpn(num) === 0
3971
- }
3972
-
3973
- /**
3974
- * Compares the current BigNumber with the given number and returns whether they're equal.
3975
- *
3976
- * @method eq
3977
- * @param num - The number to compare equality with.
3978
- * @returns Returns true if the current BigNumber is equal to the provided number, otherwise false.
3979
- *
3980
- * @example
3981
- * let bigNum = new BigNumber(10);
3982
- * bigNum.eq(new BigNumber(10)); // true
3983
- */
3984
- eq (num: BigNumber): boolean {
3985
- return this.cmp(num) === 0
3986
- }
3987
-
3988
- /**
3989
- * Converts a BigNumber to a reduction context ensuring the number is a positive integer and is not already in a reduction context.
3990
- * Throws an error in case the number is either negative or already in a reduction context.
3991
- *
3992
- * @method toRed
3993
- * @param ctx - The ReductionContext to convert the BigNumber to.
3994
- * @returns Returns the BigNumber in the given ReductionContext.
3995
- *
3996
- * @example
3997
- * let bigNum = new BigNumber(10);
3998
- * let redCtx = new ReductionContext();
3999
- * bigNum.toRed(redCtx);
4000
- */
4001
- toRed (ctx: ReductionContext): BigNumber {
4002
- this.assert(this.red == null, 'Already a number in reduction context')
4003
- this.assert(this.negative === 0, 'red works only with positives')
4004
- return ctx.convertTo(this).forceRed(ctx)
4005
- }
4006
-
4007
- /**
4008
- * Converts a BigNumber from a reduction context, making sure the number is indeed in a reduction context.
4009
- * Throws an error in case the number is not in a reduction context.
4010
- *
4011
- * @method fromRed
4012
- * @returns Returns the BigNumber out of the ReductionContext.
4013
- *
4014
- * @example
4015
- * let bigNum = new BigNumber(10);
4016
- * let redCtx = new ReductionContext();
4017
- * bigNum.toRed(redCtx);
4018
- * bigNum.fromRed();
4019
- */
4020
- fromRed (): BigNumber {
4021
- this.assert(
4022
- this.red,
4023
- 'fromRed works only with numbers in reduction context'
4024
- )
4025
- return (this.red).convertFrom(this)
4026
- }
4027
-
4028
- /**
4029
- * Forces the current BigNumber into a reduction context, irrespective of the BigNumber's current state.
4030
- *
4031
- * @method forceRed
4032
- * @param ctx - The ReductionContext to forcefully convert the BigNumber to.
4033
- * @returns Returns the BigNumber in the given ReductionContext.
4034
- *
4035
- * @example
4036
- * let bigNum = new BigNumber(10);
4037
- * let redCtx = new ReductionContext();
4038
- * bigNum.forceRed(redCtx);
4039
- */
4040
- forceRed (ctx: ReductionContext): BigNumber {
4041
- // this.assert(this.red == null, 'Already a number in reduction context')
4042
- this.red = ctx
4043
- return this
4044
- }
4045
-
4046
- /**
4047
- * Performs addition operation of the current BigNumber with the given number in a reduction context.
4048
- * Throws an error in case the number is not in a reduction context.
4049
- *
4050
- * @method redAdd
4051
- * @param num - The number to add to the current BigNumber.
4052
- * @returns Returns a new BigNumber that's the sum of the current BigNumber and the provided number in the reduction context.
4053
- *
4054
- * @example
4055
- * let bigNum = new BigNumber(10);
4056
- * let redCtx = new ReductionContext();
4057
- * bigNum.toRed(redCtx);
4058
- * bigNum.redAdd(new BigNumber(20)); // returns a BigNumber of 30 in reduction context
4059
- */
4060
- redAdd (num: BigNumber): BigNumber {
4061
- this.assert(this.red, 'redAdd works only with red numbers')
4062
- return (this.red).add(this, num)
4063
- }
4064
-
4065
- /**
4066
- * Performs in-place addition operation of the current BigNumber with the given number in a reduction context.
4067
- * Throws an error in case the number is not in a reduction context.
4068
- *
4069
- * @method redIAdd
4070
- * @param num - The number to add to the current BigNumber.
4071
- * @returns Returns the modified current BigNumber after adding the provided number in the reduction context.
4072
- *
4073
- * @example
4074
- * let bigNum = new BigNumber(10);
4075
- * let redCtx = new ReductionContext();
4076
- * bigNum.toRed(redCtx);
4077
- * bigNum.redIAdd(new BigNumber(20)); // modifies the bigNum to 30 in reduction context
4078
- */
4079
- redIAdd (num: BigNumber): BigNumber {
4080
- this.assert(this.red, 'redIAdd works only with red numbers')
4081
- return (this.red).iadd(this, num)
4082
- }
4083
-
4084
- /**
4085
- * Performs subtraction operation of the current BigNumber with the given number in a reduction context.
4086
- * Throws an error in case the number is not in a reduction context.
4087
- *
4088
- * @method redSub
4089
- * @param num - The number to subtract from the current BigNumber.
4090
- * @returns Returns a new BigNumber that's the subtraction result of the current BigNumber and the provided number in the reduction context.
4091
- *
4092
- * @example
4093
- * let bigNum = new BigNumber(30);
4094
- * let redCtx = new ReductionContext();
4095
- * bigNum.toRed(redCtx);
4096
- * bigNum.redSub(new BigNumber(20)); // returns a BigNumber of 10 in reduction context
4097
- */
4098
- redSub (num: BigNumber): BigNumber {
4099
- this.assert(this.red, 'redSub works only with red numbers')
4100
- return (this.red).sub(this, num)
4101
- }
4102
-
4103
- /**
4104
- * Performs in-place subtraction operation of the current BigNumber with the given number in a reduction context.
4105
- * Throws an error in case the number is not in a reduction context.
4106
- *
4107
- * @method redISub
4108
- * @param num - The number to subtract from the current BigNumber.
4109
- * @returns Returns the modified current BigNumber after subtracting the provided number in the reduction context.
4110
- *
4111
- * @example
4112
- * let bigNum = new BigNumber(30);
4113
- * let redCtx = new ReductionContext();
4114
- * bigNum.toRed(redCtx);
4115
- * bigNum.redISub(new BigNumber(20)); // modifies the bigNum to 10 in reduction context
4116
- */
4117
- redISub (num: BigNumber): BigNumber {
4118
- this.assert(this.red, 'redISub works only with red numbers')
4119
- return (this.red).isub(this, num)
4120
- }
4121
-
4122
- /**
4123
- * Performs the shift left operation on the current BigNumber in the reduction context.
4124
- * Throws an error in case the number is not in a reduction context.
4125
- *
4126
- * @method redShl
4127
- * @param num - The positions to shift left the current BigNumber.
4128
- * @returns Returns a new BigNumber after performing the shift left operation on the current BigNumber in the reduction context.
4129
- *
4130
- * @example
4131
- * let bigNum = new BigNumber(1);
4132
- * let redCtx = new ReductionContext();
4133
- * bigNum.toRed(redCtx);
4134
- * bigNum.redShl(2); // returns a BigNumber of 4 in reduction context
4135
- */
4136
- redShl (num: number): BigNumber {
4137
- this.assert(this.red, 'redShl works only with red numbers')
4138
- return (this.red).shl(this, num)
4139
- }
4140
-
4141
- /**
4142
- * Performs multiplication operation of the current BigNumber with the given number in a reduction context.
4143
- * Throws an error in case the number is not in a reduction context.
4144
- *
4145
- * @method redMul
4146
- * @param num - The number to multiply with the current BigNumber.
4147
- * @returns Returns a new BigNumber that's the product of the current BigNumber and the provided number in the reduction context.
4148
- *
4149
- * @example
4150
- * let bigNum = new BigNumber(10);
4151
- * let redCtx = new ReductionContext();
4152
- * bigNum.toRed(redCtx);
4153
- * bigNum.redMul(new BigNumber(20)); // returns a BigNumber of 200 in reduction context
4154
- */
4155
- redMul (num: BigNumber): BigNumber {
4156
- this.assert(this.red, 'redMul works only with red numbers')
4157
- ; (this.red).verify2(this, num)
4158
- return (this.red).mul(this, num)
4159
- }
4160
-
4161
- /**
4162
- * Performs an in-place multiplication of this BigNumber instance with another BigNumber within a reduction context.
4163
- * Expects that this BigNumber is within the reduction context i.e., it has been reduced.
4164
- *
4165
- * @method redIMul
4166
- * @param num - The BigNumber to multiply with the current BigNumber.
4167
- * @returns A BigNumber that is the result of the in-place multiplication operation, within the reduction context.
4168
- *
4169
- * @example
4170
- * let bigNum1 = new BigNumber('10').toRed(someRed);
4171
- * let bigNum2 = new BigNumber('5');
4172
- * bigNum1.redIMul(bigNum2);
4173
- */
4174
- redIMul (num: BigNumber): BigNumber {
4175
- this.assert(this.red, 'redMul works only with red numbers')
4176
- ; (this.red).verify2(this, num)
4177
- return (this.red).imul(this, num)
4178
- }
4179
-
4180
- /**
4181
- * Square of a "red" (reduced) BigNumber.
4182
- * This function squares the calling BigNumber and returns the result.
4183
- * It only works if the number is "reduced". A number is considered reduced
4184
- * if it has a `red` field that points to a reduction context object.
4185
- *
4186
- * @method redSqr
4187
- * @throws If the BigNumber is not reduced
4188
- * @returns The square of the BigNumber
4189
- *
4190
- * @example
4191
- * const num = new BigNumber('25').toRed(someRed);
4192
- * const result = num.redSqr();
4193
- * console.log(result.toString()); // Outputs: '625' mod the red value
4194
- */
4195
- redSqr (): BigNumber {
4196
- this.assert(this.red, 'redSqr works only with red numbers')
4197
- ; (this.red).verify1(this)
4198
- return (this.red).sqr(this)
4199
- }
4200
-
4201
- /**
4202
- * In-place square of a "red" (reduced) BigNumber.
4203
- * This function squares the calling BigNumber and overwrites it with the result.
4204
- * It only works if the number is "reduced". A number is considered reduced
4205
- * if it has a `red` field that points to a reduction context object.
4206
- *
4207
- * @method redISqr
4208
- * @throws If the BigNumber is not reduced
4209
- * @returns This BigNumber squared in place
4210
- *
4211
- * @example
4212
- * const num = new BigNumber('25').toRed(someRed);
4213
- * num.redISqr();
4214
- * console.log(num.toString()); // Outputs: '625' mod the red value
4215
- */
4216
- redISqr (): BigNumber {
4217
- this.assert(this.red, 'redISqr works only with red numbers')
4218
- ; (this.red).verify1(this)
4219
- return (this.red).isqr(this)
4220
- }
4221
-
4222
- /**
4223
- * Square root of a "red" (reduced) BigNumber.
4224
- * This function calculates the square root of the calling BigNumber
4225
- * and returns the result. It only works if the number is "reduced".
4226
- * A number is considered reduced if it has a `red`
4227
- * field that points to a reduction context object.
4228
- *
4229
- * @method redSqrt
4230
- * @throws If the BigNumber is not reduced
4231
- * @returns The square root of the BigNumber
4232
- *
4233
- * @example
4234
- * const num = new BigNumber('4').toRed(someRed);
4235
- * const result = num.redSqrt();
4236
- * console.log(result.toString()); // Outputs: '2' mod the red value
4237
- */
4238
- redSqrt (): BigNumber {
4239
- this.assert(this.red, 'redSqrt works only with red numbers')
4240
- ; (this.red).verify1(this)
4241
- return (this.red).sqrt(this)
4242
- }
4243
-
4244
- /**
4245
- * Find multiplicative inverse (reciprocal) in respect to reduction context.
4246
- * The method works only on numbers that have a reduction context set.
4247
- *
4248
- * @method redInvm
4249
- * @returns Returns a BigNumber that is multiplicative inverse in respect to the reduction context.
4250
- * @throws Will throw an error if this number does not have a reduction context.
4251
- *
4252
- * @example
4253
- * let a = new BigNumber('2345', 16);
4254
- * a.red = someReductionContext;
4255
- * let aInverse = a.redInvm();
4256
- */
4257
- redInvm (): BigNumber {
4258
- this.assert(this.red, 'redInvm works only with red numbers')
4259
- ; (this.red).verify1(this)
4260
- return (this.red).invm(this)
4261
- }
4262
-
4263
- /**
4264
- * Find negative version of this number in respect to reduction context.
4265
- * The method works only on numbers that have a reduction context set.
4266
- *
4267
- * @method redNeg
4268
- * @returns Returns a BigNumber that is the negative version of this number in respect to the reduction context.
4269
- * @throws Will throw an error if this number does not have a reduction context.
4270
- *
4271
- * @example
4272
- * let a = new BigNumber('2345', 16);
4273
- * a.red = someReductionContext;
4274
- * let aNeg = a.redNeg();
4275
- */
4276
- redNeg (): BigNumber {
4277
- this.assert(this.red, 'redNeg works only with red numbers')
4278
- ; (this.red).verify1(this)
4279
- return (this.red).neg(this)
4280
- }
4281
-
4282
- /**
4283
- * Raises this number to the power of 'num', in respect to reduction context.
4284
- * Note that 'num' must not have a reduction context set.
4285
- *
4286
- * @method redPow
4287
- * @param num - The exponent to raise this number to.
4288
- * @returns Returns a BigNumber that is this number raised to the power of 'num', in respect to the reduction context.
4289
- * @throws Will throw an error if this number does not have a reduction context or 'num' has a reduction context.
4290
- *
4291
- * @example
4292
- * let a = new BigNumber(3);
4293
- * a.red = someReductionContext;
4294
- * let b = new BigNumber(3);
4295
- * let result = a.redPow(b); // equivalent to (a^b) mod red
4296
- */
4297
- redPow (num: BigNumber): BigNumber {
4298
- this.assert((this.red != null) && (num.red == null), 'redPow(normalNum)')
4299
- ; (this.red).verify1(this)
4300
- return (this.red).pow(this, num)
4301
- }
4302
-
4303
- /**
4304
- * Creates a BigNumber from a hexadecimal string.
4305
- *
4306
- * @static
4307
- * @method fromHex
4308
- * @param hex - The hexadecimal string to create a BigNumber from.
4309
- * @returns Returns a BigNumber created from the hexadecimal input string.
4310
- *
4311
- * @example
4312
- * const exampleHex = 'a1b2c3';
4313
- * const bigNumber = BigNumber.fromHex(exampleHex);
4314
- */
4315
- static fromHex (hex: string, endian?: 'little' | 'big'): BigNumber {
4316
- if (endian === 'big') {
4317
- return new BigNumber(hex, 16)
4318
- } else {
4319
- return new BigNumber(hex, 16, 'le')
4320
- }
4321
- }
4322
-
4323
- /**
4324
- * Converts this BigNumber to a hexadecimal string.
4325
- *
4326
- * @method toHex
4327
- * @param length - The minimum length of the hex string
4328
- * @returns Returns a string representing the hexadecimal value of this BigNumber.
4329
- *
4330
- * @example
4331
- * const bigNumber = new BigNumber(255);
4332
- * const hex = bigNumber.toHex();
4333
- */
4334
- toHex (length: number = 0): string {
4335
- return this.toString('hex', length * 2)
4336
- }
4337
-
4338
- /**
4339
- * Creates a BigNumber from a JSON-serialized string.
4340
- *
4341
- * @static
4342
- * @method fromJSON
4343
- * @param str - The JSON-serialized string to create a BigNumber from.
4344
- * @returns Returns a BigNumber created from the JSON input string.
4345
- *
4346
- * @example
4347
- * const serialized = '{"type":"BigNumber","hex":"a1b2c3"}';
4348
- * const bigNumber = BigNumber.fromJSON(serialized);
4349
- */
4350
- static fromJSON (str: string): BigNumber {
4351
- return new BigNumber(str)
4352
- }
4353
-
4354
- /**
4355
- * Creates a BigNumber from a number.
4356
- *
4357
- * @static
4358
- * @method fromNumber
4359
- * @param n - The number to create a BigNumber from.
4360
- * @returns Returns a BigNumber equivalent to the input number.
4361
- *
4362
- * @example
4363
- * const number = 1234;
4364
- * const bigNumber = BigNumber.fromNumber(number);
4365
- */
4366
- static fromNumber (n: number): BigNumber {
4367
- return new BigNumber(n)
4368
- }
4369
-
4370
- /**
4371
- * Creates a BigNumber from a string, considering an optional base.
4372
- *
4373
- * @static
4374
- * @method fromString
4375
- * @param str - The string to create a BigNumber from.
4376
- * @param base - The base used for conversion. If not provided, base 10 is assumed.
4377
- * @returns Returns a BigNumber equivalent to the string after conversion from the specified base.
4378
- *
4379
- * @example
4380
- * const str = '1234';
4381
- * const bigNumber = BigNumber.fromString(str, 16);
4382
- */
4383
- static fromString (str: string, base?: number | 'hex'): BigNumber {
4384
- return new BigNumber(str, base)
4385
- }
4386
-
4387
- /**
4388
- * Creates a BigNumber from a signed magnitude number.
4389
- *
4390
- * @static
4391
- * @method fromSm
4392
- * @param num - The signed magnitude number to convert to a BigNumber.
4393
- * @param endian - Defines endianess. If not provided, big endian is assumed.
4394
- * @returns Returns a BigNumber equivalent to the signed magnitude number interpreted with specified endianess.
4395
- *
4396
- * @example
4397
- * const num = [0x81]
4398
- * const bigNumber = BigNumber.fromSm(num, { endian: 'little' }); // equivalent to BigNumber from '-1'
4399
- */
4400
- static fromSm (num: number[], endian: 'big' | 'little' = 'big'): BigNumber {
4401
- if (num.length === 0) {
4402
- return new BigNumber(0)
4403
- }
4404
-
4405
- if (endian === 'little') {
4406
- num = num.reverse()
4407
- }
4408
-
4409
- if ((num[0] & 0x80) !== 0) {
4410
- num[0] = num[0] & 0x7f
4411
- return new BigNumber(num).neg()
4412
- } else {
4413
- return new BigNumber(num)
4414
- }
4415
- }
4416
-
4417
- /**
4418
- * Converts this BigNumber to a signed magnitude number.
4419
- *
4420
- * @method toSm
4421
- * @param endian - Defines endianess. If not provided, big endian is assumed.
4422
- * @returns Returns an array equivalent to this BigNumber interpreted as a signed magnitude with specified endianess.
4423
- *
4424
- * @example
4425
- * const bigNumber = new BigNumber(-1);
4426
- * const num = bigNumber.toSm('little'); // [0x81]
4427
- */
4428
- toSm (endian: 'big' | 'little' = 'big'): number[] {
4429
- let num: number[]
4430
- if (this.cmpn(0) === -1) {
4431
- num = this.neg().toArray()
4432
- if ((num[0] & 0x80) !== 0) {
4433
- num = [0x80, ...num]
4434
- } else {
4435
- num[0] = num[0] | 0x80
4436
- }
4437
- } else {
4438
- num = this.toArray()
4439
- if ((num[0] & 0x80) !== 0) {
4440
- num = [0x00, ...num]
4441
- }
4442
- }
4443
-
4444
- if (num.length === 1 && num[0] === 0) {
4445
- num = []
4446
- }
4447
-
4448
- if (endian === 'little') {
4449
- num = num.reverse()
4450
- }
4451
-
4452
- return num
4453
- }
4454
-
4455
- /**
4456
- * Creates a BigNumber from a number representing the "bits" value in a block header.
4457
- *
4458
- * @static
4459
- * @method fromBits
4460
- * @param bits - The number representing the bits value in a block header.
4461
- * @param strict - If true, an error is thrown if the number has negative bit set.
4462
- * @returns Returns a BigNumber equivalent to the "bits" value in a block header.
4463
- * @throws Will throw an error if `strict` is `true` and the number has negative bit set.
4464
- *
4465
- * @example
4466
- * const bits = 0x1d00ffff;
4467
- * const bigNumber = BigNumber.fromBits(bits);
4468
- */
4469
- static fromBits (bits: number, strict: boolean = false): BigNumber {
4470
- // Convert to signed 32-bit value manually without using Buffer
4471
- bits = (bits & 0x80000000) ? bits - 0x100000000 : bits
4472
- if (strict && (bits & 0x00800000) !== 0) {
4473
- throw new Error('negative bit set')
4474
- }
4475
- const nsize = bits >> 24
4476
- const nword = bits & 0x007fffff
4477
-
4478
- // Manually create the byte array (similar to the original buffer)
4479
- let bytes = [
4480
- (nword >> 24) & 0xFF,
4481
- (nword >> 16) & 0xFF,
4482
- (nword >> 8) & 0xFF,
4483
- nword & 0xFF
4484
- ]
4485
-
4486
- if (nsize <= 3) {
4487
- bytes = bytes.slice(1, 1 + nsize) // remove the most significant byte(s) as necessary
4488
- } else {
4489
- // add trailing zeros (similar to the original buffer fill)
4490
- for (let i = 0; i < nsize - 3; i++) {
4491
- bytes.push(0)
4492
- }
4493
- }
4494
-
4495
- // Adjust for sign if the negative bit was set, and then convert array to BigNumber
4496
- if ((bits & 0x00800000) !== 0) {
4497
- return new BigNumber(bytes).neg()
4498
- } else {
4499
- return new BigNumber(bytes)
4500
- }
4501
- }
4502
-
4503
- /**
4504
- * Converts this BigNumber to a number representing the "bits" value in a block header.
4505
- *
4506
- * @method toBits
4507
- * @returns Returns a number equivalent to the "bits" value in a block header.
4508
- *
4509
- * @example
4510
- * const bigNumber = new BigNumber(1);
4511
- * const bits = bigNumber.toBits();
4512
- */
4513
- toBits (): number {
4514
- let byteArray
4515
- if (this.ltn(0)) {
4516
- byteArray = this.neg().toArray('be')
4517
- } else {
4518
- byteArray = this.toArray('be')
4519
- }
4520
-
4521
- // Ensure that the byte array is of a minimum size
4522
- while (byteArray.length < 4) {
4523
- byteArray.unshift(0)
4524
- }
4525
-
4526
- // For the case where byteArray represents '00', the bits should be 0x00000000
4527
- if (byteArray.every(byte => byte === 0)) {
4528
- return 0x00000000
4529
- }
4530
-
4531
- // Remove leading zeros from the byte array for further processing
4532
- while (byteArray[0] === 0) {
4533
- byteArray.shift()
4534
- }
4535
-
4536
- let nsize = byteArray.length
4537
-
4538
- // We're interested in the first three bytes for the "nword"
4539
- // or in smaller cases, what's available
4540
- let nword = byteArray.slice(0, 3).reduce((acc, val) => (acc * 256) + val, 0)
4541
-
4542
- // Ensure we don't have the sign bit set initially
4543
- if ((nword & 0x800000) !== 0) {
4544
- // If the 24th bit is set, we're going to need one more byte to represent this number
4545
- byteArray.unshift(0) // Unshift a zero byte to not change the actual number
4546
- nsize += 1
4547
- nword >>>= 8 // Shift right to make room for that byte
4548
- }
4549
-
4550
- // Encode size and the 3 bytes into "nword"
4551
- let bits = (nsize << 24) | nword
4552
-
4553
- if (this.ltn(0)) {
4554
- // If the number is negative, set the 0x00800000 bit to indicate sign
4555
- bits |= 0x00800000
4556
- }
4557
-
4558
- return bits >>> 0 // Convert to unsigned 32-bit integer
4559
- }
4560
-
4561
- /**
4562
- * Creates a BigNumber from the format used in Bitcoin scripts.
4563
- *
4564
- * @static
4565
- * @method fromScriptNum
4566
- * @param num - The number in the format used in Bitcoin scripts.
4567
- * @param requireMinimal - If true, non-minimally encoded values will throw an error.
4568
- * @param maxNumSize - The maximum allowed size for the number. If not provided, defaults to 4.
4569
- * @returns Returns a BigNumber equivalent to the number used in a Bitcoin script.
4570
- * @throws Will throw an error if `requireMinimal` is `true` and the value is non-minimally encoded. Will throw an error if number length is greater than `maxNumSize`.
4571
- *
4572
- * @example
4573
- * const num = [0x02, 0x01]
4574
- * const bigNumber = BigNumber.fromScriptNum(num, true, 5)
4575
- */
4576
- static fromScriptNum (
4577
- num: number[], requireMinimal?: boolean, maxNumSize?: number
4578
- ): BigNumber {
4579
- if (maxNumSize === undefined) {
4580
- maxNumSize = 4
4581
- }
4582
- if (num.length > maxNumSize) {
4583
- throw new Error('script number overflow')
4584
- }
4585
- if (requireMinimal && num.length > 0) {
4586
- // Check that the number is encoded with the minimum possible
4587
- // number of bytes.
4588
- //
4589
- // If the most-significant-byte - excluding the sign bit - is zero
4590
- // then we're not minimal. Note how this test also rejects the
4591
- // negative-zero encoding, 0x80.
4592
- if ((num[num.length - 1] & 0x7f) === 0) {
4593
- // One exception: if there's more than one byte and the most
4594
- // significant bit of the second-most-significant-byte is set
4595
- // it would conflict with the sign bit. An example of this case
4596
- // is +-255, which encode to 0xff00 and 0xff80 respectively.
4597
- // (big-endian).
4598
- if (num.length <= 1 || (num[num.length - 2] & 0x80) === 0) {
4599
- throw new Error('non-minimally encoded script number')
4600
- }
4601
- }
4602
- }
4603
- return BigNumber.fromSm(num, 'little')
4604
- }
4605
-
4606
- /**
4607
- * Converts this BigNumber to a number in the format used in Bitcoin scripts.
4608
- *
4609
- * @method toScriptNum
4610
- * @returns Returns the equivalent to this BigNumber as a Bitcoin script number.
4611
- *
4612
- * @example
4613
- * const bigNumber = new BigNumber(258)
4614
- * const num = bigNumber.toScriptNum() // equivalent to bigNumber.toSm('little')
4615
- */
4616
- toScriptNum (): number[] {
4617
- return this.toSm('little')
4618
- }
4619
- }