How to find a bug in a Rust implementation of ML-DSA (f.k.a. Dilithium)?
A look back at the discovery of an interoperability bug in a Rust implementation of ML-DSA (FIPS 204), through a differential test.

Table of contents
Context
During the summer of 2024, I was interning at Fortanix, in the Netherlands. The goal of the internship was to implement ML-DSA (fka. CRYSTALS-Dilithium, standardized in FIPS-204) in Rust and then integrate it into the Fortanix DSM product. Like any self-respecting developer, once the implementation was complete, I wrote various tests: roundtrip, conformance, resilience (edge cases), as well as interoperability tests. It is the latter that allowed me to find a bug in a Rust implementation of ML-DSA; the following one: integritychain/fips204 (the first Google result for “rust fips204”!).
What is an interoperability test?
An interoperability test works as follows: the main algorithm takes libraries to test as input, then uses them concurrently on common inputs generated on the fly; the main algorithm then checks that these libraries behaved in the same way (whether in error handling or in the returned business value). Here is an image illustrating how this works for libraries:
Test configuration
Once this main test algorithm was implemented, I let it run on two libraries: the one I wrote during my internship and the one from the Rust crate “fips204” by “integritychain”: integritychain/fips204.
The following harnesses were implemented:
- (, ) = : the main algorithm varies and checks that the serialization of the two keys is identical;
- = : the main algorithm varies and and checks that the two signatures are identical, which therefore requires using the deterministic version or hooking the PRNG;
- = : the main algorithm varies , and and checks that the bit is identical.
Each harness also checks that the functions do not return an error, or return the same one.
After running the test algorithm with the harnesses described earlier, it was after 3/4 minutes of testing that the harness reported a difference. Indeed, the serialized keys were not the same for a given seed .
Analyzing the difference
As a reminder, here is the key generation algorithm as described in FIPS 204:

The problem with the interoperability test is that once the difference is found, you now have to determine which library the bug comes from… After many println! calls of intermediate values, I found that the difference was present as soon as the Power2Round function was entered. Indeed, the code of the integritychain crate performs the following operations:
// 5: t ← NTT−1(cap_a_hat ◦ NTT(s_1)) + s_2 ▷ Compute t = As1 + s2
let t: [R; K] = {
let s_1_hat: [T; L] = ntt(&s_1);
let as1_hat: [T; K] = mat_vec_mul(&cap_a_hat, &s_1_hat);
vec_add(&inv_ntt(&as1_hat), &s_2)
};
The output of the inv_ntt function is reduced modulo , i.e. it contains coefficients in , and contains coefficients in . The result of vec_add(&inv_ntt(&as1_hat), &s_2) therefore contains coefficients in . Problem: the Power2Round function expects a vector with coefficients modulo as input.
As a reminder, the Power2Round function decomposes an integer into two integers such that and . For optimization reasons, the function assumes that the input is reduced and operates directly on the bits.
Here is the implementation of the Power2Round function:
pub(crate) fn power2round<const K: usize>(r: &[R; K]) -> ([R; K], [R; K]) {
// 1: r+ ← r mod q
// 2: r0 ← r+ mod±2^d
// 3: return ((r+ − r0)/2^d, r0)
let r_1: [R; K] = core::array::from_fn(|k| {
R(core::array::from_fn(|n| (r[k].0[n] + (1 << (D - 1)) - 1) >> D))
});
let r_0: [R; K] =
core::array::from_fn(|k| R(core::array::from_fn(|n| r[k].0[n] - (r_1[k].0[n] << D))));
debug_assert!(
{
let mut result = true;
for k in 0..K {
for n in 0..256 {
result &= r[k].0[n] == ((r_1[k].0[n] << D) + r_0[k].0[n]);
}
}
result
},
"Alg 29: fail"
);
(r_1, r_0)
}
This implementation treats a number as an int32_t and will behave differently for two different but equivalent inputs . It therefore returns incorrect values. TODO: figure conclusion
Impact of the bug
TODO
Probability of the bug occurring
TODO