Skip to content

Commit 3937a4f

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 ca5b484 commit 3937a4f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

wolfcrypt/src/dilithium.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7724,6 +7724,9 @@ static int dilithium_make_key_from_seed(dilithium_key* key, const byte* seed)
77247724
/* Step 5: t <- NTT-1(A_circum o NTT(s1)) + s2 */
77257725
dilithium_vec_ntt_small_full(s1, params->l);
77267726
dilithium_matrix_mul(t, a, s1, params->k, params->l);
7727+
#ifdef WOLFSSL_DILITHIUM_SMALL
7728+
dilithium_vec_red(t, params->k);
7729+
#endif
77277730
dilithium_vec_invntt_full(t, params->k);
77287731
dilithium_vec_add(t, s2, params->k);
77297732

@@ -8272,6 +8275,9 @@ static int dilithium_sign_with_seed_mu(dilithium_key* key,
82728275
XMEMCPY(y_ntt, y, params->s1Sz);
82738276
dilithium_vec_ntt_full(y_ntt, params->l);
82748277
dilithium_matrix_mul(w, a, y_ntt, params->k, params->l);
8278+
#ifdef WOLFSSL_DILITHIUM_SMALL
8279+
dilithium_vec_red(w, params->k);
8280+
#endif
82758281
dilithium_vec_invntt_full(w, params->k);
82768282
/* Step 14, Step 22: Make values positive and decompose. */
82778283
dilithium_vec_make_pos(w, params->k);
@@ -8535,6 +8541,9 @@ static int dilithium_sign_with_seed_mu(dilithium_key* key,
85358541
XMEMCPY(y_ntt, y, params->s1Sz);
85368542
dilithium_vec_ntt_full(y_ntt, params->l);
85378543
dilithium_matrix_mul(w, a, y_ntt, maxK, params->l);
8544+
#ifdef WOLFSSL_DILITHIUM_SMALL
8545+
dilithium_vec_red(w, params->k);
8546+
#endif
85388547
dilithium_vec_invntt_full(w, maxK);
85398548
/* Step 14, Step 22: Make values positive and decompose. */
85408549
dilithium_vec_make_pos(w, maxK);
@@ -9433,6 +9442,9 @@ static int dilithium_verify_mu(dilithium_key* key, const byte* mu,
94339442
/* Step 10: w = NTT-1(A o NTT(z) - NTT(c) o NTT(t1)) */
94349443
dilithium_vec_ntt_full(z, params->l);
94359444
dilithium_matrix_mul(w, a, z, params->k, params->l);
9445+
#ifdef WOLFSSL_DILITHIUM_SMALL
9446+
dilithium_vec_red(w, params->k);
9447+
#endif
94369448
dilithium_ntt_small_full(c);
94379449
dilithium_vec_mul(t1c, c, t1, params->k);
94389450
dilithium_vec_sub(w, t1c, params->k);
@@ -11180,6 +11192,9 @@ int wc_dilithium_check_key(dilithium_key* key)
1118011192
/* Calcaluate t = NTT-1(A o NTT(s1)) + s2 */
1118111193
dilithium_vec_ntt_small_full(s1, params->l);
1118211194
dilithium_matrix_mul(t, a, s1, params->k, params->l);
11195+
#ifdef WOLFSSL_DILITHIUM_SMALL
11196+
dilithium_vec_red(t, params->k);
11197+
#endif
1118311198
dilithium_vec_invntt_full(t, params->k);
1118411199
dilithium_vec_add(t, s2, params->k);
1118511200
/* Subtract t0 from t. */

0 commit comments

Comments
 (0)