ccxt 4.2.11 → 4.2.12

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 (375) hide show
  1. package/README.md +5 -5
  2. package/build.sh +2 -2
  3. package/dist/ccxt.browser.js +640 -261
  4. package/dist/ccxt.browser.min.js +3 -3
  5. package/dist/cjs/_virtual/agent.js +7 -0
  6. package/dist/cjs/_virtual/parse-proxy-response.js +7 -0
  7. package/dist/cjs/_virtual/promisify.js +7 -0
  8. package/dist/cjs/ccxt.js +1 -1
  9. package/dist/cjs/js/ccxt.js +474 -0
  10. package/dist/cjs/js/src/abstract/ace.js +9 -0
  11. package/dist/cjs/js/src/abstract/alpaca.js +9 -0
  12. package/dist/cjs/js/src/abstract/ascendex.js +9 -0
  13. package/dist/cjs/js/src/abstract/bigone.js +9 -0
  14. package/dist/cjs/js/src/abstract/binance.js +9 -0
  15. package/dist/cjs/js/src/abstract/bingx.js +9 -0
  16. package/dist/cjs/js/src/abstract/bit2c.js +9 -0
  17. package/dist/cjs/js/src/abstract/bitbank.js +9 -0
  18. package/dist/cjs/js/src/abstract/bitbns.js +9 -0
  19. package/dist/cjs/js/src/abstract/bitfinex.js +9 -0
  20. package/dist/cjs/js/src/abstract/bitfinex2.js +9 -0
  21. package/dist/cjs/js/src/abstract/bitflyer.js +9 -0
  22. package/dist/cjs/js/src/abstract/bitforex.js +9 -0
  23. package/dist/cjs/js/src/abstract/bitget.js +9 -0
  24. package/dist/cjs/js/src/abstract/bithumb.js +9 -0
  25. package/dist/cjs/js/src/abstract/bitmart.js +9 -0
  26. package/dist/cjs/js/src/abstract/bitmex.js +9 -0
  27. package/dist/cjs/js/src/abstract/bitopro.js +9 -0
  28. package/dist/cjs/js/src/abstract/bitpanda.js +9 -0
  29. package/dist/cjs/js/src/abstract/bitrue.js +9 -0
  30. package/dist/cjs/js/src/abstract/bitso.js +9 -0
  31. package/dist/cjs/js/src/abstract/bitstamp.js +9 -0
  32. package/dist/cjs/js/src/abstract/bitteam.js +9 -0
  33. package/dist/cjs/js/src/abstract/bitvavo.js +9 -0
  34. package/dist/cjs/js/src/abstract/bl3p.js +9 -0
  35. package/dist/cjs/js/src/abstract/blockchaincom.js +9 -0
  36. package/dist/cjs/js/src/abstract/btcalpha.js +9 -0
  37. package/dist/cjs/js/src/abstract/btcbox.js +9 -0
  38. package/dist/cjs/js/src/abstract/btcmarkets.js +9 -0
  39. package/dist/cjs/js/src/abstract/btcturk.js +9 -0
  40. package/dist/cjs/js/src/abstract/bybit.js +9 -0
  41. package/dist/cjs/js/src/abstract/cex.js +9 -0
  42. package/dist/cjs/js/src/abstract/coinbase.js +9 -0
  43. package/dist/cjs/js/src/abstract/coinbasepro.js +9 -0
  44. package/dist/cjs/js/src/abstract/coincheck.js +9 -0
  45. package/dist/cjs/js/src/abstract/coinex.js +9 -0
  46. package/dist/cjs/js/src/abstract/coinlist.js +9 -0
  47. package/dist/cjs/js/src/abstract/coinmate.js +9 -0
  48. package/dist/cjs/js/src/abstract/coinone.js +9 -0
  49. package/dist/cjs/js/src/abstract/coinsph.js +9 -0
  50. package/dist/cjs/js/src/abstract/coinspot.js +9 -0
  51. package/dist/cjs/js/src/abstract/cryptocom.js +9 -0
  52. package/dist/cjs/js/src/abstract/currencycom.js +9 -0
  53. package/dist/cjs/js/src/abstract/delta.js +9 -0
  54. package/dist/cjs/js/src/abstract/deribit.js +9 -0
  55. package/dist/cjs/js/src/abstract/digifinex.js +9 -0
  56. package/dist/cjs/js/src/abstract/exmo.js +9 -0
  57. package/dist/cjs/js/src/abstract/gate.js +9 -0
  58. package/dist/cjs/js/src/abstract/gemini.js +9 -0
  59. package/dist/cjs/js/src/abstract/hitbtc.js +9 -0
  60. package/dist/cjs/js/src/abstract/hollaex.js +9 -0
  61. package/dist/cjs/js/src/abstract/htx.js +9 -0
  62. package/dist/cjs/js/src/abstract/huobijp.js +9 -0
  63. package/dist/cjs/js/src/abstract/idex.js +9 -0
  64. package/dist/cjs/js/src/abstract/independentreserve.js +9 -0
  65. package/dist/cjs/js/src/abstract/indodax.js +9 -0
  66. package/dist/cjs/js/src/abstract/kraken.js +9 -0
  67. package/dist/cjs/js/src/abstract/krakenfutures.js +9 -0
  68. package/dist/cjs/js/src/abstract/kucoin.js +9 -0
  69. package/dist/cjs/js/src/abstract/kucoinfutures.js +9 -0
  70. package/dist/cjs/js/src/abstract/kuna.js +9 -0
  71. package/dist/cjs/js/src/abstract/latoken.js +9 -0
  72. package/dist/cjs/js/src/abstract/lbank.js +9 -0
  73. package/dist/cjs/js/src/abstract/luno.js +9 -0
  74. package/dist/cjs/js/src/abstract/lykke.js +9 -0
  75. package/dist/cjs/js/src/abstract/mercado.js +9 -0
  76. package/dist/cjs/js/src/abstract/mexc.js +9 -0
  77. package/dist/cjs/js/src/abstract/ndax.js +9 -0
  78. package/dist/cjs/js/src/abstract/novadax.js +9 -0
  79. package/dist/cjs/js/src/abstract/oceanex.js +9 -0
  80. package/dist/cjs/js/src/abstract/okcoin.js +9 -0
  81. package/dist/cjs/js/src/abstract/okx.js +9 -0
  82. package/dist/cjs/js/src/abstract/p2b.js +9 -0
  83. package/dist/cjs/js/src/abstract/paymium.js +9 -0
  84. package/dist/cjs/js/src/abstract/phemex.js +9 -0
  85. package/dist/cjs/js/src/abstract/poloniex.js +9 -0
  86. package/dist/cjs/js/src/abstract/poloniexfutures.js +9 -0
  87. package/dist/cjs/js/src/abstract/probit.js +9 -0
  88. package/dist/cjs/js/src/abstract/timex.js +9 -0
  89. package/dist/cjs/js/src/abstract/tokocrypto.js +9 -0
  90. package/dist/cjs/js/src/abstract/upbit.js +9 -0
  91. package/dist/cjs/js/src/abstract/wavesexchange.js +9 -0
  92. package/dist/cjs/js/src/abstract/wazirx.js +9 -0
  93. package/dist/cjs/js/src/abstract/whitebit.js +9 -0
  94. package/dist/cjs/js/src/abstract/woo.js +9 -0
  95. package/dist/cjs/js/src/abstract/yobit.js +9 -0
  96. package/dist/cjs/js/src/abstract/zaif.js +9 -0
  97. package/dist/cjs/js/src/abstract/zonda.js +9 -0
  98. package/dist/cjs/js/src/ace.js +1058 -0
  99. package/dist/cjs/js/src/alpaca.js +1125 -0
  100. package/dist/cjs/js/src/ascendex.js +3360 -0
  101. package/dist/cjs/js/src/base/Exchange.js +5110 -0
  102. package/dist/cjs/js/src/base/Precise.js +263 -0
  103. package/dist/cjs/js/src/base/errors.js +299 -0
  104. package/dist/cjs/js/src/base/functions/crypto.js +78 -0
  105. package/dist/cjs/js/src/base/functions/encode.js +44 -0
  106. package/dist/cjs/js/src/base/functions/generic.js +193 -0
  107. package/dist/cjs/js/src/base/functions/misc.js +96 -0
  108. package/dist/cjs/js/src/base/functions/number.js +297 -0
  109. package/dist/cjs/js/src/base/functions/platform.js +28 -0
  110. package/dist/cjs/js/src/base/functions/rsa.js +34 -0
  111. package/dist/cjs/js/src/base/functions/string.js +48 -0
  112. package/dist/cjs/js/src/base/functions/throttle.js +66 -0
  113. package/dist/cjs/js/src/base/functions/time.js +187 -0
  114. package/dist/cjs/js/src/base/functions/totp.js +24 -0
  115. package/dist/cjs/js/src/base/functions/type.js +162 -0
  116. package/dist/cjs/js/src/base/functions.js +157 -0
  117. package/dist/cjs/js/src/base/ws/Cache.js +254 -0
  118. package/dist/cjs/js/src/base/ws/Client.js +299 -0
  119. package/dist/cjs/js/src/base/ws/Future.js +34 -0
  120. package/dist/cjs/js/src/base/ws/OrderBook.js +107 -0
  121. package/dist/cjs/js/src/base/ws/OrderBookSide.js +281 -0
  122. package/dist/cjs/js/src/base/ws/WsClient.js +69 -0
  123. package/dist/cjs/js/src/bequant.js +33 -0
  124. package/dist/cjs/js/src/bigone.js +2142 -0
  125. package/dist/cjs/js/src/binance.js +9729 -0
  126. package/dist/cjs/js/src/binancecoinm.js +45 -0
  127. package/dist/cjs/js/src/binanceus.js +84 -0
  128. package/dist/cjs/js/src/binanceusdm.js +58 -0
  129. package/dist/cjs/js/src/bingx.js +3737 -0
  130. package/dist/cjs/js/src/bit2c.js +916 -0
  131. package/dist/cjs/js/src/bitbank.js +1000 -0
  132. package/dist/cjs/js/src/bitbay.js +17 -0
  133. package/dist/cjs/js/src/bitbns.js +1220 -0
  134. package/dist/cjs/js/src/bitcoincom.js +17 -0
  135. package/dist/cjs/js/src/bitfinex.js +1670 -0
  136. package/dist/cjs/js/src/bitfinex2.js +2990 -0
  137. package/dist/cjs/js/src/bitflyer.js +1045 -0
  138. package/dist/cjs/js/src/bitforex.js +852 -0
  139. package/dist/cjs/js/src/bitget.js +8284 -0
  140. package/dist/cjs/js/src/bithumb.js +1090 -0
  141. package/dist/cjs/js/src/bitmart.js +4454 -0
  142. package/dist/cjs/js/src/bitmex.js +2884 -0
  143. package/dist/cjs/js/src/bitopro.js +1724 -0
  144. package/dist/cjs/js/src/bitpanda.js +2002 -0
  145. package/dist/cjs/js/src/bitrue.js +3253 -0
  146. package/dist/cjs/js/src/bitso.js +1753 -0
  147. package/dist/cjs/js/src/bitstamp.js +2188 -0
  148. package/dist/cjs/js/src/bitteam.js +2309 -0
  149. package/dist/cjs/js/src/bitvavo.js +1968 -0
  150. package/dist/cjs/js/src/bl3p.js +447 -0
  151. package/dist/cjs/js/src/blockchaincom.js +1160 -0
  152. package/dist/cjs/js/src/btcalpha.js +929 -0
  153. package/dist/cjs/js/src/btcbox.js +565 -0
  154. package/dist/cjs/js/src/btcmarkets.js +1237 -0
  155. package/dist/cjs/js/src/btcturk.js +929 -0
  156. package/dist/cjs/js/src/bybit.js +7646 -0
  157. package/dist/cjs/js/src/cex.js +1693 -0
  158. package/dist/cjs/js/src/coinbase.js +3274 -0
  159. package/dist/cjs/js/src/coinbasepro.js +1866 -0
  160. package/dist/cjs/js/src/coincheck.js +843 -0
  161. package/dist/cjs/js/src/coinex.js +5414 -0
  162. package/dist/cjs/js/src/coinlist.js +2329 -0
  163. package/dist/cjs/js/src/coinmate.js +989 -0
  164. package/dist/cjs/js/src/coinone.js +1185 -0
  165. package/dist/cjs/js/src/coinsph.js +1933 -0
  166. package/dist/cjs/js/src/coinspot.js +548 -0
  167. package/dist/cjs/js/src/cryptocom.js +3007 -0
  168. package/dist/cjs/js/src/currencycom.js +2015 -0
  169. package/dist/cjs/js/src/delta.js +3256 -0
  170. package/dist/cjs/js/src/deribit.js +3306 -0
  171. package/dist/cjs/js/src/digifinex.js +4307 -0
  172. package/dist/cjs/js/src/exmo.js +2645 -0
  173. package/dist/cjs/js/src/fmfwio.js +34 -0
  174. package/dist/cjs/js/src/gate.js +7054 -0
  175. package/dist/cjs/js/src/gateio.js +16 -0
  176. package/dist/cjs/js/src/gemini.js +1801 -0
  177. package/dist/cjs/js/src/hitbtc.js +3660 -0
  178. package/dist/cjs/js/src/hitbtc3.js +19 -0
  179. package/dist/cjs/js/src/hollaex.js +1882 -0
  180. package/dist/cjs/js/src/htx.js +9024 -0
  181. package/dist/cjs/js/src/huobi.js +16 -0
  182. package/dist/cjs/js/src/huobijp.js +1918 -0
  183. package/dist/cjs/js/src/idex.js +1770 -0
  184. package/dist/cjs/js/src/independentreserve.js +759 -0
  185. package/dist/cjs/js/src/indodax.js +1069 -0
  186. package/dist/cjs/js/src/kraken.js +2861 -0
  187. package/dist/cjs/js/src/krakenfutures.js +2407 -0
  188. package/dist/cjs/js/src/kucoin.js +4489 -0
  189. package/dist/cjs/js/src/kucoinfutures.js +2475 -0
  190. package/dist/cjs/js/src/kuna.js +1949 -0
  191. package/dist/cjs/js/src/latoken.js +1729 -0
  192. package/dist/cjs/js/src/lbank.js +2825 -0
  193. package/dist/cjs/js/src/luno.js +1044 -0
  194. package/dist/cjs/js/src/lykke.js +1303 -0
  195. package/dist/cjs/js/src/mercado.js +897 -0
  196. package/dist/cjs/js/src/mexc.js +5407 -0
  197. package/dist/cjs/js/src/ndax.js +2450 -0
  198. package/dist/cjs/js/src/novadax.js +1556 -0
  199. package/dist/cjs/js/src/oceanex.js +964 -0
  200. package/dist/cjs/js/src/okcoin.js +3115 -0
  201. package/dist/cjs/js/src/okx.js +7329 -0
  202. package/dist/cjs/js/src/p2b.js +1243 -0
  203. package/dist/cjs/js/src/paymium.js +597 -0
  204. package/dist/cjs/js/src/phemex.js +4715 -0
  205. package/dist/cjs/js/src/poloniex.js +2356 -0
  206. package/dist/cjs/js/src/poloniexfutures.js +1794 -0
  207. package/dist/cjs/js/src/pro/alpaca.js +714 -0
  208. package/dist/cjs/js/src/pro/ascendex.js +957 -0
  209. package/dist/cjs/js/src/pro/bequant.js +33 -0
  210. package/dist/cjs/js/src/pro/binance.js +2796 -0
  211. package/dist/cjs/js/src/pro/binancecoinm.js +23 -0
  212. package/dist/cjs/js/src/pro/binanceus.js +51 -0
  213. package/dist/cjs/js/src/pro/binanceusdm.js +32 -0
  214. package/dist/cjs/js/src/pro/bingx.js +944 -0
  215. package/dist/cjs/js/src/pro/bitcoincom.js +29 -0
  216. package/dist/cjs/js/src/pro/bitfinex.js +672 -0
  217. package/dist/cjs/js/src/pro/bitfinex2.js +1159 -0
  218. package/dist/cjs/js/src/pro/bitget.js +1733 -0
  219. package/dist/cjs/js/src/pro/bitmart.js +1486 -0
  220. package/dist/cjs/js/src/pro/bitmex.js +1576 -0
  221. package/dist/cjs/js/src/pro/bitopro.js +327 -0
  222. package/dist/cjs/js/src/pro/bitpanda.js +1341 -0
  223. package/dist/cjs/js/src/pro/bitrue.js +462 -0
  224. package/dist/cjs/js/src/pro/bitstamp.js +547 -0
  225. package/dist/cjs/js/src/pro/bitvavo.js +704 -0
  226. package/dist/cjs/js/src/pro/blockchaincom.js +794 -0
  227. package/dist/cjs/js/src/pro/bybit.js +1843 -0
  228. package/dist/cjs/js/src/pro/cex.js +1510 -0
  229. package/dist/cjs/js/src/pro/coinbase.js +561 -0
  230. package/dist/cjs/js/src/pro/coinbasepro.js +968 -0
  231. package/dist/cjs/js/src/pro/coinex.js +1095 -0
  232. package/dist/cjs/js/src/pro/cryptocom.js +1020 -0
  233. package/dist/cjs/js/src/pro/currencycom.js +563 -0
  234. package/dist/cjs/js/src/pro/deribit.js +825 -0
  235. package/dist/cjs/js/src/pro/exmo.js +658 -0
  236. package/dist/cjs/js/src/pro/gate.js +1316 -0
  237. package/dist/cjs/js/src/pro/gateio.js +16 -0
  238. package/dist/cjs/js/src/pro/gemini.js +649 -0
  239. package/dist/cjs/js/src/pro/hitbtc.js +1293 -0
  240. package/dist/cjs/js/src/pro/hollaex.js +597 -0
  241. package/dist/cjs/js/src/pro/htx.js +2383 -0
  242. package/dist/cjs/js/src/pro/huobi.js +16 -0
  243. package/dist/cjs/js/src/pro/huobijp.js +606 -0
  244. package/dist/cjs/js/src/pro/idex.js +714 -0
  245. package/dist/cjs/js/src/pro/independentreserve.js +280 -0
  246. package/dist/cjs/js/src/pro/kraken.js +1364 -0
  247. package/dist/cjs/js/src/pro/krakenfutures.js +1500 -0
  248. package/dist/cjs/js/src/pro/kucoin.js +1052 -0
  249. package/dist/cjs/js/src/pro/kucoinfutures.js +981 -0
  250. package/dist/cjs/js/src/pro/luno.js +322 -0
  251. package/dist/cjs/js/src/pro/mexc.js +1170 -0
  252. package/dist/cjs/js/src/pro/ndax.js +545 -0
  253. package/dist/cjs/js/src/pro/okcoin.js +760 -0
  254. package/dist/cjs/js/src/pro/okx.js +1558 -0
  255. package/dist/cjs/js/src/pro/phemex.js +1511 -0
  256. package/dist/cjs/js/src/pro/poloniex.js +1253 -0
  257. package/dist/cjs/js/src/pro/poloniexfutures.js +1014 -0
  258. package/dist/cjs/js/src/pro/probit.js +586 -0
  259. package/dist/cjs/js/src/pro/upbit.js +234 -0
  260. package/dist/cjs/js/src/pro/wazirx.js +776 -0
  261. package/dist/cjs/js/src/pro/whitebit.js +927 -0
  262. package/dist/cjs/js/src/pro/woo.js +769 -0
  263. package/dist/cjs/js/src/probit.js +1865 -0
  264. package/dist/cjs/js/src/static_dependencies/fflake/browser.js +401 -0
  265. package/dist/cjs/js/src/static_dependencies/jsencrypt/JSEncrypt.js +195 -0
  266. package/dist/cjs/js/src/static_dependencies/jsencrypt/JSEncryptRSAKey.js +308 -0
  267. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/asn1js/asn1.js +554 -0
  268. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/asn1js/base64.js +94 -0
  269. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/asn1js/hex.js +70 -0
  270. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/asn1js/int10.js +91 -0
  271. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/base64.js +16 -0
  272. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/jsbn.js +1760 -0
  273. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/prng4.js +52 -0
  274. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/rng.js +81 -0
  275. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/rsa.js +376 -0
  276. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/util.js +70 -0
  277. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsrsasign/asn1-1.0.js +1580 -0
  278. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsrsasign/yahoo.js +74 -0
  279. package/dist/cjs/js/src/static_dependencies/noble-curves/_shortw_utils.js +24 -0
  280. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/curve.js +158 -0
  281. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/edwards.js +429 -0
  282. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/hash-to-curve.js +176 -0
  283. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/modular.js +324 -0
  284. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/montgomery.js +163 -0
  285. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/utils.js +245 -0
  286. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/weierstrass.js +1018 -0
  287. package/dist/cjs/js/src/static_dependencies/noble-curves/ed25519.js +383 -0
  288. package/dist/cjs/js/src/static_dependencies/noble-curves/secp256k1.js +258 -0
  289. package/dist/cjs/js/src/static_dependencies/noble-hashes/_assert.js +53 -0
  290. package/dist/cjs/js/src/static_dependencies/noble-hashes/_sha2.js +120 -0
  291. package/dist/cjs/js/src/static_dependencies/noble-hashes/_u64.js +69 -0
  292. package/dist/cjs/js/src/static_dependencies/noble-hashes/crypto.js +7 -0
  293. package/dist/cjs/js/src/static_dependencies/noble-hashes/hmac.js +83 -0
  294. package/dist/cjs/js/src/static_dependencies/noble-hashes/md5.js +240 -0
  295. package/dist/cjs/js/src/static_dependencies/noble-hashes/sha1.js +91 -0
  296. package/dist/cjs/js/src/static_dependencies/noble-hashes/sha256.js +130 -0
  297. package/dist/cjs/js/src/static_dependencies/noble-hashes/sha3.js +214 -0
  298. package/dist/cjs/js/src/static_dependencies/noble-hashes/sha512.js +239 -0
  299. package/dist/cjs/js/src/static_dependencies/noble-hashes/utils.js +93 -0
  300. package/dist/cjs/js/src/static_dependencies/node-fetch/body.js +354 -0
  301. package/dist/cjs/js/src/static_dependencies/node-fetch/errors/abort-error.js +16 -0
  302. package/dist/cjs/js/src/static_dependencies/node-fetch/errors/base.js +20 -0
  303. package/dist/cjs/js/src/static_dependencies/node-fetch/errors/fetch-error.js +30 -0
  304. package/dist/cjs/js/src/static_dependencies/node-fetch/headers.js +239 -0
  305. package/dist/cjs/js/src/static_dependencies/node-fetch/index.js +372 -0
  306. package/dist/cjs/js/src/static_dependencies/node-fetch/request.js +273 -0
  307. package/dist/cjs/js/src/static_dependencies/node-fetch/response.js +139 -0
  308. package/dist/cjs/js/src/static_dependencies/node-fetch/utils/get-search.js +14 -0
  309. package/dist/cjs/js/src/static_dependencies/node-fetch/utils/is-redirect.js +16 -0
  310. package/dist/cjs/js/src/static_dependencies/node-fetch/utils/is.js +81 -0
  311. package/dist/cjs/js/src/static_dependencies/node-fetch/utils/referrer.js +292 -0
  312. package/dist/cjs/js/src/static_dependencies/proxies/agent-base/index.js +103 -0
  313. package/dist/cjs/js/src/static_dependencies/proxies/http-proxy-agent/index.js +140 -0
  314. package/dist/cjs/js/src/static_dependencies/proxies/https-proxy-agent/index.js +175 -0
  315. package/dist/cjs/js/src/static_dependencies/proxies/https-proxy-agent/parse-proxy-response.js +95 -0
  316. package/dist/cjs/js/src/static_dependencies/qs/index.cjs.js +7 -0
  317. package/dist/cjs/js/src/static_dependencies/scure-base/index.js +383 -0
  318. package/dist/cjs/js/src/timex.js +1562 -0
  319. package/dist/cjs/js/src/tokocrypto.js +2542 -0
  320. package/dist/cjs/js/src/upbit.js +1844 -0
  321. package/dist/cjs/js/src/wavesexchange.js +2607 -0
  322. package/dist/cjs/js/src/wazirx.js +953 -0
  323. package/dist/cjs/js/src/whitebit.js +2309 -0
  324. package/dist/cjs/js/src/woo.js +2715 -0
  325. package/dist/cjs/js/src/yobit.js +1314 -0
  326. package/dist/cjs/js/src/zaif.js +736 -0
  327. package/dist/cjs/js/src/zonda.js +1883 -0
  328. package/js/ccxt.d.ts +1 -1
  329. package/js/ccxt.js +1 -1
  330. package/js/src/abstract/bigone.d.ts +18 -0
  331. package/js/src/abstract/binance.d.ts +2 -0
  332. package/js/src/abstract/binancecoinm.d.ts +2 -0
  333. package/js/src/abstract/binanceus.d.ts +2 -0
  334. package/js/src/abstract/binanceusdm.d.ts +2 -0
  335. package/js/src/abstract/bybit.d.ts +1 -0
  336. package/js/src/abstract/gate.d.ts +11 -0
  337. package/js/src/abstract/gateio.d.ts +11 -0
  338. package/js/src/alpaca.js +18 -18
  339. package/js/src/base/Exchange.d.ts +5 -1
  340. package/js/src/base/Exchange.js +101 -12
  341. package/js/src/bigone.d.ts +1 -2
  342. package/js/src/bigone.js +340 -145
  343. package/js/src/binance.js +15 -8
  344. package/js/src/bingx.js +9 -2
  345. package/js/src/bitfinex.d.ts +2 -2
  346. package/js/src/bitfinex.js +2 -3
  347. package/js/src/bitget.js +21 -8
  348. package/js/src/bitmart.d.ts +2 -2
  349. package/js/src/bitmart.js +3 -3
  350. package/js/src/bitmex.js +1 -0
  351. package/js/src/bybit.js +2 -0
  352. package/js/src/coinlist.js +2 -3
  353. package/js/src/coinsph.js +2 -3
  354. package/js/src/deribit.js +1 -0
  355. package/js/src/gate.d.ts +4 -4
  356. package/js/src/gate.js +22 -3
  357. package/js/src/hitbtc.d.ts +4 -4
  358. package/js/src/hitbtc.js +2 -3
  359. package/js/src/htx.js +4 -7
  360. package/js/src/huobijp.js +2 -3
  361. package/js/src/kraken.js +1 -0
  362. package/js/src/mexc.js +2 -1
  363. package/js/src/okx.js +13 -3
  364. package/js/src/pro/binance.d.ts +2 -23
  365. package/js/src/pro/binance.js +58 -22
  366. package/js/src/pro/coinbase.d.ts +2 -2
  367. package/js/src/pro/coinbase.js +4 -1
  368. package/js/src/pro/coinbasepro.d.ts +2 -2
  369. package/js/src/pro/hitbtc.d.ts +2 -2
  370. package/js/src/pro/poloniex.d.ts +2 -2
  371. package/js/src/upbit.d.ts +3 -101
  372. package/js/src/upbit.js +12 -12
  373. package/js/src/woo.js +2 -0
  374. package/package.json +1 -1
  375. package/skip-tests.json +5 -0
@@ -0,0 +1,1018 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var modular = require('./modular.js');
6
+ var utils = require('./utils.js');
7
+ var curve = require('./curve.js');
8
+
9
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
10
+ function validatePointOpts(curve$1) {
11
+ const opts = curve.validateBasic(curve$1);
12
+ utils.validateObject(opts, {
13
+ a: 'field',
14
+ b: 'field',
15
+ fromBytes: 'function',
16
+ toBytes: 'function',
17
+ }, {
18
+ allowedPrivateKeyLengths: 'array',
19
+ wrapPrivateKey: 'boolean',
20
+ isTorsionFree: 'function',
21
+ clearCofactor: 'function',
22
+ allowInfinityPoint: 'boolean',
23
+ });
24
+ const { endo, Fp, a } = opts;
25
+ if (endo) {
26
+ if (!Fp.eql(a, Fp.ZERO)) {
27
+ throw new Error('Endomorphism can only be defined for Koblitz curves that have a=0');
28
+ }
29
+ if (typeof endo !== 'object' ||
30
+ typeof endo.beta !== 'bigint' ||
31
+ typeof endo.splitScalar !== 'function') {
32
+ throw new Error('Expected endomorphism with beta: bigint and splitScalar: function');
33
+ }
34
+ }
35
+ return Object.freeze({ ...opts });
36
+ }
37
+ // ASN.1 DER encoding utilities
38
+ const { bytesToNumberBE: b2n, hexToBytes: h2b } = utils;
39
+ const DER = {
40
+ // asn.1 DER encoding utils
41
+ Err: class DERErr extends Error {
42
+ constructor(m = '') {
43
+ super(m);
44
+ }
45
+ },
46
+ _parseInt(data) {
47
+ const { Err: E } = DER;
48
+ if (data.length < 2 || data[0] !== 0x02)
49
+ throw new E('Invalid signature integer tag');
50
+ const len = data[1];
51
+ const res = data.subarray(2, len + 2);
52
+ if (!len || res.length !== len)
53
+ throw new E('Invalid signature integer: wrong length');
54
+ if (res[0] === 0x00 && res[1] <= 0x7f)
55
+ throw new E('Invalid signature integer: trailing length');
56
+ // ^ Weird condition: not about length, but about first bytes of number.
57
+ return { d: b2n(res), l: data.subarray(len + 2) }; // d is data, l is left
58
+ },
59
+ toSig(hex) {
60
+ // parse DER signature
61
+ const { Err: E } = DER;
62
+ const data = typeof hex === 'string' ? h2b(hex) : hex;
63
+ if (!(data instanceof Uint8Array))
64
+ throw new Error('ui8a expected');
65
+ let l = data.length;
66
+ if (l < 2 || data[0] != 0x30)
67
+ throw new E('Invalid signature tag');
68
+ if (data[1] !== l - 2)
69
+ throw new E('Invalid signature: incorrect length');
70
+ const { d: r, l: sBytes } = DER._parseInt(data.subarray(2));
71
+ const { d: s, l: rBytesLeft } = DER._parseInt(sBytes);
72
+ if (rBytesLeft.length)
73
+ throw new E('Invalid signature: left bytes after parsing');
74
+ return { r, s };
75
+ },
76
+ hexFromSig(sig) {
77
+ const slice = (s) => (Number.parseInt(s[0], 16) >= 8 ? '00' + s : s); // slice DER
78
+ const h = (num) => {
79
+ const hex = num.toString(16);
80
+ return hex.length & 1 ? `0${hex}` : hex;
81
+ };
82
+ const s = slice(h(sig.s));
83
+ const r = slice(h(sig.r));
84
+ const shl = s.length / 2;
85
+ const rhl = r.length / 2;
86
+ const sl = h(shl);
87
+ const rl = h(rhl);
88
+ return `30${h(rhl + shl + 4)}02${rl}${r}02${sl}${s}`;
89
+ },
90
+ };
91
+ // Be friendly to bad ECMAScript parsers by not using bigint literals like 123n
92
+ const _0n = BigInt(0);
93
+ const _1n = BigInt(1);
94
+ function weierstrassPoints(opts) {
95
+ const CURVE = validatePointOpts(opts);
96
+ const { Fp } = CURVE; // All curves has same field / group length as for now, but they can differ
97
+ /**
98
+ * y² = x³ + ax + b: Short weierstrass curve formula
99
+ * @returns y²
100
+ */
101
+ function weierstrassEquation(x) {
102
+ const { a, b } = CURVE;
103
+ const x2 = Fp.sqr(x); // x * x
104
+ const x3 = Fp.mul(x2, x); // x2 * x
105
+ return Fp.add(Fp.add(x3, Fp.mul(x, a)), b); // x3 + a * x + b
106
+ }
107
+ // Valid group elements reside in range 1..n-1
108
+ function isWithinCurveOrder(num) {
109
+ return typeof num === 'bigint' && _0n < num && num < CURVE.n;
110
+ }
111
+ function assertGE(num) {
112
+ if (!isWithinCurveOrder(num))
113
+ throw new Error('Expected valid bigint: 0 < bigint < curve.n');
114
+ }
115
+ // Validates if priv key is valid and converts it to bigint.
116
+ // Supports options allowedPrivateKeyLengths and wrapPrivateKey.
117
+ function normPrivateKeyToScalar(key) {
118
+ const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n } = CURVE;
119
+ if (lengths && typeof key !== 'bigint') {
120
+ if (key instanceof Uint8Array)
121
+ key = utils.bytesToHex(key);
122
+ // Normalize to hex string, pad. E.g. P521 would norm 130-132 char hex to 132-char bytes
123
+ if (typeof key !== 'string' || !lengths.includes(key.length))
124
+ throw new Error('Invalid key');
125
+ key = key.padStart(nByteLength * 2, '0');
126
+ }
127
+ let num;
128
+ try {
129
+ num =
130
+ typeof key === 'bigint'
131
+ ? key
132
+ : utils.bytesToNumberBE(utils.ensureBytes('private key', key, nByteLength));
133
+ }
134
+ catch (error) {
135
+ throw new Error(`private key must be ${nByteLength} bytes, hex or bigint, not ${typeof key}`);
136
+ }
137
+ if (wrapPrivateKey)
138
+ num = modular.mod(num, n); // disabled by default, enabled for BLS
139
+ assertGE(num); // num in range [1..N-1]
140
+ return num;
141
+ }
142
+ const pointPrecomputes = new Map();
143
+ function assertPrjPoint(other) {
144
+ if (!(other instanceof Point))
145
+ throw new Error('ProjectivePoint expected');
146
+ }
147
+ /**
148
+ * Projective Point works in 3d / projective (homogeneous) coordinates: (x, y, z) ∋ (x=x/z, y=y/z)
149
+ * Default Point works in 2d / affine coordinates: (x, y)
150
+ * We're doing calculations in projective, because its operations don't require costly inversion.
151
+ */
152
+ class Point {
153
+ constructor(px, py, pz) {
154
+ this.px = px;
155
+ this.py = py;
156
+ this.pz = pz;
157
+ if (px == null || !Fp.isValid(px))
158
+ throw new Error('x required');
159
+ if (py == null || !Fp.isValid(py))
160
+ throw new Error('y required');
161
+ if (pz == null || !Fp.isValid(pz))
162
+ throw new Error('z required');
163
+ }
164
+ // Does not validate if the point is on-curve.
165
+ // Use fromHex instead, or call assertValidity() later.
166
+ static fromAffine(p) {
167
+ const { x, y } = p || {};
168
+ if (!p || !Fp.isValid(x) || !Fp.isValid(y))
169
+ throw new Error('invalid affine point');
170
+ if (p instanceof Point)
171
+ throw new Error('projective point not allowed');
172
+ const is0 = (i) => Fp.eql(i, Fp.ZERO);
173
+ // fromAffine(x:0, y:0) would produce (x:0, y:0, z:1), but we need (x:0, y:1, z:0)
174
+ if (is0(x) && is0(y))
175
+ return Point.ZERO;
176
+ return new Point(x, y, Fp.ONE);
177
+ }
178
+ get x() {
179
+ return this.toAffine().x;
180
+ }
181
+ get y() {
182
+ return this.toAffine().y;
183
+ }
184
+ /**
185
+ * Takes a bunch of Projective Points but executes only one
186
+ * inversion on all of them. Inversion is very slow operation,
187
+ * so this improves performance massively.
188
+ * Optimization: converts a list of projective points to a list of identical points with Z=1.
189
+ */
190
+ static normalizeZ(points) {
191
+ const toInv = Fp.invertBatch(points.map((p) => p.pz));
192
+ return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
193
+ }
194
+ /**
195
+ * Converts hash string or Uint8Array to Point.
196
+ * @param hex short/long ECDSA hex
197
+ */
198
+ static fromHex(hex) {
199
+ const P = Point.fromAffine(CURVE.fromBytes(utils.ensureBytes('pointHex', hex)));
200
+ P.assertValidity();
201
+ return P;
202
+ }
203
+ // Multiplies generator point by privateKey.
204
+ static fromPrivateKey(privateKey) {
205
+ return Point.BASE.multiply(normPrivateKeyToScalar(privateKey));
206
+ }
207
+ // "Private method", don't use it directly
208
+ _setWindowSize(windowSize) {
209
+ this._WINDOW_SIZE = windowSize;
210
+ pointPrecomputes.delete(this);
211
+ }
212
+ // A point on curve is valid if it conforms to equation.
213
+ assertValidity() {
214
+ // Zero is valid point too!
215
+ if (this.is0()) {
216
+ if (CURVE.allowInfinityPoint)
217
+ return;
218
+ throw new Error('bad point: ZERO');
219
+ }
220
+ // Some 3rd-party test vectors require different wording between here & `fromCompressedHex`
221
+ const { x, y } = this.toAffine();
222
+ // Check if x, y are valid field elements
223
+ if (!Fp.isValid(x) || !Fp.isValid(y))
224
+ throw new Error('bad point: x or y not FE');
225
+ const left = Fp.sqr(y); // y²
226
+ const right = weierstrassEquation(x); // x³ + ax + b
227
+ if (!Fp.eql(left, right))
228
+ throw new Error('bad point: equation left != right');
229
+ if (!this.isTorsionFree())
230
+ throw new Error('bad point: not in prime-order subgroup');
231
+ }
232
+ hasEvenY() {
233
+ const { y } = this.toAffine();
234
+ if (Fp.isOdd)
235
+ return !Fp.isOdd(y);
236
+ throw new Error("Field doesn't support isOdd");
237
+ }
238
+ /**
239
+ * Compare one point to another.
240
+ */
241
+ equals(other) {
242
+ assertPrjPoint(other);
243
+ const { px: X1, py: Y1, pz: Z1 } = this;
244
+ const { px: X2, py: Y2, pz: Z2 } = other;
245
+ const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
246
+ const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
247
+ return U1 && U2;
248
+ }
249
+ /**
250
+ * Flips point to one corresponding to (x, -y) in Affine coordinates.
251
+ */
252
+ negate() {
253
+ return new Point(this.px, Fp.neg(this.py), this.pz);
254
+ }
255
+ // Renes-Costello-Batina exception-free doubling formula.
256
+ // There is 30% faster Jacobian formula, but it is not complete.
257
+ // https://eprint.iacr.org/2015/1060, algorithm 3
258
+ // Cost: 8M + 3S + 3*a + 2*b3 + 15add.
259
+ double() {
260
+ const { a, b } = CURVE;
261
+ const b3 = Fp.mul(b, 3n);
262
+ const { px: X1, py: Y1, pz: Z1 } = this;
263
+ let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO; // prettier-ignore
264
+ let t0 = Fp.mul(X1, X1); // step 1
265
+ let t1 = Fp.mul(Y1, Y1);
266
+ let t2 = Fp.mul(Z1, Z1);
267
+ let t3 = Fp.mul(X1, Y1);
268
+ t3 = Fp.add(t3, t3); // step 5
269
+ Z3 = Fp.mul(X1, Z1);
270
+ Z3 = Fp.add(Z3, Z3);
271
+ X3 = Fp.mul(a, Z3);
272
+ Y3 = Fp.mul(b3, t2);
273
+ Y3 = Fp.add(X3, Y3); // step 10
274
+ X3 = Fp.sub(t1, Y3);
275
+ Y3 = Fp.add(t1, Y3);
276
+ Y3 = Fp.mul(X3, Y3);
277
+ X3 = Fp.mul(t3, X3);
278
+ Z3 = Fp.mul(b3, Z3); // step 15
279
+ t2 = Fp.mul(a, t2);
280
+ t3 = Fp.sub(t0, t2);
281
+ t3 = Fp.mul(a, t3);
282
+ t3 = Fp.add(t3, Z3);
283
+ Z3 = Fp.add(t0, t0); // step 20
284
+ t0 = Fp.add(Z3, t0);
285
+ t0 = Fp.add(t0, t2);
286
+ t0 = Fp.mul(t0, t3);
287
+ Y3 = Fp.add(Y3, t0);
288
+ t2 = Fp.mul(Y1, Z1); // step 25
289
+ t2 = Fp.add(t2, t2);
290
+ t0 = Fp.mul(t2, t3);
291
+ X3 = Fp.sub(X3, t0);
292
+ Z3 = Fp.mul(t2, t1);
293
+ Z3 = Fp.add(Z3, Z3); // step 30
294
+ Z3 = Fp.add(Z3, Z3);
295
+ return new Point(X3, Y3, Z3);
296
+ }
297
+ // Renes-Costello-Batina exception-free addition formula.
298
+ // There is 30% faster Jacobian formula, but it is not complete.
299
+ // https://eprint.iacr.org/2015/1060, algorithm 1
300
+ // Cost: 12M + 0S + 3*a + 3*b3 + 23add.
301
+ add(other) {
302
+ assertPrjPoint(other);
303
+ const { px: X1, py: Y1, pz: Z1 } = this;
304
+ const { px: X2, py: Y2, pz: Z2 } = other;
305
+ let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO; // prettier-ignore
306
+ const a = CURVE.a;
307
+ const b3 = Fp.mul(CURVE.b, 3n);
308
+ let t0 = Fp.mul(X1, X2); // step 1
309
+ let t1 = Fp.mul(Y1, Y2);
310
+ let t2 = Fp.mul(Z1, Z2);
311
+ let t3 = Fp.add(X1, Y1);
312
+ let t4 = Fp.add(X2, Y2); // step 5
313
+ t3 = Fp.mul(t3, t4);
314
+ t4 = Fp.add(t0, t1);
315
+ t3 = Fp.sub(t3, t4);
316
+ t4 = Fp.add(X1, Z1);
317
+ let t5 = Fp.add(X2, Z2); // step 10
318
+ t4 = Fp.mul(t4, t5);
319
+ t5 = Fp.add(t0, t2);
320
+ t4 = Fp.sub(t4, t5);
321
+ t5 = Fp.add(Y1, Z1);
322
+ X3 = Fp.add(Y2, Z2); // step 15
323
+ t5 = Fp.mul(t5, X3);
324
+ X3 = Fp.add(t1, t2);
325
+ t5 = Fp.sub(t5, X3);
326
+ Z3 = Fp.mul(a, t4);
327
+ X3 = Fp.mul(b3, t2); // step 20
328
+ Z3 = Fp.add(X3, Z3);
329
+ X3 = Fp.sub(t1, Z3);
330
+ Z3 = Fp.add(t1, Z3);
331
+ Y3 = Fp.mul(X3, Z3);
332
+ t1 = Fp.add(t0, t0); // step 25
333
+ t1 = Fp.add(t1, t0);
334
+ t2 = Fp.mul(a, t2);
335
+ t4 = Fp.mul(b3, t4);
336
+ t1 = Fp.add(t1, t2);
337
+ t2 = Fp.sub(t0, t2); // step 30
338
+ t2 = Fp.mul(a, t2);
339
+ t4 = Fp.add(t4, t2);
340
+ t0 = Fp.mul(t1, t4);
341
+ Y3 = Fp.add(Y3, t0);
342
+ t0 = Fp.mul(t5, t4); // step 35
343
+ X3 = Fp.mul(t3, X3);
344
+ X3 = Fp.sub(X3, t0);
345
+ t0 = Fp.mul(t3, t1);
346
+ Z3 = Fp.mul(t5, Z3);
347
+ Z3 = Fp.add(Z3, t0); // step 40
348
+ return new Point(X3, Y3, Z3);
349
+ }
350
+ subtract(other) {
351
+ return this.add(other.negate());
352
+ }
353
+ is0() {
354
+ return this.equals(Point.ZERO);
355
+ }
356
+ wNAF(n) {
357
+ return wnaf.wNAFCached(this, pointPrecomputes, n, (comp) => {
358
+ const toInv = Fp.invertBatch(comp.map((p) => p.pz));
359
+ return comp.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
360
+ });
361
+ }
362
+ /**
363
+ * Non-constant-time multiplication. Uses double-and-add algorithm.
364
+ * It's faster, but should only be used when you don't care about
365
+ * an exposed private key e.g. sig verification, which works over *public* keys.
366
+ */
367
+ multiplyUnsafe(n) {
368
+ const I = Point.ZERO;
369
+ if (n === _0n)
370
+ return I;
371
+ assertGE(n); // Will throw on 0
372
+ if (n === _1n)
373
+ return this;
374
+ const { endo } = CURVE;
375
+ if (!endo)
376
+ return wnaf.unsafeLadder(this, n);
377
+ // Apply endomorphism
378
+ let { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
379
+ let k1p = I;
380
+ let k2p = I;
381
+ let d = this;
382
+ while (k1 > _0n || k2 > _0n) {
383
+ if (k1 & _1n)
384
+ k1p = k1p.add(d);
385
+ if (k2 & _1n)
386
+ k2p = k2p.add(d);
387
+ d = d.double();
388
+ k1 >>= _1n;
389
+ k2 >>= _1n;
390
+ }
391
+ if (k1neg)
392
+ k1p = k1p.negate();
393
+ if (k2neg)
394
+ k2p = k2p.negate();
395
+ k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
396
+ return k1p.add(k2p);
397
+ }
398
+ /**
399
+ * Constant time multiplication.
400
+ * Uses wNAF method. Windowed method may be 10% faster,
401
+ * but takes 2x longer to generate and consumes 2x memory.
402
+ * Uses precomputes when available.
403
+ * Uses endomorphism for Koblitz curves.
404
+ * @param scalar by which the point would be multiplied
405
+ * @returns New point
406
+ */
407
+ multiply(scalar) {
408
+ assertGE(scalar);
409
+ let n = scalar;
410
+ let point, fake; // Fake point is used to const-time mult
411
+ const { endo } = CURVE;
412
+ if (endo) {
413
+ const { k1neg, k1, k2neg, k2 } = endo.splitScalar(n);
414
+ let { p: k1p, f: f1p } = this.wNAF(k1);
415
+ let { p: k2p, f: f2p } = this.wNAF(k2);
416
+ k1p = wnaf.constTimeNegate(k1neg, k1p);
417
+ k2p = wnaf.constTimeNegate(k2neg, k2p);
418
+ k2p = new Point(Fp.mul(k2p.px, endo.beta), k2p.py, k2p.pz);
419
+ point = k1p.add(k2p);
420
+ fake = f1p.add(f2p);
421
+ }
422
+ else {
423
+ const { p, f } = this.wNAF(n);
424
+ point = p;
425
+ fake = f;
426
+ }
427
+ // Normalize `z` for both points, but return only real one
428
+ return Point.normalizeZ([point, fake])[0];
429
+ }
430
+ /**
431
+ * Efficiently calculate `aP + bQ`. Unsafe, can expose private key, if used incorrectly.
432
+ * Not using Strauss-Shamir trick: precomputation tables are faster.
433
+ * The trick could be useful if both P and Q are not G (not in our case).
434
+ * @returns non-zero affine point
435
+ */
436
+ multiplyAndAddUnsafe(Q, a, b) {
437
+ const G = Point.BASE; // No Strauss-Shamir trick: we have 10% faster G precomputes
438
+ const mul = (P, a // Select faster multiply() method
439
+ ) => (a === _0n || a === _1n || !P.equals(G) ? P.multiplyUnsafe(a) : P.multiply(a));
440
+ const sum = mul(this, a).add(mul(Q, b));
441
+ return sum.is0() ? undefined : sum;
442
+ }
443
+ // Converts Projective point to affine (x, y) coordinates.
444
+ // Can accept precomputed Z^-1 - for example, from invertBatch.
445
+ // (x, y, z) ∋ (x=x/z, y=y/z)
446
+ toAffine(iz) {
447
+ const { px: x, py: y, pz: z } = this;
448
+ const is0 = this.is0();
449
+ // If invZ was 0, we return zero point. However we still want to execute
450
+ // all operations, so we replace invZ with a random number, 1.
451
+ if (iz == null)
452
+ iz = is0 ? Fp.ONE : Fp.inv(z);
453
+ const ax = Fp.mul(x, iz);
454
+ const ay = Fp.mul(y, iz);
455
+ const zz = Fp.mul(z, iz);
456
+ if (is0)
457
+ return { x: Fp.ZERO, y: Fp.ZERO };
458
+ if (!Fp.eql(zz, Fp.ONE))
459
+ throw new Error('invZ was invalid');
460
+ return { x: ax, y: ay };
461
+ }
462
+ isTorsionFree() {
463
+ const { h: cofactor, isTorsionFree } = CURVE;
464
+ if (cofactor === _1n)
465
+ return true; // No subgroups, always torsion-free
466
+ if (isTorsionFree)
467
+ return isTorsionFree(Point, this);
468
+ throw new Error('isTorsionFree() has not been declared for the elliptic curve');
469
+ }
470
+ clearCofactor() {
471
+ const { h: cofactor, clearCofactor } = CURVE;
472
+ if (cofactor === _1n)
473
+ return this; // Fast-path
474
+ if (clearCofactor)
475
+ return clearCofactor(Point, this);
476
+ return this.multiplyUnsafe(CURVE.h);
477
+ }
478
+ toRawBytes(isCompressed = true) {
479
+ this.assertValidity();
480
+ return CURVE.toBytes(Point, this, isCompressed);
481
+ }
482
+ toHex(isCompressed = true) {
483
+ return utils.bytesToHex(this.toRawBytes(isCompressed));
484
+ }
485
+ }
486
+ Point.BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
487
+ Point.ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
488
+ const _bits = CURVE.nBitLength;
489
+ const wnaf = curve.wNAF(Point, CURVE.endo ? Math.ceil(_bits / 2) : _bits);
490
+ return {
491
+ ProjectivePoint: Point,
492
+ normPrivateKeyToScalar,
493
+ weierstrassEquation,
494
+ isWithinCurveOrder,
495
+ };
496
+ }
497
+ function validateOpts(curve$1) {
498
+ const opts = curve.validateBasic(curve$1);
499
+ utils.validateObject(opts, {
500
+ hash: 'hash',
501
+ hmac: 'function',
502
+ randomBytes: 'function',
503
+ }, {
504
+ bits2int: 'function',
505
+ bits2int_modN: 'function',
506
+ lowS: 'boolean',
507
+ });
508
+ return Object.freeze({ lowS: true, ...opts });
509
+ }
510
+ function weierstrass(curveDef) {
511
+ const CURVE = validateOpts(curveDef);
512
+ const CURVE_ORDER = CURVE.n;
513
+ const Fp = CURVE.Fp;
514
+ const compressedLen = Fp.BYTES + 1; // e.g. 33 for 32
515
+ const uncompressedLen = 2 * Fp.BYTES + 1; // e.g. 65 for 32
516
+ function isValidFieldElement(num) {
517
+ return _0n < num && num < Fp.ORDER; // 0 is banned since it's not invertible FE
518
+ }
519
+ function modN(a) {
520
+ return modular.mod(a, CURVE_ORDER);
521
+ }
522
+ function invN(a) {
523
+ return modular.invert(a, CURVE_ORDER);
524
+ }
525
+ const { ProjectivePoint: Point, normPrivateKeyToScalar, weierstrassEquation, isWithinCurveOrder, } = weierstrassPoints({
526
+ ...CURVE,
527
+ toBytes(c, point, isCompressed) {
528
+ const a = point.toAffine();
529
+ const x = Fp.toBytes(a.x);
530
+ const cat = utils.concatBytes;
531
+ if (isCompressed) {
532
+ return cat(Uint8Array.from([point.hasEvenY() ? 0x02 : 0x03]), x);
533
+ }
534
+ else {
535
+ return cat(Uint8Array.from([0x04]), x, Fp.toBytes(a.y));
536
+ }
537
+ },
538
+ fromBytes(bytes) {
539
+ const len = bytes.length;
540
+ const head = bytes[0];
541
+ const tail = bytes.subarray(1);
542
+ // this.assertValidity() is done inside of fromHex
543
+ if (len === compressedLen && (head === 0x02 || head === 0x03)) {
544
+ const x = utils.bytesToNumberBE(tail);
545
+ if (!isValidFieldElement(x))
546
+ throw new Error('Point is not on curve');
547
+ const y2 = weierstrassEquation(x); // y² = x³ + ax + b
548
+ let y = Fp.sqrt(y2); // y = y² ^ (p+1)/4
549
+ const isYOdd = (y & _1n) === _1n;
550
+ // ECDSA
551
+ const isHeadOdd = (head & 1) === 1;
552
+ if (isHeadOdd !== isYOdd)
553
+ y = Fp.neg(y);
554
+ return { x, y };
555
+ }
556
+ else if (len === uncompressedLen && head === 0x04) {
557
+ const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
558
+ const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
559
+ return { x, y };
560
+ }
561
+ else {
562
+ throw new Error(`Point of length ${len} was invalid. Expected ${compressedLen} compressed bytes or ${uncompressedLen} uncompressed bytes`);
563
+ }
564
+ },
565
+ });
566
+ const numToNByteStr = (num) => utils.bytesToHex(utils.numberToBytesBE(num, CURVE.nByteLength));
567
+ function isBiggerThanHalfOrder(number) {
568
+ const HALF = CURVE_ORDER >> _1n;
569
+ return number > HALF;
570
+ }
571
+ function normalizeS(s) {
572
+ return isBiggerThanHalfOrder(s) ? modN(-s) : s;
573
+ }
574
+ // slice bytes num
575
+ const slcNum = (b, from, to) => utils.bytesToNumberBE(b.slice(from, to));
576
+ /**
577
+ * ECDSA signature with its (r, s) properties. Supports DER & compact representations.
578
+ */
579
+ class Signature {
580
+ constructor(r, s, recovery) {
581
+ this.r = r;
582
+ this.s = s;
583
+ this.recovery = recovery;
584
+ this.assertValidity();
585
+ }
586
+ // pair (bytes of r, bytes of s)
587
+ static fromCompact(hex) {
588
+ const l = CURVE.nByteLength;
589
+ hex = utils.ensureBytes('compactSignature', hex, l * 2);
590
+ return new Signature(slcNum(hex, 0, l), slcNum(hex, l, 2 * l));
591
+ }
592
+ // DER encoded ECDSA signature
593
+ // https://bitcoin.stackexchange.com/questions/57644/what-are-the-parts-of-a-bitcoin-transaction-input-script
594
+ static fromDER(hex) {
595
+ const { r, s } = DER.toSig(utils.ensureBytes('DER', hex));
596
+ return new Signature(r, s);
597
+ }
598
+ assertValidity() {
599
+ // can use assertGE here
600
+ if (!isWithinCurveOrder(this.r))
601
+ throw new Error('r must be 0 < r < CURVE.n');
602
+ if (!isWithinCurveOrder(this.s))
603
+ throw new Error('s must be 0 < s < CURVE.n');
604
+ }
605
+ addRecoveryBit(recovery) {
606
+ return new Signature(this.r, this.s, recovery);
607
+ }
608
+ recoverPublicKey(msgHash) {
609
+ const { r, s, recovery: rec } = this;
610
+ const h = bits2int_modN(utils.ensureBytes('msgHash', msgHash)); // Truncate hash
611
+ if (rec == null || ![0, 1, 2, 3].includes(rec))
612
+ throw new Error('recovery id invalid');
613
+ const radj = rec === 2 || rec === 3 ? r + CURVE.n : r;
614
+ if (radj >= Fp.ORDER)
615
+ throw new Error('recovery id 2 or 3 invalid');
616
+ const prefix = (rec & 1) === 0 ? '02' : '03';
617
+ const R = Point.fromHex(prefix + numToNByteStr(radj));
618
+ const ir = invN(radj); // r^-1
619
+ const u1 = modN(-h * ir); // -hr^-1
620
+ const u2 = modN(s * ir); // sr^-1
621
+ const Q = Point.BASE.multiplyAndAddUnsafe(R, u1, u2); // (sr^-1)R-(hr^-1)G = -(hr^-1)G + (sr^-1)
622
+ if (!Q)
623
+ throw new Error('point at infinify'); // unsafe is fine: no priv data leaked
624
+ Q.assertValidity();
625
+ return Q;
626
+ }
627
+ // Signatures should be low-s, to prevent malleability.
628
+ hasHighS() {
629
+ return isBiggerThanHalfOrder(this.s);
630
+ }
631
+ normalizeS() {
632
+ return this.hasHighS() ? new Signature(this.r, modN(-this.s), this.recovery) : this;
633
+ }
634
+ // DER-encoded
635
+ toDERRawBytes() {
636
+ return utils.hexToBytes(this.toDERHex());
637
+ }
638
+ toDERHex() {
639
+ return DER.hexFromSig({ r: this.r, s: this.s });
640
+ }
641
+ // padded bytes of r, then padded bytes of s
642
+ toCompactRawBytes() {
643
+ return utils.hexToBytes(this.toCompactHex());
644
+ }
645
+ toCompactHex() {
646
+ return numToNByteStr(this.r) + numToNByteStr(this.s);
647
+ }
648
+ }
649
+ const utils$1 = {
650
+ isValidPrivateKey(privateKey) {
651
+ try {
652
+ normPrivateKeyToScalar(privateKey);
653
+ return true;
654
+ }
655
+ catch (error) {
656
+ return false;
657
+ }
658
+ },
659
+ normPrivateKeyToScalar: normPrivateKeyToScalar,
660
+ /**
661
+ * Produces cryptographically secure private key from random of size (nBitLength+64)
662
+ * as per FIPS 186 B.4.1 with modulo bias being neglible.
663
+ */
664
+ randomPrivateKey: () => {
665
+ const rand = CURVE.randomBytes(Fp.BYTES + 8);
666
+ const num = modular.hashToPrivateScalar(rand, CURVE_ORDER);
667
+ return utils.numberToBytesBE(num, CURVE.nByteLength);
668
+ },
669
+ /**
670
+ * Creates precompute table for an arbitrary EC point. Makes point "cached".
671
+ * Allows to massively speed-up `point.multiply(scalar)`.
672
+ * @returns cached point
673
+ * @example
674
+ * const fast = utils.precompute(8, ProjectivePoint.fromHex(someonesPubKey));
675
+ * fast.multiply(privKey); // much faster ECDH now
676
+ */
677
+ precompute(windowSize = 8, point = Point.BASE) {
678
+ point._setWindowSize(windowSize);
679
+ point.multiply(BigInt(3)); // 3 is arbitrary, just need any number here
680
+ return point;
681
+ },
682
+ };
683
+ /**
684
+ * Computes public key for a private key. Checks for validity of the private key.
685
+ * @param privateKey private key
686
+ * @param isCompressed whether to return compact (default), or full key
687
+ * @returns Public key, full when isCompressed=false; short when isCompressed=true
688
+ */
689
+ function getPublicKey(privateKey, isCompressed = true) {
690
+ return Point.fromPrivateKey(privateKey).toRawBytes(isCompressed);
691
+ }
692
+ /**
693
+ * Quick and dirty check for item being public key. Does not validate hex, or being on-curve.
694
+ */
695
+ function isProbPub(item) {
696
+ const arr = item instanceof Uint8Array;
697
+ const str = typeof item === 'string';
698
+ const len = (arr || str) && item.length;
699
+ if (arr)
700
+ return len === compressedLen || len === uncompressedLen;
701
+ if (str)
702
+ return len === 2 * compressedLen || len === 2 * uncompressedLen;
703
+ if (item instanceof Point)
704
+ return true;
705
+ return false;
706
+ }
707
+ /**
708
+ * ECDH (Elliptic Curve Diffie Hellman).
709
+ * Computes shared public key from private key and public key.
710
+ * Checks: 1) private key validity 2) shared key is on-curve.
711
+ * Does NOT hash the result.
712
+ * @param privateA private key
713
+ * @param publicB different public key
714
+ * @param isCompressed whether to return compact (default), or full key
715
+ * @returns shared public key
716
+ */
717
+ function getSharedSecret(privateA, publicB, isCompressed = true) {
718
+ if (isProbPub(privateA))
719
+ throw new Error('first arg must be private key');
720
+ if (!isProbPub(publicB))
721
+ throw new Error('second arg must be public key');
722
+ const b = Point.fromHex(publicB); // check for being on-curve
723
+ return b.multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
724
+ }
725
+ // RFC6979: ensure ECDSA msg is X bytes and < N. RFC suggests optional truncating via bits2octets.
726
+ // FIPS 186-4 4.6 suggests the leftmost min(nBitLen, outLen) bits, which matches bits2int.
727
+ // bits2int can produce res>N, we can do mod(res, N) since the bitLen is the same.
728
+ // int2octets can't be used; pads small msgs with 0: unacceptatble for trunc as per RFC vectors
729
+ const bits2int = CURVE.bits2int ||
730
+ function (bytes) {
731
+ // For curves with nBitLength % 8 !== 0: bits2octets(bits2octets(m)) !== bits2octets(m)
732
+ // for some cases, since bytes.length * 8 is not actual bitLength.
733
+ const num = utils.bytesToNumberBE(bytes); // check for == u8 done here
734
+ const delta = bytes.length * 8 - CURVE.nBitLength; // truncate to nBitLength leftmost bits
735
+ return delta > 0 ? num >> BigInt(delta) : num;
736
+ };
737
+ const bits2int_modN = CURVE.bits2int_modN ||
738
+ function (bytes) {
739
+ return modN(bits2int(bytes)); // can't use bytesToNumberBE here
740
+ };
741
+ // NOTE: pads output with zero as per spec
742
+ const ORDER_MASK = utils.bitMask(CURVE.nBitLength);
743
+ /**
744
+ * Converts to bytes. Checks if num in `[0..ORDER_MASK-1]` e.g.: `[0..2^256-1]`.
745
+ */
746
+ function int2octets(num) {
747
+ if (typeof num !== 'bigint')
748
+ throw new Error('bigint expected');
749
+ if (!(_0n <= num && num < ORDER_MASK))
750
+ throw new Error(`bigint expected < 2^${CURVE.nBitLength}`);
751
+ // works with order, can have different size than numToField!
752
+ return utils.numberToBytesBE(num, CURVE.nByteLength);
753
+ }
754
+ // Steps A, D of RFC6979 3.2
755
+ // Creates RFC6979 seed; converts msg/privKey to numbers.
756
+ // Used only in sign, not in verify.
757
+ // NOTE: we cannot assume here that msgHash has same amount of bytes as curve order, this will be wrong at least for P521.
758
+ // Also it can be bigger for P224 + SHA256
759
+ function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
760
+ if (['recovered', 'canonical'].some((k) => k in opts))
761
+ throw new Error('sign() legacy options not supported');
762
+ const { hash, randomBytes } = CURVE;
763
+ let { lowS, prehash, extraEntropy: ent } = opts; // generates low-s sigs by default
764
+ if (lowS == null)
765
+ lowS = true; // RFC6979 3.2: we skip step A, because we already provide hash
766
+ msgHash = utils.ensureBytes('msgHash', msgHash);
767
+ if (prehash)
768
+ msgHash = utils.ensureBytes('prehashed msgHash', hash(msgHash));
769
+ // We can't later call bits2octets, since nested bits2int is broken for curves
770
+ // with nBitLength % 8 !== 0. Because of that, we unwrap it here as int2octets call.
771
+ // const bits2octets = (bits) => int2octets(bits2int_modN(bits))
772
+ const h1int = bits2int_modN(msgHash);
773
+ const d = normPrivateKeyToScalar(privateKey); // validate private key, convert to bigint
774
+ const seedArgs = [int2octets(d), int2octets(h1int)];
775
+ // extraEntropy. RFC6979 3.6: additional k' (optional).
776
+ if (ent != null) {
777
+ // K = HMAC_K(V || 0x00 || int2octets(x) || bits2octets(h1) || k')
778
+ const e = ent === true ? randomBytes(Fp.BYTES) : ent; // generate random bytes OR pass as-is
779
+ seedArgs.push(utils.ensureBytes('extraEntropy', e, Fp.BYTES)); // check for being of size BYTES
780
+ }
781
+ const seed = utils.concatBytes(...seedArgs); // Step D of RFC6979 3.2
782
+ const m = h1int; // NOTE: no need to call bits2int second time here, it is inside truncateHash!
783
+ // Converts signature params into point w r/s, checks result for validity.
784
+ function k2sig(kBytes) {
785
+ // RFC 6979 Section 3.2, step 3: k = bits2int(T)
786
+ const k = bits2int(kBytes); // Cannot use fields methods, since it is group element
787
+ if (!isWithinCurveOrder(k))
788
+ return; // Important: all mod() calls here must be done over N
789
+ const ik = invN(k); // k^-1 mod n
790
+ const q = Point.BASE.multiply(k).toAffine(); // q = Gk
791
+ const r = modN(q.x); // r = q.x mod n
792
+ if (r === _0n)
793
+ return;
794
+ // Can use scalar blinding b^-1(bm + bdr) where b ∈ [1,q−1] according to
795
+ // https://tches.iacr.org/index.php/TCHES/article/view/7337/6509. We've decided against it:
796
+ // a) dependency on CSPRNG b) 15% slowdown c) doesn't really help since bigints are not CT
797
+ const s = modN(ik * modN(m + r * d)); // Not using blinding here
798
+ if (s === _0n)
799
+ return;
800
+ let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n); // recovery bit (2 or 3, when q.x > n)
801
+ let normS = s;
802
+ if (lowS && isBiggerThanHalfOrder(s)) {
803
+ normS = normalizeS(s); // if lowS was passed, ensure s is always
804
+ recovery ^= 1; // // in the bottom half of N
805
+ }
806
+ return new Signature(r, normS, recovery); // use normS, not s
807
+ }
808
+ return { seed, k2sig };
809
+ }
810
+ const defaultSigOpts = { lowS: CURVE.lowS, prehash: false };
811
+ const defaultVerOpts = { lowS: CURVE.lowS, prehash: false };
812
+ /**
813
+ * Signs message hash (not message: you need to hash it by yourself).
814
+ * ```
815
+ * sign(m, d, k) where
816
+ * (x, y) = G × k
817
+ * r = x mod n
818
+ * s = (m + dr)/k mod n
819
+ * ```
820
+ * @param opts `lowS, extraEntropy, prehash`
821
+ */
822
+ function sign(msgHash, privKey, opts = defaultSigOpts) {
823
+ const { seed, k2sig } = prepSig(msgHash, privKey, opts); // Steps A, D of RFC6979 3.2.
824
+ const drbg = utils.createHmacDrbg(CURVE.hash.outputLen, CURVE.nByteLength, CURVE.hmac);
825
+ return drbg(seed, k2sig); // Steps B, C, D, E, F, G
826
+ }
827
+ // Enable precomputes. Slows down first publicKey computation by 20ms.
828
+ Point.BASE._setWindowSize(8);
829
+ // utils.precompute(8, ProjectivePoint.BASE)
830
+ /**
831
+ * Verifies a signature against message hash and public key.
832
+ * Rejects lowS signatures by default: to override,
833
+ * specify option `{lowS: false}`. Implements section 4.1.4 from https://www.secg.org/sec1-v2.pdf:
834
+ *
835
+ * ```
836
+ * verify(r, s, h, P) where
837
+ * U1 = hs^-1 mod n
838
+ * U2 = rs^-1 mod n
839
+ * R = U1⋅G - U2⋅P
840
+ * mod(R.x, n) == r
841
+ * ```
842
+ */
843
+ function verify(signature, msgHash, publicKey, opts = defaultVerOpts) {
844
+ const sg = signature;
845
+ msgHash = utils.ensureBytes('msgHash', msgHash);
846
+ publicKey = utils.ensureBytes('publicKey', publicKey);
847
+ if ('strict' in opts)
848
+ throw new Error('options.strict was renamed to lowS');
849
+ const { lowS, prehash } = opts;
850
+ let _sig = undefined;
851
+ let P;
852
+ try {
853
+ if (typeof sg === 'string' || sg instanceof Uint8Array) {
854
+ // Signature can be represented in 2 ways: compact (2*nByteLength) & DER (variable-length).
855
+ // Since DER can also be 2*nByteLength bytes, we check for it first.
856
+ try {
857
+ _sig = Signature.fromDER(sg);
858
+ }
859
+ catch (derError) {
860
+ if (!(derError instanceof DER.Err))
861
+ throw derError;
862
+ _sig = Signature.fromCompact(sg);
863
+ }
864
+ }
865
+ else if (typeof sg === 'object' && typeof sg.r === 'bigint' && typeof sg.s === 'bigint') {
866
+ const { r, s } = sg;
867
+ _sig = new Signature(r, s);
868
+ }
869
+ else {
870
+ throw new Error('PARSE');
871
+ }
872
+ P = Point.fromHex(publicKey);
873
+ }
874
+ catch (error) {
875
+ if (error.message === 'PARSE')
876
+ throw new Error(`signature must be Signature instance, Uint8Array or hex string`);
877
+ return false;
878
+ }
879
+ if (lowS && _sig.hasHighS())
880
+ return false;
881
+ if (prehash)
882
+ msgHash = CURVE.hash(msgHash);
883
+ const { r, s } = _sig;
884
+ const h = bits2int_modN(msgHash); // Cannot use fields methods, since it is group element
885
+ const is = invN(s); // s^-1
886
+ const u1 = modN(h * is); // u1 = hs^-1 mod n
887
+ const u2 = modN(r * is); // u2 = rs^-1 mod n
888
+ const R = Point.BASE.multiplyAndAddUnsafe(P, u1, u2)?.toAffine(); // R = u1⋅G + u2⋅P
889
+ if (!R)
890
+ return false;
891
+ const v = modN(R.x);
892
+ return v === r;
893
+ }
894
+ return {
895
+ CURVE,
896
+ getPublicKey,
897
+ getSharedSecret,
898
+ sign,
899
+ verify,
900
+ ProjectivePoint: Point,
901
+ Signature,
902
+ utils: utils$1,
903
+ };
904
+ }
905
+ // Implementation of the Shallue and van de Woestijne method for any Weierstrass curve
906
+ // TODO: check if there is a way to merge this with uvRatio in Edwards && move to modular?
907
+ // b = True and y = sqrt(u / v) if (u / v) is square in F, and
908
+ // b = False and y = sqrt(Z * (u / v)) otherwise.
909
+ function SWUFpSqrtRatio(Fp, Z) {
910
+ // Generic implementation
911
+ const q = Fp.ORDER;
912
+ let l = 0n;
913
+ for (let o = q - 1n; o % 2n === 0n; o /= 2n)
914
+ l += 1n;
915
+ const c1 = l; // 1. c1, the largest integer such that 2^c1 divides q - 1.
916
+ const c2 = (q - 1n) / 2n ** c1; // 2. c2 = (q - 1) / (2^c1) # Integer arithmetic
917
+ const c3 = (c2 - 1n) / 2n; // 3. c3 = (c2 - 1) / 2 # Integer arithmetic
918
+ const c4 = 2n ** c1 - 1n; // 4. c4 = 2^c1 - 1 # Integer arithmetic
919
+ const c5 = 2n ** (c1 - 1n); // 5. c5 = 2^(c1 - 1) # Integer arithmetic
920
+ const c6 = Fp.pow(Z, c2); // 6. c6 = Z^c2
921
+ const c7 = Fp.pow(Z, (c2 + 1n) / 2n); // 7. c7 = Z^((c2 + 1) / 2)
922
+ let sqrtRatio = (u, v) => {
923
+ let tv1 = c6; // 1. tv1 = c6
924
+ let tv2 = Fp.pow(v, c4); // 2. tv2 = v^c4
925
+ let tv3 = Fp.sqr(tv2); // 3. tv3 = tv2^2
926
+ tv3 = Fp.mul(tv3, v); // 4. tv3 = tv3 * v
927
+ let tv5 = Fp.mul(u, tv3); // 5. tv5 = u * tv3
928
+ tv5 = Fp.pow(tv5, c3); // 6. tv5 = tv5^c3
929
+ tv5 = Fp.mul(tv5, tv2); // 7. tv5 = tv5 * tv2
930
+ tv2 = Fp.mul(tv5, v); // 8. tv2 = tv5 * v
931
+ tv3 = Fp.mul(tv5, u); // 9. tv3 = tv5 * u
932
+ let tv4 = Fp.mul(tv3, tv2); // 10. tv4 = tv3 * tv2
933
+ tv5 = Fp.pow(tv4, c5); // 11. tv5 = tv4^c5
934
+ let isQR = Fp.eql(tv5, Fp.ONE); // 12. isQR = tv5 == 1
935
+ tv2 = Fp.mul(tv3, c7); // 13. tv2 = tv3 * c7
936
+ tv5 = Fp.mul(tv4, tv1); // 14. tv5 = tv4 * tv1
937
+ tv3 = Fp.cmov(tv2, tv3, isQR); // 15. tv3 = CMOV(tv2, tv3, isQR)
938
+ tv4 = Fp.cmov(tv5, tv4, isQR); // 16. tv4 = CMOV(tv5, tv4, isQR)
939
+ // 17. for i in (c1, c1 - 1, ..., 2):
940
+ for (let i = c1; i > 1; i--) {
941
+ let tv5 = 2n ** (i - 2n); // 18. tv5 = i - 2; 19. tv5 = 2^tv5
942
+ let tvv5 = Fp.pow(tv4, tv5); // 20. tv5 = tv4^tv5
943
+ const e1 = Fp.eql(tvv5, Fp.ONE); // 21. e1 = tv5 == 1
944
+ tv2 = Fp.mul(tv3, tv1); // 22. tv2 = tv3 * tv1
945
+ tv1 = Fp.mul(tv1, tv1); // 23. tv1 = tv1 * tv1
946
+ tvv5 = Fp.mul(tv4, tv1); // 24. tv5 = tv4 * tv1
947
+ tv3 = Fp.cmov(tv2, tv3, e1); // 25. tv3 = CMOV(tv2, tv3, e1)
948
+ tv4 = Fp.cmov(tvv5, tv4, e1); // 26. tv4 = CMOV(tv5, tv4, e1)
949
+ }
950
+ return { isValid: isQR, value: tv3 };
951
+ };
952
+ if (Fp.ORDER % 4n === 3n) {
953
+ // sqrt_ratio_3mod4(u, v)
954
+ const c1 = (Fp.ORDER - 3n) / 4n; // 1. c1 = (q - 3) / 4 # Integer arithmetic
955
+ const c2 = Fp.sqrt(Fp.neg(Z)); // 2. c2 = sqrt(-Z)
956
+ sqrtRatio = (u, v) => {
957
+ let tv1 = Fp.sqr(v); // 1. tv1 = v^2
958
+ const tv2 = Fp.mul(u, v); // 2. tv2 = u * v
959
+ tv1 = Fp.mul(tv1, tv2); // 3. tv1 = tv1 * tv2
960
+ let y1 = Fp.pow(tv1, c1); // 4. y1 = tv1^c1
961
+ y1 = Fp.mul(y1, tv2); // 5. y1 = y1 * tv2
962
+ const y2 = Fp.mul(y1, c2); // 6. y2 = y1 * c2
963
+ const tv3 = Fp.mul(Fp.sqr(y1), v); // 7. tv3 = y1^2; 8. tv3 = tv3 * v
964
+ const isQR = Fp.eql(tv3, u); // 9. isQR = tv3 == u
965
+ let y = Fp.cmov(y2, y1, isQR); // 10. y = CMOV(y2, y1, isQR)
966
+ return { isValid: isQR, value: y }; // 11. return (isQR, y) isQR ? y : y*c2
967
+ };
968
+ }
969
+ // No curves uses that
970
+ // if (Fp.ORDER % 8n === 5n) // sqrt_ratio_5mod8
971
+ return sqrtRatio;
972
+ }
973
+ // From draft-irtf-cfrg-hash-to-curve-16
974
+ function mapToCurveSimpleSWU(Fp, opts) {
975
+ modular.validateField(Fp);
976
+ if (!Fp.isValid(opts.A) || !Fp.isValid(opts.B) || !Fp.isValid(opts.Z))
977
+ throw new Error('mapToCurveSimpleSWU: invalid opts');
978
+ const sqrtRatio = SWUFpSqrtRatio(Fp, opts.Z);
979
+ if (!Fp.isOdd)
980
+ throw new Error('Fp.isOdd is not implemented!');
981
+ // Input: u, an element of F.
982
+ // Output: (x, y), a point on E.
983
+ return (u) => {
984
+ // prettier-ignore
985
+ let tv1, tv2, tv3, tv4, tv5, tv6, x, y;
986
+ tv1 = Fp.sqr(u); // 1. tv1 = u^2
987
+ tv1 = Fp.mul(tv1, opts.Z); // 2. tv1 = Z * tv1
988
+ tv2 = Fp.sqr(tv1); // 3. tv2 = tv1^2
989
+ tv2 = Fp.add(tv2, tv1); // 4. tv2 = tv2 + tv1
990
+ tv3 = Fp.add(tv2, Fp.ONE); // 5. tv3 = tv2 + 1
991
+ tv3 = Fp.mul(tv3, opts.B); // 6. tv3 = B * tv3
992
+ tv4 = Fp.cmov(opts.Z, Fp.neg(tv2), !Fp.eql(tv2, Fp.ZERO)); // 7. tv4 = CMOV(Z, -tv2, tv2 != 0)
993
+ tv4 = Fp.mul(tv4, opts.A); // 8. tv4 = A * tv4
994
+ tv2 = Fp.sqr(tv3); // 9. tv2 = tv3^2
995
+ tv6 = Fp.sqr(tv4); // 10. tv6 = tv4^2
996
+ tv5 = Fp.mul(tv6, opts.A); // 11. tv5 = A * tv6
997
+ tv2 = Fp.add(tv2, tv5); // 12. tv2 = tv2 + tv5
998
+ tv2 = Fp.mul(tv2, tv3); // 13. tv2 = tv2 * tv3
999
+ tv6 = Fp.mul(tv6, tv4); // 14. tv6 = tv6 * tv4
1000
+ tv5 = Fp.mul(tv6, opts.B); // 15. tv5 = B * tv6
1001
+ tv2 = Fp.add(tv2, tv5); // 16. tv2 = tv2 + tv5
1002
+ x = Fp.mul(tv1, tv3); // 17. x = tv1 * tv3
1003
+ const { isValid, value } = sqrtRatio(tv2, tv6); // 18. (is_gx1_square, y1) = sqrt_ratio(tv2, tv6)
1004
+ y = Fp.mul(tv1, u); // 19. y = tv1 * u -> Z * u^3 * y1
1005
+ y = Fp.mul(y, value); // 20. y = y * y1
1006
+ x = Fp.cmov(x, tv3, isValid); // 21. x = CMOV(x, tv3, is_gx1_square)
1007
+ y = Fp.cmov(y, value, isValid); // 22. y = CMOV(y, y1, is_gx1_square)
1008
+ const e1 = Fp.isOdd(u) === Fp.isOdd(y); // 23. e1 = sgn0(u) == sgn0(y)
1009
+ y = Fp.cmov(Fp.neg(y), y, e1); // 24. y = CMOV(-y, y, e1)
1010
+ x = Fp.div(x, tv4); // 25. x = x / tv4
1011
+ return { x, y };
1012
+ };
1013
+ }
1014
+
1015
+ exports.SWUFpSqrtRatio = SWUFpSqrtRatio;
1016
+ exports.mapToCurveSimpleSWU = mapToCurveSimpleSWU;
1017
+ exports.weierstrass = weierstrass;
1018
+ exports.weierstrassPoints = weierstrassPoints;