Skip to content

Commit

Permalink
Merge pull request #53 from NilFoundation/solana-circuits
Browse files Browse the repository at this point in the history
Solana circuits
  • Loading branch information
nkaskov authored Aug 19, 2022
2 parents 1d7e5e4 + 43d2b32 commit b8eac05
Show file tree
Hide file tree
Showing 14 changed files with 997 additions and 273 deletions.
216 changes: 141 additions & 75 deletions include/nil/crypto3/zk/components/hashes/sha256/plonk/sha512.hpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace nil {
assignment.witness(W6)[i + 2] = sigma0_chunks[0][4];
assignment.witness(W7)[i + 2] = message_scheduling_words[(i - row) / 6 + 9];
assignment.witness(W8)[i + 2] = message_scheduling_words[(i - row) / 6];

typename CurveType::base_field_type::integral_type integral_b =
typename CurveType::base_field_type::integral_type(
message_scheduling_words[(i - row) / 6 + 14].data);
Expand Down Expand Up @@ -310,7 +310,7 @@ namespace nil {
typename CurveType::base_field_type::integral_type(typename CurveType::base_field_type::value_type(2).pow(64).data);
assignment.witness(W5)[i + 3] = message_scheduling_words[(i - row) / 6 + 16];
assignment.witness(W6)[i + 3] = (sum - message_scheduling_words[(i - row) / 6 + 16]) /
typename CurveType::base_field_type::integral_type(typename CurveType::base_field_type::value_type(2).pow(64).data);
typename CurveType::base_field_type::integral_type(typename CurveType::base_field_type::value_type(2).pow(64).data);
}
row = row + 384;
for (std::size_t i = row; i < row + 720; i = i + 9) {
Expand Down Expand Up @@ -364,9 +364,9 @@ namespace nil {
assignment.witness(W4)[i + 2] = Sigma1_chunks[0][4];
typename CurveType::base_field_type::integral_type Sigma1 =
Sigma1_chunks[0][0] + Sigma1_chunks[0][1] * (1 << (sigma_sizes[0])) +
Sigma1_chunks[0][2] * (1 << (sigma_sizes[0] + sigma_sizes[1])) +
Sigma1_chunks[0][3] * (1 << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2])) +
Sigma1_chunks[0][4] * (1 << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2] + sigma_sizes[3]));
Sigma1_chunks[0][2] * (one << (sigma_sizes[0] + sigma_sizes[1])) +
Sigma1_chunks[0][3] * (one << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2])) +
Sigma1_chunks[0][4] * (one << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2] + sigma_sizes[3]));


sparse_values[4] = typename CurveType::base_field_type::integral_type((e_chunks[1][0] +
Expand Down Expand Up @@ -460,9 +460,9 @@ namespace nil {

typename CurveType::base_field_type::integral_type Sigma0 =
Sigma0_chunks[0][0] + Sigma0_chunks[0][1] * (1 << sigma_sizes[0]) +
Sigma0_chunks[0][2] * (1 << (sigma_sizes[0] + sigma_sizes[1])) +
Sigma0_chunks[0][3] * (1 << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2])) +
Sigma0_chunks[0][4] * (1 << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2] + sigma_sizes[3]));
Sigma0_chunks[0][2] * (one << (sigma_sizes[0] + sigma_sizes[1])) +
Sigma0_chunks[0][3] * (one << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2])) +
Sigma0_chunks[0][4] * (one << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2] + sigma_sizes[3]));

sparse_values[0] = typename CurveType::base_field_type::integral_type((a_chunks[1][0] + a_chunks[1][1] * base4_value.pow(a_sizes[0]) +
a_chunks[1][2] * base4_value.pow(a_sizes[0] + a_sizes[1]) +
Expand Down Expand Up @@ -695,9 +695,9 @@ namespace nil {
var(W1, +1) -
(var(W8, 0) + var(W0, +1) +
var(W0, -1) + var(W1, -1) * (1 << (sigma_sizes[0])) +
var(W2, -1) * (1 << (sigma_sizes[0] + sigma_sizes[1])) +
var(W3, -1) * (1 << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2])) +
var(W4, -1) * (1 << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2] + sigma_sizes[3])) +
var(W2, -1) * (one << (sigma_sizes[0] + sigma_sizes[1])) +
var(W3, -1) * (one << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2])) +
var(W4, -1) * (one << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2] + sigma_sizes[3])) +
var(W2, 0) + var(W3, 0) * (1 << 16) +
var(W4, 0) * (one << 32) + var(W5, 0) * (one << 48) +
var(W0, 0, true, var::column_type::constant)));
Expand All @@ -713,9 +713,9 @@ namespace nil {
var(W7, 0) + m*var(W8, 0)-
(var(W1, -1) +
var(W0, +1) + var(W1, +1) * (1 << sigma_sizes[0]) +
var(W2, +1) * (1 << (sigma_sizes[0] + sigma_sizes[1])) +
var(W3, +1) * (1 << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2])) +
var(W4, +1) * (1 << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2] + sigma_sizes[3])) +
var(W2, +1) * (one << (sigma_sizes[0] + sigma_sizes[1])) +
var(W3, +1) * (one << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2])) +
var(W4, +1) * (one << (sigma_sizes[0] + sigma_sizes[1] + sigma_sizes[2] + sigma_sizes[3])) +
var(W2, 0) + var(W3, 0) * (1 << 16) +
var(W4, 0) * (one << 32) + var(W5, 0) * (one << 48)));
auto constraint_5 = bp.add_constraint((var(W8, 0) - 6) * (var(W8, 0) - 5) *
Expand Down Expand Up @@ -870,6 +870,51 @@ namespace nil {
blueprint_public_assignment_table<ArithmetizationType> &assignment,
const params_type &params,
const std::size_t &start_row_index) {

std::size_t row = start_row_index + 2;

for (std::size_t i = 1; i <= 15; ++i) {
bp.add_copy_constraint({var(W0, row + (i - 1)*6 + 0, false), params.input_words[i]});
}
for (std::size_t i = 9; i <= 15; ++i) {
bp.add_copy_constraint({var(W7, row + (i - 9)*6 + 2, false), params.input_words[i]});
}
for (std::size_t i = 0; i <= 15; ++i) {
bp.add_copy_constraint({var(W8, row + (i - 0)*6 + 2, false), params.input_words[i]});
}
for (std::size_t i = 14; i <= 15; ++i) {
bp.add_copy_constraint({var(W0, row + (i - 14)*6 + 5, false), params.input_words[i]});
}

row = row + 384;

bp.add_copy_constraint({var(W6, row + 2, false), var(W5, start_row_index + 1)});
bp.add_copy_constraint({var(W6, row + 3, false), var(W6, start_row_index + 1)});
bp.add_copy_constraint({var(W6, row + 6, false), var(W1, start_row_index + 1)});
bp.add_copy_constraint({var(W6, row + 5, false), var(W2, start_row_index + 1)});

for (std::size_t i = row; i < row + 720 - 9; i = i + 9){
bp.add_copy_constraint ({var(W6, (i + 2) + 9, false), var(W5, (i + 2), false)});
bp.add_copy_constraint ({var(W6, (i + 3) + 9, false), var(W6, (i + 2), false)});
bp.add_copy_constraint ({var(W6, (i + 5) + 9, false), var(W6, (i + 6), false)});
bp.add_copy_constraint ({var(W6, (i + 6) + 9, false), var(W5, (i + 6), false)});
}

bp.add_copy_constraint({var(W0, row + 8, false), params.input_state[0]});
bp.add_copy_constraint({var(W7, row + 3, false), params.input_state[3]});
bp.add_copy_constraint({var(W0, row + 0, false), params.input_state[4]});
bp.add_copy_constraint({var(W8, row + 3, false), params.input_state[7]});

row = row + 720;

bp.add_copy_constraint({var(W0, row, false), params.input_state[0]});
bp.add_copy_constraint({var(W1, row, false), params.input_state[1]});
bp.add_copy_constraint({var(W2, row, false), params.input_state[2]});
bp.add_copy_constraint({var(W3, row, false), params.input_state[3]});
bp.add_copy_constraint({var(W0, row + 2, false), params.input_state[4]});
bp.add_copy_constraint({var(W1, row + 2, false), params.input_state[5]});
bp.add_copy_constraint({var(W2, row + 2, false), params.input_state[6]});
bp.add_copy_constraint({var(W3, row + 2, false), params.input_state[7]});
}
static void
generate_assignments_constant(blueprint<ArithmetizationType> &bp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace nil {
public:
constexpr static const std::size_t rows_amount =
2 * non_native_range_component::rows_amount + 8 * multiplication_component::rows_amount +
3 * addition_component::rows_amount + subtraction_component::rows_amount + 2;
3 * addition_component::rows_amount + subtraction_component::rows_amount;

constexpr static const std::size_t gates_amount = 0;

Expand Down Expand Up @@ -176,7 +176,6 @@ namespace nil {
assignment.witness(W2)[row] = x3[2];
assignment.witness(W3)[row] = x3[3];
std::array<var, 4> P_x = {var(W0, row), var(W1, row), var(W2, row), var(W3, row)};
row++;

typename non_native_range_component::params_type range_params_x3 = {P_x};
non_native_range_component::generate_assignments(assignment, range_params_x3, row);
Expand All @@ -187,7 +186,6 @@ namespace nil {
assignment.witness(W2)[row] = y3[2];
assignment.witness(W3)[row] = y3[3];
std::array<var, 4> P_y = {var(W0, row), var(W1, row), var(W2, row), var(W3, row)};
row++;

typename non_native_range_component::params_type range_params_y3 = {P_y};
non_native_range_component::generate_assignments(assignment, range_params_y3, row);
Expand Down Expand Up @@ -255,14 +253,12 @@ namespace nil {
}
std::size_t row = start_row_index;
std::array<var, 4> P_x = {var(W0, row), var(W1, row), var(W2, row), var(W3, row)};
row++;

typename non_native_range_component::params_type range_params_x3 = {P_x};
non_native_range_component::generate_circuit(bp, assignment, range_params_x3, row);
row+=non_native_range_component::rows_amount;

std::array<var, 4> P_y = {var(W0, row), var(W1, row), var(W2, row), var(W3, row)};
row++;

typename non_native_range_component::params_type range_params_y3 = {P_y};
non_native_range_component::generate_circuit(bp, assignment, range_params_y3, row);
Expand Down Expand Up @@ -333,7 +329,6 @@ namespace nil {
const std::size_t start_row_index) {
std::size_t row = start_row_index;

row+=2;
row+=non_native_range_component::rows_amount;
row+=non_native_range_component::rows_amount;
row+=multiplication_component::rows_amount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ namespace nil {
row += mult_component::rows_amount;
auto right = add_component::generate_assignments(assignment, {one_var, t2}, row).output;
row += add_component::rows_amount;

for (int i = 0; i < 4; ++i) {
std::cout << assignment.var_value(left[i]).data << ' ' << assignment.var_value(right[i]).data << '\n';
}

return result_type(component_start_row);
}

Expand Down Expand Up @@ -204,14 +199,12 @@ namespace nil {
auto t0 = mult_component::generate_circuit(bp, assignment, {x_2, a_var}, row).output;
row += mult_component::rows_amount;
auto left = add_component::generate_circuit(bp, assignment, {y_2, t0}, row).output;
std::cout << row << '\n';
row += add_component::rows_amount;
auto t1 = mult_component::generate_circuit(bp, assignment, {y_2, x_2}, row).output;
row += mult_component::rows_amount;
auto t2 = mult_component::generate_circuit(bp, assignment, {d_var, t1}, row).output;
row += mult_component::rows_amount;
auto right = add_component::generate_circuit(bp, assignment, {one_var, t2}, row).output;
std::cout << row << '\n';
row += add_component::rows_amount;

generate_copy_constraints(bp, assignment, params, start_row_index);
Expand All @@ -234,9 +227,9 @@ namespace nil {
const params_type &params,
std::size_t component_start_row) {
std::size_t row = component_start_row + 2 * non_native_range_component::rows_amount + 3 * mult_component::rows_amount;
auto left = (typename add_component::result_type(25)).output;
auto left = (typename add_component::result_type(component_start_row + 25)).output;
row += 2 * mult_component::rows_amount + add_component::rows_amount;
auto right = (typename add_component::result_type(43)).output;
auto right = (typename add_component::result_type(component_start_row + 43)).output;

bp.add_copy_constraint({left[0], right[0]});
bp.add_copy_constraint({left[1], right[1]});
Expand Down
Loading

0 comments on commit b8eac05

Please sign in to comment.