Skip to content

Commit

Permalink
EVM verifier for lookups issues fixed #65
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Jan 26, 2024
1 parent 97792e5 commit 7ffffb1
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 150 deletions.
29 changes: 8 additions & 21 deletions include/nil/blueprint/transpiler/lpc_scheme_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ namespace nil {
PlaceholderParams
>::preprocessed_data_type::common_data_type;

std::string rot_string (int j){
if(j == 0) return "xi"; else
if(j == 1 ) return "mulmod(xi, omega, modulus)"; else
if(j == -1) return "mulmod(xi, inversed_omega, modulus)"; else
if(j > 0) return "mulmod(xi, field.pow_small(omega, " + to_string(j) + ", modulus), modulus)"; else
if(j < 0) return "mulmod(xi, field.pow_small(inversed_omega, " + to_string(-j) + ", modulus), modulus)";
return "";
}


template<typename PlaceholderParams>
void commitment_scheme_replaces(
transpiler_replacements& replacements,
Expand All @@ -72,17 +62,14 @@ namespace nil {
"evm" // Generator mode
);

/* std::stringstream points_ids;
for(std::size_t i = 0; i < points.size(); i++){
std::size_t j = 0;
for(const auto &unique_point:unique_points){
if(points[i] == unique_point){
points_ids << std::hex << std::setw(2) << std::setfill('0') << j;
break;
}
j++;
}
}*/
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
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ unchecked {
{
uint256 offset;
if (challenge!= transcript.get_field_challenge(tr_state, modulus)) return false;
if (challenge!= transcript.get_field_challenge(tr_state, modulus)) {
console.log("Wrong challenge");
return false;
}
for(uint8 i = 0; i < batches_num;){
transcript.update_transcript_b32(tr_state, bytes32(commitments[i]));
Expand Down Expand Up @@ -348,6 +351,9 @@ unchecked {
off -= 0x20;
i++;
}
for(uint256 i = 0; i < state.unique_eval_points.length;){
i++;
}
}
$ETA_VALUES_VERIFICATION$
Expand Down
135 changes: 77 additions & 58 deletions include/nil/blueprint/transpiler/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ namespace nil {
return strstr.str();
}

static inline std::string rot_string (int j, std::string mode){
static inline std::string rot_string (int j, std::size_t rows_amount, std::string mode){
int abs_j = j>0? j: -j;
int other_j = rows_amount - abs_j;
if(other_j < abs_j) {
j = j > 0? -other_j: other_j;
}

if( mode == "recursive"){
if(j == 0) return "xi"; else
if(j == 1 ) return "xi*omega"; else
Expand All @@ -60,8 +66,8 @@ namespace nil {
if(j == 0) return "xi"; else
if(j == 1 ) return "mulmod(xi, omega, modulus)"; else
if(j == -1) return "mulmod(xi, inversed_omega, modulus)"; else
if(j > 0) return "mulmod(xi,pow_small(omega, " + to_string(j) + ", modulus), modulus)"; else
if(j < 0) return "mulmod(xi,pow_small(omega, " + to_string(-j) + ", modulus), modulus)";
if(j > 0) return "mulmod(xi, field.pow_small(omega, " + to_string(j) + ", modulus), modulus)"; else
if(j < 0) return "mulmod(xi, field.pow_small(inversed_omega, " + to_string(-j) + ", modulus), modulus)";
}
return "";
}
Expand Down Expand Up @@ -104,9 +110,10 @@ namespace nil {
std::vector<std::string> singles;
std::map<std::string, std::size_t> singles_map;
std::vector<std::vector<std::size_t>> poly_ids;
std::size_t rows_amount = common_data.rows_amount;

singles.push_back(rot_string(0, mode));
singles_map[rot_string(0, mode)] = singles_map.size();
singles.push_back(rot_string(0, rows_amount, mode));
singles_map[rot_string(0, rows_amount, mode)] = singles_map.size();

singles.push_back("eta");
singles_map["eta"] = singles_map.size();
Expand All @@ -115,132 +122,144 @@ namespace nil {
// Sigma and permutation polys
std::size_t count = 0;
for( std::size_t i = 0; i < permutation_size; i++){
poly_ids[singles_map[rot_string(0, mode)]].push_back(count);
poly_ids[singles_map[rot_string(0, rows_amount, mode)]].push_back(count);
poly_ids[singles_map["eta"]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, mode)]);
z_points_indices.push_back(singles_map[rot_string(0, rows_amount, mode)]);
z_points_indices.push_back(singles_map["eta"]);
poly_ids[singles_map["eta"]].push_back(count+1);
poly_ids[singles_map[rot_string(0, mode)]].push_back(count+1);
z_points_indices.push_back(singles_map[rot_string(0, mode)]);
poly_ids[singles_map[rot_string(0, rows_amount, mode)]].push_back(count+1);
z_points_indices.push_back(singles_map[rot_string(0, rows_amount, mode)]);
z_points_indices.push_back(singles_map["eta"]);
count += 2;
}
std::cout << "Permutations are done!" << std::endl;

// Special selectors
singles.push_back(rot_string(1, mode));
singles_map[rot_string(1, mode)] = singles_map.size();
singles.push_back(rot_string(1, rows_amount, mode));
singles_map[rot_string(1, rows_amount, mode)] = singles_map.size();
poly_ids.resize(singles.size());

poly_ids[singles_map["eta"]].push_back(count);
poly_ids[singles_map[rot_string(0, mode)]].push_back(count);
poly_ids[singles_map[rot_string(1, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, mode)]);
z_points_indices.push_back(singles_map[rot_string(1, mode)]);
poly_ids[singles_map[rot_string(0, rows_amount, mode)]].push_back(count);
poly_ids[singles_map[rot_string(1, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, rows_amount, mode)]);
z_points_indices.push_back(singles_map[rot_string(1, rows_amount, mode)]);
z_points_indices.push_back(singles_map["eta"]);
count++;
poly_ids[singles_map["eta"]].push_back(count);
poly_ids[singles_map[rot_string(0, mode)]].push_back(count);
poly_ids[singles_map[rot_string(1, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, mode)]);
z_points_indices.push_back(singles_map[rot_string(1, mode)]);
poly_ids[singles_map[rot_string(0, rows_amount, mode)]].push_back(count);
poly_ids[singles_map[rot_string(1, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, rows_amount, mode)]);
z_points_indices.push_back(singles_map[rot_string(1, rows_amount, mode)]);
z_points_indices.push_back(singles_map["eta"]);
count++;

std::cout << "Special selectors are done!" << std::endl;

for(std::size_t i = 0; i < PlaceholderParams::constant_columns; i++){
std::stringstream str;
for(auto j:common_data.columns_rotations[i + PlaceholderParams::witness_columns + PlaceholderParams::public_input_columns]){
if(singles_map.find(rot_string(j, mode)) == singles_map.end()){
singles_map[rot_string(j, mode)] = singles_map.size();
singles.push_back(rot_string(j, mode));
if(singles_map.find(rot_string(j, rows_amount, mode)) == singles_map.end()){
singles_map[rot_string(j, rows_amount, mode)] = singles_map.size();
singles.push_back(rot_string(j, rows_amount, mode));
poly_ids.resize(singles.size());
}
poly_ids[singles_map[rot_string(j, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(j, mode)]);
poly_ids[singles_map[rot_string(j, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(j, rows_amount, mode)]);
}
poly_ids[singles_map["eta"]].push_back(count);
z_points_indices.push_back(singles_map["eta"]);
count++;
}
std::cout << "Constants are done!" << std::endl;

for(std::size_t i = 0; i < PlaceholderParams::selector_columns; i++){
std::stringstream str;
for(auto j:common_data.columns_rotations[i + PlaceholderParams::witness_columns + PlaceholderParams::public_input_columns + PlaceholderParams::constant_columns]){
if(singles_map.find(rot_string(j, mode)) == singles_map.end()){
singles_map[rot_string(j, mode)] = singles_map.size();
singles.push_back(rot_string(j, mode));
if(singles_map.find(rot_string(j, rows_amount, mode)) == singles_map.end()){
singles_map[rot_string(j, rows_amount, mode)] = singles_map.size();
singles.push_back(rot_string(j, rows_amount, mode));
poly_ids.resize(singles.size());
}
poly_ids[singles_map[rot_string(j, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(j, mode)]);
poly_ids[singles_map[rot_string(j, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(j, rows_amount, mode)]);
}
poly_ids[singles_map["eta"]].push_back(count);
z_points_indices.push_back(singles_map["eta"]);
count++;
}
std::cout << "Selectors are done!" << std::endl;

for(std::size_t i = 0; i < PlaceholderParams::witness_columns; i++){
std::stringstream str;
for(auto j:common_data.columns_rotations[i]){
if(singles_map.find(rot_string(j, mode)) == singles_map.end()){
singles_map[rot_string(j, mode)] = singles_map.size();
singles.push_back(rot_string(j, mode));
if(singles_map.find(rot_string(j, rows_amount, mode)) == singles_map.end()){
singles_map[rot_string(j, rows_amount, mode)] = singles_map.size();
singles.push_back(rot_string(j, rows_amount, mode));
poly_ids.resize(singles.size());
}
poly_ids[singles_map[rot_string(j, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(j, mode)]);
poly_ids[singles_map[rot_string(j, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(j, rows_amount, mode)]);
}
count++;
}
std::cout << "Witnesses are done!" << std::endl;

for(std::size_t i = 0; i < PlaceholderParams::public_input_columns; i++){
std::stringstream str;
for(auto j:common_data.columns_rotations[i + PlaceholderParams::witness_columns]){
if(singles_map.find(rot_string(j, mode)) == singles_map.end()){
singles_map[rot_string(j, mode)] = singles_map.size();
singles.push_back(rot_string(j, mode));
if(singles_map.find(rot_string(j, rows_amount, mode)) == singles_map.end()){
singles_map[rot_string(j, rows_amount, mode)] = singles_map.size();
singles.push_back(rot_string(j, rows_amount, mode));
poly_ids.resize(singles.size());
}
poly_ids[singles_map[rot_string(j, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(j, mode)]);
poly_ids[singles_map[rot_string(j, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(j, rows_amount, mode)]);
}
count++;
}
std::cout << "Public inputs are done!" << std::endl;

// Permutation argument
poly_ids[singles_map[rot_string(0, mode)]].push_back(count);
poly_ids[singles_map[rot_string(1, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, mode)]);
z_points_indices.push_back(singles_map[rot_string(1, mode)]);
poly_ids[singles_map[rot_string(0, rows_amount, mode)]].push_back(count);
poly_ids[singles_map[rot_string(1, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, rows_amount, mode)]);
z_points_indices.push_back(singles_map[rot_string(1, rows_amount, mode)]);
count++;
std::cout << "Permutation polynomials are done!" << std::endl;

// Lookup permutation
if(use_lookups){
poly_ids[singles_map[rot_string(0, mode)]].push_back(count);
poly_ids[singles_map[rot_string(1, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, mode)]);
z_points_indices.push_back(singles_map[rot_string(1, mode)]);
poly_ids[singles_map[rot_string(0, rows_amount, mode)]].push_back(count);
poly_ids[singles_map[rot_string(1, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, rows_amount, mode)]);
z_points_indices.push_back(singles_map[rot_string(1, rows_amount, mode)]);
count++;
std::cout << "Lookup permutation polynomials are done!" << std::endl;
}
// Quotient
for(std::size_t i = 0; i < quotient_size; i++){
poly_ids[singles_map[rot_string(0, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, mode)]);
poly_ids[singles_map[rot_string(0, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, rows_amount, mode)]);
count++;
}
std::cout << "Quotient polynomials are done!" << std::endl;
// Lookup batch
if(use_lookups){
singles_map[rot_string(common_data.usable_rows_amount, mode)] = singles.size();
singles.push_back(rot_string(common_data.usable_rows_amount, mode));
if(singles_map.find(rot_string(common_data.usable_rows_amount, rows_amount, mode)) == singles_map.end()){
singles_map[rot_string(common_data.usable_rows_amount, rows_amount, mode)] = singles.size();
singles.push_back(rot_string(common_data.usable_rows_amount, rows_amount, mode));
poly_ids.resize(singles.size());
}
for( std::size_t i = 0; i < sorted_size; i++ ){
poly_ids[singles_map[rot_string(0, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, mode)]);
poly_ids[singles_map[rot_string(1, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(1, mode)]);
poly_ids[singles_map[rot_string(common_data.usable_rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(common_data.usable_rows_amount, mode)]);
poly_ids[singles_map[rot_string(0, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(0, rows_amount, mode)]);
poly_ids[singles_map[rot_string(1, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(1, rows_amount, mode)]);
poly_ids[singles_map[rot_string(common_data.usable_rows_amount, rows_amount, mode)]].push_back(count);
z_points_indices.push_back(singles_map[rot_string(common_data.usable_rows_amount, rows_amount, mode)]);
count++;
}
std::cout << "Lookup polynomials are done!" << std::endl;
}

return std::make_tuple(z_points_indices, singles, singles_map, poly_ids);
Expand Down
1 change: 0 additions & 1 deletion test/detail/circuits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,6 @@ namespace nil {
typename FieldType::value_type zero = FieldType::value_type::zero();

auto r = rnd() % 7;
table[0] = std::vector<typename FieldType::value_type>(16);
std::size_t j = 0;
for( std::size_t i = 0; i < 7; i++){
if( j == r ) j++;
Expand Down
Loading

0 comments on commit 7ffffb1

Please sign in to comment.