Change ASSERT to CHECK and always fail with C_KZG_BADARGS

This commit is contained in:
Ben Edgington 2021-02-17 09:24:12 +00:00
parent f557f32ed1
commit 39e190984f
7 changed files with 39 additions and 40 deletions

View File

@ -40,7 +40,7 @@ typedef enum {
#ifdef DEBUG #ifdef DEBUG
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#define ASSERT(cond, ret) \ #define CHECK(cond) \
if (!(cond)) { \ if (!(cond)) { \
printf("\n%s:%d: Failed ASSERT: %s\n", __FILE__, __LINE__, #cond); \ printf("\n%s:%d: Failed ASSERT: %s\n", __FILE__, __LINE__, #cond); \
abort(); \ abort(); \
@ -54,8 +54,8 @@ typedef enum {
} \ } \
} }
#else #else
#define ASSERT(cond, ret) \ #define CHECK(cond) \
if (!(cond)) return (ret) if (!(cond)) return C_KZG_BADARGS
#define TRY(result) \ #define TRY(result) \
{ \ { \
C_KZG_RET ret = (result); \ C_KZG_RET ret = (result); \
@ -64,18 +64,17 @@ typedef enum {
} }
#endif // DEBUG #endif // DEBUG
/** @def ASSERT /** @def CHECK
* *
* Handle errors. * Handle errors.
* *
* This macro comes in two versions according to whether `DEBUG` is defined or not (`-DDEBUG` compiler flag). * 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 @p ret, otherwise * - `DEBUG` is undefined: when @p cond is false, return from the current function with the value `C_KZG_BADARGS`,
* continue. * otherwise continue.
* - `DEBUG` is defined: when @p cond is false, print file and line number information and abort the run. This is very * - `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. * useful for dubugging. The @p ret parameter is ignored in this case.
* *
* @param cond The condition to be tested * @param cond The condition to be tested
* @param ret The return code to be returned in case the condition is false
*/ */
/** @def TRY /** @def TRY

View File

@ -54,10 +54,10 @@ C_KZG_RET expand_root_of_unity(fr_t *out, const fr_t *root, uint64_t width) {
out[1] = *root; out[1] = *root;
for (uint64_t i = 2; !fr_is_one(&out[i - 1]); i++) { for (uint64_t i = 2; !fr_is_one(&out[i - 1]); i++) {
ASSERT(i <= width, C_KZG_BADARGS); CHECK(i <= width);
fr_mul(&out[i], &out[i - 1], root); fr_mul(&out[i], &out[i - 1], root);
} }
ASSERT(fr_is_one(&out[width]), C_KZG_BADARGS); CHECK(fr_is_one(&out[width]));
return C_KZG_OK; return C_KZG_OK;
} }
@ -85,7 +85,7 @@ C_KZG_RET expand_root_of_unity(fr_t *out, const fr_t *root, uint64_t width) {
C_KZG_RET new_fft_settings(FFTSettings *fs, unsigned int max_scale) { C_KZG_RET new_fft_settings(FFTSettings *fs, unsigned int max_scale) {
fs->max_width = (uint64_t)1 << max_scale; fs->max_width = (uint64_t)1 << max_scale;
ASSERT((max_scale < sizeof scale2_root_of_unity / sizeof scale2_root_of_unity[0]), C_KZG_BADARGS); CHECK((max_scale < sizeof scale2_root_of_unity / sizeof scale2_root_of_unity[0]));
fr_from_uint64s(&fs->root_of_unity, scale2_root_of_unity[max_scale]); fr_from_uint64s(&fs->root_of_unity, scale2_root_of_unity[max_scale]);
// Allocate space for the roots of unity // Allocate space for the roots of unity

View File

@ -95,8 +95,8 @@ void fft_fr_fast(fr_t *out, const fr_t *in, uint64_t stride, const fr_t *roots,
*/ */
C_KZG_RET fft_fr(fr_t *out, const fr_t *in, bool inverse, uint64_t n, const FFTSettings *fs) { C_KZG_RET fft_fr(fr_t *out, const fr_t *in, bool inverse, uint64_t n, const FFTSettings *fs) {
uint64_t stride = fs->max_width / n; uint64_t stride = fs->max_width / n;
ASSERT(n <= fs->max_width, C_KZG_BADARGS); CHECK(n <= fs->max_width);
ASSERT(is_power_of_two(n), C_KZG_BADARGS); CHECK(is_power_of_two(n));
if (inverse) { if (inverse) {
fr_t inv_len; fr_t inv_len;
fr_from_uint64(&inv_len, n); fr_from_uint64(&inv_len, n);

View File

@ -96,8 +96,8 @@ void fft_g1_fast(g1_t *out, const g1_t *in, uint64_t stride, const fr_t *roots,
*/ */
C_KZG_RET fft_g1(g1_t *out, const g1_t *in, bool inverse, uint64_t n, const FFTSettings *fs) { C_KZG_RET fft_g1(g1_t *out, const g1_t *in, bool inverse, uint64_t n, const FFTSettings *fs) {
uint64_t stride = fs->max_width / n; uint64_t stride = fs->max_width / n;
ASSERT(n <= fs->max_width, C_KZG_BADARGS); CHECK(n <= fs->max_width);
ASSERT(is_power_of_two(n), C_KZG_BADARGS); CHECK(is_power_of_two(n));
if (inverse) { if (inverse) {
fr_t inv_len; fr_t inv_len;
fr_from_uint64(&inv_len, n); fr_from_uint64(&inv_len, n);

View File

@ -88,8 +88,8 @@ uint32_t reverse_bits_limited(uint32_t n, uint32_t value) {
* @retval C_CZK_BADARGS Invalid parameters were supplied * @retval C_CZK_BADARGS Invalid parameters were supplied
*/ */
C_KZG_RET reverse_bit_order(void *values, size_t size, uint64_t n) { C_KZG_RET reverse_bit_order(void *values, size_t size, uint64_t n) {
ASSERT(n >> 32 == 0, C_KZG_BADARGS); CHECK(n >> 32 == 0);
ASSERT(is_power_of_two(n), C_KZG_BADARGS); CHECK(is_power_of_two(n));
byte tmp[size]; byte tmp[size];
int unused_bit_len = 32 - log2_pow2(n); int unused_bit_len = 32 - log2_pow2(n);
@ -151,7 +151,7 @@ C_KZG_RET toeplitz_part_1(g1_t *out, const g1_t *x, uint64_t n, const FFTSetting
C_KZG_RET toeplitz_part_2(g1_t *out, const poly *toeplitz_coeffs, const g1_t *x_ext_fft, const FFTSettings *fs) { C_KZG_RET toeplitz_part_2(g1_t *out, const poly *toeplitz_coeffs, const g1_t *x_ext_fft, const FFTSettings *fs) {
fr_t *toeplitz_coeffs_fft; fr_t *toeplitz_coeffs_fft;
// ASSERT(toeplitz_coeffs->length == fk->x_ext_fft_len, C_KZG_BADARGS); // TODO: how to implement? // CHECK(toeplitz_coeffs->length == fk->x_ext_fft_len); // TODO: how to implement?
TRY(new_fr(&toeplitz_coeffs_fft, toeplitz_coeffs->length)); TRY(new_fr(&toeplitz_coeffs_fft, toeplitz_coeffs->length));
TRY(fft_fr(toeplitz_coeffs_fft, toeplitz_coeffs->coeffs, false, toeplitz_coeffs->length, fs)); TRY(fft_fr(toeplitz_coeffs_fft, toeplitz_coeffs->coeffs, false, toeplitz_coeffs->length, fs));
@ -203,7 +203,7 @@ C_KZG_RET toeplitz_part_3(g1_t *out, const g1_t *h_ext_fft, uint64_t n2, const F
C_KZG_RET toeplitz_coeffs_stride(poly *out, const poly *in, uint64_t offset, uint64_t stride) { C_KZG_RET toeplitz_coeffs_stride(poly *out, const poly *in, uint64_t offset, uint64_t stride) {
uint64_t n = in->length, k, k2; uint64_t n = in->length, k, k2;
ASSERT(stride > 0, C_KZG_BADARGS); CHECK(stride > 0);
k = n / stride; k = n / stride;
k2 = k * 2; k2 = k * 2;
@ -257,8 +257,8 @@ C_KZG_RET fk20_single_da_opt(g1_t *out, const poly *p, const FK20SingleSettings
g1_t *h, *h_ext_fft; g1_t *h, *h_ext_fft;
poly toeplitz_coeffs; poly toeplitz_coeffs;
ASSERT(n2 <= fk->ks->fs->max_width, C_KZG_BADARGS); CHECK(n2 <= fk->ks->fs->max_width);
ASSERT(is_power_of_two(n), C_KZG_BADARGS); CHECK(is_power_of_two(n));
TRY(new_poly(&toeplitz_coeffs, 2 * p->length)); TRY(new_poly(&toeplitz_coeffs, 2 * p->length));
TRY(toeplitz_coeffs_step(&toeplitz_coeffs, p)); TRY(toeplitz_coeffs_step(&toeplitz_coeffs, p));
@ -296,8 +296,8 @@ C_KZG_RET fk20_single_da_opt(g1_t *out, const poly *p, const FK20SingleSettings
C_KZG_RET da_using_fk20_single(g1_t *out, const poly *p, const FK20SingleSettings *fk) { C_KZG_RET da_using_fk20_single(g1_t *out, const poly *p, const FK20SingleSettings *fk) {
uint64_t n = p->length, n2 = n * 2; uint64_t n = p->length, n2 = n * 2;
ASSERT(n2 <= fk->ks->fs->max_width, C_KZG_BADARGS); CHECK(n2 <= fk->ks->fs->max_width);
ASSERT(is_power_of_two(n), C_KZG_BADARGS); CHECK(is_power_of_two(n));
TRY(fk20_single_da_opt(out, p, fk)); TRY(fk20_single_da_opt(out, p, fk));
TRY(reverse_bit_order(out, sizeof out[0], n2)); TRY(reverse_bit_order(out, sizeof out[0], n2));
@ -329,7 +329,7 @@ C_KZG_RET fk20_compute_proof_multi(g1_t *out, const poly *p, const FK20MultiSett
g1_t *h_ext_fft, *h_ext_fft_file, *h; g1_t *h_ext_fft, *h_ext_fft_file, *h;
poly toeplitz_coeffs; poly toeplitz_coeffs;
ASSERT(fk->ks->fs->max_width >= n2, C_KZG_BADARGS); CHECK(fk->ks->fs->max_width >= n2);
TRY(new_p1(&h_ext_fft, n2)); TRY(new_p1(&h_ext_fft, n2));
for (uint64_t i = 0; i < n2; i++) { for (uint64_t i = 0; i < n2; i++) {
@ -374,8 +374,8 @@ C_KZG_RET fk20_multi_da_opt(g1_t *out, const poly *p, const FK20MultiSettings *f
g1_t *h_ext_fft, *h_ext_fft_file, *h; g1_t *h_ext_fft, *h_ext_fft_file, *h;
poly toeplitz_coeffs; poly toeplitz_coeffs;
ASSERT(n2 <= fk->ks->fs->max_width, C_KZG_BADARGS); CHECK(n2 <= fk->ks->fs->max_width);
ASSERT(is_power_of_two(n), C_KZG_BADARGS); CHECK(is_power_of_two(n));
n = n2 / 2; n = n2 / 2;
k = n / fk->chunk_len; k = n / fk->chunk_len;
@ -424,8 +424,8 @@ C_KZG_RET fk20_multi_da_opt(g1_t *out, const poly *p, const FK20MultiSettings *f
C_KZG_RET da_using_fk20_multi(g1_t *out, const poly *p, const FK20MultiSettings *fk) { C_KZG_RET da_using_fk20_multi(g1_t *out, const poly *p, const FK20MultiSettings *fk) {
uint64_t n = p->length, n2 = n * 2; uint64_t n = p->length, n2 = n * 2;
ASSERT(n2 <= fk->ks->fs->max_width, C_KZG_BADARGS); CHECK(n2 <= fk->ks->fs->max_width);
ASSERT(is_power_of_two(n), C_KZG_BADARGS); CHECK(is_power_of_two(n));
TRY(fk20_multi_da_opt(out, p, fk)); TRY(fk20_multi_da_opt(out, p, fk));
TRY(reverse_bit_order(out, sizeof out[0], n2 / fk->chunk_len)); TRY(reverse_bit_order(out, sizeof out[0], n2 / fk->chunk_len));
@ -451,9 +451,9 @@ C_KZG_RET new_fk20_single_settings(FK20SingleSettings *fk, uint64_t n2, const KZ
int n = n2 / 2; int n = n2 / 2;
g1_t *x; g1_t *x;
ASSERT(n2 <= ks->fs->max_width, C_KZG_BADARGS); CHECK(n2 <= ks->fs->max_width);
ASSERT(is_power_of_two(n2), C_KZG_BADARGS); CHECK(is_power_of_two(n2));
ASSERT(n2 >= 2, C_KZG_BADARGS); CHECK(n2 >= 2);
fk->ks = ks; fk->ks = ks;
fk->x_ext_fft_len = n2; fk->x_ext_fft_len = n2;
@ -490,12 +490,12 @@ C_KZG_RET new_fk20_multi_settings(FK20MultiSettings *fk, uint64_t n2, uint64_t c
uint64_t n, k; uint64_t n, k;
g1_t *x; g1_t *x;
ASSERT(n2 <= ks->fs->max_width, C_KZG_BADARGS); CHECK(n2 <= ks->fs->max_width);
ASSERT(is_power_of_two(n2), C_KZG_BADARGS); CHECK(is_power_of_two(n2));
ASSERT(n2 >= 2, C_KZG_BADARGS); CHECK(n2 >= 2);
ASSERT(chunk_len <= n2, C_KZG_BADARGS); CHECK(chunk_len <= n2);
ASSERT(is_power_of_two(chunk_len), C_KZG_BADARGS); CHECK(is_power_of_two(chunk_len));
ASSERT(chunk_len > 0, C_KZG_BADARGS); CHECK(chunk_len > 0);
n = n2 / 2; n = n2 / 2;
k = n / chunk_len; k = n / chunk_len;

View File

@ -100,7 +100,7 @@ C_KZG_RET compute_proof_multi(g1_t *out, const poly *p, const fr_t *x0, uint64_t
poly divisor, q; poly divisor, q;
fr_t x_pow_n; fr_t x_pow_n;
ASSERT(is_power_of_two(n), C_KZG_BADARGS); CHECK(is_power_of_two(n));
// Construct x^n - x0^n = (x - w^0)(x - w^1)...(x - w^(n-1)) // Construct x^n - x0^n = (x - w^0)(x - w^1)...(x - w^(n-1))
TRY(new_poly(&divisor, n + 1)); TRY(new_poly(&divisor, n + 1));
@ -153,7 +153,7 @@ C_KZG_RET check_proof_multi(bool *out, const g1_t *commitment, const g1_t *proof
g2_t xn2, xn_minus_yn; g2_t xn2, xn_minus_yn;
g1_t is1, commit_minus_interp; g1_t is1, commit_minus_interp;
ASSERT(is_power_of_two(n), C_KZG_BADARGS); CHECK(is_power_of_two(n));
// Interpolate at a coset. // Interpolate at a coset.
TRY(new_poly(&interp, n)); TRY(new_poly(&interp, n));
@ -206,7 +206,7 @@ C_KZG_RET check_proof_multi(bool *out, const g1_t *commitment, const g1_t *proof
C_KZG_RET new_kzg_settings(KZGSettings *ks, const g1_t *secret_g1, const g2_t *secret_g2, uint64_t length, C_KZG_RET new_kzg_settings(KZGSettings *ks, const g1_t *secret_g1, const g2_t *secret_g2, uint64_t length,
FFTSettings const *fs) { FFTSettings const *fs) {
ASSERT(length >= fs->max_width, C_KZG_BADARGS); CHECK(length >= fs->max_width);
ks->length = length; ks->length = length;
// Allocate space for the secrets // Allocate space for the secrets

View File

@ -88,7 +88,7 @@ C_KZG_RET new_poly_long_div(poly *out, const poly *dividend, const poly *divisor
fr_t a[dividend->length]; fr_t a[dividend->length];
// Dividing by zero is undefined // Dividing by zero is undefined
ASSERT(divisor->length > 0, C_KZG_BADARGS); CHECK(divisor->length > 0);
// Initialise the output polynomial // Initialise the output polynomial
TRY(new_poly(out, poly_quotient_length(dividend, divisor))); TRY(new_poly(out, poly_quotient_length(dividend, divisor)));