diff --git a/Cargo.lock b/Cargo.lock index 5528f2568c..dcf2d820d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -375,9 +375,9 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf2b3e8478797446514c91ef04bafcb59faba183e621ad488df88983cc14128c" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" dependencies = [ "cfg-if", "crossbeam-utils", @@ -507,13 +507,13 @@ dependencies = [ [[package]] name = "errno" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d6a0976c999d473fe89ad888d5a284e55366d9dc9038b1ba2aa15128c4afa0" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" dependencies = [ "errno-dragonfly", "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -574,9 +574,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" dependencies = [ "cfg-if", "js-sys", @@ -679,13 +679,13 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" +checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" dependencies = [ "hermit-abi 0.3.1", "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -732,9 +732,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.140" +version = "0.2.141" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" +checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" [[package]] name = "libm" @@ -744,9 +744,9 @@ checksum = "348108ab3fba42ec82ff6e9564fc4ca0247bdccdc68dd8af9764bbc79c3c8ffb" [[package]] name = "libmimalloc-sys" -version = "0.1.30" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8c7cbf8b89019683667e347572e6d55a7df7ea36b0c4ce69961b0cde67b174" +checksum = "43a558e3d911bc3c7bfc8c78bc580b404d6e51c1cefbf656e176a94b49b0df40" dependencies = [ "cc", "libc", @@ -784,9 +784,9 @@ dependencies = [ [[package]] name = "mimalloc" -version = "0.1.34" +version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dcb174b18635f7561a0c6c9fc2ce57218ac7523cf72c50af80e2d79ab8f3ba1" +checksum = "3d88dad3f985ec267a3fcb7a1726f5cb1a7e8cad8b646e70a84f967210df23da" dependencies = [ "libmimalloc-sys", ] @@ -949,9 +949,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.54" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] @@ -1111,16 +1111,16 @@ dependencies = [ [[package]] name = "rustix" -version = "0.37.6" +version = "0.37.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d097081ed288dfe45699b72f5b5d648e5f15d64d900c7080273baa20c16a6849" +checksum = "85597d61f83914ddeba6a47b3b8ffe7365107221c2e557ed94426489fefb5f77" dependencies = [ "bitflags", "errno", "io-lifetimes", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1228,7 +1228,7 @@ checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" dependencies = [ "proc-macro2", "quote", - "syn 2.0.12", + "syn 2.0.13", ] [[package]] @@ -1346,9 +1346,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.12" +version = "2.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" +checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec" dependencies = [ "proc-macro2", "quote", @@ -1371,7 +1371,7 @@ dependencies = [ "fastrand", "redox_syscall", "rustix", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -1415,7 +1415,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.12", + "syn 2.0.13", ] [[package]] @@ -1661,7 +1661,16 @@ version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" dependencies = [ - "windows-targets", + "windows-targets 0.42.2", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.0", ] [[package]] @@ -1670,13 +1679,28 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-targets" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +dependencies = [ + "windows_aarch64_gnullvm 0.48.0", + "windows_aarch64_msvc 0.48.0", + "windows_i686_gnu 0.48.0", + "windows_i686_msvc 0.48.0", + "windows_x86_64_gnu 0.48.0", + "windows_x86_64_gnullvm 0.48.0", + "windows_x86_64_msvc 0.48.0", ] [[package]] @@ -1685,42 +1709,84 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + [[package]] name = "windows_aarch64_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + [[package]] name = "windows_i686_gnu" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + [[package]] name = "windows_i686_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + [[package]] name = "windows_x86_64_gnu" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + [[package]] name = "windows_x86_64_msvc" version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + [[package]] name = "wyz" version = "0.5.1" @@ -1747,5 +1813,5 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.12", + "syn 2.0.13", ] diff --git a/bench/criterion_benchmark.rs b/bench/criterion_benchmark.rs index 280dca47c7..1e2d819e2f 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", + "poseidon_integration_benchmark", "pedersen", ]; const BENCH_PATH: &str = "cairo_programs/benchmarks/"; diff --git a/bench/iai_benchmark.rs b/bench/iai_benchmark.rs index bc77fa9b86..8c76dc802a 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! {poseidon_integration_benchmark} iai_bench_expand_prog! {pedersen} main!( @@ -133,5 +134,6 @@ main!( operations_with_data_structures_benchmarks, uint256_integration_benchmark, set_integration_benchmark, + poseidon_integration_benchmark pedersen, ); diff --git a/cairo_programs/benchmarks/poseidon_integration_benchmark.cairo b/cairo_programs/benchmarks/poseidon_integration_benchmark.cairo new file mode 100644 index 0000000000..742c9efc16 --- /dev/null +++ b/cairo_programs/benchmarks/poseidon_integration_benchmark.cairo @@ -0,0 +1,8 @@ +from poseidon_multirun import run_test +from starkware.cairo.common.cairo_builtins import PoseidonBuiltin + +func main{poseidon_ptr: PoseidonBuiltin*}() -> () { + // For each test, 3 poseidon hashes are computed (single, 2 values, and 3 values) + run_test(10000); + return (); +} diff --git a/cairo_programs/poseidon_multirun.cairo b/cairo_programs/poseidon_multirun.cairo new file mode 100644 index 0000000000..1c28c3aaff --- /dev/null +++ b/cairo_programs/poseidon_multirun.cairo @@ -0,0 +1,79 @@ +%builtins poseidon +from starkware.cairo.common.cairo_builtins import PoseidonBuiltin +from starkware.cairo.common.builtin_poseidon.poseidon import ( + poseidon_hash, + poseidon_hash_single, + poseidon_hash_many, +) +from starkware.cairo.common.alloc import alloc + +func construct_array(base: felt, step: felt, length: felt) -> felt* { + alloc_locals; + let array: felt* = alloc(); + fill_array(base, step, length, 0, array); + return array; +} + +func fill_array(base: felt, step: felt, length: felt, acc: felt, array: felt*) { + alloc_locals; + if (acc == length) { + return (); + } + assert array[acc] = base + acc * step; + return fill_array(base, step, length, acc + 1, array); +} + +func compute_poseidon_single{poseidon_ptr: PoseidonBuiltin*}( + array: felt*, length: felt, acc: felt +) { + if (acc == length) { + return (); + } + let (r) = poseidon_hash_single(array[acc]); + return compute_poseidon_single(array, length, acc + 1); +} + +func compute_poseidon_double{poseidon_ptr: PoseidonBuiltin*}( + array: felt*, length: felt, acc: felt +) { + if (acc == length) { + return (); + } + let (r) = poseidon_hash(array[acc], array[acc + 1]); + return compute_poseidon_double(array, length, acc + 2); +} + +func compute_poseidon_triple{poseidon_ptr: PoseidonBuiltin*}( + array: felt*, length: felt, acc: felt +) { + if (acc == length) { + return (); + } + let sub_array: felt* = alloc(); + assert sub_array[0] = array[acc]; + assert sub_array[1] = array[acc + 1]; + assert sub_array[2] = array[acc + 2]; + let (r) = poseidon_hash_many(3, sub_array); + return compute_poseidon_triple(array, length, acc + 3); +} + +func run_test{poseidon_ptr: PoseidonBuiltin*}(n: felt) { + alloc_locals; + let base = 218676008889449692916464780911713710628115973574242889792891157041292792362; + let step = 23589912357; + let single_length = n; + let double_length = n * 2; + let triple_length = n * 3; + let single_array = construct_array(step, base, single_length); + let double_array = construct_array(base, step, double_length); + let triple_array = construct_array(base, step, triple_length); + compute_poseidon_single(single_array, single_length, 0); + compute_poseidon_double(double_array, double_length, 0); + compute_poseidon_triple(triple_array, triple_length, 0); + return (); +} + +func main{poseidon_ptr: PoseidonBuiltin*}() { + run_test(10); + return (); +}