openvm_stark_backend/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! [StarkGenericConfig](config::StarkGenericConfig) and associated types. Originally taken from Plonky3 under MIT license.

use std::marker::PhantomData;

use p3_challenger::{CanObserve, CanSample, FieldChallenger};
use p3_commit::{Pcs, PolynomialSpace};
use p3_field::{ExtensionField, Field};

use crate::interaction::RapPhaseSeq;

/// Based on [p3_uni_stark::StarkGenericConfig].
pub trait StarkGenericConfig
where
    Domain<Self>: Send + Sync,
    Com<Self>: Send + Sync,
    PcsProof<Self>: Send + Sync,
    PcsProverData<Self>: Send + Sync,
{
    /// The PCS used to commit to trace polynomials.
    type Pcs: Pcs<Self::Challenge, Self::Challenger>;

    /// The RAP challenge phases used to establish, e.g., that interactions are balanced.
    type RapPhaseSeq: RapPhaseSeq<Val<Self>, Self::Challenge, Self::Challenger>;

    /// The field from which most random challenges are drawn.
    type Challenge: ExtensionField<Val<Self>> + Send + Sync;

    /// The challenger (Fiat-Shamir) implementation used.
    type Challenger: FieldChallenger<Val<Self>>
        + CanObserve<<Self::Pcs as Pcs<Self::Challenge, Self::Challenger>>::Commitment>
        + CanSample<Self::Challenge>;

    fn pcs(&self) -> &Self::Pcs;

    fn rap_phase_seq(&self) -> &Self::RapPhaseSeq;
}

pub type Val<SC> = <<<SC as StarkGenericConfig>::Pcs as Pcs<
    <SC as StarkGenericConfig>::Challenge,
    <SC as StarkGenericConfig>::Challenger,
>>::Domain as PolynomialSpace>::Val;

pub type Com<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
    <SC as StarkGenericConfig>::Challenge,
    <SC as StarkGenericConfig>::Challenger,
>>::Commitment;

pub type PcsProverData<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
    <SC as StarkGenericConfig>::Challenge,
    <SC as StarkGenericConfig>::Challenger,
>>::ProverData;

pub type PcsProof<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
    <SC as StarkGenericConfig>::Challenge,
    <SC as StarkGenericConfig>::Challenger,
>>::Proof;

pub type PcsError<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
    <SC as StarkGenericConfig>::Challenge,
    <SC as StarkGenericConfig>::Challenger,
>>::Error;

pub type Domain<SC> = <<SC as StarkGenericConfig>::Pcs as Pcs<
    <SC as StarkGenericConfig>::Challenge,
    <SC as StarkGenericConfig>::Challenger,
>>::Domain;

pub type RapPhaseSeqPartialProof<SC> = <<SC as StarkGenericConfig>::RapPhaseSeq as RapPhaseSeq<
    Val<SC>,
    <SC as StarkGenericConfig>::Challenge,
    <SC as StarkGenericConfig>::Challenger,
>>::PartialProof;

pub type RapPhaseSeqProvingKey<SC> = <<SC as StarkGenericConfig>::RapPhaseSeq as RapPhaseSeq<
    Val<SC>,
    <SC as StarkGenericConfig>::Challenge,
    <SC as StarkGenericConfig>::Challenger,
>>::ProvingKey;

pub type RapPhaseSeqError<SC> = <<SC as StarkGenericConfig>::RapPhaseSeq as RapPhaseSeq<
    Val<SC>,
    <SC as StarkGenericConfig>::Challenge,
    <SC as StarkGenericConfig>::Challenger,
>>::Error;

pub type PackedVal<SC> = <Val<SC> as Field>::Packing;

pub type PackedChallenge<SC> =
    <<SC as StarkGenericConfig>::Challenge as ExtensionField<Val<SC>>>::ExtensionPacking;

#[derive(Debug)]
pub struct StarkConfig<Pcs, RapPhaseSeq, Challenge, Challenger> {
    pcs: Pcs,
    rap_phase: RapPhaseSeq,
    _phantom: PhantomData<(Challenge, Challenger)>,
}

impl<Pcs, RapPhaseSeq, Challenge, Challenger> StarkConfig<Pcs, RapPhaseSeq, Challenge, Challenger> {
    pub const fn new(pcs: Pcs, rap_phase: RapPhaseSeq) -> Self {
        Self {
            pcs,
            rap_phase,
            _phantom: PhantomData,
        }
    }
}

impl<Pcs, Rps, Challenge, Challenger> StarkGenericConfig
    for StarkConfig<Pcs, Rps, Challenge, Challenger>
where
    Challenge: ExtensionField<<Pcs::Domain as PolynomialSpace>::Val>,
    Pcs: p3_commit::Pcs<Challenge, Challenger>,
    Pcs::Domain: Send + Sync,
    Pcs::Commitment: Send + Sync,
    Pcs::ProverData: Send + Sync,
    Pcs::Proof: Send + Sync,
    Rps: RapPhaseSeq<<Pcs::Domain as PolynomialSpace>::Val, Challenge, Challenger>,
    Challenger: FieldChallenger<<Pcs::Domain as PolynomialSpace>::Val>
        + CanObserve<<Pcs as p3_commit::Pcs<Challenge, Challenger>>::Commitment>
        + CanSample<Challenge>,
{
    type Pcs = Pcs;
    type RapPhaseSeq = Rps;
    type Challenge = Challenge;
    type Challenger = Challenger;

    fn pcs(&self) -> &Self::Pcs {
        &self.pcs
    }
    fn rap_phase_seq(&self) -> &Self::RapPhaseSeq {
        &self.rap_phase
    }
}

pub struct UniStarkConfig<SC>(pub SC);

impl<SC: StarkGenericConfig> p3_uni_stark::StarkGenericConfig for UniStarkConfig<SC> {
    type Pcs = SC::Pcs;

    type Challenge = SC::Challenge;

    type Challenger = SC::Challenger;

    fn pcs(&self) -> &Self::Pcs {
        self.0.pcs()
    }
}