Update java bindings to support YAML ref tests (#166)

* Update java bindings to support YAML ref tests

* Clean things up a little

* Fix some more nits

* Swap comparision to be expected/actual

* Add missing input length checks

* Move test formats to new directory
This commit is contained in:
Justin Traglia 2023-03-06 06:58:49 -07:00 committed by GitHub
parent 54ab82c93b
commit 410eed4db6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 483 additions and 269 deletions

View File

@ -23,7 +23,7 @@ dependencies {
testFixturesImplementation("org.apache.tuweni:tuweni-units:2.3.1")
testFixturesImplementation("com.fasterxml.jackson.core:jackson-databind:2.14.2")
testFixturesImplementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.14.2")
}
spotless {

View File

@ -193,6 +193,20 @@ JNIEXPORT jbyteArray JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_computeKzgProof(
return NULL;
}
size_t blob_size = (size_t)(*env)->GetArrayLength(env, blob);
if (blob_size != BYTES_PER_BLOB)
{
throw_invalid_size_exception(env, "Invalid blob size.", blob_size, BYTES_PER_BLOB);
return NULL;
}
size_t z_bytes_size = (size_t)(*env)->GetArrayLength(env, z_bytes);
if (z_bytes_size != BYTES_PER_FIELD_ELEMENT)
{
throw_invalid_size_exception(env, "Invalid z size.", z_bytes_size, BYTES_PER_FIELD_ELEMENT);
return NULL;
}
Blob *blob_native = (Blob *)(*env)->GetByteArrayElements(env, blob, NULL);
Bytes32 *z_native = (Bytes32 *)(*env)->GetByteArrayElements(env, z_bytes, NULL);
@ -255,6 +269,34 @@ JNIEXPORT jboolean JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_verifyKzgProof(JNI
return 0;
}
size_t commitment_bytes_size = (size_t)(*env)->GetArrayLength(env, commitment_bytes);
if (commitment_bytes_size != BYTES_PER_COMMITMENT)
{
throw_invalid_size_exception(env, "Invalid commitment size.", commitment_bytes_size, BYTES_PER_COMMITMENT);
return 0;
}
size_t z_bytes_size = (size_t)(*env)->GetArrayLength(env, z_bytes);
if (z_bytes_size != BYTES_PER_FIELD_ELEMENT)
{
throw_invalid_size_exception(env, "Invalid z size.", z_bytes_size, BYTES_PER_FIELD_ELEMENT);
return 0;
}
size_t y_bytes_size = (size_t)(*env)->GetArrayLength(env, y_bytes);
if (y_bytes_size != BYTES_PER_FIELD_ELEMENT)
{
throw_invalid_size_exception(env, "Invalid y size.", y_bytes_size, BYTES_PER_FIELD_ELEMENT);
return 0;
}
size_t proof_bytes_size = (size_t)(*env)->GetArrayLength(env, proof_bytes);
if (proof_bytes_size != BYTES_PER_PROOF)
{
throw_invalid_size_exception(env, "Invalid proof size.", proof_bytes_size, BYTES_PER_PROOF);
return 0;
}
Bytes48 *commitment_native = (Bytes48 *)(*env)->GetByteArrayElements(env, commitment_bytes, NULL);
Bytes48 *proof_native = (Bytes48 *)(*env)->GetByteArrayElements(env, proof_bytes, NULL);
Bytes32 *z_native = (Bytes32 *)(*env)->GetByteArrayElements(env, z_bytes, NULL);
@ -300,7 +342,7 @@ JNIEXPORT jboolean JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_verifyBlobKzgProof
}
size_t proof_bytes_size = (size_t)(*env)->GetArrayLength(env, proof_bytes);
if (proof_bytes_size != BYTES_PER_COMMITMENT)
if (proof_bytes_size != BYTES_PER_PROOF)
{
throw_invalid_size_exception(env, "Invalid proof size.", proof_bytes_size, BYTES_PER_PROOF);
return 0;

View File

@ -60,6 +60,22 @@ public class CKZG4844JNIBenchmark {
}
}
@State(Scope.Benchmark)
public static class VerifyKzgProofState {
private byte[] commitment;
private byte[] z;
private byte[] y;
private byte[] proof;
@Setup(Level.Iteration)
public void setUp() {
commitment = TestUtils.createRandomCommitments(1);
z = TestUtils.randomBLSFieldElementBytes();
y = TestUtils.randomBLSFieldElementBytes();
proof = TestUtils.createRandomProofs(1);
}
}
@State(Scope.Benchmark)
public static class VerifyBlobKzgProofState {
private byte[] blob;
@ -91,23 +107,6 @@ public class CKZG4844JNIBenchmark {
}
}
@State(Scope.Benchmark)
public static class VerifyKzgProofState {
private byte[] commitment;
private byte[] z;
private byte[] y;
private byte[] proof;
@Setup
public void setUp() {
final VerifyKzgProofParameters parameters = TestUtils.getVerifyKzgProofTestVectors().get(2);
commitment = parameters.getCommitment();
z = parameters.getZ();
y = parameters.getY();
proof = parameters.getProof();
}
}
@Setup
public void setUp() {
CKZG4844JNI.loadTrustedSetup("../../src/trusted_setup.txt");

View File

@ -4,12 +4,12 @@ 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;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import ethereum.ckzg4844.CKZG4844JNI.Preset;
import java.nio.file.Files;
import java.nio.file.Paths;
import ethereum.ckzg4844.test_formats.*;
import java.util.Map;
import java.util.Optional;
import java.util.stream.IntStream;
@ -38,17 +38,9 @@ public class CKZG4844JNITest {
private static final Map<Preset, String> TRUSTED_SETUP_RESOURCE_BY_PRESET =
Map.of(
Preset.MAINNET,
"/test-vectors/trusted_setup.txt",
"/trusted-setups/trusted_setup.txt",
Preset.MINIMAL,
"/test-vectors/trusted_setup_4.txt");
private static final String BLOB_TO_KZG_COMMITMENT_TESTS = "../../tests/blob_to_kzg_commitment/";
private static final String COMPUTE_KZG_PROOF_TESTS = "../../tests/compute_kzg_proof/";
private static final String COMPUTE_BLOB_KZG_PROOF_TESTS = "../../tests/compute_blob_kzg_proof/";
private static final String VERIFY_KZG_PROOF_TESTS = "../../tests/verify_kzg_proof/";
private static final String VERIFY_BLOB_KZG_PROOF_TESTS = "../../tests/verify_blob_kzg_proof/";
private static final String VERIFY_BLOB_KZG_PROOF_BATCH_TESTS =
"../../tests/verify_blob_kzg_proof_batch/";
"/trusted-setups/trusted_setup_4.txt");
static {
PRESET =
@ -59,144 +51,97 @@ public class CKZG4844JNITest {
CKZG4844JNI.loadNativeLibrary(PRESET);
}
@Test
public void blobToKzgCommitmentTests() {
@ParameterizedTest
@MethodSource("getBlobToKzgCommitmentTests")
public void blobToKzgCommitmentTests(final BlobToKzgCommitmentTest test) {
if (PRESET != Preset.MAINNET) return;
loadTrustedSetup();
for (String test : TestUtils.getFiles(BLOB_TO_KZG_COMMITMENT_TESTS)) {
byte[] blob = TestUtils.getBytes(Paths.get(test, "blob.txt"));
try {
byte[] commitment = CKZG4844JNI.blobToKzgCommitment(blob);
byte[] expectedCommitment = TestUtils.getBytes(Paths.get(test, "commitment.txt"));
assertArrayEquals(commitment, expectedCommitment);
} catch (CKZGException ex) {
assertFalse(Files.exists(Paths.get(test, "commitment.txt")));
}
try {
byte[] commitment = CKZG4844JNI.blobToKzgCommitment(test.getInput().getBlob());
assertArrayEquals(test.getOutput(), commitment);
} catch (CKZGException ex) {
assertNull(test.getOutput());
}
CKZG4844JNI.freeTrustedSetup();
}
@Test
public void computeKzgProofTests() {
@ParameterizedTest
@MethodSource("getComputeKzgProofTests")
public void computeKzgProofTests(final ComputeKzgProofTest test) {
if (PRESET != Preset.MAINNET) return;
loadTrustedSetup();
for (String test : TestUtils.getFiles(COMPUTE_KZG_PROOF_TESTS)) {
byte[] blob = TestUtils.getBytes(Paths.get(test, "blob.txt"));
byte[] inputPoint = TestUtils.getBytes(Paths.get(test, "input_point.txt"));
try {
byte[] proof = CKZG4844JNI.computeKzgProof(blob, inputPoint);
byte[] expectedProof = TestUtils.getBytes(Paths.get(test, "proof.txt"));
assertArrayEquals(proof, expectedProof);
} catch (CKZGException ex) {
assertFalse(Files.exists(Paths.get(test, "proof.txt")));
}
try {
byte[] proof = CKZG4844JNI.computeKzgProof(test.getInput().getBlob(), test.getInput().getZ());
assertArrayEquals(test.getOutput(), proof);
} catch (CKZGException ex) {
assertNull(test.getOutput());
}
CKZG4844JNI.freeTrustedSetup();
}
@Test
public void computeBlobKzgProofTests() {
@ParameterizedTest
@MethodSource("getComputeBlobKzgProofTests")
public void computeBlobKzgProofTests(final ComputeBlobKzgProofTest test) {
if (PRESET != Preset.MAINNET) return;
loadTrustedSetup();
for (String test : TestUtils.getFiles(COMPUTE_BLOB_KZG_PROOF_TESTS)) {
byte[] blob = TestUtils.getBytes(Paths.get(test, "blob.txt"));
try {
byte[] proof = CKZG4844JNI.computeBlobKzgProof(blob);
byte[] expectedProof = TestUtils.getBytes(Paths.get(test, "proof.txt"));
assertArrayEquals(proof, expectedProof);
} catch (CKZGException ex) {
assertFalse(Files.exists(Paths.get(test, "proof.txt")));
}
try {
byte[] proof = CKZG4844JNI.computeBlobKzgProof(test.getInput().getBlob());
assertArrayEquals(test.getOutput(), proof);
} catch (CKZGException ex) {
assertNull(test.getOutput());
}
CKZG4844JNI.freeTrustedSetup();
}
@Test
public void verifyKzgProofTests() {
@ParameterizedTest
@MethodSource("getVerifyKzgProofTests")
public void verifyKzgProofTests(final VerifyKzgProofTest test) {
if (PRESET != Preset.MAINNET) return;
loadTrustedSetup();
for (String test : TestUtils.getFiles(VERIFY_KZG_PROOF_TESTS)) {
byte[] commitment = TestUtils.getBytes(Paths.get(test, "commitment.txt"));
byte[] inputPoint = TestUtils.getBytes(Paths.get(test, "input_point.txt"));
byte[] claimedValue = TestUtils.getBytes(Paths.get(test, "claimed_value.txt"));
byte[] proof = TestUtils.getBytes(Paths.get(test, "proof.txt"));
try {
boolean ok = CKZG4844JNI.verifyKzgProof(commitment, inputPoint, claimedValue, proof);
boolean expectedOk = TestUtils.getBoolean(Paths.get(test, "ok.txt"));
assertEquals(ok, expectedOk);
} catch (CKZGException ex) {
assertFalse(Files.exists(Paths.get(test, "ok.txt")));
}
try {
boolean valid =
CKZG4844JNI.verifyKzgProof(
test.getInput().getCommitment(),
test.getInput().getZ(),
test.getInput().getY(),
test.getInput().getProof());
assertEquals(test.getOutput(), valid);
} catch (CKZGException ex) {
assertNull(test.getOutput());
}
CKZG4844JNI.freeTrustedSetup();
}
@Test
public void verifyBlobKzgProofTests() {
@ParameterizedTest
@MethodSource("getVerifyBlobKzgProofTests")
public void verifyBlobKzgProofTests(final VerifyBlobKzgProofTest test) {
if (PRESET != Preset.MAINNET) return;
loadTrustedSetup();
for (String test : TestUtils.getFiles(VERIFY_BLOB_KZG_PROOF_TESTS)) {
byte[] blob = TestUtils.getBytes(Paths.get(test, "blob.txt"));
byte[] commitment = TestUtils.getBytes(Paths.get(test, "commitment.txt"));
byte[] proof = TestUtils.getBytes(Paths.get(test, "proof.txt"));
try {
boolean ok = CKZG4844JNI.verifyBlobKzgProof(blob, commitment, proof);
boolean expectedOk = TestUtils.getBoolean(Paths.get(test, "ok.txt"));
assertEquals(ok, expectedOk);
} catch (CKZGException ex) {
assertFalse(Files.exists(Paths.get(test, "ok.txt")));
}
try {
boolean valid =
CKZG4844JNI.verifyBlobKzgProof(
test.getInput().getBlob(),
test.getInput().getCommitment(),
test.getInput().getProof());
assertEquals(test.getOutput(), valid);
} catch (CKZGException ex) {
assertNull(test.getOutput());
}
CKZG4844JNI.freeTrustedSetup();
}
@Test
public void verifyBlobKzgProofBatchTests() {
@ParameterizedTest
@MethodSource("getVerifyBlobKzgProofBatchTests")
public void verifyBlobKzgProofBatchTests(final VerifyBlobKzgProofBatchTest test) {
if (PRESET != Preset.MAINNET) return;
loadTrustedSetup();
for (String test : TestUtils.getFiles(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS)) {
byte[] blobs =
TestUtils.flatten(
TestUtils.getFiles(Paths.get(test, "blobs").toString()).stream()
.map(Paths::get)
.map(TestUtils::getBytes)
.toArray(byte[][]::new));
byte[] commitments =
TestUtils.flatten(
TestUtils.getFiles(Paths.get(test, "commitments").toString()).stream()
.map(Paths::get)
.map(TestUtils::getBytes)
.toArray(byte[][]::new));
byte[] proofs =
TestUtils.flatten(
TestUtils.getFiles(Paths.get(test, "proofs").toString()).stream()
.map(Paths::get)
.map(TestUtils::getBytes)
.toArray(byte[][]::new));
try {
boolean ok =
CKZG4844JNI.verifyBlobKzgProofBatch(
blobs, commitments, proofs, blobs.length / CKZG4844JNI.getBytesPerBlob());
boolean expectedOk = TestUtils.getBoolean(Paths.get(test, "ok.txt"));
assertEquals(ok, expectedOk);
} catch (CKZGException ex) {
assertFalse(Files.exists(Paths.get(test, "ok.txt")));
}
try {
int count = test.getInput().getBlobs().length / CKZG4844JNI.getBytesPerBlob();
boolean valid =
CKZG4844JNI.verifyBlobKzgProofBatch(
test.getInput().getBlobs(),
test.getInput().getCommitments(),
test.getInput().getProofs(),
count);
assertEquals(test.getOutput(), valid);
} catch (CKZGException ex) {
assertNull(test.getOutput());
}
CKZG4844JNI.freeTrustedSetup();
}
@Test
@ -207,17 +152,6 @@ public class CKZG4844JNITest {
CKZG4844JNI.getBytesPerBlob());
}
@ParameterizedTest(name = "{index}")
@MethodSource("getVerifyKzgProofTestVectors")
public void testVerifyKzgProof(final VerifyKzgProofParameters parameters) {
assertTrue(
CKZG4844JNI.verifyKzgProof(
parameters.getCommitment(),
parameters.getZ(),
parameters.getY(),
parameters.getProof()));
}
@ParameterizedTest
@EnumSource(TrustedSetupSource.class)
public void testVerifyBlobKzgProofBatch(final TrustedSetupSource trustedSetupSource) {
@ -458,8 +392,34 @@ public class CKZG4844JNITest {
TRUSTED_SETUP_RESOURCE_BY_PRESET.get(PRESET), CKZG4844JNITest.class);
}
private static Stream<VerifyKzgProofParameters> getVerifyKzgProofTestVectors() {
private static Stream<BlobToKzgCommitmentTest> getBlobToKzgCommitmentTests() {
loadTrustedSetup();
return TestUtils.getVerifyKzgProofTestVectors().stream().onClose(CKZG4844JNI::freeTrustedSetup);
return TestUtils.getBlobToKzgCommitmentTests().stream().onClose(CKZG4844JNI::freeTrustedSetup);
}
private static Stream<ComputeKzgProofTest> getComputeKzgProofTests() {
loadTrustedSetup();
return TestUtils.getComputeKzgProofTests().stream().onClose(CKZG4844JNI::freeTrustedSetup);
}
private static Stream<ComputeBlobKzgProofTest> getComputeBlobKzgProofTests() {
loadTrustedSetup();
return TestUtils.getComputeBlobKzgProofTests().stream().onClose(CKZG4844JNI::freeTrustedSetup);
}
private static Stream<VerifyKzgProofTest> getVerifyKzgProofTests() {
loadTrustedSetup();
return TestUtils.getVerifyKzgProofTests().stream().onClose(CKZG4844JNI::freeTrustedSetup);
}
private static Stream<VerifyBlobKzgProofTest> getVerifyBlobKzgProofTests() {
loadTrustedSetup();
return TestUtils.getVerifyBlobKzgProofTests().stream().onClose(CKZG4844JNI::freeTrustedSetup);
}
private static Stream<VerifyBlobKzgProofBatchTest> getVerifyBlobKzgProofBatchTests() {
loadTrustedSetup();
return TestUtils.getVerifyBlobKzgProofBatchTests().stream()
.onClose(CKZG4844JNI::freeTrustedSetup);
}
}

View File

@ -1,8 +1,8 @@
package ethereum.ckzg4844;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import ethereum.ckzg4844.test_formats.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
@ -14,6 +14,7 @@ import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
@ -21,15 +22,22 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
public class TestUtils {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(new YAMLFactory());
private static final Random RANDOM = new Random();
private static final String BLOB_TO_KZG_COMMITMENT_TESTS = "../../tests/blob_to_kzg_commitment/";
private static final String COMPUTE_KZG_PROOF_TESTS = "../../tests/compute_kzg_proof/";
private static final String COMPUTE_BLOB_KZG_PROOF_TESTS = "../../tests/compute_blob_kzg_proof/";
private static final String VERIFY_KZG_PROOF_TESTS = "../../tests/verify_kzg_proof/";
private static final String VERIFY_BLOB_KZG_PROOF_TESTS = "../../tests/verify_blob_kzg_proof/";
private static final String VERIFY_BLOB_KZG_PROOF_BATCH_TESTS =
"../../tests/verify_blob_kzg_proof_batch/";
public static byte[] flatten(final byte[]... bytes) {
final int capacity = Arrays.stream(bytes).mapToInt(b -> b.length).sum();
final ByteBuffer buffer = ByteBuffer.allocate(capacity);
@ -81,37 +89,103 @@ public class TestUtils {
return flatten(blob);
}
/**
* Generated using <a
* href="https://github.com/crate-crypto/proto-danksharding-fuzzy-test/">proto-danksharding-fuzzy-test</a>
*/
public static List<VerifyKzgProofParameters> getVerifyKzgProofTestVectors() {
final JsonNode jsonNode;
try (final InputStream testVectors =
readResource("test-vectors/public_verify_kzg_proof.json")) {
jsonNode = OBJECT_MAPPER.readTree(testVectors);
} catch (final IOException ex) {
public static List<BlobToKzgCommitmentTest> getBlobToKzgCommitmentTests() {
final Stream.Builder<BlobToKzgCommitmentTest> tests = Stream.builder();
try {
for (String testFile : getTestFiles(BLOB_TO_KZG_COMMITMENT_TESTS)) {
String data = Files.readString(Path.of(testFile));
BlobToKzgCommitmentTest test = OBJECT_MAPPER.readValue(data, BlobToKzgCommitmentTest.class);
tests.add(test);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
final ArrayNode testCases = (ArrayNode) jsonNode.get("TestCases");
final Stream.Builder<VerifyKzgProofParameters> testVectors = Stream.builder();
testVectors.add(VerifyKzgProofParameters.ZERO);
IntStream.range(0, jsonNode.get("NumTestCases").asInt())
.mapToObj(
i -> {
final JsonNode testCase = testCases.get(i);
final Bytes32 z = Bytes32.fromHexString(testCase.get("InputPoint").asText());
final Bytes32 y = Bytes32.fromHexString(testCase.get("ClaimedValue").asText());
final Bytes commitment =
Bytes.fromHexString(
testCase.get("Commitment").asText(), CKZG4844JNI.BYTES_PER_COMMITMENT);
final Bytes proof =
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 tests.build().collect(Collectors.toList());
}
public static List<ComputeKzgProofTest> getComputeKzgProofTests() {
final Stream.Builder<ComputeKzgProofTest> tests = Stream.builder();
try {
for (String testFile : getTestFiles(COMPUTE_KZG_PROOF_TESTS)) {
String jsonData = Files.readString(Path.of(testFile));
ComputeKzgProofTest test = OBJECT_MAPPER.readValue(jsonData, ComputeKzgProofTest.class);
tests.add(test);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return tests.build().collect(Collectors.toList());
}
public static List<ComputeBlobKzgProofTest> getComputeBlobKzgProofTests() {
final Stream.Builder<ComputeBlobKzgProofTest> tests = Stream.builder();
try {
for (String testFile : getTestFiles(COMPUTE_BLOB_KZG_PROOF_TESTS)) {
String jsonData = Files.readString(Path.of(testFile));
ComputeBlobKzgProofTest test =
OBJECT_MAPPER.readValue(jsonData, ComputeBlobKzgProofTest.class);
tests.add(test);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return tests.build().collect(Collectors.toList());
}
public static List<VerifyKzgProofTest> getVerifyKzgProofTests() {
final Stream.Builder<VerifyKzgProofTest> tests = Stream.builder();
try {
for (String testFile : getTestFiles(VERIFY_KZG_PROOF_TESTS)) {
String jsonData = Files.readString(Path.of(testFile));
VerifyKzgProofTest test = OBJECT_MAPPER.readValue(jsonData, VerifyKzgProofTest.class);
tests.add(test);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return tests.build().collect(Collectors.toList());
}
public static List<VerifyBlobKzgProofTest> getVerifyBlobKzgProofTests() {
final Stream.Builder<VerifyBlobKzgProofTest> tests = Stream.builder();
try {
for (String testFile : getTestFiles(VERIFY_BLOB_KZG_PROOF_TESTS)) {
String jsonData = Files.readString(Path.of(testFile));
VerifyBlobKzgProofTest test =
OBJECT_MAPPER.readValue(jsonData, VerifyBlobKzgProofTest.class);
tests.add(test);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return tests.build().collect(Collectors.toList());
}
public static List<VerifyBlobKzgProofBatchTest> getVerifyBlobKzgProofBatchTests() {
final Stream.Builder<VerifyBlobKzgProofBatchTest> tests = Stream.builder();
try {
for (String testFile : getTestFiles(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS)) {
String jsonData = Files.readString(Path.of(testFile));
VerifyBlobKzgProofBatchTest test =
OBJECT_MAPPER.readValue(jsonData, VerifyBlobKzgProofBatchTest.class);
tests.add(test);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return tests.build().collect(Collectors.toList());
}
public static LoadTrustedSetupParameters createLoadTrustedSetupParameters(
@ -152,22 +226,6 @@ public class TestUtils {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
}
public static byte[] getBytes(Path path) {
try {
return Bytes.fromHexString(Files.readString(path)).toArray();
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}
}
public static boolean getBoolean(Path path) {
try {
return Files.readString(path).contains("true");
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}
}
public static List<String> getFiles(String path) {
try {
try (Stream<Path> stream = Files.list(Paths.get(path))) {
@ -177,4 +235,14 @@ public class TestUtils {
throw new UncheckedIOException(ex);
}
}
public static List<String> getTestFiles(String path) {
List<String> testFiles = new ArrayList<>();
for (final String suite : getFiles(path)) {
for (final String test : getFiles(suite)) {
testFiles.addAll(getFiles(test));
}
}
return testFiles;
}
}

View File

@ -1,45 +0,0 @@
package ethereum.ckzg4844;
public class VerifyKzgProofParameters {
public static final VerifyKzgProofParameters ZERO;
static {
final byte[] commitment = new byte[CKZG4844JNI.BYTES_PER_COMMITMENT];
commitment[0] = (byte) 0xc0;
final byte[] z = new byte[32];
final byte[] y = new byte[32];
final byte[] proof = new byte[CKZG4844JNI.BYTES_PER_PROOF];
proof[0] = (byte) 0xc0;
ZERO = new VerifyKzgProofParameters(commitment, z, y, proof);
}
private final byte[] commitment;
private final byte[] z;
private final byte[] y;
private final byte[] proof;
public VerifyKzgProofParameters(
final byte[] commitment, final byte[] z, final byte[] y, final byte[] proof) {
this.commitment = commitment;
this.z = z;
this.y = y;
this.proof = proof;
}
public byte[] getCommitment() {
return commitment;
}
public byte[] getZ() {
return z;
}
public byte[] getY() {
return y;
}
public byte[] getProof() {
return proof;
}
}

View File

@ -0,0 +1,27 @@
package ethereum.ckzg4844.test_formats;
import org.apache.tuweni.bytes.Bytes;
public class BlobToKzgCommitmentTest {
public static class Input {
private String blob;
public byte[] getBlob() {
return Bytes.fromHexString(blob).toArray();
}
}
private Input input;
private String output;
public Input getInput() {
return input;
}
public byte[] getOutput() {
if (output == null) {
return null;
}
return Bytes.fromHexString(output).toArray();
}
}

View File

@ -0,0 +1,27 @@
package ethereum.ckzg4844.test_formats;
import org.apache.tuweni.bytes.Bytes;
public class ComputeBlobKzgProofTest {
public static class Input {
private String blob;
public byte[] getBlob() {
return Bytes.fromHexString(blob).toArray();
}
}
private Input input;
private String output;
public Input getInput() {
return input;
}
public byte[] getOutput() {
if (output == null) {
return null;
}
return Bytes.fromHexString(output).toArray();
}
}

View File

@ -0,0 +1,32 @@
package ethereum.ckzg4844.test_formats;
import org.apache.tuweni.bytes.Bytes;
public class ComputeKzgProofTest {
public static class Input {
private String blob;
private String z;
public byte[] getBlob() {
return Bytes.fromHexString(blob).toArray();
}
public byte[] getZ() {
return Bytes.fromHexString(z).toArray();
}
}
private Input input;
private String output;
public Input getInput() {
return input;
}
public byte[] getOutput() {
if (output == null) {
return null;
}
return Bytes.fromHexString(output).toArray();
}
}

View File

@ -0,0 +1,52 @@
package ethereum.ckzg4844.test_formats;
import ethereum.ckzg4844.TestUtils;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.tuweni.bytes.Bytes;
public class VerifyBlobKzgProofBatchTest {
public static class Input {
private List<String> blobs;
private List<String> commitments;
private List<String> proofs;
public byte[] getBlobs() {
return TestUtils.flatten(
blobs.stream()
.map(Bytes::fromHexString)
.map(Bytes::toArray)
.collect(Collectors.toList())
.toArray(byte[][]::new));
}
public byte[] getCommitments() {
return TestUtils.flatten(
commitments.stream()
.map(Bytes::fromHexString)
.map(Bytes::toArray)
.collect(Collectors.toList())
.toArray(byte[][]::new));
}
public byte[] getProofs() {
return TestUtils.flatten(
proofs.stream()
.map(Bytes::fromHexString)
.map(Bytes::toArray)
.collect(Collectors.toList())
.toArray(byte[][]::new));
}
}
private Input input;
private Boolean output;
public Input getInput() {
return input;
}
public Boolean getOutput() {
return output;
}
}

View File

@ -0,0 +1,34 @@
package ethereum.ckzg4844.test_formats;
import org.apache.tuweni.bytes.Bytes;
public class VerifyBlobKzgProofTest {
public static class Input {
private String blob;
private String commitment;
private String proof;
public byte[] getBlob() {
return Bytes.fromHexString(blob).toArray();
}
public byte[] getCommitment() {
return Bytes.fromHexString(commitment).toArray();
}
public byte[] getProof() {
return Bytes.fromHexString(proof).toArray();
}
}
private Input input;
private Boolean output;
public Input getInput() {
return input;
}
public Boolean getOutput() {
return output;
}
}

View File

@ -0,0 +1,39 @@
package ethereum.ckzg4844.test_formats;
import org.apache.tuweni.bytes.Bytes;
public class VerifyKzgProofTest {
public static class Input {
private String commitment;
private String z;
private String y;
private String proof;
public byte[] getCommitment() {
return Bytes.fromHexString(commitment).toArray();
}
public byte[] getZ() {
return Bytes.fromHexString(z).toArray();
}
public byte[] getY() {
return Bytes.fromHexString(y).toArray();
}
public byte[] getProof() {
return Bytes.fromHexString(proof).toArray();
}
}
private Input input;
private Boolean output;
public Input getInput() {
return input;
}
public Boolean getOutput() {
return output;
}
}

File diff suppressed because one or more lines are too long