Skip to content

Commit

Permalink
bip340: Allow variable-length messages
Browse files Browse the repository at this point in the history
  • Loading branch information
real-or-random committed Aug 4, 2022
1 parent 43da5de commit 8d2c3c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
29 changes: 26 additions & 3 deletions bip-0340.mediawiki
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ As an alternative to generating keys randomly, it is also possible and safe to r

Input:
* The secret key ''sk'': a 32-byte array
* The message ''m'': a 32-byte array
* The message ''m'': a byte array
* Auxiliary random data ''a'': a 32-byte array
The algorithm ''Sign(sk, m)'' is defined as:
Expand Down Expand Up @@ -174,7 +174,7 @@ It should be noted that various alternative signing algorithms can be used to pr

Input:
* The public key ''pk'': a 32-byte array
* The message ''m'': a 32-byte array
* The message ''m'': a byte array
* A signature ''sig'': a 64-byte array
The algorithm ''Verify(pk, m, sig)'' is defined as:
Expand All @@ -197,7 +197,7 @@ Note that the correctness of verification relies on the fact that ''lift_x'' alw
Input:
* The number ''u'' of signatures
* The public keys ''pk<sub>1..u</sub>'': ''u'' 32-byte arrays
* The messages ''m<sub>1..u</sub>'': ''u'' 32-byte arrays
* The messages ''m<sub>1..u</sub>'': ''u'' byte arrays
* The signatures ''sig<sub>1..u</sub>'': ''u'' 64-byte arrays
The algorithm ''BatchVerify(pk<sub>1..u</sub>, m<sub>1..u</sub>, sig<sub>1..u</sub>)'' is defined as:
Expand All @@ -213,6 +213,29 @@ The algorithm ''BatchVerify(pk<sub>1..u</sub>, m<sub>1..u</sub>, sig<sub>1..u</s
If all individual signatures are valid (i.e., ''Verify'' would return success for them), ''BatchVerify'' will always return success. If at least one signature is invalid, ''BatchVerify'' will return success with at most a negligible probability.

=== Usage Considerations ===

==== Messages of Arbitrary Size ====

The signature scheme specified in this BIP accepts byte strings of arbitrary size as input messages.<ref>In theory, the message size is restricted due to the fact that SHA256 accepts byte strings only up to size of 2^56-1 bytes.</ref>
It is understood that implementations may reject messages which are too large in their environment or application context,
e.g., messages which exceed predefined buffers or would otherwise cause resource exhaustion.

Earlier revisions of this BIP required messages to be exactly 32 bytes.
This restriction puts a burden on callers
who typically need to perform pre-hashing of the actual input message by feeding it through SHA256 (or another collision-resistant cryptographic hash function)
to create a 32-byte digest which can be passed to signing or verification
(as for example done in [[bip-0341.mediawiki|BIP341]].)

Since pre-hashing may not always be desirable,
e.g. when actual messages are shorter than 32 bytes,
the restriction to 32-byte messages has been lifted.
We note that pre-hashing is recommended for performance reasons in applications that deal with large messages.
If large messages are not pre-hashed,
the algorithms of the signature scheme will perform more hashing internally.
In particular, the hashing performed by the signing algorithm will process the message twice,
which leads to performance penalties for large messages.

== Applications ==

There are several interesting applications beyond simple signatures.
Expand Down
4 changes: 0 additions & 4 deletions bip-0340/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ def pubkey_gen(seckey: bytes) -> bytes:
return bytes_from_point(P)

def schnorr_sign(msg: bytes, seckey: bytes, aux_rand: bytes) -> bytes:
if len(msg) != 32:
raise ValueError('The message must be a 32-byte array.')
d0 = int_from_bytes(seckey)
if not (1 <= d0 <= n - 1):
raise ValueError('The secret key must be an integer in the range 1..n-1.')
Expand All @@ -122,8 +120,6 @@ def schnorr_sign(msg: bytes, seckey: bytes, aux_rand: bytes) -> bytes:
return sig

def schnorr_verify(msg: bytes, pubkey: bytes, sig: bytes) -> bool:
if len(msg) != 32:
raise ValueError('The message must be a 32-byte array.')
if len(pubkey) != 32:
raise ValueError('The public key must be a 32-byte array.')
if len(sig) != 64:
Expand Down

0 comments on commit 8d2c3c0

Please sign in to comment.