From 2c0faa605698b53f46d865f383b8bcfe996bc052 Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Tue, 13 Aug 2024 10:54:29 -0500 Subject: [PATCH] Enable more compiler warnings (#480) * Enable more compiler warnings * Fix some more issues (thanks gcc) * Fix some more implicit conversions * Fix docs * List them individually + more * Remove -Wvla * Remove -Wconditional-uninitialized because gcc on Windows is old --- src/Makefile | 62 +++++++++++++++++- src/common/lincomb.c | 4 +- src/common/utils.c | 38 ++++++------ src/common/utils.h | 8 +-- src/eip4844/blob.c | 4 +- src/eip7594/cell.c | 2 +- src/eip7594/eip7594.c | 8 +-- src/eip7594/fk20.c | 4 +- src/eip7594/poly.c | 1 + src/eip7594/recovery.c | 2 +- src/setup/setup.c | 4 +- src/test/tests.c | 138 +++++++++++++++++++---------------------- src/test/tinytest.h | 6 +- 13 files changed, 165 insertions(+), 116 deletions(-) diff --git a/src/Makefile b/src/Makefile index 46b259655..0fdfdc815 100644 --- a/src/Makefile +++ b/src/Makefile @@ -20,7 +20,67 @@ ifeq ($(PLATFORM),Darwin) endif # The base compiler flags. More can be added on command line. -CFLAGS += -I. -I../inc -O2 -Wall -Wextra +CFLAGS += -I. -I../inc -O2 +# Enable a bunch of optional warnings. +CFLAGS += \ + -pedantic \ + -Wall \ + -Wextra \ + -Waggregate-return \ + -Walloca \ + -Warray-bounds \ + -Wbad-function-cast \ + -Wc++-compat \ + -Wc++11-compat \ + -Wcast-align \ + -Wcast-qual \ + -Wconversion \ + -Wdisabled-optimization \ + -Wdouble-promotion \ + -Wenum-compare \ + -Wfloat-equal \ + -Wformat-nonliteral \ + -Wformat-security \ + -Wformat-y2k \ + -Wformat=2 \ + -Wframe-larger-than=1048576 \ + -Wimplicit \ + -Wimplicit-fallthrough \ + -Winit-self \ + -Winline \ + -Winvalid-pch \ + -Wlarger-than=512 \ + -Wlong-long \ + -Wmissing-declarations \ + -Wmissing-field-initializers \ + -Wmissing-format-attribute \ + -Wmissing-include-dirs \ + -Wmissing-noreturn \ + -Wmissing-prototypes \ + -Wnested-externs \ + -Wnull-dereference \ + -Wold-style-definition \ + -Woverlength-strings \ + -Wpacked \ + -Wpadded \ + -Wpointer-arith \ + -Wredundant-decls \ + -Wredundant-move \ + -Wshadow \ + -Wsign-compare \ + -Wsign-conversion \ + -Wstack-protector \ + -Wstrict-aliasing=2 \ + -Wstrict-overflow=5 \ + -Wstrict-prototypes \ + -Wswitch-default \ + -Wswitch-enum \ + -Wtype-limits \ + -Wundef \ + -Wuninitialized \ + -Wunreachable-code \ + -Wvariadic-macros \ + -Wwrite-strings # Cross-platform compilation settings. ifeq ($(PLATFORM),Windows) diff --git a/src/common/lincomb.c b/src/common/lincomb.c index 948a1eac4..d410ff7dd 100644 --- a/src/common/lincomb.c +++ b/src/common/lincomb.c @@ -63,7 +63,7 @@ void g1_lincomb_naive(g1_t *out, const g1_t *p, const fr_t *coeffs, uint64_t len */ C_KZG_RET g1_lincomb_fast(g1_t *out, const g1_t *p, const fr_t *coeffs, size_t len) { C_KZG_RET ret; - void *scratch = NULL; + limb_t *scratch = NULL; blst_p1 *p_filtered = NULL; blst_p1_affine *p_affine = NULL; blst_scalar *scalars = NULL; @@ -88,7 +88,7 @@ C_KZG_RET g1_lincomb_fast(g1_t *out, const g1_t *p, const fr_t *coeffs, size_t l /* Allocate space for Pippenger scratch */ size_t scratch_size = blst_p1s_mult_pippenger_scratch_sizeof(len); - ret = c_kzg_malloc(&scratch, scratch_size); + ret = c_kzg_malloc((void **)&scratch, scratch_size); if (ret != C_KZG_OK) goto out; /* Transform the field elements to 256-bit scalars */ diff --git a/src/common/utils.c b/src/common/utils.c index d38f93426..0eb33db66 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -17,6 +17,7 @@ #include "common/utils.h" #include "common/alloc.h" +#include /* For size_t */ #include /* For NULL */ #include /* For memcpy */ @@ -43,26 +44,26 @@ bool is_power_of_two(uint64_t n) { * @return The log base two of n. * * @remark In other words, the bit index of the one bit. - * @remark Works only for n a power of two, and only for n up to 2^31. + * @remark Works only for n a power of two. * @remark Not the fastest implementation, but it doesn't need to be fast. */ -int log2_pow2(uint32_t n) { - int position = 0; +uint64_t log2_pow2(uint64_t n) { + uint64_t position = 0; while (n >>= 1) position++; return position; } /** - * Reverse the bit order in a 32-bit integer. + * Reverse the bit order in a 64-bit integer. * * @param[in] n The integer to be reversed * * @return An integer with the bits of `n` reversed. */ -uint32_t reverse_bits(uint32_t n) { - uint32_t result = 0; - for (int i = 0; i < 32; ++i) { +uint64_t reverse_bits(uint64_t n) { + uint64_t result = 0; + for (size_t i = 0; i < 64; ++i) { result <<= 1; result |= (n & 1); n >>= 1; @@ -71,7 +72,7 @@ uint32_t reverse_bits(uint32_t n) { } /** - * Reverse the low-order bits in a 32-bit integer. + * Reverse the low-order bits in a 64-bit integer. * * @param[in] n To reverse `b` bits, set `n = 2 ^ b` * @param[in] value The bits to be reversed @@ -80,8 +81,8 @@ uint32_t reverse_bits(uint32_t n) { * * @remark n must be a power of two. */ -uint32_t reverse_bits_limited(uint32_t n, uint32_t value) { - size_t unused_bit_len = 32 - log2_pow2(n); +uint64_t reverse_bits_limited(uint64_t n, uint64_t value) { + size_t unused_bit_len = 64 - log2_pow2(n); return reverse_bits(value) >> unused_bit_len; } @@ -90,8 +91,7 @@ uint32_t reverse_bits_limited(uint32_t n, uint32_t value) { * * @param[in,out] values The array, which is re-ordered in-place * @param[in] size The size in bytes of an element of the array - * @param[in] n The length of the array, must be a power of two - * strictly greater than 1 and less than 2^32. + * @param[in] n The length of the array, must be a power of two strictly greater than 1 * * @remark Operates in-place on the array. * @remark Can handle arrays of any type: provide the element size in `size`. @@ -102,10 +102,10 @@ uint32_t reverse_bits_limited(uint32_t n, uint32_t value) { C_KZG_RET bit_reversal_permutation(void *values, size_t size, uint64_t n) { C_KZG_RET ret; byte *tmp = NULL; - byte *v = values; + byte *v = (byte *)values; /* Some sanity checks */ - if (n < 2 || n >= UINT32_MAX || !is_power_of_two(n)) { + if (n < 2 || !is_power_of_two(n)) { ret = C_KZG_BADARGS; goto out; } @@ -115,9 +115,9 @@ C_KZG_RET bit_reversal_permutation(void *values, size_t size, uint64_t n) { if (ret != C_KZG_OK) goto out; /* Reorder elements */ - int unused_bit_len = 32 - log2_pow2(n); - for (uint32_t i = 0; i < n; i++) { - uint32_t r = reverse_bits(i) >> unused_bit_len; + uint64_t unused_bit_len = 64 - log2_pow2(n); + for (uint64_t i = 0; i < n; i++) { + uint64_t r = reverse_bits(i) >> unused_bit_len; if (r > i) { /* Swap the two elements */ memcpy(tmp, v + (i * size), size); @@ -140,9 +140,9 @@ C_KZG_RET bit_reversal_permutation(void *values, size_t size, uint64_t n) { * * @remark `out` is left untouched if `n == 0`. */ -void compute_powers(fr_t *out, const fr_t *x, uint64_t n) { +void compute_powers(fr_t *out, const fr_t *x, size_t n) { fr_t current_power = FR_ONE; - for (uint64_t i = 0; i < n; i++) { + for (size_t i = 0; i < n; i++) { out[i] = current_power; blst_fr_mul(¤t_power, ¤t_power, x); } diff --git a/src/common/utils.h b/src/common/utils.h index acd56477f..9f937da9b 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -32,11 +32,11 @@ extern "C" { #endif bool is_power_of_two(uint64_t n); -int log2_pow2(uint32_t n); -uint32_t reverse_bits(uint32_t n); -uint32_t reverse_bits_limited(uint32_t n, uint32_t value); +uint64_t log2_pow2(uint64_t n); +uint64_t reverse_bits(uint64_t n); +uint64_t reverse_bits_limited(uint64_t n, uint64_t value); C_KZG_RET bit_reversal_permutation(void *values, size_t size, uint64_t n); -void compute_powers(fr_t *out, const fr_t *x, uint64_t n); +void compute_powers(fr_t *out, const fr_t *x, size_t n); bool pairings_verify(const g1_t *a1, const g2_t *a2, const g1_t *b1, const g2_t *b2); #ifdef __cplusplus diff --git a/src/eip4844/blob.c b/src/eip4844/blob.c index 4e2689e99..261bd1c68 100644 --- a/src/eip4844/blob.c +++ b/src/eip4844/blob.c @@ -29,7 +29,7 @@ C_KZG_RET blob_to_polynomial(fr_t *p, const Blob *blob) { C_KZG_RET ret; for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) { - ret = bytes_to_bls_field(&p[i], (Bytes32 *)&blob->bytes[i * BYTES_PER_FIELD_ELEMENT]); + ret = bytes_to_bls_field(&p[i], (const Bytes32 *)&blob->bytes[i * BYTES_PER_FIELD_ELEMENT]); if (ret != C_KZG_OK) return ret; } return C_KZG_OK; @@ -42,7 +42,7 @@ C_KZG_RET blob_to_polynomial(fr_t *p, const Blob *blob) { */ void print_blob(const Blob *blob) { for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) { - Bytes32 *field = (Bytes32 *)&blob->bytes[i * BYTES_PER_FIELD_ELEMENT]; + const Bytes32 *field = (const Bytes32 *)&blob->bytes[i * BYTES_PER_FIELD_ELEMENT]; print_bytes32(field); } } diff --git a/src/eip7594/cell.c b/src/eip7594/cell.c index 627b8abef..1dccb7df7 100644 --- a/src/eip7594/cell.c +++ b/src/eip7594/cell.c @@ -26,7 +26,7 @@ */ void print_cell(const Cell *cell) { for (size_t i = 0; i < FIELD_ELEMENTS_PER_CELL; i++) { - Bytes32 *field = (Bytes32 *)&cell->bytes[i * BYTES_PER_FIELD_ELEMENT]; + const Bytes32 *field = (const Bytes32 *)&cell->bytes[i * BYTES_PER_FIELD_ELEMENT]; print_bytes32(field); } } diff --git a/src/eip7594/eip7594.c b/src/eip7594/eip7594.c index 0d4f71dc5..90b9ba8cd 100644 --- a/src/eip7594/eip7594.c +++ b/src/eip7594/eip7594.c @@ -224,7 +224,7 @@ C_KZG_RET recover_cells_and_kzg_proofs( /* Convert the untrusted bytes to a field element */ size_t offset = j * BYTES_PER_FIELD_ELEMENT; - ret = bytes_to_bls_field(field, (Bytes32 *)&cells[i].bytes[offset]); + ret = bytes_to_bls_field(field, (const Bytes32 *)&cells[i].bytes[offset]); if (ret != C_KZG_OK) goto out; } } @@ -632,7 +632,7 @@ C_KZG_RET verify_cell_kzg_proof_batch( for (size_t j = 0; j < FIELD_ELEMENTS_PER_CELL; j++) { fr_t field, scaled; size_t offset = j * BYTES_PER_FIELD_ELEMENT; - ret = bytes_to_bls_field(&field, (Bytes32 *)&cells[i].bytes[offset]); + ret = bytes_to_bls_field(&field, (const Bytes32 *)&cells[i].bytes[offset]); if (ret != C_KZG_OK) goto out; blst_fr_mul(&scaled, &field, &r_powers[i]); size_t index = cell_indices[i] * FIELD_ELEMENTS_PER_CELL + j; @@ -680,7 +680,7 @@ C_KZG_RET verify_cell_kzg_proof_batch( * To unscale, divide by the coset. It's faster to multiply with the inverse. We can skip * the first iteration because its dividing by one. */ - uint32_t pos = reverse_bits_limited(CELLS_PER_EXT_BLOB, i); + uint64_t pos = reverse_bits_limited(CELLS_PER_EXT_BLOB, i); fr_t inv_coset_factor; blst_fr_eucl_inverse(&inv_coset_factor, &s->roots_of_unity[pos]); shift_poly(column_interpolation_poly, FIELD_ELEMENTS_PER_CELL, &inv_coset_factor); @@ -709,7 +709,7 @@ C_KZG_RET verify_cell_kzg_proof_batch( //////////////////////////////////////////////////////////////////////////////////////////////// for (size_t i = 0; i < num_cells; i++) { - uint32_t pos = reverse_bits_limited(CELLS_PER_EXT_BLOB, cell_indices[i]); + uint64_t pos = reverse_bits_limited(CELLS_PER_EXT_BLOB, cell_indices[i]); fr_t coset_factor = s->roots_of_unity[pos]; fr_pow(&weights[i], &coset_factor, FIELD_ELEMENTS_PER_CELL); blst_fr_mul(&weighted_powers_of_r[i], &r_powers[i], &weights[i]); diff --git a/src/eip7594/fk20.c b/src/eip7594/fk20.c index 4350fc6ba..ae17391ba 100644 --- a/src/eip7594/fk20.c +++ b/src/eip7594/fk20.c @@ -73,7 +73,7 @@ C_KZG_RET compute_fk20_proofs(g1_t *out, const fr_t *p, size_t n, const KZGSetti fr_t *toeplitz_coeffs_fft = NULL; g1_t *h = NULL; g1_t *h_ext_fft = NULL; - void *scratch = NULL; + limb_t *scratch = NULL; bool precompute = s->wbits != 0; /* Initialize length variables */ @@ -92,7 +92,7 @@ C_KZG_RET compute_fk20_proofs(g1_t *out, const fr_t *p, size_t n, const KZGSetti if (precompute) { /* Allocations for fixed-base MSM */ - ret = c_kzg_malloc(&scratch, s->scratch_size); + ret = c_kzg_malloc((void **)&scratch, s->scratch_size); if (ret != C_KZG_OK) goto out; ret = c_kzg_calloc((void **)&scalars, FIELD_ELEMENTS_PER_CELL, sizeof(blst_scalar)); if (ret != C_KZG_OK) goto out; diff --git a/src/eip7594/poly.c b/src/eip7594/poly.c index ae69b181e..0ec8f90be 100644 --- a/src/eip7594/poly.c +++ b/src/eip7594/poly.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include "poly.h" #include "common/alloc.h" #include "common/ec.h" #include "common/ret.h" diff --git a/src/eip7594/recovery.c b/src/eip7594/recovery.c index 1e5524d71..9bb986c8e 100644 --- a/src/eip7594/recovery.c +++ b/src/eip7594/recovery.c @@ -252,7 +252,7 @@ C_KZG_RET recover_cells( /* Iterate over each cell index and check if we have received it */ if (!is_in_array(cell_indices, num_cells, i)) { /* If the cell is missing, bit reverse the index and add it to the missing array */ - uint32_t brp_i = reverse_bits_limited(CELLS_PER_EXT_BLOB, i); + uint64_t brp_i = reverse_bits_limited(CELLS_PER_EXT_BLOB, i); missing_cell_indices[len_missing++] = brp_i; } } diff --git a/src/setup/setup.c b/src/setup/setup.c index d0c973beb..a00af7c4d 100644 --- a/src/setup/setup.c +++ b/src/setup/setup.c @@ -94,7 +94,7 @@ static C_KZG_RET compute_roots_of_unity(KZGSettings *s) { C_KZG_RET ret; fr_t root_of_unity; - uint32_t max_scale = 0; + size_t max_scale = 0; while ((1ULL << max_scale) < FIELD_ELEMENTS_PER_EXT_BLOB) max_scale++; @@ -382,7 +382,7 @@ C_KZG_RET load_trusted_setup( } /* 1<= n1 */ - uint32_t max_scale = 0; + size_t max_scale = 0; while ((1ULL << max_scale) < NUM_G1_POINTS) max_scale++; diff --git a/src/test/tests.c b/src/test/tests.c index 70a2f9728..1224c1bf7 100644 --- a/src/test/tests.c +++ b/src/test/tests.c @@ -99,10 +99,10 @@ static void bytes48_from_hex(Bytes48 *out, const char *hex) { } } -static void get_rand_uint32(uint32_t *out) { +static void get_rand_uint64(uint64_t *out) { Bytes32 b; get_rand_bytes32(&b); - *out = *(uint32_t *)(b.bytes); + memcpy(out, b.bytes, sizeof(*out)); } static void eval_poly(fr_t *out, fr_t *poly_coefficients, fr_t *x) { @@ -313,14 +313,14 @@ static void test_fr_batch_inv__test_zero(void) { //////////////////////////////////////////////////////////////////////////////////////////////////// static void test_g1_mul__test_consistent(void) { - blst_scalar s; + blst_scalar scalar; Bytes32 b; fr_t f; g1_t g, r, check; get_rand_field_element(&b); - blst_scalar_from_lendian(&s, b.bytes); - blst_fr_from_scalar(&f, &s); + blst_scalar_from_lendian(&scalar, b.bytes); + blst_fr_from_scalar(&f, &scalar); get_rand_g1(&g); @@ -346,13 +346,13 @@ static void test_g1_mul__test_different_bit_lengths(void) { Bytes32 b; fr_t f, two; g1_t g, r, check; - blst_scalar s; + blst_scalar scalar; fr_from_uint64(&f, 1); fr_from_uint64(&two, 2); - blst_scalar_from_fr(&s, &f); + blst_scalar_from_fr(&scalar, &f); /* blst_p1_mult needs it to be little-endian */ - blst_lendian_from_scalar(b.bytes, &s); + blst_lendian_from_scalar(b.bytes, &scalar); for (int i = 1; i < 255; i++) { get_rand_g1(&g); @@ -363,8 +363,8 @@ static void test_g1_mul__test_different_bit_lengths(void) { ASSERT("points are equal", blst_p1_is_equal(&check, &r)); blst_fr_mul(&f, &f, &two); - blst_scalar_from_fr(&s, &f); - blst_lendian_from_scalar(b.bytes, &s); + blst_scalar_from_fr(&scalar, &f); + blst_lendian_from_scalar(b.bytes, &scalar); } } @@ -373,33 +373,33 @@ static void test_g1_mul__test_different_bit_lengths(void) { //////////////////////////////////////////////////////////////////////////////////////////////////// static void test_pairings_verify__good_pairing(void) { - fr_t s; + fr_t f; g1_t g1, sg1; g2_t g2, sg2; - get_rand_fr(&s); + get_rand_fr(&f); get_rand_g1(&g1); get_rand_g2(&g2); - g1_mul(&sg1, &g1, &s); - g2_mul(&sg2, &g2, &s); + g1_mul(&sg1, &g1, &f); + g2_mul(&sg2, &g2, &f); ASSERT("pairings verify", pairings_verify(&g1, &sg2, &sg1, &g2)); } static void test_pairings_verify__bad_pairing(void) { - fr_t s, splusone; + fr_t f, splusone; g1_t g1, sg1; g2_t g2, s1g2; - get_rand_fr(&s); - blst_fr_add(&splusone, &s, &FR_ONE); + get_rand_fr(&f); + blst_fr_add(&splusone, &f, &FR_ONE); get_rand_g1(&g1); get_rand_g2(&g2); - g1_mul(&sg1, &g1, &s); + g1_mul(&sg1, &g1, &f); g2_mul(&s1g2, &g2, &splusone); ASSERT("pairings fail", !pairings_verify(&g1, &s1g2, &sg1, &g2)); @@ -750,31 +750,31 @@ static void test_validate_kzg_g1__fails_with_mask_bits_001(void) { //////////////////////////////////////////////////////////////////////////////////////////////////// static void test_reverse_bits__succeeds_round_trip(void) { - uint32_t original; - uint32_t reversed; - uint32_t reversed_reversed; + uint64_t original; + uint64_t reversed; + uint64_t reversed_reversed; - get_rand_uint32(&original); + get_rand_uint64(&original); reversed = reverse_bits(original); reversed_reversed = reverse_bits(reversed); ASSERT_EQUALS(reversed_reversed, original); } static void test_reverse_bits__succeeds_all_bits_are_zero(void) { - uint32_t original = 0; - uint32_t reversed = 0; + uint64_t original = 0; + uint64_t reversed = 0; ASSERT_EQUALS(reverse_bits(original), reversed); } static void test_reverse_bits__succeeds_some_bits_are_one(void) { - uint32_t original = 2826829826; - uint32_t reversed = 1073774101; + uint64_t original = 17004747765872328575ULL; + uint64_t reversed = 18374677679283584983ULL; ASSERT_EQUALS(reverse_bits(original), reversed); } static void test_reverse_bits__succeeds_all_bits_are_one(void) { - uint32_t original = 4294967295; - uint32_t reversed = 4294967295; + uint64_t original = 18446744073709551615ULL; + uint64_t reversed = 18446744073709551615ULL; ASSERT_EQUALS(reverse_bits(original), reversed); } @@ -784,16 +784,16 @@ static void test_reverse_bits__succeeds_all_bits_are_one(void) { static void test_bit_reversal_permutation__succeeds_round_trip(void) { C_KZG_RET ret; - uint32_t original[128]; - uint32_t reversed_reversed[128]; + uint64_t original[128]; + uint64_t reversed_reversed[128]; for (size_t i = 0; i < 128; i++) { - get_rand_uint32(&original[i]); + get_rand_uint64(&original[i]); reversed_reversed[i] = original[i]; } - ret = bit_reversal_permutation(&reversed_reversed, sizeof(uint32_t), 128); + ret = bit_reversal_permutation(&reversed_reversed, sizeof(uint64_t), 128); ASSERT_EQUALS(ret, C_KZG_OK); - ret = bit_reversal_permutation(&reversed_reversed, sizeof(uint32_t), 128); + ret = bit_reversal_permutation(&reversed_reversed, sizeof(uint64_t), 128); ASSERT_EQUALS(ret, C_KZG_OK); for (size_t i = 0; i < 128; i++) { ASSERT_EQUALS(reversed_reversed[i], original[i]); @@ -802,14 +802,14 @@ static void test_bit_reversal_permutation__succeeds_round_trip(void) { static void test_bit_reversal_permutation__specific_items(void) { C_KZG_RET ret; - uint32_t original[128]; - uint32_t reversed[128]; + uint64_t original[128]; + uint64_t reversed[128]; for (size_t i = 0; i < 128; i++) { - get_rand_uint32(&original[i]); + get_rand_uint64(&original[i]); reversed[i] = original[i]; } - ret = bit_reversal_permutation(&reversed, sizeof(uint32_t), 128); + ret = bit_reversal_permutation(&reversed, sizeof(uint64_t), 128); ASSERT_EQUALS(ret, C_KZG_OK); // Test the first 8 elements of the bit reversal permutation @@ -827,14 +827,14 @@ static void test_bit_reversal_permutation__specific_items(void) { static void test_bit_reversal_permutation__coset_structure(void) { C_KZG_RET ret; - uint32_t original[256]; - uint32_t reversed[256]; + uint64_t original[256]; + uint64_t reversed[256]; for (size_t i = 0; i < 256; i++) { original[i] = i % 16; reversed[i] = original[i]; } - ret = bit_reversal_permutation(&reversed, sizeof(uint32_t), 256); + ret = bit_reversal_permutation(&reversed, sizeof(uint64_t), 256); ASSERT_EQUALS(ret, C_KZG_OK); for (size_t i = 0; i < 16; i++) { for (size_t j = 1; j < 16; j++) { @@ -843,36 +843,25 @@ static void test_bit_reversal_permutation__coset_structure(void) { } } -static void test_bit_reversal_permutation__fails_n_too_large(void) { - C_KZG_RET ret; - uint32_t reversed[256]; - - for (size_t i = 0; i < 256; i++) { - reversed[i] = 0; - } - ret = bit_reversal_permutation(&reversed, sizeof(uint32_t), (uint64_t)1 << 32); - ASSERT_EQUALS(ret, C_KZG_BADARGS); -} - static void test_bit_reversal_permutation__fails_n_not_power_of_two(void) { C_KZG_RET ret; - uint32_t reversed[256]; + uint64_t reversed[256]; for (size_t i = 0; i < 256; i++) { reversed[i] = 0; } - ret = bit_reversal_permutation(&reversed, sizeof(uint32_t), 255); + ret = bit_reversal_permutation(&reversed, sizeof(uint64_t), 255); ASSERT_EQUALS(ret, C_KZG_BADARGS); } static void test_bit_reversal_permutation__fails_n_is_one(void) { C_KZG_RET ret; - uint32_t reversed[1]; + uint64_t reversed[1]; for (size_t i = 0; i < 1; i++) { reversed[i] = 0; } - ret = bit_reversal_permutation(&reversed, sizeof(uint32_t), 1); + ret = bit_reversal_permutation(&reversed, sizeof(uint64_t), 1); ASSERT_EQUALS(ret, C_KZG_BADARGS); } @@ -884,7 +873,7 @@ static void test_compute_powers__succeeds_expected_powers(void) { C_KZG_RET ret; Bytes32 field_element_bytes; fr_t field_element_fr; - const int n = 3; + const size_t n = 3; int diff; fr_t powers[n]; Bytes32 powers_bytes[n]; @@ -921,7 +910,7 @@ static void test_compute_powers__succeeds_expected_powers(void) { &expected_bytes[2], "2f417bcb88693ff8bc5d61b6d44503f3a99e8c3df3891e0040dee96047458a0e" ); - for (int i = 0; i < n; i++) { + for (size_t i = 0; i < n; i++) { bytes_from_bls_field(&powers_bytes[i], &powers[i]); diff = memcmp(powers_bytes[i].bytes, expected_bytes[i].bytes, sizeof(Bytes32)); ASSERT_EQUALS(diff, 0); @@ -1028,8 +1017,8 @@ static void test_evaluate_polynomial_in_evaluation_form__random_polynomial(void) //////////////////////////////////////////////////////////////////////////////////////////////////// static void test_log2_pow2__succeeds_expected_values(void) { - uint32_t x = 1; - for (int i = 0; i < 31; i++) { + uint64_t x = 1; + for (size_t i = 0; i < 31; i++) { ASSERT_EQUALS(i, log2_pow2(x)); x <<= 1; } @@ -1041,7 +1030,7 @@ static void test_log2_pow2__succeeds_expected_values(void) { static void test_is_power_of_two__succeeds_powers_of_two(void) { uint64_t x = 1; - for (int i = 0; i < 63; i++) { + for (size_t i = 0; i < 63; i++) { ASSERT("is_power_of_two good", is_power_of_two(x)); x <<= 1; } @@ -1049,7 +1038,7 @@ static void test_is_power_of_two__succeeds_powers_of_two(void) { static void test_is_power_of_two__fails_not_powers_of_two(void) { uint64_t x = 4; - for (int i = 2; i < 63; i++) { + for (size_t i = 2; i < 63; i++) { ASSERT("is_power_of_two bad", !is_power_of_two(x + 1)); ASSERT("is_power_of_two bad", !is_power_of_two(x - 1)); x <<= 1; @@ -1479,7 +1468,7 @@ static void test_compute_and_verify_blob_kzg_proof__fails_invalid_blob(void) { static void test_verify_kzg_proof_batch__succeeds_round_trip(void) { C_KZG_RET ret; - const int n_cells = 16; + const size_t n_cells = 16; Bytes48 proofs[n_cells]; KZGCommitment commitments[n_cells]; Blob *blobs = NULL; @@ -1490,7 +1479,7 @@ static void test_verify_kzg_proof_batch__succeeds_round_trip(void) { ASSERT_EQUALS(ret, C_KZG_OK); /* Some preparation */ - for (int i = 0; i < n_cells; i++) { + for (size_t i = 0; i < n_cells; i++) { get_rand_blob(&blobs[i]); ret = blob_to_kzg_commitment(&commitments[i], &blobs[i], &s); ASSERT_EQUALS(ret, C_KZG_OK); @@ -1500,7 +1489,7 @@ static void test_verify_kzg_proof_batch__succeeds_round_trip(void) { /* Verify batched proofs for 0,1,2..16 blobs */ /* This should still work with zero blobs */ - for (int count = 0; count <= n_cells; count++) { + for (size_t count = 0; count <= n_cells; count++) { ret = verify_blob_kzg_proof_batch(&ok, blobs, commitments, proofs, count, &s); ASSERT_EQUALS(ret, C_KZG_OK); ASSERT_EQUALS(ok, true); @@ -1512,14 +1501,14 @@ static void test_verify_kzg_proof_batch__succeeds_round_trip(void) { static void test_verify_kzg_proof_batch__fails_with_incorrect_proof(void) { C_KZG_RET ret; - const int n_cells = 2; + const size_t n_cells = 2; Bytes48 proofs[n_cells]; KZGCommitment commitments[n_cells]; Blob blobs[n_cells]; bool ok; /* Some preparation */ - for (int i = 0; i < n_cells; i++) { + for (size_t i = 0; i < n_cells; i++) { get_rand_blob(&blobs[i]); ret = blob_to_kzg_commitment(&commitments[i], &blobs[i], &s); ASSERT_EQUALS(ret, C_KZG_OK); @@ -1537,14 +1526,14 @@ static void test_verify_kzg_proof_batch__fails_with_incorrect_proof(void) { static void test_verify_kzg_proof_batch__fails_proof_not_in_g1(void) { C_KZG_RET ret; - const int n_cells = 2; + const size_t n_cells = 2; Bytes48 proofs[n_cells]; KZGCommitment commitments[n_cells]; Blob blobs[n_cells]; bool ok; /* Some preparation */ - for (int i = 0; i < n_cells; i++) { + for (size_t i = 0; i < n_cells; i++) { get_rand_blob(&blobs[i]); ret = blob_to_kzg_commitment(&commitments[i], &blobs[i], &s); ASSERT_EQUALS(ret, C_KZG_OK); @@ -1565,14 +1554,14 @@ static void test_verify_kzg_proof_batch__fails_proof_not_in_g1(void) { static void test_verify_kzg_proof_batch__fails_commitment_not_in_g1(void) { C_KZG_RET ret; - const int n_cells = 2; + const size_t n_cells = 2; Bytes48 proofs[n_cells]; KZGCommitment commitments[n_cells]; Blob blobs[n_cells]; bool ok; /* Some preparation */ - for (int i = 0; i < n_cells; i++) { + for (size_t i = 0; i < n_cells; i++) { get_rand_blob(&blobs[i]); ret = blob_to_kzg_commitment(&commitments[i], &blobs[i], &s); ASSERT_EQUALS(ret, C_KZG_OK); @@ -1593,7 +1582,7 @@ static void test_verify_kzg_proof_batch__fails_commitment_not_in_g1(void) { static void test_verify_kzg_proof_batch__fails_invalid_blob(void) { C_KZG_RET ret; - const int n_cells = 2; + const size_t n_cells = 2; Bytes48 proofs[n_cells]; KZGCommitment commitments[n_cells]; Blob blobs[n_cells]; @@ -1601,7 +1590,7 @@ static void test_verify_kzg_proof_batch__fails_invalid_blob(void) { bool ok; /* Some preparation */ - for (int i = 0; i < n_cells; i++) { + for (size_t i = 0; i < n_cells; i++) { get_rand_blob(&blobs[i]); ret = blob_to_kzg_commitment(&commitments[i], &blobs[i], &s); ASSERT_EQUALS(ret, C_KZG_OK); @@ -1794,8 +1783,8 @@ static void test_deduplicate_commitments__all_duplicates(void) { } static void test_deduplicate_commitments__no_commitments(void) { - Bytes48 commitments[0]; - uint64_t indices[0]; + Bytes48 *commitments = NULL; + uint64_t *indices = NULL; size_t count = 0; deduplicate_commitments(commitments, indices, &count); @@ -2250,7 +2239,6 @@ int main(void) { RUN(test_bit_reversal_permutation__succeeds_round_trip); RUN(test_bit_reversal_permutation__specific_items); RUN(test_bit_reversal_permutation__coset_structure); - RUN(test_bit_reversal_permutation__fails_n_too_large); RUN(test_bit_reversal_permutation__fails_n_not_power_of_two); RUN(test_bit_reversal_permutation__fails_n_is_one); RUN(test_compute_powers__succeeds_expected_powers); diff --git a/src/test/tinytest.h b/src/test/tinytest.h index d8445c824..f9d4f5675 100644 --- a/src/test/tinytest.h +++ b/src/test/tinytest.h @@ -71,7 +71,7 @@ const char* tt_current_expression = NULL; const char* tt_current_file = NULL; int tt_current_line = 0; -void tt_execute(const char* name, void (*test_function)(void)) +static void tt_execute(const char* name, void (*test_function)(void)) { tt_current_test_failed = 0; test_function(); @@ -84,7 +84,7 @@ void tt_execute(const char* name, void (*test_function)(void)) } } -int tt_assert(const char* file, int line, const char* msg, const char* expression, int pass) +static int tt_assert(const char* file, int line, const char* msg, const char* expression, int pass) { tt_current_msg = msg; tt_current_expression = expression; @@ -94,7 +94,7 @@ int tt_assert(const char* file, int line, const char* msg, const char* expressio return pass; } -int tt_report(void) +static int tt_report(void) { if (tt_fails) { printf("%c%sFAILED%c%s [%s] (passed:%d, failed:%d, total:%d)\n",