openvm_stark_backend/
config.rs

1//! [StarkGenericConfig](config::StarkGenericConfig) and associated types. Originally taken from
2//! Plonky3 under MIT license.
3
4use std::marker::PhantomData;
5
6use p3_challenger::{CanObserve, CanSample, FieldChallenger, GrindingChallenger};
7use p3_commit::{Pcs, PolynomialSpace};
8use p3_field::{ExtensionField, Field};
9
10use crate::interaction::RapPhaseSeq;
11
12/// Based on [p3_uni_stark::StarkGenericConfig].
13pub trait StarkGenericConfig
14where
15    Domain<Self>: Send + Sync,
16    Com<Self>: Send + Sync,
17    PcsProof<Self>: Send + Sync,
18    PcsProverData<Self>: Send + Sync,
19    RapPhaseSeqPartialProof<Self>: Send + Sync,
20    RapPartialProvingKey<Self>: Send + Sync,
21{
22    /// The PCS used to commit to trace polynomials.
23    type Pcs: Pcs<Self::Challenge, Self::Challenger>;
24
25    /// The RAP challenge phases used to establish, e.g., that interactions are balanced.
26    type RapPhaseSeq: RapPhaseSeq<Val<Self>, Self::Challenge, Self::Challenger>;
27
28    /// The field from which most random challenges are drawn.
29    type Challenge: ExtensionField<Val<Self>> + Send + Sync;
30
31    /// The challenger (Fiat-Shamir) implementation used.
32    type Challenger: FieldChallenger<Val<Self>>
33        + CanObserve<<Self::Pcs as Pcs<Self::Challenge, Self::Challenger>>::Commitment>
34        + CanSample<Self::Challenge>
35        + GrindingChallenger<Witness = Val<Self>>;
36
37    fn pcs(&self) -> &Self::Pcs;
38
39    fn rap_phase_seq(&self) -> &Self::RapPhaseSeq;
40
41    fn deep_ali_params(&self) -> DeepAliParameters;
42}
43
44pub type Val<SC> = <Domain<SC> as PolynomialSpace>::Val;
45
46pub type Com<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
47    <SC as StarkGenericConfig>::Challenge,
48    <SC as StarkGenericConfig>::Challenger,
49>>::Commitment;
50
51pub type PcsProverData<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
52    <SC as StarkGenericConfig>::Challenge,
53    <SC as StarkGenericConfig>::Challenger,
54>>::ProverData;
55
56pub type PcsProof<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
57    <SC as StarkGenericConfig>::Challenge,
58    <SC as StarkGenericConfig>::Challenger,
59>>::Proof;
60
61pub type PcsError<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
62    <SC as StarkGenericConfig>::Challenge,
63    <SC as StarkGenericConfig>::Challenger,
64>>::Error;
65
66pub type Domain<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
67    <SC as StarkGenericConfig>::Challenge,
68    <SC as StarkGenericConfig>::Challenger,
69>>::Domain;
70
71pub type RapPhaseSeqPartialProof<SC> = <<SC as StarkGenericConfig>::RapPhaseSeq as RapPhaseSeq<
72    Val<SC>,
73    <SC as StarkGenericConfig>::Challenge,
74    <SC as StarkGenericConfig>::Challenger,
75>>::PartialProof;
76
77pub type RapPartialProvingKey<SC> = <<SC as StarkGenericConfig>::RapPhaseSeq as RapPhaseSeq<
78    Val<SC>,
79    <SC as StarkGenericConfig>::Challenge,
80    <SC as StarkGenericConfig>::Challenger,
81>>::PartialProvingKey;
82
83pub type RapPhaseSeqError<SC> = <<SC as StarkGenericConfig>::RapPhaseSeq as RapPhaseSeq<
84    Val<SC>,
85    <SC as StarkGenericConfig>::Challenge,
86    <SC as StarkGenericConfig>::Challenger,
87>>::Error;
88
89pub type PackedVal<SC> = <Val<SC> as Field>::Packing;
90
91pub type PackedChallenge<SC> =
92    <<SC as StarkGenericConfig>::Challenge as ExtensionField<Val<SC>>>::ExtensionPacking;
93
94#[derive(Debug)]
95pub struct StarkConfig<Pcs, RapPhaseSeq, Challenge, Challenger> {
96    pcs: Pcs,
97    _challenger: Challenger,
98    rap_phase: RapPhaseSeq,
99    deep_ali_params: DeepAliParameters,
100    _phantom: PhantomData<(Challenge, Challenger)>,
101}
102
103impl<Pcs, RapPhaseSeq, Challenge, Challenger> StarkConfig<Pcs, RapPhaseSeq, Challenge, Challenger> {
104    pub const fn new(
105        pcs: Pcs,
106        _challenger: Challenger,
107        rap_phase: RapPhaseSeq,
108        deep_ali_params: DeepAliParameters,
109    ) -> Self {
110        Self {
111            pcs,
112            _challenger,
113            rap_phase,
114            deep_ali_params,
115            _phantom: PhantomData,
116        }
117    }
118}
119
120impl<Pcs, Rps, Challenge, Challenger> StarkGenericConfig
121    for StarkConfig<Pcs, Rps, Challenge, Challenger>
122where
123    Challenge: ExtensionField<<Pcs::Domain as PolynomialSpace>::Val>,
124    Pcs: p3_commit::Pcs<Challenge, Challenger>,
125    Pcs::Domain: Send + Sync,
126    Pcs::Commitment: Send + Sync,
127    Pcs::ProverData: Send + Sync,
128    Pcs::Proof: Send + Sync,
129    Rps: RapPhaseSeq<<Pcs::Domain as PolynomialSpace>::Val, Challenge, Challenger>,
130    Rps::PartialProof: Send + Sync,
131    Rps::PartialProvingKey: Send + Sync,
132    Challenger: FieldChallenger<<Pcs::Domain as PolynomialSpace>::Val>
133        + CanObserve<<Pcs as p3_commit::Pcs<Challenge, Challenger>>::Commitment>
134        + CanSample<Challenge>
135        + GrindingChallenger<Witness = <Pcs::Domain as PolynomialSpace>::Val>,
136{
137    type Pcs = Pcs;
138    type RapPhaseSeq = Rps;
139    type Challenge = Challenge;
140    type Challenger = Challenger;
141
142    fn pcs(&self) -> &Self::Pcs {
143        &self.pcs
144    }
145    fn rap_phase_seq(&self) -> &Self::RapPhaseSeq {
146        &self.rap_phase
147    }
148    fn deep_ali_params(&self) -> DeepAliParameters {
149        self.deep_ali_params
150    }
151}
152
153#[derive(Copy, Clone, Debug, derive_new::new)]
154pub struct DeepAliParameters {
155    /// The number of proof-of-work bits for the DEEP proof-of-work phase.
156    pub deep_pow_bits: usize,
157}