Skip to content

Commit

Permalink
relocate verify merkle branch
Browse files Browse the repository at this point in the history
rejig
  • Loading branch information
g11tech committed Nov 21, 2023
1 parent d072280 commit 860784d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,7 @@ export const INTERVALS_PER_SLOT = 3;
export const BYTES_PER_FIELD_ELEMENT = 32;
export const BLOB_TX_TYPE = 0x03;
export const VERSIONED_HASH_VERSION_KZG = 0x01;

// ssz.deneb.BeaconBlockBody.getPathInfo(['blobKzgCommitments',0]).gindex
export const KZG_COMMITMENT_GINDEX0 = ACTIVE_PRESET === PresetName.minimal ? 864 : 221184;
export const KZG_COMMITMENT_SUBTREE_INDEX0 = KZG_COMMITMENT_GINDEX0 - 2 ** KZG_COMMITMENT_INCLUSION_PROOF_DEPTH;
19 changes: 12 additions & 7 deletions packages/state-transition/src/util/blobs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SHA256 from "@chainsafe/as-sha256";
import {VERSIONED_HASH_VERSION_KZG} from "@lodestar/params";
import {Tree} from "@chainsafe/persistent-merkle-tree";
import {VERSIONED_HASH_VERSION_KZG, KZG_COMMITMENT_GINDEX0, ForkName} from "@lodestar/params";
import {deneb, ssz, allForks} from "@lodestar/types";
import {ChainForkConfig} from "@lodestar/config";

Expand All @@ -15,17 +16,19 @@ export function kzgCommitmentToVersionedHash(kzgCommitment: deneb.KZGCommitment)
}

export function computeInclusionProof(
_signedBlock: allForks.SignedBeaconBlock,
_blobsData: {blob: deneb.Blob; kzgProof: deneb.KZGProof; kzgCommitment: deneb.KZGCommitment}
fork: ForkName,
body: allForks.BeaconBlockBody,
index: number
): deneb.KzgCommitmentInclusionProof {
// TODO PR correctly implement the inclusion proof
return ssz.deneb.KzgCommitmentInclusionProof.defaultValue();
const bodyView = (ssz[fork].BeaconBlockBody as allForks.AllForksSSZTypes["BeaconBlockBody"]).toView(body);
const commitmentGindex = KZG_COMMITMENT_GINDEX0 + index;
return new Tree(bodyView.node).getSingleProof(BigInt(commitmentGindex));
}

export function computeBlobSidecars(
config: ChainForkConfig,
signedBlock: allForks.SignedBeaconBlock,
contents: deneb.Contents
contents: deneb.Contents & {kzgCommitmentInclusionProofs?: deneb.KzgCommitmentInclusionProof[]}
): deneb.BlobSidecars {
const blobKzgCommitments = (signedBlock as deneb.SignedBeaconBlock).message.body.blobKzgCommitments;
if (blobKzgCommitments === undefined) {
Expand All @@ -34,11 +37,13 @@ export function computeBlobSidecars(

const blockHeader = blockToHeader(config, signedBlock.message);
const signedBlockHeader = {message: blockHeader, signature: signedBlock.signature};
const fork = config.getForkName(blockHeader.slot);

return blobKzgCommitments.map((kzgCommitment, index) => {
const blob = contents.blobs[index];
const kzgProof = contents.kzgProofs[index];
const kzgCommitmentInclusionProof = computeInclusionProof(signedBlock, {blob, kzgProof, kzgCommitment});
const kzgCommitmentInclusionProof =
contents.kzgCommitmentInclusionProofs?.[index] ?? computeInclusionProof(fork, signedBlock.message.body, index);

return {index, blob, kzgCommitment, kzgProof, signedBlockHeader, kzgCommitmentInclusionProof};
});
Expand Down
1 change: 1 addition & 0 deletions packages/state-transition/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from "./slot.js";
export * from "./syncCommittee.js";
export * from "./validator.js";
export * from "./weakSubjectivity.js";
export * from "./verifyMerkleBranch.js";

0 comments on commit 860784d

Please sign in to comment.