Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

577 new fast tbl #611

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,11 @@ jobs:
always() && !cancelled() &&
(needs.handle-syncwith.result == 'success' || needs.handle-syncwith.result == 'skipped') &&
needs.build-and-test-linux.result == 'success'
uses: NilFoundation/proof-producer/.github/workflows/reusable-generate-proofs-linux.yml@ef8cd9152b4bec871e7efdc1d6b606e445bad274
uses: NilFoundation/proof-producer/.github/workflows/reusable-generate-proofs-linux.yml@f450e9219b0b77fa32ce27c5e9a9d9144cc577dd
with:
artifact-name: ${{ needs.build-and-test-linux.outputs.examples-artifact-name }}
# Update next line if you need new version of proof producer
proof-producer-ref: ef8cd9152b4bec871e7efdc1d6b606e445bad274
proof-producer-ref: f450e9219b0b77fa32ce27c5e9a9d9144cc577dd
refs: ${{ needs.handle-syncwith.outputs.prs-refs }}
targets: ${{ needs.build-and-test-linux.outputs.prover-targets }}

Expand Down
710 changes: 550 additions & 160 deletions bin/assigner/src/main.cpp

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions bin/recursive_gen/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#include <nil/blueprint/transpiler/public_input.hpp>
#include <nil/blueprint/transpiler/recursive_verifier_generator.hpp>

#include "../../table_packing.hpp"

template<typename StreamType>
std::optional<StreamType> open_file(const std::string& path, std::ios_base::openmode mode) {
StreamType file(path, mode);
Expand Down Expand Up @@ -106,6 +108,7 @@ std::optional<MarshallingType> decode_marshalling_from_file(
return marshalled_data;
}


template<typename BlueprintFieldType>
struct ParametersPolicy {
constexpr static const std::size_t WitnessColumns = WITNESS_COLUMNS;
Expand Down Expand Up @@ -414,13 +417,21 @@ int curve_dependent_main(
public_input_sizes = (*constraint_system).public_input_sizes();

if( vm.count("assignment-table") ){
auto marshalled_value = decode_marshalling_from_file<assignment_table_marshalling_type>(assignment_table_file_name);
if (!marshalled_value) {
return false;
}
auto [description, table] = nil::crypto3::marshalling::types::make_assignment_table<Endianness, AssignmentTableType>(
*marshalled_value
);

AssignmentTableType table;
zk::snark::plonk_table_description<BlueprintFieldType> description (
WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns);

assignment_table_marshalling_type marshalled_table_data =
extract_table_from_binary_file<assignment_table_marshalling_type>
(assignment_table_file_name);

std::tie(description, table) =
nil::crypto3::marshalling::types::make_assignment_table<Endianness, AssignmentTableType>(
marshalled_table_data
);


assignment_table.emplace(table);
desc.emplace(description);
}
Expand Down
72 changes: 72 additions & 0 deletions bin/table_packing.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
std::string add_filename_prefix(
const std::string& prefix,
const std::string& file_name
) {
std::filesystem::path path(file_name);
std::filesystem::path parent_path = path.parent_path();
std::filesystem::path filename = path.filename();

std::string new_filename = prefix + filename.string();
std::filesystem::path new_path = parent_path / new_filename;

return new_path.string();
}


bool append_binary_file_content_to_vector (
std::vector<std::uint8_t>& result_vector,
const std::string& prefix,
const std::string& assignment_table_file_name

) {
std::ifstream icolumn;
icolumn.open(add_filename_prefix(prefix, assignment_table_file_name), std::ios_base::binary | std::ios_base::in);
if (!icolumn) {
std::cout << "Cannot open " << add_filename_prefix(prefix, assignment_table_file_name) << std::endl;
return false;
}
icolumn.seekg(0, std::ios_base::end);
const auto input_size = icolumn.tellg();
std::size_t old_size = result_vector.size();
result_vector.resize(old_size + input_size);
icolumn.seekg(0, std::ios_base::beg);
icolumn.read(reinterpret_cast<char*>(result_vector.data() + old_size), input_size);
icolumn.close();
return true;
}

template<typename marshalling_type>
void unmarshall_from_vector (
const std::vector<std::uint8_t>& v,
marshalling_type& marhsalling_data
) {
auto vect_iterator = v.begin();
auto marshalling_status = marhsalling_data.read(vect_iterator, v.size());
ASSERT(marshalling_status == nil::marshalling::status_type::success);
}

template<typename marshalling_type>
void extract_from_binary_file(
marshalling_type& marshalling_file,
const std::string& prefix,
const std::string& file_name
) {
std::vector<std::uint8_t> file_vector = {};
ASSERT(append_binary_file_content_to_vector(file_vector, prefix, file_name));
unmarshall_from_vector<marshalling_type> (file_vector, marshalling_file);
}

template<typename marshalling_type>
marshalling_type extract_table_from_binary_file(
const std::string& file_name
) {
std::vector<std::uint8_t> file_vector = {};
ASSERT(append_binary_file_content_to_vector(file_vector, "header_", file_name));
ASSERT(append_binary_file_content_to_vector(file_vector, "witness_", file_name));
ASSERT(append_binary_file_content_to_vector(file_vector, "pub_inp_", file_name));
ASSERT(append_binary_file_content_to_vector(file_vector, "constants_", file_name));
ASSERT(append_binary_file_content_to_vector(file_vector, "selectors_", file_name));
marshalling_type marshalling_data;
unmarshall_from_vector<marshalling_type> (file_vector, marshalling_data);
return marshalling_data;
}
28 changes: 8 additions & 20 deletions bin/transpiler/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include <nil/blueprint/transpiler/evm_verifier_gen.hpp>
#include <nil/blueprint/transpiler/public_input.hpp>

#include "../../table_packing.hpp"

template<typename BlueprintFieldType, typename ConstraintSystemType, typename ColumnsRotationsType>
void print_sol_files(
zk::snark::plonk_table_description<BlueprintFieldType> desc,
Expand Down Expand Up @@ -242,6 +244,7 @@ int main(int argc, char *argv[]) {

}


template<typename BlueprintFieldType, bool is_multi_prover>
int curve_dependent_main(
boost::program_options::options_description options_desc,
Expand Down Expand Up @@ -361,26 +364,11 @@ int curve_dependent_main(

AssignmentTableType assignment_table;
{
std::ifstream iassignment;
iassignment.open(assignment_table_file_name, std::ios_base::binary | std::ios_base::in);
if (!iassignment) {
std::cout << "Cannot open " << assignment_table_file_name << std::endl;
return 1;
}
std::vector<std::uint8_t> 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<char*>(v.data()), fsize);
if (!iassignment) {
std::cout << "Cannot parse input file " << assignment_table_file_name << std::endl;
return 1;
}
iassignment.close();
table_value_marshalling_type marshalled_table_data;
auto read_iter = v.begin();
auto status = marshalled_table_data.read(read_iter, v.size());

table_value_marshalling_type marshalled_table_data =
extract_table_from_binary_file<table_value_marshalling_type>
(assignment_table_file_name);

std::tie(desc, assignment_table) =
nil::crypto3::marshalling::types::make_assignment_table<Endianness, AssignmentTableType>(
marshalled_table_data
Expand Down
50 changes: 44 additions & 6 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function(assign_ir)
COMMAND $<TARGET_FILE:assigner>
-b ${binary_name}
-c circuit_${target}.crct
-j table_pieces_${target}.json
-t assignment_${target}.tbl
-e ${curve_type}
--generate-type circuit
${max_num_provers_flag} ${max_num_provers_amount}
Expand All @@ -48,6 +50,7 @@ function(assign_ir)
-i ${INPUTS_DIR}/${input}
${minus_p} ${private_input_string}
-c circuit_${target}.crct
-j table_pieces_${target}.json
-t assignment_${target}.tbl -e ${curve_type} --check
--generate-type circuit-assignment
${max_num_provers_flag} ${max_num_provers_amount}
Expand All @@ -62,6 +65,7 @@ function(assign_ir)
-i ${INPUTS_DIR}/${input}
${minus_p} ${private_input_string}
-c circuit_${target}.crct
-j table_pieces_${target}.json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need table_pieces for generate both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it includes generate_crct, and generate_crct generates table_pieces

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should allow skip table_pieces command parameter if used generate_both

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that idea, generate_both includes circuit and table generation, first one generates table_pieces. One would expect that circuit generation generates table pieces regardless of whether circuit and table are generated simultaneously or separately. contradicts the principle of least astonishment. And maybe there shouldn't be such thing as table_pieces_name at all. Just use table_pieces_circuit_file_name.crct. What do you think?

-t assignment_${target}.tbl -e ${curve_type}
--generate-type circuit-assignment
${max_num_provers_flag} ${max_num_provers_amount}
Expand All @@ -83,7 +87,35 @@ function(assign_ir)
COMMAND_EXPAND_LISTS
VERBATIM)

add_custom_target(${target}_generate_tbl_no_check
add_custom_target(${target}_generate_tbl_fast
COMMAND $<TARGET_FILE:assigner>
-b ${binary_name}
-i ${INPUTS_DIR}/${input}
${minus_p} ${private_input_string}
-j table_pieces_${target}.json
-t assignment_${target}.tbl -e ${curve_type}
--generate-type assignment-fast
${max_num_provers_flag} ${max_num_provers_amount}
${arithmetization_flag} ${arithmetization_amount}
DEPENDS ${target} ${INPUTS_DIR}/${input} $<TARGET_FILE:assigner>
COMMAND_EXPAND_LISTS
VERBATIM)

add_custom_target(${target}_generate_tbl_fast_depends_on_crct
COMMAND $<TARGET_FILE:assigner>
-b ${binary_name}
-i ${INPUTS_DIR}/${input}
${minus_p} ${private_input_string}
-j table_pieces_${target}.json
-t assignment_${target}.tbl -e ${curve_type}
--generate-type assignment-fast
${max_num_provers_flag} ${max_num_provers_amount}
${arithmetization_flag} ${arithmetization_amount}
DEPENDS ${target} ${INPUTS_DIR}/${input} ${target}_generate_crct $<TARGET_FILE:assigner>
COMMAND_EXPAND_LISTS
VERBATIM)

add_custom_target(${target}_generate_tbl_no_check
COMMAND $<TARGET_FILE:assigner>
-b ${binary_name}
-i ${INPUTS_DIR}/${input}
Expand All @@ -99,6 +131,7 @@ function(assign_ir)
add_custom_target(${target}_estimate_size
COMMAND $<TARGET_FILE:assigner>
-b ${binary_name}
-j table_pieces_${target}.json
-e ${curve_type}
--generate-type size_estimation
${max_num_provers_flag} ${max_num_provers_amount}
Expand All @@ -120,15 +153,15 @@ function(assign_ir)
VERBATIM)
endfunction()

function(gen_proof target curve_type provers_amount)
function(gen_proof target curve_type provers_amount tbl_gen_speed)
if(provers_amount EQUAL 0)
gen_single_proof(${target} ${curve_type} 0)
gen_single_proof(${target} ${curve_type} 0 ${tbl_gen_speed})
else()
add_custom_target(${target}_prove)

foreach(prover_num RANGE 1 ${provers_amount})
math(EXPR prover_num_minus_1 "${prover_num} - 1")
gen_single_proof(${target} ${curve_type} ${prover_num})
gen_single_proof(${target} ${curve_type} ${prover_num} ${tbl_gen_speed})
add_dependencies(${target}_prove ${target}_prove${prover_num_minus_1})
endforeach()

Expand All @@ -143,22 +176,27 @@ function(gen_proof target curve_type provers_amount)
endfunction()


function(gen_single_proof target curve_type provers_amount)
function(gen_single_proof target curve_type provers_amount tbl_gen_speed)
if(NOT provers_amount EQUAL 0)
set(multi_prover_flag --multi-prover)
math(EXPR prover_num "${provers_amount} - 1")
else()
set(prover_num "")
endif()

set (depends_tbl ${target}_generate_both)
if (tbl_gen_speed STREQUAL "fast")
set (depends_tbl ${target}_generate_tbl_fast_depends_on_crct)
endif()

add_custom_target(${target}_prove${prover_num}
COMMAND $<TARGET_FILE:transpiler> -m gen-test-proof
-c circuit_${target}.crct${prover_num}
-t assignment_${target}.tbl${prover_num}
-o transpiler_output_${target}${prover_num}
-e ${curve_type}
${multi_prover_flag}
DEPENDS ${target}_generate_crct ${target}_generate_tbl $<TARGET_FILE:transpiler>
DEPENDS ${depends_tbl} $<TARGET_FILE:transpiler>
COMMAND_EXPAND_LISTS
VERBATIM)
endfunction()
Expand Down
Loading
Loading