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

use triton_vm::prelude::*;

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

/// ### Behavior
///
/// ```text
/// BEFORE: _ [exp: u32] [base: XFieldElement]
/// AFTER:  _ [base^(2^exp): XFieldElement]
/// ```
///
/// ### Preconditions
///
/// - all input arguments are properly [`BFieldCodec`] encoded
///
/// ### Postconditions
///
/// None.
//
// todo: Explain the precondition. It seems to be a bit arbitrary. The tests
//   only check `exp` in range 0..32, while the assembly should work fine with
//   any number.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ToThePowerOfPowerOf2;

impl BasicSnippet for ToThePowerOfPowerOf2 {
    fn inputs(&self) -> Vec<(DataType, String)> {
        vec![
            (DataType::U32, "log_2_exponent".to_owned()),
            (DataType::Xfe, "base".to_owned()),
        ]
    }

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

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

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

        triton_asm!(
            {entrypoint}:
                call {loop_label}

                pick 3
                pop 1

                return

            // INVARIANT: _ remainder [acc]
            {loop_label}:
                // end condition: remainder == 0
                dup 3
                push 0
                eq
                skiz
                    return

                // _ remainder [acc]
                dup 2
                dup 2
                dup 2
                xx_mul
                // _ remainder [acc^2]

                pick 3
                addi -1
                place 3
                // _ (remainder - 1) [acc^2]

                recurse
        )
    }

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

#[cfg(test)]
mod tests {
    use twenty_first::math::traits::ModPowU32;

    use super::*;
    use crate::arithmetic::xfe::mod_pow_u32::XfeModPowU32;
    use crate::test_prelude::*;

    impl Closure for ToThePowerOfPowerOf2 {
        type Args = (u32, XFieldElement);

        fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
            let (exponent_log_2, base) = pop_encodable::<Self::Args>(stack);
            let result = base.mod_pow_u32(2_u32.pow(exponent_log_2));
            push_encodable(stack, &result);
        }

        fn pseudorandom_args(
            &self,
            seed: [u8; 32],
            bench_case: Option<BenchmarkCase>,
        ) -> Self::Args {
            let mut rng = StdRng::from_seed(seed);
            let exponent = match bench_case {
                Some(BenchmarkCase::CommonCase) => 20,
                Some(BenchmarkCase::WorstCase) => 31,
                None => rng.random_range(0..32),
            };

            (exponent, rng.random())
        }

        fn corner_case_args(&self) -> Vec<Self::Args> {
            (0..=5)
                .chain([30, 31])
                .map(|log_2_exp| (log_2_exp, xfe!([14; 3])))
                .collect()
        }
    }

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

    #[test]
    fn compare_to_generic_pow_u32() {
        let base = rand::random();
        for log_2_exponent in 0..32 {
            let final_pow_pow_stack = test_rust_equivalence_given_complete_state(
                &ShadowedClosure::new(ToThePowerOfPowerOf2),
                &ToThePowerOfPowerOf2.set_up_test_stack((log_2_exponent, base)),
                &[],
                &NonDeterminism::default(),
                &None,
                None,
            )
            .op_stack
            .stack;

            let final_generic_stack = test_rust_equivalence_given_complete_state(
                &ShadowedClosure::new(XfeModPowU32),
                &XfeModPowU32.set_up_test_stack((2_u32.pow(log_2_exponent), base)),
                &[],
                &NonDeterminism::default(),
                &None,
                None,
            )
            .op_stack
            .stack;

            // Assert that height agrees, and the top-3 elements agree
            assert_eq!(19, final_pow_pow_stack.len());
            assert_eq!(19, final_generic_stack.len());
            assert_eq!(final_pow_pow_stack[16..=18], final_generic_stack[16..=18],);
        }
    }
}

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

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