From bed4bfca9ac627a1e5b92330d1169a0768cfde0a Mon Sep 17 00:00:00 2001 From: "e.tatuzova" Date: Mon, 4 Mar 2024 00:17:30 +0400 Subject: [PATCH] Crypto3 and libs/actor/zk updated #83 --- CMakeLists.txt | 1 - bin/jsonizer/CMakeLists.txt | 93 ----- .../recursive_json_generator.hpp | 320 --------------- bin/jsonizer/src/main.cpp | 374 ------------------ .../nil/proof-generator/arg_parser.hpp | 4 +- .../arithmetization_params.hpp | 20 +- .../include/nil/proof-generator/prover.hpp | 34 +- bin/proof-generator/src/arg_parser.cpp | 40 +- bin/proof-generator/src/main.cpp | 2 + libs/actor/zk | 2 +- libs/crypto3 | 2 +- 11 files changed, 67 insertions(+), 825 deletions(-) delete mode 100644 bin/jsonizer/CMakeLists.txt delete mode 100644 bin/jsonizer/include/nil/proof-producer/recursive_json_generator.hpp delete mode 100644 bin/jsonizer/src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 885ec55d..a374a7bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,7 +107,6 @@ endif() set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}) add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/bin/proof-generator") -add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/bin/jsonizer") install(SCRIPT cmake/create-symlinks.cmake) diff --git a/bin/jsonizer/CMakeLists.txt b/bin/jsonizer/CMakeLists.txt deleted file mode 100644 index 9d5f8b09..00000000 --- a/bin/jsonizer/CMakeLists.txt +++ /dev/null @@ -1,93 +0,0 @@ -#---------------------------------------------------------------------------# -# Copyright (c) 2024 Nikita Kaskov -# -# Distributed under the Boost Software License, Version 1.0 -# See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt -#---------------------------------------------------------------------------# - -cmake_minimum_required(VERSION 3.5) - -cm_find_package(CM) -include(CMDeploy) -include(CMSetupVersion) - -cm_project(proof2json WORKSPACE_NAME ${CMAKE_WORKSPACE_NAME} LANGUAGES ASM C CXX) - -string(TOUPPER ${CURRENT_PROJECT_NAME} UPPER_CURRENT_PROJECT_NAME) -if (CPACK_PACKAGE_VERSION) - add_compile_definitions(${UPPER_CURRENT_PROJECT_NAME}_VERSION=${CPACK_PACKAGE_VERSION}) -endif() - -if (NOT Boost_FOUND) - find_package(Boost COMPONENTS REQUIRED filesystem log log_setup program_options) -endif () - -cm_setup_version(VERSION 0.1.0) - -# get header files; only needed by CMake generators, -# e.g., for creating proper Xcode projects -set(${CURRENT_PROJECT_NAME}_HEADERS) - -# list cpp files excluding platform-dependent files -list(APPEND ${CURRENT_PROJECT_NAME}_SOURCES - src/main.cpp - ) - -set(${CURRENT_PROJECT_NAME}_HEADERS - ) - -add_executable(${CURRENT_PROJECT_NAME} - ${${CURRENT_PROJECT_NAME}_HEADERS} - ${${CURRENT_PROJECT_NAME}_SOURCES}) - -set_target_properties(${CURRENT_PROJECT_NAME} PROPERTIES - LINKER_LANGUAGE CXX - EXPORT_NAME ${CURRENT_PROJECT_NAME} - CXX_STANDARD 17 - CXX_STANDARD_REQUIRED TRUE) - -list(APPEND libraries - crypto3::algebra - crypto3::block - crypto3::codec - crypto3::multiprecision - crypto3::pkpad - crypto3::pubkey - crypto3::random - crypto3::zk - - marshalling::core - marshalling::crypto3_algebra - marshalling::crypto3_multiprecision - marshalling::crypto3_zk - - Boost::filesystem - Boost::log - Boost::program_options -) - -target_link_libraries(${CURRENT_PROJECT_NAME} ${libraries}) - -target_include_directories(${CURRENT_PROJECT_NAME} PUBLIC - $ - $ - - $<$:${Boost_INCLUDE_DIRS}>) - -if (APPLE OR NOT ${CMAKE_TARGET_ARCHITECTURE} STREQUAL ${CMAKE_HOST_SYSTEM_PROCESSOR}) - set_target_properties(${CURRENT_PROJECT_NAME} PROPERTIES - XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "${APPLE_CODE_SIGN_IDENTITY}" - XCODE_ATTRIBUTE_DEVELOPMENT_TEAM "${CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM}") -elseif (CMAKE_CROSSCOMPILING AND CMAKE_SYSTEM_NAME STREQUAL "Emscripten") - if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten") - set_target_properties(${CURRENT_PROJECT_NAME} PROPERTIES - COMPILE_FLAGS "-s USE_BOOST_HEADERS=1 -s USE_PTHREADS -s PROXY_TO_PTHREAD" - LINK_FLAGS "-s USE_BOOST_HEADERS=1 -s USE_PTHREADS -s PROXY_TO_PTHREAD" - LINK_DIRECTORIES "${CMAKE_BINARY_DIR}/libs/boost/src/boost/stage/lib") - endif () - - add_dependencies(${CURRENT_PROJECT_NAME} boost) -endif () - -install(TARGETS ${CURRENT_PROJECT_NAME} RUNTIME DESTINATION bin) diff --git a/bin/jsonizer/include/nil/proof-producer/recursive_json_generator.hpp b/bin/jsonizer/include/nil/proof-producer/recursive_json_generator.hpp deleted file mode 100644 index 7d70da55..00000000 --- a/bin/jsonizer/include/nil/proof-producer/recursive_json_generator.hpp +++ /dev/null @@ -1,320 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2023 Elena Tatuzova -// -// MIT License -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -//---------------------------------------------------------------------------// -// @file Declaration of interfaces for PLONK unified addition component. -//---------------------------------------------------------------------------// - -#ifndef CRYPTO3_RECURSIVE_JSON_GENERATOR_HPP -#define CRYPTO3_RECURSIVE_JSON_GENERATOR_HPP - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace nil { - namespace proof_producer { - template - std::string to_string(T val) { - std::stringstream strstr; - strstr << val; - return strstr.str(); - } - - template - std::string to_hex_string(T val) { - std::stringstream strstr; - strstr << std::hex << val << std::dec; - return strstr.str(); - } - - template - struct recursive_json_generator { - using field_type = typename PlaceholderParams::field_type; - using arithmetization_params = typename PlaceholderParams::arithmetization_params; - using proof_type = ProofType; - using commitment_scheme_type = typename PlaceholderParams::commitment_scheme_type; - using constraint_system_type = typename PlaceholderParams::constraint_system_type; - using columns_rotations_type = std::array, PlaceholderParams::total_columns>; - using variable_type = typename constraint_system_type::variable_type; - using variable_indices_type = std::map; - using degree_visitor_type = typename constraint_system_type::degree_visitor_type; - using expression_type = typename constraint_system_type::expression_type; - using term_type = typename constraint_system_type::term_type; - using binary_operation_type = typename constraint_system_type::binary_operation_type; - using pow_operation_type = typename constraint_system_type::pow_operation_type; - using assignment_table_type = typename PlaceholderParams::assignment_table_type; - - static std::string generate_field_array2_from_64_hex_string(std::string str) { - BOOST_ASSERT_MSG(str.size() == 64, "input string must be 64 hex characters long"); - std::string first_half = str.substr(0, 32); - std::string second_half = str.substr(32, 32); - return "{\"vector\": [{\"field\": \"0x" + first_half + "\"},{\"field\": \"0x" + second_half + "\"}]}"; - } - - template - static inline std::string generate_hash(typename HashType::digest_type hashed_data) { - if constexpr (std::is_same>::value) { - std::stringstream out; - out << hashed_data; - return generate_field_array2_from_64_hex_string(out.str()); - } else if constexpr (std::is_same>::value) { - std::stringstream out; - out << hashed_data; - return generate_field_array2_from_64_hex_string(out.str()); - } else { - std::stringstream out; - out << "{\"field\": \"" << hashed_data << "\"}"; - return out.str(); - } - BOOST_ASSERT_MSG(false, "unsupported merkle hash type"); - return "unsupported merkle hash type"; - } - - template - static inline std::string generate_commitment(typename CommitmentSchemeType::commitment_type commitment) { - return generate_hash(commitment); - } - - template - static inline std::string generate_eval_proof(typename CommitmentSchemeType::proof_type eval_proof) { - if (CommitmentSchemeType::lpc::use_grinding) { - BOOST_ASSERT_MSG(false, "grinding is not supported"); - std::cout << "Grinding is not supported" << std::endl; - return "Grinding is not supported"; - } - - std::stringstream out; - out << "\t\t{\"array\":[" << std::endl; - auto batch_info = eval_proof.z.get_batch_info(); - std::size_t sum = 0; - std::size_t poly_num = 0; - for (const auto &[k, v] : batch_info) { - for (std::size_t i = 0; i < v; i++) { - poly_num++; - BOOST_ASSERT(eval_proof.z.get_poly_points_number(k, i) != 0); - for (std::size_t j = 0; j < eval_proof.z.get_poly_points_number(k, i); j++) { - if (sum != 0) - out << "," << std::endl; - out << "\t\t\t{\"field\":\"" << eval_proof.z.get(k, i, j) << "\"}"; - sum++; - } - } - } - out << std::endl << "\t\t]}," << std::endl; - out << "\t\t{\"array\": [" << std::endl; - for (std::size_t i = 0; i < eval_proof.fri_proof.fri_roots.size(); i++) { - if (i != 0) - out << "," << std::endl; - out << "\t\t\t" << generate_commitment(eval_proof.fri_proof.fri_roots[i]); - } - out << std::endl << "\t\t]}," << std::endl; - out << "\t\t{\"array\": [" << std::endl; - std::size_t cur = 0; - for (std::size_t i = 0; i < eval_proof.fri_proof.query_proofs.size(); i++) { - for (const auto &[j, initial_proof] : eval_proof.fri_proof.query_proofs[i].initial_proof) { - for (std::size_t k = 0; k < initial_proof.values.size(); k++) { - if (cur != 0) - out << "," << std::endl; - BOOST_ASSERT_MSG(initial_proof.values[k].size() == 1, "Unsupported step_list[0] value"); - out << "\t\t\t{\"field\":\"" << initial_proof.values[k][0][0] << "\"}," << std::endl; - out << "\t\t\t{\"field\":\"" << initial_proof.values[k][0][1] << "\"}"; - cur++; - cur++; - } - } - } - out << std::endl << "\t\t]}," << std::endl; - out << "\t\t{\"array\": [" << std::endl; - cur = 0; - for (std::size_t i = 0; i < eval_proof.fri_proof.query_proofs.size(); i++) { - for (std::size_t j = 0; j < eval_proof.fri_proof.query_proofs[i].round_proofs.size(); j++) { - const auto &round_proof = eval_proof.fri_proof.query_proofs[i].round_proofs[j]; - if (cur != 0) - out << "," << std::endl; - BOOST_ASSERT_MSG(round_proof.y.size() == 1, "Unsupported step_lis value"); - out << "\t\t\t{\"field\":\"" << round_proof.y[0][0] << "\"}," << std::endl; - out << "\t\t\t{\"field\":\"" << round_proof.y[0][1] << "\"}"; - cur++; - cur++; - } - } - out << std::endl << "\t\t]}," << std::endl; - - out << "\t\t{\"array\": [" << std::endl; - cur = 0; - for (std::size_t i = 0; i < eval_proof.fri_proof.query_proofs.size(); i++) { - for (const auto &[j, initial_proof] : eval_proof.fri_proof.query_proofs[i].initial_proof) { - for (std::size_t k = 0; k < initial_proof.p.path().size(); k++) { - if (cur != 0) - out << "," << std::endl; - out << "\t\t\t{\"int\":" << initial_proof.p.path()[k][0].position() << "}"; - cur++; - } - break; - } - } - out << std::endl << "\t\t]}," << std::endl; - - out << "\t\t{\"array\": [" << std::endl; - cur = 0; - for (std::size_t i = 0; i < eval_proof.fri_proof.query_proofs.size(); i++) { - for (const auto &[j, initial_proof] : eval_proof.fri_proof.query_proofs[i].initial_proof) { - for (std::size_t k = 0; k < initial_proof.p.path().size(); k++) { - if (cur != 0) - out << "," << std::endl; - out << "\t\t\t" - << generate_hash( - initial_proof.p.path()[k][0].hash() - ); - cur++; - } - } - } - out << std::endl << "\t\t]}," << std::endl; - - out << "\t\t{\"array\": [" << std::endl; - cur = 0; - for (std::size_t i = 0; i < eval_proof.fri_proof.query_proofs.size(); i++) { - for (std::size_t j = 0; j < eval_proof.fri_proof.query_proofs[i].round_proofs.size(); j++) { - const auto &p = eval_proof.fri_proof.query_proofs[i].round_proofs[j].p; - for (std::size_t k = 0; k < p.path().size(); k++) { - if (cur != 0) - out << "," << std::endl; - out << "\t\t\t{\"int\": " << p.path()[k][0].position() << "}"; - cur++; - } - } - } - out << std::endl << "\t\t]}," << std::endl; - - out << "\t\t{\"array\": [" << std::endl; - cur = 0; - for (std::size_t i = 0; i < eval_proof.fri_proof.query_proofs.size(); i++) { - for (std::size_t j = 0; j < eval_proof.fri_proof.query_proofs[i].round_proofs.size(); j++) { - const auto &p = eval_proof.fri_proof.query_proofs[i].round_proofs[j].p; - for (std::size_t k = 0; k < p.path().size(); k++) { - if (cur != 0) - out << "," << std::endl; - out << "\t\t\t" - << generate_hash( - p.path()[k][0].hash() - ); - cur++; - } - } - } - out << std::endl << "\t\t]}," << std::endl; - - cur = 0; - out << "\t\t{\"array\": [" << std::endl; - for (std::size_t i = 0; i < eval_proof.fri_proof.final_polynomial.size(); i++) { - if (cur != 0) - out << "," << std::endl; - out << "\t\t\t{\"field\": \"" << eval_proof.fri_proof.final_polynomial[i] << "\"}"; - cur++; - } - out << std::endl << "\t\t]}"; - - return out.str(); - } - - static inline std::string generate_proof_json( - const proof_type &proof, - const typename assignment_table_type::public_input_container_type &public_inputs, - const std::array public_input_sizes - ) { - BOOST_LOG_TRIVIAL(info) << "Generate input..." << std::endl; - std::stringstream out; - out << "[" << std::endl; - - out << "\t{\"array\":[" << std::endl; - std::size_t cur = 0; - for (std::size_t i = 0; i < arithmetization_params::public_input_columns; i++) { - std::size_t max_non_zero = 0; - for (std::size_t j = 0; j < public_inputs[i].size(); j++) { - if (public_inputs[i][j] != 0) - max_non_zero = j; - } - if (max_non_zero >= public_input_sizes[i]) - throw std::runtime_error( - "Public input real size > expected size: " + std::to_string(max_non_zero + 1) + " > " - + std::to_string(public_input_sizes[i]) - ); - for (std::size_t j = 0; j < public_input_sizes[i]; j++) { - if (cur != 0) - out << "," << std::endl; - if (j >= public_inputs[i].size()) - out << "\t\t{\"field\": \"" << typename field_type::value_type(0) << "\"}"; - else - out << "\t\t{\"field\": \"" << public_inputs[i][j] << "\"}"; - cur++; - } - } - out << std::endl << "\t]}," << std::endl; - - out << "\t{\"struct\":[" << std::endl; - out << "\t\t{\"array\":[" << std::endl; - out << "\t\t\t" - << generate_commitment(proof.commitments.at(1) - ) //(nil::crypto3::zk::snark::VARIABLE_VALUES_BATCH) - << "," << std::endl; - out << "\t\t\t" - << generate_commitment(proof.commitments.at(2) - ) //(nil::crypto3::zk::snark::PERMUTATION_BATCH) - << "," << std::endl; - out << "\t\t\t" - << generate_commitment(proof.commitments.at(3) - ) // (nil::crypto3::zk::snark::QUOTIENT_BATCH) - ; - - if (proof.commitments.find(4) != proof.commitments.end()) { /*nil::crypto3::zk::snark::LOOKUP_BATCH*/ - out << "," << std::endl - << "\t\t\t" - << generate_commitment( - proof.commitments.at(4) // nil::crypto3::zk::snark::LOOKUP_BATCH) - ); - } - out << std::endl; - - out << "\t\t]}," << std::endl; - out << "\t\t{\"field\": \"" << proof.eval_proof.challenge << "\"}," << std::endl; - out << generate_eval_proof( - proof.eval_proof.eval_proof - ) << std::endl; - out << "\t]}" << std::endl; - - out << "]" << std::endl; - return out.str(); - } - }; - } // namespace proof_producer -} // namespace nil - -#endif // CRYPTO3_RECURSIVE_JSON_GENERATOR_HPP \ No newline at end of file diff --git a/bin/jsonizer/src/main.cpp b/bin/jsonizer/src/main.cpp deleted file mode 100644 index 01eb1c57..00000000 --- a/bin/jsonizer/src/main.cpp +++ /dev/null @@ -1,374 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2024 Nikita Kaskov -// Copyright (c) 2024 Elena Tatuzova -// -// 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. -//---------------------------------------------------------------------------// - -#include -#include -#include -#include -#include -#include - -#ifndef BOOST_FILESYSTEM_NO_DEPRECATED -#define BOOST_FILESYSTEM_NO_DEPRECATED -#endif -#ifndef BOOST_SYSTEM_NO_DEPRECATED -#define BOOST_SYSTEM_NO_DEPRECATED -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -bool read_buffer_from_file(std::ifstream &ifile, std::vector &v) { - char c; - char c1; - uint8_t b; - - ifile >> c; - if (c != '0') - return false; - ifile >> c; - if (c != 'x') - return false; - while (ifile) { - std::string str = ""; - ifile >> c >> c1; - if (!isxdigit(c) || !isxdigit(c1)) - return false; - str += c; - str += c1; - b = stoi(str, 0, 0x10); - v.push_back(b); - } - return true; -} - -template -int instanciated_main( - boost::filesystem::path proof_file_path, - boost::filesystem::path assignment_table_file_path, - boost::filesystem::path circuit_file_path, - std::size_t used_public_input_rows, - std::size_t used_shared_rows -) { - - using ConstraintSystemType = - nil::crypto3::zk::snark::plonk_constraint_system; - using TableDescriptionType = - nil::crypto3::zk::snark::plonk_table_description; - using Endianness = nil::marshalling::option::big_endian; - using TTypeBase = nil::marshalling::field_type; - using value_marshalling_type = - nil::crypto3::marshalling::types::plonk_constraint_system; - - using ColumnType = nil::crypto3::zk::snark::plonk_column; - using AssignmentTableType = - nil::crypto3::zk::snark::plonk_table; - using table_value_marshalling_type = - nil::crypto3::marshalling::types::plonk_assignment_table; - - using ColumnsRotationsType = std::array, ArithmetizationParams::total_columns>; - - TableDescriptionType table_description; - AssignmentTableType assignment_table; - - { - std::ifstream iassignment; - iassignment.open(assignment_table_file_path, std::ios_base::binary | std::ios_base::in); - if (!iassignment) { - BOOST_LOG_TRIVIAL(error) << "Cannot open " << assignment_table_file_path; - return false; - } - std::vector v; - iassignment.seekg(0, std::ios_base::end); - const auto fsize = iassignment.tellg(); - v.resize(fsize); - iassignment.seekg(0, std::ios_base::beg); - iassignment.read(reinterpret_cast(v.data()), fsize); - if (!iassignment) { - BOOST_LOG_TRIVIAL(error) << "Cannot parse input file " << assignment_table_file_path; - return false; - } - iassignment.close(); - table_value_marshalling_type marshalled_table_data; - auto read_iter = v.begin(); - auto status = marshalled_table_data.read(read_iter, v.size()); - std::tie(table_description.usable_rows_amount, assignment_table) = - nil::crypto3::marshalling::types::make_assignment_table( - marshalled_table_data - ); - table_description.rows_amount = assignment_table.rows_amount(); - } - - const std::size_t Lambda = 9; - using Hash = nil::crypto3::hashes::keccak_1600<256>; - using circuit_params = - nil::crypto3::zk::snark::placeholder_circuit_params; - - std::size_t table_rows_log = std::ceil(std::log2(table_description.rows_amount)); - using lpc_params_type = nil::crypto3::zk::commitments::list_polynomial_commitment_params; - using lpc_type = nil::crypto3::zk::commitments::list_polynomial_commitment; - using lpc_scheme_type = typename nil::crypto3::zk::commitments::lpc_commitment_scheme; - using placeholder_params = nil::crypto3::zk::snark::placeholder_params; - - std::array public_input_sizes; - for (std::size_t i = 0; i < ArithmetizationParams::public_input_columns; i++) { - public_input_sizes[i] = used_public_input_rows; - } - if (ArithmetizationParams::public_input_columns > 1 && used_shared_rows > 0) { - public_input_sizes[ArithmetizationParams::public_input_columns - 1] = used_shared_rows; - } - - using ProofType = nil::crypto3::zk::snark::placeholder_proof; - using proof_marshalling_type = nil::crypto3::marshalling::types::placeholder_proof; - - ProofType proof; - BOOST_LOG_TRIVIAL(info) << "Proof Type = " << typeid(ProofType).name() << std::endl; - { - std::ifstream iproof; - iproof.open(proof_file_path, std::ios_base::binary | std::ios_base::in); - if (!iproof) { - BOOST_LOG_TRIVIAL(error) << "Cannot open " << proof_file_path; - return false; - } - - std::vector v; - if (!read_buffer_from_file(iproof, v)) { - BOOST_LOG_TRIVIAL(error) << "Cannot parse input file " << proof_file_path << std::endl; - return false; - } - - proof_marshalling_type marshalled_proof_data; - auto read_iter = v.begin(); - auto status = marshalled_proof_data.read(read_iter, v.size()); - proof = nil::crypto3::marshalling::types::make_placeholder_proof(marshalled_proof_data); - } - - proof_file_path.replace_extension(".json"); - std::ofstream json_proof_file_stream; - json_proof_file_stream.open(proof_file_path); - json_proof_file_stream << nil::proof_producer::recursive_json_generator< - placeholder_params, - nil::crypto3::zk::snark::placeholder_proof>:: - generate_proof_json(proof, assignment_table.public_inputs(), public_input_sizes); - json_proof_file_stream.close(); - BOOST_LOG_TRIVIAL(info) << "JSON written" << std::endl; - return 0; -} - -int main(int argc, char *argv[]) { - - boost::program_options::options_description options_desc( - "zkLLVM proof2json recursive verifier input creation tool options" - ); - - // clang-format off - options_desc.add_options()("help,h", "Display help message") - ("version,v", "Display version") - ("proof,b", boost::program_options::value(), "Bytecode input file with the proof") - ("assignment-table,t", boost::program_options::value(), "Assignment table file - required for public preprocessed data generation.") - ("circuit,c", boost::program_options::value(), "Circuit file with the constraint system - required for public preprocessed data generation.") - ("used-public-input-rows,p", boost::program_options::value(), "Public input columns expected size") - ("used-shared-rows,s", boost::program_options::value(), "Shared column expected size") - ("log-level,l", boost::program_options::value(), "Log level (trace, debug, info, warning, error, fatal)") - ("elliptic-curve-type,e", boost::program_options::value(), "Native elliptic curve type (pallas, vesta, ed25519, bls12381)") - ; - // clang-format on - - boost::program_options::variables_map vm; - try { - boost::program_options::store( - boost::program_options::command_line_parser(argc, argv).options(options_desc).run(), - vm - ); - boost::program_options::notify(vm); - } catch (const boost::program_options::unknown_option &e) { - BOOST_LOG_TRIVIAL(error) << "Invalid command line argument: " << e.what(); - std::cout << options_desc << std::endl; - return 1; - } - - if (vm.count("help")) { - std::cout << options_desc << std::endl; - return 0; - } - - if (vm.count("version")) { -#ifdef PROOF2JSON_VERSION -#define xstr(s) str(s) -#define str(s) #s - std::cout << xstr(PROOF2JSON_VERSION) << std::endl; -#else - std::cout << "undefined" << std::endl; -#endif - return 0; - } - - boost::filesystem::path proof_file_path; - boost::filesystem::path assignment_table_file_path; - boost::filesystem::path circuit_file_path; - std::size_t used_public_input_rows; - std::size_t used_shared_rows; - std::string log_level; - - // We use Boost log trivial severity levels, these are string representations of their names - std::map log_options { - {"trace", boost::log::trivial::trace}, - {"debug", boost::log::trivial::debug}, - {"info", boost::log::trivial::info}, - {"warning", boost::log::trivial::warning}, - {"error", boost::log::trivial::error}, - {"fatal", boost::log::trivial::fatal}}; - - if (vm.count("log-level")) { - log_level = vm["log-level"].as(); - } else { - log_level = "info"; - } - - if (log_options.find(log_level) == log_options.end()) { - BOOST_LOG_TRIVIAL(error) << "Invalid command line argument -l (log level): " << log_level << std::endl; - std::cout << options_desc << std::endl; - return 1; - } - - boost::log::core::get()->set_filter(boost::log::trivial::severity >= log_options[log_level]); - - if (vm.count("proof")) { - proof_file_path = vm["proof"].as(); - } else { - proof_file_path = boost::filesystem::current_path() / "proof.bin"; - BOOST_LOG_TRIVIAL(debug) << "Proof file path not specified, using default: " << proof_file_path; - } - - if (vm.count("assignment-table")) { - assignment_table_file_path = boost::filesystem::path(vm["assignment-table"].as()); - BOOST_LOG_TRIVIAL(debug) << "Assignment table file path: " << assignment_table_file_path; - } else { - BOOST_LOG_TRIVIAL(error) << "Invalid command line argument - assignment table file name is not specified"; - std::cout << options_desc << std::endl; - return 1; - } - - if (vm.count("circuit")) { - circuit_file_path = boost::filesystem::path(vm["circuit"].as()); - BOOST_LOG_TRIVIAL(debug) << "Circuit file path: " << circuit_file_path; - } else { - BOOST_LOG_TRIVIAL(error) << "Invalid command line argument - circuit file name is not specified"; - std::cout << options_desc << std::endl; - return 1; - } - - if (vm.count("used-public-input-rows")) { - used_public_input_rows = vm["used-public-input-rows"].as(); - } else { - used_public_input_rows = 50; - } - - if (vm.count("used-shared-rows")) { - used_shared_rows = vm["used-shared-rows"].as(); - } else { - used_shared_rows = 0; - } - - std::string elliptic_curve; - - if (vm.count("elliptic-curve-type")) { - elliptic_curve = vm["elliptic-curve-type"].as(); - } else { - std::cerr << "Invalid command line argument - elliptic curve type is not specified" << std::endl; - std::cout << options_desc << std::endl; - return 1; - } - - std::map curve_options { - {"pallas", 0}, - {"vesta", 1}, - {"ed25519", 2}, - {"bls12381", 3}, - }; - - if (curve_options.find(elliptic_curve) == curve_options.end()) { - std::cerr << "Invalid command line argument -e (Native elliptic curve type): " << elliptic_curve << std::endl; - std::cout << options_desc << std::endl; - return 1; - } - - constexpr std::size_t ComponentConstantColumns = 5; - constexpr std::size_t LookupConstantColumns = 30; - constexpr std::size_t ComponentSelectorColumns = 30; - constexpr std::size_t LookupSelectorConstantColumns = 6; - - constexpr std::size_t WitnessColumns = 15; - constexpr std::size_t PublicInputColumns = 1; - constexpr std::size_t ConstantColumns = ComponentConstantColumns + LookupConstantColumns; - constexpr std::size_t SelectorColumns = ComponentSelectorColumns + LookupSelectorConstantColumns; - - using ArithmetizationParams = nil::crypto3::zk::snark:: - plonk_arithmetization_params; - - switch (curve_options[elliptic_curve]) { - case 0: { - using curve_type = nil::crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::base_field_type; - return instanciated_main( - proof_file_path, - assignment_table_file_path, - circuit_file_path, - used_public_input_rows, - used_shared_rows - ); - } - case 1: { - BOOST_LOG_TRIVIAL(error) << "vesta curve based circuits are not supported yet"; - return 1; - } - case 2: { - BOOST_LOG_TRIVIAL(error) << "ed25519 curve based circuits are not supported yet"; - return 1; - } - case 3: { - using curve_type = nil::crypto3::algebra::curves::bls12<381>; - using BlueprintFieldType = typename curve_type::base_field_type; - BOOST_LOG_TRIVIAL(error) << "bls12-381 curve based circuits proving is temporarily disabled"; - } - }; -} \ No newline at end of file diff --git a/bin/proof-generator/include/nil/proof-generator/arg_parser.hpp b/bin/proof-generator/include/nil/proof-generator/arg_parser.hpp index 44b703eb..603eaaf4 100644 --- a/bin/proof-generator/include/nil/proof-generator/arg_parser.hpp +++ b/bin/proof-generator/include/nil/proof-generator/arg_parser.hpp @@ -42,7 +42,7 @@ namespace nil { bool skip_verification = false; bool verification_only = false; CurvesVariant elliptic_curve_type = type_identity {}; - HashesVariant hash_type = type_identity> {}; + HashesVariant hash_type; columns_params columns = all_columns_params[0]; lambda_param lambda = all_lambda_params[0]; grind_param grind = all_grind_params[0]; @@ -54,4 +54,4 @@ namespace nil { } // namespace proof_generator } // namespace nil -#endif // PROOF_GENERATOR_ARG_PARSER_HPP +#endif // PROOF_GENERATOR_ARG_PARSER_HPP \ No newline at end of file diff --git a/bin/proof-generator/include/nil/proof-generator/arithmetization_params.hpp b/bin/proof-generator/include/nil/proof-generator/arithmetization_params.hpp index 2ae8b059..fa2efdf6 100644 --- a/bin/proof-generator/include/nil/proof-generator/arithmetization_params.hpp +++ b/bin/proof-generator/include/nil/proof-generator/arithmetization_params.hpp @@ -21,7 +21,11 @@ #include #include +#include + #include +#include +#include #include @@ -49,13 +53,17 @@ namespace nil { // Add more params as needed. }; - using CurveTypes = std::tuple; + using CurveTypes = std::tuple< + nil::crypto3::algebra::curves::pallas + // Add more curves as needed. + >; - using HashTypes = std::tuple - // Add more hashes as needed. - >; + using HashTypes = std::tuple< + nil::crypto3::hashes::keccak_1600<256>, + nil::crypto3::hashes::sha2<256>, + nil::crypto3::hashes::poseidon> + // Add more hashes as needed. + >; } // namespace proof_generator } // namespace nil diff --git a/bin/proof-generator/include/nil/proof-generator/prover.hpp b/bin/proof-generator/include/nil/proof-generator/prover.hpp index bda66940..ae9a8dc5 100644 --- a/bin/proof-generator/include/nil/proof-generator/prover.hpp +++ b/bin/proof-generator/include/nil/proof-generator/prover.hpp @@ -252,15 +252,13 @@ namespace nil { static constexpr std::size_t SelectorColumns = ComponentSelectorColumns + LookupSelectorColumns; // clang-format on - using ArithmetizationParams = nil::crypto3::zk::snark:: - plonk_arithmetization_params; using BlueprintField = typename CurveType::base_field_type; using LpcParams = nil::crypto3::zk::commitments:: list_polynomial_commitment_params; using Lpc = nil::crypto3::zk::commitments::list_polynomial_commitment; using LpcScheme = typename nil::crypto3::zk::commitments::lpc_commitment_scheme; using CircuitParams = - nil::crypto3::zk::snark::placeholder_circuit_params; + nil::crypto3::zk::snark::placeholder_circuit_params; using PlaceholderParams = nil::crypto3::zk::snark::placeholder_params; using Proof = nil::crypto3::zk::snark::placeholder_proof; using PublicPreprocessedData = typename nil::crypto3::zk::snark:: @@ -268,9 +266,9 @@ namespace nil { using PrivatePreprocessedData = typename nil::crypto3::zk::snark:: placeholder_private_preprocessor::preprocessed_data_type; using ConstraintSystem = - nil::crypto3::zk::snark::plonk_constraint_system; + nil::crypto3::zk::snark::plonk_constraint_system; using TableDescription = - nil::crypto3::zk::snark::plonk_table_description; + nil::crypto3::zk::snark::plonk_table_description; using Endianness = nil::marshalling::option::big_endian; using FriParams = typename Lpc::fri_type::params_type; @@ -280,6 +278,7 @@ namespace nil { nil::crypto3::zk::snark::placeholder_verifier::process( *public_preprocessed_data_, proof, + *table_description_, *constraint_system_, *lpc_scheme_ ); @@ -301,7 +300,7 @@ namespace nil { using Column = nil::crypto3::zk::snark::plonk_column; using AssignmentTable = - nil::crypto3::zk::snark::plonk_table; + nil::crypto3::zk::snark::plonk_table; { auto marshalled_value = detail::decode_marshalling_from_file(circuit_file_); @@ -317,21 +316,16 @@ namespace nil { using TableValueMarshalling = nil::crypto3::marshalling::types::plonk_assignment_table; - AssignmentTable assignment_table; - { - TableDescription table_description; - auto marshalled_table = - detail::decode_marshalling_from_file(assignment_table_file_); - if (!marshalled_table) { - return false; - } - std::tie(table_description.usable_rows_amount, assignment_table) = - nil::crypto3::marshalling::types::make_assignment_table( - *marshalled_table - ); - table_description.rows_amount = assignment_table.rows_amount(); - table_description_.emplace(table_description); + auto marshalled_table = + detail::decode_marshalling_from_file(assignment_table_file_); + if (!marshalled_table) { + return false; } + auto [table_description, assignment_table] = + nil::crypto3::marshalling::types::make_assignment_table( + *marshalled_table + ); + table_description_ = table_description; // Lambdas and grinding bits should be passed threw preprocessor directives std::size_t table_rows_log = std::ceil(std::log2(table_description_->rows_amount)); diff --git a/bin/proof-generator/src/arg_parser.cpp b/bin/proof-generator/src/arg_parser.cpp index 785e2d9f..2aeb8f47 100644 --- a/bin/proof-generator/src/arg_parser.cpp +++ b/bin/proof-generator/src/arg_parser.cpp @@ -273,13 +273,39 @@ namespace nil { GENERATE_READ_OPERATOR(CURVE_TYPES, CurvesVariant) #undef X -#define HASH_TYPES X(nil::crypto3::hashes::keccak_1600<256>, "keccak") -#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 +std::ostream& operator<<(std::ostream& strm, const HashesVariant& variant) { + strm << std::visit( + [&strm](auto&& arg) -> std::string { + using SelectedType = std::decay_t; + if constexpr (std::is_same_v>>) return "keccak"; + if constexpr (std::is_same_v>>) return "sha2"; + if constexpr (std::is_same_v>>>) return "sha2"; + strm.setstate(std::ios_base::failbit); + return ""; + }, + variant + ); + return strm; +} + +std::istream& operator>>(std::istream& strm, HashesVariant& variant) { + std::string str; + strm >> str; + if(str == "keccak") { + variant = std::variant_alternative_t<0, HashesVariant>(); + return strm; + } + if(str == "sha2") { + variant = std::variant_alternative_t<1, HashesVariant>(); + return strm; + } + if(str == "poseidon") { + variant = std::variant_alternative_t<2, HashesVariant>(); + return strm; + } + strm.setstate(std::ios_base::failbit); + return strm; +} } // namespace proof_generator } // namespace nil diff --git a/bin/proof-generator/src/main.cpp b/bin/proof-generator/src/main.cpp index f4c2925a..8a7ac8bc 100644 --- a/bin/proof-generator/src/main.cpp +++ b/bin/proof-generator/src/main.cpp @@ -74,6 +74,7 @@ int grind_param_wrapper(const prover_options& prover_options) { template int hash_wrapper(const prover_options& prover_options) { + std::cout << "Hash wrapper" << std::endl; int ret; auto run_prover_wrapper = [&prover_options, &ret]() { using HashType = typename HashTypeIdentity::type; @@ -122,6 +123,7 @@ int initial_wrapper(const prover_options& prover_options) { int main(int argc, char* argv[]) { std::optional prover_options = nil::proof_generator::parse_args(argc, argv); + std::cout << prover_options->circuit_file_path << std::endl; if (!prover_options) { // Action has already taken a place (help, version, etc.) return 0; diff --git a/libs/actor/zk b/libs/actor/zk index 3afc1535..ac41d579 160000 --- a/libs/actor/zk +++ b/libs/actor/zk @@ -1 +1 @@ -Subproject commit 3afc15359b3628ed1463b28ea6d298e6928a73df +Subproject commit ac41d57920fa06fd96f7c490b08f1bec2ca5e1de diff --git a/libs/crypto3 b/libs/crypto3 index f5332a79..7c95aae7 160000 --- a/libs/crypto3 +++ b/libs/crypto3 @@ -1 +1 @@ -Subproject commit f5332a7929ba9a645b911a4739ee6b86280f5d2f +Subproject commit 7c95aae747522869a2ef9c15a4068943e1aa0d32