From c93b3fc70c03a50c22855f34a7ef8a6a38977156 Mon Sep 17 00:00:00 2001 From: Mario Rugiero Date: Mon, 10 Apr 2023 15:58:06 -0300 Subject: [PATCH] Add Pedersen hash benchmark (#950) --- bench/criterion_benchmark.rs | 1 + bench/iai_benchmark.rs | 2 ++ cairo_programs/benchmarks/pedersen.cairo | 45 ++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 cairo_programs/benchmarks/pedersen.cairo diff --git a/bench/criterion_benchmark.rs b/bench/criterion_benchmark.rs index 5c72147b53..280dca47c7 100644 --- a/bench/criterion_benchmark.rs +++ b/bench/criterion_benchmark.rs @@ -29,6 +29,7 @@ const BENCH_NAMES: &[&str] = &[ "operations_with_data_structures_benchmarks", "uint256_integration_benchmark", "set_integration_benchmark", + "pedersen", ]; const BENCH_PATH: &str = "cairo_programs/benchmarks/"; diff --git a/bench/iai_benchmark.rs b/bench/iai_benchmark.rs index 2f325adb1b..bc77fa9b86 100644 --- a/bench/iai_benchmark.rs +++ b/bench/iai_benchmark.rs @@ -115,6 +115,7 @@ iai_bench_expand_prog! {math_cmp_and_pow_integration_benchmark} iai_bench_expand_prog! {operations_with_data_structures_benchmarks} iai_bench_expand_prog! {uint256_integration_benchmark} iai_bench_expand_prog! {set_integration_benchmark} +iai_bench_expand_prog! {pedersen} main!( math_integration_benchmark, @@ -132,4 +133,5 @@ main!( operations_with_data_structures_benchmarks, uint256_integration_benchmark, set_integration_benchmark, + pedersen, ); diff --git a/cairo_programs/benchmarks/pedersen.cairo b/cairo_programs/benchmarks/pedersen.cairo new file mode 100644 index 0000000000..457ffdbf87 --- /dev/null +++ b/cairo_programs/benchmarks/pedersen.cairo @@ -0,0 +1,45 @@ +%builtins pedersen + +from starkware.cairo.common.cairo_builtins import HashBuiltin +from starkware.cairo.common.hash import hash2 + +func get_hash(hash_ptr: HashBuiltin*, num_a: felt, num_b: felt) -> ( + hash_ptr: HashBuiltin*, r: felt +) { + with hash_ptr { + let (result) = hash2(num_a, num_b); + } + return (hash_ptr=hash_ptr, r=result); +} + +func builtins_wrapper{ + pedersen_ptr: HashBuiltin*, +}(num_a: felt, num_b: felt) { + let (pedersen_ptr, result: felt) = get_hash(pedersen_ptr, num_a, num_b); + + return (); +} + +func builtins_wrapper_iter{ + pedersen_ptr: HashBuiltin*, +}(num_a: felt, num_b: felt, n_iterations: felt) { + builtins_wrapper(num_a, num_b); + if (n_iterations != 0) { + builtins_wrapper_iter(num_a, num_b, n_iterations - 1); + tempvar pedersen_ptr = pedersen_ptr; + } else { + tempvar pedersen_ptr = pedersen_ptr; + } + + return (); +} + +func main{ + pedersen_ptr: HashBuiltin*, +}() { + let num_a = 123568; + let num_b = 5673940; + builtins_wrapper_iter(num_a, num_b, 50000); + + return (); +}