tasm_lib/neptune/mutator_set/
commit.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
use rand::prelude::*;
use triton_vm::prelude::*;
use triton_vm::twenty_first::prelude::AlgebraicHasher;

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

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

/// Commit computes the addition record for the mutator set AOCL from
///  an item (which is a digest), the sender's randomness (also
/// digest), and the receiver's digest (ditto). Note that the order
/// is:
///  (top of stack -->) - item 0
///                     - item 1
///                     - item 2
///                     - item 3
///                     - item 4
///                     - sender_randomness 0
///                     - sender_randomness 1
///                     - sender_randomness 2
///                     - sender_randomness 3
///                     - sender_randomness 4
///                     - receiver_digest 0
///                     - receiver_digest 1
///                     - receiver_digest 2
///                     - receiver_digest 3
///                     - receiver_digest 4
impl Commit {
    fn test_state() -> InitVmState {
        let item: Digest = random();
        let sender_randomness: Digest = random();
        let receiver_digest: Digest = random();
        let mut stack = empty_stack();
        for d in [
            item.values(),
            sender_randomness.values(),
            receiver_digest.values(),
        ]
        .concat()
        .to_vec()
        .iter()
        .rev()
        {
            stack.push(*d);
        }
        InitVmState::with_stack(stack)
    }
}

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

    fn input_field_names(&self) -> Vec<String>
    where
        Self: Sized,
    {
        vec![
            "receiver_digest4".to_string(),
            "receiver_digest3".to_string(),
            "receiver_digest2".to_string(),
            "receiver_digest1".to_string(),
            "receiver_digest0".to_string(),
            "sender_randomness4".to_string(),
            "sender_randomness3".to_string(),
            "sender_randomness2".to_string(),
            "sender_randomness1".to_string(),
            "sender_randomness0".to_string(),
            "item4".to_string(),
            "item3".to_string(),
            "item2".to_string(),
            "item1".to_string(),
            "item0".to_string(),
        ]
    }

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

    fn output_field_names(&self) -> Vec<String>
    where
        Self: Sized,
    {
        vec![
            "commitment4".to_string(),
            "commitment3".to_string(),
            "commitment2".to_string(),
            "commitment1".to_string(),
            "commitment0".to_string(),
        ]
    }

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

    fn stack_diff(&self) -> isize
    where
        Self: Sized,
    {
        -10
    }

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

        format!(
            "
            // BEFORE: _ r4 r3 r2 r1 r0 s4 s3 s2 s1 s0 i4 i3 i2 i1 i0
            // AFTER:  _ c4 c3 c2 c1 c0
            {entrypoint}:
                hash
                hash
                return
            "
        )
    }

    fn crash_conditions(&self) -> Vec<String>
    where
        Self: Sized,
    {
        vec!["Stack too shallow.".to_string()]
    }

    fn gen_input_states(&self) -> Vec<crate::InitVmState>
    where
        Self: Sized,
    {
        vec![Self::test_state()]
    }

    fn common_case_input_state(&self) -> crate::InitVmState
    where
        Self: Sized,
    {
        Self::test_state()
    }

    fn worst_case_input_state(&self) -> crate::InitVmState
    where
        Self: Sized,
    {
        Self::test_state()
    }

    fn rust_shadowing(
        &self,
        stack: &mut Vec<BFieldElement>,
        _std_in: Vec<BFieldElement>,
        _secret_in: Vec<BFieldElement>,
        _memory: &mut std::collections::HashMap<BFieldElement, BFieldElement>,
    ) where
        Self: Sized,
    {
        let item = Digest::new([
            stack.pop().unwrap(),
            stack.pop().unwrap(),
            stack.pop().unwrap(),
            stack.pop().unwrap(),
            stack.pop().unwrap(),
        ]);
        let sender_randomness = Digest::new([
            stack.pop().unwrap(),
            stack.pop().unwrap(),
            stack.pop().unwrap(),
            stack.pop().unwrap(),
            stack.pop().unwrap(),
        ]);
        let receiver_digest = Digest::new([
            stack.pop().unwrap(),
            stack.pop().unwrap(),
            stack.pop().unwrap(),
            stack.pop().unwrap(),
            stack.pop().unwrap(),
        ]);
        let commitment = VmHasher::hash_pair(
            VmHasher::hash_pair(item, sender_randomness),
            receiver_digest,
        );
        stack.push(commitment.values()[4]);
        stack.push(commitment.values()[3]);
        stack.push(commitment.values()[2]);
        stack.push(commitment.values()[1]);
        stack.push(commitment.values()[0]);
    }
}

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

    #[test]
    fn new_prop_test() {
        test_rust_equivalence_multiple_deprecated(&Commit, true);
    }
}

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

    #[test]
    fn commit_benchmark() {
        bench_and_write(Commit);
    }
}