From 47ddabde6358de4f97f8608cab87c084a6f8b98d Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Tue, 4 Apr 2023 10:29:40 -0500 Subject: [PATCH] Update C tests to support minimal config (#263) --- .github/workflows/c-tests.yml | 62 +++++++++++++++++++++++++++++++---- src/Makefile | 3 +- src/test_c_kzg_4844.c | 55 ++++++++++++++++++++++++------- 3 files changed, 100 insertions(+), 20 deletions(-) diff --git a/.github/workflows/c-tests.yml b/.github/workflows/c-tests.yml index b2b8d2d..083b7f6 100644 --- a/.github/workflows/c-tests.yml +++ b/.github/workflows/c-tests.yml @@ -7,6 +7,11 @@ on: branches: - main +defaults: + run: + shell: bash + working-directory: src + jobs: tests: runs-on: ${{matrix.os}} @@ -17,33 +22,76 @@ jobs: - macos-latest - windows-latest steps: + # Checkout repository and blst submodule. - uses: actions/checkout@v3 with: submodules: recursive + + # Check formatting. + # Only need to check this once. - name: Check formatting if: matrix.os == 'ubuntu-latest' - working-directory: src run: | make format git diff --exit-code - - name: Build/Test - working-directory: src - run: make + + # Build and test with minimal preset. + - name: Build (minimal) + run: | + export FIELD_ELEMENTS_PER_BLOB=4 + make clean && make test_c_kzg_4844 + unset FIELD_ELEMENTS_PER_BLOB + - name: Save test binary (minimal) + if: matrix.os == 'windows-latest' + uses: actions/upload-artifact@v3 + with: + name: test_minimal_${{matrix.os}} + path: src/test_c_kzg_4844.exe + - name: Test (minimal) + run: make test + + # Build and test with mainnet preset. + - name: Build (mainnet) + run: | + export FIELD_ELEMENTS_PER_BLOB=4096 + make clean && make test_c_kzg_4844 + unset FIELD_ELEMENTS_PER_BLOB + - name: Save test binary (mainnet) + if: matrix.os == 'windows-latest' + uses: actions/upload-artifact@v3 + with: + name: test_mainnet_${{matrix.os}} + path: src/test_c_kzg_4844.exe + - name: Test (mainnet) + run: make test + + # Run sanitizers. + # Doesn't work on Windows. - name: Clang Sanitizers if: matrix.os != 'windows-latest' - working-directory: src run: make sanitize + + # Run static analyzer. + # Doesn't work on Windows. - name: Clang Static Analyzer if: matrix.os != 'windows-latest' - working-directory: src run: make analyze + + # Install LLVM for coverage report. + # Already installed on macOS. + # Doesn't work on Windows. - name: Install LLVM if: matrix.os == 'ubuntu-latest' uses: egor-tensin/setup-clang@v1 + + # Generate the coverage report. + # Doesn't work on Windows. - name: Generate coverage report if: matrix.os != 'windows-latest' - working-directory: src run: make coverage + + # Upload the coverage report. + # Didn't generate it for Windows. - name: Save coverage report if: matrix.os != 'windows-latest' uses: actions/upload-artifact@v3 diff --git a/src/Makefile b/src/Makefile index d672a7c..cf199d7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -31,7 +31,6 @@ CFLAGS += -O2 -Wall -Wextra ifeq ($(PLATFORM),Windows) CC = gcc CFLAGS += -D_CRT_SECURE_NO_WARNINGS - CFLAGS += -Wl,--stack,8388608 # 8MB else CC = clang CFLAGS += -fPIC -Werror @@ -66,7 +65,7 @@ c_kzg_4844.o: c_kzg_4844.c $(BLST_LIBRARY) @$(CC) $(CFLAGS) -c $< test_c_kzg_4844: CFLAGS += -O0 -test_c_kzg_4844: test_c_kzg_4844.c c_kzg_4844.c +test_c_kzg_4844: test_c_kzg_4844.c c_kzg_4844.c $(BLST_LIBRARY) @$(CC) $(CFLAGS) -o $@ $< $(LIBS) .PHONY: test diff --git a/src/test_c_kzg_4844.c b/src/test_c_kzg_4844.c index 8bd5dbf..fcb686f 100644 --- a/src/test_c_kzg_4844.c +++ b/src/test_c_kzg_4844.c @@ -12,6 +12,22 @@ #include #endif +/////////////////////////////////////////////////////////////////////////////// +// Macros +/////////////////////////////////////////////////////////////////////////////// + +#if FIELD_ELEMENTS_PER_BLOB == 4096 +#define MAINNET +#define TRUSTED_SETUP_FILE "trusted_setup.txt" +#define MAX_WIDTH 32 +#elif FIELD_ELEMENTS_PER_BLOB == 4 +#define MINIMAL +#define TRUSTED_SETUP_FILE "trusted_setup_4.txt" +#define MAX_WIDTH 4 +#else +#error FIELD_ELEMENTS_PER_BLOB must be 4096 or 4 +#endif + /////////////////////////////////////////////////////////////////////////////// // Globals /////////////////////////////////////////////////////////////////////////////// @@ -517,8 +533,13 @@ static void test_blob_to_kzg_commitment__succeeds_expected_commitment(void) { */ bytes48_from_hex( &expected_commitment, +#ifdef MAINNET "9815ded2101b6d233fdf31d826ba0557778506df8526f42a" "87ccd82db36a238b50f8965c25d4484782097436d29e458e" +#else + "95d2d20379b60c353a9c2c75333a5d7d26d5ef5137c5200b" + "51bc9d0fd82d0270e98ac9d41a44c366684089e385e815e6" +#endif ); diff = memcmp(c.bytes, expected_commitment.bytes, BYTES_PER_COMMITMENT); ASSERT_EQUALS(diff, 0); @@ -1053,8 +1074,13 @@ static void test_compute_kzg_proof__succeeds_expected_proof(void) { bytes48_from_hex( &expected_proof, +#ifdef MAINNET "899b7e1e7ff2e9b28c631d2f9d6b9ae828749c9dbf84f3f4" "3b910bda9558f360f2fa0dac1143460b55908406038eb538" +#else + "a846d83184f6d5b67bbbe905a875f6cfaf1c905e527ea49c" + "0616992fb8cce56d202c702b83d6fbe1fa75cacb050ffc27" +#endif ); /* Compare the computed proof to the expected proof */ @@ -1462,9 +1488,13 @@ static void test_verify_kzg_proof_batch__succeeds_round_trip(void) { const int n_samples = 16; Bytes48 proofs[n_samples]; KZGCommitment commitments[n_samples]; - Blob blobs[n_samples]; + Blob *blobs = NULL; bool ok; + /* Allocate blobs because they are big */ + ret = c_kzg_malloc((void **)&blobs, n_samples * sizeof(Blob)); + ASSERT_EQUALS(ret, C_KZG_OK); + /* Some preparation */ for (int i = 0; i < n_samples; i++) { get_rand_blob(&blobs[i]); @@ -1478,13 +1508,16 @@ 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 <= 16; count++) { + for (int count = 0; count <= n_samples; count++) { ret = verify_blob_kzg_proof_batch( &ok, blobs, commitments, proofs, count, &s ); ASSERT_EQUALS(ret, C_KZG_OK); ASSERT_EQUALS(ok, true); } + + /* Free the blobs */ + c_kzg_free(blobs); } static void test_verify_kzg_proof_batch__fails_with_incorrect_proof(void) { @@ -1619,19 +1652,19 @@ static void test_verify_kzg_proof_batch__fails_invalid_blob(void) { static void test_fft_g1__succeeds_round_trip(void) { C_KZG_RET ret; - g1_t original[32], transformed[32], inversed[32]; + g1_t original[MAX_WIDTH], transformed[MAX_WIDTH], inversed[MAX_WIDTH]; - for (size_t i = 0; i < 32; i++) { + for (size_t i = 0; i < MAX_WIDTH; i++) { get_rand_g1(&original[i]); } - ret = fft_g1(transformed, original, false, 32, s.fs); + ret = fft_g1(transformed, original, false, MAX_WIDTH, s.fs); ASSERT_EQUALS(ret, C_KZG_OK); - ret = fft_g1(inversed, transformed, true, 32, s.fs); + ret = fft_g1(inversed, transformed, true, MAX_WIDTH, s.fs); ASSERT_EQUALS(ret, C_KZG_OK); - for (size_t i = 0; i < 32; i++) { + for (size_t i = 0; i < MAX_WIDTH; i++) { ASSERT( "same as original", blst_p1_is_equal(&original[i], &inversed[i]) ); @@ -1640,15 +1673,15 @@ static void test_fft_g1__succeeds_round_trip(void) { static void test_fft_g1__n_not_power_of_two(void) { C_KZG_RET ret; - g1_t original[32], transformed[32]; + g1_t original[MAX_WIDTH], transformed[MAX_WIDTH]; - ret = fft_g1(transformed, original, false, 31, s.fs); + ret = fft_g1(transformed, original, false, MAX_WIDTH - 1, s.fs); ASSERT_EQUALS(ret, C_KZG_BADARGS); } static void test_fft_g1__n_too_large(void) { C_KZG_RET ret; - g1_t original[32], transformed[32]; + g1_t original[MAX_WIDTH], transformed[MAX_WIDTH]; ret = fft_g1(transformed, original, false, 2 * s.fs->max_width, s.fs); ASSERT_EQUALS(ret, C_KZG_BADARGS); @@ -1799,7 +1832,7 @@ static void setup(void) { C_KZG_RET ret; /* Open the mainnet trusted setup file */ - fp = fopen("trusted_setup.txt", "r"); + fp = fopen(TRUSTED_SETUP_FILE, "r"); assert(fp != NULL); /* Load that trusted setup file */