Skip to content

Commit

Permalink
Split C code into more files (#471)
Browse files Browse the repository at this point in the history
* Split C code into more files

* Fix rust bindings

* Add common/fr.* files

* Do more common splits

* More common split/cleanup

* Fix lincomb definition

* Get rid of common/constants.h

* Get rid of common/types.h

* Move common/blob to eip4844/blob

* Delete test/debug.h

* Add dir to all includes

* Generate rust bindings

* Set InsertNewlineAtEOF to true

* Move preprocessor thing down

* Merge g1/g2 into ec

* Move KZGCommitment/Proof to eip4844

* Remove ret.c & settings.c

* Rename helpers to utils

* Move settings.h to setup

* Rename api.c/h to <dir>.c/h

* Create new eip7594/poly files

* Split eip7594 into fk20 & recovery files

* Move verification helpers to verify section
  • Loading branch information
jtraglia authored Aug 6, 2024
1 parent 7f5bc8f commit ff4f3b7
Show file tree
Hide file tree
Showing 43 changed files with 2,564 additions and 1,904 deletions.
17 changes: 5 additions & 12 deletions bindings/node.js/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,13 @@ build: install clean
@# Prepare the dependencies directory
@mkdir -p deps/c-kzg
@cp -r ../../blst deps
@# Copy source files
@# Copy files
@cp ../../src/ckzg.c deps/c-kzg
@cp ../../src/common.c deps/c-kzg
@cp ../../src/eip4844.c deps/c-kzg
@cp ../../src/eip7594.c deps/c-kzg
@cp ../../src/fft.c deps/c-kzg
@cp ../../src/setup.c deps/c-kzg
@# Copy header files
@cp ../../src/ckzg.h deps/c-kzg
@cp ../../src/common.h deps/c-kzg
@cp ../../src/eip4844.h deps/c-kzg
@cp ../../src/eip7594.h deps/c-kzg
@cp ../../src/fft.h deps/c-kzg
@cp ../../src/setup.h deps/c-kzg
@cp -r ../../src/common deps/c-kzg
@cp -r ../../src/eip4844 deps/c-kzg
@cp -r ../../src/eip7594 deps/c-kzg
@cp -r ../../src/setup deps/c-kzg
@# Copy trusted setup
@cp ../../src/trusted_setup.txt deps/c-kzg
@# Build the bindings
Expand Down
17 changes: 13 additions & 4 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn main() {
cc.flag("/std:c11");
}

cc.include(c_src_dir.clone());
cc.include(blst_headers_dir.clone());
cc.warnings(false);
cc.file(c_src_dir.join("ckzg.c"));
Expand All @@ -36,6 +37,7 @@ fn main() {
let bindings_out_path = root_dir.join("bindings/rust/src/bindings/generated.rs");
make_bindings(
header_path.to_str().expect("valid header path"),
c_src_dir.to_str().expect("valid c src path"),
blst_headers_dir.to_str().expect("valid blst header path"),
&bindings_out_path,
);
Expand All @@ -46,7 +48,12 @@ fn main() {
}

#[cfg(feature = "generate-bindings")]
fn make_bindings(header_path: &str, blst_headers_dir: &str, bindings_out_path: &std::path::Path) {
fn make_bindings(
header_path: &str,
c_src_dir: &str,
blst_headers_dir: &str,
bindings_out_path: &std::path::Path,
) {
use bindgen::Builder;

#[derive(Debug)]
Expand Down Expand Up @@ -77,13 +84,15 @@ fn make_bindings(header_path: &str, blst_headers_dir: &str, bindings_out_path: &
* Header definitions.
*/
.header(header_path)
.clang_args([format!("-I{c_src_dir}")])
.clang_args([format!("-I{blst_headers_dir}")])
// Get bindings for functions defined in the EIP files.
.allowlist_type("C_KZG_RET")
.allowlist_var("BYTES_PER_.*")
.allowlist_var("FIELD_ELEMENTS_PER_BLOB")
.allowlist_var("FIELD_ELEMENTS_PER_EXT_BLOB")
.allowlist_file(".*eip.*.h")
.allowlist_var("FIELD_ELEMENTS_PER_.*")
.allowlist_var("CELLS_PER_EXT_BLOB")
.allowlist_file(".*eip4844.h")
.allowlist_file(".*eip7594.h")
.allowlist_file(".*setup.h")
/*
* Cleanup instructions.
Expand Down
14 changes: 8 additions & 6 deletions bindings/rust/src/bindings/generated.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ BinPackParameters: False
PenaltyReturnTypeOnItsOwnLine: 1000
PenaltyBreakAssignment: 100
ColumnLimit: 100
InsertNewlineAtEOF: true
18 changes: 9 additions & 9 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ifeq ($(PLATFORM),Darwin)
endif

# The base compiler flags. More can be added on command line.
CFLAGS += -I../inc -O2 -Wall -Wextra
CFLAGS += -I. -I../inc -O2 -Wall -Wextra

# Cross-platform compilation settings.
ifeq ($(PLATFORM),Windows)
Expand All @@ -41,13 +41,13 @@ LIBS = $(BLST_LIBRARY)

# Create file lists.
SOURCE_FILES := $(shell find . -name '*.c' | sed 's|^\./||' | sort)
HEADER_FILES := $(patsubst %.c, %.h, $(SOURCE_FILES))
HEADER_FILES := $(shell find . -name '*.h' | sed 's|^\./||' | sort)
OBJECT_FILES := $(patsubst %.c, %.o, $(SOURCE_FILES))

# There is no tests header file.
HEADER_FILES := $(filter-out tests.h, $(HEADER_FILES))
HEADER_FILES := $(filter-out test/tests.h, $(HEADER_FILES))
# We don't want to format this and it is not expected to change.
HEADER_FILES := $(filter-out tinytest.h, $(HEADER_FILES))
HEADER_FILES := $(filter-out test/tinytest.h, $(HEADER_FILES))

###############################################################################
# Core
Expand All @@ -69,7 +69,7 @@ blst: $(BLST_LIBRARY)

tests: CFLAGS += -O0
tests: $(SOURCE_FILES) $(HEADER_FILES) $(BLST_LIBRARY)
@$(CC) $(CFLAGS) -o $@ tests.c $(LIBS)
@$(CC) $(CFLAGS) -o $@ test/tests.c $(LIBS)

.PHONY: test
test: tests
Expand All @@ -81,7 +81,7 @@ test: tests

tests_cov: CFLAGS += -O0 -fprofile-instr-generate -fcoverage-mapping
tests_cov: $(SOURCE_FILES) $(HEADER_FILES) $(BLST_LIBRARY)
@$(CC) $(CFLAGS) -o $@ tests.c $(LIBS)
@$(CC) $(CFLAGS) -o $@ test/tests.c $(LIBS)

.PHONY: coverage
coverage: tests_cov
Expand All @@ -103,7 +103,7 @@ tests_prof: CFLAGS += -L$(shell brew --prefix gperftools)/lib
tests_prof: CFLAGS += -I$(shell brew --prefix gperftools)/include
endif
tests_prof: $(SOURCE_FILES) $(HEADER_FILES) $(BLST_LIBRARY)
@$(CC) $(CFLAGS) -o $@ tests.c $(LIBS)
@$(CC) $(CFLAGS) -o $@ test/tests.c $(LIBS)

.PHONY: run_profiler
run_profiler: tests_prof
Expand Down Expand Up @@ -135,7 +135,7 @@ profile: \
sanitize_%: CFLAGS += -O0 -fsanitize=$*
sanitize_%: $(SOURCE_FILES) $(HEADER_FILES) $(BLST_LIBRARY)
@echo Running sanitize=$*...
@$(CC) $(CFLAGS) -o $@ tests.c $(LIBS)
@$(CC) $(CFLAGS) -o $@ test/tests.c $(LIBS)
@ASAN_OPTIONS=allocator_may_return_null=1 \
LSAN_OPTIONS=allocator_may_return_null=1 \
./$@; rm $@
Expand Down Expand Up @@ -173,6 +173,6 @@ format:

.PHONY: clean
clean:
@rm -f *.o *.profraw *.profdata *.html xray-log.* *.prof *.pdf \
@rm -f *.o */*.o *.profraw *.profdata *.html xray-log.* *.prof *.pdf \
tests tests_cov tests_prof
@rm -rf analysis-report
20 changes: 15 additions & 5 deletions src/ckzg.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@
* limitations under the License.
*/

#include "common.c"
#include "eip4844.c"
#include "eip7594.c"
#include "fft.c"
#include "setup.c"
#include "common/alloc.c"
#include "common/bytes.c"
#include "common/ec.c"
#include "common/fr.c"
#include "common/lincomb.c"
#include "common/utils.c"
#include "eip4844/blob.c"
#include "eip4844/eip4844.c"
#include "eip7594/cell.c"
#include "eip7594/eip7594.c"
#include "eip7594/fft.c"
#include "eip7594/fk20.c"
#include "eip7594/poly.c"
#include "eip7594/recovery.c"
#include "setup/setup.c"
6 changes: 3 additions & 3 deletions src/ckzg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

#pragma once

#include "eip4844.h"
#include "eip7594.h"
#include "setup.h"
#include "eip4844/eip4844.h"
#include "eip7594/eip7594.h"
#include "setup/setup.h"
Loading

0 comments on commit ff4f3b7

Please sign in to comment.