Skip to content

Commit 4e59b6f

Browse files
mkannwischerhanno-becker
authored andcommitted
Consistently use mld_assert instead of cassert
cassert turns into a CBMC proof assertion inside of CBMC proofs, but it turns into a no-op otherwise. mld_assert behaves the save inside of CBMC proofs, but also turns into a run-time assertion in debug builds. Signed-off-by: Matthias J. Kannwischer <[email protected]> Signed-off-by: Hanno Becker <[email protected]>
1 parent 387fbd5 commit 4e59b6f

File tree

5 files changed

+10
-25
lines changed

5 files changed

+10
-25
lines changed

mldsa/fips202/fips202.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ __contract__(
9393
{
9494
s[i] = 0;
9595
}
96-
97-
cassert(forall(k, 0, MLD_KECCAK_LANES, s[k] == 0));
9896
}
9997

10098
/*************************************************

mldsa/polyvec.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,7 @@ void mld_polyvecl_pointwise_acc_montgomery(mld_poly *w, const mld_polyvecl *u,
269269
t += (int64_t)u->vec[j].coeffs[i] * v->vec[j].coeffs[i];
270270
}
271271

272-
/* Substitute j == MLSDA_L into the loop invariant to get... */
273-
cassert(j == MLDSA_L);
274-
cassert(t >= -(int64_t)MLDSA_L * (MLDSA_Q - 1) * (MLD_NTT_BOUND - 1));
275-
cassert(t <= (int64_t)MLDSA_L * (MLDSA_Q - 1) * (MLD_NTT_BOUND - 1));
276-
277-
/* ...and therefore... */
278-
cassert(t >= -MONTGOMERY_REDUCE_STRONG_DOMAIN_MAX);
279-
cassert(t < MONTGOMERY_REDUCE_STRONG_DOMAIN_MAX);
280-
281-
/* ...which meets the "strong" case of mld_montgomery_reduce() */
282272
r = mld_montgomery_reduce(t);
283-
284-
/* ...and therefore we can assert a stronger bound on r */
285-
cassert(r > -MLDSA_Q);
286-
cassert(r < MLDSA_Q);
287273
w->coeffs[i] = r;
288274
}
289275
}

mldsa/reduce.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ int32_t mld_reduce32(int32_t a)
6565

6666
t = (a + (1 << 22)) >> 23;
6767
t = a - t * MLDSA_Q;
68-
cassert((t - a) % MLDSA_Q == 0);
68+
69+
mld_assert((t - a) % MLDSA_Q == 0);
6970
return t;
7071
}
7172

mldsa/rounding.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
#include <stdint.h>
66

7+
#include "debug.h"
78
#include "rounding.h"
89

910

@@ -17,20 +18,20 @@ void mld_decompose(int32_t *a0, int32_t *a1, int32_t a)
1718
{
1819
*a1 = (a + 127) >> 7;
1920
/* We know a >= 0 and a < MLDSA_Q, so... */
20-
cassert(*a1 >= 0 && *a1 <= 65472);
21+
mld_assert(*a1 >= 0 && *a1 <= 65472);
2122

2223
#if MLDSA_MODE == 2
2324
*a1 = (*a1 * 11275 + (1 << 23)) >> 24;
24-
cassert(*a1 >= 0 && *a1 <= 44);
25+
mld_assert(*a1 >= 0 && *a1 <= 44);
2526

2627
*a1 ^= ((43 - *a1) >> 31) & *a1;
27-
cassert(*a1 >= 0 && *a1 <= 43);
28+
mld_assert(*a1 >= 0 && *a1 <= 43);
2829
#else /* MLDSA_MODE == 2 */
2930
*a1 = (*a1 * 1025 + (1 << 21)) >> 22;
30-
cassert(*a1 >= 0 && *a1 <= 16);
31+
mld_assert(*a1 >= 0 && *a1 <= 16);
3132

3233
*a1 &= 15;
33-
cassert(*a1 >= 0 && *a1 <= 15);
34+
mld_assert(*a1 >= 0 && *a1 <= 15);
3435

3536
#endif /* MLDSA_MODE != 2 */
3637

mldsa/sign.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string.h>
77

88
#include "cbmc.h"
9+
#include "debug.h"
910
#include "fips202/fips202.h"
1011
#include "packing.h"
1112
#include "poly.h"
@@ -252,9 +253,7 @@ __contract__(
252253
/* If z is valid, then its coefficients are bounded by */
253254
/* MLDSA_GAMMA1 - MLDSA_BETA. This will be needed below */
254255
/* to prove the pre-condition of pack_sig() */
255-
cassert(forall(k1, 0, MLDSA_L,
256-
array_abs_bound(z.vec[k1].coeffs, 0, MLDSA_N,
257-
(MLDSA_GAMMA1 - MLDSA_BETA))));
256+
mld_assert_abs_bound_2d(z.vec, MLDSA_L, MLDSA_N, (MLDSA_GAMMA1 - MLDSA_BETA));
258257

259258
/* Check that subtracting cs2 does not change high bits of w and low bits
260259
* do not reveal secret information */

0 commit comments

Comments
 (0)