small improvements

This commit is contained in:
Stefan Bratanov 2022-11-24 21:53:15 +00:00
parent f386e524c5
commit b9ff9d3309
2 changed files with 20 additions and 19 deletions

View File

@ -6,6 +6,8 @@
static const char *C_KZG_RETURN_TYPES[] = { static const char *C_KZG_RETURN_TYPES[] = {
"C_KZG_OK", "C_KZG_BADARGS", "C_KZG_ERROR", "C_KZG_MALLOC"}; "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; KZGSettings *settings;
void reset_trusted_setup() void reset_trusted_setup()
@ -20,16 +22,6 @@ void throw_exception(JNIEnv *env, const char *message)
(*env)->ThrowNew(env, Exception, 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) JNIEXPORT void JNICALL Java_CKzg4844JNI_loadTrustedSetup(JNIEnv *env, jclass thisCls, jstring file)
{ {
if (settings != NULL) 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) 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; return;
} }
free_trusted_setup(settings); 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) 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; 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) 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; 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) 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; 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) 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; return 0;
} }

View File

@ -67,7 +67,7 @@ public class CKZg4844JNITest {
final RuntimeException exception = assertThrows(RuntimeException.class, final RuntimeException exception = assertThrows(RuntimeException.class,
() -> CKzg4844JNI.blobToKzgCommitment(createRandomBlob())); () -> 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); final RuntimeException exception = assertThrows(RuntimeException.class, this::loadTrustedSetup);
assertEquals(exception.getMessage(), assertEquals("Trusted Setup is already loaded. Free it before loading a new one.",
"Trusted Setup is already loaded. Free it before loading a new one."); exception.getMessage());
CKzg4844JNI.freeTrustedSetup(); CKzg4844JNI.freeTrustedSetup();
} }
@ -90,10 +90,14 @@ public class CKZg4844JNITest {
final RuntimeException exception = assertThrows(RuntimeException.class, final RuntimeException exception = assertThrows(RuntimeException.class,
CKzg4844JNI::freeTrustedSetup); 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() { private void loadTrustedSetup() {
CKzg4844JNI.loadTrustedSetup("../../src/trusted_setup.txt"); CKzg4844JNI.loadTrustedSetup("../../src/trusted_setup.txt");
} }