Skip to content

Commit d725be5

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 d725be5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

mldsa/src/sign.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,20 +486,27 @@ __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
505+
* https://github.com/diffblue/cbmc/issues/8813 is resolved */
506+
y = (mld_polyvecl *)yh;
507+
h = yh;
508+
509+
503510
/* Sample intermediate vector y */
504511
mld_polyvecl_uniform_gamma1(y, rhoprime, nonce);
505512

@@ -611,11 +618,10 @@ __contract__(
611618
cleanup:
612619
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
613620
MLD_FREE(challenge_bytes, uint8_t, MLDSA_CTILDEBYTES);
614-
MLD_FREE(y, mld_polyvecl, 1);
621+
MLD_FREE(yh, mld_polyveck, 1);
615622
MLD_FREE(z, mld_polyvecl, 1);
616623
MLD_FREE(w1, mld_polyveck, 1);
617624
MLD_FREE(w0, mld_polyveck, 1);
618-
MLD_FREE(h, mld_polyveck, 1);
619625
MLD_FREE(cp, mld_poly, 1);
620626

621627
return ret;

0 commit comments

Comments
 (0)