Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Use global crs in more places. Less pain. #2772

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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<uint64_t>(gate_count));
vinfo("gate count: ", gate_count);
Expand All @@ -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<plonk::verification_key_data>(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);

Expand All @@ -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);
Expand All @@ -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<plonk::verification_key_data>(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);
Expand Down Expand Up @@ -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<plonk::verification_key_data>(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 == "-") {
Expand All @@ -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<plonk::verification_key_data>(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());
Expand Down
28 changes: 10 additions & 18 deletions barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<barretenberg::srs::factories::CrsFactory<curve::BN254>> 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_);
Expand All @@ -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<uint8_t> AcirComposer::create_proof(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
acir_format::acir_format& constraint_system,
acir_format::WitnessVector& witness,
bool is_recursive)
std::vector<uint8_t> 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);
Expand All @@ -67,12 +63,10 @@ std::vector<uint8_t> 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_) {
Expand Down Expand Up @@ -108,12 +102,10 @@ std::shared_ptr<proof_system::plonk::verification_key> AcirComposer::init_verifi
return verification_key_;
}

void AcirComposer::load_verification_key(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> 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<proof_system::plonk::verification_key>(std::move(data), crs_factory->get_verifier_crs());
verification_key_ = std::make_shared<proof_system::plonk::verification_key>(
std::move(data), srs::get_crs_factory()->get_verifier_crs());
composer_ = acir_format::Composer(proving_key_, verification_key_);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ class AcirComposer {

void create_circuit(acir_format::acir_format& constraint_system);

void init_proving_key(std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
acir_format::acir_format& constraint_system);

std::vector<uint8_t> create_proof(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
acir_format::acir_format& constraint_system,
acir_format::WitnessVector& witness,
bool is_recursive);

void load_verification_key(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
proof_system::plonk::verification_key_data&& data);
void init_proving_key(acir_format::acir_format& constraint_system);

std::vector<uint8_t> 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<proof_system::plonk::verification_key> init_verification_key();

Expand Down
7 changes: 3 additions & 4 deletions barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_proofs::AcirComposer*>(*acir_composer_ptr);
auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer<std::vector<uint8_t>>(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,
Expand All @@ -48,16 +48,15 @@ WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr,
auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer<std::vector<uint8_t>>(acir_vec));
auto witness = acir_format::witness_buf_to_witness_data(from_buffer<std::vector<uint8_t>>(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);
}

WASM_EXPORT void acir_load_verification_key(in_ptr acir_composer_ptr, uint8_t const* vk_buf)
{
auto acir_composer = reinterpret_cast<acir_proofs::AcirComposer*>(*acir_composer_ptr);
auto vk_data = from_buffer<plonk::verification_key_data>(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)
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/examples/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions barretenberg/cpp/src/barretenberg/examples/simple/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ void build_circuit(Builder& builder)
}
}

BuilderComposerPtrs create_builder_and_composer(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory)
BuilderComposerPtrs create_builder_and_composer()
{
// WARNING: Size hint is essential to perform 512k circuits!
auto builder = std::make_unique<Builder>(CIRCUIT_SIZE);
Expand All @@ -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<Composer>(crs_factory);
auto composer = std::make_unique<Composer>();
auto pk = composer->compute_proving_key(*builder);

return { builder.release(), composer.release() };
Expand Down
3 changes: 1 addition & 2 deletions barretenberg/cpp/src/barretenberg/examples/simple/simple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ struct BuilderComposerPtrs {
Composer* composer;
};

BuilderComposerPtrs create_builder_and_composer(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory);
BuilderComposerPtrs create_builder_and_composer();

proof create_proof(BuilderComposerPtrs pair);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<barretenberg::srs::factories::FileCrsFactory<curve::BN254>>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

This file was deleted.

This file was deleted.

Loading