Skip to content

Commit

Permalink
proof-generator: add circuit writer (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
oclaw authored Oct 29, 2024
1 parent 91dfb3e commit 720be5b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

#include <nil/proof-generator/arithmetization_params.hpp>
#include <nil/proof-generator/assignment_table_writer.hpp>
#include <nil/proof-generator/circuit_writer.hpp>
#include <nil/proof-generator/file_operations.hpp>

namespace nil {
Expand Down Expand Up @@ -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<Endianness, BlueprintField>;

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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Daniil Kogtev <[email protected]>
//
// 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 <ostream>
#include <vector>

#include <boost/assert.hpp>

#include <nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>


namespace nil {
namespace proof_generator {


template <typename Endianness, typename BlueprintField>
class circuit_writer {
public:
using TTypeBase = nil::marshalling::field_type<Endianness>;
using Circuit = nil::crypto3::zk::snark::plonk_constraint_system<BlueprintField>;

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<std::size_t>& public_input_column_sizes) {

namespace marshalling_types = nil::crypto3::marshalling::types;
using value_marshalling_type = marshalling_types::plonk_constraint_system<TTypeBase, Circuit>;

// fill public input sizes
marshalling_types::public_input_sizes_type<TTypeBase> public_input_sizes;
using public_input_size_type = typename marshalling_types::public_input_sizes_type<TTypeBase>::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<Endianness, typename Circuit::gates_container_type::value_type>(circuit.gates()),
marshalling_types::fill_plonk_copy_constraints<Endianness, typename Circuit::field_type>(circuit.copy_constraints()),
marshalling_types::fill_plonk_lookup_gates<Endianness, typename Circuit::lookup_gates_container_type::value_type>(circuit.lookup_gates()),
marshalling_types::fill_plonk_lookup_tables<Endianness, typename Circuit::lookup_tables_type::value_type>(circuit.lookup_tables()),
public_input_sizes
));


std::vector<std::uint8_t> 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<char*>(cv.data()), cv.size());
}
};

} // namespace proof_generator
} // namespace nil


#endif // PROOF_GENERATOR_CIRCUIT_WRITER_HPP



0 comments on commit 720be5b

Please sign in to comment.