Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kzg reference tests support #7571

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something we want to support in the future? If it is, it would be good to have a ticket for it linked here.

Copy link
Contributor Author

@zilm13 zilm13 Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We may need it not only here, Stefan is already following it for another PR Support Deneb fork choice reference tests #7541
  2. There are no kzg reference tests other than for mainnet at the moment, if they are added we start to fail and will notice it
  3. Currently config type is part of the directory name and extracting it looks ugly, so I removed it and harcoded "mainnet". It's not an issue until minimal are added (see №2)

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
Loading