Skip to content

Commit

Permalink
Dynamic lookups implemented #283
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Aug 6, 2024
1 parent 16f94a3 commit 8fb1f02
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,16 @@ namespace nil {
, _public_table(public_inputs_amount, constants_amount, selectors_amount) {
}

const ColumnType& get_variable_value_without_rotation(const VariableType& var) const {
template <typename InputVariableType>
const ColumnType& get_variable_value_without_rotation(const InputVariableType& var) const {
switch (var.type) {
case VariableType::column_type::witness:
case InputVariableType::column_type::witness:
return witness(var.index);
case VariableType::column_type::public_input:
case InputVariableType::column_type::public_input:
return public_input(var.index);
case VariableType::column_type::constant:
case InputVariableType::column_type::constant:
return constant(var.index);
case VariableType::column_type::selector:
case InputVariableType::column_type::selector:
return selector(var.index);
default:
std::cerr << "Invalid column type" << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ namespace nil {
virtual ~lookup_table_definition() {};
};

template<typename FieldType>
class dynamic_table_definition {
protected:
bool defined;
public:
plonk_lookup_table<FieldType> lookup_table;
std::string name;

dynamic_table_definition(std::string _name): name(_name), defined(false) {}

void define(const plonk_lookup_table<FieldType> &table){
BOOST_ASSERT(!defined);
lookup_table = table;
defined = true;
}
bool is_defined(){
return defined;
}
virtual ~dynamic_table_definition() {};
};

template<typename FieldType>
std::vector<std::string>
get_tables_ordered_by_rows_number(const std::map<std::string, std::shared_ptr<lookup_table_definition<FieldType>>> &tables){
Expand Down Expand Up @@ -107,12 +128,12 @@ namespace nil {
std::size_t pack_lookup_tables(
const TableIdsMapType &lookup_table_ids,
const std::map<std::string, std::shared_ptr<lookup_table_definition<FieldType>>> &lookup_tables,
const std::map<std::string, std::shared_ptr<dynamic_table_definition<FieldType>>> &dynamic_tables,
plonk_constraint_system<FieldType> &bp,
plonk_assignment_table<FieldType> &assignment,
const std::vector<std::size_t> &constant_columns_ids,
std::size_t usable_rows
){
// std::cout << "Packing lookup tables" << std::endl;
// std::cout << "Usable rows before: " << usable_rows << std::endl;
std::size_t usable_rows_after = usable_rows;

Expand Down Expand Up @@ -176,6 +197,10 @@ namespace nil {
}
start_row += table->get_rows_number();
}
for( const auto&[k, table]:dynamic_tables ){
BOOST_ASSERT(table->is_defined());
bp_lookup_tables[lookup_table_ids.at(k) - 1] = table->lookup_table;
}
for(std::size_t i = 0; i < bp_lookup_tables.size(); i++){
bp.add_lookup_table(std::move(bp_lookup_tables[i]));
}
Expand All @@ -190,6 +215,7 @@ namespace nil {
std::size_t pack_lookup_tables_horizontal(
const LookupTableIds &lookup_table_ids,
const std::map<std::string, std::shared_ptr<lookup_table_definition<FieldType>>> &lookup_tables,
const std::map<std::string, std::shared_ptr<dynamic_table_definition<FieldType>>> &dynamic_tables,
plonk_constraint_system<FieldType> &bp,
plonk_assignment_table<FieldType> &assignment,
const std::vector<std::size_t> &constant_columns_ids,
Expand Down Expand Up @@ -330,6 +356,10 @@ namespace nil {
}
start_row += table->get_rows_number();
}
for( const auto&[k, table]:dynamic_tables ){
BOOST_ASSERT(table->is_defined());
bp_lookup_tables[lookup_table_ids.at(k) - 1] = table->lookup_table;
}
for(std::size_t i = 0; i < bp_lookup_tables.size(); i++){
bp.add_lookup_table(std::move(bp_lookup_tables[i]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ namespace nil {
prepare_lookup_input();
auto& lookup_input = *lookup_input_ptr;


// 3. Lookup_input and lookup_value are ready
// Now sort them!
// Reduce value and input:
Expand All @@ -184,6 +185,7 @@ namespace nil {
for( std::size_t i = 0; i < lookup_input.size(); i++ ){
reduced_input.push_back(reduce_dfs_polynomial_domain(lookup_input[i], basic_domain->m));
}

// Sort
auto sorted = sort_polynomials(reduced_input, reduced_value, basic_domain->m,
preprocessed_data.common_data.desc.usable_rows_amount);
Expand Down Expand Up @@ -419,7 +421,9 @@ namespace nil {
math::polynomial_dfs<typename FieldType::value_type> v = (typename FieldType::value_type(t_id + 1)) * lookup_tag;
theta_acc = theta;
for (std::size_t i = 0; i < l_table.columns_number; i++) {
v += theta_acc * lookup_tag * plonk_columns.constant(l_table.lookup_options[o_id][i].index);
math::polynomial_dfs<typename FieldType::value_type> c;
c = plonk_columns.get_variable_value_without_rotation(l_table.lookup_options[o_id][i]);
v += theta_acc * lookup_tag * c;
theta_acc *= theta;
}
v *= mask_assignment;
Expand Down Expand Up @@ -464,7 +468,7 @@ namespace nil {
);
visitor.visit(expr);

math::cached_expression_evaluator<DfsVariableType> evaluator(expr,
math::cached_expression_evaluator<DfsVariableType> evaluator(expr,
[&domain=basic_domain, &assignments=plonk_columns, &rotated_variable_values]
(const DfsVariableType &var) -> const polynomial_dfs_type& {
if (var.rotation == 0) {
Expand Down Expand Up @@ -701,8 +705,8 @@ namespace nil {
theta_acc = theta;
BOOST_ASSERT(table.lookup_options[o_id].size() == table.columns_number);
for( std::size_t i = 0; i < table.lookup_options[o_id].size(); i++){
auto key1 = std::tuple(table.lookup_options[o_id][i].index, 0, plonk_variable<typename FieldType::value_type>::column_type::constant);
auto shifted_key1 = std::tuple(table.lookup_options[o_id][i].index, 1, plonk_variable<typename FieldType::value_type>::column_type::constant);
auto key1 = std::tuple(table.lookup_options[o_id][i].index, 0, table.lookup_options[o_id][i].type);
auto shifted_key1 = std::tuple(table.lookup_options[o_id][i].index, 1, table.lookup_options[o_id][i].type);
v += theta_acc * evaluations[key1] * selector_value;
shifted_v += theta_acc * evaluations[shifted_key1]* shifted_selector_value;
theta_acc *= theta;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ namespace nil {
const plonk_constraint_system<FieldType> &constraint_system,
const plonk_table_description<FieldType> &table_description
) {
using var = plonk_variable<typename FieldType::value_type>;
std::vector<std::set<int>> result(table_description.table_width());

for (auto & s : result) {
Expand Down Expand Up @@ -402,11 +403,23 @@ namespace nil {
].insert(1);
for( const auto &option:table.lookup_options){
for( const auto &column:option){
result[
table_description.witness_columns +
table_description.public_input_columns +
column.index
].insert(1);
switch( column.type ){
case var::column_type::witness:
result[column.index].insert(1);
break;
case var::column_type::public_input:
result[ table_description.witness_columns + column.index].insert(1);
break;
case var::column_type::constant:
result[ table_description.witness_columns + table_description.public_input_columns + column.index ].insert(1);
break;
case var::column_type::selector:
result[ table_description.witness_columns + table_description.public_input_columns + table_description.constant_columns + column.index].insert(1);
break;
case var::column_type::uninitialized:
break;
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ namespace nil {

// 8. Run evaluation proofs
_proof.eval_proof.challenge = transcript.template challenge<FieldType>();

generate_evaluation_points();

{
Expand Down Expand Up @@ -283,7 +282,7 @@ namespace nil {
}

typename placeholder_lookup_argument_prover<FieldType, commitment_scheme_type, ParamsType>::prover_lookup_result
lookup_argument() {
lookup_argument() {
PROFILE_PLACEHOLDER_SCOPE("lookup_argument_time");

typename placeholder_lookup_argument_prover<
Expand Down

0 comments on commit 8fb1f02

Please sign in to comment.