tasm_lib/hashing/
reverse_digest.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
use rand::prelude::*;
use triton_vm::prelude::*;

use crate::data_type::DataType;
use crate::empty_stack;
use crate::traits::deprecated_snippet::DeprecatedSnippet;
use crate::Digest;
use crate::InitVmState;

/// Reverse the order of elements in a digest: [d4, d3, d2, d1, d0] -> [d0, d1, d2, d3, d4]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ReverseDigest;

impl ReverseDigest {
    fn get_input_state() -> InitVmState {
        let digest: Digest = random();
        let stack = [empty_stack(), digest.values().to_vec()].concat();
        InitVmState::with_stack(stack)
    }
}

impl DeprecatedSnippet for ReverseDigest {
    fn entrypoint_name(&self) -> String {
        "tasmlib_hashing_reverse_digest".to_owned()
    }

    fn input_field_names(&self) -> Vec<String> {
        vec![
            "d4".to_owned(),
            "d3".to_owned(),
            "d2".to_owned(),
            "d1".to_owned(),
            "d0".to_owned(),
        ]
    }

    fn input_types(&self) -> Vec<DataType> {
        vec![DataType::Digest]
    }

    fn output_field_names(&self) -> Vec<String> {
        vec![
            "d0".to_owned(),
            "d1".to_owned(),
            "d2".to_owned(),
            "d3".to_owned(),
            "d4".to_owned(),
        ]
    }

    fn output_types(&self) -> Vec<DataType> {
        vec![DataType::Digest]
    }

    fn stack_diff(&self) -> isize {
        0
    }

    fn function_code(&self, _library: &mut crate::library::Library) -> String {
        let entrypoint = self.entrypoint_name();

        format!(
            "
            // BEFORE: _ d4 d3 d2 d1 d0
            // AFTER:  _ d0 d1 d2 d3 d4
            {entrypoint}:
                swap 4
                // _ d0 d3 d2 d1 d4
                swap 1
                // _ d0 d3 d2 d4 d1
                swap 3
                // _ d0 d1 d2 d4 d3

                swap 1
                // _ d0 d1 d2 d3 d4

                // _ d0 d1 d2 d3 d4

                return
            "
        )
    }

    fn crash_conditions(&self) -> Vec<String> {
        vec![]
    }

    fn gen_input_states(&self) -> Vec<InitVmState> {
        vec![Self::get_input_state()]
    }

    fn common_case_input_state(&self) -> InitVmState {
        Self::get_input_state()
    }

    fn worst_case_input_state(&self) -> InitVmState {
        Self::get_input_state()
    }

    fn rust_shadowing(
        &self,
        stack: &mut Vec<BFieldElement>,
        _std_in: Vec<BFieldElement>,
        _secret_in: Vec<BFieldElement>,
        _memory: &mut std::collections::HashMap<BFieldElement, BFieldElement>,
    ) {
        let mut elems = vec![];
        for _ in 0..Digest::LEN {
            elems.push(stack.pop().unwrap())
        }

        for elem in elems {
            stack.push(elem);
        }
    }
}

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

    #[test]
    fn reverse_digest_test() {
        test_rust_equivalence_multiple_deprecated(&ReverseDigest, true);
    }
}

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

    #[test]
    fn reverse_digest_benchmark() {
        bench_and_write(ReverseDigest);
    }
}