Skip to content

Commit

Permalink
JSON generation added #86
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Mar 29, 2024
1 parent 4ddd858 commit 6c92fb0
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
[submodule "libs/actor/container"]
path = libs/actor/container
url = ../../NilFoundation/actor-containers.git
[submodule "libs/transpiler"]
path = libs/transpiler
url = git@github.com:NilFoundation/zkllvm-transpiler.git
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/libs/actor/core" EXCLUDE_FROM_ALL)
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/libs/actor/math" EXCLUDE_FROM_ALL)
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/libs/actor/zk" EXCLUDE_FROM_ALL)

add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/libs/transpiler" EXCLUDE_FROM_ALL)

if(NOT PROOF_PRODUCER_VERSION)
find_program(GIT_EXECUTABLE NAMES git PATHS /usr/bin /usr/local/bin)
message(STATUS "Running command: ${GIT_EXECUTABLE} describe --tags HEAD")
Expand Down
5 changes: 4 additions & 1 deletion bin/proof-generator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ function(setup_proof_generator_target)
marshalling::crypto3_multiprecision
marshalling::crypto3_zk

crypto3::transpiler

Boost::filesystem
Boost::log
Boost::program_options
Boost::thread)
Boost::thread
)

if (APPLE OR NOT ${CMAKE_TARGET_ARCHITECTURE} STREQUAL ${CMAKE_HOST_SYSTEM_PROCESSOR})
set_target_properties(${CURRENT_PROJECT_NAME} PROPERTIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace nil {

struct ProverOptions {
boost::filesystem::path proof_file_path = "proof.bin";
boost::filesystem::path json_file_path = "proof.json";
boost::filesystem::path preprocessed_common_data_path = "preprocessed_common_data.dat";
boost::filesystem::path circuit_file_path;
boost::filesystem::path assignment_table_file_path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/crypto3/hash/keccak.hpp>
#include <nil/crypto3/hash/poseidon.hpp>

#include <nil/proof-generator/non_type_arithmetization_params.hpp>

Expand All @@ -42,9 +43,12 @@ namespace nil {
// Add more curves as needed.
>;

using HashTypes = std::tuple<nil::crypto3::hashes::keccak_1600<256>
// Add more hashes as needed.
>;
using HashTypes = std::tuple<
nil::crypto3::hashes::keccak_1600<256>,
nil::crypto3::hashes::poseidon<nil::crypto3::hashes::detail::mina_poseidon_policy<
typename nil::crypto3::algebra::curves::pallas::base_field_type>>
// Add more hashes as needed.
>;

} // namespace proof_generator
} // namespace nil
Expand Down
28 changes: 24 additions & 4 deletions bin/proof-generator/include/nil/proof-generator/prover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/verifier.hpp>

#include <nil/blueprint/transpiler/recursive_verifier_generator.hpp>
#include <nil/marshalling/endianness.hpp>
#include <nil/marshalling/field_type.hpp>
#include <nil/marshalling/status_type.hpp>
Expand Down Expand Up @@ -138,6 +139,7 @@ namespace nil {
boost::filesystem::path preprocessed_common_data_file_name,
boost::filesystem::path assignment_table_file_name,
boost::filesystem::path proof_file,
boost::filesystem::path json_file,
std::size_t component_constant_columns, // We need it to calculate permutation size, and it couldn't be
// established form assignment table yet
std::size_t expand_factor
Expand All @@ -146,6 +148,7 @@ namespace nil {
, preprocessed_common_data_file_(preprocessed_common_data_file_name)
, assignment_table_file_(assignment_table_file_name)
, proof_file_(proof_file)
, json_file_(json_file)
, component_constant_columns_(component_constant_columns)
, expand_factor_(expand_factor) {
}
Expand Down Expand Up @@ -195,6 +198,22 @@ namespace nil {
BOOST_LOG_TRIVIAL(info) << "Proof written";
}

BOOST_LOG_TRIVIAL(info) << "Writing json proof to " << json_file_;
auto output_file = open_file<std::ofstream>(json_file_.string(), std::ios_base::out);
if (!output_file)
return res;

(*output_file) << nil::blueprint::recursive_verifier_generator<
PlaceholderParams,
nil::crypto3::zk::snark::placeholder_proof<BlueprintField, PlaceholderParams>,
typename nil::crypto3::zk::snark::placeholder_public_preprocessor<
BlueprintField,
PlaceholderParams>::preprocessed_data_type::common_data_type>(
*table_description_
)
.generate_input(*public_inputs_, proof, constraint_system_->public_input_sizes());
output_file->close();

return res;
}

Expand Down Expand Up @@ -252,6 +271,8 @@ namespace nil {
using TableDescription = nil::crypto3::zk::snark::plonk_table_description<BlueprintField>;
using Endianness = nil::marshalling::option::big_endian;
using FriParams = typename Lpc::fri_type::params_type;
using Column = nil::crypto3::zk::snark::plonk_column<BlueprintField>;
using AssignmentTable = nil::crypto3::zk::snark::plonk_table<BlueprintField, Column>;

bool verify(const Proof& proof) const {
BOOST_LOG_TRIVIAL(info) << "Verifying proof...";
Expand All @@ -278,10 +299,6 @@ namespace nil {
using TTypeBase = nil::marshalling::field_type<Endianness>;
using ConstraintMarshalling =
nil::crypto3::marshalling::types::plonk_constraint_system<TTypeBase, ConstraintSystem>;

using Column = nil::crypto3::zk::snark::plonk_column<BlueprintField>;
using AssignmentTable = nil::crypto3::zk::snark::plonk_table<BlueprintField, Column>;

{
auto marshalled_value = detail::decode_marshalling_from_file<ConstraintMarshalling>(circuit_file_);
if (!marshalled_value) {
Expand All @@ -305,6 +322,7 @@ namespace nil {
nil::crypto3::marshalling::types::make_assignment_table<Endianness, AssignmentTable>(
*marshalled_table
);
public_inputs_ = assignment_table.public_inputs();
table_description_.emplace(table_description);

// Lambdas and grinding bits should be passed threw preprocessor directives
Expand Down Expand Up @@ -342,12 +360,14 @@ namespace nil {
const boost::filesystem::path preprocessed_common_data_file_;
const boost::filesystem::path assignment_table_file_;
const boost::filesystem::path proof_file_;
const boost::filesystem::path json_file_;
const std::size_t expand_factor_;
const std::size_t component_constant_columns_;

// All set on prepare_for_operation()
std::optional<PublicPreprocessedData> public_preprocessed_data_;
std::optional<PrivatePreprocessedData> private_preprocessed_data_;
std::optional<typename AssignmentTable::public_input_container_type> public_inputs_;
std::optional<TableDescription> table_description_;
std::optional<ConstraintSystem> constraint_system_;
std::optional<FriParams> fri_params_;
Expand Down
16 changes: 10 additions & 6 deletions bin/proof-generator/src/arg_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ namespace nil {
// clang-format off
auto options_appender = config.add_options()
("proof,p", make_defaulted_option(prover_options.proof_file_path), "Output proof file")
("common-data", make_defaulted_option(prover_options.preprocessed_common_data_path), "Output preprocessed common data file")
("circuit,c", po::value(&prover_options.circuit_file_path)->required(), "Circuit input file")
("json,j", make_defaulted_option(prover_options.json_file_path), "JSON proof file")
("common-data,d", make_defaulted_option(prover_options.preprocessed_common_data_path), "Output preprocessed common data file")
("circuit", po::value(&prover_options.circuit_file_path)->required(), "Circuit input file")
("assignment-table,t", po::value(&prover_options.assignment_table_file_path)->required(), "Assignment table input file")
("log-level,l", make_defaulted_option(prover_options.log_level), "Log level (trace, debug, info, warning, error, fatal)")
("elliptic-curve-type,e", make_defaulted_option(prover_options.elliptic_curve_type), "Elliptic curve type (pallas)")
("hash-type", make_defaulted_option(prover_options.hash_type), "Hash type (keccak)")
("hash-type", make_defaulted_option(prover_options.hash_type), "Hash type (keccak, poseidon)")
("lambda-param", make_defaulted_option(prover_options.lambda), "Lambda param (9)")
("grind-param", make_defaulted_option(prover_options.grind), "Grind param (69)")
("expand-factor", make_defaulted_option(prover_options.expand_factor), "Expand factor")
("expand-factor,x", make_defaulted_option(prover_options.expand_factor), "Expand factor")
("component-constant-columns", make_defaulted_option(prover_options.component_constant_columns), "Component constant columns")
("skip-verification", po::bool_switch(&prover_options.skip_verification), "Skip generated proof verifying step")
("verification-only", po::bool_switch(&prover_options.verification_only), "Read proof for verification instead of writing to it");
Expand Down Expand Up @@ -239,13 +240,16 @@ namespace nil {
GENERATE_READ_OPERATOR(CURVE_TYPES, CurvesVariant)
#undef X

#define HASH_TYPES X(nil::crypto3::hashes::keccak_1600<256>, "keccak")
#define HASH_TYPES \
X(nil::crypto3::hashes::keccak_1600<256>, "keccak") \
X(nil::crypto3::hashes::poseidon<nil::crypto3::hashes::detail::mina_poseidon_policy< \
typename nil::crypto3::algebra::curves::pallas::base_field_type>>, \
"poseidon")
#define X(type, name) TYPE_TO_STRING(type, name)
GENERATE_WRITE_OPERATOR(HASH_TYPES, HashesVariant)
#undef X
#define X(type, name) STRING_TO_TYPE(type, name)
GENERATE_READ_OPERATOR(HASH_TYPES, HashesVariant)
#undef X

} // namespace proof_generator
} // namespace nil
1 change: 1 addition & 0 deletions bin/proof-generator/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int run_prover(const nil::proof_generator::ProverOptions& prover_options) {
prover_options.preprocessed_common_data_path,
prover_options.assignment_table_file_path,
prover_options.proof_file_path,
prover_options.json_file_path,
prover_options.component_constant_columns,
prover_options.expand_factor
);
Expand Down
2 changes: 1 addition & 1 deletion libs/actor/zk
1 change: 1 addition & 0 deletions libs/transpiler
Submodule transpiler added at cc7cfa

0 comments on commit 6c92fb0

Please sign in to comment.