Skip to content

Commit

Permalink
Fixed hash decomposition component.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iluvmagick committed Nov 13, 2023
1 parent 93b6463 commit 9ff6e2a
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 190 deletions.
201 changes: 130 additions & 71 deletions include/nil/blueprint/components/hashes/sha2/plonk/decomposition.hpp

Large diffs are not rendered by default.

24 changes: 0 additions & 24 deletions include/nil/blueprint/components/hashes/sha2/plonk/sha256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,6 @@ namespace nil {
using lookup_table_definition = typename
nil::crypto3::zk::snark::lookup_table_definition<BlueprintFieldType>;

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 typename sha256_process_type::sparse_values_base4_table());
result.push_back(sparse_values_base4);

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

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

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

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

auto ch = std::shared_ptr<lookup_table_definition>(new typename sha256_process_type::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
Original file line number Diff line number Diff line change
Expand Up @@ -1529,57 +1529,6 @@ namespace nil {
assignment.witness(component.W(6), row + i) = ( ((1 << 14) - 1) & (integral_a2 >> 28) );
assignment.witness(component.W(7), row + i) = integral_a2;
}
/*std::vector<std::size_t> value_sizes = {14};
// lookup table for sparse values with base = 4
for (typename CurveType::scalar_field_type::integral_type i = 0;
i < typename CurveType::scalar_field_type::integral_type(16384);
i++) {
std::vector<bool> value(14);
for (std::size_t j = 0; j < 14; j++) {
value[14 - j - 1] = multiprecision::bit_test(i, j);
}
std::array<std::vector<uint64_t>, 2> value_chunks =
detail::split_and_sparse<BlueprintFieldType>(value, value_sizes,
plonk_sha256_process<BlueprintFieldType, ArithmetizationParams>::base4);
assignment.constant(0)[start_row_index + std::size_t(i)] = value_chunks[0][0];
assignment.constant(1)[start_row_index + std::size_t(i)] = value_chunks[1][0];
}
// lookup table for sparse values with base = 7
for (typename CurveType::scalar_field_type::integral_type i = 0;
i < typename CurveType::scalar_field_type::integral_type(16384);
i++) {
std::vector<bool> value(14);
for (std::size_t j = 0; j < 14; j++) {
value[14 - j - 1] = multiprecision::bit_test(i, j);
}
std::array<std::vector<uint64_t>, 2> value_chunks =
detail::split_and_sparse<BlueprintFieldType>(value, value_sizes,
plonk_sha256_process<BlueprintFieldType, ArithmetizationParams>::base7);
assignment.constant(2)[start_row_index + std::size_t(i)] = value_chunks[0][0];
assignment.constant(3)[start_row_index + std::size_t(i)] = value_chunks[1][0];
}
// lookup table for maj function
value_sizes = {8};
for (typename CurveType::scalar_field_type::integral_type i = 0;
i < typename CurveType::scalar_field_type::integral_type(65535);
i++) {
static std::array<std::vector<typename CurveType::scalar_field_type::integral_type>, 2>
value = detail::reversed_sparse_and_split<BlueprintFieldType>(i, value_sizes,
plonk_sha256_process<BlueprintFieldType, ArithmetizationParams>::base4);
assignment.constant(4)[start_row_index + std::size_t(i)] = value[0][0];
assignment.constant(5)[start_row_index + std::size_t(i)] = i;
}
// lookup table for ch function
for (typename CurveType::scalar_field_type::integral_type i = 0;
i < typename CurveType::scalar_field_type::integral_type(5765041);
i++) {
static std::array<std::vector<typename CurveType::scalar_field_type::integral_type>, 2>
value = detail::reversed_sparse_and_split<BlueprintFieldType>(i, value_sizes,
plonk_sha256_process<BlueprintFieldType, ArithmetizationParams>::base7);
assignment.constant(4)[start_row_index + std::size_t(i)] = value[0][0];
assignment.constant(5)[start_row_index + std::size_t(i)] = i;
}*/

return typename plonk_sha256_process<BlueprintFieldType, ArithmetizationParams>::result_type(
component, start_row_index);
Expand Down
11 changes: 6 additions & 5 deletions include/nil/blueprint/utils/satisfiability_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@
#include <nil/crypto3/zk/snark/arithmetization/plonk/copy_constraint.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/lookup_constraint.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/variable.hpp>
#include <nil/crypto3/zk/math/expression_visitors.hpp>

namespace nil {
namespace blueprint {

template<typename BlueprintFieldType,
typename ArithmetizationParams>
bool is_satisfied(const circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType,
ArithmetizationParams>> &bp,
ArithmetizationParams>> &bp,
const assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType,
ArithmetizationParams>> &assignments){

Expand Down Expand Up @@ -125,14 +126,14 @@ namespace nil {
}
}
if (!found) {
std::cout << "Constraint " << j << " from lookup gate " << i
<< " from table " << table_name << " on row " << selector_row
<< " is not satisfied." << std::endl;
std::cout << "Input values:";
for(std::size_t k = 0; k < input_values.size(); k++){
std::cout << input_values[k] << " ";
}
std::cout << std::endl;
std::cout << "Constraint " << j << " from lookup gate " << i << " from table " << table_name << " on row " << selector_row
<< " is not satisfied." << std::endl;
std::cout << "Offending Lookup Gate: " << std::endl;
std::cout << std::endl << "Offending Lookup Gate: " << std::endl;
for (const auto &constraint : lookup_gates[i].constraints) {
std::cout << "Table id: " << constraint.table_id << std::endl;
for (auto &lookup_input : constraint.lookup_input) {
Expand Down
73 changes: 38 additions & 35 deletions test/hashes/plonk/decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ void test_decomposition(std::vector<typename BlueprintFieldType::value_type> pub

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 0;
constexpr std::size_t SelectorColumns = 1;
constexpr std::size_t ConstantColumns = 8;
constexpr std::size_t SelectorColumns = 3;
using hash_type = crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 40;

Expand All @@ -69,6 +69,12 @@ void test_decomposition(std::vector<typename BlueprintFieldType::value_type> pub

auto result_check = [&expected_res](AssignmentType &assignment,
typename component_type::result_type &real_res) {
for (std::size_t i = 0; i < real_res.output.size(); i++){
std::cout << var_value(assignment, real_res.output[i]).data << std::endl;
}
for (std::size_t i = 0; i < expected_res.size(); i++){
std::cout << expected_res[i].data << std::endl;
}
for (std::size_t i = 0; i < real_res.output.size(); i++){
assert(expected_res[i] == var_value(assignment, real_res.output[i]));
}
Expand All @@ -80,52 +86,49 @@ void test_decomposition(std::vector<typename BlueprintFieldType::value_type> pub

if (expected_to_pass) {
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input);
component_instance, public_input, result_check, instance_input,
nil::crypto3::detail::connectedness_check_type::WEAK);
} else {
crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check_to_fail, instance_input);
component_instance, public_input, result_check_to_fail, instance_input,
nil::crypto3::detail::connectedness_check_type::WEAK);
}
}

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

template<typename FieldType>
std::vector<typename FieldType::value_type> calculate_decomposition(std::vector<typename FieldType::value_type> data_value) {
std::array<typename FieldType::integral_type, 2> data = {
typename FieldType::integral_type(data_value[0].data),
typename FieldType::integral_type(data_value[1].data)};
std::array<typename FieldType::integral_type, 16> range_chunks;
std::size_t shift = 0;

for (std::size_t i = 0; i < 8; i++) {
range_chunks[i] = (data[0] >> shift) & ((1 << 16) - 1);
range_chunks[i + 8] = (data[1] >> shift) & ((1 << 16) - 1);
shift += 16;
}

std::array<typename FieldType::integral_type, 8> output;

output[0] = range_chunks[1] * (1 << 16) + range_chunks[0];
output[1] = range_chunks[3] * (1 << 16) + range_chunks[2];
output[2] = range_chunks[5] * (1 << 16) + range_chunks[4];
output[3] = range_chunks[7] * (1 << 16) + range_chunks[6];
output[4] = range_chunks[9] * (1 << 16) + range_chunks[8];
output[5] = range_chunks[11] * (1 << 16) + range_chunks[10];
output[6] = range_chunks[13] * (1 << 16) + range_chunks[12];
output[7] = range_chunks[15] * (1 << 16) + range_chunks[14];

std::vector<typename FieldType::value_type> output_value;

for (std::size_t i = 0; i < output.size(); i++){
output_value.push_back(typename FieldType::value_type(output[i]));
}

return output_value;
std::vector<typename FieldType::value_type> calculate_decomposition(
const std::vector<typename FieldType::value_type> &data_value) {

std::array<typename FieldType::integral_type, 2> data = {
typename FieldType::integral_type(data_value[0].data),
typename FieldType::integral_type(data_value[1].data)};
std::size_t shift = 0;
std::array<typename FieldType::integral_type, 8> output;
const typename FieldType::integral_type one = 1;

for (std::size_t i = 0; i < 4; i++, shift += 32) {
output[i + 4] = (data[0] >> shift) & ((one << 32) - 1);
output[i] = (data[1] >> shift) & ((one << 32) - 1);
}

std::vector<typename FieldType::value_type> output_value(output.size());

for (std::size_t i = 0; i < output.size(); i++){
output_value[output.size() - 1 - i] = typename FieldType::value_type(output[i]);
}
return output_value;
}

BOOST_AUTO_TEST_CASE(blueprint_plonk_decomposition_test0) {
using field_type = typename crypto3::algebra::curves::pallas::base_field_type;

test_decomposition<field_type>(
{0x1_cppui255, 0x2_cppui255},
calculate_decomposition<field_type>({0x1_cppui255, 0x2_cppui255}),
true);

test_decomposition<field_type>(
{0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255},
calculate_decomposition<field_type>({0x8d741211e928fdd4d33a13970d0ce7f3_cppui255, 0x92f209334030f9ec8fa8a025e987a5dd_cppui255}),
Expand Down
4 changes: 2 additions & 2 deletions test/hashes/plonk/sha256_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_sha256_process) {
using BlueprintFieldType = typename curve_type::base_field_type;
constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 33;
constexpr std::size_t SelectorColumns = 50;
constexpr std::size_t ConstantColumns = 20;
constexpr std::size_t SelectorColumns = 30;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

Expand Down
7 changes: 5 additions & 2 deletions test/test_plonk_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ namespace nil {
assigner(component_instance, assignment, instance_input, start_row));
result_check(assignment, component_result);

// assignment.export_table(std::cout);
// bp.export_circuit(std::cout);

if constexpr (!PrivateInput) {
bool is_connected;
if (connectedness_check == detail::connectedness_check_type::STRONG) {
Expand Down Expand Up @@ -248,7 +251,7 @@ namespace nil {
// blueprint::detail::export_connectedness_zones(
// zones, assignment, instance_input.all_vars(), start_row, component_instance.rows_amount, std::cout);

//BOOST_ASSERT_MSG(is_connected,
// BOOST_ASSERT_MSG(is_connected,
// "Component disconnected! See comment above this assert for a way to output a visual representation of the connectedness graph.");
}

Expand Down Expand Up @@ -289,7 +292,7 @@ namespace nil {
bp.get_reserved_tables(),
bp, assignment, lookup_columns_indices,
desc.usable_rows_amount,
500000
desc.usable_rows_amount
);
}
desc.rows_amount = zk::snark::basic_padding(assignment);
Expand Down

0 comments on commit 9ff6e2a

Please sign in to comment.