tasm_lib/arithmetic/u64/
popcount_u64.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
use num::Zero;
use rand::prelude::*;
use triton_vm::prelude::*;
use triton_vm::twenty_first::prelude::U32s;

use crate::data_type::DataType;
use crate::empty_stack;
use crate::library::Library;
use crate::push_encodable;
use crate::traits::deprecated_snippet::DeprecatedSnippet;
use crate::InitVmState;

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

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

    fn input_field_names(&self) -> Vec<String> {
        vec!["value_hi".to_string(), "value_lo".to_string()]
    }

    fn input_types(&self) -> Vec<DataType> {
        vec![DataType::U64]
    }

    fn output_field_names(&self) -> Vec<String> {
        vec!["popcount".to_string()]
    }

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

    fn stack_diff(&self) -> isize {
        -1
    }

    fn function_code(&self, _library: &mut Library) -> String {
        let entrypoint = self.entrypoint_name();
        format!(
            "
            // BEFORE: _ x_hi x_lo
            // AFTER:  _ popcount
            {entrypoint}:
                pop_count
                swap 1
                pop_count
                add

                return

            "
        )
    }

    fn crash_conditions(&self) -> Vec<String> {
        vec!["Input are not valid u32s".to_string()]
    }

    fn gen_input_states(&self) -> Vec<InitVmState> {
        let mut rng = rand::thread_rng();

        let mut ret = vec![];
        for _ in 0..100 {
            ret.push(prepare_state(rng.next_u64()));
        }

        // add cornercases
        let mut init_stack_zero = empty_stack();
        init_stack_zero.push(BFieldElement::zero());
        init_stack_zero.push(BFieldElement::zero());
        ret.push(InitVmState::with_stack(init_stack_zero));

        let mut init_stack_max_value = empty_stack();
        init_stack_max_value.push(BFieldElement::new((1u64 << 32) - 1));
        init_stack_max_value.push(BFieldElement::new((1u64 << 32) - 1));
        ret.push(InitVmState::with_stack(init_stack_max_value));

        let mut init_stack_max_value2 = empty_stack();
        init_stack_max_value2.push(BFieldElement::new((1u64 << 32) - 1));
        init_stack_max_value2.push(BFieldElement::new((1u64 << 30) - 1));
        ret.push(InitVmState::with_stack(init_stack_max_value2));

        let mut init_stack_max_value3 = empty_stack();
        init_stack_max_value3.push(BFieldElement::new((1u64 << 30) - 1));
        init_stack_max_value3.push(BFieldElement::new((1u64 << 32) - 1));
        ret.push(InitVmState::with_stack(init_stack_max_value3));

        ret
    }

    fn common_case_input_state(&self) -> InitVmState {
        prepare_state(1 << 60)
    }

    fn worst_case_input_state(&self) -> InitVmState {
        prepare_state(1 << 60)
    }

    fn rust_shadowing(
        &self,
        stack: &mut Vec<BFieldElement>,
        _std_in: Vec<BFieldElement>,
        _secret_in: Vec<BFieldElement>,
        _memory: &mut std::collections::HashMap<BFieldElement, BFieldElement>,
    ) {
        // top element on stack
        let a_lo: u32 = stack.pop().unwrap().try_into().unwrap();
        let a_hi: u32 = stack.pop().unwrap().try_into().unwrap();
        let a = ((a_hi as u64) << 32) + a_lo as u64;

        let pop_count = a.count_ones();

        stack.push(BFieldElement::new(pop_count as u64));
    }
}

fn prepare_state(a: u64) -> InitVmState {
    let a = U32s::<2>::try_from(a).unwrap();
    let mut init_stack = empty_stack();
    push_encodable(&mut init_stack, &a);
    InitVmState::with_stack(init_stack)
}

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

    #[test]
    fn popcount_u64_test() {
        test_rust_equivalence_multiple_deprecated(&PopCountU64, true);
    }
}

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

    #[test]
    fn popcount_u64_benchmark() {
        bench_and_write(PopCountU64);
    }
}