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