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

use triton_vm::prelude::*;

use crate::prelude::*;
use crate::rust_shadowing_helper_functions::list::list_new;
use crate::snippet_bencher::BenchmarkCase;
use crate::traits::function::Function;
use crate::traits::function::FunctionInitialState;

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct New;

impl BasicSnippet for New {
    fn inputs(&self) -> Vec<(DataType, String)> {
        vec![]
    }

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

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

    fn code(&self, library: &mut Library) -> Vec<LabelledInstruction> {
        let dyn_malloc = library.import(Box::new(DynMalloc));

        triton_asm!(
            // BEFORE: _
            // AFTER:  _ *list
            {self.entrypoint()}:
                call {dyn_malloc}
                            // _ *list

                /* write initial length = 0 to `*list` */
                push 0
                pick 1
                write_mem 1
                addi -1
                            // _ *list

                return
        )
    }
}

impl Function for New {
    fn rust_shadow(
        &self,
        stack: &mut Vec<BFieldElement>,
        memory: &mut HashMap<BFieldElement, BFieldElement>,
    ) {
        DynMalloc.rust_shadow(stack, memory);

        let &list_pointer = stack.last().unwrap();
        list_new(list_pointer, memory);
    }

    fn pseudorandom_initial_state(
        &self,
        _: [u8; 32],
        _: Option<BenchmarkCase>,
    ) -> FunctionInitialState {
        FunctionInitialState {
            stack: self.init_stack_for_isolated_run(),
            ..Default::default()
        }
    }
}

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

    #[test]
    fn rust_shadow() {
        ShadowedFunction::new(New).test();
    }
}

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

    #[test]
    fn benchmark() {
        ShadowedFunction::new(New).bench();
    }
}