use std::collections::HashMap;
use triton_vm::prelude::*;
use crate::prelude::*;
use crate::traits::basic_snippet::Reviewer;
use crate::traits::basic_snippet::SignOffFingerprint;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct TrailingZeros;
impl BasicSnippet for TrailingZeros {
fn parameters(&self) -> Vec<(DataType, String)> {
vec![(DataType::U32, "arg".to_string())]
}
fn return_values(&self) -> Vec<(DataType, String)> {
vec![(DataType::U32, "trailing_zeros(arg)".to_string())]
}
fn entrypoint(&self) -> String {
"tasmlib_arithmetic_u32_trailing_zeros".to_string()
}
fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
let entrypoint = self.entrypoint();
let arg_eq_0 = format!("{entrypoint}_arg_eq_0");
let arg_neq_0 = format!("{entrypoint}_arg_neq_0");
triton_asm! {
{entrypoint}:
push 1
dup 1
push 0
eq
skiz call {arg_eq_0}
skiz call {arg_neq_0}
return
{arg_eq_0}:
pop 2
push 32
push 0
return
{arg_neq_0}:
dup 0
push {u32::MAX}
hint u32_max: u32 = stack[0]
xor
hint bitwise_negated_arg: u32 = stack[0]
addi 1
and
log_2_floor
return
}
}
fn sign_offs(&self) -> HashMap<Reviewer, SignOffFingerprint> {
let mut sign_offs = HashMap::new();
sign_offs.insert(Reviewer("ferdinand"), 0x2ec8110d7af9b4e9.into());
sign_offs
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_prelude::*;
impl Closure for TrailingZeros {
type Args = u32;
fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
let arg = pop_encodable::<Self::Args>(stack);
push_encodable(stack, &arg.trailing_zeros());
}
fn pseudorandom_args(
&self,
seed: [u8; 32],
bench_case: Option<BenchmarkCase>,
) -> Self::Args {
match bench_case {
Some(BenchmarkCase::CommonCase) => 0b1111_1111 << 3,
Some(BenchmarkCase::WorstCase) => 1 << 31,
None => StdRng::from_seed(seed).random(),
}
}
fn corner_case_args(&self) -> Vec<Self::Args> {
[1, 1 << 31, u32::MAX - 1]
.into_iter()
.flat_map(|i| [i - 1, i, i + 1])
.collect()
}
}
#[test]
fn unit() {
ShadowedClosure::new(TrailingZeros).test();
}
}
#[cfg(test)]
mod benches {
use super::*;
use crate::test_prelude::*;
#[test]
fn benchmark() {
ShadowedClosure::new(TrailingZeros).bench()
}
}