Skip to content

Commit

Permalink
Save preprocessed common data to file. Temp solution
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Feb 19, 2024
1 parent 0cac135 commit eb21fbb
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 134 deletions.
3 changes: 1 addition & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ BraceWrapping:
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakStringLiterals: true
ColumnLimit: 120
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,11 @@ jobs:
- name: Run clang-format
run: |
find bin -iname '*.cpp' -o -iname '*.hpp' | xargs clang-format -n --Werror
if [ $? -ne 0 ]; then
if find bin -iname '*.cpp' -o -iname '*.hpp' | xargs clang-format -n --Werror; then
echo "Code formatting is correct"
else
echo "Code formatting differs from clang-format's expectations, run 'find bin -iname *.hpp -o -iname *.cpp | xargs clang-format -i'"
exit 1
else
echo "Code formatting is correct"
fi
- name: Mark git directory as safe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace nil {

struct prover_options {
boost::filesystem::path proof_file_path = "proof.bin";
boost::filesystem::path preprocessed_common_data_path = "preprocessed_common_data.dat";
boost::filesystem::path circuit_file_path;
boost::filesystem::path assignment_table_file_path;
boost::log::trivial::severity_level log_level = boost::log::trivial::severity_level::info;
Expand Down
83 changes: 0 additions & 83 deletions bin/proof-generator/include/nil/proof-generator/detail/utils.hpp

This file was deleted.

90 changes: 71 additions & 19 deletions bin/proof-generator/include/nil/proof-generator/file_operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ namespace nil {
}

template<typename StreamType>
std::optional<StreamType> open_file(
const std::string& path,
std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out
) {
std::optional<StreamType> open_file(const std::string& path, std::ios_base::openmode mode) {
StreamType file(path, mode);
if (!file.is_open()) {
BOOST_LOG_TRIVIAL(error) << "Unable to open file: " << path;
Expand All @@ -101,27 +98,16 @@ namespace nil {
return file;
}

auto open_file_r(const std::string& path, std::ios_base::openmode mode = std::ios_base::in) {
return open_file<std::ifstream>(path, mode);
}

auto open_file_w(const std::string& path, std::ios_base::openmode mode = std::ios_base::out) {
return open_file<std::ofstream>(path, mode);
}
std::optional<std::vector<std::uint8_t>> read_file_to_vector(const std::string& path) {

std::optional<std::vector<std::uint8_t>> read_file_to_vector(
const std::string& path,
std::ios_base::openmode mode = std::ios_base::binary
) {
auto file = open_file_r(path, mode);
auto file = open_file<std::ifstream>(path, std::ios_base::in | std::ios::binary | std::ios::ate);
if (!file.has_value()) {
return std::nullopt;
}

std::ifstream& stream = file.value();
stream.seekg(0, std::ios_base::end);
const auto fsize = stream.tellg();
stream.seekg(0, std::ios_base::beg);
std::streamsize fsize = stream.tellg();
stream.seekg(0, std::ios::beg);
std::vector<std::uint8_t> v(static_cast<size_t>(fsize));

stream.read(reinterpret_cast<char*>(v.data()), fsize);
Expand All @@ -134,6 +120,72 @@ namespace nil {
return v;
}

bool write_vector_to_file(const std::vector<std::uint8_t>& vector, const std::string& path) {

auto file = open_file<std::ofstream>(path, std::ios_base::out | std::ios_base::binary);
if (!file.has_value()) {
return false;
}

std::ofstream& stream = file.value();
stream.write(reinterpret_cast<const char*>(vector.data()), vector.size());

if (stream.fail()) {
BOOST_LOG_TRIVIAL(error) << "Error occured during writing file " << path;
return false;
}

return true;
}

// HEX data format is not efficient, we will remove it later
std::optional<std::vector<std::uint8_t>> read_hex_file_to_vector(const std::string& path) {
auto file = open_file<std::ifstream>(path, std::ios_base::in);
if (!file.has_value()) {
return std::nullopt;
}

std::ifstream& stream = file.value();
std::vector<uint8_t> result;
std::string line;
while (std::getline(stream, line)) {
if (line.rfind("0x", 0) == 0 && line.length() >= 3) {
for (size_t i = 2; i < line.length(); i += 2) {
std::string hex_string = line.substr(i, 2);
uint8_t byte = static_cast<uint8_t>(std::stoul(hex_string, nullptr, 16));
result.push_back(byte);
}
} else {
BOOST_LOG_TRIVIAL(error) << "File contains non-hex string";
return std::nullopt;
}
}

return result;
}

bool write_vector_to_hex_file(const std::vector<std::uint8_t>& vector, const std::string& path) {
auto file = open_file<std::ofstream>(path, std::ios_base::out);
if (!file.has_value()) {
return false;
}

std::ofstream& stream = file.value();

stream << "0x" << std::hex;
for (auto it = vector.cbegin(); it != vector.cend(); ++it) {
stream << std::setfill('0') << std::setw(2) << std::right << int(*it);
}
stream << std::dec;

if (stream.fail()) {
BOOST_LOG_TRIVIAL(error) << "Error occurred during writing to file " << path;
return false;
}

return true;
}

} // namespace proof_generator
} // namespace nil

Expand Down
79 changes: 67 additions & 12 deletions bin/proof-generator/include/nil/proof-generator/prover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <boost/test/unit_test.hpp> // TODO: remove this. Required only because of an incorrect assert check in zk

#include <nil/crypto3/algebra/fields/arithmetic_params/pallas.hpp>
#include <nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp>
#include <nil/crypto3/marshalling/zk/types/placeholder/proof.hpp>
#include <nil/crypto3/marshalling/zk/types/plonk/assignment_table.hpp>
#include <nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp>
Expand All @@ -47,15 +48,17 @@
#include <nil/marshalling/field_type.hpp>
#include <nil/marshalling/status_type.hpp>
#include <nil/proof-generator/arithmetization_params.hpp>
#include <nil/proof-generator/detail/utils.hpp>
#include <nil/proof-generator/file_operations.hpp>

namespace nil {
namespace proof_generator {
namespace detail {
template<typename MarshallingType>
std::optional<MarshallingType> parse_marshalling_from_file(const boost::filesystem::path& path) {
const auto v = read_file_to_vector(path.c_str());
std::optional<MarshallingType> decode_marshalling_from_file(
const boost::filesystem::path& path,
bool hex = false
) {
const auto v = hex ? read_hex_file_to_vector(path.c_str()) : read_file_to_vector(path.c_str());
if (!v.has_value()) {
return std::nullopt;
}
Expand All @@ -64,12 +67,30 @@ namespace nil {
auto read_iter = v->begin();
auto status = marshalled_data.read(read_iter, v->size());
if (status != nil::marshalling::status_type::success) {
BOOST_LOG_TRIVIAL(error) << "Marshalled structure parsing failed";
BOOST_LOG_TRIVIAL(error) << "Marshalled structure decoding failed";
return std::nullopt;
}
return marshalled_data;
}

template<typename MarshallingType>
bool encode_marshalling_to_file(
const boost::filesystem::path& path,
const MarshallingType& data_for_marshalling,
bool hex = false
) {
std::vector<std::uint8_t> v;
v.resize(data_for_marshalling.length(), 0x00);
auto write_iter = v.begin();
nil::marshalling::status_type status = data_for_marshalling.write(write_iter, v.size());
if (status != nil::marshalling::status_type::success) {
BOOST_LOG_TRIVIAL(error) << "Marshalled structure encoding failed";
return false;
}

return hex ? write_vector_to_hex_file(v, path.c_str()) : write_vector_to_file(v, path.c_str());
}

std::vector<std::size_t> generate_random_step_list(const std::size_t r, const int max_step) {
using Distribution = std::uniform_int_distribution<int>;
static std::random_device random_engine;
Expand Down Expand Up @@ -119,10 +140,12 @@ namespace nil {
public:
Prover(
boost::filesystem::path circuit_file_name,
boost::filesystem::path preprocessed_common_data_file_name,
boost::filesystem::path assignment_table_file_name,
boost::filesystem::path proof_file
)
: circuit_file_(circuit_file_name)
, preprocessed_common_data_file_(preprocessed_common_data_file_name)
, assignment_table_file_(assignment_table_file_name)
, proof_file_(proof_file) {
}
Expand Down Expand Up @@ -163,22 +186,53 @@ namespace nil {
BOOST_LOG_TRIVIAL(info) << "Writing proof to " << proof_file_;
auto filled_placeholder_proof =
nil::crypto3::marshalling::types::fill_placeholder_proof<Endianness, Proof>(proof, *fri_params_);
nil::proof_generator::detail::proof_print<Endianness, Proof>(proof, *fri_params_, proof_file_);
BOOST_LOG_TRIVIAL(info) << "Proof written";
return true;
bool res = nil::proof_generator::detail::encode_marshalling_to_file(
proof_file_,
filled_placeholder_proof,
true
);
if (res) {
BOOST_LOG_TRIVIAL(info) << "Proof written";
}

return res;
}

bool verify_from_file() {
prepare_for_operation();
using ProofMarshalling = nil::crypto3::marshalling::types::
placeholder_proof<nil::marshalling::field_type<Endianness>, Proof>;
auto marshalled_proof = detail::parse_marshalling_from_file<ProofMarshalling>(proof_file_);
BOOST_LOG_TRIVIAL(info) << "Reading proof from file";
auto marshalled_proof = detail::decode_marshalling_from_file<ProofMarshalling>(proof_file_, true);
if (!marshalled_proof) {
return false;
}
return verify(
nil::crypto3::marshalling::types::make_placeholder_proof<Endianness, Proof>(*marshalled_proof)
bool res =
verify(nil::crypto3::marshalling::types::make_placeholder_proof<Endianness, Proof>(*marshalled_proof
));
if (res) {
BOOST_LOG_TRIVIAL(info) << "Proof verified";
}
return res;
}

bool save_preprocessed_common_data_to_file() {
BOOST_LOG_TRIVIAL(info) << "Writing preprocessed common data to file...";
using Endianness = nil::marshalling::option::big_endian;
using TTypeBase = nil::marshalling::field_type<Endianness>;
using CommonData = typename PublicPreprocessedData::preprocessed_data_type::common_data_type;
auto marshalled_common_data =
nil::crypto3::marshalling::types::fill_placeholder_common_data<Endianness, CommonData>(
public_preprocessed_data_->common_data
);
bool res = nil::proof_generator::detail::encode_marshalling_to_file(
preprocessed_common_data_file_,
marshalled_common_data
);
if (res) {
BOOST_LOG_TRIVIAL(info) << "Preprocessed common data written";
}
return res;
}

private:
Expand Down Expand Up @@ -248,7 +302,7 @@ namespace nil {
nil::crypto3::zk::snark::plonk_table<BlueprintField, ArithmetizationParams, Column>;

{
auto marshalled_value = detail::parse_marshalling_from_file<ConstraintMarshalling>(circuit_file_);
auto marshalled_value = detail::decode_marshalling_from_file<ConstraintMarshalling>(circuit_file_);
if (!marshalled_value) {
return false;
}
Expand All @@ -265,7 +319,7 @@ namespace nil {
{
TableDescription table_description;
auto marshalled_table =
detail::parse_marshalling_from_file<TableValueMarshalling>(assignment_table_file_);
detail::decode_marshalling_from_file<TableValueMarshalling>(assignment_table_file_);
if (!marshalled_table) {
return false;
}
Expand Down Expand Up @@ -307,6 +361,7 @@ namespace nil {
}

const boost::filesystem::path circuit_file_;
const boost::filesystem::path preprocessed_common_data_file_;
const boost::filesystem::path assignment_table_file_;
const boost::filesystem::path proof_file_;

Expand Down
Loading

0 comments on commit eb21fbb

Please sign in to comment.