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

use triton_vm::prelude::*;

use crate::prelude::*;
use crate::traits::basic_snippet::Reviewer;
use crate::traits::basic_snippet::SignOffFingerprint;

/// Set the length of a [`BFieldCodec`]-encoded list in memory.
///
/// This snippet does not perform any checks. It is to be considered “unsafe”.
///
/// ### Behavior
///
/// ```text
/// BEFORE: _ *list [list_length: u32]
/// AFTER:  _ *list
/// ```
///
/// ### Preconditions
///
/// - all input arguments are properly [`BFieldCodec`] encoded
/// - the input argument `*list` is a pointer to a [`BFieldCodec`] encoded list
///
/// ### Postconditions
///
/// None.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct SetLength;

impl BasicSnippet for SetLength {
    fn inputs(&self) -> Vec<(DataType, String)> {
        vec![
            (DataType::VoidPointer, "*list".to_string()),
            (DataType::U32, "list_length".to_string()),
        ]
    }

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

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

    fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
        triton_asm!(
            // BEFORE: _ *list list_length
            // AFTER:  _ *list
            {self.entrypoint()}:
                pick 1      // _ list_length *list
                write_mem 1 // _ (*list + 1)
                addi -1     // _ *list
                return
        )
    }

    fn sign_offs(&self) -> HashMap<Reviewer, SignOffFingerprint> {
        let mut sign_offs = HashMap::new();
        sign_offs.insert(Reviewer("ferdinand"), 0x8306d6fa39eba00a.into());
        sign_offs
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::empty_stack;
    use crate::rust_shadowing_helper_functions::list::list_set_length;
    use crate::test_prelude::*;
    use crate::U32_TO_USIZE_ERR;

    impl Function for SetLength {
        fn rust_shadow(
            &self,
            stack: &mut Vec<BFieldElement>,
            memory: &mut HashMap<BFieldElement, BFieldElement>,
        ) {
            let new_length = pop_encodable::<u32>(stack);
            let list_address = stack.pop().unwrap();
            stack.push(list_address);

            let new_length = new_length.try_into().expect(U32_TO_USIZE_ERR);
            list_set_length(list_address, new_length, memory);
        }

        fn pseudorandom_initial_state(
            &self,
            seed: [u8; 32],
            _: Option<BenchmarkCase>,
        ) -> FunctionInitialState {
            let mut rng = StdRng::from_seed(seed);

            let mut stack = empty_stack();
            stack.push(rng.random());
            stack.push(bfe!(rng.next_u32()));

            FunctionInitialState {
                stack,
                ..Default::default()
            }
        }
    }

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

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

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