Skip to content

Commit 5ecdabc

Browse files
committed
[SQUASH ME] Address review feedback
Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent e2d613e commit 5ecdabc

File tree

9 files changed

+52
-54
lines changed

9 files changed

+52
-54
lines changed

mldsa/mldsa_native.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
/* mldsa/src/packing.h */
224224
#undef MLD_PACKING_H
225225
#undef mld_pack_pk
226-
#undef mld_pack_sig
226+
#undef mld_pack_sig_c_h
227227
#undef mld_pack_sig_z
228228
#undef mld_pack_sk
229229
#undef mld_unpack_pk

mldsa/mldsa_native.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@
219219
/* mldsa/src/packing.h */
220220
#undef MLD_PACKING_H
221221
#undef mld_pack_pk
222-
#undef mld_pack_sig
222+
#undef mld_pack_sig_c_h
223223
#undef mld_pack_sig_z
224224
#undef mld_pack_sk
225225
#undef mld_unpack_pk

mldsa/src/packing.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ void mld_unpack_sk(uint8_t rho[MLDSA_SEEDBYTES], uint8_t tr[MLDSA_TRBYTES],
9999
}
100100

101101
MLD_INTERNAL_API
102-
void mld_pack_sig(uint8_t sig[MLDSA_CRYPTO_BYTES],
103-
const uint8_t c[MLDSA_CTILDEBYTES], const mld_polyveck *h,
104-
const unsigned int number_of_hints)
102+
void mld_pack_sig_c_h(uint8_t sig[MLDSA_CRYPTO_BYTES],
103+
const uint8_t c[MLDSA_CTILDEBYTES], const mld_polyveck *h,
104+
const unsigned int number_of_hints)
105105
{
106106
unsigned int i, j, k;
107107

mldsa/src/packing.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ __contract__(
6969
);
7070

7171

72-
#define mld_pack_sig MLD_NAMESPACE_KL(pack_sig)
72+
#define mld_pack_sig_c_h MLD_NAMESPACE_KL(pack_sig_c_h)
7373
/*************************************************
74-
* Name: mld_pack_sig
74+
* Name: mld_pack_sig_c_h
7575
*
7676
* Description: Bit-pack c and h component of sig = (c, z, h).
7777
* The z component is packed separately using mld_pack_sig_z.
@@ -88,9 +88,9 @@ __contract__(
8888
* proof of type safety.
8989
**************************************************/
9090
MLD_INTERNAL_API
91-
void mld_pack_sig(uint8_t sig[MLDSA_CRYPTO_BYTES],
92-
const uint8_t c[MLDSA_CTILDEBYTES], const mld_polyveck *h,
93-
const unsigned int number_of_hints)
91+
void mld_pack_sig_c_h(uint8_t sig[MLDSA_CRYPTO_BYTES],
92+
const uint8_t c[MLDSA_CTILDEBYTES], const mld_polyveck *h,
93+
const unsigned int number_of_hints)
9494
__contract__(
9595
requires(memory_no_alias(sig, MLDSA_CRYPTO_BYTES))
9696
requires(memory_no_alias(c, MLDSA_CTILDEBYTES))
@@ -106,7 +106,8 @@ __contract__(
106106
* Name: mld_pack_sig_z
107107
*
108108
* Description: Bit-pack single polynomial of z component of sig = (c, z, h).
109-
* The c and h components are packed separately using mld_pack_sig.
109+
* The c and h components are packed separately using
110+
* mld_pack_sig_c_h.
110111
*
111112
* Arguments: - uint8_t sig[]: output byte array
112113
* - const mld_poly *zi: pointer to a single polynomial in z

mldsa/src/sign.c

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -457,34 +457,29 @@ __contract__(
457457
MLD_MUST_CHECK_RETURN_VALUE
458458
static int mld_compute_pack_z(uint8_t sig[MLDSA_CRYPTO_BYTES],
459459
const mld_poly *cp, const mld_polyvecl *s1,
460-
const mld_polyvecl *y)
460+
const mld_polyvecl *y, mld_poly *z)
461461
__contract__(
462462
requires(memory_no_alias(sig, MLDSA_CRYPTO_BYTES))
463463
requires(memory_no_alias(cp, sizeof(mld_poly)))
464464
requires(memory_no_alias(s1, sizeof(mld_polyvecl)))
465465
requires(memory_no_alias(y, sizeof(mld_polyvecl)))
466+
requires(memory_no_alias(z, sizeof(mld_poly)))
466467
requires(array_abs_bound(cp->coeffs, 0, MLDSA_N, MLD_NTT_BOUND))
467468
requires(forall(k0, 0, MLDSA_L,
468469
array_bound(y->vec[k0].coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1)))
469470
requires(forall(k1, 0, MLDSA_L, array_abs_bound(s1->vec[k1].coeffs, 0, MLDSA_N, MLD_NTT_BOUND)))
470471
assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES))
472+
assigns(memory_slice(z, sizeof(mld_poly)))
471473
ensures(return_value == 0 || return_value == MLD_ERR_FAIL ||
472474
return_value == MLD_ERR_OUT_OF_MEMORY)
473475
)
474476
{
475477
unsigned int i;
476478
uint32_t z_invalid;
477-
int ret;
478-
MLD_ALLOC(z, mld_poly, 1);
479-
if (z == NULL)
480-
{
481-
ret = MLD_ERR_OUT_OF_MEMORY;
482-
goto cleanup;
483-
}
484479
for (i = 0; i < MLDSA_L; i++)
485480
__loop__(
486481
assigns(i, memory_slice(z, sizeof(mld_poly)), memory_slice(sig, MLDSA_CRYPTO_BYTES))
487-
invariant(i <= MLDSA_L)
482+
invariant(i <= MLDSA_L)
488483
)
489484
{
490485
mld_poly_pointwise_montgomery(z, cp, &s1->vec[i]);
@@ -494,38 +489,37 @@ __contract__(
494489

495490
z_invalid = mld_poly_chknorm(z, MLDSA_GAMMA1 - MLDSA_BETA);
496491
/* Constant time: It is fine (and prohibitively expensive to avoid)
497-
* leaking the result of the norm check. In case of rejection it
498-
* would even be okay to leak which coefficient led to rejection
499-
* as the candidate signature will be discarded anyway.
492+
* to leak the result of the norm check and which polynomial in z caused a
493+
* rejection. It would even be okay to leak which coefficient led to
494+
* rejection as the candidate signature will be discarded anyway.
500495
* See Section 5.5 of @[Round3_Spec]. */
501496
MLD_CT_TESTING_DECLASSIFY(&z_invalid, sizeof(uint32_t));
502497
if (z_invalid)
503498
{
504-
ret = MLD_ERR_FAIL; /* reject */
505-
goto cleanup;
499+
return MLD_ERR_FAIL; /* reject */
506500
}
507-
/* If z is valid, then its coefficients are bounded by */
508-
/* MLDSA_GAMMA1 - MLDSA_BETA. This will be needed below */
509-
/* to prove the pre-condition of pack_sig_z() */
501+
/* If z is valid, then its coefficients are bounded by
502+
* MLDSA_GAMMA1 - MLDSA_BETA. This will be needed below
503+
* to prove the pre-condition of pack_sig_z() */
510504
mld_assert_abs_bound(z, MLDSA_N, (MLDSA_GAMMA1 - MLDSA_BETA));
505+
506+
/* After the norm check, the distribution of each coefficient of z is
507+
* independent of the secret key and it can, hence, be considered
508+
* public. It is, hence, okay to immediately pack it into the user-provided
509+
* signature buffer. */
511510
mld_pack_sig_z(sig, z, i);
512511
}
513-
ret = 0;
514-
515-
cleanup:
516-
MLD_FREE(z, mld_poly, 1);
517-
return ret;
512+
return 0;
518513
}
519514

520-
/* Reference: The reference implementation does not explicitly */
521-
/* check the maximum nonce value, but instead loops indefinitely */
522-
/* (even when the nonce would overflow). Internally, */
523-
/* sampling of y uses (nonceL), (nonceL+1), ... (nonce*L+L-1). */
524-
/* Hence, there are no overflows if nonce < (UINT16_MAX - L)/L. */
525-
/* Explicitly checking for this explicitly allows us to prove */
526-
/* type-safety. Note that FIPS204 explicitly allows an upper- */
527-
/* bound this loop of 814 (< (UINT16_MAX - L)/L) - see */
528-
/* @[FIPS204, Appendix C]. */
515+
/* Reference: The reference implementation does not explicitly check the
516+
* maximum nonce value, but instead loops indefinitely (even when the nonce
517+
* would overflow). Internally, sampling of y uses
518+
* (nonceL), (nonceL+1), ... (nonce*L+L-1).
519+
* Hence, there are no overflows if nonce < (UINT16_MAX - L)/L.
520+
* Explicitly checking for this explicitly allows us to prove type-safety.
521+
* Note that FIPS204 explicitly allows an upper-bound this loop of
522+
* 814 (< (UINT16_MAX - L)/L) - see @[FIPS204, Appendix C]. */
529523
#define MLD_NONCE_UB ((UINT16_MAX - MLDSA_L) / MLDSA_L)
530524

531525
/*************************************************
@@ -608,9 +602,10 @@ __contract__(
608602
MLD_ALLOC(w1tmp, w1tmp_u, 1);
609603
MLD_ALLOC(w0, mld_polyveck, 1);
610604
MLD_ALLOC(cp, mld_poly, 1);
605+
MLD_ALLOC(t, mld_poly, 1);
611606

612607
if (challenge_bytes == NULL || yh == NULL || z == NULL || w1tmp == NULL ||
613-
w0 == NULL || cp == NULL)
608+
w0 == NULL || cp == NULL || t == NULL)
614609
{
615610
ret = MLD_ERR_OUT_OF_MEMORY;
616611
goto cleanup;
@@ -646,13 +641,12 @@ __contract__(
646641
mld_poly_ntt(cp);
647642

648643
/* Compute z, reject if it reveals secret */
649-
ret = mld_compute_pack_z(sig, cp, s1, y);
644+
ret = mld_compute_pack_z(sig, cp, s1, y, t);
650645
if (ret)
651646
{
652647
goto cleanup;
653648
}
654649

655-
656650
/* Check that subtracting cs2 does not change high bits of w and low bits
657651
* do not reveal secret information */
658652
mld_polyveck_pointwise_poly_montgomery(h, cp, s2);
@@ -703,14 +697,15 @@ __contract__(
703697
}
704698

705699
/* All is well - write signature */
706-
mld_pack_sig(sig, challenge_bytes, h, n);
700+
mld_pack_sig_c_h(sig, challenge_bytes, h, n);
707701
/* Constant time: At this point it is clear that the signature is valid - it
708702
* can, hence, be considered public. */
709703
MLD_CT_TESTING_DECLASSIFY(sig, MLDSA_CRYPTO_BYTES);
710704
ret = 0; /* success */
711705

712706
cleanup:
713707
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
708+
MLD_FREE(t, mld_poly, 1);
714709
MLD_FREE(cp, mld_poly, 1);
715710
MLD_FREE(w0, mld_polyveck, 1);
716711
MLD_FREE(w1tmp, w1tmp_u, 1);

proofs/cbmc/attempt_signature_generation/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)polyvecl_uniform_gamma1 \
3737
$(MLD_NAMESPACE)polyveck_chknorm \
3838
$(MLD_NAMESPACE)polyveck_add \
3939
$(MLD_NAMESPACE)polyveck_make_hint \
40-
$(MLD_NAMESPACE)pack_sig \
40+
$(MLD_NAMESPACE)pack_sig_c_h \
4141
mld_compute_pack_z
4242
USE_FUNCTION_CONTRACTS+=mld_zeroize
4343

proofs/cbmc/compute_pack_z/compute_pack_z_harness.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
#include "sign.h"
55

66
int mld_compute_pack_z(uint8_t sig[MLDSA_CRYPTO_BYTES], const mld_poly *cp,
7-
const mld_polyvecl *s1, const mld_polyvecl *y);
7+
const mld_polyvecl *s1, const mld_polyvecl *y,
8+
mld_poly *t);
89

910
void harness(void)
1011
{
1112
uint8_t *sig;
1213
mld_poly *cp;
1314
mld_polyvecl *s1;
1415
mld_polyvecl *y;
16+
mld_poly *t;
1517

1618
int r;
17-
r = mld_compute_pack_z(sig, cp, s1, y);
19+
r = mld_compute_pack_z(sig, cp, s1, y, t);
1820
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
include ../Makefile_params.common
55

66
HARNESS_ENTRY = harness
7-
HARNESS_FILE = pack_sig_harness
7+
HARNESS_FILE = pack_sig_c_h_harness
88

99
# This should be a unique identifier for this proof, and will appear on the
1010
# Litani dashboard. It can be human-readable and contain spaces if you wish.
11-
PROOF_UID = pack_sig
11+
PROOF_UID = pack_sig_c_h
1212

1313
DEFINES +=
1414
INCLUDES +=
@@ -18,7 +18,7 @@ REMOVE_FUNCTION_BODY +=
1818
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
1919
PROJECT_SOURCES += $(SRCDIR)/mldsa/src/packing.c
2020

21-
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)pack_sig
21+
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)pack_sig_c_h
2222
USE_FUNCTION_CONTRACTS=
2323
APPLY_LOOP_CONTRACTS=on
2424
USE_DYNAMIC_FRAMES=1
@@ -28,7 +28,7 @@ EXTERNAL_SAT_SOLVER=
2828
CBMCFLAGS=--smt2
2929
CBMCFLAGS+=--slice-formula
3030

31-
FUNCTION_NAME = pack_sig
31+
FUNCTION_NAME = pack_sig_c_h
3232

3333
# If this proof is found to consume huge amounts of RAM, you can set the
3434
# EXPENSIVE variable. With new enough versions of the proof tools, this will

proofs/cbmc/pack_sig/pack_sig_harness.c renamed to proofs/cbmc/pack_sig_c_h/pack_sig_c_h_harness.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ void harness(void)
99
uint8_t *a, *b;
1010
mld_polyveck *h;
1111
unsigned int nh;
12-
mld_pack_sig(a, b, h, nh);
12+
mld_pack_sig_c_h(a, b, h, nh);
1313
}

0 commit comments

Comments
 (0)