mirror of
https://github.com/status-im/c-kzg-4844.git
synced 2025-02-22 14:58:22 +00:00
[Java binding] Validation of loadTrustedSetup parameters (#152)
This commit is contained in:
parent
e171cb92ff
commit
2e55967455
@ -42,7 +42,8 @@ void throw_invalid_size_exception(JNIEnv *env, const char *prefix, size_t size,
|
||||
throw_c_kzg_exception(env, C_KZG_BADARGS, message);
|
||||
}
|
||||
|
||||
KZGSettings *allocate_settings(JNIEnv *env) {
|
||||
KZGSettings *allocate_settings(JNIEnv *env)
|
||||
{
|
||||
KZGSettings *s = malloc(sizeof(KZGSettings));
|
||||
if (s == NULL)
|
||||
{
|
||||
@ -105,6 +106,24 @@ JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup___3BJ
|
||||
return;
|
||||
}
|
||||
|
||||
size_t g1_bytes = (size_t)(*env)->GetArrayLength(env, g1);
|
||||
size_t g1_expected_bytes = (size_t)g1Count * 48;
|
||||
|
||||
if (g1_bytes != g1_expected_bytes)
|
||||
{
|
||||
throw_invalid_size_exception(env, "Invalid g1 size.", g1_bytes, g1_expected_bytes);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t g2_bytes = (size_t)(*env)->GetArrayLength(env, g2);
|
||||
size_t g2_expected_bytes = (size_t)g2Count * 96;
|
||||
|
||||
if (g2_bytes != g2_expected_bytes)
|
||||
{
|
||||
throw_invalid_size_exception(env, "Invalid g2 size.", g2_bytes, g2_expected_bytes);
|
||||
return;
|
||||
}
|
||||
|
||||
settings = allocate_settings(env);
|
||||
|
||||
jbyte *g1_native = (*env)->GetByteArrayElements(env, g1, NULL);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ethereum.ckzg4844;
|
||||
|
||||
import static ethereum.ckzg4844.CKZGException.CKZGError.C_KZG_BADARGS;
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@ -7,7 +8,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import ethereum.ckzg4844.CKZG4844JNI.Preset;
|
||||
import ethereum.ckzg4844.CKZGException.CKZGError;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
@ -278,7 +278,7 @@ public class CKZG4844JNITest {
|
||||
final CKZGException exception =
|
||||
assertThrows(CKZGException.class, () -> CKZG4844JNI.blobToKzgCommitment(blob));
|
||||
|
||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||
assertEquals(C_KZG_BADARGS, exception.getError());
|
||||
assertEquals("There was an error in blobToKzgCommitment.", exception.getErrorMessage());
|
||||
|
||||
CKZG4844JNI.freeTrustedSetup();
|
||||
@ -298,7 +298,7 @@ public class CKZG4844JNITest {
|
||||
assertThrows(
|
||||
CKZGException.class,
|
||||
() -> CKZG4844JNI.verifyBlobKzgProofBatch(blobs, commitments, proofs, count));
|
||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||
assertEquals(C_KZG_BADARGS, exception.getError());
|
||||
assertEquals(
|
||||
"Invalid commitments size. Expected 96 bytes but got 144.", exception.getErrorMessage());
|
||||
|
||||
@ -313,7 +313,7 @@ public class CKZG4844JNITest {
|
||||
CKZGException exception =
|
||||
assertThrows(CKZGException.class, () -> CKZG4844JNI.blobToKzgCommitment(new byte[0]));
|
||||
|
||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||
assertEquals(C_KZG_BADARGS, exception.getError());
|
||||
assertEquals(
|
||||
String.format(
|
||||
"Invalid blob size. Expected %d bytes but got 0.", CKZG4844JNI.getBytesPerBlob()),
|
||||
@ -322,7 +322,7 @@ public class CKZG4844JNITest {
|
||||
exception =
|
||||
assertThrows(CKZGException.class, () -> CKZG4844JNI.computeBlobKzgProof(new byte[123]));
|
||||
|
||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||
assertEquals(C_KZG_BADARGS, exception.getError());
|
||||
assertEquals(
|
||||
String.format(
|
||||
"Invalid blob size. Expected %d bytes but got 123.", CKZG4844JNI.getBytesPerBlob()),
|
||||
@ -338,7 +338,7 @@ public class CKZG4844JNITest {
|
||||
TestUtils.createRandomProofs(2),
|
||||
2));
|
||||
|
||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||
assertEquals(C_KZG_BADARGS, exception.getError());
|
||||
assertEquals(
|
||||
String.format(
|
||||
"Invalid blobs size. Expected %d bytes but got 42.", CKZG4844JNI.getBytesPerBlob() * 2),
|
||||
@ -386,7 +386,9 @@ public class CKZG4844JNITest {
|
||||
public void shouldThrowExceptionOnIncorrectTrustedSetupParameters() {
|
||||
final LoadTrustedSetupParameters parameters =
|
||||
TestUtils.createLoadTrustedSetupParameters(TRUSTED_SETUP_FILE_BY_PRESET.get(PRESET));
|
||||
final CKZGException ckzgException =
|
||||
|
||||
// wrong g1Count
|
||||
CKZGException exception =
|
||||
assertThrows(
|
||||
CKZGException.class,
|
||||
() ->
|
||||
@ -395,17 +397,31 @@ public class CKZG4844JNITest {
|
||||
parameters.getG1Count() + 1,
|
||||
parameters.getG2(),
|
||||
parameters.getG2Count()));
|
||||
assertTrue(ckzgException.getMessage().contains("C_KZG_BADARGS"));
|
||||
assertEquals(C_KZG_BADARGS, exception.getError());
|
||||
assertTrue(exception.getErrorMessage().contains("Invalid g1 size."));
|
||||
|
||||
// wrong g2Count
|
||||
exception =
|
||||
assertThrows(
|
||||
CKZGException.class,
|
||||
() ->
|
||||
CKZG4844JNI.loadTrustedSetup(
|
||||
parameters.getG1(),
|
||||
parameters.getG1Count(),
|
||||
parameters.getG2(),
|
||||
parameters.getG2Count() + 1));
|
||||
assertEquals(C_KZG_BADARGS, exception.getError());
|
||||
assertTrue(exception.getErrorMessage().contains("Invalid g2 size."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowExceptionOnIncorrectTrustedSetupFromFile() {
|
||||
final Preset incorrectPreset = PRESET == Preset.MAINNET ? Preset.MINIMAL : Preset.MAINNET;
|
||||
final CKZGException ckzgException =
|
||||
final CKZGException exception =
|
||||
assertThrows(
|
||||
CKZGException.class,
|
||||
() -> CKZG4844JNI.loadTrustedSetup(TRUSTED_SETUP_FILE_BY_PRESET.get(incorrectPreset)));
|
||||
assertTrue(ckzgException.getMessage().contains("C_KZG_BADARGS"));
|
||||
assertEquals(C_KZG_BADARGS, exception.getError());
|
||||
}
|
||||
|
||||
private void assertExceptionIsTrustedSetupIsNotLoaded(final RuntimeException exception) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user