Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing horizontal table packer #228

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,9 @@ namespace nil {
for (std::size_t column = layout_x + fold * column_amount;
column < layout_x + (fold + 1) * column_amount; column++) {

for (std::size_t row = layout_y, table_row = 0; row < layout_y + rows_amount;
row++, table_row++) {
for (std::size_t row = layout_y, table_row = fold * rows_amount;
row < layout_y + rows_amount;
row++, table_row++) {

if (table_row < table_rows_amount) {
constant_columns[column][row] =
Expand All @@ -366,7 +367,7 @@ namespace nil {
continue;
}
// Check if the current rows are already included in some selector so that we reuse it
// Because folded table would not have a non-full subtable, this works
// This works because a folded table would not have a non-full subtable
std::size_t next_selector;
if (selector_ids.find(std::make_pair(subtable.begin, subtable.end)) !=
selector_ids.end()) {
Expand All @@ -386,7 +387,7 @@ namespace nil {
for(std::size_t fold = 0; fold < fold_count; fold++) {
std::vector<plonk_variable<typename FieldType::value_type>> option;
for(const auto &column_index : subtable.column_indices) {
option.emplace_back( plonk_variable<typename FieldType::value_type>(
option.emplace_back(plonk_variable<typename FieldType::value_type>(
constant_columns_ids[layout_x + fold * column_amount + column_index], 0,
false, plonk_variable<typename FieldType::value_type>::column_type::constant
));
Expand Down
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ set(TESTS_NAMES
"systems/plonk/pickles/oracles"
"systems/plonk/pickles/to_field"
"systems/plonk/pickles/to_group"

"systems/plonk/placeholder/placeholder"
"systems/plonk/placeholder/performance"

Expand All @@ -105,6 +105,8 @@ set(TESTS_NAMES
"transcript/transcript"
"transcript/kimchi_transcript"

"systems/plonk/placeholder/lookup_table_packer"

"systems/plonk/plonk_constraint")

foreach(TEST_NAME ${TESTS_NAMES})
Expand Down
140 changes: 140 additions & 0 deletions test/systems/plonk/placeholder/lookup_table_packer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2023 Dmitrii Tabalin
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#include <cstddef>
#define BOOST_TEST_MODULE zk_lookup_table_packer_test

#include <iostream>

#include <boost/test/unit_test.hpp>

#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/crypto3/algebra/fields/arithmetic_params/pallas.hpp>

#include <nil/crypto3/hash/algorithm/hash.hpp>
#include <nil/crypto3/hash/keccak.hpp>

#include <nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp>

#include <nil/crypto3/zk/snark/arithmetization/plonk/params.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp>

using namespace nil::crypto3;
using namespace nil::crypto3::zk;
using namespace nil::crypto3::zk::snark;

class test_lookup_table : public lookup_table_definition<algebra::curves::pallas::base_field_type> {
public:
std::size_t rows_amount;
std::size_t columns_amount;

test_lookup_table(std::size_t _rows_amount, std::size_t _columns_amount) :
lookup_table_definition<algebra::curves::pallas::base_field_type>(
"test_table" + std::to_string(_rows_amount) + "_" + std::to_string(_columns_amount)),
rows_amount(_rows_amount),
columns_amount(_columns_amount) {
std::vector<std::size_t> column_indices(columns_amount);
std::iota(column_indices.begin(), column_indices.end(), 0);
this->subtables["full"] = {column_indices, 0, rows_amount - 1};
}

void generate() override {
_table.resize(columns_amount);
for (std::size_t i = 0; i < columns_amount; i++) {
_table[i].resize(rows_amount);
}
for (std::size_t i = 0; i < columns_amount; ++i) {
for (std::size_t j = 0; j < rows_amount; j++) {
this->_table[i][j] = i * rows_amount + j;
}
}
}

std::size_t get_columns_number() override {
return columns_amount;
}
virtual std::size_t get_rows_number() override {
return rows_amount;
}
};

BOOST_AUTO_TEST_SUITE(lookup_table_packer_test_suite)

BOOST_AUTO_TEST_CASE(horizontal_lookup_table_packer_test) {
using curve_type = algebra::curves::pallas;
using FieldType = 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;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using ArithmetizationParams = plonk_arithmetization_params<WitnessColumns,
PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = plonk_constraint_system<FieldType, ArithmetizationParams>;
plonk_constraint_system<FieldType, ArithmetizationParams> bp;
plonk_assignment_table<FieldType, ArithmetizationParams> assignment;

std::map<std::string, std::shared_ptr<lookup_table_definition<FieldType>>> lookup_tables;
std::map<std::string, std::size_t> lookup_table_ids;

std::vector<std::size_t> constant_columns_ids(ConstantColumns);
std::iota(constant_columns_ids.begin(), constant_columns_ids.end(), 0);

test_lookup_table table(4, 5);
lookup_tables[table.table_name] = std::make_shared<test_lookup_table>(table);
lookup_table_ids[table.table_name + "/full"] = 1;

const std::size_t rows_amount = pack_lookup_tables_horizontal<FieldType, ArithmetizationParams>(
lookup_table_ids,
lookup_tables,
bp,
assignment,
constant_columns_ids,
0,
2);
// Check that the folding scheme worked correctly
const auto &values = table.get_table();
const auto &constants = assignment.constants();
for (std::size_t column = 0; column < table.get_columns_number(); column++) {
for (std::size_t row = 0; row < table.get_rows_number(); row++) {
const std::size_t assignment_row = 1 + row % 2;
const std::size_t assignment_column = column + row / 2 * table.columns_amount;
BOOST_CHECK_EQUAL(constants[assignment_column][assignment_row], values[column][row]);
}
}
// Check that the selector is ther
const auto &selectors = assignment.selectors();
BOOST_CHECK_EQUAL(selectors[1][0], 0);
for (std::size_t row = 1; row < 3; row++) {
BOOST_CHECK_EQUAL(selectors[1][row], 1);
}
}

BOOST_AUTO_TEST_SUITE_END()