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

Keccak component #334

Draft
wants to merge 48 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
de7524e
raw round component #183
tshchelovek Jul 18, 2023
fb701f3
calculated configurations #183
tshchelovek Jul 26, 2023
75d0155
gates calculation #183
tshchelovek Jul 27, 2023
7991a45
gates added #183
tshchelovek Jul 28, 2023
4a9c312
fixed some computation bugs #183
tshchelovek Aug 6, 2023
97a2204
fixed calculations #183
tshchelovek Aug 9, 2023
62a49fc
padding component #183
tshchelovek Aug 16, 2023
0da0733
less constraints update #183
tshchelovek Aug 29, 2023
b3db4fb
some round constr fized, code to padding added #183
tshchelovek Sep 4, 2023
b5f2c91
fixed some constraints #183
tshchelovek Sep 11, 2023
0c7d7b9
finished round (no lookups) #183
tshchelovek Oct 2, 2023
956b66e
added code to main component #183
tshchelovek Oct 12, 2023
a76bba3
some updates #183
tshchelovek Oct 16, 2023
63fb978
minor changes #183
tshchelovek Oct 18, 2023
43d8db6
updates for gate_manifest #183
tshchelovek Oct 23, 2023
59c189e
manifest and lookup updates #183
tshchelovek Oct 26, 2023
b0e5aa6
minor changes #183
tshchelovek Nov 2, 2023
8ee021c
deleted junk file
tshchelovek Nov 2, 2023
fe9ecba
keccak padding component fixes
Nov 10, 2023
543049b
keccak padding witness_amount=15 fixed
Nov 15, 2023
88bfb90
added lookups to keccak_round #183
tshchelovek Nov 18, 2023
b5370c8
added padding lookups #183
tshchelovek Nov 20, 2023
e98fb44
padding fixes #183
tshchelovek Nov 20, 2023
257bc31
pack table in lookup lib #183
tshchelovek Nov 23, 2023
f23e91b
keccak padding almost done #183
tshchelovek Nov 28, 2023
2d2c926
finished padding ver1 #183
tshchelovek Nov 29, 2023
0a24b3d
padding added shift constraint #183
tshchelovek Dec 11, 2023
e5deea7
keccak gates #183
tshchelovek Dec 11, 2023
2ff5655
keccak round lookup table fix
Dec 8, 2023
ea5de69
fixed keccak_round lookups # 183
tshchelovek Dec 14, 2023
66be6a9
added gates #183
tshchelovek Dec 20, 2023
f1240d0
all vars update
tshchelovek Dec 20, 2023
7c019e1
Updated the components for arithmetization changes.
Iluvmagick Feb 20, 2024
ca2c624
keccak component updates
Valeh2012 Apr 30, 2024
e40594c
Master rebase + warning fixes.
Iluvmagick Apr 30, 2024
e6622b4
run_tests.yml rebase fix.
Iluvmagick Apr 30, 2024
d6312e5
keccak connectedness attempt
Valeh2012 Apr 30, 2024
ece7eb6
Fixed out-of-memory errors in keccak round.
Iluvmagick Apr 30, 2024
0929ce0
WIP saving progress.
Iluvmagick Apr 30, 2024
019904f
Moved configuration out of keccak files.
Iluvmagick Apr 30, 2024
73838c3
keccak connectedness solved
Valeh2012 May 7, 2024
79d876d
constraints updates, don't work yet
Shatooon Jun 20, 2024
9a6224f
Update keccak_round.hpp
tshchelovek Jun 27, 2024
baa4103
Added constraints to keccak_padding
tshchelovek Jul 26, 2024
f4478e9
uncommented keccak_padding.cpp
tshchelovek Jul 26, 2024
d627f77
keccak_padding without templates, not finished
tshchelovek Aug 15, 2024
7d4d35b
cmakelists for no template
tshchelovek Aug 15, 2024
4c4b87c
functions for bytes to bit chunks added
tshchelovek Aug 15, 2024
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
6 changes: 5 additions & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ jobs:
blueprint_verifiers_flexible_multiplications_test,
blueprint_verifiers_flexible_poseidon_test,
blueprint_verifiers_flexible_constant_pow_test,
blueprint_verifiers_placeholder_verifier_test
blueprint_verifiers_placeholder_verifier_test,
blueprint_hashes_plonk_keccak_round_test,
blueprint_hashes_plonk_keccak_padding_test,
blueprint_hashes_plonk_keccak_test,
blueprint_proxy_test
] # Tests to execute
include: # Abused to enable proof generation for some tests; add more as needed
- target: blueprint_algebra_fields_plonk_non_native_logic_ops_test
Expand Down
1,018 changes: 1,018 additions & 0 deletions include/nil/blueprint/components/hashes/keccak/keccak_component.hpp

Large diffs are not rendered by default.

1,299 changes: 1,299 additions & 0 deletions include/nil/blueprint/components/hashes/keccak/keccak_padding.hpp

Large diffs are not rendered by default.

1,334 changes: 1,334 additions & 0 deletions include/nil/blueprint/components/hashes/keccak/keccak_padding_no_template.hpp

Large diffs are not rendered by default.

2,358 changes: 2,358 additions & 0 deletions include/nil/blueprint/components/hashes/keccak/keccak_round.hpp

Large diffs are not rendered by default.

110 changes: 110 additions & 0 deletions include/nil/blueprint/configuration.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2023 Polina Chernyshova <[email protected]>
// 2024 Valeh Farzaliyev <[email protected]>
//
// 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.
//---------------------------------------------------------------------------//

#pragma once

#include <vector>
#include <string>

namespace nil {
namespace blueprint {
struct configuration {
struct coordinates {
std::size_t row;
std::size_t column;

coordinates() = default;
coordinates(std::size_t row_, std::size_t column_) : row(row_), column(column_) {};
coordinates(std::pair<std::size_t, std::size_t> pair) : row(pair.first), column(pair.second) {};

bool operator==(const coordinates &other) const {
return row == other.row && column == other.column;
}

bool operator<(const coordinates &other) const {
return row < other.row || (row == other.row && column < other.column);
}
};

// In constraints we use such notation: constr[0] - result,
// constr[1]... - arguments for lookup, linear elements for regular constraints in correct order.
coordinates first_coordinate;
coordinates last_coordinate;
std::vector<coordinates> copy_to;
std::vector<std::vector<coordinates>> constraints;
std::vector<std::vector<coordinates>> lookups;
coordinates copy_from;
std::string name;

configuration() = default;
configuration(
std::pair<std::size_t, std::size_t>
first_coordinate_,
std::pair<std::size_t, std::size_t>
last_coordinate_,
std::vector<std::pair<std::size_t, std::size_t>>
copy_to_,
std::vector<std::vector<std::pair<std::size_t, std::size_t>>>
constraints_,
std::vector<std::vector<std::pair<std::size_t, std::size_t>>>
lookups_,
std::pair<std::size_t, std::size_t>
copy_from_
) {
first_coordinate = coordinates(first_coordinate_);
last_coordinate = coordinates(last_coordinate_);
for (std::size_t i = 0; i < copy_to_.size(); ++i) {
copy_to.push_back(coordinates(copy_to_[i]));
}
for (std::size_t i = 0; i < constraints_.size(); ++i) {
std::vector<coordinates> constr;
for (std::size_t j = 0; j < constraints_[i].size(); ++j) {
constr.push_back(coordinates(constraints_[i][j]));
}
constraints.push_back(constr);
}
for (std::size_t i = 0; i < lookups_.size(); ++i) {
std::vector<coordinates> lookup;
for (std::size_t j = 0; j < lookups_[i].size(); ++j) {
lookup.push_back(coordinates(lookups_[i][j]));
}
lookups.push_back(lookup);
}
copy_from = coordinates(copy_from_);
};

bool operator==(const configuration &other) const {
return first_coordinate == other.first_coordinate && last_coordinate == other.last_coordinate &&
copy_to == other.copy_to && constraints == other.constraints &&
lookups == other.lookups && copy_from == other.copy_from;
}

bool operator<(const configuration &other) const {
return first_coordinate < other.first_coordinate ||
(first_coordinate == other.first_coordinate && last_coordinate < other.last_coordinate);
}
};
} // namespace blueprint
} // namespace nil
191 changes: 191 additions & 0 deletions include/nil/blueprint/lookup_library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,43 @@ namespace nil {
virtual std::size_t get_rows_number(){ return 4; }
};

class keccak_pack_table_type : public lookup_table_definition{
typename BlueprintFieldType::value_type to_sparse(typename BlueprintFieldType::value_type value) {
using value_type = typename BlueprintFieldType::value_type;
using integral_type = typename BlueprintFieldType::integral_type;
integral_type value_integral = integral_type(value.data);
integral_type result_integral = 0;
integral_type power = 1;
for (int i = 0; i < 64; ++i) {
integral_type bit = value_integral & 1;
result_integral = result_integral + bit * power;
value_integral = value_integral >> 1;
power = power << 3;
}
return value_type(result_integral);
}
public:
keccak_pack_table_type(): lookup_table_definition("keccak_pack_table"){
this->subtables["full"] = {{0,1}, 0, 255};
this->subtables["range_check"] = {{0}, 0, 255};
this->subtables["range_check_sparse"] = {{1}, 0, 255};
}
virtual void generate(){
this->_table.resize(2);

for (typename BlueprintFieldType::integral_type i = 0;
i < typename BlueprintFieldType::integral_type(256);
i++
) {
this->_table[0].push_back(i);
this->_table[1].push_back(to_sparse(i));
}
}
virtual std::size_t get_columns_number(){ return 2; }
virtual std::size_t get_rows_number(){ return 256; }
};
protected:

class binary_and_table_type : public lookup_table_definition{
public:
binary_and_table_type(): lookup_table_definition("binary_and_table"){
Expand Down Expand Up @@ -244,6 +281,154 @@ namespace nil {
virtual std::size_t get_columns_number(){return 2;}
virtual std::size_t get_rows_number(){return 5764801;}
};

class sparse_values_base8_table : public lookup_table_definition{
typename BlueprintFieldType::value_type to_sparse(typename BlueprintFieldType::value_type value) {
using value_type = typename BlueprintFieldType::value_type;
using integral_type = typename BlueprintFieldType::integral_type;
integral_type value_integral = integral_type(value.data);
integral_type result_integral = 0;
integral_type power = 1;
for (int i = 0; i < 64; ++i) {
integral_type bit = value_integral & 1;
result_integral = result_integral + bit * power;
value_integral = value_integral >> 1;
power = power << 3;
}
return value_type(result_integral);
}
public:
sparse_values_base8_table(): lookup_table_definition("keccak_pack_table"){
this->subtables["full"] = {{0,1}, 0, 255};
this->subtables["range_check"] = {{0}, 0, 255};
this->subtables["range_check_sparse"] = {{1}, 0, 255};
this->subtables["64bit"] = {{0}, 128, 255};
}
virtual void generate(){
this->_table.resize(2);

for (typename BlueprintFieldType::integral_type i = 0;
i < typename BlueprintFieldType::integral_type(256);
i++
) {
this->_table[0].push_back(i);
this->_table[1].push_back(to_sparse(i));
}
}
virtual std::size_t get_columns_number(){ return 2; }
virtual std::size_t get_rows_number(){ return 256; }
};

class sparse_values_base8_sign_bit_table : public lookup_table_definition{
// "keccak_pack_table/64bit" doesn't work, so we need to use this temporary table
typename BlueprintFieldType::value_type to_sparse(typename BlueprintFieldType::value_type value) {
using value_type = typename BlueprintFieldType::value_type;
using integral_type = typename BlueprintFieldType::integral_type;
integral_type value_integral = integral_type(value.data);
integral_type result_integral = 0;
integral_type power = 1;
for (int i = 0; i < 64; ++i) {
integral_type bit = value_integral & 1;
result_integral = result_integral + bit * power;
value_integral = value_integral >> 1;
power = power << 3;
}
return value_type(result_integral);
}
public:
sparse_values_base8_sign_bit_table(): lookup_table_definition("keccak_sign_bit_table"){
this->subtables["full"] = {{0}, 0, 128};
}
virtual void generate(){
this->_table.resize(2);
this->_table[0].push_back(0);
this->_table[1].push_back(0);
for (typename BlueprintFieldType::integral_type i = 128;
i < typename BlueprintFieldType::integral_type(256);
i++
) {
this->_table[0].push_back(i);
this->_table[1].push_back(to_sparse(i));
}
}
virtual std::size_t get_columns_number(){ return 1; }
virtual std::size_t get_rows_number(){ return 129; }
};

class normalize_base8_table_type : public lookup_table_definition{
std::size_t base;
virtual std::array<typename BlueprintFieldType::integral_type, 2> to_base(std::size_t base, typename BlueprintFieldType::integral_type num) {
typename BlueprintFieldType::integral_type result = 0;
typename BlueprintFieldType::integral_type normalized_result = 0;
typename BlueprintFieldType::integral_type power = 1;
while (num > 0) {
result = result + (num % base)*power;
normalized_result = normalized_result + ((num % base) & 1)*power;
num /= base;
power <<= 3;
}
return {result, normalized_result};
}
public:
normalize_base8_table_type(std::size_t base_)
: lookup_table_definition("keccak_normalize" + std::to_string(base_) + "_table"), base(base_) {

this->subtables["full"] = {{0,1}, 0, 65535};
}

virtual void generate(){
this->_table.resize(2);
std::vector<std::size_t> value_sizes = {8};

for (typename BlueprintFieldType::integral_type i = 0;
i < typename BlueprintFieldType::integral_type(65536);
i++
) {
std::array<typename BlueprintFieldType::integral_type, 2> value = to_base(base, i);
this->_table[0].push_back(value[0]);
this->_table[1].push_back(value[1]);
}
}
virtual std::size_t get_columns_number(){ return 2; }
virtual std::size_t get_rows_number(){ return 65536; }
};

class chi_table_type : public lookup_table_definition{
virtual std::array<typename BlueprintFieldType::integral_type, 2> to_base_chi(typename BlueprintFieldType::integral_type num) {
std::size_t base = 5;
typename BlueprintFieldType::integral_type table[5] = {0, 1, 1, 0, 0};
typename BlueprintFieldType::integral_type result = 0;
typename BlueprintFieldType::integral_type chi_result = 0;
typename BlueprintFieldType::integral_type power = 1;
while (num > 0) {
result = result + (num % base) * power;
chi_result = chi_result + table[int(num % base)] * power;
num /= base;
power <<= 3;
}
return {result, chi_result};
}
public:
chi_table_type(): lookup_table_definition("keccak_chi_table") {
this->subtables["full"] = {{0,1}, 0, 65535};
}
virtual void generate(){
this->_table.resize(2);
std::vector<std::size_t> value_sizes = {8};

for (typename BlueprintFieldType::integral_type i = 0;
i < typename BlueprintFieldType::integral_type(65536);
i++
) {
std::array<typename BlueprintFieldType::integral_type, 2> value = to_base_chi(i);
this->_table[0].push_back(value[0]);
this->_table[1].push_back(value[1]);
}
}
virtual std::size_t get_columns_number(){ return 2; }
virtual std::size_t get_rows_number(){ return 65536; }
};

public:
using bimap_type = boost::bimap<boost::bimaps::set_of<std::string>, boost::bimaps::set_of<std::size_t>>;
using left_reserved_type = typename bimap_type::left_map;
Expand All @@ -260,6 +445,12 @@ namespace nil {
tables["sha256_reverse_sparse_base7"] = std::shared_ptr<lookup_table_definition>(new reverse_sparse_sigmas_base7_table());
tables["sha256_maj"] = std::shared_ptr<lookup_table_definition>(new maj_function_table());
tables["sha256_ch"] = std::shared_ptr<lookup_table_definition>(new ch_function_table());
tables["keccak_pack_table"] = std::shared_ptr<lookup_table_definition>(new sparse_values_base8_table());
tables["keccak_sign_bit_table"] = std::shared_ptr<lookup_table_definition>(new sparse_values_base8_sign_bit_table());
tables["keccak_normalize3_table"] = std::shared_ptr<lookup_table_definition>(new normalize_base8_table_type(3));
tables["keccak_normalize4_table"] = std::shared_ptr<lookup_table_definition>(new normalize_base8_table_type(4));
tables["keccak_normalize6_table"] = std::shared_ptr<lookup_table_definition>(new normalize_base8_table_type(6));
tables["keccak_chi_table"] = std::shared_ptr<lookup_table_definition>(new chi_table_type());
}

void register_lookup_table(std::shared_ptr<lookup_table_definition> table){
Expand Down
4 changes: 2 additions & 2 deletions include/nil/blueprint/utils/connectedness_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ namespace nil {
const std::size_t end_row = start_row_index + rows_amount;

nil::blueprint::assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> output_assignment(
assignment.witnesses_amount(), assignment.constants_amount(),
assignment.public_inputs_amount(), assignment.selectors_amount()
assignment.witnesses_amount(), assignment.public_inputs_amount(),
assignment.constants_amount(), assignment.selectors_amount()
);

// We do '+1' in all the assignments to separate the unassigned cells (0 by default)
Expand Down
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ set(PLONK_TESTS_FILES
"hashes/plonk/sha512"
"hashes/plonk/sha256_process"
"hashes/plonk/sha512_process"
"hashes/plonk/keccak_round"
"hashes/plonk/keccak_padding"
"hashes/plonk/keccak_padding_no_template"
"hashes/plonk/keccak"
"hashes/plonk/decomposition"
"hashes/plonk/detail/sha_table_generators_base4"
"hashes/plonk/detail/sha_table_generators_base7"
Expand Down
Loading