diff --git a/bindings/java/c_kzg_4844_jni.c b/bindings/java/c_kzg_4844_jni.c index 51d59e3..f3c9a5b 100644 --- a/bindings/java/c_kzg_4844_jni.c +++ b/bindings/java/c_kzg_4844_jni.c @@ -6,6 +6,8 @@ static const char *C_KZG_RETURN_TYPES[] = { "C_KZG_OK", "C_KZG_BADARGS", "C_KZG_ERROR", "C_KZG_MALLOC"}; +static const char *TRUSTED_SETUP_NOT_LOADED = "Trusted Setup is not loaded."; + KZGSettings *settings; void reset_trusted_setup() @@ -20,16 +22,6 @@ void throw_exception(JNIEnv *env, const char *message) (*env)->ThrowNew(env, Exception, message); } -bool verify_trusted_setup_is_loaded(JNIEnv *env) -{ - if (settings == NULL) - { - throw_exception(env, "Trusted Setup is not loaded."); - return false; - } - return true; -} - JNIEXPORT void JNICALL Java_CKzg4844JNI_loadTrustedSetup(JNIEnv *env, jclass thisCls, jstring file) { if (settings != NULL) @@ -71,8 +63,9 @@ JNIEXPORT void JNICALL Java_CKzg4844JNI_loadTrustedSetup(JNIEnv *env, jclass thi JNIEXPORT void JNICALL Java_CKzg4844JNI_freeTrustedSetup(JNIEnv *env, jclass thisCls) { - if (!verify_trusted_setup_is_loaded(env)) + if (settings == NULL) { + throw_exception(env, TRUSTED_SETUP_NOT_LOADED); return; } free_trusted_setup(settings); @@ -81,8 +74,9 @@ JNIEXPORT void JNICALL Java_CKzg4844JNI_freeTrustedSetup(JNIEnv *env, jclass thi JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_computeAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jlong count) { - if (!verify_trusted_setup_is_loaded(env)) + if (settings == NULL) { + throw_exception(env, TRUSTED_SETUP_NOT_LOADED); return NULL; } @@ -112,8 +106,9 @@ JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_computeAggregateKzgProof(JNIEnv *e JNIEXPORT jboolean JNICALL Java_CKzg4844JNI_verifyAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jbyteArray commitments, jlong count, jbyteArray proof) { - if (!verify_trusted_setup_is_loaded(env)) + if (settings == NULL) { + throw_exception(env, TRUSTED_SETUP_NOT_LOADED); return 0; } @@ -170,8 +165,9 @@ JNIEXPORT jboolean JNICALL Java_CKzg4844JNI_verifyAggregateKzgProof(JNIEnv *env, JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_blobToKzgCommitment(JNIEnv *env, jclass thisCls, jbyteArray blob) { - if (!verify_trusted_setup_is_loaded(env)) + if (settings == NULL) { + throw_exception(env, TRUSTED_SETUP_NOT_LOADED); return NULL; } @@ -192,8 +188,9 @@ JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_blobToKzgCommitment(JNIEnv *env, j JNIEXPORT jboolean JNICALL Java_CKzg4844JNI_verifyKzgProof(JNIEnv *env, jclass thisCls, jbyteArray commitment, jbyteArray z, jbyteArray y, jbyteArray proof) { - if (!verify_trusted_setup_is_loaded(env)) + if (settings == NULL) { + throw_exception(env, TRUSTED_SETUP_NOT_LOADED); return 0; } diff --git a/bindings/java/src/test/java/CKZg4844JNITest.java b/bindings/java/src/test/java/CKZg4844JNITest.java index 9e121b9..13f9d53 100644 --- a/bindings/java/src/test/java/CKZg4844JNITest.java +++ b/bindings/java/src/test/java/CKZg4844JNITest.java @@ -67,7 +67,7 @@ public class CKZg4844JNITest { final RuntimeException exception = assertThrows(RuntimeException.class, () -> CKzg4844JNI.blobToKzgCommitment(createRandomBlob())); - assertEquals(exception.getMessage(), "Trusted Setup is not loaded."); + assertExceptionIsTrustedSetupIsNotLoaded(exception); } @@ -78,8 +78,8 @@ public class CKZg4844JNITest { final RuntimeException exception = assertThrows(RuntimeException.class, this::loadTrustedSetup); - assertEquals(exception.getMessage(), - "Trusted Setup is already loaded. Free it before loading a new one."); + assertEquals("Trusted Setup is already loaded. Free it before loading a new one.", + exception.getMessage()); CKzg4844JNI.freeTrustedSetup(); } @@ -90,10 +90,14 @@ public class CKZg4844JNITest { final RuntimeException exception = assertThrows(RuntimeException.class, CKzg4844JNI::freeTrustedSetup); - assertEquals(exception.getMessage(), "Trusted Setup is not loaded."); + assertExceptionIsTrustedSetupIsNotLoaded(exception); } + private void assertExceptionIsTrustedSetupIsNotLoaded(final RuntimeException exception) { + assertEquals("Trusted Setup is not loaded.", exception.getMessage()); + } + private void loadTrustedSetup() { CKzg4844JNI.loadTrustedSetup("../../src/trusted_setup.txt"); }