Skip to content

Commit 724ea57

Browse files
committed
add table_pieces handling into assigner main
1 parent b6fa261 commit 724ea57

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

bin/assigner/src/main.cpp

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ int curve_dependent_main(std::string bytecode_file_name,
487487
std::string private_input_file_name,
488488
std::string assignment_table_file_name,
489489
std::string circuit_file_name,
490+
std::string table_pieces_file_name,
490491
std::string processed_public_input_file_name,
491492
long stack_size,
492493
bool check_validity,
@@ -534,6 +535,27 @@ int curve_dependent_main(std::string bytecode_file_name,
534535
return 1;
535536
}
536537

538+
using var = crypto3::zk::snark::plonk_variable<typename AssignmentTableType::field_type::value_type>;
539+
std::vector<table_piece<var>> table_pieces = {}; // we create table pieces in any case and pass into assigner by reference
540+
if (gen_mode.has_fast_tbl()) { // if we are generating tables in a fast way then need to parse table_pieces from file
541+
542+
std::ifstream inp_json(table_pieces_file_name);
543+
544+
if (!inp_json.is_open()) {
545+
std::cerr << "unable to open table_pieces file" << std::endl;
546+
return 1;
547+
}
548+
549+
std::string str((std::istreambuf_iterator<char>(inp_json)),
550+
std::istreambuf_iterator<char>());
551+
auto parsed = boost::json::parse(str);
552+
boost::json::array arr = parsed.as_array();
553+
554+
for (const auto& item : arr) {
555+
table_pieces.emplace_back(item.as_object());
556+
}
557+
}
558+
537559
auto assigner_instance_creation_start = std::chrono::high_resolution_clock::now();
538560
nil::blueprint::assigner<BlueprintFieldType> assigner_instance(
539561
desc,
@@ -563,7 +585,13 @@ int curve_dependent_main(std::string bytecode_file_name,
563585
BOOST_LOG_TRIVIAL(debug) << "parse_ir_file_duration: " << parse_ir_file_duration.count() << "ms";
564586

565587
auto parser_evaluation_start = std::chrono::high_resolution_clock::now();
566-
if (!assigner_instance.evaluate(public_input_json_value.as_array(), private_input_json_value.as_array())) {
588+
if (
589+
!assigner_instance.evaluate(
590+
public_input_json_value.as_array(),
591+
private_input_json_value.as_array(),
592+
table_pieces
593+
)
594+
) {
567595
return 1;
568596
}
569597
auto parser_evaluation_duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - parser_evaluation_start);
@@ -575,6 +603,20 @@ int curve_dependent_main(std::string bytecode_file_name,
575603
return 0;
576604
}
577605

606+
if (gen_mode.has_circuit()) { // if we are generation circuit then we are generation table pieces in the same time. Need to write itno file
607+
boost::json::array pieces_json;
608+
// for (const auto& piece : nil::blueprint::table_pieces) {
609+
for (const auto& piece : table_pieces) {
610+
pieces_json.push_back(piece.to_json());
611+
}
612+
613+
std::string serialized = boost::json::serialize(pieces_json);
614+
615+
std::ofstream file(table_pieces_file_name);
616+
file << serialized;
617+
file.close();
618+
}
619+
578620
auto pack_lookup_start = std::chrono::high_resolution_clock::now();
579621
// pack lookup tables
580622
if (assigner_instance.circuits[0].get_reserved_tables().size() > 0) {
@@ -744,6 +786,7 @@ int main(int argc, char *argv[]) {
744786
("private-input,p", boost::program_options::value<std::string>(), "Private input file")
745787
("assignment-table,t", boost::program_options::value<std::string>(), "Assignment table output file")
746788
("circuit,c", boost::program_options::value<std::string>(), "Circuit output file")
789+
("table-pieces,j", boost::program_options::value<std::string>(), "Table pieces json file")
747790
("input-column", boost::program_options::value<std::string>(), "Output file for public input column")
748791
("elliptic-curve-type,e", boost::program_options::value<std::string>(), "Native elliptic curve type (pallas, vesta, ed25519, bls12381)")
749792
("stack-size,s", boost::program_options::value<long>(), "Stack size in bytes")
@@ -752,7 +795,7 @@ int main(int argc, char *argv[]) {
752795
("print_circuit_output", "deprecated, use \"-f\" instead")
753796
("print-circuit-output-format,f", boost::program_options::value<std::string>(), "print output of the circuit (dec, hex)")
754797
("policy", boost::program_options::value<std::string>(), "Policy for creating circuits. Possible values: default")
755-
("generate-type", boost::program_options::value<std::string>(), "Define generated output. Possible values: circuit, assignment, circuit-assignment, public-input-column, size_estimation(does not generate anything, just evaluates circuit size). Default value is circuit-assignment")
798+
("generate-type", boost::program_options::value<std::string>(), "Define generated output. Possible values: circuit, assignment, assignment-fast, circuit-assignment, public-input-column, size_estimation(does not generate anything, just evaluates circuit size). Default value is circuit-assignment")
756799
("max-num-provers", boost::program_options::value<int>(), "Maximum number of provers. Possible values >= 1")
757800
("max-lookup-rows", boost::program_options::value<int>(), "Maximum number of lookup rows")
758801
("target-prover", boost::program_options::value<int>(), "Assignment table and circuit will be generated only for defined prover. Possible values [0, max-num-provers)")
@@ -794,6 +837,7 @@ int main(int argc, char *argv[]) {
794837
std::string private_input_file_name;
795838
std::string assignment_table_file_name;
796839
std::string circuit_file_name;
840+
std::string table_pieces_file_name;
797841
std::string processed_public_input_file_name;
798842
std::string elliptic_curve;
799843
nil::blueprint::print_format circuit_output_print_format;
@@ -815,8 +859,10 @@ int main(int argc, char *argv[]) {
815859
gen_mode = nil::blueprint::generation_mode::circuit();
816860
} else if (generate_type == "assignment") {
817861
gen_mode = nil::blueprint::generation_mode::assignments();
862+
} else if (generate_type == "assignment-fast") {
863+
gen_mode = nil::blueprint::generation_mode::fast_tbl();
818864
} else if (generate_type == "size_estimation") {
819-
gen_mode = nil::blueprint::generation_mode::size_estimation();
865+
gen_mode = nil::blueprint::generation_mode::size_estimation() | nil::blueprint::generation_mode::circuit();
820866
} else if (generate_type == "public-input-column") {
821867
gen_mode = nil::blueprint::generation_mode::public_input_column();
822868
} else if (generate_type != "circuit-assignment") {
@@ -826,7 +872,7 @@ int main(int argc, char *argv[]) {
826872
}
827873
}
828874

829-
if (!vm.count("public-input") && !vm.count("private-input") && gen_mode.has_assignments()) {
875+
if (!vm.count("public-input") && !vm.count("private-input") && !gen_mode.has_size_estimation()) {
830876
std::cerr << "Both public and private input file names are not specified" << std::endl;
831877
std::cout << options_desc << std::endl;
832878
return 1;
@@ -853,22 +899,34 @@ int main(int argc, char *argv[]) {
853899
if (vm.count("assignment-table")) {
854900
assignment_table_file_name = vm["assignment-table"].as<std::string>();
855901
} else {
856-
std::cerr << "Invalid command line argument - assignment table file name is not specified" << std::endl;
857-
std::cout << options_desc << std::endl;
858-
return 1;
902+
if (!gen_mode.has_size_estimation()) {
903+
std::cerr << "Invalid command line argument - assignment table file name is not specified" << std::endl;
904+
std::cout << options_desc << std::endl;
905+
return 1;
906+
}
859907
}
860908
}
861909

862910
if (gen_mode.has_circuit()) {
863911
if (vm.count("circuit")) {
864912
circuit_file_name = vm["circuit"].as<std::string>();
865913
} else {
866-
std::cerr << "Invalid command line argument - circuit file name is not specified" << std::endl;
867-
std::cout << options_desc << std::endl;
868-
return 1;
914+
if (!gen_mode.has_size_estimation()) {
915+
std::cerr << "Invalid command line argument - circuit file name is not specified" << std::endl;
916+
std::cout << options_desc << std::endl;
917+
return 1;
918+
}
869919
}
870920
}
871921

922+
if (vm.count("table-pieces")) {
923+
table_pieces_file_name = vm["table-pieces"].as<std::string>();
924+
} else {
925+
std::cerr << "Invalid command line argument - table-pieces file name is not specified" << std::endl;
926+
std::cout << options_desc << std::endl;
927+
return 1;
928+
}
929+
872930
if (vm.count("elliptic-curve-type")) {
873931
elliptic_curve = vm["elliptic-curve-type"].as<std::string>();
874932
} else {
@@ -1010,6 +1068,7 @@ int main(int argc, char *argv[]) {
10101068
private_input_file_name,
10111069
assignment_table_file_name,
10121070
circuit_file_name,
1071+
table_pieces_file_name,
10131072
processed_public_input_file_name,
10141073
stack_size,
10151074
vm.count("check"),
@@ -1039,6 +1098,7 @@ int main(int argc, char *argv[]) {
10391098
private_input_file_name,
10401099
assignment_table_file_name,
10411100
circuit_file_name,
1101+
table_pieces_file_name,
10421102
processed_public_input_file_name,
10431103
stack_size,
10441104
vm.count("check"),

0 commit comments

Comments
 (0)