Skip to content

Commit f219006

Browse files
committed
add example for prehash to readme
1 parent d0330ba commit f219006

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,33 @@ so you can simply import the NIST level you want to play with:
144144
The above example would also work with the other NIST levels
145145
`ML_DSA_65` and `ML_DSA_87`.
146146

147+
#### Hash ML-DSA
148+
149+
Following algorithms 4 and 5 of FIPS 204 we also include a version of pre-hash ML-DSA which hashes the message before signing it using SHA512 by default for
150+
all three security levels. This is used in much the same way as ML-DSA:
151+
152+
```python
153+
>>> from dilithium_py.ml_dsa import HASH_ML_DSA_44_WITH_SHA512
154+
>>>
155+
>>> # Example of signing
156+
>>> pk, sk = HASH_ML_DSA_44_WITH_SHA512.keygen()
157+
>>> msg = b"Your message signed by ML_DSA"
158+
>>> sig = HASH_ML_DSA_44_WITH_SHA512.sign(sk, msg)
159+
>>> assert HASH_ML_DSA_44_WITH_SHA512.verify(pk, msg, sig)
160+
>>>
161+
>>> # Verification will fail with the wrong msg or pk
162+
>>> assert not HASH_ML_DSA_44_WITH_SHA512.verify(pk, b"", sig)
163+
>>> pk_new, sk_new = HASH_ML_DSA_44_WITH_SHA512.keygen()
164+
>>> assert not HASH_ML_DSA_44_WITH_SHA512.verify(pk_new, msg, sig)
165+
```
166+
167+
There is also support for other hash functions (at the time, only SHA256 and SHAKE128), but there seem to only be OIDs for the pre-hash version using SHA512
168+
so this is what is included. To access signing with other hash functions the methods are `HASH_ML_DSA_44_WITH_SHA512._sign_with_pre_hash` and
169+
`HASH_ML_DSA_44_WITH_SHA512._verify_with_pre_hash`. For more information see the
170+
implementation and comments in `hash_ml_dsa.py`.
171+
172+
The pre-hash version of ML-DSA has purposefully been added to a child class of ML-DSA as the signatures which are produced between these variants are incompatible.
173+
147174
### Benchmarks
148175

149176
Some very rough benchmarks to give an idea about performance:

0 commit comments

Comments
 (0)