From 244bfe874085d7fd59472b260711c1ed590cb33f Mon Sep 17 00:00:00 2001 From: Ben Edgington Date: Thu, 24 Jun 2021 16:22:37 +0100 Subject: [PATCH] Add ASSERT macro --- src/c_kzg.h | 31 ++++++++++++++++++++++++++++--- src/recover.c | 4 ++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/c_kzg.h b/src/c_kzg.h index 4b5bc26..80a3199 100644 --- a/src/c_kzg.h +++ b/src/c_kzg.h @@ -42,7 +42,7 @@ typedef enum { #include #define CHECK(cond) \ if (!(cond)) { \ - printf("\n%s:%d: Failed ASSERT: %s\n", __FILE__, __LINE__, #cond); \ + printf("\n%s:%d: Failed CHECK: %s\n", __FILE__, __LINE__, #cond); \ abort(); \ } #define TRY(result) \ @@ -53,6 +53,11 @@ typedef enum { abort(); \ } \ } +#define ASSERT(cond) \ + if (!(cond)) { \ + printf("\n%s:%d: Failed ASSERT: %s\n", __FILE__, __LINE__, #cond); \ + abort(); \ + } #else #define CHECK(cond) \ if (!(cond)) return C_KZG_BADARGS @@ -62,11 +67,15 @@ typedef enum { if (ret == C_KZG_MALLOC) return ret; \ if (ret != C_KZG_OK) return C_KZG_ERROR; \ } +#define ASSERT(cond) \ + if (!(cond)) return C_KZG_ERROR #endif // DEBUG /** @def CHECK * - * Handle errors. + * Test input parameters. + * + * Differs from `ASSERT` in returning `C_KZG_BADARGS`. * * This macro comes in two versions according to whether `DEBUG` is defined or not (`-DDEBUG` compiler flag). * - `DEBUG` is undefined: when @p cond is false, return from the current function with the value `C_KZG_BADARGS`, @@ -79,7 +88,7 @@ typedef enum { /** @def TRY * - * Handle errors. + * Handle errors in called functions. * * This macro comes in two versions according to whether `DEBUG` is defined or not (`-DDEBUG` compiler flag). * - `DEBUG` is undefined: if the @p result is not `C_KZG_OK`, return immediately with either `C_KZG_MALLOC` or @@ -89,4 +98,20 @@ typedef enum { * * @param result The function call result to be tested */ + +/** @def ASSERT + * + * Test the correctness of statements. + * + * Differs from `CHECK` in returning `C_KZG_ERROR`. + * + * This macro comes in two versions according to whether `DEBUG` is defined or not (`-DDEBUG` compiler flag). + * - `DEBUG` is undefined: when @p cond is false, return from the current function with the value `C_KZG_ERROR`, + * otherwise continue. + * - `DEBUG` is defined: when @p cond is false, print file and line number information and abort the run. This is very + * useful for dubugging. The @p ret parameter is ignored in this case. + * + * @param cond The condition to be tested + */ + #endif // C_KZG_H diff --git a/src/recover.c b/src/recover.c index a5c9409..920a4a5 100644 --- a/src/recover.c +++ b/src/recover.c @@ -124,7 +124,7 @@ C_KZG_RET recover_poly_from_samples(fr_t *reconstructed_data, fr_t *samples, uin // Check all is well for (uint64_t i = 0; i < len_samples; i++) { - TRY(fr_is_null(&samples[i]) == fr_is_zero(&zero_eval[i]) ? C_KZG_OK : C_KZG_ERROR); + ASSERT(fr_is_null(&samples[i]) == fr_is_zero(&zero_eval[i])); } // Construct E * Z_r,I: the loop makes the evaluation polynomial @@ -170,7 +170,7 @@ C_KZG_RET recover_poly_from_samples(fr_t *reconstructed_data, fr_t *samples, uin // Check all is well for (uint64_t i = 0; i < len_samples; i++) { - TRY(fr_is_null(&samples[i]) || fr_equal(&reconstructed_data[i], &samples[i]) ? C_KZG_OK : C_KZG_ERROR); + ASSERT(fr_is_null(&samples[i]) || fr_equal(&reconstructed_data[i], &samples[i])); } free(scratch);