Skip to content

Commit

Permalink
Improve merkle proof test by verifying miscHelpersDeneb helpers (Cons…
Browse files Browse the repository at this point in the history
  • Loading branch information
zilm13 authored Nov 20, 2023
1 parent a82f964 commit 5ec5d34
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector;
import tech.pegasys.teku.infrastructure.ssz.primitive.SszBytes32;
import tech.pegasys.teku.infrastructure.ssz.schema.collections.SszBytes32VectorSchema;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.reference.TestDataUtils;
import tech.pegasys.teku.reference.TestExecutor;
import tech.pegasys.teku.spec.config.SpecConfigDeneb;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.type.SszKZGCommitment;
import tech.pegasys.teku.spec.logic.common.helpers.Predicates;
import tech.pegasys.teku.spec.logic.versions.deneb.helpers.MiscHelpersDeneb;

public class SingleMerkleProofTestExecutor implements TestExecutor {
private static final Pattern TEST_NAME_PATTERN = Pattern.compile("(.+)/(.+)");
Expand Down Expand Up @@ -90,14 +95,40 @@ void runBeaconBlockBodyTest(

switch (proofType) {
case "blob_kzg_commitment_merkle_proof":
final Bytes32 kzgCommitmentHash = Bytes32.fromHexString(data.leaf);

// Forward check
assertThat(
predicates.isValidMerkleBranch(
Bytes32.fromHexString(data.leaf),
kzgCommitmentHash,
createKzgCommitmentMerkleProofBranchFromData(testDefinition, data.branch),
getKzgCommitmentInclusionProofDepth(testDefinition),
data.leafIndex,
beaconBlockBody.hashTreeRoot()))
.isTrue();

// Find which commitment is in puzzle by hash root
final SszList<SszKZGCommitment> sszKZGCommitments =
beaconBlockBody.getOptionalBlobKzgCommitments().orElseThrow();
Optional<Integer> kzgCommitmentIndexFound = Optional.empty();
for (int i = 0; i < sszKZGCommitments.size(); ++i) {
if (sszKZGCommitments.get(i).hashTreeRoot().equals(kzgCommitmentHash)) {
kzgCommitmentIndexFound = Optional.of(i);
break;
}
}
assertThat(kzgCommitmentIndexFound).isPresent();
final UInt64 kzgCommitmentIndex = UInt64.valueOf(kzgCommitmentIndexFound.get());

// Verify 2 MiscHelpersDeneb helpers
final MiscHelpersDeneb miscHelpersDeneb =
MiscHelpersDeneb.required(testDefinition.getSpec().getGenesisSpec().miscHelpers());
assertThat(miscHelpersDeneb.getBlobSidecarKzgCommitmentGeneralizedIndex(kzgCommitmentIndex))
.isEqualTo(data.leafIndex);
assertThat(
miscHelpersDeneb.computeKzgCommitmentInclusionProof(
kzgCommitmentIndex, beaconBlockBody))
.isEqualTo(data.branch.stream().map(Bytes32::fromHexString).toList());
break;
default:
throw new RuntimeException("Unknown proof type " + proofType);
Expand Down

0 comments on commit 5ec5d34

Please sign in to comment.