Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/examples/basic/mldsa_native/src/debug.h

raw
1 /*
2 * Copyright (c) The mlkem-native project authors
3 * Copyright (c) The mldsa-native project authors
4 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
5 */
6 #ifndef MLD_DEBUG_H
7 #define MLD_DEBUG_H
8 #include "common.h"
9
10 #if defined(MLDSA_DEBUG)
11 #include <stdint.h>
12
13 /*************************************************
14 * Name: mld_assert
15 *
16 * Description: Check debug assertion
17 *
18 * Prints an error message to stderr and calls
19 * exit(1) if not.
20 *
21 * Arguments: - file: filename
22 * - line: line number
23 * - val: Value asserted to be non-zero
24 **************************************************/
25 #define mld_debug_check_assert MLD_NAMESPACE(mldsa_debug_assert)
26 void mld_debug_check_assert(const char *file, int line, const int val);
27
28 /*************************************************
29 * Name: mld_debug_check_bounds
30 *
31 * Description: Check whether values in an array of int32_t
32 * are within specified bounds.
33 *
34 * Prints an error message to stderr and calls
35 * exit(1) if not.
36 *
37 * Arguments: - file: filename
38 * - line: line number
39 * - ptr: Base of array to be checked
40 * - len: Number of int32_t in ptr
41 * - lower_bound_exclusive: Exclusive lower bound
42 * - upper_bound_exclusive: Exclusive upper bound
43 **************************************************/
44 #define mld_debug_check_bounds MLD_NAMESPACE(mldsa_debug_check_bounds)
45 void mld_debug_check_bounds(const char *file, int line, const int32_t *ptr,
46 unsigned len, int64_t lower_bound_exclusive,
47 int64_t upper_bound_exclusive);
48
49 /* Check assertion, calling exit() upon failure
50 *
51 * val: Value that's asserted to be non-zero
52 */
53 #define mld_assert(val) mld_debug_check_assert(__FILE__, __LINE__, (val))
54
55 /* Check bounds in array of int32_t's
56 * ptr: Base of int32_t array; will be explicitly cast to int32_t*,
57 * so you may pass a byte-compatible type such as mld_poly or mld_polyvec.
58 * len: Number of int32_t in array
59 * value_lb: Inclusive lower value bound
60 * value_ub: Exclusive upper value bound */
61 #define mld_assert_bound(ptr, len, value_lb, value_ub) \
62 mld_debug_check_bounds(__FILE__, __LINE__, (const int32_t *)(ptr), (len), \
63 ((int64_t)(value_lb)) - 1, (value_ub))
64
65 /* Check absolute bounds in array of int32_t's
66 * ptr: Base of array, expression of type int32_t*
67 * len: Number of int32_t in array
68 * value_abs_bd: Exclusive absolute upper bound */
69 #define mld_assert_abs_bound(ptr, len, value_abs_bd) \
70 mld_assert_bound((ptr), (len), (-((int64_t)(value_abs_bd)) + 1), \
71 (value_abs_bd))
72
73 /* Version of bounds assertions for 2-dimensional arrays */
74 #define mld_assert_bound_2d(ptr, len0, len1, value_lb, value_ub) \
75 mld_assert_bound((ptr), ((len0) * (len1)), (value_lb), (value_ub))
76
77 #define mld_assert_abs_bound_2d(ptr, len0, len1, value_abs_bd) \
78 mld_assert_abs_bound((ptr), ((len0) * (len1)), (value_abs_bd))
79
80 /* When running CBMC, convert debug assertions into proof obligations */
81 #elif defined(CBMC)
82 #include "cbmc.h"
83
84 #define mld_assert(val) cassert(val)
85
86 #define mld_assert_bound(ptr, len, value_lb, value_ub) \
87 cassert(array_bound(((int32_t *)(ptr)), 0, (len), (value_lb), (value_ub)))
88
89 #define mld_assert_abs_bound(ptr, len, value_abs_bd) \
90 cassert(array_abs_bound(((int32_t *)(ptr)), 0, (len), (value_abs_bd)))
91
92 /* Because of https://github.com/diffblue/cbmc/issues/8570, we can't
93 * just use a single flattened array_bound(...) here. */
94 #define mld_assert_bound_2d(ptr, M, N, value_lb, value_ub) \
95 cassert(forall(kN, 0, (M), \
96 array_bound(&((int32_t(*)[(N)])(ptr))[kN][0], 0, (N), \
97 (value_lb), (value_ub))))
98
99 #define mld_assert_abs_bound_2d(ptr, M, N, value_abs_bd) \
100 cassert(forall(kN, 0, (M), \
101 array_abs_bound(&((int32_t(*)[(N)])(ptr))[kN][0], 0, (N), \
102 (value_abs_bd))))
103
104 #else /* !MLDSA_DEBUG && CBMC */
105
106 #define mld_assert(val) \
107 do \
108 { \
109 } while (0)
110 #define mld_assert_bound(ptr, len, value_lb, value_ub) \
111 do \
112 { \
113 } while (0)
114 #define mld_assert_abs_bound(ptr, len, value_abs_bd) \
115 do \
116 { \
117 } while (0)
118
119 #define mld_assert_bound_2d(ptr, len0, len1, value_lb, value_ub) \
120 do \
121 { \
122 } while (0)
123
124 #define mld_assert_abs_bound_2d(ptr, len0, len1, value_abs_bd) \
125 do \
126 { \
127 } while (0)
128
129
130 #endif /* !MLDSA_DEBUG && !CBMC */
131 #endif /* !MLD_DEBUG_H */