tasm_lib/verifier/eval_arg/
compute_terminal_from_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
165
166
use triton_vm::prelude::*;

use crate::data_type::DataType;
use crate::library::Library;
use crate::traits::basic_snippet::BasicSnippet;

pub struct ComputeTerminalFromDigestInitialIsOne;

impl BasicSnippet for ComputeTerminalFromDigestInitialIsOne {
    fn inputs(&self) -> Vec<(DataType, String)> {
        vec![
            (DataType::Xfe, "challenge".to_owned()),
            (DataType::Digest, "digest".to_owned()),
        ]
    }

    fn outputs(&self) -> Vec<(DataType, String)> {
        vec![(DataType::Xfe, "terminal".to_owned())]
    }

    fn entrypoint(&self) -> String {
        "tasmlib_verifier_eval_arg_compute_terminal_from_digest".to_owned()
    }

    fn code(&self, _library: &mut Library) -> Vec<LabelledInstruction> {
        let entrypoint = self.entrypoint();

        triton_asm!(
            {entrypoint}:
                // _ [challenge] d4 d3 d2 d1 d0

                dup 7
                dup 7
                dup 7
                // _ [challenge] d4 d3 d2 d1 d0 c2 c1 c0

                dup 3
                add
                // _ [challenge] d4 d3 d2 d1 d0 [challenge + d0]

                dup 10
                dup 10
                dup 10
                xx_mul
                // _ [challenge] d4 d3 d2 d1 d0 [(challenge + d0) * challenge]

                dup 4
                add
                // _ [challenge] d4 d3 d2 d1 d0 [(challenge + d0) * challenge + d1]

                dup 10
                dup 10
                dup 10
                xx_mul
                // _ [challenge] d4 d3 d2 d1 d0 [((challenge + d0) * challenge + d1) * challenge]

                dup 5
                add
                // _ [challenge] d4 d3 d2 d1 d0 [((challenge + d0) * challenge + d1) * challenge + d2]

                dup 10
                dup 10
                dup 10
                xx_mul
                dup 6
                add
                // _ [challenge] d4 d3 d2 d1 d0 [running_eval_3]

                dup 10
                dup 10
                dup 10
                xx_mul
                dup 7
                add
                // _ [challenge] d4 d3 d2 d1 d0 [terminal]

                swap 8
                pop 1
                swap 8
                pop 1
                swap 8
                pop 1
                // _ [terminal] d4 d3 d2 d1 d0

                pop 5
                // _ [terminal]

                return
        )
    }
}

#[cfg(test)]
mod tests {
    use itertools::Itertools;
    use num::One;
    use rand::prelude::*;
    use triton_vm::air::cross_table_argument::CrossTableArg;
    use triton_vm::air::cross_table_argument::EvalArg;

    use super::*;
    use crate::snippet_bencher::BenchmarkCase;
    use crate::traits::closure::Closure;
    use crate::traits::closure::ShadowedClosure;
    use crate::traits::rust_shadow::RustShadow;

    #[test]
    fn compute_terminal_from_digest_pbt() {
        ShadowedClosure::new(ComputeTerminalFromDigestInitialIsOne).test();
    }

    impl Closure for ComputeTerminalFromDigestInitialIsOne {
        fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
            let digest = Digest::new([
                stack.pop().unwrap(),
                stack.pop().unwrap(),
                stack.pop().unwrap(),
                stack.pop().unwrap(),
                stack.pop().unwrap(),
            ]);

            let challenge = XFieldElement::new([
                stack.pop().unwrap(),
                stack.pop().unwrap(),
                stack.pop().unwrap(),
            ]);

            let result = EvalArg::compute_terminal(&digest.0, XFieldElement::one(), challenge);

            for elem in result.coefficients.into_iter().rev() {
                stack.push(elem);
            }
        }

        fn pseudorandom_initial_state(
            &self,
            seed: [u8; 32],
            _bench_case: Option<BenchmarkCase>,
        ) -> Vec<BFieldElement> {
            let mut rng: StdRng = SeedableRng::from_seed(seed);
            let challenge = rng.gen();
            let digest = rng.gen();
            self.prepare_state(challenge, digest)
        }
    }

    impl ComputeTerminalFromDigestInitialIsOne {
        fn prepare_state(&self, challenge: XFieldElement, digest: Digest) -> Vec<BFieldElement> {
            let challenge = challenge.coefficients.into_iter().rev().collect_vec();
            let digest = digest.values().into_iter().rev().collect_vec();
            [self.init_stack_for_isolated_run(), challenge, digest].concat()
        }
    }
}

#[cfg(test)]
mod benches {
    use super::*;
    use crate::traits::closure::ShadowedClosure;
    use crate::traits::rust_shadow::RustShadow;

    #[test]
    fn compute_terminal_from_digest_bench() {
        ShadowedClosure::new(ComputeTerminalFromDigestInitialIsOne).bench()
    }
}