Skip to content

Commit

Permalink
Add kzg reference tests support (#7571)
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 authored Oct 4, 2023
1 parent 4234442 commit 0905125
Show file tree
Hide file tree
Showing 9 changed files with 461 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import tech.pegasys.teku.reference.phase0.bls.BlsTests;
import tech.pegasys.teku.reference.phase0.forkchoice.ForkChoiceTestExecutor;
import tech.pegasys.teku.reference.phase0.genesis.GenesisTests;
import tech.pegasys.teku.reference.phase0.kzg.KzgTests;
import tech.pegasys.teku.reference.phase0.rewards.RewardsTestExecutorPhase0;
import tech.pegasys.teku.reference.phase0.sanity.SanityTests;
import tech.pegasys.teku.reference.phase0.shuffling.ShufflingTestExecutor;
Expand All @@ -37,6 +38,7 @@ public abstract class Eth2ReferenceTestCase {
private static final ImmutableMap<String, TestExecutor> COMMON_TEST_TYPES =
ImmutableMap.<String, TestExecutor>builder()
.putAll(BlsTests.BLS_TEST_TYPES)
.putAll(KzgTests.KZG_TEST_TYPES)
.putAll(ForkChoiceTestExecutor.FORK_CHOICE_TEST_TYPES)
.putAll(GenesisTests.GENESIS_TEST_TYPES)
.putAll(ShufflingTestExecutor.SHUFFLING_TEST_TYPES)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Consensys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.reference.phase0.kzg;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.tuweni.bytes.Bytes;
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.kzg.KZGCommitment;
import tech.pegasys.teku.kzg.ckzg4844.CKZG4844;

public class KzgBlobToCommitmentTestExecutor extends KzgTestExecutor {

@Override
public void runTestImpl(final TestDefinition testDefinition) throws Throwable {
final Data data = loadDataFile(testDefinition, Data.class);
final KZGCommitment expectedKzgCommitment = data.getOutput();
KZGCommitment actualKzgCommitment;
try {
final Bytes blob = data.getInput().getBlob();
actualKzgCommitment = CKZG4844.getInstance().blobToKzgCommitment(blob);
} catch (RuntimeException e) {
actualKzgCommitment = null;
}
assertThat(actualKzgCommitment).isEqualTo(expectedKzgCommitment);
}

private static class Data {
@JsonProperty(value = "input", required = true)
private Input input;

@JsonProperty(value = "output", required = true)
private String output;

public Input getInput() {
return input;
}

public KZGCommitment getOutput() {
return output == null ? null : KZGCommitment.fromHexString(output);
}

private static class Input {
@JsonProperty(value = "blob", required = true)
private String blob;

public Bytes getBlob() {
return Bytes.fromHexString(blob);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright Consensys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.reference.phase0.kzg;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.tuweni.bytes.Bytes;
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.kzg.KZGCommitment;
import tech.pegasys.teku.kzg.KZGProof;
import tech.pegasys.teku.kzg.ckzg4844.CKZG4844;

public class KzgComputeBlobProofTestExecutor extends KzgTestExecutor {

@Override
public void runTestImpl(final TestDefinition testDefinition) throws Throwable {
final Data data = loadDataFile(testDefinition, Data.class);
final KZGProof expectedKzgProof = data.getOutput();
KZGProof actualKzgProof;
try {
final Bytes blob = data.getInput().getBlob();
final KZGCommitment commitment = data.getInput().getCommitment();
actualKzgProof = CKZG4844.getInstance().computeBlobKzgProof(blob, commitment);
} catch (RuntimeException e) {
actualKzgProof = null;
}
assertThat(actualKzgProof).isEqualTo(expectedKzgProof);
}

private static class Data {
@JsonProperty(value = "input", required = true)
private Input input;

@JsonProperty(value = "output", required = true)
private String output;

public Input getInput() {
return input;
}

public KZGProof getOutput() {
return output == null ? null : KZGProof.fromHexString(output);
}

private static class Input {
@JsonProperty(value = "blob", required = true)
private String blob;

@JsonProperty(value = "commitment", required = true)
private String commitment;

public Bytes getBlob() {
return Bytes.fromHexString(blob);
}

public KZGCommitment getCommitment() {
return KZGCommitment.fromHexString(commitment);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright Consensys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.reference.phase0.kzg;

import static tech.pegasys.teku.ethtests.finder.KzgTestFinder.KZG_DATA_FILE;

import java.io.IOException;
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.kzg.KZG;
import tech.pegasys.teku.kzg.ckzg4844.CKZG4844;
import tech.pegasys.teku.networks.Eth2NetworkConfiguration;
import tech.pegasys.teku.reference.TestDataUtils;
import tech.pegasys.teku.reference.TestExecutor;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;

public abstract class KzgTestExecutor implements TestExecutor {
// We don't support config reloading and all tests are currently for mainnet
private static final String CONFIG_NAME = "mainnet";

@Override
public final void runTest(TestDefinition testDefinition) throws Throwable {
final Eth2NetworkConfiguration networkConfig =
Eth2NetworkConfiguration.builder(CONFIG_NAME).build();
final SpecConfigDeneb specConfigDeneb =
SpecConfigDeneb.required(networkConfig.getSpec().getGenesisSpecConfig());

KZG kzg = null;
try {
kzg = CKZG4844.createInstance(specConfigDeneb.getFieldElementsPerBlob());
kzg.loadTrustedSetup(specConfigDeneb.getTrustedSetupPath().orElseThrow());
runTestImpl(testDefinition);
} finally {
if (kzg != null) {
kzg.freeTrustedSetup();
}
}
}

protected <T> T loadDataFile(final TestDefinition testDefinition, final Class<T> type)
throws IOException {
String dataFile =
testDefinition.getTestName().endsWith(".yaml")
? testDefinition.getTestName()
: KZG_DATA_FILE;
return TestDataUtils.loadYaml(testDefinition, dataFile, type);
}

protected abstract void runTestImpl(TestDefinition testDefinition) throws Throwable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Consensys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.reference.phase0.kzg;

import com.google.common.collect.ImmutableMap;
import tech.pegasys.teku.reference.TestExecutor;

public class KzgTests {

public static final ImmutableMap<String, TestExecutor> KZG_TEST_TYPES =
ImmutableMap.<String, TestExecutor>builder()
.put("kzg/blob_to_kzg_commitment", new KzgBlobToCommitmentTestExecutor())
.put("kzg/compute_blob_kzg_proof", new KzgComputeBlobProofTestExecutor())
// no KZG interface on CL side, EL responsibility
.put("kzg/compute_kzg_proof", TestExecutor.IGNORE_TESTS)
// actually uses verify_blob_kzg_proof_batch KZG interface
.put("kzg/verify_blob_kzg_proof", new KzgVerifyBlobProofTestExecutor())
.put("kzg/verify_blob_kzg_proof_batch", new KzgVerifyBlobProofBatchTestExecutor())
// no KZG interface on CL side, EL responsibility
.put("kzg/verify_kzg_proof", TestExecutor.IGNORE_TESTS)
.build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright Consensys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.reference.phase0.kzg;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import org.apache.tuweni.bytes.Bytes;
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.kzg.KZGCommitment;
import tech.pegasys.teku.kzg.KZGProof;
import tech.pegasys.teku.kzg.ckzg4844.CKZG4844;

public class KzgVerifyBlobProofBatchTestExecutor extends KzgTestExecutor {

@Override
public void runTestImpl(final TestDefinition testDefinition) throws Throwable {
final Data data = loadDataFile(testDefinition, Data.class);
final Boolean expectedVerificationResult = data.getOutput();
Boolean actualVerificationResult;
try {
final List<Bytes> blobs = data.getInput().getBlobs();
final List<KZGCommitment> commitments = data.getInput().getCommitments();
final List<KZGProof> proofs = data.getInput().getProofs();
actualVerificationResult =
CKZG4844.getInstance().verifyBlobKzgProofBatch(blobs, commitments, proofs);
} catch (RuntimeException e) {
actualVerificationResult = null;
}
assertThat(actualVerificationResult).isEqualTo(expectedVerificationResult);
}

private static class Data {
@JsonProperty(value = "input", required = true)
private Input input;

@JsonProperty(value = "output", required = true)
private Boolean output;

public Input getInput() {
return input;
}

public Boolean getOutput() {
return output;
}

private static class Input {
@JsonProperty(value = "blobs", required = true)
private List<String> blobs;

@JsonProperty(value = "commitments", required = true)
private List<String> commitments;

@JsonProperty(value = "proofs", required = true)
private List<String> proofs;

public List<Bytes> getBlobs() {
return blobs.stream().map(Bytes::fromHexString).toList();
}

public List<KZGCommitment> getCommitments() {
return commitments.stream().map(KZGCommitment::fromHexString).toList();
}

public List<KZGProof> getProofs() {
return proofs.stream().map(KZGProof::fromHexString).toList();
}
}
}
}
Loading

0 comments on commit 0905125

Please sign in to comment.