Merge pull request #25 from StefanBratanov/canonical_blobs

[Java binding] Use canonical random blobs in tests
This commit is contained in:
Ramana Kumar 2022-12-10 19:35:35 +00:00 committed by GitHub
commit dc3fdcf49b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 10 deletions

View File

@ -8,6 +8,11 @@ repositories {
mavenCentral()
}
java {
sourceCompatibility = 11
targetCompatibility = 11
}
dependencies {
def junitVersion = "5.9.1"

View File

@ -193,7 +193,17 @@ JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_blobToKzgCommitm
jbyte *blob_native = (*env)->GetByteArrayElements(env, blob, NULL);
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);
uint8_t *out = (uint8_t *)(*env)->GetByteArrayElements(env, commitment, 0);

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -237,4 +237,4 @@ eval "set -- $(
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"
exec "$JAVACMD" "$@"

View File

@ -88,4 +88,4 @@ exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
:omega

View File

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