diff --git a/Cargo.lock b/Cargo.lock index 34a9d70..fd4951b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,6 +48,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + [[package]] name = "anstream" version = "0.3.2" @@ -135,7 +141,7 @@ dependencies = [ "byteorder", "cfg-if", "color-eyre 0.6.2", - "criterion", + "criterion 0.3.6", "ethers-core", "fnv", "hex", @@ -162,7 +168,7 @@ dependencies = [ "byteorder", "cfg-if", "color-eyre 0.5.11", - "criterion", + "criterion 0.3.6", "ethers-core", "fnv", "hex", @@ -190,7 +196,7 @@ dependencies = [ "byteorder", "cfg-if", "color-eyre 0.6.2", - "criterion", + "criterion 0.3.6", "fnv", "hex", "num", @@ -714,6 +720,33 @@ dependencies = [ "num-traits", ] +[[package]] +name = "ciborium" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656" + +[[package]] +name = "ciborium-ll" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b" +dependencies = [ + "ciborium-io", + "half", +] + [[package]] name = "clap" version = "2.34.0" @@ -721,10 +754,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" dependencies = [ "bitflags", - "textwrap", + "textwrap 0.11.0", "unicode-width", ] +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "bitflags", + "clap_lex 0.2.4", + "indexmap", + "textwrap 0.16.0", +] + [[package]] name = "clap" version = "4.2.7" @@ -745,7 +790,7 @@ dependencies = [ "anstream", "anstyle", "bitflags", - "clap_lex", + "clap_lex 0.4.1", "once_cell", "strsim", ] @@ -762,6 +807,15 @@ dependencies = [ "syn 2.0.16", ] +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "clap_lex" version = "0.4.1" @@ -1032,7 +1086,7 @@ dependencies = [ "atty", "cast", "clap 2.34.0", - "criterion-plot", + "criterion-plot 0.4.5", "csv", "itertools", "lazy_static", @@ -1049,6 +1103,32 @@ dependencies = [ "walkdir", ] +[[package]] +name = "criterion" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb" +dependencies = [ + "anes", + "atty", + "cast", + "ciborium", + "clap 3.2.25", + "criterion-plot 0.5.0", + "itertools", + "lazy_static", + "num-traits", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + [[package]] name = "criterion-plot" version = "0.4.5" @@ -1059,6 +1139,16 @@ dependencies = [ "itertools", ] +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools", +] + [[package]] name = "crossbeam-channel" version = "0.5.8" @@ -2146,6 +2236,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "os_str_bytes" +version = "6.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" + [[package]] name = "owo-colors" version = "1.3.0" @@ -3120,6 +3216,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + [[package]] name = "thiserror" version = "1.0.38" @@ -3358,6 +3460,7 @@ dependencies = [ "ark-bn254 0.4.0", "ark-ff 0.4.1", "color-eyre 0.6.2", + "criterion 0.4.0", "hex-literal", "num-bigint", "num-traits", diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 02416ed..3cfeed2 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -3,6 +3,7 @@ name = "utils" version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" +bench = false [dependencies] ark-ff = { version = "=0.4.1", default-features = false, features = ["asm"] } @@ -17,8 +18,13 @@ ark-bn254 = "=0.4.0" num-traits = "0.2.11" hex-literal = "0.3.4" tiny-keccak = { version = "2.0.2", features = ["keccak"] } +criterion = { version = "0.4", features = ["html_reports"] } [features] default = ["parallel"] parallel = ["ark-ff/parallel"] pmtree-ft = ["pmtree"] + +[[bench]] +name = "merkle_tree_benchmark" +harness = false diff --git a/utils/README.md b/utils/README.md new file mode 100644 index 0000000..f3375a0 --- /dev/null +++ b/utils/README.md @@ -0,0 +1,15 @@ +# Utils crate + +## Building + +1. `cargo build` + +## Testing + +1. `cargo test` + +## Benchmarking + +1. `cargo bench` + +To view the results of the benchmark, open the `target/criterion/report/index.html` file generated after the bench \ No newline at end of file diff --git a/utils/benches/merkle_tree_benchmark.rs b/utils/benches/merkle_tree_benchmark.rs new file mode 100644 index 0000000..40d428d --- /dev/null +++ b/utils/benches/merkle_tree_benchmark.rs @@ -0,0 +1,119 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use hex_literal::hex; +use tiny_keccak::{Hasher as _, Keccak}; +use utils::{ + FullMerkleConfig, FullMerkleTree, Hasher, OptimalMerkleConfig, OptimalMerkleTree, + ZerokitMerkleTree, +}; + +#[derive(Clone, Copy, Eq, PartialEq)] +struct Keccak256; + +impl Hasher for Keccak256 { + type Fr = [u8; 32]; + + fn default_leaf() -> Self::Fr { + [0; 32] + } + + fn hash(inputs: &[Self::Fr]) -> Self::Fr { + let mut output = [0; 32]; + let mut hasher = Keccak::v256(); + for element in inputs { + hasher.update(element); + } + hasher.finalize(&mut output); + output + } +} + +pub fn optimal_merkle_tree_benchmark(c: &mut Criterion) { + let mut tree = + OptimalMerkleTree::::new(2, [0; 32], OptimalMerkleConfig::default()).unwrap(); + + let leaves = [ + hex!("0000000000000000000000000000000000000000000000000000000000000001"), + hex!("0000000000000000000000000000000000000000000000000000000000000002"), + hex!("0000000000000000000000000000000000000000000000000000000000000003"), + hex!("0000000000000000000000000000000000000000000000000000000000000004"), + ]; + + c.bench_function("OptimalMerkleTree::set", |b| { + b.iter(|| { + tree.set(0, leaves[0]).unwrap(); + }) + }); + + c.bench_function("OptimalMerkleTree::delete", |b| { + b.iter(|| { + tree.delete(0).unwrap(); + }) + }); + + c.bench_function("OptimalMerkleTree::override_range", |b| { + b.iter(|| { + tree.override_range(0, leaves, [0, 1, 2, 3]).unwrap(); + }) + }); + + c.bench_function("OptimalMerkleTree::compute_root", |b| { + b.iter(|| { + tree.compute_root().unwrap(); + }) + }); + + c.bench_function("OptimalMerkleTree::get", |b| { + b.iter(|| { + tree.get(0).unwrap(); + }) + }); +} + +pub fn full_merkle_tree_benchmark(c: &mut Criterion) { + let mut tree = + FullMerkleTree::::new(2, [0; 32], FullMerkleConfig::default()).unwrap(); + + let leaves = [ + hex!("0000000000000000000000000000000000000000000000000000000000000001"), + hex!("0000000000000000000000000000000000000000000000000000000000000002"), + hex!("0000000000000000000000000000000000000000000000000000000000000003"), + hex!("0000000000000000000000000000000000000000000000000000000000000004"), + ]; + + c.bench_function("FullMerkleTree::set", |b| { + b.iter(|| { + tree.set(0, leaves[0]).unwrap(); + }) + }); + + c.bench_function("FullMerkleTree::delete", |b| { + b.iter(|| { + tree.delete(0).unwrap(); + }) + }); + + c.bench_function("FullMerkleTree::override_range", |b| { + b.iter(|| { + tree.override_range(0, leaves, [0, 1, 2, 3]).unwrap(); + }) + }); + + c.bench_function("FullMerkleTree::compute_root", |b| { + b.iter(|| { + tree.compute_root().unwrap(); + }) + }); + + c.bench_function("FullMerkleTree::get", |b| { + b.iter(|| { + tree.get(0).unwrap(); + }) + }); +} + +criterion_group!( + benches, + optimal_merkle_tree_benchmark, + full_merkle_tree_benchmark +); +criterion_main!(benches);