tasm_lib/list/higher_order/
inner_function.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
use std::collections::HashMap;

use triton_vm::isa::instruction::AnInstruction;
use triton_vm::prelude::*;

use crate::data_type::DataType;
use crate::library::Library;
use crate::traits::basic_snippet::BasicSnippet;
use crate::traits::deprecated_snippet::DeprecatedSnippet;

const MORE_THAN_ONE_INPUT_OR_OUTPUT_TYPE_IN_INNER_FUNCTION: &str = "higher-order functions \
currently only work with *one* input element in inner function. \
Use a tuple data type to circumvent this.";

/// A data structure for describing an inner function predicate to filter with,
/// or a function to map with.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct RawCode {
    pub function: Vec<LabelledInstruction>,
    pub input_type: DataType,
    pub output_type: DataType,
}

impl RawCode {
    pub fn new(
        function: Vec<LabelledInstruction>,
        input_type: DataType,
        output_type: DataType,
    ) -> Self {
        let is_label = |x: &_| matches!(x, LabelledInstruction::Label(_));
        let is_instruction = |x: &_| matches!(x, LabelledInstruction::Instruction(_));
        let labels_and_instructions = function.iter().filter(|i| is_label(i) || is_instruction(i));

        // Verify that 1st line is a label
        assert!(
            labels_and_instructions.count() >= 2,
            "Inner function must have at least two lines: a label and a return or recurse"
        );
        assert!(
            matches!(function[0], LabelledInstruction::Label(_)),
            "First line of inner function must be label. Got: {}",
            function[0]
        );
        assert!(
            matches!(
                function.last().unwrap(),
                LabelledInstruction::Instruction(AnInstruction::Return)
                    | LabelledInstruction::Instruction(AnInstruction::Recurse)
                    | LabelledInstruction::Instruction(AnInstruction::RecurseOrReturn)
            ),
            "Last line of inner function must be either return, recurse, or recurse_or_return. Got: {}",
            function.last().unwrap()
        );

        Self {
            function,
            input_type,
            output_type,
        }
    }
}

impl RawCode {
    /// Return the entrypoint, label, of the inner function. Used to make a call to this function.
    pub fn entrypoint(&self) -> String {
        let is_label = |x: &_| matches!(x, LabelledInstruction::Label(_));
        let is_instruction = |x: &_| matches!(x, LabelledInstruction::Instruction(_));
        let first_label_or_instruction = self
            .function
            .iter()
            .find(|&x| is_label(x) || is_instruction(x));
        let Some(labelled_instruction) = first_label_or_instruction else {
            panic!("Inner function must start with a label. Got neither labels nor instructions.")
        };
        let LabelledInstruction::Label(label) = labelled_instruction else {
            panic!("Inner function must start with a label. Got: {labelled_instruction}");
        };

        label.to_string()
    }

    /// Returns `Some(code)` iff the raw code is a function that can be inlined
    ///
    /// Type hints and breakpoints are stripped.
    pub fn inlined_body(&self) -> Option<Vec<LabelledInstruction>> {
        let is_label = |x: &_| matches!(x, LabelledInstruction::Label(_));
        let is_instruction = |x: &_| matches!(x, LabelledInstruction::Instruction(_));
        let is_recursive = |x: &_| {
            matches!(
                x,
                LabelledInstruction::Instruction(AnInstruction::Recurse)
                    | LabelledInstruction::Instruction(AnInstruction::RecurseOrReturn)
            )
        };

        if self.function.iter().any(is_recursive) {
            // recursion needs to be wrapped in a function
            return None;
        }

        let mut labels_and_instructions = self
            .function
            .iter()
            .filter(|i| is_label(i) || is_instruction(i));

        let Some(first_thing) = labels_and_instructions.next() else {
            return Some(triton_asm!());
        };
        let LabelledInstruction::Label(_) = first_thing else {
            panic!("Raw Code must start with a label.")
        };

        let Some(LabelledInstruction::Instruction(AnInstruction::Return)) =
            labels_and_instructions.next_back()
        else {
            panic!("Raw Code is probably buggy: too short, or doesn't end with `return`.");
        };

        Some(labels_and_instructions.cloned().collect())
    }
}

pub enum InnerFunction {
    RawCode(RawCode),
    DeprecatedSnippet(Box<dyn DeprecatedSnippet>),
    BasicSnippet(Box<dyn BasicSnippet>),

    // Used when a snippet is declared somewhere else, and it's not the responsibility of
    // the higher order function to import it.
    NoFunctionBody(NoFunctionBody),
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct NoFunctionBody {
    pub label_name: String,
    pub input_type: DataType,
    pub output_type: DataType,
}

impl InnerFunction {
    pub fn domain(&self) -> DataType {
        match self {
            InnerFunction::RawCode(raw) => raw.input_type.clone(),
            InnerFunction::DeprecatedSnippet(f) => {
                let [ref input] = f.input_types()[..] else {
                    panic!("{MORE_THAN_ONE_INPUT_OR_OUTPUT_TYPE_IN_INNER_FUNCTION}");
                };
                input.clone()
            }
            InnerFunction::NoFunctionBody(f) => f.input_type.clone(),
            InnerFunction::BasicSnippet(bs) => {
                let [(ref input, _)] = bs.inputs()[..] else {
                    panic!("{MORE_THAN_ONE_INPUT_OR_OUTPUT_TYPE_IN_INNER_FUNCTION}");
                };
                input.clone()
            }
        }
    }

    pub fn range(&self) -> DataType {
        match self {
            InnerFunction::RawCode(rc) => rc.output_type.clone(),
            InnerFunction::DeprecatedSnippet(sn) => {
                let [ref output] = sn.output_types()[..] else {
                    panic!("{MORE_THAN_ONE_INPUT_OR_OUTPUT_TYPE_IN_INNER_FUNCTION}");
                };
                output.clone()
            }
            InnerFunction::NoFunctionBody(lnat) => lnat.output_type.clone(),
            InnerFunction::BasicSnippet(bs) => {
                let [(ref output, _)] = bs.outputs()[..] else {
                    panic!("{MORE_THAN_ONE_INPUT_OR_OUTPUT_TYPE_IN_INNER_FUNCTION}");
                };
                output.clone()
            }
        }
    }

    /// Return the entrypoint, label, of the inner function. Used to make a call to this function.
    pub fn entrypoint(&self) -> String {
        match self {
            InnerFunction::RawCode(rc) => rc.entrypoint(),
            InnerFunction::DeprecatedSnippet(sn) => sn.entrypoint_name(),
            InnerFunction::NoFunctionBody(sn) => sn.label_name.to_owned(),
            InnerFunction::BasicSnippet(bs) => bs.entrypoint(),
        }
    }

    /// Run the VM for on a given stack and memory to observe how it manipulates the
    /// stack. This is a helper function for [`apply`](Self::apply), which in some cases
    /// just grabs the inner function's code and then needs a VM to apply it.
    fn run_vm(
        instructions: &[LabelledInstruction],
        stack: &mut Vec<BFieldElement>,
        memory: &HashMap<BFieldElement, BFieldElement>,
    ) {
        let Some(LabelledInstruction::Label(label)) = instructions.first() else {
            panic!();
        };
        let instructions = triton_asm!(
            call {label}
            halt
            {&instructions}
        );
        let program = Program::new(&instructions);
        let mut vmstate = VMState::new(program, PublicInput::default(), NonDeterminism::default());
        vmstate.op_stack.stack.clone_from(stack);
        vmstate.ram.clone_from(memory);
        vmstate.run().unwrap();
        *stack = vmstate.op_stack.stack;
    }

    /// Computes the inner function and applies the resulting change to the given stack
    pub fn apply(
        &self,
        stack: &mut Vec<BFieldElement>,
        memory: &HashMap<BFieldElement, BFieldElement>,
    ) {
        match &self {
            InnerFunction::RawCode(rc) => Self::run_vm(&rc.function, stack, memory),
            InnerFunction::DeprecatedSnippet(sn) => {
                sn.rust_shadowing(stack, vec![], vec![], &mut memory.clone());
            }
            InnerFunction::NoFunctionBody(_lnat) => {
                panic!("Cannot apply inner function without function body")
            }
            InnerFunction::BasicSnippet(bs) => {
                let mut snippet_state = Library::new();
                let entrypoint = bs.entrypoint();
                let function_body = bs.annotated_code(&mut snippet_state);
                let library_code = snippet_state.all_imports();

                // The TASM code is always run through a function call, so the 1st instruction
                // is a call to the function in question.
                let code = triton_asm!(
                    call {entrypoint}
                    halt

                    {&function_body}
                    {&library_code}
                );

                Self::run_vm(&code, stack, memory);
            }
        };
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn breakpoint_does_not_influence_raw_code_inlining() {
        let raw_code = RawCode {
            function: triton_asm! { my_label: return break },
            input_type: DataType::VoidPointer,
            output_type: DataType::VoidPointer,
        };
        let inlined_code = raw_code.inlined_body().unwrap();
        assert_eq!(triton_asm!(), inlined_code);
    }

    #[test]
    fn type_hints_do_not_influence_raw_code_inlining() {
        let raw_code = RawCode {
            function: triton_asm! { my_label: hint a = stack[0] hint b = stack[1] return },
            input_type: DataType::VoidPointer,
            output_type: DataType::VoidPointer,
        };
        let inlined_code = raw_code.inlined_body().unwrap();
        assert_eq!(triton_asm!(), inlined_code);
    }

    #[test]
    fn allow_raw_code_with_recurse_or_return_instruction() {
        let raw_code = triton_asm!(
            please_help_me:
                hint im_falling = stack[0]
                hint in_love_with_you = stack[1]

                call close_the_door_to_temptation

                return

                close_the_door_to_temptation:
                    hint turn_away_from_me_darling = stack[5]
                    break
                    merkle_step_mem
                    recurse_or_return
        );
        let raw_code = RawCode::new(raw_code, DataType::VoidPointer, DataType::VoidPointer);
        assert!(
            raw_code.inlined_body().is_none(),
            "Disallow inling of code with `recurse_or_return` instruction"
        );
    }
}