Skip to content

Commit

Permalink
test: Test DataType::compare()
Browse files Browse the repository at this point in the history
Also benchmark it while I'm at it.
  • Loading branch information
jan-ferdinand committed Dec 19, 2024
1 parent ce5a684 commit ad6c580
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tasm-lib/benchmarks/tasmlib_test_compare_Bfe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "tasmlib_test_compare_Bfe",
"benchmark_result": {
"clock_cycle_count": 4,
"hash_table_height": 6,
"u32_table_height": 0,
"op_stack_table_height": 1,
"ram_table_height": 0
},
"case": "CommonCase"
},
{
"name": "tasmlib_test_compare_Bfe",
"benchmark_result": {
"clock_cycle_count": 4,
"hash_table_height": 6,
"u32_table_height": 0,
"op_stack_table_height": 1,
"ram_table_height": 0
},
"case": "WorstCase"
}
]
24 changes: 24 additions & 0 deletions tasm-lib/benchmarks/tasmlib_test_compare_Digest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "tasmlib_test_compare_Digest",
"benchmark_result": {
"clock_cycle_count": 17,
"hash_table_height": 18,
"u32_table_height": 0,
"op_stack_table_height": 9,
"ram_table_height": 0
},
"case": "CommonCase"
},
{
"name": "tasmlib_test_compare_Digest",
"benchmark_result": {
"clock_cycle_count": 17,
"hash_table_height": 18,
"u32_table_height": 0,
"op_stack_table_height": 9,
"ram_table_height": 0
},
"case": "WorstCase"
}
]
78 changes: 78 additions & 0 deletions tasm-lib/src/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,81 @@ mod tests {
prop_assert!(<[[Digest; INNER_LEN]; OUTER_LEN]>::decode(&array).is_ok());
}
}

/// Test [`DataType::compare`] by wrapping it in [`BasicSnippet`] and
/// implementing [`RustShadow`] for it.
#[cfg(test)]
mod compare_literals {
use super::*;
use crate::prelude::*;
use crate::test_prelude::*;

macro_rules! comparison_snippet {
($name:ident for tasm_ty $tasm_ty:ident and rust_ty $rust_ty:ident) => {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
struct $name;

impl BasicSnippet for $name {
fn inputs(&self) -> Vec<(DataType, String)> {
["left", "right"]
.map(|s| (DataType::$tasm_ty, s.to_string()))
.to_vec()
}

fn outputs(&self) -> Vec<(DataType, String)> {
vec![(DataType::Bool, "are_eq".to_string())]
}

fn entrypoint(&self) -> String {
let ty = stringify!($tasm_ty);
format!("tasmlib_test_compare_{ty}")
}

fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
triton_asm!({self.entrypoint()}: {&DataType::$tasm_ty.compare()} return)
}
}

impl Closure for $name {
type Args = ($rust_ty, $rust_ty);

fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
let (right, left) = pop_encodable::<Self::Args>(stack);
push_encodable(stack, &(left == right));
}

fn pseudorandom_args(
&self,
seed: [u8; 32],
_: Option<BenchmarkCase>
) -> Self::Args {
// almost certainly different arguments, comparison gives `false`
StdRng::from_seed(seed).gen()
}

fn corner_case_args(&self) -> Vec<Self::Args> {
// identical arguments, comparison gives `true`
vec![Self::Args::default()]
}
}
};
}

// stack size == 1
comparison_snippet!(CompareBfes for tasm_ty Bfe and rust_ty BFieldElement);

// stack size > 1
comparison_snippet!(CompareDigests for tasm_ty Digest and rust_ty Digest);

#[test]
fn test() {
ShadowedClosure::new(CompareBfes).test();
ShadowedClosure::new(CompareDigests).test();
}

#[test]
fn bench() {
ShadowedClosure::new(CompareBfes).bench();
ShadowedClosure::new(CompareDigests).bench();
}
}

0 comments on commit ad6c580

Please sign in to comment.