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)
|
|
|
|
{
|
2022-12-16 11:42:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-20 14:49:24 +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);
|
|
|
|
}
|
|
|
|
|
2022-12-06 12:58:56 +00:00
|
|
|
JNIEXPORT jint JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_getFieldElementsPerBlob(JNIEnv *env, jclass thisCls)
|
|
|
|
{
|
|
|
|
return (jint)FIELD_ELEMENTS_PER_BLOB;
|
|
|
|
}
|
|
|
|
|
2022-12-16 11:42:53 +00:00
|
|
|
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
|
|
|
{
|
2022-12-20 14:49:24 +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-12-19 16:53:04 +00:00
|
|
|
|
2022-11-23 17:17:06 +00:00
|
|
|
settings = malloc(sizeof(KZGSettings));
|
2022-12-19 16:53:04 +00:00
|
|
|
if (settings == NULL)
|
|
|
|
{
|
2022-12-20 16:34:29 +00:00
|
|
|
throw_exception(env, "Failed to allocate memory for the Trusted Setup.");
|
2022-12-19 18:22:45 +00:00
|
|
|
return;
|
2022-12-19 16:53:04 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-10 16:07:40 +00:00
|
|
|
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();
|
2022-12-16 11:42:53 +00:00
|
|
|
throw_c_kzg_exception(env, ret, "There was an error while loading the Trusted Setup.");
|
2022-11-23 17:17:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 11:42:53 +00:00
|
|
|
JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup___3BJ_3BJ(JNIEnv *env, jclass thisCls, jbyteArray g1, jlong g1Count, jbyteArray g2, jlong g2Count)
|
|
|
|
{
|
2022-12-20 14:49:24 +00:00
|
|
|
if (settings)
|
2022-12-16 11:42:53 +00:00
|
|
|
{
|
|
|
|
throw_exception(env, "Trusted Setup is already loaded. Free it before loading a new one.");
|
|
|
|
return;
|
|
|
|
}
|
2022-12-20 14:49:24 +00:00
|
|
|
|
2022-12-16 11:42:53 +00:00
|
|
|
settings = malloc(sizeof(KZGSettings));
|
2022-12-19 16:53:04 +00:00
|
|
|
if (settings == NULL)
|
|
|
|
{
|
2022-12-20 16:34:29 +00:00
|
|
|
throw_exception(env, "Failed to allocate memory for the Trusted Setup.");
|
2022-12-19 18:22:45 +00:00
|
|
|
return;
|
2022-12-19 16:53:04 +00:00
|
|
|
}
|
2022-12-16 11:42:53 +00:00
|
|
|
|
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-16 11:42:53 +00:00
|
|
|
|
2022-12-18 08:58:57 +00:00
|
|
|
(*env)->ReleaseByteArrayElements(env, g1, g1_native, JNI_ABORT);
|
|
|
|
(*env)->ReleaseByteArrayElements(env, g2, g2_native, JNI_ABORT);
|
2022-12-16 11:42:53 +00:00
|
|
|
|
|
|
|
if (ret != C_KZG_OK)
|
|
|
|
{
|
|
|
|
reset_trusted_setup();
|
|
|
|
throw_c_kzg_exception(env, ret, "There was an error while loading the Trusted Setup.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 12:58:56 +00:00
|
|
|
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 12:10:56 +00:00
|
|
|
{
|
2022-11-24 21:53:15 +00:00
|
|
|
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
|
2022-11-24 12:10:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-23 20:24:48 +00:00
|
|
|
reset_trusted_setup();
|
2022-11-23 17:17:06 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:58:56 +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 12:10:56 +00:00
|
|
|
{
|
2022-11-24 21:53:15 +00:00
|
|
|
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
|
2022-11-24 12:10:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-24 11:50:37 +00:00
|
|
|
|
2022-12-16 11:42:53 +00:00
|
|
|
size_t blobs_size = (size_t)(*env)->GetArrayLength(env, blobs);
|
2022-12-20 14:49:24 +00:00
|
|
|
size_t expected_blobs_size = BYTES_PER_BLOB * (size_t)count;
|
|
|
|
if (blobs_size != expected_blobs_size)
|
2022-12-16 11:42:53 +00:00
|
|
|
{
|
2022-12-20 14:49:24 +00:00
|
|
|
throw_invalid_size_exception(env, "Invalid blobs size.", blobs_size, expected_blobs_size);
|
2022-12-20 16:34:29 +00:00
|
|
|
return NULL;
|
2022-12-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
size_t count_native = (size_t)count;
|
2022-11-24 11:50:37 +00:00
|
|
|
jbyte *blobs_native = (*env)->GetByteArrayElements(env, blobs, NULL);
|
2023-01-16 20:05:23 +00:00
|
|
|
jbyteArray proof = (*env)->NewByteArray(env, BYTES_PER_PROOF);
|
|
|
|
KZGProof *proof_native = (KZGProof *)(uint8_t *)(*env)->GetByteArrayElements(env, proof, NULL);
|
2022-11-24 11:50:37 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
C_KZG_RET ret = compute_aggregate_kzg_proof(proof_native, (const Blob *)blobs_native, count_native, 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);
|
2023-01-16 20:05:23 +00:00
|
|
|
(*env)->ReleaseByteArrayElements(env, proof, (jbyte *)proof_native, 0);
|
2022-12-18 08:58:57 +00:00
|
|
|
|
2022-11-24 11:50:37 +00:00
|
|
|
if (ret != C_KZG_OK)
|
|
|
|
{
|
2022-12-16 11:42:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:17:06 +00:00
|
|
|
return proof;
|
|
|
|
}
|
|
|
|
|
2022-12-06 12:58:56 +00:00
|
|
|
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 12:10:56 +00:00
|
|
|
{
|
2022-11-24 21:53:15 +00:00
|
|
|
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
|
2022-11-24 12:10:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-11-24 11:50:37 +00:00
|
|
|
|
2023-01-17 17:47:46 +00:00
|
|
|
size_t count_native = (size_t)count;
|
|
|
|
|
2022-12-06 12:58:56 +00:00
|
|
|
size_t blobs_size = (size_t)(*env)->GetArrayLength(env, blobs);
|
2023-01-17 17:47:46 +00:00
|
|
|
size_t expected_blobs_size = BYTES_PER_BLOB * count_native;
|
2022-12-20 14:49:24 +00:00
|
|
|
if (blobs_size != expected_blobs_size)
|
2022-12-06 12:58:56 +00:00
|
|
|
{
|
2022-12-20 14:49:24 +00:00
|
|
|
throw_invalid_size_exception(env, "Invalid blobs size.", blobs_size, expected_blobs_size);
|
2022-12-06 12:58:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-17 17:47:46 +00:00
|
|
|
size_t commitments_size = (size_t)(*env)->GetArrayLength(env, commitments);
|
|
|
|
size_t expected_commitments_size = BYTES_PER_COMMITMENT * count_native;
|
|
|
|
if (commitments_size != expected_commitments_size)
|
|
|
|
{
|
|
|
|
throw_invalid_size_exception(env, "Invalid commitments size.", commitments_size, expected_commitments_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
KZGProof *proof_native = (KZGProof *)(*env)->GetByteArrayElements(env, proof, NULL);
|
|
|
|
KZGCommitment *commitments_native = (KZGCommitment *)(*env)->GetByteArrayElements(env, commitments, NULL);
|
2022-12-18 08:58:57 +00:00
|
|
|
jbyte *blobs_native = (*env)->GetByteArrayElements(env, blobs, NULL);
|
|
|
|
|
2022-11-24 11:50:37 +00:00
|
|
|
bool out;
|
2023-01-16 20:05:23 +00:00
|
|
|
C_KZG_RET ret = verify_aggregate_kzg_proof(&out, (const Blob *)blobs_native, commitments_native, count_native, proof_native, settings);
|
2022-11-24 11:50:37 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
(*env)->ReleaseByteArrayElements(env, proof, (jbyte *)proof_native, JNI_ABORT);
|
|
|
|
(*env)->ReleaseByteArrayElements(env, commitments, (jbyte *)commitments_native, JNI_ABORT);
|
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)
|
|
|
|
{
|
2022-12-16 11:42:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-06 12:58:56 +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 12:10:56 +00:00
|
|
|
{
|
2022-11-24 21:53:15 +00:00
|
|
|
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
|
2022-11-24 12:10:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-24 11:50:37 +00:00
|
|
|
|
2022-12-06 12:58:56 +00:00
|
|
|
size_t blob_size = (size_t)(*env)->GetArrayLength(env, blob);
|
2022-12-20 14:49:24 +00:00
|
|
|
if (blob_size != BYTES_PER_BLOB)
|
2022-12-06 12:58:56 +00:00
|
|
|
{
|
2022-12-20 14:49:24 +00:00
|
|
|
throw_invalid_size_exception(env, "Invalid blob size.", blob_size, BYTES_PER_BLOB);
|
2022-12-20 16:34:29 +00:00
|
|
|
return NULL;
|
2022-12-06 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jbyte *blob_native = (*env)->GetByteArrayElements(env, blob, NULL);
|
2023-01-16 20:05:23 +00:00
|
|
|
jbyteArray commitment = (*env)->NewByteArray(env, BYTES_PER_COMMITMENT);
|
|
|
|
KZGCommitment *commitment_native = (KZGCommitment *)(*env)->GetByteArrayElements(env, commitment, NULL);
|
2022-11-24 11:50:37 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
C_KZG_RET ret = blob_to_kzg_commitment(commitment_native, (const Blob *)blob_native, settings);
|
2022-12-10 19:05:16 +00:00
|
|
|
|
2022-12-18 08:58:57 +00:00
|
|
|
(*env)->ReleaseByteArrayElements(env, blob, blob_native, JNI_ABORT);
|
2023-01-16 20:05:23 +00:00
|
|
|
(*env)->ReleaseByteArrayElements(env, commitment, (jbyte *)commitment_native, 0);
|
2022-12-18 08:58:57 +00:00
|
|
|
|
2022-12-10 19:05:16 +00:00
|
|
|
if (ret != C_KZG_OK)
|
|
|
|
{
|
2022-12-16 11:42:53 +00:00
|
|
|
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
|
|
|
|
|
|
|
return commitment;
|
2022-11-23 17:17:06 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:58:56 +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 12:10:56 +00:00
|
|
|
{
|
2022-11-24 21:53:15 +00:00
|
|
|
throw_exception(env, TRUSTED_SETUP_NOT_LOADED);
|
2022-11-24 12:10:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-11-24 11:50:37 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
KZGCommitment *commitment_native = (KZGCommitment *)(*env)->GetByteArrayElements(env, commitment, NULL);
|
|
|
|
KZGProof *proof_native = (KZGProof *)(*env)->GetByteArrayElements(env, proof, NULL);
|
2023-01-24 18:23:42 +00:00
|
|
|
Bytes32 *z_native = (Bytes32 *)(*env)->GetByteArrayElements(env, z, NULL);
|
|
|
|
Bytes32 *y_native = (Bytes32 *)(*env)->GetByteArrayElements(env, y, NULL);
|
2022-11-24 11:50:37 +00:00
|
|
|
|
2022-12-18 08:58:57 +00:00
|
|
|
bool out;
|
2023-01-16 20:05:23 +00:00
|
|
|
C_KZG_RET ret = verify_kzg_proof(&out, commitment_native, z_native, y_native, proof_native, settings);
|
2022-12-18 08:58:57 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
(*env)->ReleaseByteArrayElements(env, commitment, (jbyte *)commitment_native, JNI_ABORT);
|
|
|
|
(*env)->ReleaseByteArrayElements(env, z, (jbyte *)z_native, JNI_ABORT);
|
|
|
|
(*env)->ReleaseByteArrayElements(env, y, (jbyte *)y_native, JNI_ABORT);
|
|
|
|
(*env)->ReleaseByteArrayElements(env, proof, (jbyte *)proof_native, JNI_ABORT);
|
2022-11-24 11:50:37 +00:00
|
|
|
|
|
|
|
if (ret != C_KZG_OK)
|
|
|
|
{
|
2022-12-16 11:42:53 +00:00
|
|
|
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
|
|
|
}
|