Skip to content

Commit

Permalink
Moved precomputed lookup tables into the binaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iluvmagick committed Jan 20, 2024
1 parent b0ece6e commit f12e2f7
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 98 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,6 @@ cm_deploy(TARGETS ${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME}
INCLUDE include
NAMESPACE ${CMAKE_WORKSPACE_NAME}::)


set(BLUEPRINT_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
target_compile_definitions(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} INTERFACE BLUEPRINT_PATH="${BLUEPRINT_PATH}")

if(BUILD_TESTS)
add_subdirectory(test)
endif()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
// SOFTWARE.
//---------------------------------------------------------------------------//


#include <nil/blueprint/components/hashes/sha2/plonk/detail/split_functions.hpp>
#include <nil/crypto3/marshalling/zk/types/commitments/fri.hpp>

#include <array>
#include <string>
#include <vector>
Expand All @@ -36,8 +32,18 @@
#include <mutex>
#include <unordered_set>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <nil/blueprint/components/hashes/sha2/plonk/detail/split_functions.hpp>

#include <nil/crypto3/multiprecision/cpp_int.hpp>

#include <nil/marshalling/status_type.hpp>
#include <nil/marshalling/field_type.hpp>
#include <nil/marshalling/endianness.hpp>
#include <nil/marshalling/field_type.hpp>
#include <nil/crypto3/algebra/marshalling.hpp>
#include <nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp>
#include <nil/crypto3/marshalling/zk/types/plonk/assignment_table.hpp>


namespace nil {
namespace blueprint {
Expand All @@ -62,7 +68,7 @@ namespace nil {
using value_type = typename BlueprintFieldType::value_type;
using Endianness = nil::marshalling::option::big_endian;
using TTypeBase = nil::marshalling::field_type<Endianness>;
using marshalling_value_type = nil::crypto3::marshalling::types::field_element<TTypeBase, value_type>;
using marshalling_value_type = crypto3::marshalling::types::field_element<TTypeBase, value_type>;
stream << input.size() << std::endl;
for (const auto &[preimage, image] : input) {
std::vector<marshalling_value_type> pair = {
Expand Down Expand Up @@ -335,83 +341,7 @@ namespace nil {
std::cerr << "Time elapsed: " << duration.count() << " seconds" << std::endl;
std::cerr << "Total size: " << std::dec << output_set.size() << std::endl;
}

template<typename Iterator, typename BlueprintFieldType>
struct value_pair_parser : boost::spirit::qi::grammar<Iterator,
std::pair<typename BlueprintFieldType::value_type,
typename BlueprintFieldType::value_type>(),
boost::spirit::qi::ascii::space_type> {
using value_type = typename BlueprintFieldType::value_type;
using integral_type = typename BlueprintFieldType::integral_type;
using return_type = std::pair<value_type, value_type>;

value_pair_parser() : value_pair_parser::base_type(start) {
using boost::spirit::qi::uint_parser;
using boost::spirit::qi::_val;
using boost::spirit::qi::_1;
using boost::spirit::qi::_2;
using boost::phoenix::construct;
using boost::phoenix::val;
auto nubmer = uint_parser<integral_type, 16, 1,
(BlueprintFieldType::modulus_bits + 16 - 1) / 16>();
start = (nubmer >> nubmer)
[_val = construct<std::pair<value_type, value_type>>(_1, _2)];

boost::spirit::qi::on_error<boost::spirit::qi::fail>(
start,
std::cerr << val("Error! Expecting ") << boost::spirit::qi::_4 << val(" here: \"")
<< construct<std::string>(boost::spirit::_3, boost::spirit::_2) << val("\"\n")
);
}

boost::spirit::qi::rule<Iterator, return_type(), boost::spirit::qi::ascii::space_type> start;
};

// Loads the table from file, trying multiple different filen paths if one fails
template <typename BlueprintFieldType>
std::vector<std::vector<typename BlueprintFieldType::value_type>> load_sha_table(
const std::set<std::string> &candidate_file_paths) {
using value_type = typename BlueprintFieldType::value_type;
std::vector<std::vector<value_type>> result;
result.resize(2);
for (const auto &path : candidate_file_paths) {
// try opening the file
std::ifstream file(path);
if (!file.is_open()) {
continue;
}
std::string line;
// Get the table size
std::getline(file, line);
std::size_t table_size = std::stoull(line);
result[0].resize(table_size);
result[1].resize(table_size);
bool parsing_failed = false;
for (std::size_t i = 0; i < table_size; i++) {
std::getline(file, line);
std::pair<value_type, value_type> pair;
value_pair_parser<decltype(line.begin()), BlueprintFieldType> parser;
boost::spirit::qi::ascii::space_type space;
bool parsing_result =
boost::spirit::qi::phrase_parse(line.begin(), line.end(), parser, space, pair);
if (!parsing_result) {
std::cerr << "Failed to parse file " << path << " as table, retrying..." << std::endl;
parsing_failed = true;
break;
}
result[0][i] = pair.first;
result[1][i] = pair.second;
}
if (!parsing_failed) {
return result;
}
}
// if all the attempts failed, return empty vector
result.resize(0);
return result;
}

} // namespace detail
} // namespace components
} // namespace blueprint
} // namespace nil
} // namespace nil
151 changes: 151 additions & 0 deletions include/nil/blueprint/detail/lookup_table_loaders.hpp

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions include/nil/blueprint/lookup_library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#include <nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp>
#include <nil/blueprint/components/hashes/sha2/plonk/detail/split_functions.hpp>
#include <nil/blueprint/components/hashes/sha2/plonk/detail/sha_table_generators.hpp>
#include <nil/blueprint/detail/lookup_table_loaders.hpp>
#include <nil/blueprint/manifest.hpp>
#include <nil/blueprint/assert.hpp>

Expand Down Expand Up @@ -137,12 +137,12 @@ namespace nil {
};

virtual void generate() {
std::string blueprint_path = BLUEPRINT_PATH;
this->_table = components::detail::load_sha_table<BlueprintFieldType>(
{blueprint_path + "/include/nil/blueprint/components/hashes/sha2/plonk/detail/8_split_4.txt"});
if (this->_table.size() == 0 || this->_table[0].size() == 0) {
bool status = components::detail::load_lookup_table_from_bin<BlueprintFieldType>(
"8_split_4.txt",
this->_table);
if (!status) {
std::cerr << "Failed to load table 8_split_4.txt!"
" Please check the paths and generate the table."
" Please check the paths and generate the table."
<< std::endl;
BLUEPRINT_RELEASE_ASSERT(0);
}
Expand Down Expand Up @@ -186,10 +186,10 @@ namespace nil {
this->subtables["full"] = {{0,1}, 0, 43903};
};
virtual void generate() {
std::string blueprint_path = BLUEPRINT_PATH;
this->_table = components::detail::load_sha_table<BlueprintFieldType>(
{blueprint_path + "/include/nil/blueprint/components/hashes/sha2/plonk/detail/8_split_7.txt"});
if (this->_table.size() == 0 || this->_table[0].size() == 0) {
bool status = components::detail::load_lookup_table_from_bin<BlueprintFieldType>(
"8_split_7.txt",
this->_table);
if (!status) {
std::cerr << "Failed to load table 8_split_7.txt!"
" Please check the paths and generate the table."
<< std::endl;
Expand Down

0 comments on commit f12e2f7

Please sign in to comment.