mirror of
https://github.com/status-im/c-kzg-4844.git
synced 2025-02-17 04:27:08 +00:00
Add spotless to the java bindings (#136)
* Add spotless to the java bindings * Use check task which includes test + all verification
This commit is contained in:
parent
8c3dc2df41
commit
9a764de619
@ -65,7 +65,7 @@ build:
|
|||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test:
|
test:
|
||||||
${GRADLE_COMMAND} clean test
|
${GRADLE_COMMAND} clean check
|
||||||
|
|
||||||
.PHONY: benchmark
|
.PHONY: benchmark
|
||||||
benchmark:
|
benchmark:
|
||||||
|
@ -2,6 +2,7 @@ plugins {
|
|||||||
id "application"
|
id "application"
|
||||||
id "java-test-fixtures"
|
id "java-test-fixtures"
|
||||||
id "me.champeau.jmh" version "0.6.8"
|
id "me.champeau.jmh" version "0.6.8"
|
||||||
|
id "com.diffplug.spotless" version "6.15.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
@ -15,16 +16,22 @@ java {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|
||||||
def junitVersion = "5.9.1"
|
def junitVersion = "5.9.2"
|
||||||
|
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter:${junitVersion}")
|
testImplementation("org.junit.jupiter:junit-jupiter:${junitVersion}")
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter-params:${junitVersion}")
|
testImplementation("org.junit.jupiter:junit-jupiter-params:${junitVersion}")
|
||||||
|
|
||||||
testFixturesImplementation("org.apache.tuweni:tuweni-units:2.3.1")
|
testFixturesImplementation("org.apache.tuweni:tuweni-units:2.3.1")
|
||||||
testFixturesImplementation("com.fasterxml.jackson.core:jackson-databind:2.14.1")
|
testFixturesImplementation("com.fasterxml.jackson.core:jackson-databind:2.14.2")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
spotless {
|
||||||
|
java {
|
||||||
|
googleJavaFormat()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
}
|
}
|
@ -67,10 +67,12 @@ public class CKZG4844JNIBenchmark {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
final byte[][] blobs = new byte[count][];
|
final byte[][] blobs = new byte[count][];
|
||||||
final byte[][] commitments = new byte[count][];
|
final byte[][] commitments = new byte[count][];
|
||||||
IntStream.range(0, count).forEach(i -> {
|
IntStream.range(0, count)
|
||||||
blobs[i] = TestUtils.createRandomBlob();
|
.forEach(
|
||||||
commitments[i] = CKZG4844JNI.blobToKzgCommitment(blobs[i]);
|
i -> {
|
||||||
});
|
blobs[i] = TestUtils.createRandomBlob();
|
||||||
|
commitments[i] = CKZG4844JNI.blobToKzgCommitment(blobs[i]);
|
||||||
|
});
|
||||||
this.blobs = TestUtils.flatten(blobs);
|
this.blobs = TestUtils.flatten(blobs);
|
||||||
this.commitments = TestUtils.flatten(commitments);
|
this.commitments = TestUtils.flatten(commitments);
|
||||||
proof = CKZG4844JNI.computeAggregateKzgProof(TestUtils.flatten(blobs), count);
|
proof = CKZG4844JNI.computeAggregateKzgProof(TestUtils.flatten(blobs), count);
|
||||||
@ -122,8 +124,8 @@ public class CKZG4844JNIBenchmark {
|
|||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public boolean verifyAggregateKzgProof(final ComputeAndVerifyState state) {
|
public boolean verifyAggregateKzgProof(final ComputeAndVerifyState state) {
|
||||||
return CKZG4844JNI.verifyAggregateKzgProof(state.blobs, state.commitments, state.count,
|
return CKZG4844JNI.verifyAggregateKzgProof(
|
||||||
state.proof);
|
state.blobs, state.commitments, state.count, state.proof);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@ -131,5 +133,4 @@ public class CKZG4844JNIBenchmark {
|
|||||||
public boolean verifyKzgProof(final VerifyKzgProofState state) {
|
public boolean verifyKzgProof(final VerifyKzgProofState state) {
|
||||||
return CKZG4844JNI.verifyKzgProof(state.commitment, state.z, state.y, state.proof);
|
return CKZG4844JNI.verifyKzgProof(state.commitment, state.z, state.y, state.proof);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
@ -20,16 +20,21 @@ public class CKZG4844JNI {
|
|||||||
*/
|
*/
|
||||||
public static void loadNativeLibrary(Preset preset) {
|
public static void loadNativeLibrary(Preset preset) {
|
||||||
String libraryResourcePath =
|
String libraryResourcePath =
|
||||||
"lib/" + System.getProperty("os.arch") + "/" + preset.name().toLowerCase() + "/"
|
"lib/"
|
||||||
|
+ System.getProperty("os.arch")
|
||||||
|
+ "/"
|
||||||
|
+ preset.name().toLowerCase()
|
||||||
|
+ "/"
|
||||||
+ PLATFORM_NATIVE_LIBRARY_NAME;
|
+ PLATFORM_NATIVE_LIBRARY_NAME;
|
||||||
InputStream libraryResource = CKZG4844JNI.class.getResourceAsStream(libraryResourcePath);
|
InputStream libraryResource = CKZG4844JNI.class.getResourceAsStream(libraryResourcePath);
|
||||||
if (libraryResource == null) {
|
if (libraryResource == null) {
|
||||||
try {
|
try {
|
||||||
System.loadLibrary(LIBRARY_NAME);
|
System.loadLibrary(LIBRARY_NAME);
|
||||||
} catch (UnsatisfiedLinkError __) {
|
} catch (UnsatisfiedLinkError __) {
|
||||||
String exceptionMessage = String.format(
|
String exceptionMessage =
|
||||||
"Couldn't load native library (%s). It wasn't available at %s or the library path.",
|
String.format(
|
||||||
LIBRARY_NAME, libraryResourcePath);
|
"Couldn't load native library (%s). It wasn't available at %s or the library path.",
|
||||||
|
LIBRARY_NAME, libraryResourcePath);
|
||||||
throw new RuntimeException(exceptionMessage);
|
throw new RuntimeException(exceptionMessage);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -48,7 +53,8 @@ public class CKZG4844JNI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum Preset {
|
public enum Preset {
|
||||||
MAINNET(4096), MINIMAL(4);
|
MAINNET(4096),
|
||||||
|
MINIMAL(4);
|
||||||
|
|
||||||
public final int fieldElementsPerBlob;
|
public final int fieldElementsPerBlob;
|
||||||
|
|
||||||
@ -57,14 +63,14 @@ public class CKZG4844JNI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final BigInteger BLS_MODULUS = new BigInteger(
|
public static final BigInteger BLS_MODULUS =
|
||||||
"52435875175126190479447740508185965837690552500527637822603658699938581184513");
|
new BigInteger(
|
||||||
|
"52435875175126190479447740508185965837690552500527637822603658699938581184513");
|
||||||
public static final int BYTES_PER_COMMITMENT = 48;
|
public static final int BYTES_PER_COMMITMENT = 48;
|
||||||
public static final int BYTES_PER_PROOF = 48;
|
public static final int BYTES_PER_PROOF = 48;
|
||||||
public static final int BYTES_PER_FIELD_ELEMENT = 32;
|
public static final int BYTES_PER_FIELD_ELEMENT = 32;
|
||||||
|
|
||||||
private CKZG4844JNI() {
|
private CKZG4844JNI() {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the bytes per blob based on the output from {@link #getFieldElementsPerBlob()}
|
* Calculates the bytes per blob based on the output from {@link #getFieldElementsPerBlob()}
|
||||||
@ -85,9 +91,9 @@ public class CKZG4844JNI {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the trusted setup from a file. Once loaded, the same setup will be used for all the
|
* Loads the trusted setup from a file. Once loaded, the same setup will be used for all the
|
||||||
* crypto native calls. To load a new setup, free the current one by calling
|
* crypto native calls. To load a new setup, free the current one by calling {@link
|
||||||
* {@link #freeTrustedSetup()} and then load the new one. If no trusted setup has been loaded, all
|
* #freeTrustedSetup()} and then load the new one. If no trusted setup has been loaded, all the
|
||||||
* the crypto native calls will throw a {@link RuntimeException}.
|
* crypto native calls will throw a {@link RuntimeException}.
|
||||||
*
|
*
|
||||||
* @param file a path to a trusted setup file
|
* @param file a path to a trusted setup file
|
||||||
* @throws CKZGException if there is a crypto error
|
* @throws CKZGException if there is a crypto error
|
||||||
@ -98,9 +104,9 @@ public class CKZG4844JNI {
|
|||||||
* An alternative to {@link #loadTrustedSetup(String)}. Loads the trusted setup from method
|
* An alternative to {@link #loadTrustedSetup(String)}. Loads the trusted setup from method
|
||||||
* parameters instead of a file.
|
* parameters instead of a file.
|
||||||
*
|
*
|
||||||
* @param g1 g1 values as bytes
|
* @param g1 g1 values as bytes
|
||||||
* @param g1Count the count of the g1 values
|
* @param g1Count the count of the g1 values
|
||||||
* @param g2 g2 values as bytes
|
* @param g2 g2 values as bytes
|
||||||
* @param g2Count the count of the g2 values
|
* @param g2Count the count of the g2 values
|
||||||
* @throws CKZGException if there is a crypto error
|
* @throws CKZGException if there is a crypto error
|
||||||
*/
|
*/
|
||||||
@ -110,11 +116,11 @@ public class CKZG4844JNI {
|
|||||||
* An alternative to {@link #loadTrustedSetup(String)}. Loads the trusted setup from a resource.
|
* An alternative to {@link #loadTrustedSetup(String)}. Loads the trusted setup from a resource.
|
||||||
*
|
*
|
||||||
* @param resource the resource name that contains the trusted setup
|
* @param resource the resource name that contains the trusted setup
|
||||||
* @param clazz the class to use to get the resource
|
* @param clazz the class to use to get the resource
|
||||||
* @throws CKZGException if there is a crypto error
|
* @throws CKZGException if there is a crypto error
|
||||||
* @throws IllegalArgumentException if the resource does not exist
|
* @throws IllegalArgumentException if the resource does not exist
|
||||||
*/
|
*/
|
||||||
public static void loadTrustedSetupFromResource(String resource, Class clazz) {
|
public static <T> void loadTrustedSetupFromResource(String resource, Class<T> clazz) {
|
||||||
InputStream is = clazz.getResourceAsStream(resource);
|
InputStream is = clazz.getResourceAsStream(resource);
|
||||||
if (is == null) {
|
if (is == null) {
|
||||||
throw new IllegalArgumentException("Resource " + resource + " does not exist.");
|
throw new IllegalArgumentException("Resource " + resource + " does not exist.");
|
||||||
@ -139,7 +145,7 @@ public class CKZG4844JNI {
|
|||||||
/**
|
/**
|
||||||
* Compute proof at point z for the polynomial represented by blob.
|
* Compute proof at point z for the polynomial represented by blob.
|
||||||
*
|
*
|
||||||
* @param blob blob bytes
|
* @param blob blob bytes
|
||||||
* @param z_bytes a point
|
* @param z_bytes a point
|
||||||
* @return the proof
|
* @return the proof
|
||||||
* @throws CKZGException if there is a crypto error
|
* @throws CKZGException if there is a crypto error
|
||||||
@ -159,16 +165,15 @@ public class CKZG4844JNI {
|
|||||||
/**
|
/**
|
||||||
* Verify aggregated proof and commitments for the given blobs
|
* Verify aggregated proof and commitments for the given blobs
|
||||||
*
|
*
|
||||||
* @param blobs blobs as flattened bytes
|
* @param blobs blobs as flattened bytes
|
||||||
* @param commitments_bytes commitments as flattened bytes
|
* @param commitments_bytes commitments as flattened bytes
|
||||||
* @param count the count of the blobs (should be same as the count of the
|
* @param count the count of the blobs (should be same as the count of the commitments)
|
||||||
* commitments)
|
* @param aggregated_proof_bytes the proof that needs verifying
|
||||||
* @param proof_bytes the proof that needs verifying
|
|
||||||
* @return true if the proof is valid and false otherwise
|
* @return true if the proof is valid and false otherwise
|
||||||
* @throws CKZGException if there is a crypto error
|
* @throws CKZGException if there is a crypto error
|
||||||
*/
|
*/
|
||||||
public static native boolean verifyAggregateKzgProof(byte[] blobs, byte[] commitments_bytes,
|
public static native boolean verifyAggregateKzgProof(
|
||||||
long count, byte[] aggregated_proof_bytes);
|
byte[] blobs, byte[] commitments_bytes, long count, byte[] aggregated_proof_bytes);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates commitment for a given blob
|
* Calculates commitment for a given blob
|
||||||
@ -183,12 +188,12 @@ public class CKZG4844JNI {
|
|||||||
* Verify the proof by point evaluation for the given commitment
|
* Verify the proof by point evaluation for the given commitment
|
||||||
*
|
*
|
||||||
* @param commitment_bytes commitment bytes
|
* @param commitment_bytes commitment bytes
|
||||||
* @param z_bytes Z
|
* @param z_bytes Z
|
||||||
* @param y_bytes Y
|
* @param y_bytes Y
|
||||||
* @param proof_bytes the proof that needs verifying
|
* @param proof_bytes the proof that needs verifying
|
||||||
* @return true if the proof is valid and false otherwise
|
* @return true if the proof is valid and false otherwise
|
||||||
* @throws CKZGException if there is a crypto error
|
* @throws CKZGException if there is a crypto error
|
||||||
*/
|
*/
|
||||||
public static native boolean verifyKzgProof(byte[] commitment_bytes, byte[] z_bytes,
|
public static native boolean verifyKzgProof(
|
||||||
byte[] y_bytes, byte[] proof_bytes);
|
byte[] commitment_bytes, byte[] z_bytes, byte[] y_bytes, byte[] proof_bytes);
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,7 @@ import static ethereum.ckzg4844.CKZGException.CKZGError.fromErrorCode;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/** Thrown when there is an error in the underlying c-kzg library. */
|
||||||
* Thrown when there is an error in the underlying c-kzg library.
|
|
||||||
*/
|
|
||||||
public class CKZGException extends RuntimeException {
|
public class CKZGException extends RuntimeException {
|
||||||
|
|
||||||
private final CKZGError error;
|
private final CKZGError error;
|
||||||
@ -27,8 +25,10 @@ public class CKZGException extends RuntimeException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum CKZGError {
|
public enum CKZGError {
|
||||||
|
UNKNOWN(0),
|
||||||
UNKNOWN(0), C_KZG_BADARGS(1), C_KZG_ERROR(2), C_KZG_MALLOC(3);
|
C_KZG_BADARGS(1),
|
||||||
|
C_KZG_ERROR(2),
|
||||||
|
C_KZG_MALLOC(3);
|
||||||
|
|
||||||
public final int errorCode;
|
public final int errorCode;
|
||||||
|
|
||||||
@ -37,9 +37,10 @@ public class CKZGException extends RuntimeException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static CKZGError fromErrorCode(int errorCode) {
|
public static CKZGError fromErrorCode(int errorCode) {
|
||||||
return Arrays.stream(CKZGError.values()).filter(error -> error.errorCode == errorCode)
|
return Arrays.stream(CKZGError.values())
|
||||||
.findFirst().orElse(UNKNOWN);
|
.filter(error -> error.errorCode == errorCode)
|
||||||
|
.findFirst()
|
||||||
|
.orElse(UNKNOWN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,22 +25,34 @@ public class CKZG4844JNITest {
|
|||||||
|
|
||||||
private static final Preset PRESET;
|
private static final Preset PRESET;
|
||||||
|
|
||||||
private static final Map<Preset, String> TRUSTED_SETUP_FILE_BY_PRESET = Map.of(Preset.MAINNET,
|
private static final Map<Preset, String> TRUSTED_SETUP_FILE_BY_PRESET =
|
||||||
"../../src/trusted_setup.txt", Preset.MINIMAL, "../../src/trusted_setup_4.txt");
|
Map.of(
|
||||||
|
Preset.MAINNET,
|
||||||
|
"../../src/trusted_setup.txt",
|
||||||
|
Preset.MINIMAL,
|
||||||
|
"../../src/trusted_setup_4.txt");
|
||||||
|
|
||||||
private static final Map<Preset, String> TRUSTED_SETUP_RESOURCE_BY_PRESET = Map.of(Preset.MAINNET,
|
private static final Map<Preset, String> TRUSTED_SETUP_RESOURCE_BY_PRESET =
|
||||||
"/test-vectors/trusted_setup.txt", Preset.MINIMAL, "/test-vectors/trusted_setup_4.txt");
|
Map.of(
|
||||||
|
Preset.MAINNET,
|
||||||
|
"/test-vectors/trusted_setup.txt",
|
||||||
|
Preset.MINIMAL,
|
||||||
|
"/test-vectors/trusted_setup_4.txt");
|
||||||
|
|
||||||
static {
|
static {
|
||||||
PRESET = Optional.ofNullable(System.getenv("PRESET")).map(String::toUpperCase)
|
PRESET =
|
||||||
.map(Preset::valueOf).orElse(Preset.MAINNET);
|
Optional.ofNullable(System.getenv("PRESET"))
|
||||||
|
.map(String::toUpperCase)
|
||||||
|
.map(Preset::valueOf)
|
||||||
|
.orElse(Preset.MAINNET);
|
||||||
CKZG4844JNI.loadNativeLibrary(PRESET);
|
CKZG4844JNI.loadNativeLibrary(PRESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getsTheConfiguredFieldElementsPerBlob() {
|
public void getsTheConfiguredFieldElementsPerBlob() {
|
||||||
assertEquals(PRESET.fieldElementsPerBlob, CKZG4844JNI.getFieldElementsPerBlob());
|
assertEquals(PRESET.fieldElementsPerBlob, CKZG4844JNI.getFieldElementsPerBlob());
|
||||||
assertEquals(PRESET.fieldElementsPerBlob * CKZG4844JNI.BYTES_PER_FIELD_ELEMENT,
|
assertEquals(
|
||||||
|
PRESET.fieldElementsPerBlob * CKZG4844JNI.BYTES_PER_FIELD_ELEMENT,
|
||||||
CKZG4844JNI.getBytesPerBlob());
|
CKZG4844JNI.getBytesPerBlob());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,10 +66,12 @@ public class CKZG4844JNITest {
|
|||||||
|
|
||||||
final byte[][] blobsArray = new byte[count][];
|
final byte[][] blobsArray = new byte[count][];
|
||||||
final byte[][] commitmentsArray = new byte[count][];
|
final byte[][] commitmentsArray = new byte[count][];
|
||||||
IntStream.range(0, count).forEach(i -> {
|
IntStream.range(0, count)
|
||||||
blobsArray[i] = TestUtils.createRandomBlob();
|
.forEach(
|
||||||
commitmentsArray[i] = CKZG4844JNI.blobToKzgCommitment(blobsArray[i]);
|
i -> {
|
||||||
});
|
blobsArray[i] = TestUtils.createRandomBlob();
|
||||||
|
commitmentsArray[i] = CKZG4844JNI.blobToKzgCommitment(blobsArray[i]);
|
||||||
|
});
|
||||||
final byte[] blobs = TestUtils.flatten(blobsArray);
|
final byte[] blobs = TestUtils.flatten(blobsArray);
|
||||||
final byte[] commitments = TestUtils.flatten(commitmentsArray);
|
final byte[] commitments = TestUtils.flatten(commitmentsArray);
|
||||||
|
|
||||||
@ -79,14 +93,16 @@ public class CKZG4844JNITest {
|
|||||||
assertFalse(CKZG4844JNI.verifyAggregateKzgProof(blobs, fakeCommitments, count, proof));
|
assertFalse(CKZG4844JNI.verifyAggregateKzgProof(blobs, fakeCommitments, count, proof));
|
||||||
|
|
||||||
CKZG4844JNI.freeTrustedSetup();
|
CKZG4844JNI.freeTrustedSetup();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest(name = "{index}")
|
@ParameterizedTest(name = "{index}")
|
||||||
@MethodSource("getVerifyKzgProofTestVectors")
|
@MethodSource("getVerifyKzgProofTestVectors")
|
||||||
public void testVerifyKzgProof(final VerifyKzgProofParameters parameters) {
|
public void testVerifyKzgProof(final VerifyKzgProofParameters parameters) {
|
||||||
assertTrue(
|
assertTrue(
|
||||||
CKZG4844JNI.verifyKzgProof(parameters.getCommitment(), parameters.getZ(), parameters.getY(),
|
CKZG4844JNI.verifyKzgProof(
|
||||||
|
parameters.getCommitment(),
|
||||||
|
parameters.getZ(),
|
||||||
|
parameters.getY(),
|
||||||
parameters.getProof()));
|
parameters.getProof()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +119,6 @@ public class CKZG4844JNITest {
|
|||||||
assertEquals(CKZG4844JNI.BYTES_PER_PROOF, proof.length);
|
assertEquals(CKZG4844JNI.BYTES_PER_PROOF, proof.length);
|
||||||
|
|
||||||
CKZG4844JNI.freeTrustedSetup();
|
CKZG4844JNI.freeTrustedSetup();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -113,12 +128,12 @@ public class CKZG4844JNITest {
|
|||||||
|
|
||||||
final byte[] blob = TestUtils.createNonCanonicalBlob();
|
final byte[] blob = TestUtils.createNonCanonicalBlob();
|
||||||
|
|
||||||
final CKZGException exception = assertThrows(CKZGException.class,
|
final CKZGException exception =
|
||||||
() -> CKZG4844JNI.blobToKzgCommitment(blob));
|
assertThrows(CKZGException.class, () -> CKZG4844JNI.blobToKzgCommitment(blob));
|
||||||
|
|
||||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||||
assertEquals("There was an error while converting blob to commitment.",
|
assertEquals(
|
||||||
exception.getErrorMessage());
|
"There was an error while converting blob to commitment.", exception.getErrorMessage());
|
||||||
|
|
||||||
CKZG4844JNI.freeTrustedSetup();
|
CKZG4844JNI.freeTrustedSetup();
|
||||||
}
|
}
|
||||||
@ -134,12 +149,14 @@ public class CKZG4844JNITest {
|
|||||||
// different length for commitments
|
// different length for commitments
|
||||||
final byte[] commitments = TestUtils.createRandomCommitments(3);
|
final byte[] commitments = TestUtils.createRandomCommitments(3);
|
||||||
|
|
||||||
final CKZGException exception = assertThrows(CKZGException.class,
|
final CKZGException exception =
|
||||||
() -> CKZG4844JNI.verifyAggregateKzgProof(blobs, commitments, count, proof));
|
assertThrows(
|
||||||
|
CKZGException.class,
|
||||||
|
() -> CKZG4844JNI.verifyAggregateKzgProof(blobs, commitments, count, proof));
|
||||||
|
|
||||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||||
assertEquals("Invalid commitments size. Expected 96 bytes but got 144.",
|
assertEquals(
|
||||||
exception.getErrorMessage());
|
"Invalid commitments size. Expected 96 bytes but got 144.", exception.getErrorMessage());
|
||||||
|
|
||||||
CKZG4844JNI.freeTrustedSetup();
|
CKZG4844JNI.freeTrustedSetup();
|
||||||
}
|
}
|
||||||
@ -149,29 +166,39 @@ public class CKZG4844JNITest {
|
|||||||
|
|
||||||
loadTrustedSetup();
|
loadTrustedSetup();
|
||||||
|
|
||||||
CKZGException exception = assertThrows(CKZGException.class,
|
CKZGException exception =
|
||||||
() -> CKZG4844JNI.blobToKzgCommitment(new byte[0]));
|
assertThrows(CKZGException.class, () -> CKZG4844JNI.blobToKzgCommitment(new byte[0]));
|
||||||
|
|
||||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||||
assertEquals(String.format("Invalid blob size. Expected %d bytes but got 0.",
|
assertEquals(
|
||||||
CKZG4844JNI.getBytesPerBlob()),
|
String.format(
|
||||||
|
"Invalid blob size. Expected %d bytes but got 0.", CKZG4844JNI.getBytesPerBlob()),
|
||||||
exception.getErrorMessage());
|
exception.getErrorMessage());
|
||||||
|
|
||||||
exception = assertThrows(CKZGException.class,
|
exception =
|
||||||
() -> CKZG4844JNI.computeAggregateKzgProof(new byte[123], 1));
|
assertThrows(
|
||||||
|
CKZGException.class, () -> CKZG4844JNI.computeAggregateKzgProof(new byte[123], 1));
|
||||||
|
|
||||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||||
assertEquals(String.format("Invalid blobs size. Expected %d bytes but got 123.",
|
assertEquals(
|
||||||
CKZG4844JNI.getBytesPerBlob()),
|
String.format(
|
||||||
|
"Invalid blobs size. Expected %d bytes but got 123.", CKZG4844JNI.getBytesPerBlob()),
|
||||||
exception.getErrorMessage());
|
exception.getErrorMessage());
|
||||||
|
|
||||||
exception = assertThrows(CKZGException.class,
|
exception =
|
||||||
() -> CKZG4844JNI.verifyAggregateKzgProof(new byte[42], TestUtils.createRandomCommitment(),
|
assertThrows(
|
||||||
2, TestUtils.createRandomProof(2)));
|
CKZGException.class,
|
||||||
|
() ->
|
||||||
|
CKZG4844JNI.verifyAggregateKzgProof(
|
||||||
|
new byte[42],
|
||||||
|
TestUtils.createRandomCommitment(),
|
||||||
|
2,
|
||||||
|
TestUtils.createRandomProof(2)));
|
||||||
|
|
||||||
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
assertEquals(CKZGError.C_KZG_BADARGS, exception.getError());
|
||||||
assertEquals(String.format("Invalid blobs size. Expected %d bytes but got 42.",
|
assertEquals(
|
||||||
CKZG4844JNI.getBytesPerBlob() * 2),
|
String.format(
|
||||||
|
"Invalid blobs size. Expected %d bytes but got 42.", CKZG4844JNI.getBytesPerBlob() * 2),
|
||||||
exception.getErrorMessage());
|
exception.getErrorMessage());
|
||||||
|
|
||||||
CKZG4844JNI.freeTrustedSetup();
|
CKZG4844JNI.freeTrustedSetup();
|
||||||
@ -180,11 +207,12 @@ public class CKZG4844JNITest {
|
|||||||
@Test
|
@Test
|
||||||
public void throwsIfMethodIsUsedWithoutLoadingTrustedSetup() {
|
public void throwsIfMethodIsUsedWithoutLoadingTrustedSetup() {
|
||||||
|
|
||||||
final RuntimeException exception = assertThrows(RuntimeException.class,
|
final RuntimeException exception =
|
||||||
() -> CKZG4844JNI.blobToKzgCommitment(TestUtils.createRandomBlob()));
|
assertThrows(
|
||||||
|
RuntimeException.class,
|
||||||
|
() -> CKZG4844JNI.blobToKzgCommitment(TestUtils.createRandomBlob()));
|
||||||
|
|
||||||
assertExceptionIsTrustedSetupIsNotLoaded(exception);
|
assertExceptionIsTrustedSetupIsNotLoaded(exception);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -192,24 +220,23 @@ public class CKZG4844JNITest {
|
|||||||
|
|
||||||
loadTrustedSetup();
|
loadTrustedSetup();
|
||||||
|
|
||||||
final RuntimeException exception = assertThrows(RuntimeException.class,
|
final RuntimeException exception =
|
||||||
CKZG4844JNITest::loadTrustedSetup);
|
assertThrows(RuntimeException.class, CKZG4844JNITest::loadTrustedSetup);
|
||||||
|
|
||||||
assertEquals("Trusted Setup is already loaded. Free it before loading a new one.",
|
assertEquals(
|
||||||
|
"Trusted Setup is already loaded. Free it before loading a new one.",
|
||||||
exception.getMessage());
|
exception.getMessage());
|
||||||
|
|
||||||
CKZG4844JNI.freeTrustedSetup();
|
CKZG4844JNI.freeTrustedSetup();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void throwsIfTryToFreeTrustedSetupWithoutLoadingIt() {
|
public void throwsIfTryToFreeTrustedSetupWithoutLoadingIt() {
|
||||||
|
|
||||||
final RuntimeException exception = assertThrows(RuntimeException.class,
|
final RuntimeException exception =
|
||||||
CKZG4844JNI::freeTrustedSetup);
|
assertThrows(RuntimeException.class, CKZG4844JNI::freeTrustedSetup);
|
||||||
|
|
||||||
assertExceptionIsTrustedSetupIsNotLoaded(exception);
|
assertExceptionIsTrustedSetupIsNotLoaded(exception);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertExceptionIsTrustedSetupIsNotLoaded(final RuntimeException exception) {
|
private void assertExceptionIsTrustedSetupIsNotLoaded(final RuntimeException exception) {
|
||||||
@ -222,10 +249,7 @@ public class CKZG4844JNITest {
|
|||||||
loadTrustedSetup();
|
loadTrustedSetup();
|
||||||
break;
|
break;
|
||||||
case PARAMETERS:
|
case PARAMETERS:
|
||||||
final LoadTrustedSetupParameters parameters = TestUtils.createLoadTrustedSetupParameters(
|
loadTrustedSetupFromParameters();
|
||||||
TRUSTED_SETUP_FILE_BY_PRESET.get(PRESET));
|
|
||||||
CKZG4844JNI.loadTrustedSetup(parameters.getG1(), parameters.getG1Count(), parameters.getG2(),
|
|
||||||
parameters.getG2Count());
|
|
||||||
break;
|
break;
|
||||||
case RESOURCE:
|
case RESOURCE:
|
||||||
loadTrustedSetupFromResource();
|
loadTrustedSetupFromResource();
|
||||||
@ -237,8 +261,16 @@ public class CKZG4844JNITest {
|
|||||||
CKZG4844JNI.loadTrustedSetup(TRUSTED_SETUP_FILE_BY_PRESET.get(PRESET));
|
CKZG4844JNI.loadTrustedSetup(TRUSTED_SETUP_FILE_BY_PRESET.get(PRESET));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void loadTrustedSetupFromParameters() {
|
||||||
|
final LoadTrustedSetupParameters parameters =
|
||||||
|
TestUtils.createLoadTrustedSetupParameters(TRUSTED_SETUP_FILE_BY_PRESET.get(PRESET));
|
||||||
|
CKZG4844JNI.loadTrustedSetup(
|
||||||
|
parameters.getG1(), parameters.getG1Count(), parameters.getG2(), parameters.getG2Count());
|
||||||
|
}
|
||||||
|
|
||||||
public static void loadTrustedSetupFromResource() {
|
public static void loadTrustedSetupFromResource() {
|
||||||
CKZG4844JNI.loadTrustedSetupFromResource(TRUSTED_SETUP_RESOURCE_BY_PRESET.get(PRESET), CKZG4844JNITest.class);
|
CKZG4844JNI.loadTrustedSetupFromResource(
|
||||||
|
TRUSTED_SETUP_RESOURCE_BY_PRESET.get(PRESET), CKZG4844JNITest.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<VerifyKzgProofParameters> getVerifyKzgProofTestVectors() {
|
private static Stream<VerifyKzgProofParameters> getVerifyKzgProofTestVectors() {
|
||||||
|
@ -7,8 +7,8 @@ public class LoadTrustedSetupParameters {
|
|||||||
private final byte[] g2;
|
private final byte[] g2;
|
||||||
private final long g2Count;
|
private final long g2Count;
|
||||||
|
|
||||||
public LoadTrustedSetupParameters(final byte[] g1, final long g1Count, final byte[] g2,
|
public LoadTrustedSetupParameters(
|
||||||
final long g2Count) {
|
final byte[] g1, final long g1Count, final byte[] g2, final long g2Count) {
|
||||||
this.g1 = g1;
|
this.g1 = g1;
|
||||||
this.g1Count = g1Count;
|
this.g1Count = g1Count;
|
||||||
this.g2 = g2;
|
this.g2 = g2;
|
||||||
|
@ -35,15 +35,17 @@ public class TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] createRandomBlob() {
|
public static byte[] createRandomBlob() {
|
||||||
final byte[][] blob = IntStream.range(0, CKZG4844JNI.getFieldElementsPerBlob())
|
final byte[][] blob =
|
||||||
.mapToObj(__ -> randomBLSFieldElement())
|
IntStream.range(0, CKZG4844JNI.getFieldElementsPerBlob())
|
||||||
.map(fieldElement -> fieldElement.toArray(ByteOrder.LITTLE_ENDIAN)).toArray(byte[][]::new);
|
.mapToObj(__ -> randomBLSFieldElement())
|
||||||
|
.map(fieldElement -> fieldElement.toArray(ByteOrder.LITTLE_ENDIAN))
|
||||||
|
.toArray(byte[][]::new);
|
||||||
return flatten(blob);
|
return flatten(blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] createRandomBlobs(final int count) {
|
public static byte[] createRandomBlobs(final int count) {
|
||||||
final byte[][] blobs = IntStream.range(0, count).mapToObj(__ -> createRandomBlob())
|
final byte[][] blobs =
|
||||||
.toArray(byte[][]::new);
|
IntStream.range(0, count).mapToObj(__ -> createRandomBlob()).toArray(byte[][]::new);
|
||||||
return flatten(blobs);
|
return flatten(blobs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,16 +58,17 @@ public class TestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] createRandomCommitments(final int count) {
|
public static byte[] createRandomCommitments(final int count) {
|
||||||
final byte[][] commitments = IntStream.range(0, count).mapToObj(__ -> createRandomCommitment())
|
final byte[][] commitments =
|
||||||
.toArray(byte[][]::new);
|
IntStream.range(0, count).mapToObj(__ -> createRandomCommitment()).toArray(byte[][]::new);
|
||||||
return flatten(commitments);
|
return flatten(commitments);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] createNonCanonicalBlob() {
|
public static byte[] createNonCanonicalBlob() {
|
||||||
final byte[][] blob = IntStream.range(0, CKZG4844JNI.getFieldElementsPerBlob())
|
final byte[][] blob =
|
||||||
.mapToObj(__ -> UInt256.valueOf(CKZG4844JNI.BLS_MODULUS.add(BigInteger.valueOf(42))))
|
IntStream.range(0, CKZG4844JNI.getFieldElementsPerBlob())
|
||||||
.map(greaterThanModulus -> greaterThanModulus.toArray(ByteOrder.LITTLE_ENDIAN))
|
.mapToObj(__ -> UInt256.valueOf(CKZG4844JNI.BLS_MODULUS.add(BigInteger.valueOf(42))))
|
||||||
.toArray(byte[][]::new);
|
.map(greaterThanModulus -> greaterThanModulus.toArray(ByteOrder.LITTLE_ENDIAN))
|
||||||
|
.toArray(byte[][]::new);
|
||||||
return flatten(blob);
|
return flatten(blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,8 +78,8 @@ public class TestUtils {
|
|||||||
*/
|
*/
|
||||||
public static List<VerifyKzgProofParameters> getVerifyKzgProofTestVectors() {
|
public static List<VerifyKzgProofParameters> getVerifyKzgProofTestVectors() {
|
||||||
final JsonNode jsonNode;
|
final JsonNode jsonNode;
|
||||||
try (final InputStream testVectors = readResource(
|
try (final InputStream testVectors =
|
||||||
"test-vectors/public_verify_kzg_proof.json")) {
|
readResource("test-vectors/public_verify_kzg_proof.json")) {
|
||||||
jsonNode = OBJECT_MAPPER.readTree(testVectors);
|
jsonNode = OBJECT_MAPPER.readTree(testVectors);
|
||||||
} catch (final IOException ex) {
|
} catch (final IOException ex) {
|
||||||
throw new UncheckedIOException(ex);
|
throw new UncheckedIOException(ex);
|
||||||
@ -84,17 +87,21 @@ public class TestUtils {
|
|||||||
final ArrayNode testCases = (ArrayNode) jsonNode.get("TestCases");
|
final ArrayNode testCases = (ArrayNode) jsonNode.get("TestCases");
|
||||||
final Stream.Builder<VerifyKzgProofParameters> testVectors = Stream.builder();
|
final Stream.Builder<VerifyKzgProofParameters> testVectors = Stream.builder();
|
||||||
testVectors.add(VerifyKzgProofParameters.ZERO);
|
testVectors.add(VerifyKzgProofParameters.ZERO);
|
||||||
IntStream.range(0, jsonNode.get("NumTestCases").asInt()).mapToObj(i -> {
|
IntStream.range(0, jsonNode.get("NumTestCases").asInt())
|
||||||
final JsonNode testCase = testCases.get(i);
|
.mapToObj(
|
||||||
final Bytes32 z = Bytes32.fromHexString(testCase.get("InputPoint").asText());
|
i -> {
|
||||||
final Bytes32 y = Bytes32.fromHexString(testCase.get("ClaimedValue").asText());
|
final JsonNode testCase = testCases.get(i);
|
||||||
final Bytes commitment = Bytes.fromHexString(testCase.get("Commitment").asText(),
|
final Bytes32 z = Bytes32.fromHexString(testCase.get("InputPoint").asText());
|
||||||
CKZG4844JNI.BYTES_PER_COMMITMENT);
|
final Bytes32 y = Bytes32.fromHexString(testCase.get("ClaimedValue").asText());
|
||||||
final Bytes proof = Bytes.fromHexString(testCase.get("Proof").asText(),
|
final Bytes commitment =
|
||||||
CKZG4844JNI.BYTES_PER_PROOF);
|
Bytes.fromHexString(
|
||||||
return new VerifyKzgProofParameters(commitment.toArray(), z.toArray(), y.toArray(),
|
testCase.get("Commitment").asText(), CKZG4844JNI.BYTES_PER_COMMITMENT);
|
||||||
proof.toArray());
|
final Bytes proof =
|
||||||
}).forEach(testVectors::add);
|
Bytes.fromHexString(testCase.get("Proof").asText(), CKZG4844JNI.BYTES_PER_PROOF);
|
||||||
|
return new VerifyKzgProofParameters(
|
||||||
|
commitment.toArray(), z.toArray(), y.toArray(), proof.toArray());
|
||||||
|
})
|
||||||
|
.forEach(testVectors::add);
|
||||||
return testVectors.build().collect(Collectors.toList());
|
return testVectors.build().collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,5 +142,4 @@ public class TestUtils {
|
|||||||
private static InputStream readResource(final String resource) {
|
private static InputStream readResource(final String resource) {
|
||||||
return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
|
return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,8 @@ public class VerifyKzgProofParameters {
|
|||||||
private final byte[] y;
|
private final byte[] y;
|
||||||
private final byte[] proof;
|
private final byte[] proof;
|
||||||
|
|
||||||
public VerifyKzgProofParameters(final byte[] commitment, final byte[] z, final byte[] y,
|
public VerifyKzgProofParameters(
|
||||||
final byte[] proof) {
|
final byte[] commitment, final byte[] z, final byte[] y, final byte[] proof) {
|
||||||
this.commitment = commitment;
|
this.commitment = commitment;
|
||||||
this.z = z;
|
this.z = z;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
@ -42,5 +42,4 @@ public class VerifyKzgProofParameters {
|
|||||||
public byte[] getProof() {
|
public byte[] getProof() {
|
||||||
return proof;
|
return proof;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user