tasm_lib/arithmetic/u64/
decr.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
use std::collections::HashMap;

use triton_vm::prelude::*;

use crate::prelude::*;
use crate::traits::basic_snippet::Reviewer;
use crate::traits::basic_snippet::SignOffFingerprint;

/// [Decrement][dec] the given argument by 1.
///
/// ### Behavior
///
/// ```text
/// BEFORE: _ [arg: u64]
/// AFTER:  _ [arg-1: u64]
/// ```
///
/// ### Preconditions
///
/// - the input `arg` is greater than 0
/// - the input `arg` is properly [`BFieldCodec`] encoded
///
/// ### Postconditions
///
/// - the output is properly [`BFieldCodec`] encoded
///
/// [dec]: num::Integer::dec
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Decr;

impl Decr {
    pub const OVERFLOW_ERROR_ID: i128 = 110;
}

impl BasicSnippet for Decr {
    fn inputs(&self) -> Vec<(DataType, String)> {
        vec![(DataType::U64, "arg".to_string())]
    }

    fn outputs(&self) -> Vec<(DataType, String)> {
        vec![(DataType::U64, "(arg-1)".to_string())]
    }

    fn entrypoint(&self) -> String {
        "tasmlib_arithmetic_u64_decr".to_string()
    }

    fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
        let entrypoint = self.entrypoint();
        let propagate_carry = format!("{entrypoint}_propagate_carry");

        triton_asm!(
            {entrypoint}:
                addi -1
                // _ arg_hi (arg_lo - 1)

                dup 0
                push -1
                eq
                skiz
                    call {propagate_carry}
                return

            // BEFORE: _ arg_hi -1
            // AFTER:  _ (arg_hi - 1) u32::MAX
            {propagate_carry}:
                add
                // _ (arg_hi - 1)

                dup 0
                push -1
                eq
                // _ (arg_hi - 1) (arg_hi - 1 == -1)
                // _ (arg_hi - 1) (arg_hi == 0)

                push 0
                eq
                // _ (arg_hi - 1) (arg_hi != 0)

                assert error_id {Self::OVERFLOW_ERROR_ID}

                push {u32::MAX}
                return
        )
    }

    fn sign_offs(&self) -> HashMap<Reviewer, SignOffFingerprint> {
        let mut sign_offs = HashMap::new();
        sign_offs.insert(Reviewer("ferdinand"), 0xc7d3ab3d4996bc9c.into());
        sign_offs
    }
}

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

    impl Closure for Decr {
        type Args = u64;

        fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
            let arg = pop_encodable::<Self::Args>(stack);
            push_encodable(stack, &arg.checked_sub(1).unwrap());
        }

        fn pseudorandom_args(
            &self,
            seed: [u8; 32],
            bench_case: Option<BenchmarkCase>,
        ) -> Self::Args {
            match bench_case {
                Some(BenchmarkCase::CommonCase) => 7,
                Some(BenchmarkCase::WorstCase) => 1 << 35,
                None => StdRng::from_seed(seed).random_range(1..=u64::MAX),
            }
        }
    }

    #[test]
    fn rust_shadow() {
        ShadowedClosure::new(Decr).test();
    }

    #[test]
    fn negative_test() {
        test_assertion_failure(
            &ShadowedClosure::new(Decr),
            InitVmState::with_stack(Decr.set_up_test_stack(0)),
            &[Decr::OVERFLOW_ERROR_ID],
        );
    }
}

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

    #[test]
    fn benchmark() {
        ShadowedClosure::new(Decr).bench();
    }
}