Skip to content

Commit 895fb29

Browse files
committed
ML-DSA/Dilithium: reduce vector when small build
Ensure the matrix multiplication result has values reduced to the correct range. Only an issue for small builds.
1 parent 2ef096a commit 895fb29

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

wolfcrypt/src/dilithium.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7331,7 +7331,6 @@ static void dilithium_poly_red(sword32* a)
73317331
}
73327332
}
73337333

7334-
#ifndef WOLFSSL_DILITHIUM_SIGN_SMALL_MEM
73357334
/* Modulo reduce values in polynomials of vector. Range (-2^31)..(2^31-1).
73367335
*
73377336
* @param [in, out] a Vector of polynomials.
@@ -7346,7 +7345,6 @@ static void dilithium_vec_red(sword32* a, byte l)
73467345
a += DILITHIUM_N;
73477346
}
73487347
}
7349-
#endif /* WOLFSSL_DILITHIUM_SIGN_SMALL_MEM*/
73507348
#endif /* !WOLFSSL_DILITHIUM_NO_SIGN */
73517349

73527350
#if (!defined(WOLFSSL_DILITHIUM_NO_SIGN) || \
@@ -7724,6 +7722,9 @@ static int dilithium_make_key_from_seed(dilithium_key* key, const byte* seed)
77247722
/* Step 5: t <- NTT-1(A_circum o NTT(s1)) + s2 */
77257723
dilithium_vec_ntt_small_full(s1, params->l);
77267724
dilithium_matrix_mul(t, a, s1, params->k, params->l);
7725+
#ifdef WOLFSSL_DILITHIUM_SMALL
7726+
dilithium_vec_red(t, params->k);
7727+
#endif
77277728
dilithium_vec_invntt_full(t, params->k);
77287729
dilithium_vec_add(t, s2, params->k);
77297730

@@ -8272,6 +8273,9 @@ static int dilithium_sign_with_seed_mu(dilithium_key* key,
82728273
XMEMCPY(y_ntt, y, params->s1Sz);
82738274
dilithium_vec_ntt_full(y_ntt, params->l);
82748275
dilithium_matrix_mul(w, a, y_ntt, params->k, params->l);
8276+
#ifdef WOLFSSL_DILITHIUM_SMALL
8277+
dilithium_vec_red(w, params->k);
8278+
#endif
82758279
dilithium_vec_invntt_full(w, params->k);
82768280
/* Step 14, Step 22: Make values positive and decompose. */
82778281
dilithium_vec_make_pos(w, params->k);
@@ -8535,6 +8539,9 @@ static int dilithium_sign_with_seed_mu(dilithium_key* key,
85358539
XMEMCPY(y_ntt, y, params->s1Sz);
85368540
dilithium_vec_ntt_full(y_ntt, params->l);
85378541
dilithium_matrix_mul(w, a, y_ntt, maxK, params->l);
8542+
#ifdef WOLFSSL_DILITHIUM_SMALL
8543+
dilithium_vec_red(w, params->k);
8544+
#endif
85388545
dilithium_vec_invntt_full(w, maxK);
85398546
/* Step 14, Step 22: Make values positive and decompose. */
85408547
dilithium_vec_make_pos(w, maxK);
@@ -9433,6 +9440,9 @@ static int dilithium_verify_mu(dilithium_key* key, const byte* mu,
94339440
/* Step 10: w = NTT-1(A o NTT(z) - NTT(c) o NTT(t1)) */
94349441
dilithium_vec_ntt_full(z, params->l);
94359442
dilithium_matrix_mul(w, a, z, params->k, params->l);
9443+
#ifdef WOLFSSL_DILITHIUM_SMALL
9444+
dilithium_vec_red(w, params->k);
9445+
#endif
94369446
dilithium_ntt_small_full(c);
94379447
dilithium_vec_mul(t1c, c, t1, params->k);
94389448
dilithium_vec_sub(w, t1c, params->k);
@@ -11180,6 +11190,9 @@ int wc_dilithium_check_key(dilithium_key* key)
1118011190
/* Calcaluate t = NTT-1(A o NTT(s1)) + s2 */
1118111191
dilithium_vec_ntt_small_full(s1, params->l);
1118211192
dilithium_matrix_mul(t, a, s1, params->k, params->l);
11193+
#ifdef WOLFSSL_DILITHIUM_SMALL
11194+
dilithium_vec_red(t, params->k);
11195+
#endif
1118311196
dilithium_vec_invntt_full(t, params->k);
1118411197
dilithium_vec_add(t, s2, params->k);
1118511198
/* Subtract t0 from t. */

0 commit comments

Comments
 (0)