Skip to content

Commit 283b8b5

Browse files
committed
sign stack usage: Re-use y/h buffer
This commit is the second commit working towards bringing down the memory consumption of signature_internal. It combines the y and h buffer as those lifetime does not overlap. Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 4da1fa2 commit 283b8b5

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

mldsa/src/sign.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,20 +486,26 @@ __contract__(
486486
uint32_t z_invalid, w0_invalid, h_invalid;
487487
int ret;
488488
MLD_ALLOC(challenge_bytes, uint8_t, MLDSA_CTILDEBYTES);
489-
MLD_ALLOC(y, mld_polyvecl, 1);
489+
MLD_ALLOC(yh, mld_polyveck, 1);
490490
MLD_ALLOC(z, mld_polyvecl, 1);
491491
MLD_ALLOC(w1, mld_polyveck, 1);
492492
MLD_ALLOC(w0, mld_polyveck, 1);
493-
MLD_ALLOC(h, mld_polyveck, 1);
494493
MLD_ALLOC(cp, mld_poly, 1);
494+
mld_polyvecl *y;
495+
mld_polyveck *h;
495496

496-
if (challenge_bytes == NULL || y == NULL || z == NULL || w1 == NULL ||
497-
w0 == NULL || h == NULL || cp == NULL)
497+
if (challenge_bytes == NULL || yh == NULL || z == NULL || w1 == NULL ||
498+
w0 == NULL || cp == NULL)
498499
{
499500
ret = MLD_ERR_OUT_OF_MEMORY;
500501
goto cleanup;
501502
}
502503

504+
/* TODO: Change yh to a union once CBMC issue #8813 is resolved */
505+
y = (mld_polyvecl *)yh;
506+
h = yh;
507+
508+
503509
/* Sample intermediate vector y */
504510
mld_polyvecl_uniform_gamma1(y, rhoprime, nonce);
505511

@@ -611,11 +617,10 @@ __contract__(
611617
cleanup:
612618
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
613619
MLD_FREE(challenge_bytes, uint8_t, MLDSA_CTILDEBYTES);
614-
MLD_FREE(y, mld_polyvecl, 1);
620+
MLD_FREE(yh, mld_polyveck, 1);
615621
MLD_FREE(z, mld_polyvecl, 1);
616622
MLD_FREE(w1, mld_polyveck, 1);
617623
MLD_FREE(w0, mld_polyveck, 1);
618-
MLD_FREE(h, mld_polyveck, 1);
619624
MLD_FREE(cp, mld_poly, 1);
620625

621626
return ret;

0 commit comments

Comments
 (0)