Merge pull request #12 from StefanBratanov/java_bindings_improvements

Java bindings improvements
This commit is contained in:
Ramana Kumar 2022-11-26 22:07:36 +00:00 committed by GitHub
commit 8a6f9f5b7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 164 additions and 66 deletions

View File

@ -1,7 +1,9 @@
INCLUDE_DIRS = ../../src ../../blst/bindings INCLUDE_DIRS = ../../src ../../blst/bindings
LIBRARY_FOLDER=src/main/resources/ethereum/ckzg4844/lib
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
CLANG_EXECUTABLE=gcc CLANG_EXECUTABLE=clang
GRADLE_COMMAND=gradlew GRADLE_COMMAND=gradlew
CLANG_FLAGS=-shared CLANG_FLAGS=-shared
JNI_INCLUDE_FOLDER=win32 JNI_INCLUDE_FOLDER=win32
@ -32,9 +34,9 @@ endif
all: build test all: build test
build: build:
${CLANG_EXECUTABLE} ${CLANG_FLAGS} -O -Wall ${addprefix -I,${INCLUDE_DIRS}} -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/${JNI_INCLUDE_FOLDER}" -o src/main/resources/lib/${LIBRARY_RESOURCE} c_kzg_4844_jni.c c_kzg_4844.o ../../lib/libblst.a mkdir -p ${LIBRARY_FOLDER}
${CLANG_EXECUTABLE} ${CLANG_FLAGS} -O -Wall ${addprefix -I,${INCLUDE_DIRS}} -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/${JNI_INCLUDE_FOLDER}" -o ${LIBRARY_FOLDER}/${LIBRARY_RESOURCE} c_kzg_4844_jni.c c_kzg_4844.o ../../lib/libblst.a
test: test:
${GRADLE_COMMAND} clean test ${GRADLE_COMMAND} clean test

View File

@ -2,15 +2,15 @@
## Prerequisites ## Prerequisites
* Follow the instructions in the [README.md](../../README.md) to install blst and build the C-KZG code. * Follow the instructions in the [README.md](../../README.md) to install blst and build the C-KZG library.
* `JAVA_HOME` environment variable is set to a JDK with an `include` folder containing a jni.h file. * `JAVA_HOME` environment variable is set to a JDK with an `include` folder containing a `jni.h` file.
## Build ## Build
```bash ```bash
make build make build
``` ```
This will install the library in the `src/main/resources/lib` folder according to your OS This will install the library in the `src/main/resources/ethereum/ckzg4844/lib` folder with a name according to your OS
## Test ## Test
```bash ```bash

View File

@ -7,7 +7,12 @@ repositories {
} }
dependencies { dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
def junitVersion = "5.9.1"
testImplementation("org.junit.jupiter:junit-jupiter:${junitVersion}")
testImplementation("org.junit.jupiter:junit-jupiter-params:${junitVersion}")
} }
test { test {

View File

@ -22,7 +22,7 @@ void throw_exception(JNIEnv *env, const char *message)
(*env)->ThrowNew(env, Exception, message); (*env)->ThrowNew(env, Exception, message);
} }
JNIEXPORT void JNICALL Java_CKzg4844JNI_loadTrustedSetup(JNIEnv *env, jclass thisCls, jstring file) JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_loadTrustedSetup(JNIEnv *env, jclass thisCls, jstring file)
{ {
if (settings != NULL) if (settings != NULL)
{ {
@ -61,7 +61,7 @@ JNIEXPORT void JNICALL Java_CKzg4844JNI_loadTrustedSetup(JNIEnv *env, jclass thi
(*env)->ReleaseStringUTFChars(env, file, file_native); (*env)->ReleaseStringUTFChars(env, file, file_native);
} }
JNIEXPORT void JNICALL Java_CKzg4844JNI_freeTrustedSetup(JNIEnv *env, jclass thisCls) JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_freeTrustedSetup(JNIEnv *env, jclass thisCls)
{ {
if (settings == NULL) if (settings == NULL)
{ {
@ -72,7 +72,7 @@ JNIEXPORT void JNICALL Java_CKzg4844JNI_freeTrustedSetup(JNIEnv *env, jclass thi
reset_trusted_setup(); reset_trusted_setup();
} }
JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_computeAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jlong count) JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_computeAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jlong count)
{ {
if (settings == NULL) if (settings == NULL)
{ {
@ -104,7 +104,7 @@ JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_computeAggregateKzgProof(JNIEnv *e
return proof; return proof;
} }
JNIEXPORT jboolean JNICALL Java_CKzg4844JNI_verifyAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jbyteArray commitments, jlong count, jbyteArray proof) JNIEXPORT jboolean JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_verifyAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jbyteArray commitments, jlong count, jbyteArray proof)
{ {
if (settings == NULL) if (settings == NULL)
{ {
@ -163,7 +163,7 @@ JNIEXPORT jboolean JNICALL Java_CKzg4844JNI_verifyAggregateKzgProof(JNIEnv *env,
return (jboolean)out; return (jboolean)out;
} }
JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_blobToKzgCommitment(JNIEnv *env, jclass thisCls, jbyteArray blob) JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_blobToKzgCommitment(JNIEnv *env, jclass thisCls, jbyteArray blob)
{ {
if (settings == NULL) if (settings == NULL)
{ {
@ -186,7 +186,7 @@ JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_blobToKzgCommitment(JNIEnv *env, j
return commitment; return commitment;
} }
JNIEXPORT jboolean JNICALL Java_CKzg4844JNI_verifyKzgProof(JNIEnv *env, jclass thisCls, jbyteArray commitment, jbyteArray z, jbyteArray y, jbyteArray proof) JNIEXPORT jboolean JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_verifyKzgProof(JNIEnv *env, jclass thisCls, jbyteArray commitment, jbyteArray z, jbyteArray y, jbyteArray proof)
{ {
if (settings == NULL) if (settings == NULL)
{ {

View File

@ -1,59 +1,54 @@
/* DO NOT EDIT THIS FILE - it is machine generated */ /* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h> #include <jni.h>
/* Header for class CKzg4844JNI */ /* Header for class ethereum_ckzg4844_CKzg4844JNI */
#ifndef _Included_CKzg4844JNI #ifndef _Included_ethereum_ckzg4844_CKzg4844JNI
#define _Included_CKzg4844JNI #define _Included_ethereum_ckzg4844_CKzg4844JNI
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
/* /*
* Class: CKzg4844JNI * Class: ethereum_ckzg4844_CKzg4844JNI
* Method: loadTrustedSetup * Method: loadTrustedSetup
* Signature: (Ljava/lang/String;)V * Signature: (Ljava/lang/String;)V
*/ */
JNIEXPORT void JNICALL Java_CKzg4844JNI_loadTrustedSetup JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_loadTrustedSetup(JNIEnv *, jclass, jstring);
(JNIEnv *, jclass, jstring);
/* /*
* Class: CKzg4844JNI * Class: ethereum_ckzg4844_CKzg4844JNI
* Method: freeTrustedSetup * Method: freeTrustedSetup
* Signature: ()V * Signature: ()V
*/ */
JNIEXPORT void JNICALL Java_CKzg4844JNI_freeTrustedSetup JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_freeTrustedSetup(JNIEnv *, jclass);
(JNIEnv *, jclass);
/* /*
* Class: CKzg4844JNI * Class: ethereum_ckzg4844_CKzg4844JNI
* Method: computeAggregateKzgProof * Method: computeAggregateKzgProof
* Signature: ([BJ)[B * Signature: ([BJ)[B
*/ */
JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_computeAggregateKzgProof JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_computeAggregateKzgProof(JNIEnv *, jclass, jbyteArray, jlong);
(JNIEnv *, jclass, jbyteArray, jlong);
/* /*
* Class: CKzg4844JNI * Class: ethereum_ckzg4844_CKzg4844JNI
* Method: verifyAggregateKzgProof * Method: verifyAggregateKzgProof
* Signature: ([B[BJ[B)Z * Signature: ([B[BJ[B)Z
*/ */
JNIEXPORT jboolean JNICALL Java_CKzg4844JNI_verifyAggregateKzgProof JNIEXPORT jboolean JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_verifyAggregateKzgProof(JNIEnv *, jclass, jbyteArray, jbyteArray, jlong, jbyteArray);
(JNIEnv *, jclass, jbyteArray, jbyteArray, jlong, jbyteArray);
/* /*
* Class: CKzg4844JNI * Class: ethereum_ckzg4844_CKzg4844JNI
* Method: blobToKzgCommitment * Method: blobToKzgCommitment
* Signature: ([B)[B * Signature: ([B)[B
*/ */
JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_blobToKzgCommitment JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_blobToKzgCommitment(JNIEnv *, jclass, jbyteArray);
(JNIEnv *, jclass, jbyteArray);
/* /*
* Class: CKzg4844JNI * Class: ethereum_ckzg4844_CKzg4844JNI
* Method: verifyKzgProof * Method: verifyKzgProof
* Signature: ([B[B[B[B)Z * Signature: ([B[B[B[B)Z
*/ */
JNIEXPORT jboolean JNICALL Java_CKzg4844JNI_verifyKzgProof JNIEXPORT jboolean JNICALL Java_ethereum_ckzg4844_CKzg4844JNI_verifyKzgProof(JNIEnv *, jclass, jbyteArray, jbyteArray, jbyteArray, jbyteArray);
(JNIEnv *, jclass, jbyteArray, jbyteArray, jbyteArray, jbyteArray);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,3 +1,5 @@
package ethereum.ckzg4844;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
@ -39,17 +41,60 @@ public class CKzg4844JNI {
public static int BYTES_PER_FIELD_ELEMENT = 32; public static int BYTES_PER_FIELD_ELEMENT = 32;
public static int BYTES_PER_BLOB = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT; public static int BYTES_PER_BLOB = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT;
/**
* Loads the trusted setup from a file. Once loaded, the same setup will be used for all the
* native calls. To load a new setup, free the current one by calling {@link #freeTrustedSetup()}
* and then load the new one. If no trusted setup has been loaded, all the native calls will throw
* an exception.
*
* @param file a path to a trusted setup file
*/
public static native void loadTrustedSetup(String file); public static native void loadTrustedSetup(String file);
/**
* Free the current trusted setup. This method will throw an exception if no trusted setup has
* been loaded.
*/
public static native void freeTrustedSetup(); public static native void freeTrustedSetup();
/**
* Calculates aggregated proof for the given blobs
*
* @param blobs blobs as flattened bytes
* @param count the count of the blobs
* @return the aggregated proof
*/
public static native byte[] computeAggregateKzgProof(byte[] blobs, long count); public static native byte[] computeAggregateKzgProof(byte[] blobs, long count);
/**
* Verify aggregated proof and commitments for the given blobs
*
* @param blobs blobs as flattened bytes
* @param commitments commitments as flattened bytes
* @param count the count of the blobs (should be same as the count of the commitments)
* @param proof the proof that needs verifying
* @return true if the proof is valid and false otherwise
*/
public static native boolean verifyAggregateKzgProof(byte[] blobs, byte[] commitments, long count, public static native boolean verifyAggregateKzgProof(byte[] blobs, byte[] commitments, long count,
byte[] proof); byte[] proof);
/**
* Calculates commitment for a given blob
*
* @param blob blob bytes
* @return the commitment
*/
public static native byte[] blobToKzgCommitment(byte[] blob); public static native byte[] blobToKzgCommitment(byte[] blob);
/**
* Verify the proof by point evaluation for the given commitment
*
* @param commitment commitment bytes
* @param z Z
* @param y Y
* @param proof the proof that needs verifying
* @return true if the proof is valid and false otherwise
*/
public static native boolean verifyKzgProof(byte[] commitment, byte[] z, byte[] y, byte[] proof); public static native boolean verifyKzgProof(byte[] commitment, byte[] z, byte[] y, byte[] proof);
} }

View File

@ -1,3 +1,5 @@
package ethereum.ckzg4844;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@ -6,9 +8,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public class CKZg4844JNITest { public class CKzg4844JNITest {
private final Random random = new Random(); private final Random random = new Random();
@ -26,7 +32,6 @@ public class CKZg4844JNITest {
assertEquals(CKzg4844JNI.BYTES_PER_COMMITMENT, commitment.length); assertEquals(CKzg4844JNI.BYTES_PER_COMMITMENT, commitment.length);
assertEquals(CKzg4844JNI.BYTES_PER_COMMITMENT, commitment2.length); assertEquals(CKzg4844JNI.BYTES_PER_COMMITMENT, commitment2.length);
// flatten blobs and commitments
final byte[] blobs = flatten(blob, blob2); final byte[] blobs = flatten(blob, blob2);
final byte[] commitments = flatten(commitment, commitment2); final byte[] commitments = flatten(commitment, commitment2);
@ -36,7 +41,7 @@ public class CKZg4844JNITest {
assertTrue(CKzg4844JNI.verifyAggregateKzgProof(blobs, commitments, 2, proof)); assertTrue(CKzg4844JNI.verifyAggregateKzgProof(blobs, commitments, 2, proof));
final byte[] fakeProof = createRandomProof(); final byte[] fakeProof = createRandomProof(2);
assertFalse(CKzg4844JNI.verifyAggregateKzgProof(blobs, commitments, 2, fakeProof)); assertFalse(CKzg4844JNI.verifyAggregateKzgProof(blobs, commitments, 2, fakeProof));
CKzg4844JNI.freeTrustedSetup(); CKzg4844JNI.freeTrustedSetup();
@ -61,6 +66,36 @@ public class CKZg4844JNITest {
} }
@Disabled("Use for manually testing performance.")
@ParameterizedTest
@ValueSource(ints = {1, 10, 100, 1000})
public void testPerformance(final int count) {
loadTrustedSetup();
final byte[] blobs = createRandomBlobs(count);
final byte[] commitments = getCommitmentsForBlobs(blobs, count);
long startTime = System.currentTimeMillis();
final byte[] proof = CKzg4844JNI.computeAggregateKzgProof(blobs, count);
long endTime = System.currentTimeMillis();
System.out.printf("Computing aggregate proof for %d blobs took %d milliseconds%n", count,
endTime - startTime);
startTime = System.currentTimeMillis();
boolean proofValidity = CKzg4844JNI.verifyAggregateKzgProof(blobs, commitments, count, proof);
endTime = System.currentTimeMillis();
assertTrue(proofValidity);
System.out.printf("Verifying aggregate proof for %d blobs took %d milliseconds%n", count,
endTime - startTime);
CKzg4844JNI.freeTrustedSetup();
}
@Test @Test
public void throwsIfMethodIsUsedWithoutLoadingTrustedSetup() { public void throwsIfMethodIsUsedWithoutLoadingTrustedSetup() {
@ -82,6 +117,7 @@ public class CKZg4844JNITest {
exception.getMessage()); exception.getMessage());
CKzg4844JNI.freeTrustedSetup(); CKzg4844JNI.freeTrustedSetup();
} }
@Test @Test
@ -108,12 +144,27 @@ public class CKZg4844JNITest {
return blob; return blob;
} }
private byte[] createRandomProof() { private byte[] createRandomBlobs(final int count) {
final byte[] blob = createRandomBlob(); final byte[][] blobs = IntStream.range(0, count).mapToObj(__ -> createRandomBlob())
return CKzg4844JNI.computeAggregateKzgProof(blob, 1); .toArray(byte[][]::new);
return flatten(blobs);
} }
private byte[] flatten(byte[]... bytes) { private byte[] createRandomProof(final int count) {
return CKzg4844JNI.computeAggregateKzgProof(createRandomBlobs(count), count);
}
private byte[] getCommitmentsForBlobs(final byte[] blobs, final int count) {
final byte[][] commitments = new byte[count][];
IntStream.range(0, count).forEach(i -> {
final byte[] blob = Arrays.copyOfRange(blobs, i * CKzg4844JNI.BYTES_PER_BLOB,
(i + 1) * CKzg4844JNI.BYTES_PER_BLOB);
commitments[i] = CKzg4844JNI.blobToKzgCommitment(blob);
});
return flatten(commitments);
}
private byte[] flatten(final byte[]... bytes) {
final int capacity = Arrays.stream(bytes).mapToInt(b -> b.length).sum(); final int capacity = Arrays.stream(bytes).mapToInt(b -> b.length).sum();
final ByteBuffer buffer = ByteBuffer.allocate(capacity); final ByteBuffer buffer = ByteBuffer.allocate(capacity);
Arrays.stream(bytes).forEach(buffer::put); Arrays.stream(bytes).forEach(buffer::put);