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, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Sub;
impl Sub {
pub const OVERFLOW_ERROR_ID: i128 = 520;
}
impl BasicSnippet for Sub {
fn parameters(&self) -> Vec<(DataType, String)> {
["subtrahend", "minuend"]
.map(|s| (DataType::U128, s.to_string()))
.to_vec()
}
fn return_values(&self) -> Vec<(DataType, String)> {
vec![(DataType::U128, "difference".to_string())]
}
fn entrypoint(&self) -> String {
"tasmlib_arithmetic_u128_sub".to_string()
}
fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
triton_asm!(
{self.entrypoint()}:
pick 4
push -1
mul
add push {1_u64 << 32}
add
split place 7
push 0
eq pick 4
add
push -1
mul add push {1_u64 << 32}
add
split place 6
push 0
eq pick 3
add
push -1
mul add push {1_u64 << 32}
add
split place 5
push 0
eq pick 2
add
push -1
mul add
split place 4
push 0
eq
assert error_id {Self::OVERFLOW_ERROR_ID}
return
)
}
fn sign_offs(&self) -> HashMap<Reviewer, SignOffFingerprint> {
let mut sign_offs = HashMap::new();
sign_offs.insert(Reviewer("ferdinand"), 0xd1effbb4a16f9979.into());
sign_offs
}
}
#[cfg(test)]
mod tests {
use BFieldElement;
use super::*;
use crate::test_prelude::*;
impl Closure for Sub {
type Args = (u128, u128);
fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
let (subtrahend, minuend) = pop_encodable::<Self::Args>(stack);
let difference = minuend.checked_sub(subtrahend).unwrap();
push_encodable(stack, &difference);
}
fn pseudorandom_args(
&self,
seed: [u8; 32],
bench_case: Option<BenchmarkCase>,
) -> Self::Args {
let Some(bench_case) = bench_case else {
let mut rng = StdRng::from_seed(seed);
let subtrahend = rng.random();
let minuend = rng.random_range(subtrahend..=u128::MAX);
return (subtrahend, minuend);
};
match bench_case {
BenchmarkCase::CommonCase => (1 << 126, 1 << 127),
BenchmarkCase::WorstCase => ((1 << 126) + (1 << 56), (1 << 127) + (1 << 64)),
}
}
fn corner_case_args(&self) -> Vec<Self::Args> {
vec![
(0, 0),
(0, 1),
(1, 1),
(1, 0xffff_ffff_ffff_ffff_ffff_ffff_0000_0000),
(2, 0xffff_ffff_ffff_ffff_0000_0000_0000_0001),
(3, 0xffff_ffff_0000_0000_0000_0000_0000_0002),
(4, 1 << 127),
(u32::MAX.into(), u32::MAX.into()),
(u64::MAX.into(), u64::MAX.into()),
(u128::MAX, u128::MAX),
]
}
}
#[test]
fn rust_shadow() {
ShadowedClosure::new(Sub).test();
}
#[proptest]
fn overflow_crashes_vm(
#[strategy(1_u128..)] subtrahend: u128,
#[strategy(..#subtrahend)] minuend: u128,
) {
test_assertion_failure(
&ShadowedClosure::new(Sub),
InitVmState::with_stack(Sub.set_up_test_stack((subtrahend, minuend))),
&[Sub::OVERFLOW_ERROR_ID],
);
}
}
#[cfg(test)]
mod benches {
use super::*;
use crate::test_prelude::*;
#[test]
fn benchmark() {
ShadowedClosure::new(Sub).bench();
}
}