Cleanup the Makefile (#119)

* Cleanup the Makefile some

* Fix compiler error

* Fix compiler errors in tests

* Fix problems on Linux

* Add test_c_kzg_4844_cov to gitignore

* Add back closing brace for cpp

* Split clean rule into two lines

* Not echo commands when running them

* Allow other compilers to be used

* Update comment

* Only allow clang, actually
This commit is contained in:
Justin Traglia 2023-02-08 11:35:33 -06:00 committed by GitHub
parent af333c2706
commit dc28b03f9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 76 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
test_c_kzg_4844
test_c_kzg_4844_cov
coverage.html
*.profraw
*.profdata

View File

@ -9,7 +9,7 @@ clean:
rm -f *.o
build: kzg.cxx kzg.ts package.json binding.gyp Makefile
cd ../../src; make lib
cd ../../src && make c_kzg_4844.o && cp c_kzg_4844.o ../bindings/node.js
yarn install
yarn node-gyp rebuild

View File

@ -1,54 +1,85 @@
INCLUDE_DIRS = ../inc
###############################################################################
# Configuration Options
###############################################################################
ifeq ($(OS),Windows_NT)
CFLAGS += -O2
else
CFLAGS += -O2 -fPIC
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
XCRUN = xcrun
endif
#
# We use clang. Some versions of GCC report missing-braces warnings.
#
CC = clang
#
# By default, this is set to the mainnet value.
#
FIELD_ELEMENTS_PER_BLOB ?= 4096
#
# The base compiler flags. More can be added on command line.
#
CFLAGS += -I../inc
CFLAGS += -Wall -Wextra -Werror -O2
CFLAGS += -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB)
#
# Compiler flags for including blst. Must be put after the module.
#
BLST = -L../lib -lblst
#
# Compiler flags for generating coverage data.
#
COVERAGE = -fprofile-instr-generate -fcoverage-mapping
#
# Platform specific options.
#
ifneq ($(OS),Windows_NT)
CFLAGS += -fPIC
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
XCRUN = xcrun
endif
endif
CLANG_EXECUTABLE=clang
BLST_BUILD_SCRIPT=./build.sh
FIELD_ELEMENTS_PER_BLOB?=4096
###############################################################################
# Makefile Rules
###############################################################################
all: c_kzg_4844.o lib
all: c_kzg_4844.o
# If you change FIELD_ELEMENTS_PER_BLOB, remember to rm c_kzg_4844.o and make again
c_kzg_4844.o: c_kzg_4844.c Makefile
${CLANG_EXECUTABLE} -Wall -I$(INCLUDE_DIRS) -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB) $(CFLAGS) -c $<
%.o: %.c
@$(CC) $(CFLAGS) -c $<
test_c_kzg_4844: test_c_kzg_4844.c c_kzg_4844.c
@$(CC) $(CFLAGS) -o $@ $< $(BLST)
test_c_kzg_4844_cov: test_c_kzg_4844.c c_kzg_4844.c
@$(CC) $(CFLAGS) $(COVERAGE) -o $@ $< $(BLST)
.PHONY: blst
blst:
cd ../blst; \
${BLST_BUILD_SCRIPT} && \
@cd ../blst && \
./build.sh && \
cp libblst.a ../lib && \
cp bindings/*.h ../inc
# Make sure c_kzg_4844.o is built and copy it for the NodeJS bindings
lib: c_kzg_4844.o Makefile
cp *.o ../bindings/node.js
test_c_kzg_4844: test_c_kzg_4844.c c_kzg_4844.c Makefile
${CLANG_EXECUTABLE} -Wall -I$(INCLUDE_DIRS) -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB) -DUNIT_TESTS $(CFLAGS) -c c_kzg_4844.c -o test_c_kzg_4844.o
${CLANG_EXECUTABLE} -Wall -I$(INCLUDE_DIRS) -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB) $(CFLAGS) test_c_kzg_4844.o -L ../lib -lblst -o test_c_kzg_4844 $<
.PHONY: test
test: test_c_kzg_4844
./test_c_kzg_4844
test_c_kzg_4844_cov: test_c_kzg_4844.c c_kzg_4844.c Makefile
@${CLANG_EXECUTABLE} -fprofile-instr-generate -fcoverage-mapping -Wall -I$(INCLUDE_DIRS) -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB) -DUNIT_TESTS $(CFLAGS) -c c_kzg_4844.c -o test_c_kzg_4844.o
@${CLANG_EXECUTABLE} -fprofile-instr-generate -fcoverage-mapping -Wall -I$(INCLUDE_DIRS) -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB) $(CFLAGS) test_c_kzg_4844.o -L../lib -lblst -o test_c_kzg_4844 $<
@./test_c_kzg_4844
.PHONY: test_cov
test_cov: test_c_kzg_4844_cov
@LLVM_PROFILE_FILE="ckzg.profraw" ./test_c_kzg_4844
@LLVM_PROFILE_FILE="ckzg.profraw" ./test_c_kzg_4844_cov
@$(XCRUN) llvm-profdata merge --sparse ckzg.profraw -o ckzg.profdata
@$(XCRUN) llvm-cov show ./test_c_kzg_4844 --instr-profile=ckzg.profdata --format=html c_kzg_4844.c > coverage.html
@$(XCRUN) llvm-cov report ./test_c_kzg_4844 --instr-profile=ckzg.profdata --show-functions c_kzg_4844.c
@$(XCRUN) llvm-cov show --instr-profile=ckzg.profdata --format=html \
$< c_kzg_4844.c > coverage.html
@$(XCRUN) llvm-cov report --instr-profile=ckzg.profdata \
--show-functions $< c_kzg_4844.c
.PHONY: clean
clean:
rm -f *.o test_c_kzg_4844 *.profraw *.profdata *.html
@rm -f *.o *.profraw *.profdata *.html \
test_c_kzg_4844 test_c_kzg_4844_cov
.PHONY: format
format:
clang-format -i --sort-includes c_kzg_4844.* test_c_kzg_4844.c
@clang-format -i --sort-includes c_kzg_4844.* test_c_kzg_4844.c

View File

@ -32,12 +32,6 @@
#define CHECK(cond) \
if (!(cond)) return C_KZG_BADARGS
#ifdef UNIT_TESTS
#define STATIC
#else /* !defined(UNIT_TESTS) */
#define STATIC static
#endif /* defined(UNIT_TESTS) */
///////////////////////////////////////////////////////////////////////////////
// Constants
///////////////////////////////////////////////////////////////////////////////
@ -210,7 +204,7 @@ static C_KZG_RET new_fr_array(fr_t **x, size_t n) {
*
* @return The index of the highest set bit
*/
STATIC int log_2_byte(byte b) {
static int log_2_byte(byte b) {
if (b < 2) return 0;
if (b < 4) return 1;
if (b < 8) return 2;
@ -476,7 +470,7 @@ static int log2_pow2(uint32_t n) {
* @param[out] out A 48-byte array to store the serialized G1 element
* @param[in] in The G1 element to be serialized
*/
STATIC void bytes_from_g1(Bytes48 *out, const g1_t *in) {
static void bytes_from_g1(Bytes48 *out, const g1_t *in) {
blst_p1_compress(out->bytes, in);
}
@ -486,7 +480,7 @@ STATIC void bytes_from_g1(Bytes48 *out, const g1_t *in) {
* @param[out] out A 32-byte array to store the serialized field element
* @param[in] in The field element to be serialized
*/
STATIC void bytes_from_bls_field(Bytes32 *out, const fr_t *in) {
static void bytes_from_bls_field(Bytes32 *out, const fr_t *in) {
blst_scalar_from_fr((blst_scalar *)out->bytes, in);
}
@ -550,7 +544,7 @@ static bool is_power_of_two(uint64_t n) {
* @param[in] a The integer to be reversed
* @return An integer with the bits of @p a reversed
*/
STATIC uint32_t reverse_bits(uint32_t a) {
static uint32_t reverse_bits(uint32_t a) {
return rev_4byte(a);
}
@ -598,7 +592,7 @@ bit_reversal_permutation(void *values, size_t size, uint64_t n) {
* @param[out] out The field element to store the result
* @param[in] bytes A 32-byte array containing the input
*/
STATIC void hash_to_bls_field(fr_t *out, const Bytes32 *b) {
static void hash_to_bls_field(fr_t *out, const Bytes32 *b) {
blst_scalar tmp;
blst_scalar_from_lendian(&tmp, b->bytes);
blst_fr_from_scalar(out, &tmp);
@ -614,7 +608,7 @@ STATIC void hash_to_bls_field(fr_t *out, const Bytes32 *b) {
* @retval C_KZG_OK Deserialization successful
* @retval C_KZG_BADARGS Input was not a valid scalar field element
*/
STATIC C_KZG_RET bytes_to_bls_field(fr_t *out, const Bytes32 *b) {
static C_KZG_RET bytes_to_bls_field(fr_t *out, const Bytes32 *b) {
blst_scalar tmp;
blst_scalar_from_lendian(&tmp, b->bytes);
if (!blst_scalar_fr_check(&tmp)) return C_KZG_BADARGS;
@ -635,7 +629,7 @@ STATIC C_KZG_RET bytes_to_bls_field(fr_t *out, const Bytes32 *b) {
* @retval C_KZG_OK Deserialization successful
* @retval C_KZG_BADARGS Invalid input bytes
*/
STATIC C_KZG_RET validate_kzg_g1(g1_t *out, const Bytes48 *b) {
static C_KZG_RET validate_kzg_g1(g1_t *out, const Bytes48 *b) {
/* Convert the bytes to a p1 point */
blst_p1_affine p1_affine;
if (blst_p1_uncompress(&p1_affine, b->bytes) != BLST_SUCCESS)
@ -690,7 +684,7 @@ static C_KZG_RET bytes_to_kzg_proof(g1_t *out, const Bytes48 *b) {
* @retval C_KZG_OK Deserialization successful
* @retval C_KZG_BADARGS Invalid input bytes
*/
STATIC C_KZG_RET blob_to_polynomial(Polynomial *p, const Blob *blob) {
static C_KZG_RET blob_to_polynomial(Polynomial *p, const Blob *blob) {
C_KZG_RET ret;
for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) {
ret = bytes_to_bls_field(
@ -702,7 +696,7 @@ STATIC C_KZG_RET blob_to_polynomial(Polynomial *p, const Blob *blob) {
}
/* Forward function definition */
STATIC void compute_powers(fr_t *out, fr_t *x, uint64_t n);
static void compute_powers(fr_t *out, fr_t *x, uint64_t n);
/**
* Return the Fiat-Shamir challenges required by the rest of the protocol.
@ -810,7 +804,7 @@ static C_KZG_RET compute_challenges(
* We do the second of these to save memory here.
*/
static C_KZG_RET
g1_lincomb(g1_t *out, const g1_t *p, const fr_t *coeffs, const uint64_t len) {
g1_lincomb(g1_t *out, const g1_t *p, const fr_t *coeffs, uint64_t len) {
C_KZG_RET ret = C_KZG_MALLOC;
void *scratch = NULL;
blst_p1_affine *p_affine = NULL;
@ -839,7 +833,7 @@ g1_lincomb(g1_t *out, const g1_t *p, const fr_t *coeffs, const uint64_t len) {
blst_p1s_to_affine(p_affine, p_arg, len);
// Transform the field elements to 256-bit scalars
for (int i = 0; i < len; i++) {
for (uint64_t i = 0; i < len; i++) {
blst_scalar_from_fr(&scalars[i], &coeffs[i]);
}
@ -896,7 +890,7 @@ static void poly_lincomb(
* @param[in] x The field element to raise to powers
* @param[in] n The number of powers to compute
*/
STATIC void compute_powers(fr_t *out, fr_t *x, uint64_t n) {
static void compute_powers(fr_t *out, fr_t *x, uint64_t n) {
fr_t current_power = FR_ONE;
for (uint64_t i = 0; i < n; i++) {
out[i] = current_power;
@ -919,7 +913,7 @@ STATIC void compute_powers(fr_t *out, fr_t *x, uint64_t n) {
* @retval C_KZG_OK Evaluation successful
* @retval C_KZG_MALLOC Memory allocation failed
*/
STATIC C_KZG_RET evaluate_polynomial_in_evaluation_form(
static C_KZG_RET evaluate_polynomial_in_evaluation_form(
fr_t *out, const Polynomial *p, const fr_t *x, const KZGSettings *s
) {
C_KZG_RET ret;

View File

@ -177,21 +177,6 @@ C_KZG_RET compute_kzg_proof(
const KZGSettings *s
);
#ifdef UNIT_TESTS
void hash_to_bls_field(fr_t *out, const Bytes32 *b);
void bytes_from_bls_field(Bytes32 *out, const fr_t *in);
C_KZG_RET validate_kzg_g1(g1_t *out, const Bytes48 *b);
void bytes_from_g1(Bytes48 *out, const g1_t *in);
C_KZG_RET evaluate_polynomial_in_evaluation_form(
fr_t *out, const Polynomial *p, const fr_t *x, const KZGSettings *s
);
C_KZG_RET blob_to_polynomial(Polynomial *p, const Blob *blob);
C_KZG_RET bytes_to_bls_field(fr_t *out, const Bytes32 *b);
uint32_t reverse_bits(uint32_t a);
void compute_powers(fr_t *out, fr_t *x, uint64_t n);
int log_2_byte(byte b);
#endif
#ifdef __cplusplus
}
#endif

View File

@ -1,9 +1,7 @@
/*
* This file contains unit tests for C-KZG-4844.
*/
#define UNIT_TESTS
#include "c_kzg_4844.h"
#include "c_kzg_4844.c"
#include "tinytest.h"
#include <assert.h>
@ -60,7 +58,7 @@ static void get_rand_g1_bytes(Bytes48 *out) {
static void bytes32_from_hex(Bytes32 *out, const char *hex) {
int matches;
for (int i = 0; i < sizeof(Bytes32); i++) {
for (size_t i = 0; i < sizeof(Bytes32); i++) {
matches = sscanf(hex + i * 2, "%2hhx", &out->bytes[i]);
ASSERT_EQUALS(matches, 1);
}
@ -68,7 +66,7 @@ static void bytes32_from_hex(Bytes32 *out, const char *hex) {
static void bytes48_from_hex(Bytes48 *out, const char *hex) {
int matches;
for (int i = 0; i < sizeof(Bytes48); i++) {
for (size_t i = 0; i < sizeof(Bytes48); i++) {
matches = sscanf(hex + i * 2, "%2hhx", &out->bytes[i]);
ASSERT_EQUALS(matches, 1);
}