From e4f280f17ffe80c6292d0d117262719b691082fb Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Fri, 27 Jan 2023 16:16:41 +0100 Subject: [PATCH] Add compute_kzg_proof to java bindings (#94) --- bindings/java/c_kzg_4844_jni.c | 28 +++++++++++++++++++ bindings/java/c_kzg_4844_jni.h | 7 +++++ .../java/ethereum/ckzg4844/CKZG4844JNI.java | 10 +++++++ .../ethereum/ckzg4844/CKZG4844JNITest.java | 9 ++++++ .../java/ethereum/ckzg4844/TestUtils.java | 4 +++ 5 files changed, 58 insertions(+) diff --git a/bindings/java/c_kzg_4844_jni.c b/bindings/java/c_kzg_4844_jni.c index e9ba408..0b27f7f 100644 --- a/bindings/java/c_kzg_4844_jni.c +++ b/bindings/java/c_kzg_4844_jni.c @@ -125,6 +125,34 @@ JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_freeTrustedSetup(JNIEn reset_trusted_setup(); } +JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_computeKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blob, jbyteArray z_bytes) +{ + if (settings == NULL) + { + throw_exception(env, TRUSTED_SETUP_NOT_LOADED); + return NULL; + } + + Blob *blob_native = (Blob *)(*env)->GetByteArrayElements(env, blob, NULL); + Bytes32 *z_native = (Bytes32 *)(*env)->GetByteArrayElements(env, z_bytes, NULL); + + jbyteArray proof = (*env)->NewByteArray(env, BYTES_PER_PROOF); + KZGProof *proof_native = (KZGProof *)(uint8_t *)(*env)->GetByteArrayElements(env, proof, NULL); + + C_KZG_RET ret = compute_kzg_proof(proof_native, blob_native, z_native, settings); + + (*env)->ReleaseByteArrayElements(env, blob, (jbyte *)blob_native, JNI_ABORT); + (*env)->ReleaseByteArrayElements(env, proof, (jbyte *)proof_native, 0); + + if (ret != C_KZG_OK) + { + throw_c_kzg_exception(env, ret, "There was an error while computing kzg proof."); + return NULL; + } + + return proof; +} + JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_computeAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jlong count) { if (settings == NULL) diff --git a/bindings/java/c_kzg_4844_jni.h b/bindings/java/c_kzg_4844_jni.h index 91d15b0..27ed9f5 100644 --- a/bindings/java/c_kzg_4844_jni.h +++ b/bindings/java/c_kzg_4844_jni.h @@ -42,6 +42,13 @@ extern "C" */ JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_freeTrustedSetup(JNIEnv *, jclass); + /* + * Class: ethereum_ckzg4844_CKZG4844JNI + * Method: computeKzgProof + * Signature: ([B[B)[B + */ + JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_computeKzgProof(JNIEnv *, jclass, jbyteArray, jbyteArray); + /* * Class: ethereum_ckzg4844_CKZG4844JNI * Method: computeAggregateKzgProof diff --git a/bindings/java/src/main/java/ethereum/ckzg4844/CKZG4844JNI.java b/bindings/java/src/main/java/ethereum/ckzg4844/CKZG4844JNI.java index 2eb2337..4acd9de 100644 --- a/bindings/java/src/main/java/ethereum/ckzg4844/CKZG4844JNI.java +++ b/bindings/java/src/main/java/ethereum/ckzg4844/CKZG4844JNI.java @@ -113,6 +113,16 @@ public class CKZG4844JNI { */ public static native void freeTrustedSetup(); + /** + * Compute proof at point z for the polynomial represented by blob. + * + * @param blob blob bytes + * @param z_bytes a point + * @return the proof + * @throws CKZGException if there is a crypto error + */ + public static native byte[] computeKzgProof(byte[] blob, byte[] z_bytes); + /** * Calculates aggregated proof for the given blobs * diff --git a/bindings/java/src/test/java/ethereum/ckzg4844/CKZG4844JNITest.java b/bindings/java/src/test/java/ethereum/ckzg4844/CKZG4844JNITest.java index 3bfc298..f10de22 100644 --- a/bindings/java/src/test/java/ethereum/ckzg4844/CKZG4844JNITest.java +++ b/bindings/java/src/test/java/ethereum/ckzg4844/CKZG4844JNITest.java @@ -82,6 +82,15 @@ public class CKZG4844JNITest { parameters.getProof())); } + @Test + public void checkComputeKzgProof() { + loadTrustedSetup(); + final byte[] blob = TestUtils.createRandomBlob(); + final byte[] z_bytes = TestUtils.randomBLSFieldElementBytes(); + CKZG4844JNI.computeKzgProof(blob, z_bytes); + CKZG4844JNI.freeTrustedSetup(); + } + @Test public void checkCustomExceptionIsThrownAsExpected() { diff --git a/bindings/java/src/testFixtures/java/ethereum/ckzg4844/TestUtils.java b/bindings/java/src/testFixtures/java/ethereum/ckzg4844/TestUtils.java index d0dd7e5..da63cf8 100644 --- a/bindings/java/src/testFixtures/java/ethereum/ckzg4844/TestUtils.java +++ b/bindings/java/src/testFixtures/java/ethereum/ckzg4844/TestUtils.java @@ -128,6 +128,10 @@ public class TestUtils { return randomBLSFieldElement(); } + public static byte[] randomBLSFieldElementBytes() { + return randomBLSFieldElement().toArray(ByteOrder.LITTLE_ENDIAN); + } + private static InputStream readResource(final String resource) { return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); }