-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e37822
commit 977dced
Showing
11 changed files
with
161 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//---------------------------------------------------------------------------// | ||
// Elena Tatuzova | ||
// Copyright (c) 2024 Elena Tatuzova <[email protected]> | ||
// Copyright (c) 2025 Antoine Cyr <[email protected]> | ||
// | ||
// MIT License | ||
// | ||
|
@@ -23,53 +24,121 @@ | |
//---------------------------------------------------------------------------// | ||
#pragma once | ||
|
||
#include <nil/blueprint/bbf/components/hashes/keccak/keccak_dynamic.hpp> | ||
#include <nil/blueprint/bbf/generic.hpp> | ||
#include <nil/blueprint/zkevm_bbf/subcomponents/keccak_table.hpp> | ||
|
||
namespace nil { | ||
namespace blueprint { | ||
namespace bbf { | ||
template<typename FieldType, GenerationStage stage> | ||
class keccak : public generic_component<FieldType, stage> { | ||
class zkevm_keccak : public generic_component<FieldType, stage> { | ||
using typename generic_component<FieldType, stage>::context_type; | ||
using generic_component<FieldType, stage>::allocate; | ||
using generic_component<FieldType, stage>::copy_constrain; | ||
using generic_component<FieldType, stage>::constrain; | ||
using generic_component<FieldType, stage>::lookup; | ||
using generic_component<FieldType, stage>::lookup_table; | ||
|
||
public: | ||
using typename generic_component<FieldType, stage>::table_params; | ||
using typename generic_component<FieldType,stage>::TYPE; | ||
public: | ||
using KeccakDynamic = typename bbf::keccak_dynamic<FieldType, stage>; | ||
using KeccakTable = typename bbf::keccak_table<FieldType, stage>; | ||
|
||
using private_input_type = std::conditional_t< | ||
stage == GenerationStage::ASSIGNMENT, | ||
std::size_t, std::monostate | ||
>; | ||
using typename generic_component<FieldType, stage>::table_params; | ||
using typename generic_component<FieldType, stage>::TYPE; | ||
using integral_type = nil::crypto3::multiprecision::big_uint<257>; | ||
using private_input_type = | ||
typename std::conditional<stage == GenerationStage::ASSIGNMENT, | ||
zkevm_keccak_buffers, std::nullptr_t>::type; | ||
|
||
struct input_type{ | ||
struct input_type { | ||
TYPE rlc_challenge; | ||
private_input_type private_input; | ||
}; | ||
|
||
static table_params get_minimal_requirements() { | ||
static table_params get_minimal_requirements( | ||
std::size_t max_keccak_blocks) { | ||
return { | ||
.witnesses = 20, | ||
.public_inputs = 1, | ||
.constants = 3, | ||
.rows = 300 | ||
}; | ||
.rows = KeccakDynamic::get_minimal_requirements(max_keccak_blocks) | ||
.rows}; | ||
} | ||
|
||
static void allocate_public_inputs(context_type &context, input_type &input) { | ||
context.allocate(input.rlc_challenge, 0, 0, column_type::public_input); | ||
static void allocate_public_inputs(context_type& context, | ||
input_type& input, | ||
std::size_t max_keccak_blocks) { | ||
context.allocate(input.rlc_challenge, 0, 0, | ||
column_type::public_input); | ||
} | ||
|
||
keccak(context_type &context_object, const input_type &input) :generic_component<FieldType,stage>(context_object) { | ||
zkevm_keccak(context_type& context_object, const input_type& input, | ||
std::size_t max_keccak_blocks) | ||
: generic_component<FieldType, stage>(context_object) { | ||
std::vector<std::size_t> keccak_lookup_area; | ||
std::vector<std::size_t> keccak_dynamic_lookup_area; | ||
std::size_t current_column = 0; | ||
std::size_t dynamic_rows = | ||
KeccakDynamic::get_minimal_requirements(max_keccak_blocks).rows; | ||
|
||
for (std::size_t i = 0; i < KeccakTable::get_witness_amount(); i++) { | ||
keccak_lookup_area.push_back(current_column++); | ||
} | ||
|
||
for (std::size_t i = 0; | ||
i < KeccakDynamic::get_minimal_requirements(max_keccak_blocks) | ||
.witnesses; | ||
i++) { | ||
keccak_dynamic_lookup_area.push_back(current_column++); | ||
} | ||
|
||
context_type keccak_ct = context_object.subcontext( | ||
keccak_lookup_area, 1, dynamic_rows + 1); | ||
|
||
context_type keccak_dynamic_ct = context_object.subcontext( | ||
keccak_dynamic_lookup_area, 1, dynamic_rows + 1); | ||
typename KeccakDynamic::input_type input_dynamic; | ||
typename KeccakTable::input_type input_keccak_table; | ||
TYPE rlc_challenge; | ||
|
||
if constexpr (stage == GenerationStage::ASSIGNMENT) { | ||
std::cout << "Keccak assign = " << input.private_input << std::endl; | ||
rlc_challenge = input.rlc_challenge; | ||
input_dynamic.rlc_challenge = input.rlc_challenge; | ||
for (const auto& item : input.private_input.get_data()) { | ||
const auto& buffer = item.first; | ||
const auto& zkevm_word = item.second; | ||
|
||
TYPE hi = w_hi<FieldType>(zkevm_word); | ||
TYPE lo = w_lo<FieldType>(zkevm_word); | ||
std::pair<TYPE, TYPE> pair_values = {hi, lo}; | ||
|
||
input_dynamic.input.emplace_back(buffer, pair_values); | ||
} | ||
input_keccak_table.rlc_challenge = input.rlc_challenge; | ||
input_keccak_table.private_input = input.private_input; | ||
} | ||
KeccakTable kt = | ||
KeccakTable(keccak_ct, input_keccak_table, max_keccak_blocks); | ||
|
||
allocate(rlc_challenge, 0, 0); | ||
input_dynamic.rlc_challenge = rlc_challenge; | ||
KeccakDynamic kd = KeccakDynamic(keccak_dynamic_ct, input_dynamic, | ||
max_keccak_blocks); | ||
|
||
if constexpr (stage == GenerationStage::CONSTRAINTS) { | ||
std::vector<TYPE> tmp; | ||
for (std::size_t i = 0; i < max_keccak_blocks; i++) { | ||
tmp = {TYPE(1), kt.RLC[i], kt.hash_hi[i], kt.hash_lo[i], | ||
kt.is_last[i]}; | ||
lookup(tmp, "keccak_dynamic"); | ||
tmp = {kd.m[i].h.is_last, kd.m[i].h.RLC, kd.m[i].h.hash_hi, | ||
kd.m[i].h.hash_lo}; | ||
lookup(tmp, "keccak_table"); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
} // namespace bbf | ||
} // namespace blueprint | ||
} // namespace nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.