openvm_stark_backend/keygen/
types.rsuse std::sync::Arc;
use derivative::Derivative;
use p3_matrix::dense::RowMajorMatrix;
use serde::{Deserialize, Serialize};
use crate::{
air_builders::symbolic::SymbolicConstraints,
config::{Com, PcsProverData, RapPhaseSeqProvingKey, StarkGenericConfig, Val},
interaction::RapPhaseSeqKind,
};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TraceWidth {
pub preprocessed: Option<usize>,
pub cached_mains: Vec<usize>,
pub common_main: usize,
pub after_challenge: Vec<usize>,
}
impl TraceWidth {
pub fn main_widths(&self) -> Vec<usize> {
let mut ret = self.cached_mains.clone();
if self.common_main != 0 {
ret.push(self.common_main);
}
ret
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StarkVerifyingParams {
pub width: TraceWidth,
pub num_public_values: usize,
pub num_exposed_values_after_challenge: Vec<usize>,
pub num_challenges_to_sample: Vec<usize>,
}
#[derive(Derivative, Serialize, Deserialize)]
#[derivative(Clone(bound = "Com<SC>: Clone"))]
#[serde(bound(
serialize = "Com<SC>: Serialize",
deserialize = "Com<SC>: Deserialize<'de>"
))]
pub struct StarkVerifyingKey<SC: StarkGenericConfig> {
pub preprocessed_data: Option<VerifierSinglePreprocessedData<SC>>,
pub params: StarkVerifyingParams,
pub symbolic_constraints: SymbolicConstraints<Val<SC>>,
pub quotient_degree: usize,
pub rap_phase_seq_kind: RapPhaseSeqKind,
}
#[derive(Derivative, Serialize, Deserialize)]
#[derivative(Clone(bound = "Com<SC>: Clone"))]
#[serde(bound(
serialize = "Com<SC>: Serialize",
deserialize = "Com<SC>: Deserialize<'de>"
))]
pub struct MultiStarkVerifyingKey<SC: StarkGenericConfig> {
pub per_air: Vec<StarkVerifyingKey<SC>>,
}
#[derive(Serialize, Deserialize, Derivative)]
#[derivative(Clone(bound = "Com<SC>: Clone"))]
#[serde(bound(
serialize = "PcsProverData<SC>: Serialize",
deserialize = "PcsProverData<SC>: Deserialize<'de>"
))]
pub struct StarkProvingKey<SC: StarkGenericConfig> {
pub air_name: String,
pub vk: StarkVerifyingKey<SC>,
pub preprocessed_data: Option<ProverOnlySinglePreprocessedData<SC>>,
pub rap_phase_seq_pk: RapPhaseSeqProvingKey<SC>,
}
#[derive(Serialize, Deserialize, Derivative)]
#[derivative(Clone(bound = "Com<SC>: Clone"))]
#[serde(bound(
serialize = "PcsProverData<SC>: Serialize",
deserialize = "PcsProverData<SC>: Deserialize<'de>"
))]
pub struct MultiStarkProvingKey<SC: StarkGenericConfig> {
pub per_air: Vec<StarkProvingKey<SC>>,
pub max_constraint_degree: usize,
}
impl<SC: StarkGenericConfig> StarkVerifyingKey<SC> {
pub fn num_cached_mains(&self) -> usize {
self.params.width.cached_mains.len()
}
pub fn has_common_main(&self) -> bool {
self.params.width.common_main != 0
}
pub fn has_interaction(&self) -> bool {
!self.symbolic_constraints.interactions.is_empty()
}
}
impl<SC: StarkGenericConfig> MultiStarkProvingKey<SC> {
pub fn get_vk(&self) -> MultiStarkVerifyingKey<SC> {
MultiStarkVerifyingKey {
per_air: self.per_air.iter().map(|pk| pk.vk.clone()).collect(),
}
}
}
impl<SC: StarkGenericConfig> MultiStarkVerifyingKey<SC> {
pub fn num_challenges_per_phase(&self) -> Vec<usize> {
self.full_view().num_challenges_per_phase()
}
}
#[derive(Serialize, Deserialize, Derivative)]
#[derivative(Clone(bound = "Com<SC>: Clone"))]
#[serde(bound(
serialize = "PcsProverData<SC>: Serialize",
deserialize = "PcsProverData<SC>: Deserialize<'de>"
))]
pub struct ProverOnlySinglePreprocessedData<SC: StarkGenericConfig> {
pub trace: RowMajorMatrix<Val<SC>>,
pub data: Arc<PcsProverData<SC>>,
}
#[derive(Derivative, Serialize, Deserialize)]
#[derivative(Clone(bound = "Com<SC>: Clone"))]
#[serde(bound(
serialize = "Com<SC>: Serialize",
deserialize = "Com<SC>: Deserialize<'de>"
))]
pub struct VerifierSinglePreprocessedData<SC: StarkGenericConfig> {
pub commit: Com<SC>,
}