Skip to content

Commit 4e6eca3

Browse files
mkannwischerhanno-becker
authored andcommitted
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 <[email protected]>
1 parent 09cc8d2 commit 4e6eca3

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

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
@@ -966,6 +966,18 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
966966
int externalmu)
967967
{
968968
int ret, cmp;
969+
970+
/* TODO: Remove the following workaround for
971+
* https://github.com/diffblue/cbmc/issues/8813 */
972+
typedef MLK_UNION_OR_STRUCT
973+
{
974+
mld_polyveck t1;
975+
mld_polyveck w1;
976+
}
977+
t1w1_u;
978+
mld_polyveck *t1;
979+
mld_polyveck *w1;
980+
969981
MLD_ALLOC(buf, uint8_t, (MLDSA_K * MLDSA_POLYW1_PACKEDBYTES));
970982
MLD_ALLOC(rho, uint8_t, MLDSA_SEEDBYTES);
971983
MLD_ALLOC(mu, uint8_t, MLDSA_CRHBYTES);
@@ -974,18 +986,19 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
974986
MLD_ALLOC(cp, mld_poly, 1);
975987
MLD_ALLOC(mat, mld_polymat, 1);
976988
MLD_ALLOC(z, mld_polyvecl, 1);
977-
MLD_ALLOC(t1, mld_polyveck, 1);
978-
MLD_ALLOC(w1, mld_polyveck, 1);
989+
MLD_ALLOC(t1w1, t1w1_u, 1);
979990
MLD_ALLOC(tmp, mld_polyveck, 1);
980991
MLD_ALLOC(h, mld_polyveck, 1);
981992

982993
if (buf == NULL || rho == NULL || mu == NULL || c == NULL || c2 == NULL ||
983-
cp == NULL || mat == NULL || z == NULL || t1 == NULL || w1 == NULL ||
984-
tmp == NULL || h == NULL)
994+
cp == NULL || mat == NULL || z == NULL || t1w1 == NULL || tmp == NULL ||
995+
h == NULL)
985996
{
986997
ret = MLD_ERR_OUT_OF_MEMORY;
987998
goto cleanup;
988999
}
1000+
t1 = &t1w1->t1;
1001+
w1 = &t1w1->w1;
9891002

9901003
if (siglen != MLDSA_CRYPTO_BYTES)
9911004
{
@@ -1028,17 +1041,14 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
10281041

10291042
/* Matrix-vector multiplication; compute Az - c2^dt1 */
10301043
mld_poly_challenge(cp, c);
1031-
mld_polyvec_matrix_expand(mat, rho);
1032-
1033-
mld_polyvecl_ntt(z);
1034-
mld_polyvec_matrix_pointwise_montgomery(w1, mat, z);
1035-
10361044
mld_poly_ntt(cp);
10371045
mld_polyveck_shiftl(t1);
10381046
mld_polyveck_ntt(t1);
1039-
10401047
mld_polyveck_pointwise_poly_montgomery(tmp, cp, t1);
10411048

1049+
mld_polyvec_matrix_expand(mat, rho);
1050+
mld_polyvecl_ntt(z);
1051+
mld_polyvec_matrix_pointwise_montgomery(w1, mat, z);
10421052
mld_polyveck_sub(w1, tmp);
10431053
mld_polyveck_reduce(w1);
10441054
mld_polyveck_invntt_tomont(w1);
@@ -1062,8 +1072,7 @@ int mld_sign_verify_internal(const uint8_t *sig, size_t siglen,
10621072
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
10631073
MLD_FREE(h, mld_polyveck, 1);
10641074
MLD_FREE(tmp, mld_polyveck, 1);
1065-
MLD_FREE(w1, mld_polyveck, 1);
1066-
MLD_FREE(t1, mld_polyveck, 1);
1075+
MLD_FREE(t1w1, t1w1_u, 1);
10671076
MLD_FREE(z, mld_polyvecl, 1);
10681077
MLD_FREE(mat, mld_polymat, 1);
10691078
MLD_FREE(cp, mld_poly, 1);

0 commit comments

Comments
 (0)