Debian Codegraph @

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

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
7 /* NOTE: You can remove this file unless you compile with MLDSA_DEBUG. */
8
9 #include "common.h"
10
11 #if !defined(MLD_CONFIG_MULTILEVEL_NO_SHARED)
12
13 #if defined(MLDSA_DEBUG)
14
15 #include <inttypes.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include "debug.h"
19
20 #define MLD_DEBUG_ERROR_HEADER "[ERROR:%s:%04d] "
21
22 void mld_debug_check_assert(const char *file, int line, const int val)
23 {
24 if (val == 0)
25 {
26 fprintf(stderr, MLD_DEBUG_ERROR_HEADER "Assertion failed (value %d)\n",
27 file, line, val);
28 exit(1);
29 }
30 }
31
32 void mld_debug_check_bounds(const char *file, int line, const int32_t *ptr,
33 unsigned len, int64_t lower_bound_exclusive,
34 int64_t upper_bound_exclusive)
35 {
36 int err = 0;
37 unsigned i;
38 for (i = 0; i < len; i++)
39 {
40 int32_t val = ptr[i];
41 if (!(val > lower_bound_exclusive && val < upper_bound_exclusive))
42 {
43 fprintf(stderr,
44 MLD_DEBUG_ERROR_HEADER
45 "Bounds assertion failed: Index %u, value %d out of bounds "
46 "(%" PRId64 ",%" PRId64 ")\n",
47 file, line, i, (int)val, lower_bound_exclusive,
48 upper_bound_exclusive);
49 err = 1;
50 }
51 }
52
53 if (err == 1)
54 {
55 exit(1);
56 }
57 }
58
59 #else /* MLDSA_DEBUG */
60
61 MLD_EMPTY_CU(debug)
62
63 #endif /* !MLDSA_DEBUG */
64
65 #else /* !MLD_CONFIG_MULTILEVEL_NO_SHARED */
66
67 MLD_EMPTY_CU(debug)
68
69 #endif /* MLD_CONFIG_MULTILEVEL_NO_SHARED */
70
71 /* To facilitate single-compilation-unit (SCU) builds, undefine all macros.
72 * Don't modify by hand -- this is auto-generated by scripts/autogen. */
73 #undef MLD_DEBUG_ERROR_HEADER