keys: schnorr_sign and schnorr_verify take a message of any length - #20
keys: schnorr_sign and schnorr_verify take a message of any length#20fametrano wants to merge 1 commit into
Conversation
BIP-340 signs a message of any length, and libsecp256k1's secp256k1_schnorrsig_verify already takes msglen, but schnorr_verify passed a hard-coded 32 and schnorr_sign was bound to secp256k1_schnorrsig_sign32. Bind secp256k1_schnorrsig_sign_custom with the BIP-340 nonce function and the aux bytes in its extraparams, and pass the real length on both sides. The length guards were asserts, which python -O removes: a 31-byte message was then signed and verified as if it were the first 32 bytes of the buffer. Raise ValueError and TypeError instead. Add BIP-340 vectors 15 to 18, the messages of size 0, 1, 17 and 100.
|
This PR is doing several things.
This would be fixed by #18 Do you have a use case for signing arbitrary length messages ( |
|
Thanks — you're right that #18 covers the -O case; with asserts refused-off at import, the out-of-bounds read is no longer reachable on master, so that part of the motivation is moot. On arbitrary length: BIP-340 signs messages of any size (the 2023-04 "Messages of Arbitrary Size" amendment), and schnorrsig_sign_custom is the API for it, so the "use case" is really the standard's rather than a caller's to justify. For electrum-ecc the narrower question is yours: should the Schnorr API accept a message other than 32 bytes at all? Every current caller in Electrum and notary passes a 32-byte digest, so nothing here needs it today. Two clean options:
I'm happy to reduce this PR to option 1 if that's your preference — just say which way you'd like to take it. |
BIP-340 signs a message of any length, and
secp256k1_schnorrsig_verifyalready takesmsglen, butschnorr_verifypasses a hard-coded 32 andschnorr_signis bound tosecp256k1_schnorrsig_sign32. Both guard the length withassert, whichpython -Oremoves. On 0.0.7 under-O:Changes:
secp256k1_schnorrsig_sign_customand call it with the BIP-340 nonce function and the aux bytes inextraparams, passing the real message length;schnorr_verifypasseslen(msg);TypeError/ValueErroron a wrong type or length instead of asserting;The first parameter is renamed from
msg32tomsg; every caller inspesmilo/electrumandspesmilo/notarypasses it positionally. The ECDSA methods keepmsg32, since they take a digest.pytest tests: 11 passed, 19 subtests. The new vectors and the new test fail on master.