#![no_std]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
use alloc::{string::ToString, vec::Vec};
use ::serde::Serialize;
use miden_core::{Felt, WORD_SIZE, field::QuadFelt, utils::RowMajorMatrix};
use miden_crypto::stark::{
StarkConfig, air::VarLenPublicInputs, challenger::CanObserve, lmcs::Lmcs, proof::StarkOutput,
};
use miden_processor::{
FastProcessor, Program,
trace::{ExecutionTrace, build_trace},
};
use tracing::instrument;
mod proving_options;
pub use miden_air::{DeserializationError, ProcessorAir, PublicInputs, config};
pub use miden_core::proof::{ExecutionProof, HashFunction};
pub use miden_processor::{
ExecutionError, ExecutionOptions, ExecutionOutput, FutureMaybeSend, Host, InputError,
ProgramInfo, StackInputs, StackOutputs, SyncHost, TraceBuildInputs, TraceGenerationContext,
Word, advice::AdviceInputs, crypto, field, serde, utils,
};
pub use proving_options::ProvingOptions;
#[derive(Debug)]
pub struct TraceProvingInputs {
trace_inputs: TraceBuildInputs,
options: ProvingOptions,
}
impl TraceProvingInputs {
pub fn new(trace_inputs: TraceBuildInputs, options: ProvingOptions) -> Self {
Self { trace_inputs, options }
}
pub fn into_parts(self) -> (TraceBuildInputs, ProvingOptions) {
(self.trace_inputs, self.options)
}
}
#[instrument("prove_program", skip_all)]
pub async fn prove(
program: &Program,
stack_inputs: StackInputs,
advice_inputs: AdviceInputs,
host: &mut impl Host,
execution_options: ExecutionOptions,
proving_options: ProvingOptions,
) -> Result<(StackOutputs, ExecutionProof), ExecutionError> {
let processor = FastProcessor::new_with_options(stack_inputs, advice_inputs, execution_options)
.map_err(ExecutionError::advice_error_no_context)?;
let trace_inputs = processor.execute_trace_inputs(program, host).await?;
prove_from_trace_sync(TraceProvingInputs::new(trace_inputs, proving_options))
}
#[instrument("prove_program_sync", skip_all)]
pub fn prove_sync(
program: &Program,
stack_inputs: StackInputs,
advice_inputs: AdviceInputs,
host: &mut impl SyncHost,
execution_options: ExecutionOptions,
proving_options: ProvingOptions,
) -> Result<(StackOutputs, ExecutionProof), ExecutionError> {
let processor = FastProcessor::new_with_options(stack_inputs, advice_inputs, execution_options)
.map_err(ExecutionError::advice_error_no_context)?;
let trace_inputs = processor.execute_trace_inputs_sync(program, host)?;
prove_from_trace_sync(TraceProvingInputs::new(trace_inputs, proving_options))
}
#[instrument("prove_trace_sync", skip_all)]
pub fn prove_from_trace_sync(
inputs: TraceProvingInputs,
) -> Result<(StackOutputs, ExecutionProof), ExecutionError> {
let (trace_inputs, options) = inputs.into_parts();
let trace = build_trace(trace_inputs)?;
prove_execution_trace(trace, options)
}
fn prove_execution_trace(
trace: ExecutionTrace,
options: ProvingOptions,
) -> Result<(StackOutputs, ExecutionProof), ExecutionError> {
tracing::event!(
tracing::Level::INFO,
"Generated execution trace of {} columns and {} steps (padded from {})",
miden_air::trace::TRACE_WIDTH,
trace.trace_len_summary().padded_trace_len(),
trace.trace_len_summary().trace_len()
);
let stack_outputs = *trace.stack_outputs();
let precompile_requests = trace.precompile_requests().to_vec();
let hash_fn = options.hash_fn();
let trace_matrix = {
let _span = tracing::info_span!("to_row_major_matrix").entered();
trace.to_row_major_matrix()
};
let (public_values, kernel_felts) = trace.public_inputs().to_air_inputs();
let var_len_public_inputs: &[&[Felt]] = &[&kernel_felts];
let params = config::pcs_params();
let proof_bytes = match hash_fn {
HashFunction::Blake3_256 => {
let config = config::blake3_256_config(params);
prove_stark(&config, &trace_matrix, &public_values, var_len_public_inputs)
},
HashFunction::Keccak => {
let config = config::keccak_config(params);
prove_stark(&config, &trace_matrix, &public_values, var_len_public_inputs)
},
HashFunction::Rpo256 => {
let config = config::rpo_config(params);
prove_stark(&config, &trace_matrix, &public_values, var_len_public_inputs)
},
HashFunction::Poseidon2 => {
let config = config::poseidon2_config(params);
prove_stark(&config, &trace_matrix, &public_values, var_len_public_inputs)
},
HashFunction::Rpx256 => {
let config = config::rpx_config(params);
prove_stark(&config, &trace_matrix, &public_values, var_len_public_inputs)
},
}?;
let proof = ExecutionProof::new(proof_bytes, hash_fn, precompile_requests);
Ok((stack_outputs, proof))
}
pub fn prove_stark<SC>(
config: &SC,
trace: &RowMajorMatrix<Felt>,
public_values: &[Felt],
var_len_public_inputs: VarLenPublicInputs<'_, Felt>,
) -> Result<Vec<u8>, ExecutionError>
where
SC: StarkConfig<Felt, QuadFelt>,
<SC::Lmcs as Lmcs>::Commitment: Serialize,
{
let mut challenger = config.challenger();
config::observe_protocol_params(&mut challenger);
challenger.observe_slice(public_values);
config::observe_var_len_public_inputs(&mut challenger, var_len_public_inputs, &[WORD_SIZE]);
let output: StarkOutput<Felt, QuadFelt, SC> = miden_crypto::stark::prover::prove_single(
config,
&ProcessorAir,
trace,
public_values,
var_len_public_inputs,
&ProcessorAir,
challenger,
)
.map_err(|e| ExecutionError::ProvingError(e.to_string()))?;
let proof_bytes = bincode::serialize(&output.proof)
.map_err(|e| ExecutionError::ProvingError(e.to_string()))?;
Ok(proof_bytes)
}