2525if _lib .ML_DSA_ENABLED :
2626 import pytest
2727
28- from wolfcrypt .ciphers import MlDsaPrivate , MlDsaPublic , MlDsaType
28+ from wolfcrypt .ciphers import MlDsaPrivate , MlDsaPublic , MlDsaType , ML_DSA_SIGNATURE_SEED_LENGTH
2929 from wolfcrypt .random import Random
3030
3131 @pytest .fixture
@@ -134,7 +134,7 @@ def test_sign_verify(mldsa_type, rng):
134134 # Verify with wrong message
135135 wrong_message = b"This is a wrong message for ML-DSA signature"
136136 assert not mldsa_pub .verify (signature , wrong_message )
137-
137+
138138 # Verify a signature generated without a context but where a context
139139 # is provided during verify
140140 ctx = b"This is a test context for ML-DSA signature"
@@ -156,3 +156,51 @@ def test_sign_verify(mldsa_type, rng):
156156
157157 # Verify with wrong context
158158 assert not mldsa_pub .verify (signature , message , ctx = wrong_ctx )
159+
160+ def test_sign_with_seed (mldsa_type , rng ):
161+ signature_seed = rng .bytes (ML_DSA_SIGNATURE_SEED_LENGTH )
162+ mldsa_priv = MlDsaPrivate .make_key (mldsa_type , rng )
163+ pub_key = mldsa_priv .encode_pub_key ()
164+
165+ # Import public key
166+ mldsa_pub = MlDsaPublic (mldsa_type )
167+ mldsa_pub .decode_key (pub_key )
168+
169+ # Sign a message
170+ message = b"This is a test message for ML-DSA signature"
171+ signature = mldsa_priv .sign_with_seed (message , signature_seed )
172+ assert len (signature ) == mldsa_priv .sig_size
173+
174+ # Verify the signature using public key
175+ assert mldsa_pub .verify (signature , message )
176+
177+ # re-generate from the same seed:
178+ signature_from_same_seed = mldsa_priv .sign_with_seed (message , signature_seed )
179+ assert signature == signature_from_same_seed
180+
181+ # test that the seed size is checked:
182+ with pytest .raises (ValueError ):
183+ _ = mldsa_priv .sign_with_seed (message , signature_seed [:- 1 ])
184+
185+ # test that the seed type is checked (should be bytes-like, not string)
186+ with pytest .raises (TypeError ):
187+ _ = mldsa_priv .sign_with_seed (message , "" )
188+
189+ def test_sign_with_seed_and_context (mldsa_type , rng ):
190+ signature_seed = rng .bytes (ML_DSA_SIGNATURE_SEED_LENGTH )
191+ mldsa_priv = MlDsaPrivate .make_key (mldsa_type , rng )
192+ pub_key = mldsa_priv .encode_pub_key ()
193+
194+ # Import public key
195+ mldsa_pub = MlDsaPublic (mldsa_type )
196+ mldsa_pub .decode_key (pub_key )
197+
198+ # Sign a message
199+ message = b"This is a test message for ML-DSA signature"
200+ context = b"Some context for the signature"
201+ signature = mldsa_priv .sign_with_seed (message , signature_seed , ctx = context )
202+ assert len (signature ) == mldsa_priv .sig_size
203+ # test that the context length is checked (more than 255 bytes is invalid):
204+ with pytest .raises (ValueError ):
205+ _ = mldsa_priv .sign_with_seed (message , signature_seed [:- 1 ], ctx = bytes (1000 ))
206+
0 commit comments