From ebf3411ff00ed65f4e18defa7fa5189ff0a85a80 Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Tue, 10 Oct 2023 09:05:16 +0000 Subject: [PATCH 1/9] Passing around crs is painful and buggy, leverage the global crs. --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 44 +++++++++---------- .../dsl/acir_proofs/acir_composer.cpp | 9 ++-- .../dsl/acir_proofs/acir_composer.hpp | 19 +++----- .../barretenberg/dsl/acir_proofs/c_bind.cpp | 7 ++- .../barretenberg/examples/simple/simple.cpp | 5 +-- .../plonk/composer/ultra_composer.cpp | 7 ++- .../plonk/composer/ultra_composer.hpp | 6 +-- 7 files changed, 43 insertions(+), 54 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 81c4a2bfb49..dfcd63d8d3c 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -53,11 +53,11 @@ acir_format::acir_format get_constraint_system(std::string const& bytecode_path) */ bool proveAndVerify(const std::string& bytecodePath, const std::string& witnessPath, bool recursive) { - auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); + acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); - auto proof = acir_composer->create_proof(srs::get_crs_factory(), constraint_system, witness, recursive); - auto verified = acir_composer->verify_proof(proof, recursive); + auto proof = acir_composer.create_proof(constraint_system, witness, recursive); + auto verified = acir_composer.verify_proof(proof, recursive); vinfo("verified: ", verified); return verified; @@ -80,10 +80,10 @@ void prove(const std::string& bytecodePath, bool recursive, const std::string& outputPath) { - auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); + acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); - auto proof = acir_composer->create_proof(srs::get_crs_factory(), constraint_system, witness, recursive); + auto proof = acir_composer.create_proof(constraint_system, witness, recursive); if (outputPath == "-") { writeRawBytesToStdout(proof); @@ -104,10 +104,10 @@ void prove(const std::string& bytecodePath, */ void gateCount(const std::string& bytecodePath) { - auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); + acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); - acir_composer->create_circuit(constraint_system); - auto gate_count = acir_composer->get_total_circuit_size(); + acir_composer.create_circuit(constraint_system); + auto gate_count = acir_composer.get_total_circuit_size(); writeUint64AsRawBytesToStdout(static_cast(gate_count)); vinfo("gate count: ", gate_count); @@ -131,10 +131,10 @@ void gateCount(const std::string& bytecodePath) */ bool verify(const std::string& proof_path, bool recursive, const std::string& vk_path) { - auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); + acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto vk_data = from_buffer(read_file(vk_path)); - acir_composer->load_verification_key(barretenberg::srs::get_crs_factory(), std::move(vk_data)); - auto verified = acir_composer->verify_proof(read_file(proof_path), recursive); + acir_composer.load_verification_key(std::move(vk_data)); + auto verified = acir_composer.verify_proof(read_file(proof_path), recursive); vinfo("verified: ", verified); @@ -153,10 +153,10 @@ bool verify(const std::string& proof_path, bool recursive, const std::string& vk */ void writeVk(const std::string& bytecodePath, const std::string& outputPath) { - auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); + acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); - acir_composer->init_proving_key(srs::get_crs_factory(), constraint_system); - auto vk = acir_composer->init_verification_key(); + acir_composer.init_proving_key(constraint_system); + auto vk = acir_composer.init_verification_key(); auto serialized_vk = to_buffer(*vk); if (outputPath == "-") { writeRawBytesToStdout(serialized_vk); @@ -182,10 +182,10 @@ void writeVk(const std::string& bytecodePath, const std::string& outputPath) */ void contract(const std::string& output_path, const std::string& vk_path) { - auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); + acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto vk_data = from_buffer(read_file(vk_path)); - acir_composer->load_verification_key(barretenberg::srs::get_crs_factory(), std::move(vk_data)); - auto contract = acir_composer->get_solidity_verifier(); + acir_composer.load_verification_key(std::move(vk_data)); + auto contract = acir_composer.get_solidity_verifier(); if (output_path == "-") { writeStringToStdout(contract); @@ -223,9 +223,9 @@ void contract(const std::string& output_path, const std::string& vk_path) */ void proofAsFields(const std::string& proof_path, std::string const& vk_path, const std::string& output_path) { - auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); + acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto vk_data = from_buffer(read_file(vk_path)); - auto data = acir_composer->serialize_proof_into_fields(read_file(proof_path), vk_data.num_public_inputs); + auto data = acir_composer.serialize_proof_into_fields(read_file(proof_path), vk_data.num_public_inputs); auto json = format("[", join(map(data, [](auto fr) { return format("\"", fr, "\""); })), "]"); if (output_path == "-") { @@ -252,10 +252,10 @@ void proofAsFields(const std::string& proof_path, std::string const& vk_path, co */ void vkAsFields(const std::string& vk_path, const std::string& output_path) { - auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); + acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto vk_data = from_buffer(read_file(vk_path)); - acir_composer->load_verification_key(barretenberg::srs::get_crs_factory(), std::move(vk_data)); - auto data = acir_composer->serialize_verification_key_into_fields(); + acir_composer.load_verification_key(std::move(vk_data)); + auto data = acir_composer.serialize_verification_key_into_fields(); // We need to move vk_hash to the front... std::rotate(data.begin(), data.end() - 1, data.end()); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 29463a06db7..07a3578b0d1 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -108,13 +108,10 @@ std::shared_ptr AcirComposer::init_verifi return verification_key_; } -void AcirComposer::load_verification_key( - std::shared_ptr> const& crs_factory, - proof_system::plonk::verification_key_data&& data) +void AcirComposer::load_verification_key(proof_system::plonk::verification_key_data&& data) { - verification_key_ = - std::make_shared(std::move(data), crs_factory->get_verifier_crs()); - composer_ = acir_format::Composer(proving_key_, verification_key_); + verification_key_ = std::make_shared( + std::move(data), srs::get_crs_factory()->get_verifier_crs()); } bool AcirComposer::verify_proof(std::vector const& proof, bool is_recursive) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp index 25814e78d91..f4e125478ba 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp @@ -14,18 +14,13 @@ class AcirComposer { void create_circuit(acir_format::acir_format& constraint_system); - void init_proving_key(std::shared_ptr> const& crs_factory, - acir_format::acir_format& constraint_system); - - std::vector create_proof( - std::shared_ptr> const& crs_factory, - acir_format::acir_format& constraint_system, - acir_format::WitnessVector& witness, - bool is_recursive); - - void load_verification_key( - std::shared_ptr> const& crs_factory, - proof_system::plonk::verification_key_data&& data); + void init_proving_key(acir_format::acir_format& constraint_system); + + std::vector create_proof(acir_format::acir_format& constraint_system, + acir_format::WitnessVector& witness, + bool is_recursive); + + void load_verification_key(proof_system::plonk::verification_key_data&& data); std::shared_ptr init_verification_key(); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp index 1af145e2978..0bdfbb519d2 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp @@ -35,7 +35,7 @@ WASM_EXPORT void acir_init_proving_key(in_ptr acir_composer_ptr, uint8_t const* auto acir_composer = reinterpret_cast(*acir_composer_ptr); auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer>(acir_vec)); - acir_composer->init_proving_key(barretenberg::srs::get_crs_factory(), constraint_system); + acir_composer->init_proving_key(constraint_system); } WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr, @@ -48,8 +48,7 @@ WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr, auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer>(acir_vec)); auto witness = acir_format::witness_buf_to_witness_data(from_buffer>(witness_vec)); - auto proof_data = - acir_composer->create_proof(barretenberg::srs::get_crs_factory(), constraint_system, witness, *is_recursive); + auto proof_data = acir_composer->create_proof(constraint_system, witness, *is_recursive); *out = to_heap_buffer(proof_data); } @@ -57,7 +56,7 @@ WASM_EXPORT void acir_load_verification_key(in_ptr acir_composer_ptr, uint8_t co { auto acir_composer = reinterpret_cast(*acir_composer_ptr); auto vk_data = from_buffer(vk_buf); - acir_composer->load_verification_key(barretenberg::srs::get_crs_factory(), std::move(vk_data)); + acir_composer->load_verification_key(std::move(vk_data)); } WASM_EXPORT void acir_init_verification_key(in_ptr acir_composer_ptr) diff --git a/barretenberg/cpp/src/barretenberg/examples/simple/simple.cpp b/barretenberg/cpp/src/barretenberg/examples/simple/simple.cpp index 19af6aa0d69..ad35fa0ee95 100644 --- a/barretenberg/cpp/src/barretenberg/examples/simple/simple.cpp +++ b/barretenberg/cpp/src/barretenberg/examples/simple/simple.cpp @@ -19,8 +19,7 @@ void build_circuit(Builder& builder) } } -BuilderComposerPtrs create_builder_and_composer( - std::shared_ptr> const& crs_factory) +BuilderComposerPtrs create_builder_and_composer() { // WARNING: Size hint is essential to perform 512k circuits! auto builder = std::make_unique(CIRCUIT_SIZE); @@ -36,7 +35,7 @@ BuilderComposerPtrs create_builder_and_composer( info("composer gates: ", builder->get_num_gates()); info("computing proving key..."); - auto composer = std::make_unique(crs_factory); + auto composer = std::make_unique(); auto pk = composer->compute_proving_key(*builder); return { builder.release(), composer.release() }; diff --git a/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.cpp b/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.cpp index 7b0ded945c8..1026193bdf7 100644 --- a/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.cpp @@ -375,10 +375,11 @@ std::shared_ptr UltraComposer::compute_proving_key(CircuitBuilder& const size_t minimum_circuit_size = tables_size + lookups_size; const size_t num_randomized_gates = NUM_RESERVED_GATES; + auto crs_factory = srs::get_crs_factory(); // Initialize circuit_proving_key // TODO(#392)(Kesha): replace composer types. circuit_proving_key = initialize_proving_key( - circuit_constructor, crs_factory_.get(), minimum_circuit_size, num_randomized_gates, CircuitType::ULTRA); + circuit_constructor, crs_factory.get(), minimum_circuit_size, num_randomized_gates, CircuitType::ULTRA); construct_selector_polynomials(circuit_constructor, circuit_proving_key.get()); @@ -491,10 +492,12 @@ std::shared_ptr UltraComposer::compute_verification_key return circuit_verification_key; } + auto crs_factory = srs::get_crs_factory(); + if (!circuit_proving_key) { compute_proving_key(circuit_constructor); } - circuit_verification_key = compute_verification_key_common(circuit_proving_key, crs_factory_->get_verifier_crs()); + circuit_verification_key = compute_verification_key_common(circuit_proving_key, crs_factory->get_verifier_crs()); circuit_verification_key->circuit_type = CircuitType::ULTRA; diff --git a/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp b/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp index fcea9028d83..e539db4c47b 100644 --- a/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp @@ -27,7 +27,6 @@ class UltraComposer { std::shared_ptr circuit_verification_key; // The crs_factory holds the path to the srs and exposes methods to extract the srs elements - std::shared_ptr> crs_factory_; bool computed_witness = false; @@ -37,11 +36,8 @@ class UltraComposer { // vanishing_polynomial cannot be trivially fetched here, I am directly setting this to 4 - 1 = 3. static constexpr size_t s_randomness = 3; - UltraComposer() { crs_factory_ = barretenberg::srs::get_crs_factory(); } + UltraComposer() = default; - explicit UltraComposer(std::shared_ptr> crs_factory) - : crs_factory_(std::move(crs_factory)) - {} UltraComposer(std::shared_ptr p_key, std::shared_ptr v_key) : circuit_proving_key(std::move(p_key)) From d3551c85bcaf8bbd32d71072c7d310b0482d289a Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Tue, 10 Oct 2023 09:07:15 +0000 Subject: [PATCH 2/9] Stop whinging about this ownership stuff. --- barretenberg/cpp/.clangd | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/barretenberg/cpp/.clangd b/barretenberg/cpp/.clangd index 06f5d0d0590..e09234d9e7a 100644 --- a/barretenberg/cpp/.clangd +++ b/barretenberg/cpp/.clangd @@ -1,4 +1,4 @@ -CompileFlags: # Tweak the parse settings +CompileFlags: # Tweak the parse settings Remove: -fconstexpr-ops-limit=* --- # Applies all barretenberg source files @@ -42,7 +42,7 @@ Diagnostics: - misc-non-private-member-variables-in-classes - cppcoreguidelines-non-private-member-variables-in-classes # We have many `for` loops that violate this part of the bounds safety profile - - cppcoreguidelines-pro-bounds-constant-array-index + - cppcoreguidelines-pro-bounds-constant-array-index # Large diff; we often `use` an entire namespace. - google-build-using-namespace # Large diff @@ -59,6 +59,8 @@ Diagnostics: - readability-function-cognitive-complexity # It is often nicer to not be explicit - google-explicit-constructor + # Not honouring. + - cppcoreguidelines-owning-memory --- # this divider is necessary # Disable some checks for Google Test/Bench @@ -69,5 +71,4 @@ Diagnostics: # these checks get triggered by the Google macros Remove: - cppcoreguidelines-avoid-non-const-global-variables - - cppcoreguidelines-owning-memory - - cppcoreguidelines-special-member-functions \ No newline at end of file + - cppcoreguidelines-special-member-functions From e67f0803fe5668de714a84bfc3c09829564a1bc4 Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Tue, 10 Oct 2023 09:59:06 +0000 Subject: [PATCH 3/9] Build circuit to determine size of crs to load. --- barretenberg/acir_tests/flows/all_cmds.sh | 8 ++-- .../cpp/src/barretenberg/bb/file_io.hpp | 5 +- .../cpp/src/barretenberg/bb/get_crs.hpp | 2 +- barretenberg/cpp/src/barretenberg/bb/main.cpp | 48 ++++++++++++------- .../dsl/acir_proofs/acir_composer.hpp | 1 + 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/barretenberg/acir_tests/flows/all_cmds.sh b/barretenberg/acir_tests/flows/all_cmds.sh index dda4353fb6f..c7ee147f620 100755 --- a/barretenberg/acir_tests/flows/all_cmds.sh +++ b/barretenberg/acir_tests/flows/all_cmds.sh @@ -19,6 +19,8 @@ $BIN verify -k vk -p proof $FLAGS # Check supplemental functions. # Grep to determine success. $BIN contract -k vk $BFLAG -o - | grep "Verification Key Hash" > /dev/null -# Use jq to determine success. -$BIN proof_as_fields -k vk -p proof -o - | jq . > /dev/null -$BIN vk_as_fields -k vk -o - > vk_as_fields | jq . > /dev/null \ No newline at end of file +# Use jq to determine success, and also check result not empty. +OUTPUT=$($BIN proof_as_fields -k vk -p proof -o - | jq .) +[ -n "$OUTPUT" ] || exit 1 +OUTPUT=$($BIN vk_as_fields -k vk -o - | jq .) +[ -n "$OUTPUT" ] || exit 1 \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/bb/file_io.hpp b/barretenberg/cpp/src/barretenberg/bb/file_io.hpp index 09009ebbd2b..31796e57ffa 100644 --- a/barretenberg/cpp/src/barretenberg/bb/file_io.hpp +++ b/barretenberg/cpp/src/barretenberg/bb/file_io.hpp @@ -1,9 +1,10 @@ #pragma once #include #include +#include #include -inline std::vector read_file(const std::string& filename) +inline std::vector read_file(const std::string& filename, size_t bytes = 0) { // Open the file in binary mode and move to the end. std::ifstream file(filename, std::ios::binary | std::ios::ate); @@ -12,7 +13,7 @@ inline std::vector read_file(const std::string& filename) } // Get the file size. - std::streamsize size = file.tellg(); + std::streamsize size = bytes == 0 ? (std::streamsize)file.tellg() : (std::streamsize)bytes; if (size <= 0) { throw std::runtime_error("File is empty or there's an error reading it: " + filename); } diff --git a/barretenberg/cpp/src/barretenberg/bb/get_crs.hpp b/barretenberg/cpp/src/barretenberg/bb/get_crs.hpp index b4269d3ad3d..1c205f2f3e8 100644 --- a/barretenberg/cpp/src/barretenberg/bb/get_crs.hpp +++ b/barretenberg/cpp/src/barretenberg/bb/get_crs.hpp @@ -65,7 +65,7 @@ inline std::vector get_g1_data(const std::file } if (size >= num_points) { vinfo("using cached crs at: ", path); - auto data = read_file(path / "g1.dat"); + auto data = read_file(path / "g1.dat", 28 + num_points * 64); auto points = std::vector(num_points); auto size_of_points_in_bytes = num_points * 64; barretenberg::srs::IO::read_affine_elements_from_buffer( diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index dfcd63d8d3c..8657385bfdd 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -13,17 +13,29 @@ #include using namespace barretenberg; -// Transcript downloading code only supports fetching and parsing the first transcript file. -const uint32_t MAX_CIRCUIT_SIZE = 1 << 22; std::string CRS_PATH = "./crs"; bool verbose = false; -void init() +acir_proofs::AcirComposer init(acir_format::acir_format& constraint_system) { + acir_proofs::AcirComposer acir_composer(0, verbose); + acir_composer.create_circuit(constraint_system); + auto subgroup_size = acir_composer.get_circuit_subgroup_size(); + // Must +1! - auto g1_data = get_g1_data(CRS_PATH, MAX_CIRCUIT_SIZE + 1); + auto g1_data = get_g1_data(CRS_PATH, subgroup_size + 1); auto g2_data = get_g2_data(CRS_PATH); srs::init_crs_factory(g1_data, g2_data); + + return acir_composer; +} + +acir_proofs::AcirComposer init() +{ + acir_proofs::AcirComposer acir_composer(0, verbose); + auto g2_data = get_g2_data(CRS_PATH); + srs::init_crs_factory({}, g2_data); + return acir_composer; } acir_format::WitnessVector get_witness(std::string const& witness_path) @@ -53,9 +65,10 @@ acir_format::acir_format get_constraint_system(std::string const& bytecode_path) */ bool proveAndVerify(const std::string& bytecodePath, const std::string& witnessPath, bool recursive) { - acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); + auto acir_composer = init(constraint_system); + auto proof = acir_composer.create_proof(constraint_system, witness, recursive); auto verified = acir_composer.verify_proof(proof, recursive); @@ -80,9 +93,9 @@ void prove(const std::string& bytecodePath, bool recursive, const std::string& outputPath) { - acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); + auto acir_composer = init(constraint_system); auto proof = acir_composer.create_proof(constraint_system, witness, recursive); if (outputPath == "-") { @@ -104,9 +117,8 @@ void prove(const std::string& bytecodePath, */ void gateCount(const std::string& bytecodePath) { - acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); - acir_composer.create_circuit(constraint_system); + auto acir_composer = init(constraint_system); auto gate_count = acir_composer.get_total_circuit_size(); writeUint64AsRawBytesToStdout(static_cast(gate_count)); @@ -131,7 +143,9 @@ void gateCount(const std::string& bytecodePath) */ bool verify(const std::string& proof_path, bool recursive, const std::string& vk_path) { - acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); + auto g2_data = get_g2_data(CRS_PATH); + srs::init_crs_factory({}, g2_data); + acir_proofs::AcirComposer acir_composer(1, verbose); auto vk_data = from_buffer(read_file(vk_path)); acir_composer.load_verification_key(std::move(vk_data)); auto verified = acir_composer.verify_proof(read_file(proof_path), recursive); @@ -153,8 +167,8 @@ bool verify(const std::string& proof_path, bool recursive, const std::string& vk */ void writeVk(const std::string& bytecodePath, const std::string& outputPath) { - acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); + auto acir_composer = init(constraint_system); acir_composer.init_proving_key(constraint_system); auto vk = acir_composer.init_verification_key(); auto serialized_vk = to_buffer(*vk); @@ -182,7 +196,7 @@ void writeVk(const std::string& bytecodePath, const std::string& outputPath) */ void contract(const std::string& output_path, const std::string& vk_path) { - acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); + auto acir_composer = init(); auto vk_data = from_buffer(read_file(vk_path)); acir_composer.load_verification_key(std::move(vk_data)); auto contract = acir_composer.get_solidity_verifier(); @@ -223,7 +237,7 @@ void contract(const std::string& output_path, const std::string& vk_path) */ void proofAsFields(const std::string& proof_path, std::string const& vk_path, const std::string& output_path) { - acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); + auto acir_composer = init(); auto vk_data = from_buffer(read_file(vk_path)); auto data = acir_composer.serialize_proof_into_fields(read_file(proof_path), vk_data.num_public_inputs); auto json = format("[", join(map(data, [](auto fr) { return format("\"", fr, "\""); })), "]"); @@ -252,7 +266,7 @@ void proofAsFields(const std::string& proof_path, std::string const& vk_path, co */ void vkAsFields(const std::string& vk_path, const std::string& output_path) { - acir_proofs::AcirComposer acir_composer(MAX_CIRCUIT_SIZE, verbose); + auto acir_composer = init(); auto vk_data = from_buffer(read_file(vk_path)); acir_composer.load_verification_key(std::move(vk_data)); auto data = acir_composer.serialize_verification_key_into_fields(); @@ -338,17 +352,17 @@ int main(int argc, char* argv[]) if (command == "--version") { writeStringToStdout(BB_VERSION); return 0; - } else if (command == "info") { + } + if (command == "info") { std::string output_path = getOption(args, "-o", "info.json"); acvmInfo(output_path); return 0; } - init(); - if (command == "prove_and_verify") { return proveAndVerify(bytecode_path, witness_path, recursive) ? 0 : 1; - } else if (command == "prove") { + } + if (command == "prove") { std::string output_path = getOption(args, "-o", "./proofs/proof"); prove(bytecode_path, witness_path, recursive, output_path); } else if (command == "gates") { diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp index f4e125478ba..58696e4ad25 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp @@ -29,6 +29,7 @@ class AcirComposer { std::string get_solidity_verifier(); size_t get_exact_circuit_size() { return exact_circuit_size_; }; size_t get_total_circuit_size() { return total_circuit_size_; }; + size_t get_circuit_subgroup_size() { return circuit_subgroup_size_; }; std::vector serialize_proof_into_fields(std::vector const& proof, size_t num_inner_public_inputs); From 5737a73c6ab75379e96f92d3208c260c7aa23e0b Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Tue, 10 Oct 2023 10:02:57 +0000 Subject: [PATCH 4/9] Remove redundency in format. Remove composer as member in acir_composer. --- .../dsl/acir_format/acir_format.cpp | 134 ++++-------------- .../dsl/acir_format/block_constraint.cpp | 2 +- .../dsl/acir_proofs/acir_composer.cpp | 90 +++++------- .../dsl/acir_proofs/acir_composer.hpp | 1 - 4 files changed, 60 insertions(+), 167 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 250efe82ecd..59b5192a645 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -11,29 +11,30 @@ void read_witness(Builder& builder, WitnessVector const& witness) } } -void create_circuit(Builder& builder, acir_format const& constraint_system) +void add_public_vars(Builder& builder, acir_format const& constraint_system) { - if (constraint_system.public_inputs.size() > constraint_system.varnum) { - info("create_circuit: too many public inputs!"); - } - for (size_t i = 1; i < constraint_system.varnum; ++i) { // If the index is in the public inputs vector, then we add it as a public input if (std::find(constraint_system.public_inputs.begin(), constraint_system.public_inputs.end(), i) != constraint_system.public_inputs.end()) { + builder.add_public_variable(0); + } else { builder.add_variable(0); } } +} +void build_constraints(Builder& builder, acir_format const& constraint_system, bool has_valid_witness_assignments) +{ // Add arithmetic gates for (const auto& constraint : constraint_system.constraints) { builder.create_poly_gate(constraint); } - // Add and constraint + // Add logic constraint for (const auto& constraint : constraint_system.logic_constraints) { create_logic_gate( builder, constraint.a, constraint.b, constraint.result, constraint.num_bits, constraint.is_xor_gate); @@ -54,14 +55,14 @@ void create_circuit(Builder& builder, acir_format const& constraint_system) create_schnorr_verify_constraints(builder, constraint); } - // Add ECDSA K1 constraints + // Add ECDSA k1 constraints for (const auto& constraint : constraint_system.ecdsa_k1_constraints) { - create_ecdsa_k1_verify_constraints(builder, constraint, false); + create_ecdsa_k1_verify_constraints(builder, constraint, has_valid_witness_assignments); } - // Add ECDSA R1 constraints + // Add ECDSA r1 constraints for (const auto& constraint : constraint_system.ecdsa_r1_constraints) { - create_ecdsa_r1_verify_constraints(builder, constraint, false); + create_ecdsa_r1_verify_constraints(builder, constraint, has_valid_witness_assignments); } // Add blake2s constraints @@ -94,13 +95,13 @@ void create_circuit(Builder& builder, acir_format const& constraint_system) // Add block constraints for (const auto& constraint : constraint_system.block_constraints) { - create_block_constraints(builder, constraint, false); + create_block_constraints(builder, constraint, has_valid_witness_assignments); } // Add recursion constraints for (size_t i = 0; i < constraint_system.recursion_constraints.size(); ++i) { auto& constraint = constraint_system.recursion_constraints[i]; - create_recursion_constraints(builder, constraint); + create_recursion_constraints(builder, constraint, has_valid_witness_assignments); // make sure the verification key records the public input indices of the final recursion output // (N.B. up to the ACIR description to make sure that the final output aggregation object wires are public @@ -113,6 +114,16 @@ void create_circuit(Builder& builder, acir_format const& constraint_system) } } +void create_circuit(Builder& builder, acir_format const& constraint_system) +{ + if (constraint_system.public_inputs.size() > constraint_system.varnum) { + info("create_circuit: too many public inputs!"); + } + + add_public_vars(builder, constraint_system); + build_constraints(builder, constraint_system, false); +} + Builder create_circuit(const acir_format& constraint_system, size_t size_hint) { Builder builder(size_hint); @@ -135,104 +146,9 @@ void create_circuit_with_witness(Builder& builder, acir_format const& constraint info("create_circuit_with_witness: too many public inputs!"); } - for (size_t i = 1; i < constraint_system.varnum; ++i) { - // If the index is in the public inputs vector, then we add it as a public input - - if (std::find(constraint_system.public_inputs.begin(), constraint_system.public_inputs.end(), i) != - constraint_system.public_inputs.end()) { - - builder.add_public_variable(0); - - } else { - builder.add_variable(0); - } - } - + add_public_vars(builder, constraint_system); read_witness(builder, witness); - - // Add arithmetic gates - for (const auto& constraint : constraint_system.constraints) { - builder.create_poly_gate(constraint); - } - - // Add logic constraint - for (const auto& constraint : constraint_system.logic_constraints) { - create_logic_gate( - builder, constraint.a, constraint.b, constraint.result, constraint.num_bits, constraint.is_xor_gate); - } - - // Add range constraint - for (const auto& constraint : constraint_system.range_constraints) { - builder.create_range_constraint(constraint.witness, constraint.num_bits, ""); - } - - // Add sha256 constraints - for (const auto& constraint : constraint_system.sha256_constraints) { - create_sha256_constraints(builder, constraint); - } - - // Add schnorr constraints - for (const auto& constraint : constraint_system.schnorr_constraints) { - create_schnorr_verify_constraints(builder, constraint); - } - - // Add ECDSA k1 constraints - for (const auto& constraint : constraint_system.ecdsa_k1_constraints) { - create_ecdsa_k1_verify_constraints(builder, constraint); - } - - // Add ECDSA r1 constraints - for (const auto& constraint : constraint_system.ecdsa_r1_constraints) { - create_ecdsa_r1_verify_constraints(builder, constraint); - } - - // Add blake2s constraints - for (const auto& constraint : constraint_system.blake2s_constraints) { - create_blake2s_constraints(builder, constraint); - } - - // Add keccak constraints - for (const auto& constraint : constraint_system.keccak_constraints) { - create_keccak_constraints(builder, constraint); - } - for (const auto& constraint : constraint_system.keccak_var_constraints) { - create_keccak_var_constraints(builder, constraint); - } - - // Add pedersen constraints - for (const auto& constraint : constraint_system.pedersen_constraints) { - create_pedersen_constraint(builder, constraint); - } - - // Add fixed base scalar mul constraints - for (const auto& constraint : constraint_system.fixed_base_scalar_mul_constraints) { - create_fixed_base_constraint(builder, constraint); - } - - // Add hash to field constraints - for (const auto& constraint : constraint_system.hash_to_field_constraints) { - create_hash_to_field_constraints(builder, constraint); - } - - // Add block constraints - for (const auto& constraint : constraint_system.block_constraints) { - create_block_constraints(builder, constraint); - } - - // Add recursion constraints - for (size_t i = 0; i < constraint_system.recursion_constraints.size(); ++i) { - auto& constraint = constraint_system.recursion_constraints[i]; - create_recursion_constraints(builder, constraint, true); - - // make sure the verification key records the public input indices of the final recursion output - // (N.B. up to the ACIR description to make sure that the final output aggregation object wires are public - // inputs!) - if (i == constraint_system.recursion_constraints.size() - 1) { - std::vector proof_output_witness_indices(constraint.output_aggregation_object.begin(), - constraint.output_aggregation_object.end()); - builder.set_recursive_proof(proof_output_witness_indices); - } - } + build_constraints(builder, constraint_system, true); } } // namespace acir_format diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.cpp index 9eb908f0b00..882d1ac14ed 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.cpp @@ -55,7 +55,7 @@ void create_block_constraints(Builder& builder, const BlockConstraint constraint field_ct value = poly_to_field_ct(op.value, builder); field_ct index = poly_to_field_ct(op.index, builder); if (has_valid_witness_assignments == false) { - index = field_ct(0); + index = field_ct::from_witness(&builder, 0); } if (op.access_type == 0) { value.assert_equal(table.read(index)); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 07a3578b0d1..c5afdc10046 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -12,88 +12,60 @@ namespace acir_proofs { AcirComposer::AcirComposer(size_t size_hint, bool verbose) - : composer_(/*p_key=*/0, /*v_key=*/0) - , size_hint_(size_hint) + : size_hint_(size_hint) , verbose_(verbose) {} void AcirComposer::create_circuit(acir_format::acir_format& constraint_system) { + if (builder_.get_num_gates() > 1) { + return; + } + vinfo("building circuit..."); builder_ = acir_format::create_circuit(constraint_system, size_hint_); - - // We are done with the constraint system at this point, and we need the memory slab back. - constraint_system.constraints.clear(); - constraint_system.constraints.shrink_to_fit(); - exact_circuit_size_ = builder_.get_num_gates(); total_circuit_size_ = builder_.get_total_circuit_size(); circuit_subgroup_size_ = builder_.get_circuit_subgroup_size(total_circuit_size_); size_hint_ = circuit_subgroup_size_; + vinfo("gates: ", builder_.get_total_circuit_size()); } -void AcirComposer::init_proving_key( - std::shared_ptr> const& crs_factory, - acir_format::acir_format& constraint_system) +void AcirComposer::init_proving_key(acir_format::acir_format& constraint_system) { - vinfo("building circuit... ", size_hint_); - builder_ = acir_format::Builder(size_hint_); - acir_format::create_circuit(builder_, constraint_system); - - // We are done with the constraint system at this point, and we need the memory slab back. - constraint_system.constraints.clear(); - constraint_system.constraints.shrink_to_fit(); - - exact_circuit_size_ = builder_.get_num_gates(); - total_circuit_size_ = builder_.get_total_circuit_size(); - circuit_subgroup_size_ = builder_.get_circuit_subgroup_size(total_circuit_size_); - - composer_ = acir_format::Composer(crs_factory); + create_circuit(constraint_system); + acir_format::Composer composer; vinfo("computing proving key..."); - proving_key_ = composer_.compute_proving_key(builder_); + proving_key_ = composer.compute_proving_key(builder_); } -std::vector AcirComposer::create_proof( - std::shared_ptr> const& crs_factory, - acir_format::acir_format& constraint_system, - acir_format::WitnessVector& witness, - bool is_recursive) +std::vector AcirComposer::create_proof(acir_format::acir_format& constraint_system, + acir_format::WitnessVector& witness, + bool is_recursive) { - // Release prior memory first. - composer_ = acir_format::Composer(/*p_key=*/0, /*v_key=*/0); - - vinfo("building circuit..."); + vinfo("building circuit with witness..."); + builder_ = acir_format::Builder(size_hint_); create_circuit_with_witness(builder_, constraint_system, witness); vinfo("gates: ", builder_.get_total_circuit_size()); - composer_ = [&]() { + auto composer = [&]() { if (proving_key_) { - auto composer = acir_format::Composer(proving_key_, verification_key_); - // You can't produce the verification key unless you manually set the crs. Which seems like a bug. - composer_.crs_factory_ = crs_factory; - return composer; - } else { - return acir_format::Composer(crs_factory); + return acir_format::Composer(proving_key_, nullptr); } - }(); - if (!proving_key_) { + + acir_format::Composer composer; vinfo("computing proving key..."); - proving_key_ = composer_.compute_proving_key(builder_); + proving_key_ = composer.compute_proving_key(builder_); vinfo("done."); - } - - // We are done with the constraint system at this point, and we need the memory slab back. - constraint_system.constraints.clear(); - constraint_system.constraints.shrink_to_fit(); - witness.clear(); - witness.shrink_to_fit(); + return composer; + }(); vinfo("creating proof..."); std::vector proof; if (is_recursive) { - auto prover = composer_.create_prover(builder_); + auto prover = composer.create_prover(builder_); proof = prover.construct_proof().proof_data; } else { - auto prover = composer_.create_ultra_with_keccak_prover(builder_); + auto prover = composer.create_ultra_with_keccak_prover(builder_); proof = prover.construct_proof().proof_data; } vinfo("done."); @@ -102,8 +74,12 @@ std::vector AcirComposer::create_proof( std::shared_ptr AcirComposer::init_verification_key() { + if (!proving_key_) { + throw_or_abort("Compute proving key first."); + } vinfo("computing verification key..."); - verification_key_ = composer_.compute_verification_key(builder_); + acir_format::Composer composer(proving_key_, nullptr); + verification_key_ = composer.compute_verification_key(builder_); vinfo("done."); return verification_key_; } @@ -116,9 +92,11 @@ void AcirComposer::load_verification_key(proof_system::plonk::verification_key_d bool AcirComposer::verify_proof(std::vector const& proof, bool is_recursive) { + acir_format::Composer composer(proving_key_, verification_key_); + if (!verification_key_) { vinfo("computing verification key..."); - verification_key_ = composer_.compute_verification_key(builder_); + verification_key_ = composer.compute_verification_key(builder_); vinfo("done."); } @@ -126,10 +104,10 @@ bool AcirComposer::verify_proof(std::vector const& proof, bool is_recur builder_.public_inputs.resize((proof.size() - 2144) / 32); if (is_recursive) { - auto verifier = composer_.create_verifier(builder_); + auto verifier = composer.create_verifier(builder_); return verifier.verify_proof({ proof }); } else { - auto verifier = composer_.create_ultra_with_keccak_verifier(builder_); + auto verifier = composer.create_ultra_with_keccak_verifier(builder_); return verifier.verify_proof({ proof }); } } diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp index 58696e4ad25..32b678268e3 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp @@ -38,7 +38,6 @@ class AcirComposer { private: acir_format::Builder builder_; - acir_format::Composer composer_; size_t size_hint_; size_t exact_circuit_size_; size_t total_circuit_size_; From 66ea7af37318ff7b7e1a705a614f656dc1d51fcb Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Tue, 10 Oct 2023 19:08:06 +0000 Subject: [PATCH 5/9] Builds. --- .../dsl/acir_proofs/acir_composer.cpp | 20 ++-- .../cpp/src/barretenberg/examples/c_bind.cpp | 2 +- .../barretenberg/examples/simple/simple.hpp | 3 +- .../examples/simple/simple.test.cpp | 4 +- .../proofs/compute_circuit_data.hpp | 4 +- .../proofs/join_split/c_bind.cpp | 98 ------------------- .../proofs/join_split/c_bind.h | 3 - .../proofs/join_split/join_split.cpp | 13 +-- .../proofs/join_split/join_split.hpp | 5 +- .../proofs/join_split/join_split.test.cpp | 5 +- .../join_split/join_split_js_parity.test.cpp | 8 +- 11 files changed, 25 insertions(+), 140 deletions(-) delete mode 100644 barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/c_bind.cpp delete mode 100644 barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/c_bind.h diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 07a3578b0d1..5019324e298 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -31,9 +31,7 @@ void AcirComposer::create_circuit(acir_format::acir_format& constraint_system) size_hint_ = circuit_subgroup_size_; } -void AcirComposer::init_proving_key( - std::shared_ptr> const& crs_factory, - acir_format::acir_format& constraint_system) +void AcirComposer::init_proving_key(acir_format::acir_format& constraint_system) { vinfo("building circuit... ", size_hint_); builder_ = acir_format::Builder(size_hint_); @@ -47,16 +45,14 @@ void AcirComposer::init_proving_key( total_circuit_size_ = builder_.get_total_circuit_size(); circuit_subgroup_size_ = builder_.get_circuit_subgroup_size(total_circuit_size_); - composer_ = acir_format::Composer(crs_factory); + composer_ = acir_format::Composer(); vinfo("computing proving key..."); proving_key_ = composer_.compute_proving_key(builder_); } -std::vector AcirComposer::create_proof( - std::shared_ptr> const& crs_factory, - acir_format::acir_format& constraint_system, - acir_format::WitnessVector& witness, - bool is_recursive) +std::vector AcirComposer::create_proof(acir_format::acir_format& constraint_system, + acir_format::WitnessVector& witness, + bool is_recursive) { // Release prior memory first. composer_ = acir_format::Composer(/*p_key=*/0, /*v_key=*/0); @@ -67,12 +63,10 @@ std::vector AcirComposer::create_proof( composer_ = [&]() { if (proving_key_) { - auto composer = acir_format::Composer(proving_key_, verification_key_); - // You can't produce the verification key unless you manually set the crs. Which seems like a bug. - composer_.crs_factory_ = crs_factory; + auto composer = acir_format::Composer(proving_key_, nullptr); return composer; } else { - return acir_format::Composer(crs_factory); + return acir_format::Composer(); } }(); if (!proving_key_) { diff --git a/barretenberg/cpp/src/barretenberg/examples/c_bind.cpp b/barretenberg/cpp/src/barretenberg/examples/c_bind.cpp index 23129857262..53f373658fe 100644 --- a/barretenberg/cpp/src/barretenberg/examples/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/examples/c_bind.cpp @@ -6,7 +6,7 @@ using namespace proof_system::plonk::stdlib::types; WASM_EXPORT void examples_simple_create_and_verify_proof(bool* valid) { - auto ptrs = examples::simple::create_builder_and_composer(barretenberg::srs::get_crs_factory()); + auto ptrs = examples::simple::create_builder_and_composer(); auto proof = examples::simple::create_proof(ptrs); *valid = examples::simple::verify_proof(ptrs, proof); examples::simple::delete_builder_and_composer(ptrs); diff --git a/barretenberg/cpp/src/barretenberg/examples/simple/simple.hpp b/barretenberg/cpp/src/barretenberg/examples/simple/simple.hpp index 0932708bf21..264d328d2d2 100644 --- a/barretenberg/cpp/src/barretenberg/examples/simple/simple.hpp +++ b/barretenberg/cpp/src/barretenberg/examples/simple/simple.hpp @@ -12,8 +12,7 @@ struct BuilderComposerPtrs { Composer* composer; }; -BuilderComposerPtrs create_builder_and_composer( - std::shared_ptr> const& crs_factory); +BuilderComposerPtrs create_builder_and_composer(); proof create_proof(BuilderComposerPtrs pair); diff --git a/barretenberg/cpp/src/barretenberg/examples/simple/simple.test.cpp b/barretenberg/cpp/src/barretenberg/examples/simple/simple.test.cpp index a43c3de4503..17689497a75 100644 --- a/barretenberg/cpp/src/barretenberg/examples/simple/simple.test.cpp +++ b/barretenberg/cpp/src/barretenberg/examples/simple/simple.test.cpp @@ -8,8 +8,8 @@ namespace examples::simple { TEST(examples_simple, create_proof) { auto srs_path = std::filesystem::absolute("../srs_db/ignition"); - auto crs_factory = std::make_shared>(srs_path); - auto ptrs = create_builder_and_composer(crs_factory); + srs::init_crs_factory(srs_path); + auto ptrs = create_builder_and_composer(); auto proof = create_proof(ptrs); bool valid = verify_proof(ptrs, proof); delete_builder_and_composer(ptrs); diff --git a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/compute_circuit_data.hpp b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/compute_circuit_data.hpp index 40f39d24e4e..cdf0dbeb171 100644 --- a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/compute_circuit_data.hpp +++ b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/compute_circuit_data.hpp @@ -56,9 +56,9 @@ circuit_data get_circuit_data(std::string const& name, circuit_data data; data.srs = srs; data.mock = mock; - Composer composer(srs); + Composer composer; Builder builder; - Composer mock_proof_composer(srs); + Composer mock_proof_composer; Builder mock_builder; BenchmarkInfoCollator benchmark_collator; diff --git a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/c_bind.cpp b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/c_bind.cpp deleted file mode 100644 index fcddcbfdb94..00000000000 --- a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/c_bind.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include - -#include "../mock/mock_circuit.hpp" -#include "barretenberg/common/container.hpp" -#include "barretenberg/common/mem.hpp" -#include "barretenberg/common/streams.hpp" -#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" -#include "barretenberg/join_split_example/types.hpp" -#include "barretenberg/plonk/proof_system/proving_key/serialize.hpp" -#include "barretenberg/srs/global_crs.hpp" -#include "c_bind.h" -#include "compute_signing_data.hpp" -#include "join_split.hpp" - -using namespace barretenberg; -using namespace join_split_example::proofs::join_split; - -WASM_EXPORT void join_split__init_proving_key(bool mock) -{ - init_proving_key(barretenberg::srs::get_crs_factory(), mock); -} - -// WASM_EXPORT void join_split__init_proving_key_from_buffer(uint8_t const* pk_buf) -// { -// std::shared_ptr crs; -// plonk::proving_key_data pk_data; -// read(pk_buf, pk_data); -// init_proving_key(crs, std::move(pk_data)); -// } - -WASM_EXPORT void join_split__release_key() -{ - release_proving_key(); -} - -WASM_EXPORT uint32_t join_split__get_new_proving_key_data(uint8_t** output) -{ - // Computing the size of the serialized key is non trivial. We know it's ~331mb. - // Allocate a buffer large enough to hold it, and abort if we overflow. - // This is to keep memory usage down. - - auto proving_key = get_proving_key(); - auto buffer = to_buffer(*proving_key); - auto raw_buf = (uint8_t*)malloc(buffer.size()); - memcpy(raw_buf, (void*)buffer.data(), buffer.size()); - *output = raw_buf; - - return static_cast(buffer.size()); -} - -WASM_EXPORT void join_split__init_verification_key(void* /*unused*/, uint8_t const* /*unused*/) -{ - init_verification_key(barretenberg::srs::get_crs_factory()); -} - -// WASM_EXPORT void join_split__init_verification_key_from_buffer(uint8_t const* vk_buf, uint8_t const* g2x) -// { -// auto crs = std::make_shared(g2x); -// plonk::verification_key_data vk_data; -// read(vk_buf, vk_data); -// init_verification_key(crs, std::move(vk_data)); -// } - -WASM_EXPORT uint32_t join_split__get_new_verification_key_data(uint8_t** output) -{ - auto buffer = to_buffer(*get_verification_key()); - auto raw_buf = (uint8_t*)malloc(buffer.size()); - memcpy(raw_buf, (void*)buffer.data(), buffer.size()); - *output = raw_buf; - return static_cast(buffer.size()); -} - -WASM_EXPORT void join_split__compute_signing_data(uint8_t const* join_split_tx_buf, uint8_t* output) -{ - auto tx = from_buffer(join_split_tx_buf); - auto signing_data = compute_signing_data(tx); - barretenberg::fr::serialize_to_buffer(signing_data, output); -} - -WASM_EXPORT void* join_split__new_prover(uint8_t const* join_split_buf, bool mock) -{ - auto tx = from_buffer(join_split_buf); - auto prover = new_join_split_prover(tx, mock); - auto heapProver = new join_split_example::Prover(std::move(prover)); - return heapProver; -} - -WASM_EXPORT void join_split__delete_prover(void* prover) -{ - delete reinterpret_cast(prover); -} - -WASM_EXPORT bool join_split__verify_proof(uint8_t* proof, uint32_t length) -{ - plonk::proof pp = { std::vector(proof, proof + length) }; - return verify_proof(pp); -} diff --git a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/c_bind.h b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/c_bind.h deleted file mode 100644 index cd7390b1dad..00000000000 --- a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/c_bind.h +++ /dev/null @@ -1,3 +0,0 @@ -#include - -WASM_EXPORT uint32_t join_split__get_new_proving_key_data(uint8_t** output); diff --git a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.cpp b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.cpp index 1da1caee45f..c4c52a1be38 100644 --- a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.cpp +++ b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.cpp @@ -14,8 +14,7 @@ using namespace proof_system::plonk::stdlib::merkle_tree; static std::shared_ptr proving_key; static std::shared_ptr verification_key; -void init_proving_key(std::shared_ptr> const& crs_factory, - bool mock) +void init_proving_key(bool mock) { if (proving_key) { return; @@ -27,12 +26,12 @@ void init_proving_key(std::shared_ptr> const& crs_factory) +void init_verification_key() { if (!proving_key) { std::abort(); } - // Patch the 'nothing' reference string fed to init_proving_key. - proving_key->reference_string = crs_factory->get_prover_crs(proving_key->circuit_size + 1); verification_key = - proof_system::plonk::compute_verification_key_common(proving_key, crs_factory->get_verifier_crs()); + proof_system::plonk::compute_verification_key_common(proving_key, srs::get_crs_factory()->get_verifier_crs()); } Prover new_join_split_prover(join_split_tx const& tx, bool mock) diff --git a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.hpp b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.hpp index 1d4a1dd5fa7..a436d99f884 100644 --- a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.hpp +++ b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.hpp @@ -7,12 +7,11 @@ namespace join_split_example { namespace proofs { namespace join_split { -void init_proving_key(std::shared_ptr> const& crs_factory, - bool mock); +void init_proving_key(bool mock); void release_proving_key(); -void init_verification_key(std::shared_ptr> const& crs_factory); +void init_verification_key(); Prover new_join_split_prover(join_split_tx const& tx, bool mock); diff --git a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.test.cpp b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.test.cpp index 8b86c4d1641..ef41bcfbf67 100644 --- a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.test.cpp +++ b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.test.cpp @@ -44,11 +44,10 @@ class join_split_tests : public ::testing::Test { static void SetUpTestCase() { barretenberg::srs::init_crs_factory("../srs_db/ignition"); - auto null_crs_factory = std::make_shared>(); - init_proving_key(null_crs_factory, false); + init_proving_key(false); auto crs_factory = std::make_unique>("../srs_db/ignition"); - init_verification_key(std::move(crs_factory)); + init_verification_key(); info("vk hash: ", get_verification_key()->sha256_hash()); } diff --git a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split_js_parity.test.cpp b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split_js_parity.test.cpp index f23bceef85f..ba601d93447 100644 --- a/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split_js_parity.test.cpp +++ b/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split_js_parity.test.cpp @@ -25,11 +25,9 @@ class join_split_js_parity_tests : public ::testing::Test { protected: static void SetUpTestCase() { - auto null_crs_factory = std::make_shared>(); - init_proving_key(null_crs_factory, false); - auto crs_factory = - std::make_unique>("../srs_db/ignition"); - init_verification_key(std::move(crs_factory)); + srs::init_crs_factory("../srs_db/ignition"); + init_proving_key(false); + init_verification_key(); info("vk hash: ", get_verification_key()->sha256_hash()); } From 9f0987dc9a7ad69c9637d1ca491216c956e0ddb4 Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Tue, 10 Oct 2023 19:15:33 +0000 Subject: [PATCH 6/9] Formatting. --- .../cpp/src/barretenberg/plonk/composer/ultra_composer.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp b/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp index e539db4c47b..0d365a6c0c4 100644 --- a/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp @@ -38,7 +38,6 @@ class UltraComposer { UltraComposer() = default; - UltraComposer(std::shared_ptr p_key, std::shared_ptr v_key) : circuit_proving_key(std::move(p_key)) , circuit_verification_key(std::move(v_key)) From 91ed471db2af7ffdff7fa336495c7329145d7e5e Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Tue, 10 Oct 2023 19:48:30 +0000 Subject: [PATCH 7/9] Fix oopsie. --- .../cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 5019324e298..da774efecd3 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -106,6 +106,7 @@ void AcirComposer::load_verification_key(proof_system::plonk::verification_key_d { verification_key_ = std::make_shared( std::move(data), srs::get_crs_factory()->get_verifier_crs()); + composer_ = acir_format::Composer(proving_key_, verification_key_); } bool AcirComposer::verify_proof(std::vector const& proof, bool is_recursive) From ff25df277cd542c71a59d9e65339c0ea197c4442 Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Tue, 10 Oct 2023 20:10:10 +0000 Subject: [PATCH 8/9] use init --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 8657385bfdd..169dc6a10f4 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -143,9 +143,7 @@ void gateCount(const std::string& bytecodePath) */ bool verify(const std::string& proof_path, bool recursive, const std::string& vk_path) { - auto g2_data = get_g2_data(CRS_PATH); - srs::init_crs_factory({}, g2_data); - acir_proofs::AcirComposer acir_composer(1, verbose); + auto acir_composer = init(); auto vk_data = from_buffer(read_file(vk_path)); acir_composer.load_verification_key(std::move(vk_data)); auto verified = acir_composer.verify_proof(read_file(proof_path), recursive); From 68426899f040a3069bd75b2860da70d1590d6a35 Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Tue, 10 Oct 2023 20:15:14 +0000 Subject: [PATCH 9/9] Fix. --- .../cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index da774efecd3..8ed136b7b6e 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -58,6 +58,7 @@ std::vector AcirComposer::create_proof(acir_format::acir_format& constr composer_ = acir_format::Composer(/*p_key=*/0, /*v_key=*/0); vinfo("building circuit..."); + builder_ = acir_format::Builder(size_hint_); create_circuit_with_witness(builder_, constraint_system, witness); vinfo("gates: ", builder_.get_total_circuit_size());