Skip to content

Commit b0d6f22

Browse files
committed
simplify the API for internal signing
1 parent 2c64245 commit b0d6f22

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/dilithium_py/ml_dsa/ml_dsa.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,13 @@ def _keygen_internal(self, zeta):
215215

216216
return pk, sk
217217

218-
def _sign_internal(self, sk_bytes, m, rnd, external_mu=None):
218+
def _sign_internal(self, sk_bytes, m, rnd, external_mu=False):
219219
"""
220220
Deterministic algorithm to generate a signature for a formatted message
221221
M' following Algorithm 7 (FIPS 204)
222222
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()`
225225
"""
226226
# unpack the secret key
227227
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):
235235
A_hat = self._expand_matrix_from_seed(rho)
236236

237237
# 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
240240
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)
245242
rho_prime = self._h(K + rnd + mu, 64)
246243

247244
kappa = 0
@@ -383,7 +380,7 @@ def verify(self, pk_bytes, m, sig_bytes, ctx=b""):
383380
def prehash_external_mu(self, pk_bytes, m, ctx=b""):
384381
"""
385382
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()`
387384
"""
388385
# Ensure the length of the context is as expected
389386
if len(ctx) > 255:
@@ -400,23 +397,23 @@ def prehash_external_mu(self, pk_bytes, m, ctx=b""):
400397

401398
return mu
402399

403-
def sign_external_mu(self, sk_bytes, external_mu, deterministic=False):
400+
def sign_external_mu(self, sk_bytes, mu, deterministic=False):
404401
"""
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')
407404
"""
408405
# Ensure the length of the context is as expected
409-
if len(external_mu) != 64:
406+
if len(mu) != 64:
410407
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) = }"
412409
)
413410

414411
if deterministic:
415412
rnd = bytes([0] * 32)
416413
else:
417414
rnd = self.random_bytes(32)
418415

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)
422419
return sig_bytes

0 commit comments

Comments
 (0)