tasm_lib/list/higher_order/
all.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use itertools::Itertools;
use triton_vm::prelude::*;

use super::inner_function::InnerFunction;
use crate::list::get::Get;
use crate::list::length::Length;
use crate::prelude::*;

/// Runs a predicate over all elements of a list and returns true only if all elements satisfy the
/// predicate.
pub struct All {
    pub f: InnerFunction,
}

impl All {
    pub fn new(f: InnerFunction) -> Self {
        Self { f }
    }
}

impl BasicSnippet for All {
    fn inputs(&self) -> Vec<(DataType, String)> {
        let element_type = self.f.domain();
        let list_type = DataType::List(Box::new(element_type));
        vec![(list_type, "*input_list".to_string())]
    }

    fn outputs(&self) -> Vec<(DataType, String)> {
        vec![(DataType::Bool, "all_true".to_string())]
    }

    fn entrypoint(&self) -> String {
        format!("tasmlib_list_higher_order_u32_all_{}", self.f.entrypoint())
    }

    fn code(&self, library: &mut Library) -> Vec<LabelledInstruction> {
        let input_type = self.f.domain();
        let output_type = self.f.range();
        assert_eq!(output_type, DataType::Bool);

        let get_length = library.import(Box::new(Length));
        let list_get = library.import(Box::new(Get::new(input_type)));

        let inner_function_name = match &self.f {
            InnerFunction::RawCode(rc) => rc.entrypoint(),
            InnerFunction::NoFunctionBody(_) => todo!(),
            InnerFunction::BasicSnippet(bs) => {
                let labelled_instructions = bs.annotated_code(library);
                library.explicit_import(&bs.entrypoint(), &labelled_instructions)
            }
        };

        // If function was supplied as raw instructions, we need to append the inner function to the function
        // body. Otherwise, `library` handles the imports.
        let maybe_inner_function_body_raw = match &self.f {
            InnerFunction::RawCode(rc) => rc.function.iter().map(|x| x.to_string()).join("\n"),
            InnerFunction::NoFunctionBody(_) => todo!(),
            InnerFunction::BasicSnippet(_) => Default::default(),
        };
        let entrypoint = self.entrypoint();
        let main_loop = format!("{entrypoint}_loop");

        let result_type_hint = format!("hint all_{}: Boolean = stack[0]", self.f.entrypoint());

        triton_asm!(
            // BEFORE: _ input_list
            // AFTER:  _ result
            {entrypoint}:
                hint input_list = stack[0]
                push 1  // _ input_list res
                {result_type_hint}
                swap 1  // _ res input_list
                dup 0   // _ res input_list input_list
                call {get_length}
                hint list_item: Index = stack[0]
                        // _ res input_list len

                call {main_loop}
                        // _ res input_list 0

                pop 2   // _ res
                return

            // INVARIANT: _ res input_list index
            {main_loop}:
                // test return condition
                dup 0 push 0 eq
                        // _ res input_list index index==0

                skiz return
                        // _ res input_list index

                // decrement index
                push -1 add

                // body

                // read
                dup 1 dup 1
                        // _ res input_list index input_list index
                call {list_get}
                        // _ res input_list index [input_elements]

                // compute predicate
                call {inner_function_name}
                        // _ res input_list index b

                // accumulate
                dup 3   // _ res input_list index b res
                mul     // _ res input_list index (b && res)
                swap 3  // _ (b && res) input_list index res
                pop 1   // _ (b && res) input_list index

                recurse

            {maybe_inner_function_body_raw}
        )
    }
}

#[cfg(test)]
mod tests {
    use num::One;
    use num::Zero;

    use super::*;
    use crate::arithmetic;
    use crate::empty_stack;
    use crate::list::higher_order::inner_function::RawCode;
    use crate::list::LIST_METADATA_SIZE;
    use crate::rust_shadowing_helper_functions;
    use crate::rust_shadowing_helper_functions::list::list_get;
    use crate::rust_shadowing_helper_functions::list::untyped_insert_random_list;
    use crate::test_helpers::test_rust_equivalence_given_complete_state;
    use crate::test_prelude::*;

    impl All {
        fn generate_input_state(
            &self,
            list_pointer: BFieldElement,
            list_length: usize,
            random: bool,
        ) -> InitVmState {
            let mut stack = empty_stack();
            stack.push(list_pointer);

            let mut memory = HashMap::default();
            let input_type = self.f.domain();
            let list_bookkeeping_offset = LIST_METADATA_SIZE;
            let element_index_in_list =
                list_bookkeeping_offset + list_length * input_type.stack_size();
            let element_index = list_pointer + BFieldElement::new(element_index_in_list as u64);
            memory.insert(BFieldElement::zero(), element_index);

            if random {
                untyped_insert_random_list(
                    list_pointer,
                    list_length,
                    &mut memory,
                    input_type.stack_size(),
                );
            } else {
                rust_shadowing_helper_functions::list::list_insert(
                    list_pointer,
                    (0..list_length as u64)
                        .map(BFieldElement::new)
                        .collect_vec(),
                    &mut memory,
                );
            }

            InitVmState::with_stack_and_memory(stack, memory)
        }
    }

    impl Function for All {
        fn rust_shadow(
            &self,
            stack: &mut Vec<BFieldElement>,
            memory: &mut HashMap<BFieldElement, BFieldElement>,
        ) {
            let input_type = self.f.domain();
            let list_pointer = stack.pop().unwrap();

            // forall elements, read + map + maybe copy
            let list_length =
                rust_shadowing_helper_functions::list::list_get_length(list_pointer, memory);
            let mut satisfied = true;
            for i in 0..list_length {
                let input_item = list_get(list_pointer, i, memory, input_type.stack_size());
                for bfe in input_item.into_iter().rev() {
                    stack.push(bfe);
                }

                self.f.apply(stack, memory);

                let single_result = stack.pop().unwrap().value() != 0;
                satisfied = satisfied && single_result;
            }

            stack.push(BFieldElement::new(satisfied as u64));
        }

        fn pseudorandom_initial_state(
            &self,
            seed: [u8; 32],
            bench_case: Option<BenchmarkCase>,
        ) -> FunctionInitialState {
            let (stack, memory) = match bench_case {
                Some(BenchmarkCase::CommonCase) => {
                    let list_pointer = BFieldElement::new(5);
                    let list_length = 10;
                    let execution_state =
                        self.generate_input_state(list_pointer, list_length, false);
                    (execution_state.stack, execution_state.nondeterminism.ram)
                }
                Some(BenchmarkCase::WorstCase) => {
                    let list_pointer = BFieldElement::new(5);
                    let list_length = 100;
                    let execution_state =
                        self.generate_input_state(list_pointer, list_length, false);
                    (execution_state.stack, execution_state.nondeterminism.ram)
                }
                None => {
                    let mut rng = StdRng::from_seed(seed);
                    let list_pointer = BFieldElement::new(rng.next_u64() % (1 << 20));
                    let list_length = 1 << (rng.next_u32() as usize % 4);
                    let execution_state =
                        self.generate_input_state(list_pointer, list_length, true);
                    (execution_state.stack, execution_state.nondeterminism.ram)
                }
            };

            FunctionInitialState { stack, memory }
        }
    }

    #[test]
    fn rust_shadow() {
        let inner_function = InnerFunction::BasicSnippet(Box::new(TestHashXFieldElementLsb));
        ShadowedFunction::new(All::new(inner_function)).test();
    }

    #[test]
    fn all_lt_test() {
        const TWO_POW_31: u64 = 1u64 << 31;
        let rawcode = RawCode::new(
            triton_asm!(
                less_than_2_pow_31:
                    push 2147483648 // == 2^31
                    swap 1
                    lt
                    return
            ),
            DataType::Bfe,
            DataType::Bool,
        );
        let snippet = All::new(InnerFunction::RawCode(rawcode));
        let mut memory = HashMap::new();

        // Should return true
        rust_shadowing_helper_functions::list::list_insert(
            BFieldElement::new(42),
            (0..30).map(BFieldElement::new).collect_vec(),
            &mut memory,
        );
        let input_stack = [empty_stack(), vec![BFieldElement::new(42)]].concat();
        let expected_end_stack_true = [empty_stack(), vec![BFieldElement::one()]].concat();
        let shadowed_snippet = ShadowedFunction::new(snippet);
        let mut nondeterminism = NonDeterminism::default().with_ram(memory);
        test_rust_equivalence_given_complete_state(
            &shadowed_snippet,
            &input_stack,
            &[],
            &nondeterminism,
            &None,
            Some(&expected_end_stack_true),
        );

        // Should return false
        rust_shadowing_helper_functions::list::list_insert(
            BFieldElement::new(42),
            (0..30)
                .map(|x| BFieldElement::new(x + TWO_POW_31 - 20))
                .collect_vec(),
            &mut nondeterminism.ram,
        );
        let expected_end_stack_false = [empty_stack(), vec![BFieldElement::zero()]].concat();
        test_rust_equivalence_given_complete_state(
            &shadowed_snippet,
            &input_stack,
            &[],
            &nondeterminism,
            &None,
            Some(&expected_end_stack_false),
        );
    }

    #[test]
    fn test_with_raw_function_lsb_on_bfe() {
        let rawcode = RawCode::new(
            triton_asm!(
                lsb_bfe:
                split    // _ hi lo
                push 2   // _ hi lo 2
                swap 1   // _ hi 2 lo
                div_mod  // _ hi q r
                swap 2   // _ r q hi
                pop 2    // _ r
                return
            ),
            DataType::Bfe,
            DataType::Bool,
        );
        let snippet = All::new(InnerFunction::RawCode(rawcode));
        ShadowedFunction::new(snippet).test();
    }

    #[test]
    fn test_with_raw_function_eq_42() {
        let raw_code = RawCode::new(
            triton_asm!(
                eq_42:
                push 42
                eq
                return
            ),
            DataType::U32,
            DataType::Bool,
        );
        let snippet = All::new(InnerFunction::RawCode(raw_code));
        ShadowedFunction::new(snippet).test();
    }

    #[test]
    fn test_with_raw_function_lsb_on_xfe() {
        let rawcode = RawCode::new(
            triton_asm!(
                lsb_xfe:
                split    // _ x2 x1 hi lo
                push 2   // _ x2 x1 hi lo 2
                swap 1   // _ x2 x1 hi 2 lo
                div_mod  // _ x2 x1 hi q r
                swap 4   // _ r x1 q hi x2
                pop 4    // _ r x1 q hi
                return
            ),
            DataType::Xfe,
            DataType::Bool,
        );
        let snippet = All::new(InnerFunction::RawCode(rawcode));
        ShadowedFunction::new(snippet).test();
    }

    /// Only used for tests. Please don't export this.
    #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
    pub(super) struct TestHashXFieldElementLsb;

    impl BasicSnippet for TestHashXFieldElementLsb {
        fn inputs(&self) -> Vec<(DataType, String)> {
            vec![(DataType::Xfe, "element".to_string())]
        }

        fn outputs(&self) -> Vec<(DataType, String)> {
            vec![(DataType::Bool, "bool".to_string())]
        }

        fn entrypoint(&self) -> String {
            "test_hash_xfield_element_lsb".to_string()
        }

        fn code(&self, library: &mut Library) -> Vec<LabelledInstruction> {
            let entrypoint = self.entrypoint();
            let unused_import = library.import(Box::new(arithmetic::u32::safe_add::SafeAdd));
            triton_asm!(
            // BEFORE: _ [x: XFieldElement]
            // AFTER:  _ [b: bool]
            {entrypoint}:
                /* Useless additions: ensure that dependencies are accepted inside
                 * the generated code of `all`
                 */
                push 0
                push 0
                call {unused_import}
                pop 1

                push 0
                push 0
                push 0
                push 0
                push 0
                push 0
                push 1  // _ x2 x1 x0 0 0 0 0 0 0 1
                pick 9
                pick 9
                pick 9  // _ 0 0 0 0 0 0 1 x2 x1 x0

                sponge_init
                sponge_absorb
                sponge_squeeze
                        // _ [d; 10]

                split
                push 2
                place 1
                div_mod // _ [d'; 9] d0_hi (d0_lo // 2) (d0_lo % 2)

                place 11
                pop 5
                pop 5
                pop 1   // _ (d0_lo % 2)

                return
            )
        }
    }
}

#[cfg(test)]
mod benches {
    use super::tests::TestHashXFieldElementLsb;
    use super::*;
    use crate::test_prelude::*;

    #[test]
    fn benchmark() {
        let inner_function = InnerFunction::BasicSnippet(Box::new(TestHashXFieldElementLsb));
        ShadowedFunction::new(All::new(inner_function)).bench();
    }
}