openvm_native_recursion/fri/
domain.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use openvm_native_compiler::prelude::*;
use openvm_stark_backend::{
    p3_commit::{LagrangeSelectors, TwoAdicMultiplicativeCoset},
    p3_field::{AbstractField, Field, TwoAdicField},
};

use super::types::FriConfigVariable;
use crate::commit::PolynomialSpaceVariable;

/// Reference: [p3_commit::TwoAdicMultiplicativeCoset]
#[derive(DslVariable, Clone)]
pub struct TwoAdicMultiplicativeCosetVariable<C: Config> {
    pub log_n: Usize<C::N>,
    pub size: Usize<C::N>,
    pub shift: Felt<C::F>,
    pub g: Felt<C::F>,
}

impl<C: Config> TwoAdicMultiplicativeCosetVariable<C> {
    pub fn size(&self) -> RVar<C::N> {
        self.size.clone().into()
    }

    pub fn first_point(&self) -> Felt<C::F> {
        self.shift
    }

    pub fn gen(&self) -> Felt<C::F> {
        self.g
    }
}

impl<C: Config> FromConstant<C> for TwoAdicMultiplicativeCosetVariable<C>
where
    C::F: TwoAdicField,
{
    type Constant = TwoAdicMultiplicativeCoset<C::F>;

    fn constant(value: Self::Constant, builder: &mut Builder<C>) -> Self {
        let g_val = C::F::two_adic_generator(value.log_n);
        TwoAdicMultiplicativeCosetVariable::<C> {
            // builder.eval is necessary to assign a variable in the dynamic mode.
            log_n: builder.eval(RVar::from(value.log_n)),
            size: builder.eval(RVar::from(1 << value.log_n)),
            shift: builder.eval(value.shift),
            g: builder.eval(g_val),
        }
    }
}

impl<C: Config> PolynomialSpaceVariable<C> for TwoAdicMultiplicativeCosetVariable<C>
where
    C::F: TwoAdicField,
{
    type Constant = TwoAdicMultiplicativeCoset<C::F>;

    fn next_point(
        &self,
        builder: &mut Builder<C>,
        point: Ext<<C as Config>::F, <C as Config>::EF>,
    ) -> Ext<<C as Config>::F, <C as Config>::EF> {
        builder.eval(point * self.gen())
    }

    fn selectors_at_point(
        &self,
        builder: &mut Builder<C>,
        point: Ext<<C as Config>::F, <C as Config>::EF>,
    ) -> LagrangeSelectors<Ext<<C as Config>::F, <C as Config>::EF>> {
        let unshifted_point: Ext<_, _> = builder.eval(point * self.shift.inverse());
        let z_h_expr =
            builder.exp_power_of_2_v::<Ext<_, _>>(unshifted_point, self.log_n.clone()) - C::EF::ONE;
        let z_h: Ext<_, _> = builder.eval(z_h_expr);

        LagrangeSelectors {
            is_first_row: builder.eval(z_h / (unshifted_point - C::EF::ONE)),
            is_last_row: builder.eval(z_h / (unshifted_point - self.gen().inverse())),
            is_transition: builder.eval(unshifted_point - self.gen().inverse()),
            inv_zeroifier: builder.eval(z_h.inverse()),
        }
    }

    fn zp_at_point(
        &self,
        builder: &mut Builder<C>,
        point: Ext<<C as Config>::F, <C as Config>::EF>,
    ) -> Ext<<C as Config>::F, <C as Config>::EF> {
        let unshifted_power =
            builder.exp_power_of_2_v::<Ext<_, _>>(point * self.shift.inverse(), self.log_n.clone());
        builder.eval(unshifted_power - C::EF::ONE)
    }

    fn split_domains(
        &self,
        builder: &mut Builder<C>,
        log_num_chunks: impl Into<RVar<C::N>>,
        num_chunks: impl Into<RVar<C::N>>,
    ) -> Array<C, Self> {
        let log_num_chunks = log_num_chunks.into();
        let num_chunks = num_chunks.into();
        let log_n = builder.eval_expr(self.log_n.clone() - log_num_chunks);
        let size: Usize<_> = builder.sll(RVar::one(), log_n);

        let g_dom = self.gen();
        let g = builder.exp_power_of_2_v::<Felt<C::F>>(g_dom, log_num_chunks);

        let domain_power: Felt<_> = builder.eval(C::F::ONE);

        let domains = builder.array(num_chunks);

        builder.range(0, num_chunks).for_each(|i, builder| {
            let log_n = builder.eval(log_n);
            let domain = TwoAdicMultiplicativeCosetVariable {
                log_n,
                size: size.clone(),
                shift: builder.eval(self.shift * domain_power),
                g,
            };
            // FIXME: here must use `builder.set_value`. `builder.set` will convert `Usize::Const`
            // to `Usize::Var` because it calls `builder.eval`.
            builder.set_value(&domains, i, domain);
            builder.assign(&domain_power, domain_power * g_dom);
        });

        domains
    }

    fn split_domains_const(&self, builder: &mut Builder<C>, log_num_chunks: usize) -> Vec<Self> {
        let num_chunks = 1 << log_num_chunks;
        let log_n: Usize<_> =
            builder.eval(self.log_n.clone() - C::N::from_canonical_usize(log_num_chunks));
        let size: Usize<_> = builder.sll(RVar::one(), RVar::from(log_n.clone()));

        let g_dom = self.gen();
        let g = builder.exp_power_of_2_v::<Felt<C::F>>(g_dom, log_num_chunks);

        let domain_power: Felt<_> = builder.eval(C::F::ONE);
        let mut domains = vec![];

        for _ in 0..num_chunks {
            domains.push(TwoAdicMultiplicativeCosetVariable {
                log_n: log_n.clone(),
                size: size.clone(),
                shift: builder.eval(self.shift * domain_power),
                g,
            });
            builder.assign(&domain_power, domain_power * g_dom);
        }
        domains
    }

    fn create_disjoint_domain(
        &self,
        builder: &mut Builder<C>,
        log_degree: RVar<<C as Config>::N>,
        config: Option<FriConfigVariable<C>>,
    ) -> Self {
        let domain = config.unwrap().get_subgroup(builder, log_degree);
        TwoAdicMultiplicativeCosetVariable {
            log_n: domain.log_n,
            size: domain.size,
            shift: builder.eval(self.shift * C::F::GENERATOR),
            g: domain.g,
        }
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use openvm_native_circuit::execute_program;
    use openvm_native_compiler::asm::AsmBuilder;
    use openvm_stark_backend::{
        config::{Domain, StarkGenericConfig, Val},
        p3_commit::{Pcs, PolynomialSpace},
        p3_field::PrimeField,
    };
    use openvm_stark_sdk::config::{
        baby_bear_poseidon2::{config_from_perm, default_perm, BabyBearPoseidon2Config},
        FriParameters,
    };
    use rand::{thread_rng, Rng};

    use super::*;
    use crate::utils::const_fri_config;

    pub(crate) fn domain_assertions<F: TwoAdicField + PrimeField, C: Config<N = F, F = F>>(
        builder: &mut Builder<C>,
        domain: &TwoAdicMultiplicativeCosetVariable<C>,
        domain_val: &TwoAdicMultiplicativeCoset<F>,
        zeta_val: C::EF,
    ) {
        // Assert the domain parameters are the same.
        builder.assert_var_eq(
            domain.log_n.clone(),
            F::from_canonical_usize(domain_val.log_n),
        );
        builder.assert_var_eq(
            domain.size.clone(),
            F::from_canonical_usize(1 << domain_val.log_n),
        );
        builder.assert_felt_eq(domain.shift, domain_val.shift);

        // Get a random point.
        let zeta: Ext<_, _> = builder.eval(zeta_val.cons());

        // Compare the selector values of the reference and the builder.
        let sels_expected = domain_val.selectors_at_point(zeta_val);
        let sels = domain.selectors_at_point(builder, zeta);
        builder.assert_ext_eq(sels.is_first_row, sels_expected.is_first_row.cons());
        builder.assert_ext_eq(sels.is_last_row, sels_expected.is_last_row.cons());
        builder.assert_ext_eq(sels.is_transition, sels_expected.is_transition.cons());

        let zp_val = domain_val.zp_at_point(zeta_val);
        let zp = domain.zp_at_point(builder, zeta);
        builder.assert_ext_eq(zp, zp_val.cons());
    }

    fn test_domain_impl(static_only: bool) {
        type SC = BabyBearPoseidon2Config;
        type F = Val<SC>;
        type EF = <SC as StarkGenericConfig>::Challenge;
        type Challenger = <SC as StarkGenericConfig>::Challenger;
        type ScPcs = <SC as StarkGenericConfig>::Pcs;

        let mut rng = thread_rng();
        let fri_params = FriParameters::standard_fast();
        let config = config_from_perm(&default_perm(), fri_params);
        let pcs = config.pcs();
        let natural_domain_for_degree = |degree: usize| -> Domain<SC> {
            <ScPcs as Pcs<EF, Challenger>>::natural_domain_for_degree(pcs, degree)
        };

        // Initialize a builder.
        let mut builder = AsmBuilder::<F, EF>::default();
        builder.flags.static_only = static_only;

        let config_var = const_fri_config(&mut builder, &fri_params);
        for i in 0..5 {
            let log_d_val = 10 + i;

            let log_quotient_degree = 2;

            // Initialize a reference doamin.
            let domain_val = natural_domain_for_degree(1 << log_d_val);
            let domain = builder.constant(domain_val);

            // builder.assert_felt_eq(domain.shift, domain_val.shift);
            let zeta_val = rng.gen::<EF>();
            domain_assertions(&mut builder, &domain, &domain_val, zeta_val);

            // Try a shifted domain.
            let disjoint_domain_val =
                domain_val.create_disjoint_domain(1 << (log_d_val + log_quotient_degree));
            let disjoint_domain = builder.constant(disjoint_domain_val);
            domain_assertions(
                &mut builder,
                &disjoint_domain,
                &disjoint_domain_val,
                zeta_val,
            );

            let log_degree = log_d_val + log_quotient_degree;
            let disjoint_domain_gen = domain.create_disjoint_domain(
                &mut builder,
                log_degree.into(),
                Some(config_var.clone()),
            );
            domain_assertions(
                &mut builder,
                &disjoint_domain_gen,
                &disjoint_domain_val,
                zeta_val,
            );

            // Now try splited domains
            let qc_domains_val = disjoint_domain_val.split_domains(1 << log_quotient_degree);
            for dom_val in qc_domains_val.iter() {
                let dom = builder.constant(*dom_val);
                domain_assertions(&mut builder, &dom, dom_val, zeta_val);
            }

            // Test the splitting of domains by the builder.
            let quotient_size = 1 << log_quotient_degree;
            let qc_domains =
                disjoint_domain.split_domains(&mut builder, log_quotient_degree, quotient_size);
            for (i, dom_val) in qc_domains_val.iter().enumerate() {
                let dom = builder.get(&qc_domains, i);
                domain_assertions(&mut builder, &dom, dom_val, zeta_val);
            }
        }
        builder.halt();

        let program = builder.compile_isa();
        execute_program(program, vec![]);
    }
    #[test]
    fn test_domain_static() {
        test_domain_impl(true);
    }

    #[test]
    fn test_domain_dynamic() {
        test_domain_impl(false);
    }
}