Skip to content

Commit

Permalink
Recursive tests updated #65
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Jan 30, 2024
1 parent 7ffffb1 commit 03a25b1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 156 deletions.
4 changes: 0 additions & 4 deletions include/nil/blueprint/transpiler/evm_verifier_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,8 @@ namespace nil {
gate_argument_str << "\t\tuint256 gate;" << std::endl;
gate_argument_str << print_constraint_series(it, constraints.end());
} else {
std::cout << "I am here!" << std::endl;
auto it = constraints.begin();
while (it != constraints.end()) {
std::cout << "Gates modules count" << gate_modules_count << std::endl;
std::string code = print_constraint_series(it, constraints.end());

std::string result = modular_external_gate_library_template;
Expand All @@ -619,7 +617,6 @@ namespace nil {
++gate_modules_count;
}
}
std::cout << "Finished" << std::endl;

if (_term_powers.size() > 0) {
std::stringstream power_functions;
Expand Down Expand Up @@ -881,7 +878,6 @@ namespace nil {

void print(){
std::filesystem::create_directory(_folder_name);
std::cout << "Generating verifier " << _test_name << std::endl;

std::string gate_argument = print_gate_argument();
std::string lookup_argument = print_lookup_argument();
Expand Down
9 changes: 0 additions & 9 deletions include/nil/blueprint/transpiler/lpc_scheme_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ namespace nil {
"evm" // Generator mode
);

for( std::size_t i = 0; i < z_points_indices.size(); i ++){
std::cout << "[" << i <<"]" << "=>" << z_points_indices[i] << " ";
}
std::cout << std::endl;

for( std::size_t i = 0; i < poly_ids.size(); i++ ){
std::cout << "point " << i << " has " << poly_ids[i].size() <<" polys." << std::endl;
}

std::stringstream points_initializer;
std::size_t i = 0;

Expand Down
109 changes: 29 additions & 80 deletions include/nil/blueprint/transpiler/recursive_verifier_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ namespace nil {
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) << "\"}";
//std::cout << "batch " << k << " poly " << i << " value " << eval_proof.z.get(k, i, j) << std::endl;
sum++;
}
}
Expand Down Expand Up @@ -349,29 +348,30 @@ namespace nil {
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 + 1 > public_input_sizes[i] ) {
std::cout << "Public input size is larger than reserved. Real size = " << max_non_zero + 1 << " reserved = " << public_input_sizes[i] << std::endl;
exit(1);
}
BOOST_ASSERT(max_non_zero <= 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++;
if(arithmetization_params::public_input_columns != 0){
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 + 1 > public_input_sizes[i] ) {
std::cout << "Public input size is larger than reserved. Real size = " << max_non_zero + 1 << " reserved = " << public_input_sizes[i] << std::endl;
exit(1);
}
BOOST_ASSERT(max_non_zero <= 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 << std::endl << "\t]}," << std::endl;
std::cout << "Public input: " << out.str() << std::endl;

out << "\t{\"array\":[" << std::endl;
out << "\t\t" << generate_hash<typename PlaceholderParams::transcript_hash_type>(
Expand Down Expand Up @@ -743,7 +743,6 @@ namespace nil {
}
}
}
std::cout << "Printing" << std::endl;

std::stringstream lookup_shifted_options_list;
cur = 0;
Expand All @@ -770,40 +769,6 @@ namespace nil {
use_lookups?constraint_system.sorted_lookup_columns_number():0,
"recursive" // Generator mode
);
std::cout << "Singles" << std::endl;
for( const auto &single: singles_strs){
std::cout << single << std::endl;
}
std::cout << "Singles_map" << std::endl;
for( const auto &[single, ind]: singles_map){
std::cout << single << " => " << ind << std::endl;
}
std::cout << "Poly ids" << std::endl;
for( std::size_t i = 0; i < poly_ids.size(); i++){
std::cout << "Poly " << i << std::endl;
for( std::size_t j = 0; j < poly_ids[i].size(); j++){
std::cout << poly_ids[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << "Z points indices" << std::endl;
for( std::size_t i = 0; i < z_points_indices.size(); i++){
std::cout << "[" << i << "]=>" << z_points_indices[i] << " ";
}
std::cout << std::endl;

std::vector<std::vector<std::string>> unique_points;
std::vector<std::size_t> point_ids;
std::map<std::string, std::size_t> singles;
std::tie(unique_points, point_ids, singles) = calculate_unique_point_sets(
common_data, permutation_size, use_lookups, quotient_polys, use_lookups?constraint_system.sorted_lookup_columns_number():0
);

std::string point_inds_str = "";
for(std::size_t i = 0; i < point_ids.size(); i++){
if( i != 0) point_inds_str += ", ";
point_inds_str += to_string(point_ids[i]);
}

std::string singles_str = "";
for(const auto &[k, v]: singles_map){
Expand All @@ -822,29 +787,14 @@ namespace nil {

std::stringstream prepare_U_V_str;
prepare_U_V_str << "\tpallas::base_field_type::value_type theta_acc = pallas::base_field_type::value_type(1);\n\n";
for(std::size_t i = 0; i < unique_points.size();i++){
for(std::size_t i = 0; i < singles_strs.size();i++){
for(std::size_t j = 0; j <z_points_indices.size(); j++){
if( z_points_indices[j] == i)
prepare_U_V_str << "\tU[" + to_string(i) << "] += theta_acc * proof.z[" << j << "]; theta_acc *= challenges.lpc_theta;\n";
}
prepare_U_V_str << "\n";
}

std::stringstream compute_combined_y;
for(std::size_t i = 0; i < point_ids.size(); i++){
/*y[0] = y[0] * challenges.lpc_theta;
y[0] = y[0] + proof.initial_proof_values[initial_proof_ind] * V_evals[point_ids[k]][0];
y[1] = y[1] * challenges.lpc_theta;
y[1] = y[1] + proof.initial_proof_values[initial_proof_ind+1] * V_evals[point_ids[k]][1];*/

// compute_combined_y << "\t\ty[0] = y[0]*challenges.lpc_theta;" << std::endl;
// compute_combined_y << "\t\ty[0] = y[0] + proof.initial_proof_values[initial_proof_ind] * V_evals[" << point_ids[i]<< "][0];" << std::endl;
// compute_combined_y << "\t\tinitial_proof_ind++;" << std::endl;
// compute_combined_y << "\t\ty[1] = y[1]*challenges.lpc_theta;" << std::endl;
// compute_combined_y << "\t\ty[1] = y[1] + proof.initial_proof_values[initial_proof_ind] * V_evals[" << point_ids[i]<< "][1];" << std::endl;
// compute_combined_y << "\t\tinitial_proof_ind++;" << std::endl;
}

std::string public_input_sizes_str = "";
std::size_t full_public_input_size = 0;
for(std::size_t i = 0; i < public_input_sizes.size(); i++){
Expand All @@ -864,9 +814,8 @@ namespace nil {
}
lpc_y_computation << "\t\tQ0 -= U["<< i << "];" << std::endl;
lpc_y_computation << "\t\tQ1 -= U["<< i << "];" << std::endl;
lpc_y_computation << "\t\tQ0 /= (res[0][0] - challenges." << singles_strs[i] <<");" << std::endl;
lpc_y_computation << "\t\tQ1 /= (res[0][1] - challenges." << singles_strs[i] <<");" << std::endl;
std::cout << "single " << singles_strs[i] << std::endl;
lpc_y_computation << "\t\tQ0 /= (res[0][0] - singles[" << i << "]);" << std::endl;
lpc_y_computation << "\t\tQ1 /= (res[0][1] - singles[" << i << "]);" << std::endl;
lpc_y_computation << "\t\ty[0] += Q0;" << std::endl;
lpc_y_computation << "\t\ty[1] += Q1;" << std::endl;
}
Expand Down Expand Up @@ -920,12 +869,10 @@ namespace nil {
reps["$D0_OMEGA$"] = "pallas::base_field_type::value_type(0x" + to_hex_string(fri_params.D[0]->get_domain_element(1)) + "_cppui255)";
reps["$OMEGA$"] = "pallas::base_field_type::value_type(0x" + to_hex_string(common_data.basic_domain->get_domain_element(1)) + "_cppui255)";
reps["$FRI_ROUNDS$"] = to_string(fri_params.r);
reps["$UNIQUE_POINTS$"] = to_string(unique_points.size());
reps["$POINTS_IDS$"] = point_inds_str;
reps["$SINGLES_AMOUNT$"] = to_string(singles.size());
reps["$UNIQUE_POINTS$"] = to_string(singles_strs.size());
reps["$SINGLES_AMOUNT$"] = to_string(singles_strs.size());
reps["$SINGLES_COMPUTATION$"] = singles_str;
reps["$PREPARE_U_AND_V$"] = prepare_U_V_str.str();
reps["$COMPUTE_COMBINED_Y$"] = compute_combined_y.str();
reps["$SORTED_COLUMNS$"] = to_string(constraint_system.sorted_lookup_columns_number());
reps["$SORTED_ALPHAS$"] = to_string(use_lookups? constraint_system.sorted_lookup_columns_number() - 1: 1);
reps["$LOOKUP_TABLE_AMOUNT$"] = to_string(constraint_system.lookup_tables().size());
Expand All @@ -951,6 +898,8 @@ namespace nil {
reps["$FULL_PUBLIC_INPUT_SIZE$"] = to_string(full_public_input_size);
reps["$LPC_POLY_IDS_CONSTANT_ARRAYS$"] = lpc_poly_ids_const_arrays;
reps["$LPC_Y_COMPUTATION$"] = lpc_y_computation.str();
reps["$PUBLIC_INPUT_CHECK$"] = arithmetization_params::public_input_columns == 0 ? "" :public_input_check_str;
reps["$PUBLIC_INPUT_INPUT$"] = arithmetization_params::public_input_columns == 0 ? "" : public_input_input_str;

result = replace_all(result, reps);
return result;
Expand Down
54 changes: 23 additions & 31 deletions include/nil/blueprint/transpiler/templates/recursive_verifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace nil {
namespace blueprint {
std::string lookup_vars = R"(
#ifdef __USE_LOOKUPS__
const size_t lookup_table_amount = $LOOKUP_TABLE_AMOUNT$;
const size_t lookup_gate_amount = $LOOKUP_GATE_AMOUNT$;
constexpr std::array<std::size_t, lookup_table_amount> lookup_options_amount_list = {$LOOKUP_OPTIONS_AMOUNT_LIST$};
Expand Down Expand Up @@ -35,22 +34,18 @@ constexpr std::size_t input_size_shifted_lookup_table_lookup_options = lookup_ta
constexpr std::size_t input_size_sorted = m_parameter * 3 - 1;
$LPC_POLY_IDS_CONSTANT_ARRAYS$
#endif
)";

std::string lookup_expressions = R"(
#ifdef __USE_LOOKUPS__
std::array<pallas::base_field_type::value_type, lookup_expressions_amount> calculate_lookup_expressions(std::array<pallas::base_field_type::value_type, points_num> z){
std::array<pallas::base_field_type::value_type, lookup_expressions_amount> expressions;
$LOOKUP_EXPRESSIONS_BODY$
return expressions;
}
#endif
)";

std::string lookup_code = R"(
#ifdef __USE_LOOKUPS__
{
std::array<typename pallas::base_field_type::value_type, input_size_alphas> alphas = challenges.lookup_alphas;
std::array<typename pallas::base_field_type::value_type, input_size_lookup_gate_selectors> lookup_gate_selectors;
Expand Down Expand Up @@ -149,9 +144,24 @@ std::array<pallas::base_field_type::value_type, lookup_expressions_amount> calcu
F[5] = lookup_argument[2];
F[6] = lookup_argument[3];
}
#endif
)";

std::string public_input_check_str = R"(
//Check public input
std::size_t cur = 0;
for( std::size_t i = 0; i < public_input_amount; i++){
pallas::base_field_type::value_type Omega(1);
pallas::base_field_type::value_type result(0);
for( std::size_t j = 0; j < public_input_sizes[i]; j++){
result += public_input[cur] * Omega / (challenges.xi - Omega);
Omega *= omega;
cur++;
}
__builtin_assigner_exit_check(rows_amount * proof.z[zero_indices[witness_amount + i]] == precomputed_values.Z_at_xi * result);
}
)";
std::string public_input_input_str = "\tstd::array<pallas::base_field_type::value_type, full_public_input_size> public_input,\n";

std::string recursive_verifier_template = R"(
#include <nil/crypto3/hash/algorithm/hash.hpp>
#include <nil/crypto3/hash/poseidon.hpp>
Expand Down Expand Up @@ -204,7 +214,6 @@ const pallas::base_field_type::value_type omega = $OMEGA$;
const size_t fri_rounds = $FRI_ROUNDS$;
const std::array<int, gates_amount> gates_sizes = {$GATES_SIZES$};
const size_t unique_points = $UNIQUE_POINTS$;
const std::array<int, poly_num> point_ids = {$POINTS_IDS$};
const size_t singles_amount = $SINGLES_AMOUNT$;
const std::array<std::size_t, batches_num> batches_amount_list = {$BATCHES_AMOUNT_LIST$};
Expand Down Expand Up @@ -331,17 +340,11 @@ pallas::base_field_type::value_type pow9(pallas::base_field_type::value_type x){
}
pallas::base_field_type::value_type pow(pallas::base_field_type::value_type x, size_t p){
pallas::base_field_type::value_type result = 1;
std::size_t mask = 1;
while(mask < p){mask = mask * 2;} // 8
while(mask > 1){
result = result * result;
mask = mask / 2;
if( p >= mask ){
result = result * x;
p = p - mask;
}
}
if( p == 0 ) return pallas::base_field_type::value_type(1);
if( p == 1 ) return x;
pallas::base_field_type::value_type result = pow(x, p/2);
result = result * result;
if( p%2 == 1 ) result = result * x;
return result;
}
Expand Down Expand Up @@ -610,7 +613,7 @@ typedef __attribute__((ext_vector_type(2)))
[[circuit]] bool placeholder_verifier(
std::array<pallas::base_field_type::value_type, full_public_input_size> public_input,
$PUBLIC_INPUT_INPUT$
std::array<pallas::base_field_type::value_type, 2> vk,
placeholder_proof_type proof
) {
Expand All @@ -620,18 +623,7 @@ typedef __attribute__((ext_vector_type(2)))
precomputed_values_type precomputed_values;
std::tie(precomputed_values.l0, precomputed_values.Z_at_xi) = xi_polys(challenges.xi);
//Check public input
std::size_t cur = 0;
for( std::size_t i = 0; i < public_input_amount; i++){
pallas::base_field_type::value_type Omega(1);
pallas::base_field_type::value_type result(0);
for( std::size_t j = 0; j < public_input_sizes[i]; j++){
result += public_input[cur] * Omega / (challenges.xi - Omega);
Omega *= omega;
cur++;
}
__builtin_assigner_exit_check(rows_amount * proof.z[zero_indices[witness_amount + i]] == precomputed_values.Z_at_xi * result);
}
$PUBLIC_INPUT_CHECK$
std::array<pallas::base_field_type::value_type, 8> F;// = {0,0,0,0,0,0,0,0};
Expand Down
Loading

0 comments on commit 03a25b1

Please sign in to comment.