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 Lt;
impl BasicSnippet for Lt {
fn parameters(&self) -> Vec<(DataType, String)> {
["rhs", "lhs"]
.map(|s| (DataType::U64, s.to_owned()))
.to_vec()
}
fn return_values(&self) -> Vec<(DataType, String)> {
vec![(DataType::Bool, "lhs < rhs".to_owned())]
}
fn entrypoint(&self) -> String {
"tasmlib_arithmetic_u64_lt".to_owned()
}
fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
triton_asm!(
{self.entrypoint()}:
dup 3
dup 2
lt
pick 4
pick 3
eq
pick 3
pick 3
lt
mul
add
return
)
}
fn sign_offs(&self) -> HashMap<Reviewer, SignOffFingerprint> {
let mut sign_offs = HashMap::new();
sign_offs.insert(Reviewer("ferdinand"), 0x18418167b2d68326.into());
sign_offs
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::test_prelude::*;
impl Lt {
pub fn assert_expected_lt_behavior(&self, lhs: u64, rhs: u64) {
let initial_stack = self.set_up_test_stack((lhs, rhs));
let mut expected_stack = initial_stack.clone();
self.rust_shadow(&mut expected_stack).unwrap();
test_rust_equivalence_given_complete_state(
&ShadowedClosure::new(Self),
&initial_stack,
&[],
&NonDeterminism::default(),
&None,
Some(&expected_stack),
);
}
}
impl Closure for Lt {
type Args = (u64, u64);
fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) -> Result<(), RustShadowError> {
let (right, left) = pop_encodable::<Self::Args>(stack)?;
push_encodable(stack, &(left < right));
Ok(())
}
fn pseudorandom_args(
&self,
seed: [u8; 32],
bench_case: Option<BenchmarkCase>,
) -> Self::Args {
match bench_case {
Some(BenchmarkCase::CommonCase) => (0x100_ffff_ffff, 0x100_ffff_fffe),
Some(BenchmarkCase::WorstCase) => (u64::MAX - 1, u64::MAX),
None => StdRng::from_seed(seed).random(),
}
}
fn corner_case_args(&self) -> Vec<Self::Args> {
let edge_case_points = [0, 1 << 29, 1 << 31, 1 << 32, u64::MAX]
.into_iter()
.flat_map(|p| [p.checked_sub(1), Some(p), p.checked_add(1)])
.flatten()
.collect_vec();
edge_case_points
.iter()
.cartesian_product(&edge_case_points)
.map(|(&left, &right)| (left, right))
.collect()
}
}
#[macro_rules_attr::apply(test)]
fn rust_shadow_test() {
ShadowedClosure::new(Lt).test()
}
#[macro_rules_attr::apply(test)]
fn unit_test() {
Lt.assert_expected_lt_behavior(11 * (1 << 32), 15 * (1 << 32));
}
#[macro_rules_attr::apply(proptest)]
fn property_test(left: u64, right: u64) {
Lt.assert_expected_lt_behavior(left, right);
}
}
#[cfg(test)]
mod benches {
use super::*;
use crate::test_prelude::*;
#[macro_rules_attr::apply(test)]
fn benchmark() {
ShadowedClosure::new(Lt).bench();
}
}