-
Notifications
You must be signed in to change notification settings - Fork 165
feat: block kernel skeleton #3703
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
61cb5d2
feat: block kernel skeleton
claude f207f51
chore: apply changelog and masm conventions
claude c265890
fix: gate prove_dummy on the testing feature alone
claude 660b588
chore: upate block kernel main format
Fumuran 6f5f128
chore: split the changelog entry by breaking scope
claude b5903af
chore: trim doc comments
Fumuran b5b2a07
refactor: replace BlockProof with ExecutionProof
claude 7904a18
refactor: remove BlockKernel::build_advice_inputs, impl SequentialCom…
Fumuran e5f127c
chore: rename batch and block output files to batch_outputs and block…
Fumuran 7f1fa75
refactor: update BlockOutputError only variant
Fumuran b7d01fa
tests: remove meaningless test
Fumuran 64b1d55
chore: fix doc and clippy errors
Fumuran edd1197
Merge branch 'next' into fumuran-claude/block-kernel-skeleton
Fumuran 00df961
chore: link changelog entries to the PR
claude 3d05a00
Merge remote-tracking branch 'origin/next' into fumuran-claude/block-…
claude c64d71c
Merge branch 'next' into fumuran-claude/block-kernel-skeleton
mmagician File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| use miden_processor::{DefaultHost, ExecutionError, ExecutionOptions, FastProcessor}; | ||
| use miden_protocol::block::{BlockKernel, BlockOutputs, ProposedBlock}; | ||
|
|
||
| use crate::{BlockProverError, ExecutedBlock}; | ||
|
|
||
| // BLOCK EXECUTOR | ||
| // ================================================================================================ | ||
|
|
||
| /// Executes the block kernel over a [`ProposedBlock`], producing an [`ExecutedBlock`]. | ||
| #[derive(Clone, Default)] | ||
| pub struct BlockExecutor; | ||
|
|
||
| impl BlockExecutor { | ||
| /// Creates a new [`BlockExecutor`] instance. | ||
| pub fn new() -> Self { | ||
| Self | ||
| } | ||
|
|
||
| /// Runs the block kernel over the [`ProposedBlock`], returning an [`ExecutedBlock`] that can be | ||
| /// passed to [`LocalBlockProver::prove`](crate::LocalBlockProver::prove). | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// - the block kernel program fails to execute; | ||
| /// - the kernel output stack fails to parse. | ||
| pub fn execute( | ||
| &self, | ||
| proposed_block: ProposedBlock, | ||
| ) -> Result<ExecutedBlock, BlockProverError> { | ||
| let (stack_inputs, advice_inputs) = BlockKernel::prepare_inputs(&proposed_block); | ||
|
|
||
| let processor = FastProcessor::new_with_options( | ||
| stack_inputs, | ||
| advice_inputs, | ||
| ExecutionOptions::default(), | ||
| ) | ||
| .map_err(ExecutionError::advice_error_no_context) | ||
| .map_err(BlockProverError::BlockKernelExecutionFailed)?; | ||
|
|
||
| let trace_inputs = processor | ||
| .execute_trace_inputs_sync(&BlockKernel::main(), &mut DefaultHost::default()) | ||
| .map_err(BlockProverError::BlockKernelExecutionFailed)?; | ||
|
|
||
| // Parse and validate the output stack shape (padding cells are zero); the actual output | ||
| // values themselves are not checked until the kernel computes them. | ||
| let block_outputs = BlockOutputs::parse(trace_inputs.stack_outputs()) | ||
| .map_err(BlockProverError::BlockKernelOutputInvalid)?; | ||
|
|
||
| Ok(ExecutedBlock::new(proposed_block, trace_inputs, block_outputs)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| use miden_processor::ExecutionError; | ||
| use miden_protocol::errors::BlockOutputError; | ||
|
|
||
| // BLOCK PROVER ERROR | ||
| // ================================================================================================ | ||
|
|
||
| /// Represents errors that can occur during block proving. | ||
| /// | ||
| /// NOTE: Block proving is not yet implemented. This is a placeholder enum. | ||
| /// Represents errors that can occur during block execution and proving. | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum BlockProverError {} | ||
| pub enum BlockProverError { | ||
| #[error("block kernel execution failed")] | ||
| BlockKernelExecutionFailed(#[source] ExecutionError), | ||
| #[error("block kernel produced an invalid output stack")] | ||
| BlockKernelOutputInvalid(#[source] BlockOutputError), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| use miden_processor::TraceBuildInputs; | ||
| use miden_protocol::block::{BlockOutputs, ProposedBlock}; | ||
|
|
||
| // EXECUTED BLOCK | ||
| // ================================================================================================ | ||
|
|
||
| /// A [`ProposedBlock`] whose block kernel has been executed, but not yet proven. | ||
| /// | ||
| /// Produced by [`BlockExecutor::execute`](crate::BlockExecutor::execute) and consumed by | ||
| /// [`LocalBlockProver::prove`](crate::LocalBlockProver::prove). It carries the executed block's | ||
| /// trace inputs so that proving only needs to build the trace and generate the proof. | ||
| pub struct ExecutedBlock { | ||
| proposed_block: ProposedBlock, | ||
| trace_inputs: TraceBuildInputs, | ||
| block_outputs: BlockOutputs, | ||
| } | ||
|
|
||
| impl ExecutedBlock { | ||
| /// Creates a new [`ExecutedBlock`] from the proposed block, the trace inputs and the public | ||
| /// outputs produced by executing the block kernel over it. | ||
| pub(crate) fn new( | ||
| proposed_block: ProposedBlock, | ||
| trace_inputs: TraceBuildInputs, | ||
| block_outputs: BlockOutputs, | ||
| ) -> Self { | ||
| Self { | ||
| proposed_block, | ||
| trace_inputs, | ||
| block_outputs, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the [`ProposedBlock`] this block was executed from. | ||
| pub fn proposed_block(&self) -> &ProposedBlock { | ||
| &self.proposed_block | ||
| } | ||
|
|
||
| /// Returns the public outputs produced by the block kernel. | ||
| pub fn block_outputs(&self) -> &BlockOutputs { | ||
| &self.block_outputs | ||
| } | ||
|
|
||
| /// Consumes the executed block, returning the trace inputs needed to prove it. | ||
| pub(crate) fn into_trace_inputs(self) -> TraceBuildInputs { | ||
| self.trace_inputs | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| mod local_block_prover; | ||
| pub use local_block_prover::LocalBlockProver; | ||
| mod block_executor; | ||
| pub use block_executor::BlockExecutor; | ||
|
|
||
| mod errors; | ||
| pub use errors::BlockProverError; | ||
|
|
||
| mod executed_block; | ||
| pub use executed_block::ExecutedBlock; | ||
|
|
||
| mod local_block_prover; | ||
| pub use local_block_prover::LocalBlockProver; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,63 @@ | ||
| use miden_protocol::batch::OrderedBatches; | ||
| use miden_protocol::block::{BlockHeader, BlockInputs, BlockProof}; | ||
| use miden_protocol::block::BlockProof; | ||
| use miden_prover::{ProvingOptions, TraceProvingInputs, prove_from_trace_sync}; | ||
|
|
||
| use crate::BlockProverError; | ||
| use crate::{BlockProverError, ExecutedBlock}; | ||
|
|
||
| // LOCAL BLOCK PROVER | ||
| // ================================================================================================ | ||
|
|
||
| /// A local prover for blocks in the chain. | ||
| #[derive(Clone)] | ||
| pub struct LocalBlockProver {} | ||
| /// | ||
| /// Proves an [`ExecutedBlock`] produced by [`BlockExecutor`](crate::BlockExecutor) into a | ||
| /// [`BlockProof`] over the block's public commitments. | ||
| /// | ||
| /// # Warning | ||
| /// | ||
| /// The current block kernel is a skeleton that drops its inputs and emits an all-zero output | ||
| /// region, so the produced proof attests only that the kernel program ran over the block's | ||
| /// `[PREV_BLOCK_COMMITMENT, BATCHES_COMMITMENT]` public inputs. It does **not** yet bind the | ||
| /// block's account updates, notes or nullifiers, so a block whose contents were mutated would | ||
| /// still carry a valid proof. This must therefore not be relied on at a trust boundary until the | ||
| /// kernel verification logic that emits and binds the real commitments lands. | ||
| #[derive(Clone, Default)] | ||
| pub struct LocalBlockProver { | ||
| proving_options: ProvingOptions, | ||
| } | ||
|
|
||
| impl LocalBlockProver { | ||
| /// Creates a new [`LocalBlockProver`] instance. | ||
| pub fn new(_proof_security_level: u32) -> Self { | ||
| // TODO: This will eventually take the security level as a parameter, but until we verify | ||
| // batches it is ignored. | ||
| Self {} | ||
| // blocks it is ignored. | ||
| Self::default() | ||
| } | ||
|
|
||
| /// Generates a proof of a block in the chain based on the given header and inputs. | ||
| /// Proves the [`ExecutedBlock`] into a [`BlockProof`]. | ||
| /// | ||
| /// Builds the execution trace from the executed block and generates the proof. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// NOTE: Block proving is not yet implemented. This is a placeholder struct. | ||
| pub fn prove( | ||
| &self, | ||
| _tx_batches: OrderedBatches, | ||
| _block_header: &BlockHeader, | ||
| _block_inputs: BlockInputs, | ||
| ) -> Result<BlockProof, BlockProverError> { | ||
| Ok(BlockProof {}) | ||
| /// Returns an error if proof generation fails. | ||
| pub fn prove(&self, executed_block: ExecutedBlock) -> Result<BlockProof, BlockProverError> { | ||
| let trace_inputs = executed_block.into_trace_inputs(); | ||
|
|
||
| let (_stack_outputs, proof) = prove_from_trace_sync(TraceProvingInputs::new( | ||
| trace_inputs, | ||
| self.proving_options.clone(), | ||
| )) | ||
| .map_err(BlockProverError::BlockKernelExecutionFailed)?; | ||
|
|
||
| Ok(BlockProof::new(proof)) | ||
| } | ||
|
|
||
| /// A mock implementation of the execution of a proof of a block in the chain based on the given | ||
| /// header and inputs. | ||
| /// Returns a [`BlockProof`] carrying a dummy execution proof, without running the block kernel. | ||
| /// | ||
| /// This is exposed for testing purposes. | ||
| #[cfg(any(feature = "testing", test))] | ||
| pub fn prove_dummy( | ||
| &self, | ||
| _tx_batches: OrderedBatches, | ||
| _block_header: BlockHeader, | ||
| _block_inputs: BlockInputs, | ||
| ) -> Result<BlockProof, BlockProverError> { | ||
| Ok(BlockProof {}) | ||
| /// This is exposed for testing purposes. It is gated on the `testing` feature alone rather | ||
| /// than also on `cfg(test)`, because [`BlockProof::new_dummy`] requires `miden-protocol`'s own | ||
| /// `testing` feature, which only this crate's `testing` feature turns on. | ||
| #[cfg(feature = "testing")] | ||
| pub fn prove_dummy(&self) -> BlockProof { | ||
| BlockProof::new_dummy() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [package] | ||
| name = "miden-block-kernel" | ||
| version.workspace = true | ||
|
|
||
| [[bin]] | ||
| path = "src/main.masm" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # MAIN | ||
| # ================================================================================================= | ||
|
|
||
| #! Block kernel program (skeleton). | ||
| #! | ||
| #! A block aggregates a set of independently-proven transaction batches into the next block of the | ||
| #! chain. This program defines the public input/output contract that the block kernel will | ||
| #! eventually verify, but currently does not yet perform any verification: it drops its inputs and | ||
| #! exits, leaving the all-zero word output region as the stack's initial padding zeros. | ||
| #! | ||
| #! Inputs: [PREV_BLOCK_COMMITMENT, BATCHES_COMMITMENT, pad(8)] | ||
| #! Outputs: [BLOCK_COMMITMENT, NULLIFIER_COMMITMENT, pad(8)] | ||
| #! | ||
| #! Where: | ||
| #! - PREV_BLOCK_COMMITMENT is the commitment of the block header this block builds on top of. | ||
| #! - BATCHES_COMMITMENT is the sequential hash over the `BatchId`s of the batches in this block. It | ||
| #! pins both which batches the block contains and the order they appear in. | ||
| #! - BLOCK_COMMITMENT is the commitment of the newly created block header, which in turn | ||
| #! commits to the block's account root, nullifier root, note root, chain commitment and | ||
| #! transaction commitment. In this skeleton it is the empty word. | ||
| #! - NULLIFIER_COMMITMENT is the commitment to the set of nullifiers created in this block. In | ||
| #! this skeleton it is the empty word. | ||
| #! | ||
| proc main | ||
| dropw dropw | ||
| # => [pad(16)] | ||
| end | ||
|
|
||
| begin | ||
| exec.main | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.