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