tasm_lib/verifier/master_table/
divide_out_zerofiers.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use triton_vm::prelude::LabelledInstruction;
use triton_vm::prelude::*;
use triton_vm::table::master_table::MasterAuxTable;
use triton_vm::twenty_first::math::x_field_element::EXTENSION_DEGREE;

use crate::data_type::DataType;
use crate::library::Library;
use crate::traits::basic_snippet::BasicSnippet;
use crate::verifier::master_table::air_constraint_evaluation::AirConstraintEvaluation;
use crate::verifier::master_table::zerofiers_inverse::ConstraintType;
use crate::verifier::master_table::zerofiers_inverse::ZerofiersInverse;

/// Takes an AIR evaluation and divides out the zerofiers.
#[derive(Debug, Clone)]
pub struct DivideOutZerofiers;

impl BasicSnippet for DivideOutZerofiers {
    fn inputs(&self) -> Vec<(DataType, String)> {
        vec![
            (DataType::VoidPointer, "*air_evaluation_result".to_string()),
            (DataType::Xfe, "out_of_domain_point_curr_row".to_owned()),
            (DataType::U32, "padded_height".to_owned()),
            (DataType::Bfe, "trace_domain_generator".to_owned()),
        ]
    }

    fn outputs(&self) -> Vec<(DataType, String)> {
        vec![(
            AirConstraintEvaluation::output_type(),
            "*quotient_summands".to_owned(),
        )]
    }

    fn entrypoint(&self) -> String {
        "tasmlib_verifier_master_table_divide_out_zerofiers".to_owned()
    }

    fn code(&self, library: &mut Library) -> Vec<LabelledInstruction> {
        let entrypoint = self.entrypoint();

        let zerofiers_inverse_alloc =
            library.kmalloc(ZerofiersInverse::array_size().try_into().unwrap());
        let zerofiers_inverse_snippet = ZerofiersInverse {
            zerofiers_inverse_write_address: zerofiers_inverse_alloc.write_address(),
        };
        let zerofiers_inverse = library.import(Box::new(zerofiers_inverse_snippet));

        let read_all_air_elements = vec![
            triton_asm!(
                // _ *air_elem
                read_mem { EXTENSION_DEGREE } // _ [air_elem] *air_elem_prev
            );
            MasterAuxTable::NUM_CONSTRAINTS
        ]
        .concat();

        let mul_and_write = |constraint_type: ConstraintType, num_constraints: usize| {
            vec![
                triton_asm!(
                    // _ [[air_elem]] *air_elem

                    swap 3
                    swap 2
                    swap 1
                    // _ *air_elem [[air_elem]]

                    push {zerofiers_inverse_snippet.zerofier_inv_read_address(constraint_type)}
                    read_mem {EXTENSION_DEGREE}
                    pop 1

                    xx_mul
                    // _ [[air_elem] *air_elem [[air_elem * z_inv]]]

                    swap 1
                    swap 2
                    swap 3
                    // _ [[air_elem] [[air_elem * z_inv]]] *air_elem

                    write_mem {EXTENSION_DEGREE}
                    // _ [[air_elem] *air_elem_next

                );
                num_constraints
            ]
            .concat()
        };

        let mul_and_write = [
            mul_and_write(
                ConstraintType::Initial,
                MasterAuxTable::NUM_INITIAL_CONSTRAINTS,
            ),
            mul_and_write(
                ConstraintType::Consistency,
                MasterAuxTable::NUM_CONSISTENCY_CONSTRAINTS,
            ),
            mul_and_write(
                ConstraintType::Transition,
                MasterAuxTable::NUM_TRANSITION_CONSTRAINTS,
            ),
            mul_and_write(
                ConstraintType::Terminal,
                MasterAuxTable::NUM_TERMINAL_CONSTRAINTS,
            ),
        ]
        .concat();

        let jump_to_last_air_element = triton_asm!(
            // _ *air_elem[0]

            push {MasterAuxTable::NUM_CONSTRAINTS * EXTENSION_DEGREE - 1}
            add
            // _ *air_elem_last_word
        );

        let jump_to_first_air_element = triton_asm!(
            // _ *air_elem[n]

            push {-((MasterAuxTable::NUM_CONSTRAINTS * EXTENSION_DEGREE) as i32)}
            add
            // _ *air_elem_last_word
        );

        triton_asm!(
            {entrypoint}:
                // _ *air_evaluation_result [out_of_domain_point_curr_row] padded_height trace_domain_generator

                call {zerofiers_inverse}
                // _ *air_evaluation_result

                {&jump_to_last_air_element}
                // _ *air_elem_last_word

                {&read_all_air_elements}
                // _ [[air_elem]] (*air_constraints - 1)

                push 1 add
                // _ [[air_elem]] *air_constraints

                {&mul_and_write}
                // _ (*air_elem_last + 3)

                {&jump_to_first_air_element}

                return
        )
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::collections::HashMap;
    use std::rc::Rc;

    use itertools::Itertools;
    use rand::prelude::*;
    use triton_vm::twenty_first::math::traits::Inverse;
    use triton_vm::twenty_first::math::traits::ModPowU32;
    use triton_vm::twenty_first::math::traits::PrimitiveRootOfUnity;

    use super::*;
    use crate::empty_stack;
    use crate::execute_test;
    use crate::linker::link_for_isolated_run;

    #[test]
    fn divide_out_zerofiers_test() {
        let snippet = DivideOutZerofiers;
        let mut seed: [u8; 32] = [0u8; 32];
        thread_rng().fill_bytes(&mut seed);
        snippet.test_equivalence_with_host_machine(seed);
    }

    impl DivideOutZerofiers {
        fn test_equivalence_with_host_machine(&self, seed: [u8; 32]) {
            let mut rng: StdRng = SeedableRng::from_seed(seed);
            let (air_evaluation_result, ood_point_curr_row, padded_height, trace_domain_generator) =
                Self::random_input_values(&mut rng);

            let rust_result = Self::rust_result(
                air_evaluation_result,
                ood_point_curr_row,
                padded_height,
                trace_domain_generator,
            );

            let tasm_result = self.tasm_result(
                air_evaluation_result,
                ood_point_curr_row,
                padded_height,
                trace_domain_generator,
            );

            assert_eq!(tasm_result.len(), rust_result.len());
            assert_eq!(
                tasm_result.iter().copied().sum::<XFieldElement>(),
                rust_result.iter().copied().sum::<XFieldElement>(),
                "\ntasm: [{},...]\nrust: [{},...]",
                tasm_result.iter().take(3).join(","),
                rust_result.iter().take(3).join(",")
            );
            assert_eq!(tasm_result, rust_result);
        }

        pub(super) fn random_input_values(
            rng: &mut StdRng,
        ) -> (
            [XFieldElement; MasterAuxTable::NUM_CONSTRAINTS],
            XFieldElement,
            u32,
            BFieldElement,
        ) {
            let air_evaluation_result =
                rng.gen::<[XFieldElement; MasterAuxTable::NUM_CONSTRAINTS]>();
            let ood_point_curr_row: XFieldElement = rng.gen();
            let padded_height = 2u32.pow(rng.gen_range(8..32));
            let trace_domain_generator =
                BFieldElement::primitive_root_of_unity(padded_height as u64).unwrap();

            (
                air_evaluation_result,
                ood_point_curr_row,
                padded_height,
                trace_domain_generator,
            )
        }

        /// Return the evaluated array of quotient values, and its address in memory
        fn tasm_result(
            &self,
            air_evaluation_result: [XFieldElement; MasterAuxTable::NUM_CONSTRAINTS],
            out_of_domain_point_curr_row: XFieldElement,
            padded_height: u32,
            trace_domain_generator: BFieldElement,
        ) -> Vec<XFieldElement> {
            let free_page_pointer = BFieldElement::new(((1u64 << 32) - 3) * (1 << 32));
            let mut memory = HashMap::<BFieldElement, BFieldElement>::new();
            println!(
                "air evaluation result encoded: [{}, ...]",
                air_evaluation_result.encode().iter().take(4).join(",")
            );
            for (i, e) in air_evaluation_result.encode().into_iter().enumerate() {
                memory.insert(free_page_pointer + BFieldElement::new(i as u64), e);
            }

            let stack = [
                empty_stack(),
                vec![free_page_pointer],
                out_of_domain_point_curr_row
                    .coefficients
                    .into_iter()
                    .rev()
                    .collect_vec(),
                vec![
                    BFieldElement::new(padded_height as u64),
                    trace_domain_generator,
                ],
            ]
            .concat();
            let code = link_for_isolated_run(Rc::new(RefCell::new(self.to_owned())));
            let final_state = execute_test(
                &code,
                &mut stack.clone(),
                self.stack_diff(),
                vec![],
                NonDeterminism::default().with_ram(memory),
                None,
            );

            // read the array pointed to by the pointer living on top of the stack
            AirConstraintEvaluation::read_result_from_memory(final_state).0
        }

        pub fn rust_result(
            air_evaluation_result: [XFieldElement; MasterAuxTable::NUM_CONSTRAINTS],
            out_of_domain_point_curr_row: XFieldElement,
            padded_height: u32,
            trace_domain_generator: BFieldElement,
        ) -> Vec<XFieldElement> {
            println!("trace domain generator: {trace_domain_generator}");
            println!("padded height: {padded_height}");
            println!("out-of-domain point current row: {out_of_domain_point_curr_row}");
            let initial_zerofier_inv = (out_of_domain_point_curr_row - bfe!(1)).inverse();
            let consistency_zerofier_inv =
                (out_of_domain_point_curr_row.mod_pow_u32(padded_height) - bfe!(1)).inverse();
            let except_last_row = out_of_domain_point_curr_row - trace_domain_generator.inverse();
            let transition_zerofier_inv = except_last_row * consistency_zerofier_inv;
            let terminal_zerofier_inv = except_last_row.inverse(); // i.e., only last row

            println!("initial zerofier inverse: {}", initial_zerofier_inv);
            println!("consistency zerofier inverse: {}", consistency_zerofier_inv);
            println!("transition zerofier inverse: {}", transition_zerofier_inv);
            println!("terminal zerofier inverse: {}", terminal_zerofier_inv);

            let mut running_total_constraints = 0;
            let initial_quotients = air_evaluation_result[running_total_constraints
                ..(running_total_constraints + MasterAuxTable::NUM_INITIAL_CONSTRAINTS)]
                .iter()
                .map(|&x| x * initial_zerofier_inv)
                .collect_vec();
            running_total_constraints += MasterAuxTable::NUM_INITIAL_CONSTRAINTS;

            let consistency_quotients = air_evaluation_result[running_total_constraints
                ..(running_total_constraints + MasterAuxTable::NUM_CONSISTENCY_CONSTRAINTS)]
                .iter()
                .map(|&x| x * consistency_zerofier_inv)
                .collect_vec();
            running_total_constraints += MasterAuxTable::NUM_CONSISTENCY_CONSTRAINTS;

            let transition_quotients = air_evaluation_result[running_total_constraints
                ..(running_total_constraints + MasterAuxTable::NUM_TRANSITION_CONSTRAINTS)]
                .iter()
                .map(|&x| x * transition_zerofier_inv)
                .collect_vec();
            running_total_constraints += MasterAuxTable::NUM_TRANSITION_CONSTRAINTS;

            let terminal_quotients = air_evaluation_result[running_total_constraints
                ..(running_total_constraints + MasterAuxTable::NUM_TERMINAL_CONSTRAINTS)]
                .iter()
                .map(|&x| x * terminal_zerofier_inv)
                .collect_vec();

            [
                initial_quotients,
                consistency_quotients,
                transition_quotients,
                terminal_quotients,
            ]
            .concat()
        }
    }
}

#[cfg(test)]
mod bench {
    use std::collections::HashMap;

    use itertools::Itertools;
    use rand::prelude::*;
    use twenty_first::math::traits::PrimitiveRootOfUnity;

    use super::*;
    use crate::empty_stack;
    use crate::traits::function::Function;
    use crate::traits::function::FunctionInitialState;
    use crate::traits::function::ShadowedFunction;
    use crate::traits::rust_shadow::RustShadow;

    #[test]
    fn bench_divide_out_zerofiers() {
        ShadowedFunction::new(DivideOutZerofiers).bench();
    }

    impl Function for DivideOutZerofiers {
        fn rust_shadow(
            &self,
            _stack: &mut Vec<BFieldElement>,
            _memory: &mut HashMap<BFieldElement, BFieldElement>,
        ) {
            // Never called as we do a more manual test.
            // The more manual test is done bc we don't want to
            // have to simulate all the intermediate calculations
            // that are stored to memory.
            unimplemented!()
        }

        fn pseudorandom_initial_state(
            &self,
            seed: [u8; 32],
            _bench_case: Option<crate::snippet_bencher::BenchmarkCase>,
        ) -> FunctionInitialState {
            // Used for benchmarking
            let mut rng: StdRng = SeedableRng::from_seed(seed);
            let air_evaluation_result =
                rng.gen::<[XFieldElement; MasterAuxTable::NUM_CONSTRAINTS]>();
            let ood_point_current_row = rng.gen::<XFieldElement>();
            let padded_height = 1 << 20;
            let trace_domain_generator =
                BFieldElement::primitive_root_of_unity(padded_height).unwrap();

            let free_page_pointer = BFieldElement::new(((1u64 << 32) - 3) * (1 << 32));
            let mut memory = HashMap::<BFieldElement, BFieldElement>::new();
            for (i, e) in air_evaluation_result
                .encode()
                .into_iter()
                .skip(1)
                .enumerate()
            {
                memory.insert(free_page_pointer + BFieldElement::new(i as u64), e);
            }

            let stack = [
                empty_stack(),
                vec![free_page_pointer],
                ood_point_current_row
                    .coefficients
                    .into_iter()
                    .rev()
                    .collect_vec(),
                vec![BFieldElement::new(padded_height), trace_domain_generator],
            ]
            .concat();

            FunctionInitialState { stack, memory }
        }
    }
}