make sure to exit c method when trusted setup is not loaded

This commit is contained in:
Stefan Bratanov 2022-11-24 12:10:56 +00:00
parent 606ad9fed8
commit 679cdd20ce
3 changed files with 26 additions and 10 deletions

View File

@ -2,8 +2,8 @@
## Prerequisites
* libblst.a is available in ../../blst/ directory. Follow the instructions in the home README.md.
* JAVA_HOME environment variable is set to a jdk with `include` folder containing jni.
* Follow the instructions in the home README.md to create the libblst.a.
* JAVA_HOME environment variable is set to a jdk with an `include` folder containing jni.h file.
## Windows

View File

@ -20,12 +20,14 @@ void throw_exception(JNIEnv *env, const char *message)
(*env)->ThrowNew(env, Exception, message);
}
void verify_trusted_setup_is_loaded(JNIEnv *env)
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)
@ -71,7 +73,10 @@ JNIEXPORT void JNICALL Java_CKzg4844JNI_loadTrustedSetup(JNIEnv *env, jclass thi
JNIEXPORT void JNICALL Java_CKzg4844JNI_freeTrustedSetup(JNIEnv *env, jclass thisCls)
{
verify_trusted_setup_is_loaded(env);
if (!verify_trusted_setup_is_loaded(env))
{
return;
}
free_trusted_setup(settings);
reset_trusted_setup();
printf("Trusted Setup was freed\n");
@ -79,7 +84,10 @@ JNIEXPORT void JNICALL Java_CKzg4844JNI_freeTrustedSetup(JNIEnv *env, jclass thi
JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_computeAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jlong count)
{
verify_trusted_setup_is_loaded(env);
if (!verify_trusted_setup_is_loaded(env))
{
return NULL;
}
jbyte *blobs_native = (*env)->GetByteArrayElements(env, blobs, NULL);
@ -107,7 +115,10 @@ 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)
{
verify_trusted_setup_is_loaded(env);
if (!verify_trusted_setup_is_loaded(env))
{
return 0;
}
jbyte *blobs_native = (*env)->GetByteArrayElements(env, blobs, NULL);
uint8_t *commitments_native = (uint8_t *)(*env)->GetByteArrayElements(env, commitments, NULL);
@ -162,7 +173,10 @@ JNIEXPORT jboolean JNICALL Java_CKzg4844JNI_verifyAggregateKzgProof(JNIEnv *env,
JNIEXPORT jbyteArray JNICALL Java_CKzg4844JNI_blobToKzgCommitment(JNIEnv *env, jclass thisCls, jbyteArray blob)
{
verify_trusted_setup_is_loaded(env);
if (!verify_trusted_setup_is_loaded(env))
{
return NULL;
}
uint8_t *blob_native = (uint8_t *)(*env)->GetByteArrayElements(env, blob, NULL);
@ -181,7 +195,10 @@ 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)
{
verify_trusted_setup_is_loaded(env);
if (!verify_trusted_setup_is_loaded(env))
{
return 0;
}
uint8_t *commitment_native = (uint8_t *)(*env)->GetByteArrayElements(env, commitment, NULL);
uint8_t *z_native = (uint8_t *)(*env)->GetByteArrayElements(env, z, NULL);

View File

@ -15,7 +15,6 @@ blst:
cp libblst.a ../lib && \
cp bindings/*.h ../inc
# Copy make sure c_kzg_4844.o is built and copy it for the NodeJS and the Java bindings
# Copy make sure c_kzg_4844.o is built and copy it for the NodeJS bindings
lib: c_kzg_4844.o Makefile
cp *.o ../bindings/node.js
cp *.o ../bindings/java