@@ -215,13 +215,13 @@ def _keygen_internal(self, zeta):
215
215
216
216
return pk , sk
217
217
218
- def _sign_internal (self , sk_bytes , m , rnd , external_mu = None ):
218
+ def _sign_internal (self , sk_bytes , m , rnd , external_mu = False ):
219
219
"""
220
220
Deterministic algorithm to generate a signature for a formatted message
221
221
M' following Algorithm 7 (FIPS 204)
222
222
223
- Optionally allows for a pre-hashed message using `prehash_external_mu()`
224
- When `external_mu` is not `None`, then the message `m` must be `None `
223
+ When `external_mu` is `True`, the message `m` is interpreted instead as
224
+ the pre-hashed message `mu = prehash_external_mu() `
225
225
"""
226
226
# unpack the secret key
227
227
rho , K , tr , s1 , s2 , t0 = self ._unpack_sk (sk_bytes )
@@ -235,13 +235,10 @@ def _sign_internal(self, sk_bytes, m, rnd, external_mu=None):
235
235
A_hat = self ._expand_matrix_from_seed (rho )
236
236
237
237
# Set seeds and nonce (kappa)
238
- if external_mu is None :
239
- mu = self . _h ( tr + m , 64 )
238
+ if external_mu :
239
+ mu = m
240
240
else :
241
- # NOTE: when using external mu, the validation of the length
242
- # of external_mu is handled by the function sign_external_mu
243
- assert m is None , "Signing using external mu, message will be ignored"
244
- mu = external_mu
241
+ mu = self ._h (tr + m , 64 )
245
242
rho_prime = self ._h (K + rnd + mu , 64 )
246
243
247
244
kappa = 0
@@ -383,7 +380,7 @@ def verify(self, pk_bytes, m, sig_bytes, ctx=b""):
383
380
def prehash_external_mu (self , pk_bytes , m , ctx = b"" ):
384
381
"""
385
382
Prehash the message `m` with context `ctx` together with
386
- the public key for use with `sign_external_mu()`
383
+ the public key. For use with `sign_external_mu()`
387
384
"""
388
385
# Ensure the length of the context is as expected
389
386
if len (ctx ) > 255 :
@@ -400,23 +397,23 @@ def prehash_external_mu(self, pk_bytes, m, ctx=b""):
400
397
401
398
return mu
402
399
403
- def sign_external_mu (self , sk_bytes , external_mu , deterministic = False ):
400
+ def sign_external_mu (self , sk_bytes , mu , deterministic = False ):
404
401
"""
405
- Generates an ML-DSA signature of a message m given the prehash
406
- of the message `m` with an optional context
402
+ Generates an ML-DSA signature of a message given the prehash
403
+ mu = H(H(pk), M')
407
404
"""
408
405
# Ensure the length of the context is as expected
409
- if len (external_mu ) != 64 :
406
+ if len (mu ) != 64 :
410
407
raise ValueError (
411
- f"mu bytes must have length 64, mu has length { len (external_mu ) = } "
408
+ f"mu bytes must have length 64, mu has length { len (mu ) = } "
412
409
)
413
410
414
411
if deterministic :
415
412
rnd = bytes ([0 ] * 32 )
416
413
else :
417
414
rnd = self .random_bytes (32 )
418
415
419
- # Compute the signature given external mu, we explicitly set the message
420
- # to None
421
- sig_bytes = self ._sign_internal (sk_bytes , None , rnd , external_mu )
416
+ # Compute the signature given external mu, we set the external_mu
417
+ # to True
418
+ sig_bytes = self ._sign_internal (sk_bytes , mu , rnd , external_mu = True )
422
419
return sig_bytes
0 commit comments