@bsv/sdk 1.0.1 → 1.0.2

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 (112) hide show
  1. package/dist/cjs/package.json +8 -4
  2. package/dist/cjs/src/transaction/broadcasters/ARC.js +13 -2
  3. package/dist/cjs/src/transaction/broadcasters/ARC.js.map +1 -1
  4. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  5. package/dist/esm/src/transaction/broadcasters/ARC.js +13 -2
  6. package/dist/esm/src/transaction/broadcasters/ARC.js.map +1 -1
  7. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  8. package/dist/types/src/transaction/broadcasters/ARC.d.ts.map +1 -1
  9. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  10. package/package.json +2 -1
  11. package/src/compat/BSM.ts +51 -0
  12. package/src/compat/ECIES.ts +557 -0
  13. package/src/compat/HD.ts +348 -0
  14. package/src/compat/Mnemonic.ts +295 -0
  15. package/src/compat/__tests/BSM.test.ts +38 -0
  16. package/src/compat/__tests/ECIES.test.ts +90 -0
  17. package/src/compat/__tests/HD.test.ts +405 -0
  18. package/src/compat/__tests/Mnemonic.test.ts +177 -0
  19. package/src/compat/__tests/Mnemonic.vectors.ts +172 -0
  20. package/src/compat/bip-39-wordlist-en.ts +2053 -0
  21. package/src/compat/index.ts +4 -0
  22. package/src/messages/EncryptedMessage.ts +70 -0
  23. package/src/messages/SignedMessage.ts +87 -0
  24. package/src/messages/__tests/EncryptedMessage.test.ts +36 -0
  25. package/src/messages/__tests/SignedMessage.test.ts +53 -0
  26. package/src/messages/index.ts +2 -0
  27. package/src/primitives/AESGCM.ts +479 -0
  28. package/src/primitives/BasePoint.ts +21 -0
  29. package/src/primitives/BigNumber.ts +4619 -0
  30. package/src/primitives/Curve.ts +1163 -0
  31. package/src/primitives/DRBG.ts +102 -0
  32. package/src/primitives/ECDSA.ts +164 -0
  33. package/src/primitives/Hash.ts +1420 -0
  34. package/src/primitives/JacobianPoint.ts +410 -0
  35. package/src/primitives/K256.ts +116 -0
  36. package/src/primitives/Mersenne.ts +123 -0
  37. package/src/primitives/MontgomoryMethod.ts +160 -0
  38. package/src/primitives/Point.ts +852 -0
  39. package/src/primitives/PrivateKey.ts +195 -0
  40. package/src/primitives/PublicKey.ts +154 -0
  41. package/src/primitives/Random.ts +55 -0
  42. package/src/primitives/ReductionContext.ts +528 -0
  43. package/src/primitives/Signature.ts +235 -0
  44. package/src/primitives/SymmetricKey.ts +75 -0
  45. package/src/primitives/TransactionSignature.ts +189 -0
  46. package/src/primitives/__tests/AESGCM.test.ts +338 -0
  47. package/src/primitives/__tests/BRC42.private.vectors.ts +33 -0
  48. package/src/primitives/__tests/BRC42.public.vectors.ts +33 -0
  49. package/src/primitives/__tests/BigNumber.arithmatic.test.ts +572 -0
  50. package/src/primitives/__tests/BigNumber.binary.test.ts +203 -0
  51. package/src/primitives/__tests/BigNumber.constructor.test.ts +176 -0
  52. package/src/primitives/__tests/BigNumber.dhGroup.test.ts +18 -0
  53. package/src/primitives/__tests/BigNumber.fixtures.ts +264 -0
  54. package/src/primitives/__tests/BigNumber.serializers.test.ts +157 -0
  55. package/src/primitives/__tests/BigNumber.utils.test.ts +347 -0
  56. package/src/primitives/__tests/Curve.unit.test.ts +192 -0
  57. package/src/primitives/__tests/DRBG.test.ts +18 -0
  58. package/src/primitives/__tests/DRBG.vectors.ts +167 -0
  59. package/src/primitives/__tests/ECDH.test.ts +31 -0
  60. package/src/primitives/__tests/ECDSA.test.ts +58 -0
  61. package/src/primitives/__tests/HMAC.test.ts +59 -0
  62. package/src/primitives/__tests/Hash.test.ts +121 -0
  63. package/src/primitives/__tests/PBKDF2.vectors.ts +119 -0
  64. package/src/primitives/__tests/PrivateKey.test.ts +17 -0
  65. package/src/primitives/__tests/PublicKey.test.ts +66 -0
  66. package/src/primitives/__tests/Random.test.ts +14 -0
  67. package/src/primitives/__tests/Reader.test.ts +296 -0
  68. package/src/primitives/__tests/ReductionContext.test.ts +279 -0
  69. package/src/primitives/__tests/SymmetricKey.test.ts +58 -0
  70. package/src/primitives/__tests/SymmetricKey.vectors.ts +40 -0
  71. package/src/primitives/__tests/Writer.test.ts +198 -0
  72. package/src/primitives/__tests/sighash.vectors.ts +3503 -0
  73. package/src/primitives/__tests/utils.test.ts +108 -0
  74. package/src/primitives/index.ts +8 -0
  75. package/src/primitives/utils.ts +665 -0
  76. package/src/script/LockingScript.ts +30 -0
  77. package/src/script/OP.ts +219 -0
  78. package/src/script/Script.ts +426 -0
  79. package/src/script/ScriptChunk.ts +7 -0
  80. package/src/script/ScriptTemplate.ts +36 -0
  81. package/src/script/Spend.ts +1379 -0
  82. package/src/script/UnlockingScript.ts +30 -0
  83. package/src/script/__tests/Script.test.ts +369 -0
  84. package/src/script/__tests/Spend.test.ts +248 -0
  85. package/src/script/__tests/script.invalid.vectors.ts +925 -0
  86. package/src/script/__tests/script.valid.vectors.ts +1120 -0
  87. package/src/script/__tests/scriptFromVector.ts +42 -0
  88. package/src/script/__tests/spend.valid.vectors.ts +2288 -0
  89. package/src/script/index.ts +7 -0
  90. package/src/script/templates/P2PKH.ts +109 -0
  91. package/src/script/templates/RPuzzle.ts +140 -0
  92. package/src/script/templates/index.ts +2 -0
  93. package/src/transaction/Broadcaster.ts +42 -0
  94. package/src/transaction/ChainTracker.ts +22 -0
  95. package/src/transaction/FeeModel.ts +13 -0
  96. package/src/transaction/MerklePath.ts +259 -0
  97. package/src/transaction/Transaction.ts +602 -0
  98. package/src/transaction/TransactionInput.ts +63 -0
  99. package/src/transaction/TransactionOutput.ts +37 -0
  100. package/src/transaction/__tests/MerklePath.test.ts +181 -0
  101. package/src/transaction/__tests/Transaction.test.ts +413 -0
  102. package/src/transaction/__tests/bigtx.vectors.ts +4 -0
  103. package/src/transaction/__tests/bump.invalid.vectors.ts +8 -0
  104. package/src/transaction/__tests/bump.valid.vectors.ts +4 -0
  105. package/src/transaction/__tests/tx.invalid.vectors.ts +281 -0
  106. package/src/transaction/__tests/tx.valid.vectors.ts +364 -0
  107. package/src/transaction/broadcasters/ARC.ts +115 -0
  108. package/src/transaction/broadcasters/__tests/ARC.test.ts +115 -0
  109. package/src/transaction/broadcasters/index.ts +1 -0
  110. package/src/transaction/fee-models/SatoshisPerKilobyte.ts +71 -0
  111. package/src/transaction/fee-models/index.ts +1 -0
  112. package/src/transaction/index.ts +6 -0
@@ -0,0 +1,160 @@
1
+ import ReductionContext from './ReductionContext.js'
2
+ import BigNumber from './BigNumber.js'
3
+
4
+ /**
5
+ * Represents a Montgomery reduction context, which is a mathematical method
6
+ * for performing modular multiplication without division.
7
+ *
8
+ * Montgomery reduction is an algorithm used mainly in cryptography which can
9
+ * help to speed up calculations in contexts where there are many repeated
10
+ * computations.
11
+ *
12
+ * This class extends the `ReductionContext` class.
13
+ *
14
+ * @class MontgomoryMethod
15
+ * @extends {ReductionContext}
16
+ *
17
+ * @property shift - The number of bits in the modulus.
18
+ * @property r - The 2^shift, shifted left by the bit length of modulus `m`.
19
+ * @property r2 - The square of `r` modulo `m`.
20
+ * @property rinv - The modular multiplicative inverse of `r` mod `m`.
21
+ * @property minv - The modular multiplicative inverse of `m` mod `r`.
22
+ */
23
+ export default class MontgomoryMethod extends ReductionContext {
24
+ shift: number
25
+ r: BigNumber
26
+ r2: BigNumber
27
+ rinv: BigNumber
28
+ minv: BigNumber
29
+
30
+ /**
31
+ * @constructor
32
+ * @param m - The modulus to be used for the Montgomery method reductions.
33
+ */
34
+ constructor (m: BigNumber | 'k256') {
35
+ super(m)
36
+
37
+ this.shift = this.m.bitLength()
38
+ if (this.shift % 26 !== 0) {
39
+ this.shift += 26 - (this.shift % 26)
40
+ }
41
+
42
+ this.r = new BigNumber(1).iushln(this.shift)
43
+ this.r2 = this.imod(this.r.sqr())
44
+ this.rinv = this.r._invmp(this.m)
45
+
46
+ this.minv = this.rinv.mul(this.r).isubn(1).div(this.m)
47
+ this.minv = this.minv.umod(this.r)
48
+ this.minv = this.r.sub(this.minv)
49
+ }
50
+
51
+ /**
52
+ * Converts a number into the Montgomery domain.
53
+ *
54
+ * @method convertTo
55
+ * @param num - The number to be converted into the Montgomery domain.
56
+ * @returns The result of the conversion into the Montgomery domain.
57
+ *
58
+ * @example
59
+ * const montMethod = new MontgomoryMethod(m);
60
+ * const convertedNum = montMethod.convertTo(num);
61
+ */
62
+ convertTo (num: BigNumber): BigNumber {
63
+ return this.imod(num.ushln(this.shift))
64
+ }
65
+
66
+ /**
67
+ * Converts a number from the Montgomery domain back to the original domain.
68
+ *
69
+ * @method convertFrom
70
+ * @param num - The number to be converted from the Montgomery domain.
71
+ * @returns The result of the conversion from the Montgomery domain.
72
+ *
73
+ * @example
74
+ * const montMethod = new MontgomoryMethod(m);
75
+ * const convertedNum = montMethod.convertFrom(num);
76
+ */
77
+ convertFrom (num: BigNumber): BigNumber {
78
+ const r = this.imod(num.mul(this.rinv))
79
+ r.red = null
80
+ return r
81
+ }
82
+
83
+ /**
84
+ * Performs an in-place multiplication of two numbers in the Montgomery domain.
85
+ *
86
+ * @method imul
87
+ * @param a - The first number to multiply.
88
+ * @param b - The second number to multiply.
89
+ * @returns The result of the in-place multiplication.
90
+ *
91
+ * @example
92
+ * const montMethod = new MontgomoryMethod(m);
93
+ * const product = montMethod.imul(a, b);
94
+ */
95
+ imul (a: BigNumber, b: BigNumber): BigNumber {
96
+ if (a.isZero() || b.isZero()) {
97
+ a.words[0] = 0
98
+ a.length = 1
99
+ return a
100
+ }
101
+
102
+ const t = a.imul(b)
103
+ const c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m)
104
+ const u = t.isub(c).iushrn(this.shift)
105
+ let res = u
106
+
107
+ if (u.cmp(this.m) >= 0) {
108
+ res = u.isub(this.m)
109
+ } else if (u.cmpn(0) < 0) {
110
+ res = u.iadd(this.m)
111
+ }
112
+
113
+ return res.forceRed(this)
114
+ }
115
+
116
+ /**
117
+ * Performs the multiplication of two numbers in the Montgomery domain.
118
+ *
119
+ * @method mul
120
+ * @param a - The first number to multiply.
121
+ * @param b - The second number to multiply.
122
+ * @returns The result of the multiplication.
123
+ *
124
+ * @example
125
+ * const montMethod = new MontgomoryMethod(m);
126
+ * const product = montMethod.mul(a, b);
127
+ */
128
+ mul (a: BigNumber, b: BigNumber): BigNumber {
129
+ if (a.isZero() || b.isZero()) return new BigNumber(0).forceRed(this)
130
+
131
+ const t = a.mul(b)
132
+ const c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m)
133
+ const u = t.isub(c).iushrn(this.shift)
134
+ let res = u
135
+ if (u.cmp(this.m) >= 0) {
136
+ res = u.isub(this.m)
137
+ } else if (u.cmpn(0) < 0) {
138
+ res = u.iadd(this.m)
139
+ }
140
+
141
+ return res.forceRed(this)
142
+ }
143
+
144
+ /**
145
+ * Calculates the modular multiplicative inverse of a number in the Montgomery domain.
146
+ *
147
+ * @method invm
148
+ * @param a - The number to compute the modular multiplicative inverse of.
149
+ * @returns The modular multiplicative inverse of 'a'.
150
+ *
151
+ * @example
152
+ * const montMethod = new MontgomoryMethod(m);
153
+ * const inverse = montMethod.invm(a);
154
+ */
155
+ invm (a: BigNumber): BigNumber {
156
+ // (AR)^-1 * R^2 = (A^-1 * R^-1) * R^2 = A^-1 * R
157
+ const res = this.imod(a._invmp(this.m).mul(this.r2))
158
+ return res.forceRed(this)
159
+ }
160
+ }