c-kzg-4844/bindings/java/c_kzg_4844_jni.c

335 lines
9.6 KiB
C
Raw Normal View History

2022-11-23 20:24:48 +00:00
#include <stdio.h>
2022-11-23 18:37:46 +00:00
#include <stdlib.h>
2022-11-23 17:17:06 +00:00
#include "c_kzg_4844_jni.h"
#include "c_kzg_4844.h"
2022-11-24 21:53:15 +00:00
static const char *TRUSTED_SETUP_NOT_LOADED = "Trusted Setup is not loaded.";
2022-11-23 17:17:06 +00:00
KZGSettings *settings;
2022-11-23 20:24:48 +00:00
void reset_trusted_setup()
{
2022-12-18 09:13:26 +00:00
if (settings)
{
free_trusted_setup(settings);
free(settings);
settings = NULL;
}
2022-11-23 20:24:48 +00:00
}
void throw_exception(JNIEnv *env, const char *message)
{
jclass exception_class = (*env)->FindClass(env, "java/lang/RuntimeException");
(*env)->ThrowNew(env, exception_class, message);
}
void throw_c_kzg_exception(JNIEnv *env, C_KZG_RET error_code, const char *message)
{
jclass exception_class = (*env)->FindClass(env, "ethereum/ckzg4844/CKZGException");
jstring error_message = (*env)->NewStringUTF(env, message);
jmethodID exception_init = (*env)->GetMethodID(env, exception_class, "<init>", "(ILjava/lang/String;)V");
jobject exception = (*env)->NewObject(env, exception_class, exception_init, error_code, error_message);
(*env)->Throw(env, exception);
2022-11-23 20:24:48 +00:00
}
void throw_invalid_size_exception(JNIEnv *env, const char *prefix, size_t size, size_t expected_size)
{
char message[100];
sprintf(message, "%s Expected %zu bytes but got %zu.", prefix, expected_size, size);
throw_c_kzg_exception(env, C_KZG_BADARGS, message);
}
JNIEXPORT jint JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_getFieldElementsPerBlob(JNIEnv *env, jclass thisCls)
{
return (jint)FIELD_ELEMENTS_PER_BLOB;
}
JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup__Ljava_lang_String_2(JNIEnv *env, jclass thisCls, jstring file)
2022-11-23 17:17:06 +00:00
{
if (settings)
2022-11-24 11:50:37 +00:00
{
throw_exception(env, "Trusted Setup is already loaded. Free it before loading a new one.");
return;
}
2022-11-23 17:17:06 +00:00
settings = malloc(sizeof(KZGSettings));
if (settings == NULL)
{
throw_exception(env, "Failed to allocate memory for the trusted setup.");
2022-12-19 18:22:45 +00:00
return;
}
2022-11-23 17:17:06 +00:00
2022-11-23 18:37:46 +00:00
const char *file_native = (*env)->GetStringUTFChars(env, file, 0);
2022-11-23 17:17:06 +00:00
FILE *f = fopen(file_native, "r");
if (f == NULL)
{
2022-11-23 20:24:48 +00:00
reset_trusted_setup();
2022-11-23 18:37:46 +00:00
(*env)->ReleaseStringUTFChars(env, file, file_native);
2022-11-23 20:24:48 +00:00
throw_exception(env, "Couldn't load Trusted Setup. File might not exist or there is a permission issue.");
2022-11-23 17:17:06 +00:00
return;
}
C_KZG_RET ret = load_trusted_setup_file(settings, f);
2022-11-23 20:24:48 +00:00
2022-12-18 08:58:57 +00:00
(*env)->ReleaseStringUTFChars(env, file, file_native);
fclose(f);
2022-11-23 20:24:48 +00:00
if (ret != C_KZG_OK)
2022-11-23 17:17:06 +00:00
{
2022-11-23 20:24:48 +00:00
reset_trusted_setup();
throw_c_kzg_exception(env, ret, "There was an error while loading the Trusted Setup.");
2022-11-23 17:17:06 +00:00
return;
}
}
JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup___3BJ_3BJ(JNIEnv *env, jclass thisCls, jbyteArray g1, jlong g1Count, jbyteArray g2, jlong g2Count)
{
if (settings)
{
throw_exception(env, "Trusted Setup is already loaded. Free it before loading a new one.");
return;
}
settings = malloc(sizeof(KZGSettings));
if (settings == NULL)
{
throw_exception(env, "Failed to allocate memory for the trusted setup.");
2022-12-19 18:22:45 +00:00
return;
}
2022-12-18 08:58:57 +00:00
jbyte *g1_native = (*env)->GetByteArrayElements(env, g1, NULL);
jbyte *g2_native = (*env)->GetByteArrayElements(env, g2, NULL);
C_KZG_RET ret = load_trusted_setup(settings, (uint8_t *)g1_native, (size_t)g1Count, (uint8_t *)g2_native, (size_t)g2Count);
2022-12-18 08:58:57 +00:00
(*env)->ReleaseByteArrayElements(env, g1, g1_native, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, g2, g2_native, JNI_ABORT);
if (ret != C_KZG_OK)
{
reset_trusted_setup();
throw_c_kzg_exception(env, ret, "There was an error while loading the Trusted Setup.");
return;
}
}
JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_freeTrustedSetup(JNIEnv *env, jclass thisCls)
2022-11-23 17:17:06 +00:00
{
2022-11-24 21:53:15 +00:00
if (settings == NULL)
{
2022-11-24 21:53:15 +00:00
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
return;
}
2022-11-23 20:24:48 +00:00
reset_trusted_setup();
2022-11-23 17:17:06 +00:00
}
JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_computeAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jlong count)
2022-11-23 17:17:06 +00:00
{
2022-11-24 21:53:15 +00:00
if (settings == NULL)
{
2022-11-24 21:53:15 +00:00
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
return NULL;
}
2022-11-24 11:50:37 +00:00
size_t blobs_size = (size_t)(*env)->GetArrayLength(env, blobs);
size_t expected_blobs_size = BYTES_PER_BLOB * (size_t)count;
if (blobs_size != expected_blobs_size)
{
throw_invalid_size_exception(env, "Invalid blobs size.", blobs_size, expected_blobs_size);
return 0;
}
2022-11-24 11:50:37 +00:00
jbyte *blobs_native = (*env)->GetByteArrayElements(env, blobs, NULL);
KZGProof p;
C_KZG_RET ret = compute_aggregate_kzg_proof(&p, (uint8_t const(*)[BYTES_PER_BLOB])blobs_native, (size_t)count, settings);
2022-12-18 08:58:57 +00:00
(*env)->ReleaseByteArrayElements(env, blobs, blobs_native, JNI_ABORT);
2022-11-24 11:50:37 +00:00
if (ret != C_KZG_OK)
{
throw_c_kzg_exception(env, ret, "There was an error while computing aggregate kzg proof.");
2022-11-24 11:50:37 +00:00
return NULL;
}
jbyteArray proof = (*env)->NewByteArray(env, BYTES_PER_PROOF);
uint8_t *out = (uint8_t *)(*env)->GetByteArrayElements(env, proof, 0);
bytes_from_g1(out, &p);
(*env)->ReleaseByteArrayElements(env, proof, (jbyte *)out, 0);
2022-11-23 17:17:06 +00:00
return proof;
}
JNIEXPORT jboolean JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_verifyAggregateKzgProof(JNIEnv *env, jclass thisCls, jbyteArray blobs, jbyteArray commitments, jlong count, jbyteArray proof)
2022-11-23 17:17:06 +00:00
{
2022-11-24 21:53:15 +00:00
if (settings == NULL)
{
2022-11-24 21:53:15 +00:00
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
return 0;
}
2022-11-24 11:50:37 +00:00
size_t blobs_size = (size_t)(*env)->GetArrayLength(env, blobs);
size_t expected_blobs_size = BYTES_PER_BLOB * (size_t)count;
if (blobs_size != expected_blobs_size)
{
throw_invalid_size_exception(env, "Invalid blobs size.", blobs_size, expected_blobs_size);
return 0;
}
size_t count_native = (size_t)count;
2022-11-24 11:50:37 +00:00
2022-12-18 08:58:57 +00:00
jbyte *proof_native = (*env)->GetByteArrayElements(env, proof, NULL);
2022-11-24 11:50:37 +00:00
KZGProof f;
C_KZG_RET ret;
2022-12-18 08:58:57 +00:00
ret = bytes_to_g1(&f, (uint8_t *)proof_native);
(*env)->ReleaseByteArrayElements(env, proof, proof_native, JNI_ABORT);
2022-11-24 11:50:37 +00:00
if (ret != C_KZG_OK)
{
throw_c_kzg_exception(env, ret, "There was an error while converting proof to g1.");
2022-11-24 11:50:37 +00:00
return 0;
}
KZGCommitment *c = calloc(count_native, sizeof(KZGCommitment));
2022-11-24 11:50:37 +00:00
2022-12-18 08:58:57 +00:00
uint8_t *commitments_native = (uint8_t *)(*env)->GetByteArrayElements(env, commitments, NULL);
for (size_t i = 0; i < count_native; i++)
2022-11-24 11:50:37 +00:00
{
ret = bytes_to_g1(&c[i], &commitments_native[i * BYTES_PER_COMMITMENT]);
if (ret != C_KZG_OK)
{
2022-12-18 08:58:57 +00:00
(*env)->ReleaseByteArrayElements(env, commitments, (jbyte *)commitments_native, JNI_ABORT);
2022-11-24 11:50:37 +00:00
free(c);
2022-11-24 20:03:30 +00:00
char arr[100];
sprintf(arr, "There was an error while converting commitment (%zu/%zu) to g1.", i + 1, count_native);
throw_c_kzg_exception(env, ret, arr);
2022-11-24 11:50:37 +00:00
return 0;
}
}
2022-12-18 08:58:57 +00:00
(*env)->ReleaseByteArrayElements(env, commitments, (jbyte *)commitments_native, JNI_ABORT);
jbyte *blobs_native = (*env)->GetByteArrayElements(env, blobs, NULL);
2022-11-24 11:50:37 +00:00
bool out;
ret = verify_aggregate_kzg_proof(&out, (uint8_t const(*)[BYTES_PER_BLOB])blobs_native, c, count_native, &f, settings);
2022-11-24 11:50:37 +00:00
2022-12-18 08:58:57 +00:00
(*env)->ReleaseByteArrayElements(env, blobs, blobs_native, JNI_ABORT);
free(c);
2022-11-24 11:50:37 +00:00
if (ret != C_KZG_OK)
{
throw_c_kzg_exception(env, ret, "There was an error while verifying aggregate kzg proof.");
2022-11-24 11:50:37 +00:00
return 0;
}
return (jboolean)out;
2022-11-23 17:17:06 +00:00
}
JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_blobToKzgCommitment(JNIEnv *env, jclass thisCls, jbyteArray blob)
2022-11-23 17:17:06 +00:00
{
2022-11-24 21:53:15 +00:00
if (settings == NULL)
{
2022-11-24 21:53:15 +00:00
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
return NULL;
}
2022-11-24 11:50:37 +00:00
size_t blob_size = (size_t)(*env)->GetArrayLength(env, blob);
if (blob_size != BYTES_PER_BLOB)
{
throw_invalid_size_exception(env, "Invalid blob size.", blob_size, BYTES_PER_BLOB);
return 0;
}
jbyte *blob_native = (*env)->GetByteArrayElements(env, blob, NULL);
2022-11-24 11:50:37 +00:00
KZGCommitment c;
2022-12-10 19:05:16 +00:00
C_KZG_RET ret;
ret = blob_to_kzg_commitment(&c, (uint8_t *)blob_native, settings);
2022-12-18 08:58:57 +00:00
(*env)->ReleaseByteArrayElements(env, blob, blob_native, JNI_ABORT);
2022-12-10 19:05:16 +00:00
if (ret != C_KZG_OK)
{
throw_c_kzg_exception(env, ret, "There was an error while converting blob to commitment.");
return NULL;
2022-12-10 19:05:16 +00:00
}
2022-11-24 11:50:37 +00:00
jbyteArray commitment = (*env)->NewByteArray(env, BYTES_PER_COMMITMENT);
uint8_t *out = (uint8_t *)(*env)->GetByteArrayElements(env, commitment, 0);
bytes_from_g1(out, &c);
(*env)->ReleaseByteArrayElements(env, commitment, (jbyte *)out, 0);
return commitment;
2022-11-23 17:17:06 +00:00
}
JNIEXPORT jboolean JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_verifyKzgProof(JNIEnv *env, jclass thisCls, jbyteArray commitment, jbyteArray z, jbyteArray y, jbyteArray proof)
2022-11-23 17:17:06 +00:00
{
2022-11-24 21:53:15 +00:00
if (settings == NULL)
{
2022-11-24 21:53:15 +00:00
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
return 0;
}
2022-11-24 11:50:37 +00:00
2022-12-18 08:58:57 +00:00
C_KZG_RET ret;
2022-11-24 11:50:37 +00:00
KZGCommitment c;
2022-12-18 08:58:57 +00:00
jbyte *commitment_native = (*env)->GetByteArrayElements(env, commitment, NULL);
2022-11-24 11:50:37 +00:00
2022-12-18 08:58:57 +00:00
ret = bytes_to_g1(&c, (uint8_t *)commitment_native);
(*env)->ReleaseByteArrayElements(env, commitment, commitment_native, JNI_ABORT);
2022-11-24 11:50:37 +00:00
if (ret != C_KZG_OK)
{
throw_c_kzg_exception(env, ret, "There was an error while converting commitment to g1.");
2022-11-24 11:50:37 +00:00
return 0;
}
2022-12-18 08:58:57 +00:00
KZGProof p;
jbyte *proof_native = (*env)->GetByteArrayElements(env, proof, NULL);
ret = bytes_to_g1(&p, (uint8_t *)proof_native);
(*env)->ReleaseByteArrayElements(env, proof, proof_native, JNI_ABORT);
2022-11-24 11:50:37 +00:00
if (ret != C_KZG_OK)
{
throw_c_kzg_exception(env, ret, "There was an error while converting proof to g1.");
2022-11-24 11:50:37 +00:00
return 0;
}
2022-12-18 08:58:57 +00:00
bool out;
jbyte *z_native = (*env)->GetByteArrayElements(env, z, NULL);
jbyte *y_native = (*env)->GetByteArrayElements(env, y, NULL);
ret = verify_kzg_proof(&out, &c, (uint8_t *)z_native, (uint8_t *)y_native, &p, settings);
(*env)->ReleaseByteArrayElements(env, z, z_native, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, y, y_native, JNI_ABORT);
2022-11-24 11:50:37 +00:00
if (ret != C_KZG_OK)
{
throw_c_kzg_exception(env, ret, "There was an error while verifying kzg proof.");
2022-11-24 11:50:37 +00:00
return 0;
}
return (jboolean)out;
2022-11-23 17:17:06 +00:00
}