Use canonical random blobs in tests

This commit is contained in:
Stefan Bratanov 2022-12-10 21:05:16 +02:00
parent 03f5f1d5d0
commit 971484f85c
2 changed files with 17 additions and 7 deletions

View File

@ -193,7 +193,17 @@ JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_blobToKzgCommitm
jbyte *blob_native = (*env)->GetByteArrayElements(env, blob, NULL); jbyte *blob_native = (*env)->GetByteArrayElements(env, blob, NULL);
KZGCommitment c; KZGCommitment c;
blob_to_kzg_commitment(&c, (uint8_t *)blob_native, settings); C_KZG_RET ret;
ret = blob_to_kzg_commitment(&c, (uint8_t *)blob_native, settings);
if (ret != C_KZG_OK)
{
char arr[100];
sprintf(arr, "There was an error while converting blob bytes to a commitment: %s", C_KZG_RETURN_TYPES[ret]);
throw_exception(env, arr);
return 0;
}
jbyteArray commitment = (*env)->NewByteArray(env, BYTES_PER_COMMITMENT); jbyteArray commitment = (*env)->NewByteArray(env, BYTES_PER_COMMITMENT);
uint8_t *out = (uint8_t *)(*env)->GetByteArrayElements(env, commitment, 0); uint8_t *out = (uint8_t *)(*env)->GetByteArrayElements(env, commitment, 0);

View File

@ -2,6 +2,7 @@ package ethereum.ckzg4844;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -20,9 +21,8 @@ public class TestUtils {
public static byte[] createRandomBlob() { public static byte[] createRandomBlob() {
final byte[][] blob = IntStream.range(0, CKZG4844JNI.getFieldElementsPerBlob()) final byte[][] blob = IntStream.range(0, CKZG4844JNI.getFieldElementsPerBlob())
.mapToObj(__ -> randomBigIntegerInModulus()) .mapToObj(__ -> randomBlsFieldElement())
.map(UInt256::valueOf) .map(blsFieldElement -> blsFieldElement.toArray(ByteOrder.LITTLE_ENDIAN))
.map(UInt256::toArray)
.toArray(byte[][]::new); .toArray(byte[][]::new);
return flatten(blob); return flatten(blob);
} }
@ -47,12 +47,12 @@ public class TestUtils {
return flatten(commitments); return flatten(commitments);
} }
private static BigInteger randomBigIntegerInModulus() { private static UInt256 randomBlsFieldElement() {
final BigInteger attempt = new BigInteger(CKZG4844JNI.BLS_MODULUS.bitLength(), RANDOM); final BigInteger attempt = new BigInteger(CKZG4844JNI.BLS_MODULUS.bitLength(), RANDOM);
if (attempt.compareTo(CKZG4844JNI.BLS_MODULUS) < 0) { if (attempt.compareTo(CKZG4844JNI.BLS_MODULUS) < 0) {
return attempt; return UInt256.valueOf(attempt);
} }
return randomBigIntegerInModulus(); return randomBlsFieldElement();
} }
} }