Skip to content

Commit

Permalink
allocated_data for components #16
Browse files Browse the repository at this point in the history
  • Loading branch information
SK0M0R0H committed Apr 14, 2022
1 parent 8bbe4ca commit 8910fea
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
38 changes: 38 additions & 0 deletions include/nil/crypto3/zk/assignment/plonk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,30 @@ namespace nil {
return selector_index - 1;
}

void enable_selector(std::size_t selector_index, const std::vector<std::size_t> &&row_indices) {
assert(selector_index < this->selector_columns.size());

for (std::size_t row_index : row_indices) {
this->selector_columns[selector_index][row_index] = BlueprintFieldType::value_type::one();
}
}

void enable_selector(std::size_t selector_index, std::size_t row_index) {
assert(selector_index < this->selector_columns.size());

enable_selector(selector_index, std::vector<std::size_t>({row_index}));
}

void
enable_selector(std::size_t selector_index, std::size_t begin_row_index, std::size_t end_row_index, std::size_t index_step = 1) {

assert(selector_index < this->selector_columns.size());

for (std::size_t row_index = begin_row_index; row_index <= end_row_index; row_index += index_step) {
this->selector_columns[selector_index][row_index] = BlueprintFieldType::value_type::one();
}
}

snark::plonk_column<BlueprintFieldType> &public_input(std::size_t public_input_index) {
assert(public_input_index < this->public_input_columns.size());
this->public_input_columns[public_input_index].resize(_table_description.rows_amount);
Expand Down Expand Up @@ -324,6 +348,20 @@ namespace nil {
return _public_assignment.add_selector(begin_row_index, end_row_index, index_step);
}

void enable_selector(std::size_t selector_index, const std::vector<std::size_t> &&row_indices) {
_public_assignment.enable_selector(selector_index, std::move(row_indices));
}

void enable_selector(std::size_t selector_index, std::size_t row_index) {
_public_assignment.enable_selector(selector_index, row_index);
}

void
enable_selector(std::size_t selector_index, std::size_t begin_row_index, std::size_t end_row_index, std::size_t index_step = 1) {

_public_assignment.enable_selector(selector_index, begin_row_index, end_row_index, index_step);
}

snark::plonk_column<BlueprintFieldType> &public_input(std::size_t public_input_index) {
return _public_assignment.public_input(public_input_index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ namespace nil {
var endo_scalar;
};

struct allocated_data_type {
allocated_data_type() {
previously_allocated = false;
}

// TODO access modifiers
bool previously_allocated;
std::size_t selector_1;
std::size_t selector_2;
};

static std::size_t allocate_rows (blueprint<ArithmetizationType> &in_bp){
return in_bp.allocate_rows(required_rows_amount);
}
Expand All @@ -99,9 +110,10 @@ namespace nil {
blueprint<ArithmetizationType> &bp,
blueprint_assignment_table<ArithmetizationType> &assignment,
const params_type &params,
allocated_data_type &allocated_data,
const std::size_t &component_start_row) {

generate_gates(bp, assignment, params, component_start_row);
generate_gates(bp, assignment, params, allocated_data, component_start_row);
generate_copy_constraints(bp, assignment, params, component_start_row);
}

Expand Down Expand Up @@ -172,13 +184,26 @@ namespace nil {
static void generate_gates(blueprint<ArithmetizationType> &bp,
blueprint_assignment_table<ArithmetizationType> &assignment,
const params_type &params,
allocated_data_type &allocated_data,
const std::size_t &component_start_row = 0) {

const std::size_t &j = component_start_row;
using F = typename BlueprintFieldType::value_type;

std::size_t selector_index_1 = assignment.add_selector(j, j + required_rows_amount - 1);
std::size_t selector_index_2 = assignment.add_selector(j + required_rows_amount - 1);
std::size_t selector_index_1;
std::size_t selector_index_2;
if (!allocated_data.previously_allocated) {
selector_index_1 = assignment.add_selector(j, j + required_rows_amount - 1);
selector_index_2 = assignment.add_selector(j + required_rows_amount - 1);
allocated_data.previously_allocated = true;
allocated_data.selector_1 = selector_index_1;
allocated_data.selector_2 = selector_index_2;
} else {
selector_index_1 = allocated_data.selector_1;
selector_index_2 = allocated_data.selector_2;
assignment.enable_selector(selector_index_1, j, j + required_rows_amount - 1);
assignment.enable_selector(selector_index_2, j + required_rows_amount - 1);
}

auto c_f = [](var x) {
return (F(11) * F(6).inversed()) * x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ namespace nil {
var_ec_point Q;
};

struct allocated_data_type {
allocated_data_type() {
previously_allocated = false;
}

// TODO access modifiers
bool previously_allocated;
std::size_t add_selector;
};

static std::size_t allocate_rows (blueprint<ArithmetizationType> &bp,
std::size_t components_amount = 1){
return bp.allocate_rows(required_rows_amount *
Expand All @@ -98,9 +108,10 @@ namespace nil {
blueprint<ArithmetizationType> &bp,
blueprint_assignment_table<ArithmetizationType> &assignment,
const params_type &params,
allocated_data_type &allocated_data,
const std::size_t &component_start_row) {

generate_gates(bp, assignment, params, component_start_row);
generate_gates(bp, assignment, params, allocated_data, component_start_row);
generate_copy_constraints(bp, assignment, params, component_start_row);
}

Expand Down Expand Up @@ -168,9 +179,18 @@ namespace nil {
blueprint<ArithmetizationType> &bp,
blueprint_assignment_table<ArithmetizationType> &assignment,
const params_type &params,
allocated_data_type &allocated_data,
const std::size_t row_start_index) {

std::size_t selector_index = assignment.add_selector(row_start_index);
std::size_t selector_index;
if (!allocated_data.previously_allocated) {
selector_index = assignment.add_selector(row_start_index);
allocated_data.previously_allocated = true;
allocated_data.add_selector = selector_index;
} else {
selector_index = allocated_data.add_selector;
assignment.enable_selector(selector_index, row_start_index);
}

auto constraint_1 = bp.add_constraint(
var(W7, 0) * (var(W2, 0) - var(W0, 0)));
Expand Down
3 changes: 2 additions & 1 deletion test/test_plonk_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ namespace nil {
auto allocated_pi = assignment_bp.allocate_public_input(public_input[i]);
}

component_type::generate_circuit(bp, assignment_bp, params, start_row);
typename component_type::allocated_data_type allocated_data;
component_type::generate_circuit(bp, assignment_bp, params, allocated_data, start_row);
component_type::generate_assignments(assignment_bp, params,
start_row);

Expand Down

0 comments on commit 8910fea

Please sign in to comment.