tasm_lib/hashing/
swap_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
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
use std::collections::HashMap;

use itertools::Itertools;
use rand::prelude::*;
use triton_vm::prelude::*;

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

#[derive(Clone, Debug)]
pub struct SwapDigest;

impl DeprecatedSnippet for SwapDigest {
    fn entrypoint_name(&self) -> String {
        "tasmlib_hashing_swap_digest".to_string()
    }

    fn input_field_names(&self) -> Vec<String> {
        vec![
            "b4".to_string(),
            "b3".to_string(),
            "b2".to_string(),
            "b1".to_string(),
            "b0".to_string(),
            "a4".to_string(),
            "a3".to_string(),
            "a2".to_string(),
            "a1".to_string(),
            "a0".to_string(),
        ]
    }

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

    fn output_field_names(&self) -> Vec<String> {
        vec![
            "a4".to_string(),
            "a3".to_string(),
            "a2".to_string(),
            "a1".to_string(),
            "a0".to_string(),
            "b4".to_string(),
            "b3".to_string(),
            "b2".to_string(),
            "b1".to_string(),
            "b0".to_string(),
        ]
    }

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

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

    fn function_code(&self, _library: &mut Library) -> String {
        let entrypoint = self.entrypoint_name();
        triton_asm!(
            // Before: _ b4 b3 b2 b1 b0 a4 a3 a2 a1 a0
            // After:  _ a4 a3 a2 a1 a0 b4 b3 b2 b1 b0
            {entrypoint}:
                // a4 <-> b4:
                swap 4 // _ b4 b3 b2 b1 b0 a0 a3 a2 a1 a4
                swap 9 // _ a4 b3 b2 b1 b0 a0 a3 a2 a1 b4
                swap 4 // _ a4 b3 b2 b1 b0 b4 a3 a2 a1 a0

                // a3 <-> b3:
                swap 3 // _ a4 b3 b2 b1 b0 b4 a0 a2 a1 a3
                swap 8 // _ a4 a3 b2 b1 b0 b4 a0 a2 a1 b3
                swap 3 // _ a4 a3 b2 b1 b0 b4 b3 a2 a1 a0

                // a2 <-> b2
                swap 2 // _ a4 a3 b2 b1 b0 b4 b3 a0 a1 a2
                swap 7 // _ a4 a3 a2 b1 b0 b4 b3 a0 a1 b2
                swap 2 // _ a4 a3 a2 b1 b0 b4 b3 b2 a1 a0

                // a1 <-> b1
                swap 1 // _ a4 a3 a2 b1 b0 b4 b3 b2 a0 a1
                swap 6 // _ a4 a3 a2 a1 b0 b4 b3 b2 a0 b1
                swap 1 // _ a4 a3 a2 a1 b0 b4 b3 b2 b1 a0

                // a0 <-> b0
                swap 5 // _ a4 a3 a2 a1 a0 b4 b3 b2 b1 b0

                return
        )
        .iter()
        .join("\n")
    }

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

    fn gen_input_states(&self) -> Vec<InitVmState> {
        let mut rng = rand::thread_rng();
        let digest_a: Digest = rng.gen();
        let digest_b: Digest = rng.gen();

        let mut stack = empty_stack();
        push_encodable(&mut stack, &digest_b);
        push_encodable(&mut stack, &digest_a);

        vec![InitVmState::with_stack(stack)]
    }

    fn common_case_input_state(&self) -> InitVmState {
        let mut rng = rand::thread_rng();
        let mut stack = empty_stack();
        push_encodable(&mut stack, &rng.gen::<Digest>());
        push_encodable(&mut stack, &rng.gen::<Digest>());
        InitVmState::with_stack(stack)
    }

    fn worst_case_input_state(&self) -> InitVmState {
        self.common_case_input_state()
    }

    fn rust_shadowing(
        &self,
        stack: &mut Vec<BFieldElement>,
        _std_in: Vec<BFieldElement>,
        _secret_in: Vec<BFieldElement>,
        _memory: &mut HashMap<BFieldElement, BFieldElement>,
    ) {
        let h = stack.len();
        for i in 0..Digest::LEN {
            let ai_pos = h - i - 1;
            let bi_pos = h - i - Digest::LEN - 1;
            stack.swap(ai_pos, bi_pos);
        }
    }
}

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

    #[test]
    fn swap_digest_test() {
        test_rust_equivalence_multiple_deprecated(&SwapDigest, true);
    }
}

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

    #[test]
    fn swap_digest_benchmark() {
        bench_and_write(SwapDigest);
    }
}