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

use super::basic_snippet::BasicSnippet;
use super::rust_shadow::RustShadow;
use crate::linker::execute_bench;
use crate::prelude::Tip5;
use crate::push_encodable;
use crate::snippet_bencher::write_benchmarks;
use crate::snippet_bencher::BenchmarkCase;
use crate::snippet_bencher::NamedBenchmarkResult;
use crate::test_helpers::test_rust_equivalence_given_complete_state;

/// A Closure is a piece of tasm code that modifies the top of the stack without access to
/// memory or nondeterminism or standard input/output.
///
/// See also: [function], [algorithm], [read_only_algorithm], [procedure],
///           [accessor], [mem_preserver]
///
/// [function]: crate::traits::function::Function
/// [algorithm]: crate::traits::algorithm::Algorithm
/// [procedure]: crate::traits::procedure::Procedure
/// [accessor]: crate::traits::accessor::Accessor
/// [mem_preserver]: crate::traits::mem_preserver::MemPreserver
/// [read_only_algorithm]: crate::traits::read_only_algorithm::ReadOnlyAlgorithm
pub trait Closure: BasicSnippet {
    /// The arguments of this closure.
    ///
    /// Because rust does not support variadic types (yet?), it is recommended to
    /// use a tuple if the closure takes more than one argument. Because of the way
    /// [`BFieldCodec`] works for tuples, specify the element the closure expects on
    /// top of the stack as the last element of the tuple.
    type Args: BFieldCodec;

    fn rust_shadow(&self, stack: &mut Vec<BFieldElement>);

    /// Given a [seed](StdRng::Seed), generate pseudorandom [arguments](Self::Args),
    /// from which an [initial stack](Self::set_up_test_stack) can be constructed.
    fn pseudorandom_args(&self, seed: [u8; 32], bench_case: Option<BenchmarkCase>) -> Self::Args;

    fn corner_case_args(&self) -> Vec<Self::Args> {
        vec![]
    }

    fn set_up_test_stack(&self, args: Self::Args) -> Vec<BFieldElement> {
        let mut stack = self.init_stack_for_isolated_run();
        push_encodable(&mut stack, &args);
        stack
    }
}

pub struct ShadowedClosure<C: Closure> {
    closure: C,
}

impl<C: Closure> ShadowedClosure<C> {
    pub fn new(closure: C) -> Self {
        Self { closure }
    }
}

impl<C: Closure> RustShadow for ShadowedClosure<C> {
    fn inner(&self) -> &dyn BasicSnippet {
        &self.closure
    }

    fn rust_shadow_wrapper(
        &self,
        _stdin: &[BFieldElement],
        _nondeterminism: &NonDeterminism,
        stack: &mut Vec<BFieldElement>,
        _memory: &mut std::collections::HashMap<BFieldElement, BFieldElement>,
        _sponge: &mut Option<Tip5>,
    ) -> Vec<BFieldElement> {
        self.closure.rust_shadow(stack);
        vec![]
    }

    fn test(&self) {
        let num_states = 5;
        let mut rng = rand::rng();

        // First test corner-cases as they're easier to debug on failure
        for args in self.closure.corner_case_args() {
            test_rust_equivalence_given_complete_state(
                self,
                &self.closure.set_up_test_stack(args),
                &[],
                &NonDeterminism::default(),
                &None,
                None,
            );
        }

        for _ in 0..num_states {
            let seed: [u8; 32] = rng.random();
            let args = self.closure.pseudorandom_args(seed, None);
            let stack = self.closure.set_up_test_stack(args);

            let stdin = vec![];
            test_rust_equivalence_given_complete_state(
                self,
                &stack,
                &stdin,
                &NonDeterminism::default(),
                &None,
                None,
            );
        }
    }

    fn bench(&self) {
        let mut rng = StdRng::from_seed(
            hex::decode("73a24b6b8b32e4d7d563a4d9a85f476573a24b6b8b32e4d7d563a4d9a85f4765")
                .unwrap()
                .try_into()
                .unwrap(),
        );
        let mut benchmarks = Vec::with_capacity(2);

        for bench_case in [BenchmarkCase::CommonCase, BenchmarkCase::WorstCase] {
            let args = self
                .closure
                .pseudorandom_args(rng.random(), Some(bench_case));
            let stack = self.closure.set_up_test_stack(args);
            let program = self.closure.link_for_isolated_run();
            let benchmark =
                execute_bench(&program, &stack, vec![], NonDeterminism::new(vec![]), None);
            let benchmark = NamedBenchmarkResult {
                name: self.closure.entrypoint(),
                benchmark_result: benchmark,
                case: bench_case,
            };
            benchmarks.push(benchmark);
        }

        write_benchmarks(benchmarks);
    }
}