Skip to content

Commit

Permalink
Moved custom lookup tables to the lookup library. (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iluvmagick authored Nov 7, 2023
1 parent e922c0d commit e82a0d8
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 196 deletions.
180 changes: 0 additions & 180 deletions include/nil/blueprint/components/hashes/sha2/plonk/sha256_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <nil/blueprint/component.hpp>
#include <nil/blueprint/manifest.hpp>
#include <nil/blueprint/components/hashes/sha2/plonk/detail/split_functions.hpp>
#include <nil/blueprint/components/hashes/sha2/plonk/detail/sha_table_generators.hpp>

namespace nil {
namespace blueprint {
Expand Down Expand Up @@ -158,185 +157,6 @@ namespace nil {
component_type(witnesses, constants, public_inputs, get_manifest()) {};


using lookup_table_definition = typename nil::crypto3::zk::snark::detail::lookup_table_definition<BlueprintFieldType>;

class sparse_values_base4_table: public lookup_table_definition{
public:
sparse_values_base4_table(): lookup_table_definition("sha256_sparse_base4"){
this->subtables["full"] = {{0,1}, 0, 16383};
this->subtables["first_column"] = {{0}, 0, 16383};
};
virtual void generate(){
this->_table.resize(2);
std::vector<std::size_t> value_sizes = {14};

// lookup table for sparse values with base = 4
for (typename BlueprintFieldType::integral_type i = 0;
i < typename BlueprintFieldType::integral_type(16384);
i++
) {
std::vector<bool> value(14);
for (std::size_t j = 0; j < 14; j++) {
value[14 - j - 1] = crypto3::multiprecision::bit_test(i, j);
}
std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> value_chunks =
detail::split_and_sparse<BlueprintFieldType>(value, value_sizes, base4 );
this->_table[0].push_back(value_chunks[0][0]);
this->_table[1].push_back(value_chunks[1][0]);
}
}

virtual std::size_t get_columns_number(){return 2;}
virtual std::size_t get_rows_number(){return 16384;}
};

class reverse_sparse_sigmas_base4_table : public lookup_table_definition {
public:
reverse_sparse_sigmas_base4_table(): lookup_table_definition("sha256_reverse_sparse_base4"){
this->subtables["full"] = {{0,1}, 0, 65535};
};

virtual void generate() {
this->_table = detail::load_sha_table<BlueprintFieldType>(
{"8_split_4.txt",
"./../libs/crypto3/libs/blueprint/include/nil/blueprint/components/hashes/sha2/plonk/detail/8_split_4.txt",
"./../libs/blueprint/include/nil/blueprint/components/hashes/sha2/plonk/detail/8_split_4.txt",
"./../../../../libs/blueprint/include/nil/blueprint/components/hashes/sha2/plonk/detail/8_split_4.txt"});
if (this->_table.size() == 0 || this->_table[0].size() == 0) {
std::cerr << "Failed to load table 8_split_4.txt!"
" Please check the paths and generate the table."
<< std::endl;
BLUEPRINT_RELEASE_ASSERT(0);
}
}

virtual std::size_t get_columns_number(){return 2;}
virtual std::size_t get_rows_number(){return 65536;}
};

class sparse_values_base7_table: public lookup_table_definition{
public:
sparse_values_base7_table(): lookup_table_definition("sha256_sparse_base7"){
this->subtables["full"] = {{0,1}, 0, 16383};
this->subtables["first_column"] = {{0}, 0, 16383};
this->subtables["second_column"] = {{1}, 0, 16383};
};
virtual void generate(){
this->_table.resize(2);
std::vector<std::size_t> value_sizes = {14};
for (typename BlueprintFieldType::integral_type i = 0;
i < typename BlueprintFieldType::integral_type(16384);
i++) {
std::vector<bool> value(14);
for (std::size_t j = 0; j < 14; j++) {
value[14 - j - 1] = crypto3::multiprecision::bit_test(i, j);
}
std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> value_chunks =
detail::split_and_sparse<BlueprintFieldType>(value, value_sizes, base7);
this->_table[0].push_back(value_chunks[0][0]);
this->_table[1].push_back(value_chunks[1][0]);
}
}

virtual std::size_t get_columns_number(){return 2;}
virtual std::size_t get_rows_number(){return 16384;}
};

class reverse_sparse_sigmas_base7_table: public lookup_table_definition{
public:
reverse_sparse_sigmas_base7_table(): lookup_table_definition("sha256_reverse_sparse_base7"){
this->subtables["full"] = {{0,1}, 0, 43903};
};
virtual void generate() {
this->_table = detail::load_sha_table<BlueprintFieldType>(
{"8_split_7.txt",
"./../libs/crypto3/libs/blueprint/include/nil/blueprint/components/hashes/sha2/plonk/detail/8_split_7.txt",
"./../libs/blueprint/include/nil/blueprint/components/hashes/sha2/plonk/detail/8_split_7.txt",
"./../../../../libs/blueprint/include/nil/blueprint/components/hashes/sha2/plonk/detail/8_split_7.txt"});
if (this->_table.size() == 0 || this->_table[0].size() == 0) {
std::cerr << "Failed to load table 8_split_7.txt!"
" Please check the paths and generate the table."
<< std::endl;
BLUEPRINT_RELEASE_ASSERT(0);
}
}

virtual std::size_t get_columns_number(){return 2;}
virtual std::size_t get_rows_number(){return 43904;}
};

class maj_function_table: public lookup_table_definition{
public:
maj_function_table(): lookup_table_definition("sha256_maj"){
this->subtables["full"] = {{0,1}, 0, 65534};
this->subtables["first_column"] = {{0}, 0, 65534};
};
virtual void generate(){
this->_table.resize(2);
std::vector<std::size_t> value_sizes = {8};
for (typename BlueprintFieldType::integral_type i = 0;
i < typename BlueprintFieldType::integral_type(65535);
i++
) {
std::array<std::vector<typename BlueprintFieldType::integral_type>, 2>
value = detail::reversed_sparse_and_split_maj<BlueprintFieldType>(i, value_sizes, base4);
this->_table[0].push_back(value[0][0]);
this->_table[1].push_back(value[1][0]);
}
}

virtual std::size_t get_columns_number(){return 2;}
virtual std::size_t get_rows_number(){return 65535;}
};

class ch_function_table: public lookup_table_definition{
public:
ch_function_table(): lookup_table_definition("sha256_ch"){
this->subtables["full"] = {{0,1}, 0, 5765040};
this->subtables["first_column"] = {{0}, 0, 5765040};
};
virtual void generate(){
this->_table.resize(2);
std::vector<std::size_t> value_sizes = {8};
for (typename BlueprintFieldType::integral_type i = 0;
i < typename BlueprintFieldType::integral_type(5765041);
i++
) {
std::array<std::vector<typename BlueprintFieldType::integral_type>, 2>
value = detail::reversed_sparse_and_split_ch<BlueprintFieldType>(i, value_sizes, base7);
this->_table[0].push_back(value[0][0]);
this->_table[1].push_back(value[1][0]);
}
}

virtual std::size_t get_columns_number(){return 2;}
virtual std::size_t get_rows_number(){return 5765041;}
};

std::vector<std::shared_ptr<lookup_table_definition>> component_custom_lookup_tables(){
std::vector<std::shared_ptr<lookup_table_definition>> result = {};

auto sparse_values_base4 = std::shared_ptr<lookup_table_definition>(new sparse_values_base4_table());
result.push_back(sparse_values_base4);

auto sparse_values_base7 = std::shared_ptr<lookup_table_definition>(new sparse_values_base7_table());
result.push_back(sparse_values_base7);

auto maj = std::shared_ptr<lookup_table_definition>(new maj_function_table());
result.push_back(maj);

auto reverse_sparse_values_base4 = std::shared_ptr<lookup_table_definition>(new reverse_sparse_sigmas_base4_table());
result.push_back(reverse_sparse_values_base4);

auto reverse_sparse_values_base7 = std::shared_ptr<lookup_table_definition>(new reverse_sparse_sigmas_base7_table());
result.push_back(reverse_sparse_values_base7);

auto ch = std::shared_ptr<lookup_table_definition>(new ch_function_table());
result.push_back(ch);

return result;
}

std::map<std::string, std::size_t> component_lookup_tables(){
std::map<std::string, std::size_t> lookup_tables;
lookup_tables["sha256_sparse_base4/full"] = 0; // REQUIRED_TABLE
Expand Down
Loading

0 comments on commit e82a0d8

Please sign in to comment.