From 720be5b909e2750f570f7d2a5778d8b226f372ce Mon Sep 17 00:00:00 2001 From: Daniel Kogtev Date: Tue, 29 Oct 2024 20:45:09 +0000 Subject: [PATCH] proof-generator: add circuit writer (#111) --- .../include/nil/proof-generator/prover.hpp | 23 ++++- .../nil/proof-generator/circuit_writer.hpp | 85 +++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 proof-producer/libs/output_artifacts/include/nil/proof-generator/circuit_writer.hpp diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/prover.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/prover.hpp index 6930ed5cc7..7121b03971 100644 --- a/proof-producer/bin/proof-producer/include/nil/proof-generator/prover.hpp +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/prover.hpp @@ -59,6 +59,7 @@ #include #include +#include #include namespace nil { @@ -539,6 +540,26 @@ namespace nil { *marshalled_value ) ); + + return true; + } + + bool save_circuit_to_file(boost::filesystem::path circuit_file) { + using writer = nil::proof_generator::circuit_writer; + + BOOST_LOG_TRIVIAL(info) << "Writing circuit to " << circuit_file; + if (!constraint_system_) { + BOOST_LOG_TRIVIAL(error) << "No circuit is currently loaded"; + return false; + } + + std::ofstream out(circuit_file.string(), std::ios::binary | std::ios::out); + if (!out.is_open()) { + BOOST_LOG_TRIVIAL(error) << "Failed to open file " << circuit_file; + return false; + } + + writer::write_binary_circuit(out, *constraint_system_, constraint_system_->public_input_sizes()); return true; } @@ -589,7 +610,7 @@ namespace nil { BOOST_LOG_TRIVIAL(info) << "Writing binary assignment table to " << output_filename; if (!assignment_table_.has_value() || !table_description_.has_value()) { - BOOST_LOG_TRIVIAL(error) << "No assignment table is currently loaded into the Prover"; + BOOST_LOG_TRIVIAL(error) << "No assignment table is currently loaded"; return false; } diff --git a/proof-producer/libs/output_artifacts/include/nil/proof-generator/circuit_writer.hpp b/proof-producer/libs/output_artifacts/include/nil/proof-generator/circuit_writer.hpp new file mode 100644 index 0000000000..118c793c6f --- /dev/null +++ b/proof-producer/libs/output_artifacts/include/nil/proof-generator/circuit_writer.hpp @@ -0,0 +1,85 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2024 Daniil Kogtev +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//---------------------------------------------------------------------------// + +#ifndef PROOF_GENERATOR_CIRCUIT_WRITER_HPP +#define PROOF_GENERATOR_CIRCUIT_WRITER_HPP + +#include +#include + +#include + +#include +#include + + +namespace nil { + namespace proof_generator { + + + template + class circuit_writer { + public: + using TTypeBase = nil::marshalling::field_type; + using Circuit = nil::crypto3::zk::snark::plonk_constraint_system; + + circuit_writer() = delete; + + /** + * @brief Write circuit serialized into binary to output file. + */ + static void write_binary_circuit( + std::ostream& out, + const Circuit& circuit, + const std::vector& public_input_column_sizes) { + + namespace marshalling_types = nil::crypto3::marshalling::types; + using value_marshalling_type = marshalling_types::plonk_constraint_system; + + // fill public input sizes + marshalling_types::public_input_sizes_type public_input_sizes; + using public_input_size_type = typename marshalling_types::public_input_sizes_type::element_type; + + const auto public_input_size = public_input_column_sizes.size(); + for (const auto i : public_input_column_sizes) { + public_input_sizes.value().push_back(public_input_size_type(i)); + } + + auto filled_val = + value_marshalling_type(std::make_tuple( + marshalling_types::fill_plonk_gates(circuit.gates()), + marshalling_types::fill_plonk_copy_constraints(circuit.copy_constraints()), + marshalling_types::fill_plonk_lookup_gates(circuit.lookup_gates()), + marshalling_types::fill_plonk_lookup_tables(circuit.lookup_tables()), + public_input_sizes + )); + + + std::vector cv(filled_val.length(), 0x00); + auto iter = cv.begin(); + BOOST_ASSERT(filled_val.write(iter, cv.size()) == nil::marshalling::status_type::success); + out.write(reinterpret_cast(cv.data()), cv.size()); + } + }; + + } // namespace proof_generator +} // namespace nil + + +#endif // PROOF_GENERATOR_CIRCUIT_WRITER_HPP + + +