tasm_lib/arithmetic/u64/
decr.rsuse std::collections::HashMap;
use triton_vm::prelude::*;
use crate::prelude::*;
use crate::traits::basic_snippet::Reviewer;
use crate::traits::basic_snippet::SignOffFingerprint;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Decr;
impl Decr {
pub const OVERFLOW_ERROR_ID: i128 = 110;
}
impl BasicSnippet for Decr {
fn inputs(&self) -> Vec<(DataType, String)> {
vec![(DataType::U64, "arg".to_string())]
}
fn outputs(&self) -> Vec<(DataType, String)> {
vec![(DataType::U64, "(arg-1)".to_string())]
}
fn entrypoint(&self) -> String {
"tasmlib_arithmetic_u64_decr".to_string()
}
fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
let entrypoint = self.entrypoint();
let propagate_carry = format!("{entrypoint}_propagate_carry");
triton_asm!(
{entrypoint}:
addi -1
dup 0
push -1
eq
skiz
call {propagate_carry}
return
{propagate_carry}:
add
dup 0
push -1
eq
push 0
eq
assert error_id {Self::OVERFLOW_ERROR_ID}
push {u32::MAX}
return
)
}
fn sign_offs(&self) -> HashMap<Reviewer, SignOffFingerprint> {
let mut sign_offs = HashMap::new();
sign_offs.insert(Reviewer("ferdinand"), 0xc7d3ab3d4996bc9c.into());
sign_offs
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_prelude::*;
impl Closure for Decr {
type Args = u64;
fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
let arg = pop_encodable::<Self::Args>(stack);
push_encodable(stack, &arg.checked_sub(1).unwrap());
}
fn pseudorandom_args(
&self,
seed: [u8; 32],
bench_case: Option<BenchmarkCase>,
) -> Self::Args {
match bench_case {
Some(BenchmarkCase::CommonCase) => 7,
Some(BenchmarkCase::WorstCase) => 1 << 35,
None => StdRng::from_seed(seed).random_range(1..=u64::MAX),
}
}
}
#[test]
fn rust_shadow() {
ShadowedClosure::new(Decr).test();
}
#[test]
fn negative_test() {
test_assertion_failure(
&ShadowedClosure::new(Decr),
InitVmState::with_stack(Decr.set_up_test_stack(0)),
&[Decr::OVERFLOW_ERROR_ID],
);
}
}
#[cfg(test)]
mod benches {
use super::*;
use crate::test_prelude::*;
#[test]
fn benchmark() {
ShadowedClosure::new(Decr).bench();
}
}