@bsv/sdk 1.0.29 → 1.0.31

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