Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use official trusted setup #377

Merged
merged 19 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
32 changes: 4 additions & 28 deletions .github/workflows/c-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,10 @@ jobs:
make format
git diff --exit-code

# 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)
# Build and test.
- name: Build
run: make test_c_kzg_4844
- name: Test
run: make test

# Run sanitizers.
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/java-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ jobs:
run: |
cd src
make blst
- name: Build and Test (mainnet preset)
- name: Build and Test
run: |
cd bindings/java
make build test
- name: Build and Test (minimal preset)
run: |
cd bindings/java
make PRESET=minimal build test
7 changes: 2 additions & 5 deletions .github/workflows/rust-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ jobs:
submodules: recursive
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Build and Test (minimal preset)
- name: Build and Test
working-directory: bindings/rust
run: cargo test --features minimal-spec
- name: Build and Test (mainnet preset)
working-directory: bindings/rust
run: cargo test --features mainnet-spec
run: cargo test
- name: Benchmark
working-directory: bindings/rust
run: cargo bench
4 changes: 1 addition & 3 deletions bindings/csharp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ else
CKZG_LIBRARY_PATH = Ckzg.Bindings/runtimes/$(LOCATION)/native/ckzg$(EXTENSION)
endif

FIELD_ELEMENTS_PER_BLOB ?= 4096
INCLUDE_DIRS = ../../src ../../blst/bindings
TARGETS = ckzg.c ../../src/c_kzg_4844.c ../../blst/$(BLST_OBJ)

CFLAGS += -O2 -Wall -Wextra -Wpedantic -shared
CFLAGS += -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB)
CFLAGS += -O2 -Wall -Wextra -shared
flcl42 marked this conversation as resolved.
Show resolved Hide resolved
CFLAGS += ${addprefix -I,${INCLUDE_DIRS}}
BLST_BUILDSCRIPT_FLAGS += -D__BLST_PORTABLE__
ifdef ARCH
Expand Down
8 changes: 0 additions & 8 deletions bindings/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ Run the benchmarks with this command:
go test -bench=Benchmark
```

## Minimal

By default, `FIELD_ELEMENTS_PER_BLOB` will be 4096 (mainnet), but you can
manually set it to 4 (minimal), like so:
```
CGO_CFLAGS="-DFIELD_ELEMENTS_PER_BLOB=4" go build
```

## Note

The `go.mod` and `go.sum` files are in the project's root directory because the
Expand Down
3 changes: 0 additions & 3 deletions bindings/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package ckzg4844

// #cgo CFLAGS: -I${SRCDIR}/../../src
// #cgo CFLAGS: -I${SRCDIR}/blst_headers
// #ifndef FIELD_ELEMENTS_PER_BLOB
// #define FIELD_ELEMENTS_PER_BLOB 4096
// #endif
// #include "c_kzg_4844.c"
import "C"

Expand Down
12 changes: 2 additions & 10 deletions bindings/java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,7 @@ else
GRADLE_COMMAND=./gradlew
endif

PRESET ?= mainnet

ifeq ($(PRESET),minimal)
FIELD_ELEMENTS_PER_BLOB ?= 4
else
FIELD_ELEMENTS_PER_BLOB ?= 4096
endif

LIBRARY_FOLDER=src/main/resources/ethereum/ckzg4844/lib/${OS_ARCH}/${PRESET}
LIBRARY_FOLDER=src/main/resources/ethereum/ckzg4844/lib/${OS_ARCH}

ifeq ($(JAVA_HOME),)
$(error JAVA_HOME is not set and autodetection failed)
Expand All @@ -61,7 +53,7 @@ all: build test benchmark
.PHONY: build
build:
mkdir -p ${LIBRARY_FOLDER}
${CLANG_EXECUTABLE} ${CC_FLAGS} ${CLANG_FLAGS} ${OPTIMIZATION_LEVEL} -Wall -Wextra -Wpedantic -Werror -Wno-missing-braces -Wno-unused-parameter -Wno-format ${addprefix -I,${INCLUDE_DIRS}} -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/${JNI_INCLUDE_FOLDER}" -DFIELD_ELEMENTS_PER_BLOB=${FIELD_ELEMENTS_PER_BLOB} -o ${LIBRARY_FOLDER}/${LIBRARY_RESOURCE} ${TARGETS}
${CLANG_EXECUTABLE} ${CC_FLAGS} ${CLANG_FLAGS} ${OPTIMIZATION_LEVEL} -Wall -Wextra -Werror -Wno-missing-braces -Wno-unused-parameter -Wno-format ${addprefix -I,${INCLUDE_DIRS}} -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/${JNI_INCLUDE_FOLDER}" -o ${LIBRARY_FOLDER}/${LIBRARY_RESOURCE} ${TARGETS}

.PHONY: test
test:
Expand Down
3 changes: 1 addition & 2 deletions bindings/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ make build
```

This will install the shared library in `src/main/resources/ethereum/ckzg4844/lib` with a folder
structure
and name according to the preset selected (mainnet or minimal) and your OS.
structure and name according your OS.

All variables which could be passed to the `make` command and the defaults can be found in
the [Makefile](./Makefile).
Expand Down
5 changes: 0 additions & 5 deletions bindings/java/c_kzg_4844_jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ KZGSettings *allocate_settings(JNIEnv *env)
return s;
}

JNIEXPORT jint JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_getFieldElementsPerBlob(JNIEnv *env, jclass thisCls)
{
return (jint)FIELD_ELEMENTS_PER_BLOB;
}

JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup__Ljava_lang_String_2(JNIEnv *env, jclass thisCls, jstring file)
{
if (settings)
Expand Down
11 changes: 4 additions & 7 deletions bindings/java/c_kzg_4844_jni.h

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ethereum.ckzg4844;

import ethereum.ckzg4844.CKZG4844JNI.Preset;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
Expand All @@ -25,7 +24,7 @@
public class CKZG4844JNIBenchmark {

static {
CKZG4844JNI.loadNativeLibrary(Preset.MAINNET);
CKZG4844JNI.loadNativeLibrary();
}

@State(Scope.Benchmark)
Expand Down
47 changes: 7 additions & 40 deletions bindings/java/src/main/java/ethereum/ckzg4844/CKZG4844JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,10 @@ public class CKZG4844JNI {
private static final String LIBRARY_NAME = "ckzg4844jni";
private static final String PLATFORM_NATIVE_LIBRARY_NAME = System.mapLibraryName(LIBRARY_NAME);

/**
* Loads the appropriate native library based on your platform and the selected {@link Preset}
*
* @param preset the preset
*/
public static void loadNativeLibrary(Preset preset) {
/** Loads the appropriate native library based on your platform. */
public static void loadNativeLibrary() {
String libraryResourcePath =
"lib/"
+ System.getProperty("os.arch")
+ "/"
+ preset.name().toLowerCase()
+ "/"
+ PLATFORM_NATIVE_LIBRARY_NAME;
"lib/" + System.getProperty("os.arch") + "/" + PLATFORM_NATIVE_LIBRARY_NAME;
InputStream libraryResource = CKZG4844JNI.class.getResourceAsStream(libraryResourcePath);
if (libraryResource == null) {
try {
Expand All @@ -52,17 +43,6 @@ public static void loadNativeLibrary(Preset preset) {
}
}

public enum Preset {
MAINNET(4096),
MINIMAL(4);

public final int fieldElementsPerBlob;

Preset(int fieldElementsPerBlob) {
this.fieldElementsPerBlob = fieldElementsPerBlob;
}
}

/** Scalar field modulus of BLS12-381 */
public static final BigInteger BLS_MODULUS =
new BigInteger(
Expand All @@ -77,26 +57,13 @@ public enum Preset {
public static final int BYTES_PER_PROOF = 48;
/** Bytes used to encode a BLS scalar field element */
public static final int BYTES_PER_FIELD_ELEMENT = 32;
/** Number of field elements in a blob */
public static final int FIELD_ELEMENTS_PER_BLOB = 4096;
/** Number of field elements in a blob */
public static final int BYTES_PER_BLOB = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT;

private CKZG4844JNI() {}

/**
* Calculates the bytes per blob based on the output from {@link #getFieldElementsPerBlob()}
*
* @return the bytes per blob
*/
public static int getBytesPerBlob() {
return getFieldElementsPerBlob() * BYTES_PER_FIELD_ELEMENT;
}

/**
* Retrieves the compile-time configured FIELD_ELEMENTS_PER_BLOB. The value will be based on the
* selected {@link Preset} when loading the native library.
*
* @return the field elements per blob
*/
public static native int getFieldElementsPerBlob();

/**
* Loads the trusted setup from a file. Once loaded, the same setup will be used for all the
* crypto native calls. To load a new setup, free the current one by calling {@link
Expand Down
Loading
Loading