From 8f16ccf64eb7a5810aef69d1eed75385a0c7129a Mon Sep 17 00:00:00 2001 From: Innokentii Sennovskii Date: Fri, 5 May 2023 16:07:52 +0100 Subject: [PATCH] Add external benchmarks (https://github.com/AztecProtocol/barretenberg/pull/401) Adds an external_bench file with benchmarks we use for external benchmarking projects --- .../Dockerfile.x86_64-linux-clang-benchmarks | 23 +++ .../barretenberg/stdlib/hash/CMakeLists.txt | 3 +- .../stdlib/hash/benchmarks/CMakeLists.txt | 2 + .../hash/benchmarks/external/CMakeLists.txt | 1 + .../benchmarks/external/external.bench.cpp | 182 ++++++++++++++++++ .../hash/benchmarks/sha256/CMakeLists.txt | 1 + .../{ => benchmarks}/sha256/sha256.bench.cpp | 2 +- 7 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 barretenberg/cpp/dockerfiles/Dockerfile.x86_64-linux-clang-benchmarks create mode 100644 barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/CMakeLists.txt create mode 100644 barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/external/CMakeLists.txt create mode 100644 barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/external/external.bench.cpp create mode 100644 barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/sha256/CMakeLists.txt rename barretenberg/cpp/src/barretenberg/stdlib/hash/{ => benchmarks}/sha256/sha256.bench.cpp (98%) diff --git a/barretenberg/cpp/dockerfiles/Dockerfile.x86_64-linux-clang-benchmarks b/barretenberg/cpp/dockerfiles/Dockerfile.x86_64-linux-clang-benchmarks new file mode 100644 index 00000000000..af2bec48a95 --- /dev/null +++ b/barretenberg/cpp/dockerfiles/Dockerfile.x86_64-linux-clang-benchmarks @@ -0,0 +1,23 @@ +FROM alpine:3.17 AS builder +RUN apk update \ + && apk upgrade \ + && apk add --no-cache \ + build-base \ + clang15 \ + openmp-dev \ + cmake \ + ninja \ + git \ + curl \ + perl + +WORKDIR /usr/src/barretenberg/cpp + +COPY . . +# Build everything to ensure everything builds. All tests will be run from the result of this build. +RUN cmake --preset default && cmake --build --preset default --target external_bench + +FROM alpine:3.17 +RUN apk update && apk add curl openmp +COPY --from=builder /usr/src/barretenberg/cpp/srs_db /usr/src/barretenberg/cpp/srs_db +COPY --from=builder /usr/src/barretenberg/cpp/build/bin/*_bench /usr/src/barretenberg/cpp/build/bin/ \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/stdlib/hash/CMakeLists.txt index c8aaf1e4b4f..2ce247a010a 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/CMakeLists.txt +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/CMakeLists.txt @@ -2,4 +2,5 @@ add_subdirectory(blake2s) add_subdirectory(blake3s) add_subdirectory(pedersen) add_subdirectory(sha256) -add_subdirectory(keccak) \ No newline at end of file +add_subdirectory(keccak) +add_subdirectory(benchmarks) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/CMakeLists.txt new file mode 100644 index 00000000000..e758f7c8b0b --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(sha256) +add_subdirectory(external) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/external/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/external/CMakeLists.txt new file mode 100644 index 00000000000..2ef6b1656c7 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/external/CMakeLists.txt @@ -0,0 +1 @@ +barretenberg_module(external stdlib_primitives crypto_sha256 stdlib_sha256 stdlib_blake3s) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/external/external.bench.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/external/external.bench.cpp new file mode 100644 index 00000000000..357318bd3d8 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/external/external.bench.cpp @@ -0,0 +1,182 @@ +/** + * @file external.bench.cpp + * @author Kesha (Rumata888) + * @brief Benchmarks for external benchmarking projects (e.g. delendum-xyz) + * + */ +#include "../../sha256/sha256.hpp" +#include "../../blake3s/blake3s.hpp" +#include +#include "barretenberg/ecc/curves/bn254/fr.hpp" +#include "barretenberg/plonk/composer/ultra_composer.hpp" +#include "barretenberg/plonk/proof_system/prover/prover.hpp" +#include "barretenberg/stdlib/primitives/packed_byte_array/packed_byte_array.hpp" + +using namespace benchmark; + +using Composer = proof_system::plonk::UltraComposer; +using Prover = proof_system::plonk::UltraProver; +using Verifier = proof_system::plonk::UltraVerifier; + +constexpr size_t PROOF_COUNT_LOG = 10; +constexpr size_t NUM_PROOFS = 3; + +/** + * @brief Main function generating a circuit with num_iterations sequential sha256 hashes, where the output of a + * previous iteration is fed into the next one + * + * @param composer + * @param num_iterations + */ +void generate_test_sha256_plonk_circuit(Composer& composer, size_t num_iterations) +{ + std::string in; + in.resize(32); + for (size_t i = 0; i < 32; ++i) { + in[i] = 0; + } + proof_system::plonk::stdlib::packed_byte_array input(&composer, in); + for (size_t i = 0; i < num_iterations; i++) { + input = proof_system::plonk::stdlib::sha256(input); + } +} + +Composer external_composers[NUM_PROOFS]; +Prover external_provers[NUM_PROOFS]; +Verifier external_verifiers[NUM_PROOFS]; +plonk::proof external_proofs[NUM_PROOFS]; + +/** + * @brief Construct the circuit for sequential sha256 proofs and compute the proof for each case + * + * @param state + */ +void generate_sha256_proof_bench(State& state) noexcept +{ + for (auto _ : state) { + + size_t idx = static_cast(state.range(0)); + size_t num_iterations = 1; + for (size_t i = 0; i < idx; i++) { + num_iterations *= PROOF_COUNT_LOG; + } + external_composers[idx] = Composer(); + generate_test_sha256_plonk_circuit(external_composers[idx], num_iterations); + external_provers[idx] = external_composers[idx].create_prover(); + external_proofs[idx] = external_provers[idx].construct_proof(); + // info("Proof Size for SHA256 hash count ", num_iterations, ": ", external_proofs[idx].proof_data.size()); + } +} + +/** + * @brief We have to warm up the benchmarking function first, otherwise we spend 50% more time than expected + * + */ +BENCHMARK(generate_sha256_proof_bench)->DenseRange(0, 2)->MinWarmUpTime(10)->MinTime(2)->Unit(benchmark::kMillisecond); +/** + * @brief Create sha256 verifier + * + * @details We don't want to benchmark this + * + * @param state + */ +static void generate_sha256_verifier(const State& state) +{ + + size_t idx = static_cast(state.range(0)); + external_verifiers[idx] = external_composers[idx].create_verifier(); +} +/** + * @brief Benchmark sha256 verification + * + * @param state + */ +void verify_sha256_proof_bench(State& state) noexcept +{ + for (auto _ : state) { + + size_t idx = static_cast(state.range(0)); + external_verifiers[idx].verify_proof(external_proofs[idx]); + } +} + +BENCHMARK(verify_sha256_proof_bench)->DenseRange(0, 2)->Setup(generate_sha256_verifier)->Unit(benchmark::kMillisecond); + +/** + * @brief Main function for generating Blake 3 circuits + * + * @param composer + * @param num_iterations + */ +void generate_test_blake3s_plonk_circuit(Composer& composer, size_t num_iterations) +{ + std::string in; + in.resize(32); + for (size_t i = 0; i < 32; ++i) { + in[i] = 0; + } + proof_system::plonk::stdlib::packed_byte_array input(&composer, in); + for (size_t i = 0; i < num_iterations; i++) { + input = proof_system::plonk::stdlib::blake3s(input); + } +} + +/** + * @brief Blake3 circuit construction and proof creation benchmark function + * + * @param state + */ +void generate_blake3s_proof_bench(State& state) noexcept +{ + for (auto _ : state) { + + size_t idx = static_cast(state.range(0)); + size_t num_iterations = 1; + for (size_t i = 0; i < idx; i++) { + num_iterations *= PROOF_COUNT_LOG; + } + external_composers[idx] = Composer(); + generate_test_blake3s_plonk_circuit(external_composers[idx], num_iterations); + external_provers[idx] = external_composers[idx].create_prover(); + external_proofs[idx] = external_provers[idx].construct_proof(); + // Proof size with no public inputs is always 2144 + // info("Proof Size for Blake3s hash count ", num_iterations, ": ", external_proofs[idx].proof_data.size()); + } +} + +BENCHMARK(generate_blake3s_proof_bench)->DenseRange(0, 2)->MinWarmUpTime(10)->MinTime(2)->Unit(benchmark::kMillisecond); + +/** + * @brief Create blake 3 verifier + * + * @details We don't benchmark verifier creation + * + * @param state + */ +static void generate_blake3s_verifier(const State& state) +{ + + size_t idx = static_cast(state.range(0)); + external_verifiers[idx] = external_composers[idx].create_verifier(); +} + +/** + * @brief Benchmark blake3 proof verification + * + * @param state + */ +void verify_blake3s_proof_bench(State& state) noexcept +{ + for (auto _ : state) { + + size_t idx = static_cast(state.range(0)); + external_verifiers[idx].verify_proof(external_proofs[idx]); + } +} + +BENCHMARK(verify_blake3s_proof_bench) + ->DenseRange(0, 2) + ->Setup(generate_blake3s_verifier) + ->Unit(benchmark::kMillisecond); + +BENCHMARK_MAIN(); \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/sha256/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/sha256/CMakeLists.txt new file mode 100644 index 00000000000..f3f408e1fe5 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/sha256/CMakeLists.txt @@ -0,0 +1 @@ +barretenberg_module(stdlib_sha256_just stdlib_sha256) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/sha256/sha256.bench.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/sha256/sha256.bench.cpp similarity index 98% rename from barretenberg/cpp/src/barretenberg/stdlib/hash/sha256/sha256.bench.cpp rename to barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/sha256/sha256.bench.cpp index d912505c447..d33645a8d8e 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/sha256/sha256.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/benchmarks/sha256/sha256.bench.cpp @@ -1,4 +1,4 @@ -#include "sha256.hpp" +#include "../../sha256/sha256.hpp" #include #include "barretenberg/ecc/curves/bn254/fr.hpp" #include "barretenberg/plonk/composer/ultra_composer.hpp"