@hfunlabs/hyperliquid 0.30.0-hfunlabs.1 → 0.30.2-hfunlabs.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (397) hide show
  1. package/esm/_dnt.shims.d.ts +2 -0
  2. package/esm/_dnt.shims.d.ts.map +1 -0
  3. package/esm/_dnt.shims.js +58 -0
  4. package/esm/_dnt.shims.js.map +1 -0
  5. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.d.ts +195 -0
  6. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.d.ts.map +1 -0
  7. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.js +457 -0
  8. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.js.map +1 -0
  9. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.d.ts +90 -0
  10. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.js +207 -0
  12. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.js.map +1 -0
  13. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.d.ts +159 -0
  14. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.d.ts.map +1 -0
  15. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.js +555 -0
  16. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.js.map +1 -0
  17. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.d.ts +325 -0
  18. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.js +1222 -0
  20. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.js.map +1 -0
  21. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.d.ts +76 -0
  22. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.d.ts.map +1 -0
  23. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.js +283 -0
  24. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.js.map +1 -0
  25. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/utils.d.ts +103 -0
  26. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/utils.d.ts.map +1 -0
  27. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/utils.js +241 -0
  28. package/esm/deps/jsr.io/@noble/curves/2.0.1/src/utils.js.map +1 -0
  29. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_md.d.ts +49 -0
  30. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_md.d.ts.map +1 -0
  31. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_md.js +147 -0
  32. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_md.js.map +1 -0
  33. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.d.ts +55 -0
  34. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.d.ts.map +1 -0
  35. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.js +67 -0
  36. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.js.map +1 -0
  37. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.d.ts +36 -0
  38. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.d.ts.map +1 -0
  39. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.js +90 -0
  40. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.js.map +1 -0
  41. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.d.ts +199 -0
  42. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.d.ts.map +1 -0
  43. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.js +397 -0
  44. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.js.map +1 -0
  45. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.d.ts +58 -0
  46. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.d.ts.map +1 -0
  47. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.js +254 -0
  48. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.js.map +1 -0
  49. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/utils.d.ts +124 -0
  50. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/utils.d.ts.map +1 -0
  51. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/utils.js +243 -0
  52. package/esm/deps/jsr.io/@noble/hashes/2.0.1/src/utils.js.map +1 -0
  53. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.d.ts +44 -0
  54. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.d.ts.map +1 -0
  55. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.js +144 -0
  56. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.js.map +1 -0
  57. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.d.ts +38 -0
  58. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.d.ts.map +1 -0
  59. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.js +96 -0
  60. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.js.map +1 -0
  61. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.d.ts +23 -0
  62. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.d.ts.map +1 -0
  63. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.js +98 -0
  64. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.js.map +1 -0
  65. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.d.ts +300 -0
  66. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.d.ts.map +1 -0
  67. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.js +535 -0
  68. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.js.map +1 -0
  69. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.d.ts +98 -0
  70. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.d.ts.map +1 -0
  71. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.js +207 -0
  72. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.js.map +1 -0
  73. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.d.ts +78 -0
  74. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.d.ts.map +1 -0
  75. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.js +298 -0
  76. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.js.map +1 -0
  77. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.d.ts +6 -0
  78. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.d.ts.map +1 -0
  79. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.js +6 -0
  80. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.js.map +1 -0
  81. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.d.ts +66 -0
  82. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.d.ts.map +1 -0
  83. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.js +180 -0
  84. package/esm/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.js.map +1 -0
  85. package/esm/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.d.ts +877 -0
  86. package/esm/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.d.ts.map +1 -0
  87. package/esm/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.js +2001 -0
  88. package/esm/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.js.map +1 -0
  89. package/esm/deps/jsr.io/@scure/base/2.0.0/index.d.ts +294 -0
  90. package/esm/deps/jsr.io/@scure/base/2.0.0/index.d.ts.map +1 -0
  91. package/esm/deps/jsr.io/@scure/base/2.0.0/index.js +704 -0
  92. package/esm/deps/jsr.io/@scure/base/2.0.0/index.js.map +1 -0
  93. package/esm/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.d.ts +93 -0
  94. package/esm/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.d.ts.map +1 -0
  95. package/esm/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.js +137 -0
  96. package/esm/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.js.map +1 -0
  97. package/esm/src/api/exchange/_methods/_base/_nonce.d.ts +3 -2
  98. package/esm/src/api/exchange/_methods/_base/_nonce.d.ts.map +1 -1
  99. package/esm/src/api/exchange/_methods/_base/_nonce.js +3 -2
  100. package/esm/src/api/exchange/_methods/_base/_nonce.js.map +1 -1
  101. package/esm/src/api/exchange/_methods/_base/_semaphore.d.ts +7 -6
  102. package/esm/src/api/exchange/_methods/_base/_semaphore.d.ts.map +1 -1
  103. package/esm/src/api/exchange/_methods/_base/_semaphore.js +34 -47
  104. package/esm/src/api/exchange/_methods/_base/_semaphore.js.map +1 -1
  105. package/esm/src/api/exchange/_methods/_base/execute.d.ts +14 -3
  106. package/esm/src/api/exchange/_methods/_base/execute.d.ts.map +1 -1
  107. package/esm/src/api/exchange/_methods/_base/execute.js +44 -10
  108. package/esm/src/api/exchange/_methods/_base/execute.js.map +1 -1
  109. package/esm/src/api/exchange/_methods/batchModify.d.ts +6 -6
  110. package/esm/src/api/exchange/_methods/createVault.d.ts +5 -5
  111. package/esm/src/api/exchange/_methods/modify.d.ts +5 -5
  112. package/esm/src/api/exchange/_methods/order.d.ts +13 -13
  113. package/esm/src/api/exchange/_methods/order.d.ts.map +1 -1
  114. package/esm/src/api/exchange/_methods/order.js +4 -2
  115. package/esm/src/api/exchange/_methods/order.js.map +1 -1
  116. package/esm/src/api/exchange/client.d.ts +1 -1
  117. package/esm/src/api/exchange/client.d.ts.map +1 -1
  118. package/esm/src/api/exchange/client.js.map +1 -1
  119. package/esm/src/api/exchange/mod.d.ts +1 -1
  120. package/esm/src/api/exchange/mod.d.ts.map +1 -1
  121. package/esm/src/api/exchange/mod.js.map +1 -1
  122. package/esm/src/api/info/_methods/_base/commonSchemas.d.ts +8 -8
  123. package/esm/src/api/info/_methods/frontendOpenOrders.d.ts +3 -3
  124. package/esm/src/api/info/_methods/historicalOrders.d.ts +4 -4
  125. package/esm/src/api/info/_methods/l3Orders.d.ts +11 -11
  126. package/esm/src/api/info/_methods/l4Orders.d.ts +6 -6
  127. package/esm/src/api/info/_methods/openOrders.d.ts +3 -3
  128. package/esm/src/api/info/_methods/orderStatus.d.ts +5 -5
  129. package/esm/src/api/info/_methods/recentTrades.d.ts +2 -2
  130. package/esm/src/api/info/_methods/twapHistory.d.ts +4 -4
  131. package/esm/src/api/info/_methods/userFills.d.ts +3 -3
  132. package/esm/src/api/info/_methods/userFillsByTime.d.ts +4 -4
  133. package/esm/src/api/info/_methods/userFunding.d.ts +3 -3
  134. package/esm/src/api/info/_methods/userFunding.js +2 -2
  135. package/esm/src/api/info/_methods/userFunding.js.map +1 -1
  136. package/esm/src/api/info/_methods/userTwapSliceFills.d.ts +4 -4
  137. package/esm/src/api/info/_methods/userTwapSliceFillsByTime.d.ts +5 -5
  138. package/esm/src/api/info/_methods/webData2.d.ts +9 -9
  139. package/esm/src/api/subscription/_methods/assetCtxs.d.ts +2 -0
  140. package/esm/src/api/subscription/_methods/assetCtxs.d.ts.map +1 -1
  141. package/esm/src/api/subscription/_methods/assetCtxs.js.map +1 -1
  142. package/esm/src/api/subscription/_methods/clearinghouseState.d.ts +2 -0
  143. package/esm/src/api/subscription/_methods/clearinghouseState.d.ts.map +1 -1
  144. package/esm/src/api/subscription/_methods/clearinghouseState.js +2 -0
  145. package/esm/src/api/subscription/_methods/clearinghouseState.js.map +1 -1
  146. package/esm/src/api/subscription/_methods/explorerBlock.d.ts +2 -0
  147. package/esm/src/api/subscription/_methods/explorerBlock.d.ts.map +1 -1
  148. package/esm/src/api/subscription/_methods/explorerBlock.js +2 -0
  149. package/esm/src/api/subscription/_methods/explorerBlock.js.map +1 -1
  150. package/esm/src/api/subscription/_methods/explorerTxs.d.ts +2 -0
  151. package/esm/src/api/subscription/_methods/explorerTxs.d.ts.map +1 -1
  152. package/esm/src/api/subscription/_methods/explorerTxs.js +2 -0
  153. package/esm/src/api/subscription/_methods/explorerTxs.js.map +1 -1
  154. package/esm/src/api/subscription/_methods/openOrders.d.ts +5 -5
  155. package/esm/src/api/subscription/_methods/orderUpdates.d.ts +4 -4
  156. package/esm/src/api/subscription/_methods/trades.d.ts +3 -3
  157. package/esm/src/api/subscription/_methods/twapStates.d.ts +9 -4
  158. package/esm/src/api/subscription/_methods/twapStates.d.ts.map +1 -1
  159. package/esm/src/api/subscription/_methods/twapStates.js +6 -1
  160. package/esm/src/api/subscription/_methods/twapStates.js.map +1 -1
  161. package/esm/src/api/subscription/_methods/userEvents.d.ts +17 -17
  162. package/esm/src/api/subscription/_methods/userFills.d.ts +5 -5
  163. package/esm/src/api/subscription/_methods/userHistoricalOrders.d.ts +6 -6
  164. package/esm/src/api/subscription/_methods/userTwapHistory.d.ts +6 -6
  165. package/esm/src/api/subscription/_methods/userTwapSliceFills.d.ts +6 -6
  166. package/esm/src/api/subscription/_methods/webData2.d.ts +11 -11
  167. package/esm/src/signing/_privateKeySigner.js +1 -1
  168. package/esm/src/signing/_privateKeySigner.js.map +1 -1
  169. package/esm/src/signing/mod.js +1 -1
  170. package/esm/src/signing/mod.js.map +1 -1
  171. package/package.json +2 -3
  172. package/script/_dnt.shims.d.ts +2 -0
  173. package/script/_dnt.shims.d.ts.map +1 -0
  174. package/script/_dnt.shims.js +61 -0
  175. package/script/_dnt.shims.js.map +1 -0
  176. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.d.ts +195 -0
  177. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.d.ts.map +1 -0
  178. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.js +468 -0
  179. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.js.map +1 -0
  180. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.d.ts +90 -0
  181. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.d.ts.map +1 -0
  182. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.js +215 -0
  183. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.js.map +1 -0
  184. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.d.ts +159 -0
  185. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.d.ts.map +1 -0
  186. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.js +578 -0
  187. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.js.map +1 -0
  188. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.d.ts +325 -0
  189. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.d.ts.map +1 -0
  190. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.js +1232 -0
  191. package/script/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.js.map +1 -0
  192. package/script/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.d.ts +76 -0
  193. package/script/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.d.ts.map +1 -0
  194. package/script/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.js +286 -0
  195. package/script/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.js.map +1 -0
  196. package/script/deps/jsr.io/@noble/curves/2.0.1/src/utils.d.ts +103 -0
  197. package/script/deps/jsr.io/@noble/curves/2.0.1/src/utils.d.ts.map +1 -0
  198. package/script/deps/jsr.io/@noble/curves/2.0.1/src/utils.js +273 -0
  199. package/script/deps/jsr.io/@noble/curves/2.0.1/src/utils.js.map +1 -0
  200. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_md.d.ts +49 -0
  201. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_md.d.ts.map +1 -0
  202. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_md.js +153 -0
  203. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_md.js.map +1 -0
  204. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.d.ts +55 -0
  205. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.d.ts.map +1 -0
  206. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.js +90 -0
  207. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.js.map +1 -0
  208. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.d.ts +36 -0
  209. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.d.ts.map +1 -0
  210. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.js +95 -0
  211. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.js.map +1 -0
  212. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.d.ts +199 -0
  213. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.d.ts.map +1 -0
  214. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.js +439 -0
  215. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.js.map +1 -0
  216. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.d.ts +58 -0
  217. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.d.ts.map +1 -0
  218. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.js +259 -0
  219. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.js.map +1 -0
  220. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/utils.d.ts +124 -0
  221. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/utils.d.ts.map +1 -0
  222. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/utils.js +304 -0
  223. package/script/deps/jsr.io/@noble/hashes/2.0.1/src/utils.js.map +1 -0
  224. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.d.ts +44 -0
  225. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.d.ts.map +1 -0
  226. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.js +182 -0
  227. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.js.map +1 -0
  228. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.d.ts +38 -0
  229. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.d.ts.map +1 -0
  230. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.js +99 -0
  231. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.js.map +1 -0
  232. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.d.ts +23 -0
  233. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.d.ts.map +1 -0
  234. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.js +134 -0
  235. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.js.map +1 -0
  236. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.d.ts +300 -0
  237. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.d.ts.map +1 -0
  238. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.js +576 -0
  239. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.js.map +1 -0
  240. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.d.ts +98 -0
  241. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.d.ts.map +1 -0
  242. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.js +211 -0
  243. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.js.map +1 -0
  244. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.d.ts +78 -0
  245. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.d.ts.map +1 -0
  246. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.js +308 -0
  247. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.js.map +1 -0
  248. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.d.ts +6 -0
  249. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.d.ts.map +1 -0
  250. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.js +21 -0
  251. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.js.map +1 -0
  252. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.d.ts +66 -0
  253. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.d.ts.map +1 -0
  254. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.js +195 -0
  255. package/script/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.js.map +1 -0
  256. package/script/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.d.ts +877 -0
  257. package/script/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.d.ts.map +1 -0
  258. package/script/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.js +2032 -0
  259. package/script/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.js.map +1 -0
  260. package/script/deps/jsr.io/@scure/base/2.0.0/index.d.ts +294 -0
  261. package/script/deps/jsr.io/@scure/base/2.0.0/index.d.ts.map +1 -0
  262. package/script/deps/jsr.io/@scure/base/2.0.0/index.js +710 -0
  263. package/script/deps/jsr.io/@scure/base/2.0.0/index.js.map +1 -0
  264. package/script/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.d.ts +93 -0
  265. package/script/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.d.ts.map +1 -0
  266. package/script/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.js +141 -0
  267. package/script/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.js.map +1 -0
  268. package/script/src/api/exchange/_methods/_base/_nonce.d.ts +3 -2
  269. package/script/src/api/exchange/_methods/_base/_nonce.d.ts.map +1 -1
  270. package/script/src/api/exchange/_methods/_base/_nonce.js +4 -3
  271. package/script/src/api/exchange/_methods/_base/_nonce.js.map +1 -1
  272. package/script/src/api/exchange/_methods/_base/_semaphore.d.ts +7 -6
  273. package/script/src/api/exchange/_methods/_base/_semaphore.d.ts.map +1 -1
  274. package/script/src/api/exchange/_methods/_base/_semaphore.js +35 -50
  275. package/script/src/api/exchange/_methods/_base/_semaphore.js.map +1 -1
  276. package/script/src/api/exchange/_methods/_base/execute.d.ts +14 -3
  277. package/script/src/api/exchange/_methods/_base/execute.d.ts.map +1 -1
  278. package/script/src/api/exchange/_methods/_base/execute.js +43 -9
  279. package/script/src/api/exchange/_methods/_base/execute.js.map +1 -1
  280. package/script/src/api/exchange/_methods/batchModify.d.ts +6 -6
  281. package/script/src/api/exchange/_methods/createVault.d.ts +5 -5
  282. package/script/src/api/exchange/_methods/modify.d.ts +5 -5
  283. package/script/src/api/exchange/_methods/order.d.ts +13 -13
  284. package/script/src/api/exchange/_methods/order.d.ts.map +1 -1
  285. package/script/src/api/exchange/_methods/order.js +4 -2
  286. package/script/src/api/exchange/_methods/order.js.map +1 -1
  287. package/script/src/api/exchange/client.d.ts +1 -1
  288. package/script/src/api/exchange/client.d.ts.map +1 -1
  289. package/script/src/api/exchange/client.js.map +1 -1
  290. package/script/src/api/exchange/mod.d.ts +1 -1
  291. package/script/src/api/exchange/mod.d.ts.map +1 -1
  292. package/script/src/api/exchange/mod.js.map +1 -1
  293. package/script/src/api/info/_methods/_base/commonSchemas.d.ts +8 -8
  294. package/script/src/api/info/_methods/frontendOpenOrders.d.ts +3 -3
  295. package/script/src/api/info/_methods/historicalOrders.d.ts +4 -4
  296. package/script/src/api/info/_methods/l3Orders.d.ts +11 -11
  297. package/script/src/api/info/_methods/l4Orders.d.ts +6 -6
  298. package/script/src/api/info/_methods/openOrders.d.ts +3 -3
  299. package/script/src/api/info/_methods/orderStatus.d.ts +5 -5
  300. package/script/src/api/info/_methods/recentTrades.d.ts +2 -2
  301. package/script/src/api/info/_methods/twapHistory.d.ts +4 -4
  302. package/script/src/api/info/_methods/userFills.d.ts +3 -3
  303. package/script/src/api/info/_methods/userFillsByTime.d.ts +4 -4
  304. package/script/src/api/info/_methods/userFunding.d.ts +3 -3
  305. package/script/src/api/info/_methods/userFunding.js +2 -2
  306. package/script/src/api/info/_methods/userFunding.js.map +1 -1
  307. package/script/src/api/info/_methods/userTwapSliceFills.d.ts +4 -4
  308. package/script/src/api/info/_methods/userTwapSliceFillsByTime.d.ts +5 -5
  309. package/script/src/api/info/_methods/webData2.d.ts +9 -9
  310. package/script/src/api/subscription/_methods/assetCtxs.d.ts +2 -0
  311. package/script/src/api/subscription/_methods/assetCtxs.d.ts.map +1 -1
  312. package/script/src/api/subscription/_methods/assetCtxs.js.map +1 -1
  313. package/script/src/api/subscription/_methods/clearinghouseState.d.ts +2 -0
  314. package/script/src/api/subscription/_methods/clearinghouseState.d.ts.map +1 -1
  315. package/script/src/api/subscription/_methods/clearinghouseState.js +2 -0
  316. package/script/src/api/subscription/_methods/clearinghouseState.js.map +1 -1
  317. package/script/src/api/subscription/_methods/explorerBlock.d.ts +2 -0
  318. package/script/src/api/subscription/_methods/explorerBlock.d.ts.map +1 -1
  319. package/script/src/api/subscription/_methods/explorerBlock.js +2 -0
  320. package/script/src/api/subscription/_methods/explorerBlock.js.map +1 -1
  321. package/script/src/api/subscription/_methods/explorerTxs.d.ts +2 -0
  322. package/script/src/api/subscription/_methods/explorerTxs.d.ts.map +1 -1
  323. package/script/src/api/subscription/_methods/explorerTxs.js +2 -0
  324. package/script/src/api/subscription/_methods/explorerTxs.js.map +1 -1
  325. package/script/src/api/subscription/_methods/openOrders.d.ts +5 -5
  326. package/script/src/api/subscription/_methods/orderUpdates.d.ts +4 -4
  327. package/script/src/api/subscription/_methods/trades.d.ts +3 -3
  328. package/script/src/api/subscription/_methods/twapStates.d.ts +9 -4
  329. package/script/src/api/subscription/_methods/twapStates.d.ts.map +1 -1
  330. package/script/src/api/subscription/_methods/twapStates.js +6 -1
  331. package/script/src/api/subscription/_methods/twapStates.js.map +1 -1
  332. package/script/src/api/subscription/_methods/userEvents.d.ts +17 -17
  333. package/script/src/api/subscription/_methods/userFills.d.ts +5 -5
  334. package/script/src/api/subscription/_methods/userHistoricalOrders.d.ts +6 -6
  335. package/script/src/api/subscription/_methods/userTwapHistory.d.ts +6 -6
  336. package/script/src/api/subscription/_methods/userTwapSliceFills.d.ts +6 -6
  337. package/script/src/api/subscription/_methods/webData2.d.ts +11 -11
  338. package/script/src/signing/_privateKeySigner.js +3 -3
  339. package/script/src/signing/_privateKeySigner.js.map +1 -1
  340. package/script/src/signing/mod.js +2 -2
  341. package/script/src/signing/mod.js.map +1 -1
  342. package/src/_dnt.shims.ts +60 -0
  343. package/src/deps/jsr.io/@noble/curves/2.0.1/src/abstract/curve.ts +633 -0
  344. package/src/deps/jsr.io/@noble/curves/2.0.1/src/abstract/hash-to-curve.ts +292 -0
  345. package/src/deps/jsr.io/@noble/curves/2.0.1/src/abstract/modular.ts +621 -0
  346. package/src/deps/jsr.io/@noble/curves/2.0.1/src/abstract/weierstrass.ts +1562 -0
  347. package/src/deps/jsr.io/@noble/curves/2.0.1/src/secp256k1.ts +327 -0
  348. package/src/deps/jsr.io/@noble/curves/2.0.1/src/utils.ts +306 -0
  349. package/src/deps/jsr.io/@noble/hashes/2.0.1/src/_md.ts +156 -0
  350. package/src/deps/jsr.io/@noble/hashes/2.0.1/src/_u64.ts +91 -0
  351. package/src/deps/jsr.io/@noble/hashes/2.0.1/src/hmac.ts +94 -0
  352. package/src/deps/jsr.io/@noble/hashes/2.0.1/src/sha2.ts +469 -0
  353. package/src/deps/jsr.io/@noble/hashes/2.0.1/src/sha3.ts +295 -0
  354. package/src/deps/jsr.io/@noble/hashes/2.0.1/src/utils.ts +340 -0
  355. package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/advanced/abi-mapper.ts +212 -0
  356. package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/address.ts +100 -0
  357. package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/rlp.ts +104 -0
  358. package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx-internal.ts +626 -0
  359. package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/tx.ts +274 -0
  360. package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/core/typed-data.ts +391 -0
  361. package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/index.ts +12 -0
  362. package/src/deps/jsr.io/@paulmillr/micro-eth-signer/0.18.1/src/utils.ts +233 -0
  363. package/src/deps/jsr.io/@paulmillr/micro-packed/0.8.0/src/index.ts +2196 -0
  364. package/src/deps/jsr.io/@scure/base/2.0.0/index.ts +857 -0
  365. package/src/deps/jsr.io/@std/async/1.0.16/unstable_semaphore.ts +146 -0
  366. package/src/src/api/exchange/_methods/_base/_nonce.ts +3 -2
  367. package/src/src/api/exchange/_methods/_base/_semaphore.ts +34 -59
  368. package/src/src/api/exchange/_methods/_base/execute.ts +60 -12
  369. package/src/src/api/exchange/_methods/order.ts +5 -3
  370. package/src/src/api/exchange/client.ts +5 -1
  371. package/src/src/api/exchange/mod.ts +5 -1
  372. package/src/src/api/info/_methods/userFunding.ts +2 -2
  373. package/src/src/api/subscription/_methods/assetCtxs.ts +2 -0
  374. package/src/src/api/subscription/_methods/clearinghouseState.ts +2 -0
  375. package/src/src/api/subscription/_methods/explorerBlock.ts +2 -0
  376. package/src/src/api/subscription/_methods/explorerTxs.ts +2 -0
  377. package/src/src/api/subscription/_methods/twapStates.ts +9 -1
  378. package/src/src/signing/_privateKeySigner.ts +1 -1
  379. package/src/src/signing/mod.ts +1 -1
  380. package/esm/deps/jsr.io/@std/msgpack/1.0.3/decode.d.ts +0 -21
  381. package/esm/deps/jsr.io/@std/msgpack/1.0.3/decode.d.ts.map +0 -1
  382. package/esm/deps/jsr.io/@std/msgpack/1.0.3/decode.js +0 -292
  383. package/esm/deps/jsr.io/@std/msgpack/1.0.3/decode.js.map +0 -1
  384. package/esm/deps/jsr.io/@std/msgpack/1.0.3/mod.d.ts +0 -44
  385. package/esm/deps/jsr.io/@std/msgpack/1.0.3/mod.d.ts.map +0 -1
  386. package/esm/deps/jsr.io/@std/msgpack/1.0.3/mod.js +0 -46
  387. package/esm/deps/jsr.io/@std/msgpack/1.0.3/mod.js.map +0 -1
  388. package/script/deps/jsr.io/@std/msgpack/1.0.3/decode.d.ts +0 -21
  389. package/script/deps/jsr.io/@std/msgpack/1.0.3/decode.d.ts.map +0 -1
  390. package/script/deps/jsr.io/@std/msgpack/1.0.3/decode.js +0 -295
  391. package/script/deps/jsr.io/@std/msgpack/1.0.3/decode.js.map +0 -1
  392. package/script/deps/jsr.io/@std/msgpack/1.0.3/mod.d.ts +0 -44
  393. package/script/deps/jsr.io/@std/msgpack/1.0.3/mod.d.ts.map +0 -1
  394. package/script/deps/jsr.io/@std/msgpack/1.0.3/mod.js +0 -62
  395. package/script/deps/jsr.io/@std/msgpack/1.0.3/mod.js.map +0 -1
  396. package/src/deps/jsr.io/@std/msgpack/1.0.3/decode.ts +0 -391
  397. package/src/deps/jsr.io/@std/msgpack/1.0.3/mod.ts +0 -47
@@ -0,0 +1,1232 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DER = exports.DERErr = void 0;
4
+ exports._splitEndoScalar = _splitEndoScalar;
5
+ exports.weierstrass = weierstrass;
6
+ exports.SWUFpSqrtRatio = SWUFpSqrtRatio;
7
+ exports.mapToCurveSimpleSWU = mapToCurveSimpleSWU;
8
+ exports.ecdh = ecdh;
9
+ exports.ecdsa = ecdsa;
10
+ /**
11
+ * Short Weierstrass curve methods. The formula is: y² = x³ + ax + b.
12
+ *
13
+ * ### Design rationale for types
14
+ *
15
+ * * Interaction between classes from different curves should fail:
16
+ * `k256.Point.BASE.add(p256.Point.BASE)`
17
+ * * For this purpose we want to use `instanceof` operator, which is fast and works during runtime
18
+ * * Different calls of `curve()` would return different classes -
19
+ * `curve(params) !== curve(params)`: if somebody decided to monkey-patch their curve,
20
+ * it won't affect others
21
+ *
22
+ * TypeScript can't infer types for classes created inside a function. Classes is one instance
23
+ * of nominative types in TypeScript and interfaces only check for shape, so it's hard to create
24
+ * unique type for every function call.
25
+ *
26
+ * We can use generic types via some param, like curve opts, but that would:
27
+ * 1. Enable interaction between `curve(params)` and `curve(params)` (curves of same params)
28
+ * which is hard to debug.
29
+ * 2. Params can be generic and we can't enforce them to be constant value:
30
+ * if somebody creates curve from non-constant params,
31
+ * it would be allowed to interact with other curves with non-constant params
32
+ *
33
+ * @todo https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
34
+ * @module
35
+ */
36
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
37
+ const hmac_js_1 = require("../../../../hashes/2.0.1/src/hmac.js");
38
+ const utils_js_1 = require("../../../../hashes/2.0.1/src/utils.js");
39
+ const utils_js_2 = require("../utils.js");
40
+ const curve_js_1 = require("./curve.js");
41
+ const modular_js_1 = require("./modular.js");
42
+ // We construct basis in such way that den is always positive and equals n, but num sign depends on basis (not on secret value)
43
+ const divNearest = (num, den) => (num + (num >= 0 ? den : -den) / _2n) / den;
44
+ /**
45
+ * Splits scalar for GLV endomorphism.
46
+ */
47
+ function _splitEndoScalar(k, basis, n) {
48
+ // Split scalar into two such that part is ~half bits: `abs(part) < sqrt(N)`
49
+ // Since part can be negative, we need to do this on point.
50
+ // TODO: verifyScalar function which consumes lambda
51
+ const [[a1, b1], [a2, b2]] = basis;
52
+ const c1 = divNearest(b2 * k, n);
53
+ const c2 = divNearest(-b1 * k, n);
54
+ // |k1|/|k2| is < sqrt(N), but can be negative.
55
+ // If we do `k1 mod N`, we'll get big scalar (`> sqrt(N)`): so, we do cheaper negation instead.
56
+ let k1 = k - c1 * a1 - c2 * a2;
57
+ let k2 = -c1 * b1 - c2 * b2;
58
+ const k1neg = k1 < _0n;
59
+ const k2neg = k2 < _0n;
60
+ if (k1neg)
61
+ k1 = -k1;
62
+ if (k2neg)
63
+ k2 = -k2;
64
+ // Double check that resulting scalar less than half bits of N: otherwise wNAF will fail.
65
+ // This should only happen on wrong basises. Also, math inside is too complex and I don't trust it.
66
+ const MAX_NUM = (0, utils_js_2.bitMask)(Math.ceil((0, utils_js_2.bitLen)(n) / 2)) + _1n; // Half bits of N
67
+ if (k1 < _0n || k1 >= MAX_NUM || k2 < _0n || k2 >= MAX_NUM) {
68
+ throw new Error('splitScalar (endomorphism): failed, k=' + k);
69
+ }
70
+ return { k1neg, k1, k2neg, k2 };
71
+ }
72
+ function validateSigFormat(format) {
73
+ if (!['compact', 'recovered', 'der'].includes(format))
74
+ throw new Error('Signature format must be "compact", "recovered", or "der"');
75
+ return format;
76
+ }
77
+ function validateSigOpts(opts, def) {
78
+ const optsn = {};
79
+ for (let optName of Object.keys(def)) {
80
+ // @ts-ignore
81
+ optsn[optName] = opts[optName] === undefined ? def[optName] : opts[optName];
82
+ }
83
+ (0, utils_js_2.abool)(optsn.lowS, 'lowS');
84
+ (0, utils_js_2.abool)(optsn.prehash, 'prehash');
85
+ if (optsn.format !== undefined)
86
+ validateSigFormat(optsn.format);
87
+ return optsn;
88
+ }
89
+ class DERErr extends Error {
90
+ constructor(m = '') {
91
+ super(m);
92
+ }
93
+ }
94
+ exports.DERErr = DERErr;
95
+ /**
96
+ * ASN.1 DER encoding utilities. ASN is very complex & fragile. Format:
97
+ *
98
+ * [0x30 (SEQUENCE), bytelength, 0x02 (INTEGER), intLength, R, 0x02 (INTEGER), intLength, S]
99
+ *
100
+ * Docs: https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/, https://luca.ntop.org/Teaching/Appunti/asn1.html
101
+ */
102
+ exports.DER = {
103
+ // asn.1 DER encoding utils
104
+ Err: DERErr,
105
+ // Basic building block is TLV (Tag-Length-Value)
106
+ _tlv: {
107
+ encode: (tag, data) => {
108
+ const { Err: E } = exports.DER;
109
+ if (tag < 0 || tag > 256)
110
+ throw new E('tlv.encode: wrong tag');
111
+ if (data.length & 1)
112
+ throw new E('tlv.encode: unpadded data');
113
+ const dataLen = data.length / 2;
114
+ const len = (0, utils_js_2.numberToHexUnpadded)(dataLen);
115
+ if ((len.length / 2) & 0b1000_0000)
116
+ throw new E('tlv.encode: long form length too big');
117
+ // length of length with long form flag
118
+ const lenLen = dataLen > 127 ? (0, utils_js_2.numberToHexUnpadded)((len.length / 2) | 0b1000_0000) : '';
119
+ const t = (0, utils_js_2.numberToHexUnpadded)(tag);
120
+ return t + lenLen + len + data;
121
+ },
122
+ // v - value, l - left bytes (unparsed)
123
+ decode(tag, data) {
124
+ const { Err: E } = exports.DER;
125
+ let pos = 0;
126
+ if (tag < 0 || tag > 256)
127
+ throw new E('tlv.encode: wrong tag');
128
+ if (data.length < 2 || data[pos++] !== tag)
129
+ throw new E('tlv.decode: wrong tlv');
130
+ const first = data[pos++];
131
+ const isLong = !!(first & 0b1000_0000); // First bit of first length byte is flag for short/long form
132
+ let length = 0;
133
+ if (!isLong)
134
+ length = first;
135
+ else {
136
+ // Long form: [longFlag(1bit), lengthLength(7bit), length (BE)]
137
+ const lenLen = first & 0b0111_1111;
138
+ if (!lenLen)
139
+ throw new E('tlv.decode(long): indefinite length not supported');
140
+ if (lenLen > 4)
141
+ throw new E('tlv.decode(long): byte length is too big'); // this will overflow u32 in js
142
+ const lengthBytes = data.subarray(pos, pos + lenLen);
143
+ if (lengthBytes.length !== lenLen)
144
+ throw new E('tlv.decode: length bytes not complete');
145
+ if (lengthBytes[0] === 0)
146
+ throw new E('tlv.decode(long): zero leftmost byte');
147
+ for (const b of lengthBytes)
148
+ length = (length << 8) | b;
149
+ pos += lenLen;
150
+ if (length < 128)
151
+ throw new E('tlv.decode(long): not minimal encoding');
152
+ }
153
+ const v = data.subarray(pos, pos + length);
154
+ if (v.length !== length)
155
+ throw new E('tlv.decode: wrong value length');
156
+ return { v, l: data.subarray(pos + length) };
157
+ },
158
+ },
159
+ // https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
160
+ // since we always use positive integers here. It must always be empty:
161
+ // - add zero byte if exists
162
+ // - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
163
+ _int: {
164
+ encode(num) {
165
+ const { Err: E } = exports.DER;
166
+ if (num < _0n)
167
+ throw new E('integer: negative integers are not allowed');
168
+ let hex = (0, utils_js_2.numberToHexUnpadded)(num);
169
+ // Pad with zero byte if negative flag is present
170
+ if (Number.parseInt(hex[0], 16) & 0b1000)
171
+ hex = '00' + hex;
172
+ if (hex.length & 1)
173
+ throw new E('unexpected DER parsing assertion: unpadded hex');
174
+ return hex;
175
+ },
176
+ decode(data) {
177
+ const { Err: E } = exports.DER;
178
+ if (data[0] & 0b1000_0000)
179
+ throw new E('invalid signature integer: negative');
180
+ if (data[0] === 0x00 && !(data[1] & 0b1000_0000))
181
+ throw new E('invalid signature integer: unnecessary leading zero');
182
+ return (0, utils_js_2.bytesToNumberBE)(data);
183
+ },
184
+ },
185
+ toSig(bytes) {
186
+ // parse DER signature
187
+ const { Err: E, _int: int, _tlv: tlv } = exports.DER;
188
+ const data = (0, utils_js_2.abytes)(bytes, undefined, 'signature');
189
+ const { v: seqBytes, l: seqLeftBytes } = tlv.decode(0x30, data);
190
+ if (seqLeftBytes.length)
191
+ throw new E('invalid signature: left bytes after parsing');
192
+ const { v: rBytes, l: rLeftBytes } = tlv.decode(0x02, seqBytes);
193
+ const { v: sBytes, l: sLeftBytes } = tlv.decode(0x02, rLeftBytes);
194
+ if (sLeftBytes.length)
195
+ throw new E('invalid signature: left bytes after parsing');
196
+ return { r: int.decode(rBytes), s: int.decode(sBytes) };
197
+ },
198
+ hexFromSig(sig) {
199
+ const { _tlv: tlv, _int: int } = exports.DER;
200
+ const rs = tlv.encode(0x02, int.encode(sig.r));
201
+ const ss = tlv.encode(0x02, int.encode(sig.s));
202
+ const seq = rs + ss;
203
+ return tlv.encode(0x30, seq);
204
+ },
205
+ };
206
+ // Be friendly to bad ECMAScript parsers by not using bigint literals
207
+ // prettier-ignore
208
+ const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
209
+ /**
210
+ * Creates weierstrass Point constructor, based on specified curve options.
211
+ *
212
+ * See {@link WeierstrassOpts}.
213
+ *
214
+ * @example
215
+ ```js
216
+ const opts = {
217
+ p: 0xfffffffffffffffffffffffffffffffeffffac73n,
218
+ n: 0x100000000000000000001b8fa16dfab9aca16b6b3n,
219
+ h: 1n,
220
+ a: 0n,
221
+ b: 7n,
222
+ Gx: 0x3b4c382ce37aa192a4019e763036f4f5dd4d7ebbn,
223
+ Gy: 0x938cf935318fdced6bc28286531733c3f03c4feen,
224
+ };
225
+ const secp160k1_Point = weierstrass(opts);
226
+ ```
227
+ */
228
+ function weierstrass(params, extraOpts = {}) {
229
+ const validated = (0, curve_js_1.createCurveFields)('weierstrass', params, extraOpts);
230
+ const { Fp, Fn } = validated;
231
+ let CURVE = validated.CURVE;
232
+ const { h: cofactor, n: CURVE_ORDER } = CURVE;
233
+ (0, utils_js_2.validateObject)(extraOpts, {}, {
234
+ allowInfinityPoint: 'boolean',
235
+ clearCofactor: 'function',
236
+ isTorsionFree: 'function',
237
+ fromBytes: 'function',
238
+ toBytes: 'function',
239
+ endo: 'object',
240
+ });
241
+ const { endo } = extraOpts;
242
+ if (endo) {
243
+ // validateObject(endo, { beta: 'bigint', splitScalar: 'function' });
244
+ if (!Fp.is0(CURVE.a) || typeof endo.beta !== 'bigint' || !Array.isArray(endo.basises)) {
245
+ throw new Error('invalid endo: expected "beta": bigint and "basises": array');
246
+ }
247
+ }
248
+ const lengths = getWLengths(Fp, Fn);
249
+ function assertCompressionIsSupported() {
250
+ if (!Fp.isOdd)
251
+ throw new Error('compression is not supported: Field does not have .isOdd()');
252
+ }
253
+ // Implements IEEE P1363 point encoding
254
+ function pointToBytes(_c, point, isCompressed) {
255
+ const { x, y } = point.toAffine();
256
+ const bx = Fp.toBytes(x);
257
+ (0, utils_js_2.abool)(isCompressed, 'isCompressed');
258
+ if (isCompressed) {
259
+ assertCompressionIsSupported();
260
+ const hasEvenY = !Fp.isOdd(y);
261
+ return (0, utils_js_2.concatBytes)(pprefix(hasEvenY), bx);
262
+ }
263
+ else {
264
+ return (0, utils_js_2.concatBytes)(Uint8Array.of(0x04), bx, Fp.toBytes(y));
265
+ }
266
+ }
267
+ function pointFromBytes(bytes) {
268
+ (0, utils_js_2.abytes)(bytes, undefined, 'Point');
269
+ const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths; // e.g. for 32-byte: 33, 65
270
+ const length = bytes.length;
271
+ const head = bytes[0];
272
+ const tail = bytes.subarray(1);
273
+ // No actual validation is done here: use .assertValidity()
274
+ if (length === comp && (head === 0x02 || head === 0x03)) {
275
+ const x = Fp.fromBytes(tail);
276
+ if (!Fp.isValid(x))
277
+ throw new Error('bad point: is not on curve, wrong x');
278
+ const y2 = weierstrassEquation(x); // y² = x³ + ax + b
279
+ let y;
280
+ try {
281
+ y = Fp.sqrt(y2); // y = y² ^ (p+1)/4
282
+ }
283
+ catch (sqrtError) {
284
+ const err = sqrtError instanceof Error ? ': ' + sqrtError.message : '';
285
+ throw new Error('bad point: is not on curve, sqrt error' + err);
286
+ }
287
+ assertCompressionIsSupported();
288
+ const evenY = Fp.isOdd(y);
289
+ const evenH = (head & 1) === 1; // ECDSA-specific
290
+ if (evenH !== evenY)
291
+ y = Fp.neg(y);
292
+ return { x, y };
293
+ }
294
+ else if (length === uncomp && head === 0x04) {
295
+ // TODO: more checks
296
+ const L = Fp.BYTES;
297
+ const x = Fp.fromBytes(tail.subarray(0, L));
298
+ const y = Fp.fromBytes(tail.subarray(L, L * 2));
299
+ if (!isValidXY(x, y))
300
+ throw new Error('bad point: is not on curve');
301
+ return { x, y };
302
+ }
303
+ else {
304
+ throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
305
+ }
306
+ }
307
+ const encodePoint = extraOpts.toBytes || pointToBytes;
308
+ const decodePoint = extraOpts.fromBytes || pointFromBytes;
309
+ function weierstrassEquation(x) {
310
+ const x2 = Fp.sqr(x); // x * x
311
+ const x3 = Fp.mul(x2, x); // x² * x
312
+ return Fp.add(Fp.add(x3, Fp.mul(x, CURVE.a)), CURVE.b); // x³ + a * x + b
313
+ }
314
+ // TODO: move top-level
315
+ /** Checks whether equation holds for given x, y: y² == x³ + ax + b */
316
+ function isValidXY(x, y) {
317
+ const left = Fp.sqr(y); // y²
318
+ const right = weierstrassEquation(x); // x³ + ax + b
319
+ return Fp.eql(left, right);
320
+ }
321
+ // Validate whether the passed curve params are valid.
322
+ // Test 1: equation y² = x³ + ax + b should work for generator point.
323
+ if (!isValidXY(CURVE.Gx, CURVE.Gy))
324
+ throw new Error('bad curve params: generator point');
325
+ // Test 2: discriminant Δ part should be non-zero: 4a³ + 27b² != 0.
326
+ // Guarantees curve is genus-1, smooth (non-singular).
327
+ const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n), _4n);
328
+ const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
329
+ if (Fp.is0(Fp.add(_4a3, _27b2)))
330
+ throw new Error('bad curve params: a or b');
331
+ /** Asserts coordinate is valid: 0 <= n < Fp.ORDER. */
332
+ function acoord(title, n, banZero = false) {
333
+ if (!Fp.isValid(n) || (banZero && Fp.is0(n)))
334
+ throw new Error(`bad point coordinate ${title}`);
335
+ return n;
336
+ }
337
+ function aprjpoint(other) {
338
+ if (!(other instanceof Point))
339
+ throw new Error('Weierstrass Point expected');
340
+ }
341
+ function splitEndoScalarN(k) {
342
+ if (!endo || !endo.basises)
343
+ throw new Error('no endo');
344
+ return _splitEndoScalar(k, endo.basises, Fn.ORDER);
345
+ }
346
+ // Memoized toAffine / validity check. They are heavy. Points are immutable.
347
+ // Converts Projective point to affine (x, y) coordinates.
348
+ // Can accept precomputed Z^-1 - for example, from invertBatch.
349
+ // (X, Y, Z) ∋ (x=X/Z, y=Y/Z)
350
+ const toAffineMemo = (0, utils_js_2.memoized)((p, iz) => {
351
+ const { X, Y, Z } = p;
352
+ // Fast-path for normalized points
353
+ if (Fp.eql(Z, Fp.ONE))
354
+ return { x: X, y: Y };
355
+ const is0 = p.is0();
356
+ // If invZ was 0, we return zero point. However we still want to execute
357
+ // all operations, so we replace invZ with a random number, 1.
358
+ if (iz == null)
359
+ iz = is0 ? Fp.ONE : Fp.inv(Z);
360
+ const x = Fp.mul(X, iz);
361
+ const y = Fp.mul(Y, iz);
362
+ const zz = Fp.mul(Z, iz);
363
+ if (is0)
364
+ return { x: Fp.ZERO, y: Fp.ZERO };
365
+ if (!Fp.eql(zz, Fp.ONE))
366
+ throw new Error('invZ was invalid');
367
+ return { x, y };
368
+ });
369
+ // NOTE: on exception this will crash 'cached' and no value will be set.
370
+ // Otherwise true will be return
371
+ const assertValidMemo = (0, utils_js_2.memoized)((p) => {
372
+ if (p.is0()) {
373
+ // (0, 1, 0) aka ZERO is invalid in most contexts.
374
+ // In BLS, ZERO can be serialized, so we allow it.
375
+ // (0, 0, 0) is invalid representation of ZERO.
376
+ if (extraOpts.allowInfinityPoint && !Fp.is0(p.Y))
377
+ return;
378
+ throw new Error('bad point: ZERO');
379
+ }
380
+ // Some 3rd-party test vectors require different wording between here & `fromCompressedHex`
381
+ const { x, y } = p.toAffine();
382
+ if (!Fp.isValid(x) || !Fp.isValid(y))
383
+ throw new Error('bad point: x or y not field elements');
384
+ if (!isValidXY(x, y))
385
+ throw new Error('bad point: equation left != right');
386
+ if (!p.isTorsionFree())
387
+ throw new Error('bad point: not in prime-order subgroup');
388
+ return true;
389
+ });
390
+ function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
391
+ k2p = new Point(Fp.mul(k2p.X, endoBeta), k2p.Y, k2p.Z);
392
+ k1p = (0, curve_js_1.negateCt)(k1neg, k1p);
393
+ k2p = (0, curve_js_1.negateCt)(k2neg, k2p);
394
+ return k1p.add(k2p);
395
+ }
396
+ /**
397
+ * Projective Point works in 3d / projective (homogeneous) coordinates:(X, Y, Z) ∋ (x=X/Z, y=Y/Z).
398
+ * Default Point works in 2d / affine coordinates: (x, y).
399
+ * We're doing calculations in projective, because its operations don't require costly inversion.
400
+ */
401
+ class Point {
402
+ // base / generator point
403
+ static BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
404
+ // zero / infinity / identity point
405
+ static ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO); // 0, 1, 0
406
+ // math field
407
+ static Fp = Fp;
408
+ // scalar field
409
+ static Fn = Fn;
410
+ X;
411
+ Y;
412
+ Z;
413
+ /** Does NOT validate if the point is valid. Use `.assertValidity()`. */
414
+ constructor(X, Y, Z) {
415
+ this.X = acoord('x', X);
416
+ this.Y = acoord('y', Y, true);
417
+ this.Z = acoord('z', Z);
418
+ Object.freeze(this);
419
+ }
420
+ static CURVE() {
421
+ return CURVE;
422
+ }
423
+ /** Does NOT validate if the point is valid. Use `.assertValidity()`. */
424
+ static fromAffine(p) {
425
+ const { x, y } = p || {};
426
+ if (!p || !Fp.isValid(x) || !Fp.isValid(y))
427
+ throw new Error('invalid affine point');
428
+ if (p instanceof Point)
429
+ throw new Error('projective point not allowed');
430
+ // (0, 0) would've produced (0, 0, 1) - instead, we need (0, 1, 0)
431
+ if (Fp.is0(x) && Fp.is0(y))
432
+ return Point.ZERO;
433
+ return new Point(x, y, Fp.ONE);
434
+ }
435
+ static fromBytes(bytes) {
436
+ const P = Point.fromAffine(decodePoint((0, utils_js_2.abytes)(bytes, undefined, 'point')));
437
+ P.assertValidity();
438
+ return P;
439
+ }
440
+ static fromHex(hex) {
441
+ return Point.fromBytes((0, utils_js_2.hexToBytes)(hex));
442
+ }
443
+ get x() {
444
+ return this.toAffine().x;
445
+ }
446
+ get y() {
447
+ return this.toAffine().y;
448
+ }
449
+ /**
450
+ *
451
+ * @param windowSize
452
+ * @param isLazy true will defer table computation until the first multiplication
453
+ * @returns
454
+ */
455
+ precompute(windowSize = 8, isLazy = true) {
456
+ wnaf.createCache(this, windowSize);
457
+ if (!isLazy)
458
+ this.multiply(_3n); // random number
459
+ return this;
460
+ }
461
+ // TODO: return `this`
462
+ /** A point on curve is valid if it conforms to equation. */
463
+ assertValidity() {
464
+ assertValidMemo(this);
465
+ }
466
+ hasEvenY() {
467
+ const { y } = this.toAffine();
468
+ if (!Fp.isOdd)
469
+ throw new Error("Field doesn't support isOdd");
470
+ return !Fp.isOdd(y);
471
+ }
472
+ /** Compare one point to another. */
473
+ equals(other) {
474
+ aprjpoint(other);
475
+ const { X: X1, Y: Y1, Z: Z1 } = this;
476
+ const { X: X2, Y: Y2, Z: Z2 } = other;
477
+ const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
478
+ const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
479
+ return U1 && U2;
480
+ }
481
+ /** Flips point to one corresponding to (x, -y) in Affine coordinates. */
482
+ negate() {
483
+ return new Point(this.X, Fp.neg(this.Y), this.Z);
484
+ }
485
+ // Renes-Costello-Batina exception-free doubling formula.
486
+ // There is 30% faster Jacobian formula, but it is not complete.
487
+ // https://eprint.iacr.org/2015/1060, algorithm 3
488
+ // Cost: 8M + 3S + 3*a + 2*b3 + 15add.
489
+ double() {
490
+ const { a, b } = CURVE;
491
+ const b3 = Fp.mul(b, _3n);
492
+ const { X: X1, Y: Y1, Z: Z1 } = this;
493
+ let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO; // prettier-ignore
494
+ let t0 = Fp.mul(X1, X1); // step 1
495
+ let t1 = Fp.mul(Y1, Y1);
496
+ let t2 = Fp.mul(Z1, Z1);
497
+ let t3 = Fp.mul(X1, Y1);
498
+ t3 = Fp.add(t3, t3); // step 5
499
+ Z3 = Fp.mul(X1, Z1);
500
+ Z3 = Fp.add(Z3, Z3);
501
+ X3 = Fp.mul(a, Z3);
502
+ Y3 = Fp.mul(b3, t2);
503
+ Y3 = Fp.add(X3, Y3); // step 10
504
+ X3 = Fp.sub(t1, Y3);
505
+ Y3 = Fp.add(t1, Y3);
506
+ Y3 = Fp.mul(X3, Y3);
507
+ X3 = Fp.mul(t3, X3);
508
+ Z3 = Fp.mul(b3, Z3); // step 15
509
+ t2 = Fp.mul(a, t2);
510
+ t3 = Fp.sub(t0, t2);
511
+ t3 = Fp.mul(a, t3);
512
+ t3 = Fp.add(t3, Z3);
513
+ Z3 = Fp.add(t0, t0); // step 20
514
+ t0 = Fp.add(Z3, t0);
515
+ t0 = Fp.add(t0, t2);
516
+ t0 = Fp.mul(t0, t3);
517
+ Y3 = Fp.add(Y3, t0);
518
+ t2 = Fp.mul(Y1, Z1); // step 25
519
+ t2 = Fp.add(t2, t2);
520
+ t0 = Fp.mul(t2, t3);
521
+ X3 = Fp.sub(X3, t0);
522
+ Z3 = Fp.mul(t2, t1);
523
+ Z3 = Fp.add(Z3, Z3); // step 30
524
+ Z3 = Fp.add(Z3, Z3);
525
+ return new Point(X3, Y3, Z3);
526
+ }
527
+ // Renes-Costello-Batina exception-free addition formula.
528
+ // There is 30% faster Jacobian formula, but it is not complete.
529
+ // https://eprint.iacr.org/2015/1060, algorithm 1
530
+ // Cost: 12M + 0S + 3*a + 3*b3 + 23add.
531
+ add(other) {
532
+ aprjpoint(other);
533
+ const { X: X1, Y: Y1, Z: Z1 } = this;
534
+ const { X: X2, Y: Y2, Z: Z2 } = other;
535
+ let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO; // prettier-ignore
536
+ const a = CURVE.a;
537
+ const b3 = Fp.mul(CURVE.b, _3n);
538
+ let t0 = Fp.mul(X1, X2); // step 1
539
+ let t1 = Fp.mul(Y1, Y2);
540
+ let t2 = Fp.mul(Z1, Z2);
541
+ let t3 = Fp.add(X1, Y1);
542
+ let t4 = Fp.add(X2, Y2); // step 5
543
+ t3 = Fp.mul(t3, t4);
544
+ t4 = Fp.add(t0, t1);
545
+ t3 = Fp.sub(t3, t4);
546
+ t4 = Fp.add(X1, Z1);
547
+ let t5 = Fp.add(X2, Z2); // step 10
548
+ t4 = Fp.mul(t4, t5);
549
+ t5 = Fp.add(t0, t2);
550
+ t4 = Fp.sub(t4, t5);
551
+ t5 = Fp.add(Y1, Z1);
552
+ X3 = Fp.add(Y2, Z2); // step 15
553
+ t5 = Fp.mul(t5, X3);
554
+ X3 = Fp.add(t1, t2);
555
+ t5 = Fp.sub(t5, X3);
556
+ Z3 = Fp.mul(a, t4);
557
+ X3 = Fp.mul(b3, t2); // step 20
558
+ Z3 = Fp.add(X3, Z3);
559
+ X3 = Fp.sub(t1, Z3);
560
+ Z3 = Fp.add(t1, Z3);
561
+ Y3 = Fp.mul(X3, Z3);
562
+ t1 = Fp.add(t0, t0); // step 25
563
+ t1 = Fp.add(t1, t0);
564
+ t2 = Fp.mul(a, t2);
565
+ t4 = Fp.mul(b3, t4);
566
+ t1 = Fp.add(t1, t2);
567
+ t2 = Fp.sub(t0, t2); // step 30
568
+ t2 = Fp.mul(a, t2);
569
+ t4 = Fp.add(t4, t2);
570
+ t0 = Fp.mul(t1, t4);
571
+ Y3 = Fp.add(Y3, t0);
572
+ t0 = Fp.mul(t5, t4); // step 35
573
+ X3 = Fp.mul(t3, X3);
574
+ X3 = Fp.sub(X3, t0);
575
+ t0 = Fp.mul(t3, t1);
576
+ Z3 = Fp.mul(t5, Z3);
577
+ Z3 = Fp.add(Z3, t0); // step 40
578
+ return new Point(X3, Y3, Z3);
579
+ }
580
+ subtract(other) {
581
+ return this.add(other.negate());
582
+ }
583
+ is0() {
584
+ return this.equals(Point.ZERO);
585
+ }
586
+ /**
587
+ * Constant time multiplication.
588
+ * Uses wNAF method. Windowed method may be 10% faster,
589
+ * but takes 2x longer to generate and consumes 2x memory.
590
+ * Uses precomputes when available.
591
+ * Uses endomorphism for Koblitz curves.
592
+ * @param scalar by which the point would be multiplied
593
+ * @returns New point
594
+ */
595
+ multiply(scalar) {
596
+ const { endo } = extraOpts;
597
+ if (!Fn.isValidNot0(scalar))
598
+ throw new Error('invalid scalar: out of range'); // 0 is invalid
599
+ let point, fake; // Fake point is used to const-time mult
600
+ const mul = (n) => wnaf.cached(this, n, (p) => (0, curve_js_1.normalizeZ)(Point, p));
601
+ /** See docs for {@link EndomorphismOpts} */
602
+ if (endo) {
603
+ const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(scalar);
604
+ const { p: k1p, f: k1f } = mul(k1);
605
+ const { p: k2p, f: k2f } = mul(k2);
606
+ fake = k1f.add(k2f);
607
+ point = finishEndo(endo.beta, k1p, k2p, k1neg, k2neg);
608
+ }
609
+ else {
610
+ const { p, f } = mul(scalar);
611
+ point = p;
612
+ fake = f;
613
+ }
614
+ // Normalize `z` for both points, but return only real one
615
+ return (0, curve_js_1.normalizeZ)(Point, [point, fake])[0];
616
+ }
617
+ /**
618
+ * Non-constant-time multiplication. Uses double-and-add algorithm.
619
+ * It's faster, but should only be used when you don't care about
620
+ * an exposed secret key e.g. sig verification, which works over *public* keys.
621
+ */
622
+ multiplyUnsafe(sc) {
623
+ const { endo } = extraOpts;
624
+ const p = this;
625
+ if (!Fn.isValid(sc))
626
+ throw new Error('invalid scalar: out of range'); // 0 is valid
627
+ if (sc === _0n || p.is0())
628
+ return Point.ZERO; // 0
629
+ if (sc === _1n)
630
+ return p; // 1
631
+ if (wnaf.hasCache(this))
632
+ return this.multiply(sc); // precomputes
633
+ // We don't have method for double scalar multiplication (aP + bQ):
634
+ // Even with using Strauss-Shamir trick, it's 35% slower than naïve mul+add.
635
+ if (endo) {
636
+ const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(sc);
637
+ const { p1, p2 } = (0, curve_js_1.mulEndoUnsafe)(Point, p, k1, k2); // 30% faster vs wnaf.unsafe
638
+ return finishEndo(endo.beta, p1, p2, k1neg, k2neg);
639
+ }
640
+ else {
641
+ return wnaf.unsafe(p, sc);
642
+ }
643
+ }
644
+ /**
645
+ * Converts Projective point to affine (x, y) coordinates.
646
+ * @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
647
+ */
648
+ toAffine(invertedZ) {
649
+ return toAffineMemo(this, invertedZ);
650
+ }
651
+ /**
652
+ * Checks whether Point is free of torsion elements (is in prime subgroup).
653
+ * Always torsion-free for cofactor=1 curves.
654
+ */
655
+ isTorsionFree() {
656
+ const { isTorsionFree } = extraOpts;
657
+ if (cofactor === _1n)
658
+ return true;
659
+ if (isTorsionFree)
660
+ return isTorsionFree(Point, this);
661
+ return wnaf.unsafe(this, CURVE_ORDER).is0();
662
+ }
663
+ clearCofactor() {
664
+ const { clearCofactor } = extraOpts;
665
+ if (cofactor === _1n)
666
+ return this; // Fast-path
667
+ if (clearCofactor)
668
+ return clearCofactor(Point, this);
669
+ return this.multiplyUnsafe(cofactor);
670
+ }
671
+ isSmallOrder() {
672
+ // can we use this.clearCofactor()?
673
+ return this.multiplyUnsafe(cofactor).is0();
674
+ }
675
+ toBytes(isCompressed = true) {
676
+ (0, utils_js_2.abool)(isCompressed, 'isCompressed');
677
+ this.assertValidity();
678
+ return encodePoint(Point, this, isCompressed);
679
+ }
680
+ toHex(isCompressed = true) {
681
+ return (0, utils_js_2.bytesToHex)(this.toBytes(isCompressed));
682
+ }
683
+ toString() {
684
+ return `<Point ${this.is0() ? 'ZERO' : this.toHex()}>`;
685
+ }
686
+ }
687
+ const bits = Fn.BITS;
688
+ const wnaf = new curve_js_1.wNAF(Point, extraOpts.endo ? Math.ceil(bits / 2) : bits);
689
+ Point.BASE.precompute(8); // Enable precomputes. Slows down first publicKey computation by 20ms.
690
+ return Point;
691
+ }
692
+ // Points start with byte 0x02 when y is even; otherwise 0x03
693
+ function pprefix(hasEvenY) {
694
+ return Uint8Array.of(hasEvenY ? 0x02 : 0x03);
695
+ }
696
+ /**
697
+ * Implementation of the Shallue and van de Woestijne method for any weierstrass curve.
698
+ * TODO: check if there is a way to merge this with uvRatio in Edwards; move to modular.
699
+ * b = True and y = sqrt(u / v) if (u / v) is square in F, and
700
+ * b = False and y = sqrt(Z * (u / v)) otherwise.
701
+ * @param Fp
702
+ * @param Z
703
+ * @returns
704
+ */
705
+ function SWUFpSqrtRatio(Fp, Z) {
706
+ // Generic implementation
707
+ const q = Fp.ORDER;
708
+ let l = _0n;
709
+ for (let o = q - _1n; o % _2n === _0n; o /= _2n)
710
+ l += _1n;
711
+ const c1 = l; // 1. c1, the largest integer such that 2^c1 divides q - 1.
712
+ // We need 2n ** c1 and 2n ** (c1-1). We can't use **; but we can use <<.
713
+ // 2n ** c1 == 2n << (c1-1)
714
+ const _2n_pow_c1_1 = _2n << (c1 - _1n - _1n);
715
+ const _2n_pow_c1 = _2n_pow_c1_1 * _2n;
716
+ const c2 = (q - _1n) / _2n_pow_c1; // 2. c2 = (q - 1) / (2^c1) # Integer arithmetic
717
+ const c3 = (c2 - _1n) / _2n; // 3. c3 = (c2 - 1) / 2 # Integer arithmetic
718
+ const c4 = _2n_pow_c1 - _1n; // 4. c4 = 2^c1 - 1 # Integer arithmetic
719
+ const c5 = _2n_pow_c1_1; // 5. c5 = 2^(c1 - 1) # Integer arithmetic
720
+ const c6 = Fp.pow(Z, c2); // 6. c6 = Z^c2
721
+ const c7 = Fp.pow(Z, (c2 + _1n) / _2n); // 7. c7 = Z^((c2 + 1) / 2)
722
+ let sqrtRatio = (u, v) => {
723
+ let tv1 = c6; // 1. tv1 = c6
724
+ let tv2 = Fp.pow(v, c4); // 2. tv2 = v^c4
725
+ let tv3 = Fp.sqr(tv2); // 3. tv3 = tv2^2
726
+ tv3 = Fp.mul(tv3, v); // 4. tv3 = tv3 * v
727
+ let tv5 = Fp.mul(u, tv3); // 5. tv5 = u * tv3
728
+ tv5 = Fp.pow(tv5, c3); // 6. tv5 = tv5^c3
729
+ tv5 = Fp.mul(tv5, tv2); // 7. tv5 = tv5 * tv2
730
+ tv2 = Fp.mul(tv5, v); // 8. tv2 = tv5 * v
731
+ tv3 = Fp.mul(tv5, u); // 9. tv3 = tv5 * u
732
+ let tv4 = Fp.mul(tv3, tv2); // 10. tv4 = tv3 * tv2
733
+ tv5 = Fp.pow(tv4, c5); // 11. tv5 = tv4^c5
734
+ let isQR = Fp.eql(tv5, Fp.ONE); // 12. isQR = tv5 == 1
735
+ tv2 = Fp.mul(tv3, c7); // 13. tv2 = tv3 * c7
736
+ tv5 = Fp.mul(tv4, tv1); // 14. tv5 = tv4 * tv1
737
+ tv3 = Fp.cmov(tv2, tv3, isQR); // 15. tv3 = CMOV(tv2, tv3, isQR)
738
+ tv4 = Fp.cmov(tv5, tv4, isQR); // 16. tv4 = CMOV(tv5, tv4, isQR)
739
+ // 17. for i in (c1, c1 - 1, ..., 2):
740
+ for (let i = c1; i > _1n; i--) {
741
+ let tv5 = i - _2n; // 18. tv5 = i - 2
742
+ tv5 = _2n << (tv5 - _1n); // 19. tv5 = 2^tv5
743
+ let tvv5 = Fp.pow(tv4, tv5); // 20. tv5 = tv4^tv5
744
+ const e1 = Fp.eql(tvv5, Fp.ONE); // 21. e1 = tv5 == 1
745
+ tv2 = Fp.mul(tv3, tv1); // 22. tv2 = tv3 * tv1
746
+ tv1 = Fp.mul(tv1, tv1); // 23. tv1 = tv1 * tv1
747
+ tvv5 = Fp.mul(tv4, tv1); // 24. tv5 = tv4 * tv1
748
+ tv3 = Fp.cmov(tv2, tv3, e1); // 25. tv3 = CMOV(tv2, tv3, e1)
749
+ tv4 = Fp.cmov(tvv5, tv4, e1); // 26. tv4 = CMOV(tv5, tv4, e1)
750
+ }
751
+ return { isValid: isQR, value: tv3 };
752
+ };
753
+ if (Fp.ORDER % _4n === _3n) {
754
+ // sqrt_ratio_3mod4(u, v)
755
+ const c1 = (Fp.ORDER - _3n) / _4n; // 1. c1 = (q - 3) / 4 # Integer arithmetic
756
+ const c2 = Fp.sqrt(Fp.neg(Z)); // 2. c2 = sqrt(-Z)
757
+ sqrtRatio = (u, v) => {
758
+ let tv1 = Fp.sqr(v); // 1. tv1 = v^2
759
+ const tv2 = Fp.mul(u, v); // 2. tv2 = u * v
760
+ tv1 = Fp.mul(tv1, tv2); // 3. tv1 = tv1 * tv2
761
+ let y1 = Fp.pow(tv1, c1); // 4. y1 = tv1^c1
762
+ y1 = Fp.mul(y1, tv2); // 5. y1 = y1 * tv2
763
+ const y2 = Fp.mul(y1, c2); // 6. y2 = y1 * c2
764
+ const tv3 = Fp.mul(Fp.sqr(y1), v); // 7. tv3 = y1^2; 8. tv3 = tv3 * v
765
+ const isQR = Fp.eql(tv3, u); // 9. isQR = tv3 == u
766
+ let y = Fp.cmov(y2, y1, isQR); // 10. y = CMOV(y2, y1, isQR)
767
+ return { isValid: isQR, value: y }; // 11. return (isQR, y) isQR ? y : y*c2
768
+ };
769
+ }
770
+ // No curves uses that
771
+ // if (Fp.ORDER % _8n === _5n) // sqrt_ratio_5mod8
772
+ return sqrtRatio;
773
+ }
774
+ /**
775
+ * Simplified Shallue-van de Woestijne-Ulas Method
776
+ * https://www.rfc-editor.org/rfc/rfc9380#section-6.6.2
777
+ */
778
+ function mapToCurveSimpleSWU(Fp, opts) {
779
+ (0, modular_js_1.validateField)(Fp);
780
+ const { A, B, Z } = opts;
781
+ if (!Fp.isValid(A) || !Fp.isValid(B) || !Fp.isValid(Z))
782
+ throw new Error('mapToCurveSimpleSWU: invalid opts');
783
+ const sqrtRatio = SWUFpSqrtRatio(Fp, Z);
784
+ if (!Fp.isOdd)
785
+ throw new Error('Field does not have .isOdd()');
786
+ // Input: u, an element of F.
787
+ // Output: (x, y), a point on E.
788
+ return (u) => {
789
+ // prettier-ignore
790
+ let tv1, tv2, tv3, tv4, tv5, tv6, x, y;
791
+ tv1 = Fp.sqr(u); // 1. tv1 = u^2
792
+ tv1 = Fp.mul(tv1, Z); // 2. tv1 = Z * tv1
793
+ tv2 = Fp.sqr(tv1); // 3. tv2 = tv1^2
794
+ tv2 = Fp.add(tv2, tv1); // 4. tv2 = tv2 + tv1
795
+ tv3 = Fp.add(tv2, Fp.ONE); // 5. tv3 = tv2 + 1
796
+ tv3 = Fp.mul(tv3, B); // 6. tv3 = B * tv3
797
+ tv4 = Fp.cmov(Z, Fp.neg(tv2), !Fp.eql(tv2, Fp.ZERO)); // 7. tv4 = CMOV(Z, -tv2, tv2 != 0)
798
+ tv4 = Fp.mul(tv4, A); // 8. tv4 = A * tv4
799
+ tv2 = Fp.sqr(tv3); // 9. tv2 = tv3^2
800
+ tv6 = Fp.sqr(tv4); // 10. tv6 = tv4^2
801
+ tv5 = Fp.mul(tv6, A); // 11. tv5 = A * tv6
802
+ tv2 = Fp.add(tv2, tv5); // 12. tv2 = tv2 + tv5
803
+ tv2 = Fp.mul(tv2, tv3); // 13. tv2 = tv2 * tv3
804
+ tv6 = Fp.mul(tv6, tv4); // 14. tv6 = tv6 * tv4
805
+ tv5 = Fp.mul(tv6, B); // 15. tv5 = B * tv6
806
+ tv2 = Fp.add(tv2, tv5); // 16. tv2 = tv2 + tv5
807
+ x = Fp.mul(tv1, tv3); // 17. x = tv1 * tv3
808
+ const { isValid, value } = sqrtRatio(tv2, tv6); // 18. (is_gx1_square, y1) = sqrt_ratio(tv2, tv6)
809
+ y = Fp.mul(tv1, u); // 19. y = tv1 * u -> Z * u^3 * y1
810
+ y = Fp.mul(y, value); // 20. y = y * y1
811
+ x = Fp.cmov(x, tv3, isValid); // 21. x = CMOV(x, tv3, is_gx1_square)
812
+ y = Fp.cmov(y, value, isValid); // 22. y = CMOV(y, y1, is_gx1_square)
813
+ const e1 = Fp.isOdd(u) === Fp.isOdd(y); // 23. e1 = sgn0(u) == sgn0(y)
814
+ y = Fp.cmov(Fp.neg(y), y, e1); // 24. y = CMOV(-y, y, e1)
815
+ const tv4_inv = (0, modular_js_1.FpInvertBatch)(Fp, [tv4], true)[0];
816
+ x = Fp.mul(x, tv4_inv); // 25. x = x / tv4
817
+ return { x, y };
818
+ };
819
+ }
820
+ function getWLengths(Fp, Fn) {
821
+ return {
822
+ secretKey: Fn.BYTES,
823
+ publicKey: 1 + Fp.BYTES,
824
+ publicKeyUncompressed: 1 + 2 * Fp.BYTES,
825
+ publicKeyHasPrefix: true,
826
+ signature: 2 * Fn.BYTES,
827
+ };
828
+ }
829
+ /**
830
+ * Sometimes users only need getPublicKey, getSharedSecret, and secret key handling.
831
+ * This helper ensures no signature functionality is present. Less code, smaller bundle size.
832
+ */
833
+ function ecdh(Point, ecdhOpts = {}) {
834
+ const { Fn } = Point;
835
+ const randomBytes_ = ecdhOpts.randomBytes || utils_js_2.randomBytes;
836
+ const lengths = Object.assign(getWLengths(Point.Fp, Fn), { seed: (0, modular_js_1.getMinHashLength)(Fn.ORDER) });
837
+ function isValidSecretKey(secretKey) {
838
+ try {
839
+ const num = Fn.fromBytes(secretKey);
840
+ return Fn.isValidNot0(num);
841
+ }
842
+ catch (error) {
843
+ return false;
844
+ }
845
+ }
846
+ function isValidPublicKey(publicKey, isCompressed) {
847
+ const { publicKey: comp, publicKeyUncompressed } = lengths;
848
+ try {
849
+ const l = publicKey.length;
850
+ if (isCompressed === true && l !== comp)
851
+ return false;
852
+ if (isCompressed === false && l !== publicKeyUncompressed)
853
+ return false;
854
+ return !!Point.fromBytes(publicKey);
855
+ }
856
+ catch (error) {
857
+ return false;
858
+ }
859
+ }
860
+ /**
861
+ * Produces cryptographically secure secret key from random of size
862
+ * (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
863
+ */
864
+ function randomSecretKey(seed = randomBytes_(lengths.seed)) {
865
+ return (0, modular_js_1.mapHashToField)((0, utils_js_2.abytes)(seed, lengths.seed, 'seed'), Fn.ORDER);
866
+ }
867
+ /**
868
+ * Computes public key for a secret key. Checks for validity of the secret key.
869
+ * @param isCompressed whether to return compact (default), or full key
870
+ * @returns Public key, full when isCompressed=false; short when isCompressed=true
871
+ */
872
+ function getPublicKey(secretKey, isCompressed = true) {
873
+ return Point.BASE.multiply(Fn.fromBytes(secretKey)).toBytes(isCompressed);
874
+ }
875
+ /**
876
+ * Quick and dirty check for item being public key. Does not validate hex, or being on-curve.
877
+ */
878
+ function isProbPub(item) {
879
+ const { secretKey, publicKey, publicKeyUncompressed } = lengths;
880
+ if (!(0, utils_js_2.isBytes)(item))
881
+ return undefined;
882
+ if (('_lengths' in Fn && Fn._lengths) || secretKey === publicKey)
883
+ return undefined;
884
+ const l = (0, utils_js_2.abytes)(item, undefined, 'key').length;
885
+ return l === publicKey || l === publicKeyUncompressed;
886
+ }
887
+ /**
888
+ * ECDH (Elliptic Curve Diffie Hellman).
889
+ * Computes shared public key from secret key A and public key B.
890
+ * Checks: 1) secret key validity 2) shared key is on-curve.
891
+ * Does NOT hash the result.
892
+ * @param isCompressed whether to return compact (default), or full key
893
+ * @returns shared public key
894
+ */
895
+ function getSharedSecret(secretKeyA, publicKeyB, isCompressed = true) {
896
+ if (isProbPub(secretKeyA) === true)
897
+ throw new Error('first arg must be private key');
898
+ if (isProbPub(publicKeyB) === false)
899
+ throw new Error('second arg must be public key');
900
+ const s = Fn.fromBytes(secretKeyA);
901
+ const b = Point.fromBytes(publicKeyB); // checks for being on-curve
902
+ return b.multiply(s).toBytes(isCompressed);
903
+ }
904
+ const utils = {
905
+ isValidSecretKey,
906
+ isValidPublicKey,
907
+ randomSecretKey,
908
+ };
909
+ const keygen = (0, curve_js_1.createKeygen)(randomSecretKey, getPublicKey);
910
+ return Object.freeze({ getPublicKey, getSharedSecret, keygen, Point, utils, lengths });
911
+ }
912
+ /**
913
+ * Creates ECDSA signing interface for given elliptic curve `Point` and `hash` function.
914
+ *
915
+ * @param Point created using {@link weierstrass} function
916
+ * @param hash used for 1) message prehash-ing 2) k generation in `sign`, using hmac_drbg(hash)
917
+ * @param ecdsaOpts rarely needed, see {@link ECDSAOpts}
918
+ *
919
+ * @example
920
+ * ```js
921
+ * const p256_Point = weierstrass(...);
922
+ * const p256_sha256 = ecdsa(p256_Point, sha256);
923
+ * const p256_sha224 = ecdsa(p256_Point, sha224);
924
+ * const p256_sha224_r = ecdsa(p256_Point, sha224, { randomBytes: (length) => { ... } });
925
+ * ```
926
+ */
927
+ function ecdsa(Point, hash, ecdsaOpts = {}) {
928
+ (0, utils_js_1.ahash)(hash);
929
+ (0, utils_js_2.validateObject)(ecdsaOpts, {}, {
930
+ hmac: 'function',
931
+ lowS: 'boolean',
932
+ randomBytes: 'function',
933
+ bits2int: 'function',
934
+ bits2int_modN: 'function',
935
+ });
936
+ ecdsaOpts = Object.assign({}, ecdsaOpts);
937
+ const randomBytes = ecdsaOpts.randomBytes || utils_js_2.randomBytes;
938
+ const hmac = ecdsaOpts.hmac || ((key, msg) => (0, hmac_js_1.hmac)(hash, key, msg));
939
+ const { Fp, Fn } = Point;
940
+ const { ORDER: CURVE_ORDER, BITS: fnBits } = Fn;
941
+ const { keygen, getPublicKey, getSharedSecret, utils, lengths } = ecdh(Point, ecdsaOpts);
942
+ const defaultSigOpts = {
943
+ prehash: true,
944
+ lowS: typeof ecdsaOpts.lowS === 'boolean' ? ecdsaOpts.lowS : true,
945
+ format: 'compact',
946
+ extraEntropy: false,
947
+ };
948
+ const hasLargeCofactor = CURVE_ORDER * _2n < Fp.ORDER; // Won't CURVE().h > 2n be more effective?
949
+ function isBiggerThanHalfOrder(number) {
950
+ const HALF = CURVE_ORDER >> _1n;
951
+ return number > HALF;
952
+ }
953
+ function validateRS(title, num) {
954
+ if (!Fn.isValidNot0(num))
955
+ throw new Error(`invalid signature ${title}: out of range 1..Point.Fn.ORDER`);
956
+ return num;
957
+ }
958
+ function assertSmallCofactor() {
959
+ // ECDSA recovery is hard for cofactor > 1 curves.
960
+ // In sign, `r = q.x mod n`, and here we recover q.x from r.
961
+ // While recovering q.x >= n, we need to add r+n for cofactor=1 curves.
962
+ // However, for cofactor>1, r+n may not get q.x:
963
+ // r+n*i would need to be done instead where i is unknown.
964
+ // To easily get i, we either need to:
965
+ // a. increase amount of valid recid values (4, 5...); OR
966
+ // b. prohibit non-prime-order signatures (recid > 1).
967
+ if (hasLargeCofactor)
968
+ throw new Error('"recovered" sig type is not supported for cofactor >2 curves');
969
+ }
970
+ function validateSigLength(bytes, format) {
971
+ validateSigFormat(format);
972
+ const size = lengths.signature;
973
+ const sizer = format === 'compact' ? size : format === 'recovered' ? size + 1 : undefined;
974
+ return (0, utils_js_2.abytes)(bytes, sizer);
975
+ }
976
+ /**
977
+ * ECDSA signature with its (r, s) properties. Supports compact, recovered & DER representations.
978
+ */
979
+ class Signature {
980
+ r;
981
+ s;
982
+ recovery;
983
+ constructor(r, s, recovery) {
984
+ this.r = validateRS('r', r); // r in [1..N-1];
985
+ this.s = validateRS('s', s); // s in [1..N-1];
986
+ if (recovery != null) {
987
+ assertSmallCofactor();
988
+ if (![0, 1, 2, 3].includes(recovery))
989
+ throw new Error('invalid recovery id');
990
+ this.recovery = recovery;
991
+ }
992
+ Object.freeze(this);
993
+ }
994
+ static fromBytes(bytes, format = defaultSigOpts.format) {
995
+ validateSigLength(bytes, format);
996
+ let recid;
997
+ if (format === 'der') {
998
+ const { r, s } = exports.DER.toSig((0, utils_js_2.abytes)(bytes));
999
+ return new Signature(r, s);
1000
+ }
1001
+ if (format === 'recovered') {
1002
+ recid = bytes[0];
1003
+ format = 'compact';
1004
+ bytes = bytes.subarray(1);
1005
+ }
1006
+ const L = lengths.signature / 2;
1007
+ const r = bytes.subarray(0, L);
1008
+ const s = bytes.subarray(L, L * 2);
1009
+ return new Signature(Fn.fromBytes(r), Fn.fromBytes(s), recid);
1010
+ }
1011
+ static fromHex(hex, format) {
1012
+ return this.fromBytes((0, utils_js_2.hexToBytes)(hex), format);
1013
+ }
1014
+ assertRecovery() {
1015
+ const { recovery } = this;
1016
+ if (recovery == null)
1017
+ throw new Error('invalid recovery id: must be present');
1018
+ return recovery;
1019
+ }
1020
+ addRecoveryBit(recovery) {
1021
+ return new Signature(this.r, this.s, recovery);
1022
+ }
1023
+ recoverPublicKey(messageHash) {
1024
+ const { r, s } = this;
1025
+ const recovery = this.assertRecovery();
1026
+ const radj = recovery === 2 || recovery === 3 ? r + CURVE_ORDER : r;
1027
+ if (!Fp.isValid(radj))
1028
+ throw new Error('invalid recovery id: sig.r+curve.n != R.x');
1029
+ const x = Fp.toBytes(radj);
1030
+ const R = Point.fromBytes((0, utils_js_2.concatBytes)(pprefix((recovery & 1) === 0), x));
1031
+ const ir = Fn.inv(radj); // r^-1
1032
+ const h = bits2int_modN((0, utils_js_2.abytes)(messageHash, undefined, 'msgHash')); // Truncate hash
1033
+ const u1 = Fn.create(-h * ir); // -hr^-1
1034
+ const u2 = Fn.create(s * ir); // sr^-1
1035
+ // (sr^-1)R-(hr^-1)G = -(hr^-1)G + (sr^-1). unsafe is fine: there is no private data.
1036
+ const Q = Point.BASE.multiplyUnsafe(u1).add(R.multiplyUnsafe(u2));
1037
+ if (Q.is0())
1038
+ throw new Error('invalid recovery: point at infinify');
1039
+ Q.assertValidity();
1040
+ return Q;
1041
+ }
1042
+ // Signatures should be low-s, to prevent malleability.
1043
+ hasHighS() {
1044
+ return isBiggerThanHalfOrder(this.s);
1045
+ }
1046
+ toBytes(format = defaultSigOpts.format) {
1047
+ validateSigFormat(format);
1048
+ if (format === 'der')
1049
+ return (0, utils_js_2.hexToBytes)(exports.DER.hexFromSig(this));
1050
+ const { r, s } = this;
1051
+ const rb = Fn.toBytes(r);
1052
+ const sb = Fn.toBytes(s);
1053
+ if (format === 'recovered') {
1054
+ assertSmallCofactor();
1055
+ return (0, utils_js_2.concatBytes)(Uint8Array.of(this.assertRecovery()), rb, sb);
1056
+ }
1057
+ return (0, utils_js_2.concatBytes)(rb, sb);
1058
+ }
1059
+ toHex(format) {
1060
+ return (0, utils_js_2.bytesToHex)(this.toBytes(format));
1061
+ }
1062
+ }
1063
+ // RFC6979: ensure ECDSA msg is X bytes and < N. RFC suggests optional truncating via bits2octets.
1064
+ // FIPS 186-4 4.6 suggests the leftmost min(nBitLen, outLen) bits, which matches bits2int.
1065
+ // bits2int can produce res>N, we can do mod(res, N) since the bitLen is the same.
1066
+ // int2octets can't be used; pads small msgs with 0: unacceptatble for trunc as per RFC vectors
1067
+ const bits2int = ecdsaOpts.bits2int ||
1068
+ function bits2int_def(bytes) {
1069
+ // Our custom check "just in case", for protection against DoS
1070
+ if (bytes.length > 8192)
1071
+ throw new Error('input is too large');
1072
+ // For curves with nBitLength % 8 !== 0: bits2octets(bits2octets(m)) !== bits2octets(m)
1073
+ // for some cases, since bytes.length * 8 is not actual bitLength.
1074
+ const num = (0, utils_js_2.bytesToNumberBE)(bytes); // check for == u8 done here
1075
+ const delta = bytes.length * 8 - fnBits; // truncate to nBitLength leftmost bits
1076
+ return delta > 0 ? num >> BigInt(delta) : num;
1077
+ };
1078
+ const bits2int_modN = ecdsaOpts.bits2int_modN ||
1079
+ function bits2int_modN_def(bytes) {
1080
+ return Fn.create(bits2int(bytes)); // can't use bytesToNumberBE here
1081
+ };
1082
+ // Pads output with zero as per spec
1083
+ const ORDER_MASK = (0, utils_js_2.bitMask)(fnBits);
1084
+ /** Converts to bytes. Checks if num in `[0..ORDER_MASK-1]` e.g.: `[0..2^256-1]`. */
1085
+ function int2octets(num) {
1086
+ // IMPORTANT: the check ensures working for case `Fn.BYTES != Fn.BITS * 8`
1087
+ (0, utils_js_2.aInRange)('num < 2^' + fnBits, num, _0n, ORDER_MASK);
1088
+ return Fn.toBytes(num);
1089
+ }
1090
+ function validateMsgAndHash(message, prehash) {
1091
+ (0, utils_js_2.abytes)(message, undefined, 'message');
1092
+ return prehash ? (0, utils_js_2.abytes)(hash(message), undefined, 'prehashed message') : message;
1093
+ }
1094
+ /**
1095
+ * Steps A, D of RFC6979 3.2.
1096
+ * Creates RFC6979 seed; converts msg/privKey to numbers.
1097
+ * Used only in sign, not in verify.
1098
+ *
1099
+ * Warning: we cannot assume here that message has same amount of bytes as curve order,
1100
+ * this will be invalid at least for P521. Also it can be bigger for P224 + SHA256.
1101
+ */
1102
+ function prepSig(message, secretKey, opts) {
1103
+ const { lowS, prehash, extraEntropy } = validateSigOpts(opts, defaultSigOpts);
1104
+ message = validateMsgAndHash(message, prehash); // RFC6979 3.2 A: h1 = H(m)
1105
+ // We can't later call bits2octets, since nested bits2int is broken for curves
1106
+ // with fnBits % 8 !== 0. Because of that, we unwrap it here as int2octets call.
1107
+ // const bits2octets = (bits) => int2octets(bits2int_modN(bits))
1108
+ const h1int = bits2int_modN(message);
1109
+ const d = Fn.fromBytes(secretKey); // validate secret key, convert to bigint
1110
+ if (!Fn.isValidNot0(d))
1111
+ throw new Error('invalid private key');
1112
+ const seedArgs = [int2octets(d), int2octets(h1int)];
1113
+ // extraEntropy. RFC6979 3.6: additional k' (optional).
1114
+ if (extraEntropy != null && extraEntropy !== false) {
1115
+ // K = HMAC_K(V || 0x00 || int2octets(x) || bits2octets(h1) || k')
1116
+ // gen random bytes OR pass as-is
1117
+ const e = extraEntropy === true ? randomBytes(lengths.secretKey) : extraEntropy;
1118
+ seedArgs.push((0, utils_js_2.abytes)(e, undefined, 'extraEntropy')); // check for being bytes
1119
+ }
1120
+ const seed = (0, utils_js_2.concatBytes)(...seedArgs); // Step D of RFC6979 3.2
1121
+ const m = h1int; // no need to call bits2int second time here, it is inside truncateHash!
1122
+ // Converts signature params into point w r/s, checks result for validity.
1123
+ // To transform k => Signature:
1124
+ // q = k⋅G
1125
+ // r = q.x mod n
1126
+ // s = k^-1(m + rd) mod n
1127
+ // Can use scalar blinding b^-1(bm + bdr) where b ∈ [1,q−1] according to
1128
+ // https://tches.iacr.org/index.php/TCHES/article/view/7337/6509. We've decided against it:
1129
+ // a) dependency on CSPRNG b) 15% slowdown c) doesn't really help since bigints are not CT
1130
+ function k2sig(kBytes) {
1131
+ // RFC 6979 Section 3.2, step 3: k = bits2int(T)
1132
+ // Important: all mod() calls here must be done over N
1133
+ const k = bits2int(kBytes); // Cannot use fields methods, since it is group element
1134
+ if (!Fn.isValidNot0(k))
1135
+ return; // Valid scalars (including k) must be in 1..N-1
1136
+ const ik = Fn.inv(k); // k^-1 mod n
1137
+ const q = Point.BASE.multiply(k).toAffine(); // q = k⋅G
1138
+ const r = Fn.create(q.x); // r = q.x mod n
1139
+ if (r === _0n)
1140
+ return;
1141
+ const s = Fn.create(ik * Fn.create(m + r * d)); // s = k^-1(m + rd) mod n
1142
+ if (s === _0n)
1143
+ return;
1144
+ let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n); // recovery bit (2 or 3 when q.x>n)
1145
+ let normS = s;
1146
+ if (lowS && isBiggerThanHalfOrder(s)) {
1147
+ normS = Fn.neg(s); // if lowS was passed, ensure s is always in the bottom half of N
1148
+ recovery ^= 1;
1149
+ }
1150
+ return new Signature(r, normS, hasLargeCofactor ? undefined : recovery);
1151
+ }
1152
+ return { seed, k2sig };
1153
+ }
1154
+ /**
1155
+ * Signs message hash with a secret key.
1156
+ *
1157
+ * ```
1158
+ * sign(m, d) where
1159
+ * k = rfc6979_hmac_drbg(m, d)
1160
+ * (x, y) = G × k
1161
+ * r = x mod n
1162
+ * s = (m + dr) / k mod n
1163
+ * ```
1164
+ */
1165
+ function sign(message, secretKey, opts = {}) {
1166
+ const { seed, k2sig } = prepSig(message, secretKey, opts); // Steps A, D of RFC6979 3.2.
1167
+ const drbg = (0, utils_js_2.createHmacDrbg)(hash.outputLen, Fn.BYTES, hmac);
1168
+ const sig = drbg(seed, k2sig); // Steps B, C, D, E, F, G
1169
+ return sig.toBytes(opts.format);
1170
+ }
1171
+ /**
1172
+ * Verifies a signature against message and public key.
1173
+ * Rejects lowS signatures by default: see {@link ECDSAVerifyOpts}.
1174
+ * Implements section 4.1.4 from https://www.secg.org/sec1-v2.pdf:
1175
+ *
1176
+ * ```
1177
+ * verify(r, s, h, P) where
1178
+ * u1 = hs^-1 mod n
1179
+ * u2 = rs^-1 mod n
1180
+ * R = u1⋅G + u2⋅P
1181
+ * mod(R.x, n) == r
1182
+ * ```
1183
+ */
1184
+ function verify(signature, message, publicKey, opts = {}) {
1185
+ const { lowS, prehash, format } = validateSigOpts(opts, defaultSigOpts);
1186
+ publicKey = (0, utils_js_2.abytes)(publicKey, undefined, 'publicKey');
1187
+ message = validateMsgAndHash(message, prehash);
1188
+ if (!(0, utils_js_2.isBytes)(signature)) {
1189
+ const end = signature instanceof Signature ? ', use sig.toBytes()' : '';
1190
+ throw new Error('verify expects Uint8Array signature' + end);
1191
+ }
1192
+ validateSigLength(signature, format); // execute this twice because we want loud error
1193
+ try {
1194
+ const sig = Signature.fromBytes(signature, format);
1195
+ const P = Point.fromBytes(publicKey);
1196
+ if (lowS && sig.hasHighS())
1197
+ return false;
1198
+ const { r, s } = sig;
1199
+ const h = bits2int_modN(message); // mod n, not mod p
1200
+ const is = Fn.inv(s); // s^-1 mod n
1201
+ const u1 = Fn.create(h * is); // u1 = hs^-1 mod n
1202
+ const u2 = Fn.create(r * is); // u2 = rs^-1 mod n
1203
+ const R = Point.BASE.multiplyUnsafe(u1).add(P.multiplyUnsafe(u2)); // u1⋅G + u2⋅P
1204
+ if (R.is0())
1205
+ return false;
1206
+ const v = Fn.create(R.x); // v = r.x mod n
1207
+ return v === r;
1208
+ }
1209
+ catch (e) {
1210
+ return false;
1211
+ }
1212
+ }
1213
+ function recoverPublicKey(signature, message, opts = {}) {
1214
+ const { prehash } = validateSigOpts(opts, defaultSigOpts);
1215
+ message = validateMsgAndHash(message, prehash);
1216
+ return Signature.fromBytes(signature, 'recovered').recoverPublicKey(message).toBytes();
1217
+ }
1218
+ return Object.freeze({
1219
+ keygen,
1220
+ getPublicKey,
1221
+ getSharedSecret,
1222
+ utils,
1223
+ lengths,
1224
+ Point,
1225
+ sign,
1226
+ verify,
1227
+ recoverPublicKey,
1228
+ Signature,
1229
+ hash,
1230
+ });
1231
+ }
1232
+ //# sourceMappingURL=weierstrass.js.map