Skip to content

Commit cea4d7c

Browse files
committed
CBMC: Remove convoluted implementation of 2D bounds check
A previous issue in CBMC diffblue/cbmc#8570 had forced to break bounds assertions over a 2D array into nested bounds assertions. This issue has since been fixed, allowing for the simplification of the respective assertions, which is what this commit does. Signed-off-by: Hanno Becker <[email protected]>
1 parent 8dba13b commit cea4d7c

File tree

2 files changed

+8
-44
lines changed

2 files changed

+8
-44
lines changed

mlkem/debug.h

+5-11
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,11 @@ void mlk_debug_check_bounds(const char *file, int line, const int16_t *ptr,
8787
#define mlk_assert_abs_bound(ptr, len, value_abs_bd) \
8888
cassert(array_abs_bound(((int16_t *)(ptr)), 0, (len), (value_abs_bd)))
8989

90-
/* Because of https://github.com/diffblue/cbmc/issues/8570, we can't
91-
* just use a single flattened array_bound(...) here. */
92-
#define mlk_assert_bound_2d(ptr, M, N, value_lb, value_ub) \
93-
cassert(forall(kN, 0, (M), \
94-
array_bound(&((int16_t(*)[(N)])(ptr))[kN][0], 0, (N), \
95-
(value_lb), (value_ub))))
96-
97-
#define mlk_assert_abs_bound_2d(ptr, M, N, value_abs_bd) \
98-
cassert(forall(kN, 0, (M), \
99-
array_abs_bound(&((int16_t(*)[(N)])(ptr))[kN][0], 0, (N), \
100-
(value_abs_bd))))
90+
#define mlk_assert_bound_2d(ptr, M, N, value_lb, value_ub) \
91+
mlk_assert_bound((ptr), (N) * (M), (value_lb), (value_ub))
92+
93+
#define mlk_assert_abs_bound_2d(ptr, M, N, value_abs_bd) \
94+
mlk_assert_abs_bound((ptr), (N) * (M), (value_abs_bd))
10195

10296
#else /* MLKEM_DEBUG */
10397

mlkem/native/api.h

+3-33
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,7 @@ __contract__(
238238
requires(memory_no_alias(a, sizeof(int16_t) * 2 * MLKEM_N))
239239
requires(memory_no_alias(b, sizeof(int16_t) * 2 * MLKEM_N))
240240
requires(memory_no_alias(b_cache, sizeof(int16_t) * 2 * (MLKEM_N / 2)))
241-
/* Because of https://github.com/diffblue/cbmc/issues/8570, we can't
242-
* just use a single flattened array_bound(...) here.
243-
*
244-
* Once fixed, change to:
245-
* ```
246-
* requires(array_bound(a, 0, 2 * MLKEM_N, 0, MLKEM_UINT12_LIMIT))
247-
* ```
248-
*/
249-
requires(forall(kN, 0, 2, \
250-
array_bound(&((int16_t(*)[MLKEM_N])(a))[kN][0], 0, MLKEM_N, \
251-
0, MLKEM_UINT12_LIMIT)))
241+
requires(array_bound(a, 0, 2 * MLKEM_N, 0, MLKEM_UINT12_LIMIT))
252242
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
253243
);
254244
#endif /* MLK_MULTILEVEL_BUILD_WITH_SHARED || MLKEM_K == 2 */
@@ -281,17 +271,7 @@ __contract__(
281271
requires(memory_no_alias(a, sizeof(int16_t) * 3 * MLKEM_N))
282272
requires(memory_no_alias(b, sizeof(int16_t) * 3 * MLKEM_N))
283273
requires(memory_no_alias(b_cache, sizeof(int16_t) * 3 * (MLKEM_N / 2)))
284-
/* Because of https://github.com/diffblue/cbmc/issues/8570, we can't
285-
* just use a single flattened array_bound(...) here.
286-
*
287-
* Once fixed, change to:
288-
* ```
289-
* requires(array_bound(a, 0, 3 * MLKEM_N, 0, MLKEM_UINT12_LIMIT))
290-
* ```
291-
*/
292-
requires(forall(kN, 0, 3, \
293-
array_bound(&((int16_t(*)[MLKEM_N])(a))[kN][0], 0, MLKEM_N, \
294-
0, MLKEM_UINT12_LIMIT)))
274+
requires(array_bound(a, 0, 3 * MLKEM_N, 0, MLKEM_UINT12_LIMIT))
295275
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
296276
);
297277
#endif /* MLK_MULTILEVEL_BUILD_WITH_SHARED || MLKEM_K == 3 */
@@ -324,17 +304,7 @@ __contract__(
324304
requires(memory_no_alias(a, sizeof(int16_t) * 4 * MLKEM_N))
325305
requires(memory_no_alias(b, sizeof(int16_t) * 4 * MLKEM_N))
326306
requires(memory_no_alias(b_cache, sizeof(int16_t) * 4 * (MLKEM_N / 2)))
327-
/* Because of https://github.com/diffblue/cbmc/issues/8570, we can't
328-
* just use a single flattened array_bound(...) here.
329-
*
330-
* Once fixed, change to:
331-
* ```
332-
* requires(array_bound(a, 0, 4 * MLKEM_N, 0, MLKEM_UINT12_LIMIT))
333-
* ```
334-
*/
335-
requires(forall(kN, 0, 4, \
336-
array_bound(&((int16_t(*)[MLKEM_N])(a))[kN][0], 0, MLKEM_N, \
337-
0, MLKEM_UINT12_LIMIT)))
307+
requires(array_bound(a, 0, 4 * MLKEM_N, 0, MLKEM_UINT12_LIMIT))
338308
assigns(memory_slice(r, sizeof(int16_t) * MLKEM_N))
339309
);
340310
#endif /* MLK_MULTILEVEL_BUILD_WITH_SHARED || MLKEM_K == 4 */

0 commit comments

Comments
 (0)