Skip to content

Commit 099669a

Browse files
committed
Verify memory usage: Re-use t1/w1 buffer
This commit is the first of a series of commits reducing the stack usage of verification. It is hoisted out from #751 This commit places the t1 and w1 buffers into a union saving K KiB of memory. Operations using it are slightly reordered such that their lifetime does not overlap. As CBMC struggles with unions (issue 8813), we use the same workaround present in signing: Use a struct by default, and a union when MLD_CONFIG_REDUCE_RAM is set. Signed-off-by: Matthias J. Kannwischer <matthias@kannwischer.eu>
1 parent 052ee8d commit 099669a

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

mldsa/mldsa_native.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,13 +839,13 @@ int MLD_API_NAMESPACE(pk_from_sk)(
839839
#else /* MLD_API_LEGACY_CONFIG || !MLD_CONFIG_REDUCE_RAM */
840840
#define MLD_TOTAL_ALLOC_44_KEYPAIR 36192
841841
#define MLD_TOTAL_ALLOC_44_SIGN 32448
842-
#define MLD_TOTAL_ALLOC_44_VERIFY 26560
842+
#define MLD_TOTAL_ALLOC_44_VERIFY 22464
843843
#define MLD_TOTAL_ALLOC_65_KEYPAIR 50048
844844
#define MLD_TOTAL_ALLOC_65_SIGN 44768
845-
#define MLD_TOTAL_ALLOC_65_VERIFY 36864
845+
#define MLD_TOTAL_ALLOC_65_VERIFY 30720
846846
#define MLD_TOTAL_ALLOC_87_KEYPAIR 66336
847847
#define MLD_TOTAL_ALLOC_87_SIGN 59104
848-
#define MLD_TOTAL_ALLOC_87_VERIFY 49408
848+
#define MLD_TOTAL_ALLOC_87_VERIFY 41216
849849
#endif /* !(MLD_API_LEGACY_CONFIG || !MLD_CONFIG_REDUCE_RAM) */
850850
/* check-magic: on */
851851

mldsa/src/sign.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,18 @@ int crypto_sign_verify_internal(const uint8_t *sig, size_t siglen,
965965
int externalmu)
966966
{
967967
int ret, cmp;
968+
969+
/* TODO: Remove the following workaround for
970+
* https://github.com/diffblue/cbmc/issues/8813 */
971+
typedef MLK_UNION_OR_STRUCT
972+
{
973+
mld_polyveck t1;
974+
mld_polyveck w1;
975+
}
976+
t1w1_u;
977+
mld_polyveck *t1;
978+
mld_polyveck *w1;
979+
968980
MLD_ALLOC(buf, uint8_t, (MLDSA_K * MLDSA_POLYW1_PACKEDBYTES));
969981
MLD_ALLOC(rho, uint8_t, MLDSA_SEEDBYTES);
970982
MLD_ALLOC(mu, uint8_t, MLDSA_CRHBYTES);
@@ -973,18 +985,19 @@ int crypto_sign_verify_internal(const uint8_t *sig, size_t siglen,
973985
MLD_ALLOC(cp, mld_poly, 1);
974986
MLD_ALLOC(mat, mld_polymat, 1);
975987
MLD_ALLOC(z, mld_polyvecl, 1);
976-
MLD_ALLOC(t1, mld_polyveck, 1);
977-
MLD_ALLOC(w1, mld_polyveck, 1);
988+
MLD_ALLOC(t1w1, t1w1_u, 1);
978989
MLD_ALLOC(tmp, mld_polyveck, 1);
979990
MLD_ALLOC(h, mld_polyveck, 1);
980991

981992
if (buf == NULL || rho == NULL || mu == NULL || c == NULL || c2 == NULL ||
982-
cp == NULL || mat == NULL || z == NULL || t1 == NULL || w1 == NULL ||
983-
tmp == NULL || h == NULL)
993+
cp == NULL || mat == NULL || z == NULL || t1w1 == NULL || tmp == NULL ||
994+
h == NULL)
984995
{
985996
ret = MLD_ERR_OUT_OF_MEMORY;
986997
goto cleanup;
987998
}
999+
t1 = &t1w1->t1;
1000+
w1 = &t1w1->w1;
9881001

9891002
if (siglen != MLDSA_CRYPTO_BYTES)
9901003
{
@@ -1027,17 +1040,14 @@ int crypto_sign_verify_internal(const uint8_t *sig, size_t siglen,
10271040

10281041
/* Matrix-vector multiplication; compute Az - c2^dt1 */
10291042
mld_poly_challenge(cp, c);
1030-
mld_polyvec_matrix_expand(mat, rho);
1031-
1032-
mld_polyvecl_ntt(z);
1033-
mld_polyvec_matrix_pointwise_montgomery(w1, mat, z);
1034-
10351043
mld_poly_ntt(cp);
10361044
mld_polyveck_shiftl(t1);
10371045
mld_polyveck_ntt(t1);
1038-
10391046
mld_polyveck_pointwise_poly_montgomery(tmp, cp, t1);
10401047

1048+
mld_polyvec_matrix_expand(mat, rho);
1049+
mld_polyvecl_ntt(z);
1050+
mld_polyvec_matrix_pointwise_montgomery(w1, mat, z);
10411051
mld_polyveck_sub(w1, tmp);
10421052
mld_polyveck_reduce(w1);
10431053
mld_polyveck_invntt_tomont(w1);
@@ -1061,8 +1071,7 @@ int crypto_sign_verify_internal(const uint8_t *sig, size_t siglen,
10611071
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
10621072
MLD_FREE(h, mld_polyveck, 1);
10631073
MLD_FREE(tmp, mld_polyveck, 1);
1064-
MLD_FREE(w1, mld_polyveck, 1);
1065-
MLD_FREE(t1, mld_polyveck, 1);
1074+
MLD_FREE(t1w1, t1w1_u, 1);
10661075
MLD_FREE(z, mld_polyvecl, 1);
10671076
MLD_FREE(mat, mld_polymat, 1);
10681077
MLD_FREE(cp, mld_poly, 1);

0 commit comments

Comments
 (0)