@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,528 +0,0 @@
1
- import BigNumber from './BigNumber.js'
2
- import K256 from './K256.js'
3
- import Mersenne from './Mersenne.js'
4
-
5
- /**
6
- * A base reduction engine that provides several arithmetic operations over
7
- * big numbers under a modulus context. It's particularly suitable for
8
- * calculations required in cryptography algorithms and encoding schemas.
9
- *
10
- * @class ReductionContext
11
- *
12
- * @property prime - The prime number utilised in the reduction context, typically an instance of Mersenne class.
13
- * @property m - The modulus used for reduction operations.
14
- */
15
- export default class ReductionContext {
16
- prime: Mersenne | null
17
- m: BigNumber
18
-
19
- /**
20
- * Constructs a new ReductionContext.
21
- *
22
- * @constructor
23
- * @param m - A BigNumber representing the modulus, or 'k256' to create a context for Koblitz curve.
24
- *
25
- * @example
26
- * new ReductionContext(new BigNumber(11));
27
- * new ReductionContext('k256');
28
- */
29
- constructor (m: BigNumber | 'k256') {
30
- if (m === 'k256') {
31
- const prime = new K256()
32
- this.m = prime.p
33
- this.prime = prime
34
- } else {
35
- this.assert(m.gtn(1), 'modulus must be greater than 1')
36
- this.m = m
37
- this.prime = null
38
- }
39
- }
40
-
41
- /**
42
- * Asserts that given value is truthy. Throws an Error with a provided message
43
- * if the value is falsy.
44
- *
45
- * @private
46
- * @param val - The value to be checked.
47
- * @param msg - The error message to be thrown if the value is falsy.
48
- *
49
- * @example
50
- * this.assert(1 < 2, '1 is not less than 2');
51
- * this.assert(2 < 1, '2 is less than 1'); // throws an Error with message '2 is less than 1'
52
- */
53
- private assert (val: unknown, msg: string = 'Assertion failed'): void {
54
- if (!(val as boolean)) throw new Error(msg)
55
- }
56
-
57
- /**
58
- * Verifies that a BigNumber is positive and red. Throws an error if these
59
- * conditions are not met.
60
- *
61
- * @param a - The BigNumber to be verified.
62
- *
63
- * @example
64
- * this.verify1(new BigNumber(10).toRed());
65
- * this.verify1(new BigNumber(-10).toRed()); //throws an Error
66
- * this.verify1(new BigNumber(10)); //throws an Error
67
- */
68
- verify1 (a: BigNumber): void {
69
- this.assert(a.negative === 0, 'red works only with positives')
70
- this.assert(a.red, 'red works only with red numbers')
71
- }
72
-
73
- /**
74
- * Verifies that two BigNumbers are both positive and red. Also checks
75
- * that they have the same reduction context. Throws an error if these
76
- * conditions are not met.
77
- *
78
- * @param a - The first BigNumber to be verified.
79
- * @param b - The second BigNumber to be verified.
80
- *
81
- * @example
82
- * this.verify2(new BigNumber(10).toRed(this), new BigNumber(20).toRed(this));
83
- * this.verify2(new BigNumber(-10).toRed(this), new BigNumber(20).toRed(this)); //throws an Error
84
- * this.verify2(new BigNumber(10).toRed(this), new BigNumber(20)); //throws an Error
85
- */
86
- verify2 (a: BigNumber, b: BigNumber): void {
87
- this.assert(
88
- (a.negative | b.negative) === 0,
89
- 'red works only with positives'
90
- )
91
- this.assert(
92
- (a.red != null) && a.red === b.red,
93
- 'red works only with red numbers'
94
- )
95
- }
96
-
97
- /**
98
- * Performs an in-place reduction of the given BigNumber by the modulus of the reduction context, 'm'.
99
- *
100
- * @method imod
101
- *
102
- * @param a - BigNumber to be reduced.
103
- *
104
- * @returns Returns the reduced result.
105
- *
106
- * @example
107
- * const context = new ReductionContext(new BigNumber(7));
108
- * context.imod(new BigNumber(19)); // Returns 5
109
- */
110
- imod (a: BigNumber): BigNumber {
111
- if (this.prime != null) return this.prime.ireduce(a).forceRed(this)
112
-
113
- BigNumber.move(a, a.umod(this.m).forceRed(this))
114
- return a
115
- }
116
-
117
- /**
118
- * Negates a BigNumber in the context of the modulus.
119
- *
120
- * @method neg
121
- *
122
- * @param a - BigNumber to negate.
123
- *
124
- * @returns Returns the negation of 'a' in the reduction context.
125
- *
126
- * @example
127
- * const context = new ReductionContext(new BigNumber(7));
128
- * context.neg(new BigNumber(3)); // Returns 4
129
- */
130
- neg (a: BigNumber): BigNumber {
131
- if (a.isZero()) {
132
- return a.clone()
133
- }
134
-
135
- return this.m.sub(a).forceRed(this)
136
- }
137
-
138
- /**
139
- * Performs the addition operation on two BigNumbers in the reduction context.
140
- *
141
- * @method add
142
- *
143
- * @param a - First BigNumber to add.
144
- * @param b - Second BigNumber to add.
145
- *
146
- * @returns Returns the result of 'a + b' in the reduction context.
147
- *
148
- * @example
149
- * const context = new ReductionContext(new BigNumber(5));
150
- * context.add(new BigNumber(2), new BigNumber(4)); // Returns 1
151
- */
152
- add (a: BigNumber, b: BigNumber): BigNumber {
153
- this.verify2(a, b)
154
-
155
- const res = a.add(b)
156
- if (res.cmp(this.m) >= 0) {
157
- res.isub(this.m)
158
- }
159
- return res.forceRed(this)
160
- }
161
-
162
- /**
163
- * Performs an in-place addition operation on two BigNumbers in the reduction context
164
- * in order to avoid creating a new BigNumber, it modifies the first one with the result.
165
- *
166
- * @method iadd
167
- *
168
- * @param a - First BigNumber to add.
169
- * @param b - Second BigNumber to add.
170
- *
171
- * @returns Returns the modified 'a' after addition with 'b' in the reduction context.
172
- *
173
- * @example
174
- * const context = new ReductionContext(new BigNumber(5));
175
- * const a = new BigNumber(2);
176
- * context.iadd(a, new BigNumber(4)); // Modifies 'a' to be 1
177
- */
178
- iadd (a: BigNumber, b: BigNumber): BigNumber {
179
- this.verify2(a, b)
180
-
181
- const res = a.iadd(b)
182
- if (res.cmp(this.m) >= 0) {
183
- res.isub(this.m)
184
- }
185
- return res
186
- }
187
-
188
- /**
189
- * Subtracts one BigNumber from another BigNumber in the reduction context.
190
- *
191
- * @method sub
192
- *
193
- * @param a - BigNumber to be subtracted from.
194
- * @param b - BigNumber to subtract.
195
- *
196
- * @returns Returns the result of 'a - b' in the reduction context.
197
- *
198
- * @example
199
- * const context = new ReductionContext(new BigNumber(7));
200
- * context.sub(new BigNumber(3), new BigNumber(2)); // Returns 1
201
- */
202
- sub (a: BigNumber, b: BigNumber): BigNumber {
203
- this.verify2(a, b)
204
-
205
- const res = a.sub(b)
206
- if (res.cmpn(0) < 0) {
207
- res.iadd(this.m)
208
- }
209
- return res.forceRed(this)
210
- }
211
-
212
- /**
213
- * Performs in-place subtraction of one BigNumber from another in the reduction context,
214
- * it modifies the first BigNumber with the result.
215
- *
216
- * @method isub
217
- *
218
- * @param a - BigNumber to be subtracted from.
219
- * @param b - BigNumber to subtract.
220
- *
221
- * @returns Returns the modified 'a' after subtraction of 'b' in the reduction context.
222
- *
223
- * @example
224
- * const context = new ReductionContext(new BigNumber(5));
225
- * const a = new BigNumber(4);
226
- * context.isub(a, new BigNumber(2)); // Modifies 'a' to be 2
227
- */
228
- isub (a: BigNumber, b: BigNumber): BigNumber {
229
- this.verify2(a, b)
230
-
231
- const res = a.isub(b)
232
- if (res.cmpn(0) < 0) {
233
- res.iadd(this.m)
234
- }
235
- return res
236
- }
237
-
238
- /**
239
- * Performs bitwise shift left operation on a BigNumber in the reduction context.
240
- *
241
- * @method shl
242
- *
243
- * @param a - BigNumber to perform shift on.
244
- * @param num - The number of positions to shift.
245
- *
246
- * @returns Returns the result of shifting 'a' left by 'num' positions in the reduction context.
247
- *
248
- * @example
249
- * const context = new ReductionContext(new BigNumber(32));
250
- * context.shl(new BigNumber(4), 2); // Returns 16
251
- */
252
- shl (a: BigNumber, num: number): BigNumber {
253
- this.verify1(a)
254
- return this.imod(a.ushln(num))
255
- }
256
-
257
- /**
258
- * Performs in-place multiplication of two BigNumbers in the reduction context,
259
- * modifying the first BigNumber with the result.
260
- *
261
- * @method imul
262
- *
263
- * @param a - First BigNumber to multiply.
264
- * @param b - Second BigNumber to multiply.
265
- *
266
- * @returns Returns the modified 'a' after multiplication with 'b' in the reduction context.
267
- *
268
- * @example
269
- * const context = new ReductionContext(new BigNumber(7));
270
- * const a = new BigNumber(3);
271
- * context.imul(a, new BigNumber(2)); // Modifies 'a' to be 6
272
- */
273
- imul (a: BigNumber, b: BigNumber): BigNumber {
274
- this.verify2(a, b)
275
- return this.imod(a.imul(b))
276
- }
277
-
278
- /**
279
- * Multiplies two BigNumbers in the reduction context.
280
- *
281
- * @method mul
282
- *
283
- * @param a - First BigNumber to multiply.
284
- * @param b - Second BigNumber to multiply.
285
- *
286
- * @returns Returns the result of 'a * b' in the reduction context.
287
- *
288
- * @example
289
- * const context = new ReductionContext(new BigNumber(7));
290
- * context.mul(new BigNumber(3), new BigNumber(2)); // Returns 6
291
- */
292
- mul (a: BigNumber, b: BigNumber): BigNumber {
293
- this.verify2(a, b)
294
- return this.imod(a.mul(b))
295
- }
296
-
297
- /**
298
- * Calculates the square of a BigNumber in the reduction context,
299
- * modifying the original BigNumber with the result.
300
- *
301
- * @method isqr
302
- *
303
- * @param a - BigNumber to be squared.
304
- *
305
- * @returns Returns the squared 'a' in the reduction context.
306
- *
307
- * @example
308
- * const context = new ReductionContext(new BigNumber(7));
309
- * const a = new BigNumber(3);
310
- * context.isqr(a); // Modifies 'a' to be 2 (9 % 7 = 2)
311
- */
312
- isqr (a: BigNumber): BigNumber {
313
- return this.imul(a, a.clone())
314
- }
315
-
316
- /**
317
- * Calculates the square of a BigNumber in the reduction context.
318
- *
319
- * @method sqr
320
- *
321
- * @param a - BigNumber to be squared.
322
- *
323
- * @returns Returns the result of 'a^2' in the reduction context.
324
- *
325
- * @example
326
- * const context = new ReductionContext(new BigNumber(7));
327
- * context.sqr(new BigNumber(3)); // Returns 2 (9 % 7 = 2)
328
- */
329
- sqr (a: BigNumber): BigNumber {
330
- return this.mul(a, a)
331
- }
332
-
333
- /**
334
- * Calculates the square root of a BigNumber in the reduction context.
335
- *
336
- * @method sqrt
337
- *
338
- * @param a - The BigNumber to calculate the square root of.
339
- *
340
- * @returns Returns the square root of 'a' in the reduction context.
341
- *
342
- * @example
343
- * const context = new ReductionContext(new BigNumber(9));
344
- * context.sqrt(new BigNumber(4)); // Returns 2
345
- */
346
- sqrt (a: BigNumber): BigNumber {
347
- if (a.isZero()) return a.clone()
348
-
349
- const mod3 = this.m.andln(3)
350
- this.assert(mod3 % 2 === 1)
351
-
352
- // Fast case
353
- if (mod3 === 3) {
354
- const pow = this.m.add(new BigNumber(1)).iushrn(2)
355
- return this.pow(a, pow)
356
- }
357
-
358
- // Tonelli-Shanks algorithm (Totally unoptimized and slow)
359
- //
360
- // Find Q and S, that Q * 2 ^ S = (P - 1)
361
- const q = this.m.subn(1)
362
- let s = 0
363
- while (!q.isZero() && q.andln(1) === 0) {
364
- s++
365
- q.iushrn(1)
366
- }
367
- this.assert(!q.isZero())
368
-
369
- const one = new BigNumber(1).toRed(this)
370
- const nOne = one.redNeg()
371
-
372
- // Find quadratic non-residue
373
- // NOTE: Max is such because of generalized Riemann hypothesis.
374
- const lpow = this.m.subn(1).iushrn(1)
375
- const zl = this.m.bitLength()
376
- const z = new BigNumber(2 * zl * zl).toRed(this)
377
-
378
- while (this.pow(z, lpow).cmp(nOne) !== 0) {
379
- z.redIAdd(nOne)
380
- }
381
-
382
- let c = this.pow(z, q)
383
- let r = this.pow(a, q.addn(1).iushrn(1))
384
- let t = this.pow(a, q)
385
- let m = s
386
- while (t.cmp(one) !== 0) {
387
- let tmp = t
388
- let i = 0
389
- for (; tmp.cmp(one) !== 0; i++) {
390
- tmp = tmp.redSqr()
391
- }
392
- this.assert(i < m)
393
- const b = this.pow(c, new BigNumber(1).iushln(m - i - 1))
394
-
395
- r = r.redMul(b)
396
- c = b.redSqr()
397
- t = t.redMul(c)
398
- m = i
399
- }
400
-
401
- return r
402
- }
403
-
404
- /**
405
- * Calculates the multiplicative inverse of a BigNumber in the reduction context.
406
- *
407
- * @method invm
408
- *
409
- * @param a - The BigNumber to find the multiplicative inverse of.
410
- *
411
- * @returns Returns the multiplicative inverse of 'a' in the reduction context.
412
- *
413
- * @example
414
- * const context = new ReductionContext(new BigNumber(11));
415
- * context.invm(new BigNumber(3)); // Returns 4 (3*4 mod 11 = 1)
416
- */
417
- invm (a: BigNumber): BigNumber {
418
- const inv = a._invmp(this.m)
419
- if (inv.negative !== 0) {
420
- inv.negative = 0
421
- return this.imod(inv).redNeg()
422
- } else {
423
- return this.imod(inv)
424
- }
425
- }
426
-
427
- /**
428
- * Raises a BigNumber to a power in the reduction context.
429
- *
430
- * @method pow
431
- *
432
- * @param a - The BigNumber to be raised to a power.
433
- * @param num - The power to raise the BigNumber to.
434
- *
435
- * @returns Returns the result of 'a' raised to the power of 'num' in the reduction context.
436
- *
437
- * @example
438
- * const context = new ReductionContext(new BigNumber(7));
439
- * context.pow(new BigNumber(3), new BigNumber(2)); // Returns 2 (3^2 % 7)
440
- */
441
- pow (a: BigNumber, num: BigNumber): BigNumber {
442
- if (num.isZero()) return new BigNumber(1).toRed(this)
443
- if (num.cmpn(1) === 0) return a.clone()
444
-
445
- const windowSize = 4
446
- const wnd = new Array(1 << windowSize)
447
- wnd[0] = new BigNumber(1).toRed(this)
448
- wnd[1] = a
449
- let i = 2
450
- for (; i < wnd.length; i++) {
451
- wnd[i] = this.mul(wnd[i - 1], a)
452
- }
453
-
454
- let res = wnd[0]
455
- let current = 0
456
- let currentLen = 0
457
- let start = num.bitLength() % 26
458
- if (start === 0) {
459
- start = 26
460
- }
461
-
462
- for (i = num.length - 1; i >= 0; i--) {
463
- const word = num.words[i]
464
- for (let j = start - 1; j >= 0; j--) {
465
- const bit = (word >> j) & 1
466
- if (res !== wnd[0]) {
467
- res = this.sqr(res)
468
- }
469
-
470
- if (bit === 0 && current === 0) {
471
- currentLen = 0
472
- continue
473
- }
474
-
475
- current <<= 1
476
- current |= bit
477
- currentLen++
478
- if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue
479
-
480
- res = this.mul(res, wnd[current])
481
- currentLen = 0
482
- current = 0
483
- }
484
- start = 26
485
- }
486
-
487
- return res
488
- }
489
-
490
- /**
491
- * Converts a BigNumber to its equivalent in the reduction context.
492
- *
493
- * @method convertTo
494
- *
495
- * @param num - The BigNumber to convert to the reduction context.
496
- *
497
- * @returns Returns the converted BigNumber compatible with the reduction context.
498
- *
499
- * @example
500
- * const context = new ReductionContext(new BigNumber(7));
501
- * context.convertTo(new BigNumber(8)); // Returns 1 (8 % 7)
502
- */
503
- convertTo (num: BigNumber): BigNumber {
504
- const r = num.umod(this.m)
505
-
506
- return r === num ? r.clone() : r
507
- }
508
-
509
- /**
510
- * Converts a BigNumber from reduction context to its regular form.
511
- *
512
- * @method convertFrom
513
- *
514
- * @param num - The BigNumber to convert from the reduction context.
515
- *
516
- * @returns Returns the converted BigNumber in its regular form.
517
- *
518
- * @example
519
- * const context = new ReductionContext(new BigNumber(7));
520
- * const a = context.convertTo(new BigNumber(8)); // 'a' is now 1 in the reduction context
521
- * context.convertFrom(a); // Returns 1
522
- */
523
- convertFrom (num: BigNumber): BigNumber {
524
- const res = num.clone()
525
- res.red = null
526
- return res
527
- }
528
- }