Back to articles
Articles

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.

Dilithium logo
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 nn 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 n=2n = 2 libraries:

Illustration of the interoperability test

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:

  • (sk\mathbf{sk}, vk\mathbf{vk}) = KeyGen(ξ)\textsf{KeyGen}(\xi): the main algorithm varies ξ\xi and checks that the serialization of the two keys is identical;
  • σ\sigma = Sign(μ,sk)\textsf{Sign}(\mu, \mathbf{sk}): the main algorithm varies μ\mu and sk\mathbf{sk} and checks that the two signatures σ\sigma are identical, which therefore requires using the deterministic version or hooking the PRNG;
  • bb = Verify(σ,μ,vk)\textsf{Verify}(\sigma, \mu, \mathbf{vk}): the main algorithm varies σ\sigma, μ\mu and vk\mathbf{vk} and checks that the bit bb is identical.

Each harness also checks that the functions (KeyGen,Sign,Verify)(\textsf{KeyGen}, \textsf{Sign}, \textsf{Verify}) 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 KeyGen\textsf{KeyGen} harness reported a difference. Indeed, the serialized keys were not the same for a given seed ξ\xi.

Analyzing the difference

As a reminder, here is the key generation algorithm as described in FIPS 204:

FIPS 204 key generation

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 qq, i.e. it contains coefficients in {0,1,...,q−1}\lbrace 0, 1, ..., q-1 \rbrace, and s_2\mathbf{s\_2} contains coefficients in [−η,η][-\eta,\eta]. The result of vec_add(&inv_ntt(&as1_hat), &s_2) therefore contains coefficients in {−η,−η+1,...,q−1+η}\lbrace -\eta, -\eta +1, ..., q-1 + \eta \rbrace. Problem: the Power2Round function expects a vector with coefficients modulo qq as input.

As a reminder, the Power2Round function decomposes an integer r∈Z_qr \in \mathbb{Z}\_q into two integers (r_1,r_2)(r\_1, r\_2) such that r≡r_12d+r_0 modr \equiv r\_1 2^d + r\_0~\mathrm{mod} and r_0 mod±r\_0~\mathrm{mod}^{\pm}. For optimization reasons, the function assumes that the input is reduced mod q\mathrm{mod}~q 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 mod  q\mod{q}. It therefore returns incorrect (r_0,r_1)(r\_0, r\_1) values. TODO: figure conclusion

Impact of the bug

TODO

Probability of the bug occurring

TODO

Press Esc to close