| Copyright | (c) 2025 Jose Storopoli |
|---|---|
| License | MIT |
| Maintainer | Jose Storopoli <jose@storopoli.com> |
| Safe Haskell | None |
| Language | Haskell2010 |
Crypto.Curve.Secp256k1.MuSig2.Unsafe
Description
Escape hatches from the guarantees of Crypto.Curve.Secp256k1.MuSig2.
Do not use this module unless you know exactly why you need it. Every function here can make you reuse a nonce, and signing twice with the same nonce, in any two sessions, lets the other signers compute your secret key.
nonceGenWithRandandnonceGenCountergenerate nonces deterministically from their inputs: if the random bytes (or the counter) ever repeat, so does the nonce. Use them for test vectors, or with a counter you can prove never repeats; otherwise usenonceGen.secNonceToBytesandsecNonceFromBytesmove a secret nonce out of the process and back, e.g. to keep it on disk between the two rounds of a session. The library can only prevent reuse within one process: storing the bytes and importing them twice (or restoring a backup, or a snapshot of a virtual machine) reuses the nonce. Delete the bytes before signing with the imported nonce, and never keep a copy.signWithoutVerificationskips the check of the partial signature thatsigndoes, so a fault during signing goes unnoticed. Use it only if you verify the partial signature yourself or cannot afford the time.
For a signer who cannot rely on randomness at all, prefer
deterministicSign, which is safe when the
signer sends its nonce last.
Synopsis
- nonceGenWithRand :: ByteString -> PubKey -> NonceInputs -> IO (Either NonceGenError (SecNonce, PubNonce))
- nonceGenCounter :: SecKey -> Word64 -> NonceInputs -> IO (Either NonceGenError (SecNonce, PubNonce))
- secNonceToBytes :: SecNonce -> IO (Maybe ByteString)
- secNonceFromBytes :: ByteString -> IO (Either DecodeError SecNonce)
- signWithoutVerification :: SecNonce -> SecKey -> Session -> IO (Either SignError PartialSignature)
Deterministic nonce generation
Arguments
| :: ByteString | 32 random bytes, never reused. |
| -> PubKey | The signer's public key. |
| -> NonceInputs | |
| -> IO (Either NonceGenError (SecNonce, PubNonce)) |
BIP-327 NonceGen with caller-supplied randomness instead of the
operating system's.
Deterministic: the same random bytes and inputs give the same nonce. Reusing the bytes for two sessions reuses the nonce and leaks the secret key. The bytes must be 32 bytes of fresh, uniformly random data used for nothing else; this function exists for test vectors and for callers with their own vetted random number generator.
Each call returns a fresh one-shot SecNonce, so two calls with the same
bytes give two nonces with the same secret scalars, which
sign cannot tell apart. (It is in IO,
although deterministic, so that one call creates exactly one SecNonce:
GHC may evaluate a pure thunk twice when two threads force it at once.)
Fails with InvalidRandLength if the bytes
are not 32 long, with NonceIsZero with
negligible probability, and otherwise as
nonceGen.
Arguments
| :: SecKey | The signer's secret key. |
| -> Word64 | A counter value never used before with this key. |
| -> NonceInputs | |
| -> IO (Either NonceGenError (SecNonce, PubNonce)) |
Counter-based nonce generation (BIP-327 CounterNonceGen), compatible
with libsecp256k1's secp256k1_musig_nonce_gen_counter: NonceGen with the
secret key always hashed in (the nonceSecKey
of the inputs is ignored) and the random bytes
bytes(8, counter) || bytes(24, 0).
The counter must never repeat for the same secret key, across restarts,
backups and every device holding the key: a repeated counter with the same
inputs repeats the nonce and leaks the secret key. Use it only where a
reliable non-repeating counter is easier to get than randomness. Each call
returns a fresh one-shot SecNonce, like nonceGenWithRand.
Fails with NonceIsZero with negligible
probability, and otherwise as nonceGen.
Secret nonce import and export
secNonceToBytes :: SecNonce -> IO (Maybe ByteString) Source #
Exports a secret nonce in the 97-byte BIP-327 encoding
bytes(32, k1) || bytes(32, k2) || pk, consuming it: the SecNonce
(and every copy of it) can no longer sign. Nothing if it was already used
or exported.
The encoding does not carry the aggregate key or message the nonce may have
been generated with, so a nonce imported back with secNonceFromBytes is no
longer bound to them.
The bytes are as secret as the secret key, and importing them more than once reuses the nonce: see the module documentation.
secNonceFromBytes :: ByteString -> IO (Either DecodeError SecNonce) Source #
Imports a secret nonce from its 97-byte BIP-327 encoding, as a fresh
one-shot SecNonce. Checks that both scalars are in \([1, n)\) (a nonce
zeroed by another BIP-327 implementation after use is rejected with
ScalarZero) and that the public key is a
valid encoding.
Each encoding must be imported at most once: the library cannot know that two imports are the same nonce. See the module documentation.
Signing
signWithoutVerification :: SecNonce -> SecKey -> Session -> IO (Either SignError PartialSignature) Source #
sign without the verification of the
partial signature before it is returned.
Consumes the nonce exactly like sign and
makes the same checks, except that it never returns
SelfVerificationFailed: a fault during
signing (a bug, a bit flip, a fault attack) then goes unnoticed and the
faulty partial signature is released, which can leak the secret key. It
saves the elliptic curve multiplications of the check, which are nearly all
the work of signing once the Session exists.