Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 6f51905

Browse files
committed
PC is looked up in bytecode table #325
1 parent 266b9d2 commit 6f51905

File tree

15 files changed

+210
-214
lines changed

15 files changed

+210
-214
lines changed

libs/blueprint/include/nil/blueprint/components/hashes/keccak/keccak_dynamic.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include <nil/blueprint/components/hashes/keccak/util.hpp>
3838
#include <nil/blueprint/components/hashes/keccak/keccak_round.hpp>
3939

40+
41+
4042
namespace nil {
4143
namespace blueprint {
4244
namespace components {
@@ -867,8 +869,8 @@ namespace nil {
867869
hash = std::get<1>(instance_input.input[input_idx]);
868870
input_idx++;
869871
} else {
870-
msg = {0};
871-
hash = {0xbc36789e7a1e281436464229828f817d_cppui_modular254, 0x6612f7b477d66591ff96a9e064bcc98a_cppui_modular254};
872+
msg = {};
873+
hash = keccak_component_hash<BlueprintFieldType>(msg);
872874
}
873875
auto padded_msg = msg;
874876
padded_msg.push_back(1);

libs/blueprint/include/nil/blueprint/components/hashes/keccak/keccak_table.hpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@
4343
namespace nil {
4444
namespace blueprint {
4545
namespace components {
46+
template <typename BlueprintFieldType>
47+
std::pair<typename BlueprintFieldType::value_type, typename BlueprintFieldType::value_type> keccak_component_hash(const std::vector<uint8_t> &buffer){
48+
using value_type = typename BlueprintFieldType::value_type;
49+
nil::crypto3::hashes::keccak_1600<256>::digest_type d = nil::crypto3::hash<nil::crypto3::hashes::keccak_1600<256>>(buffer);
50+
nil::crypto3::algebra::fields::field<256>::integral_type n(d);
51+
std::pair<value_type, value_type> hash_value;
52+
53+
hash_value.first = (n & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000_cppui_modular257) >> 128;
54+
hash_value.second = n & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui_modular257;
55+
return hash_value;
56+
}
57+
4658
template<typename ArithmetizationType>
4759
class keccak_table;
4860

@@ -148,14 +160,7 @@ namespace nil {
148160
}
149161

150162
void new_buffer(const std::vector<std::uint8_t> buffer){
151-
nil::crypto3::hashes::keccak_1600<256>::digest_type d = nil::crypto3::hash<nil::crypto3::hashes::keccak_1600<256>>(buffer);
152-
nil::crypto3::algebra::fields::field<256>::integral_type n(d);
153-
std::pair<value_type, value_type> hash_value;
154-
155-
hash_value.first = (n & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000_cppui_modular257) >> 128;
156-
hash_value.second = n & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui_modular257;
157-
158-
input.push_back({buffer, hash_value});
163+
input.push_back({buffer, keccak_component_hash<BlueprintFieldType>(buffer)});
159164
}
160165

161166
data_type input;
@@ -206,44 +211,37 @@ namespace nil {
206211
&instance_input,
207212
const std::uint32_t start_row_index
208213
) {
209-
std::cout << "Keccak table generate assignments" << std::endl;
210214
using component_type = plonk_keccak_table<BlueprintFieldType>;
211215
using value_type = typename BlueprintFieldType::value_type;
212216

213-
std::cout << instance_input.rlc_challenge << std::endl;
214217
value_type theta = var_value(assignment, instance_input.rlc_challenge);
215218
std::size_t input_idx = 0;
216219
std::size_t block_counter = 0;
217220
std::vector<std::uint8_t> msg;
218221
std::pair<value_type, value_type> hash;
219222
typename component_type::keccak_table_map t(component);
220-
std::cout << "Keccak table generate assignments" << std::endl;
221223
while( block_counter < component.max_blocks ) {
222224
if( input_idx < instance_input.input.size() ){
223225
msg = std::get<0>(instance_input.input[input_idx]);
224226
hash = std::get<1>(instance_input.input[input_idx]);
225227
input_idx++;
226228
} else {
227-
msg = {0};
228-
hash = {0xbc36789e7a1e281436464229828f817d_cppui_modular254, 0x6612f7b477d66591ff96a9e064bcc98a_cppui_modular254};
229+
msg = {};
230+
hash = keccak_component_hash<BlueprintFieldType>(msg);
229231
}
230232
value_type RLC = calculateRLC<BlueprintFieldType>(msg, theta);
231233
for( std::size_t block = 0; block < std::ceil(float(msg.size() + 1)/136); block++){
232234
if( block != std::ceil(float(msg.size() + 1)/136) - 1){
233-
std::cout << "0 ";
234235
assignment.witness(t.is_last.index, start_row_index + block_counter) = 0;
235236
} else {
236-
std::cout << "1 ";
237237
assignment.witness(t.is_last.index, start_row_index + block_counter) = 1;
238238
}
239-
std::cout << std::hex << RLC << " " << hash.first << " " << hash.second << std::dec << std::endl;
240239
assignment.witness(t.RLC.index, start_row_index + block_counter) = RLC;
241240
assignment.witness(t.hash_hi.index, start_row_index + block_counter) = hash.first;
242241
assignment.witness(t.hash_lo.index, start_row_index + block_counter) = hash.second;
243242
block_counter++;
244243
}
245244
}
246-
std::cout << "Keccak table assignments generated" << std::endl;
247245
return typename component_type::result_type(component, start_row_index);
248246
}
249247

@@ -259,12 +257,10 @@ namespace nil {
259257
) {
260258
using component_type = plonk_keccak_table<BlueprintFieldType>;
261259
using var = typename component_type::var;
262-
std::cout << "Keccak table generate circuit" << std::endl;
263260

264261
bp.register_dynamic_table("keccak_table");
265262
std::size_t selector_index = bp.get_dynamic_lookup_table_selector();
266263
assignment.enable_selector(selector_index, start_row_index, start_row_index + component.rows_amount - 1);
267-
std::cout << "Keccak table selector index " << selector_index << std::endl;
268264

269265
crypto3::zk::snark::plonk_lookup_table<BlueprintFieldType> keccak_table;
270266
typename component_type::keccak_table_map t(component);

libs/blueprint/include/nil/blueprint/utils/satisfiability_check.hpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,16 @@ namespace nil {
7474
const assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &assignments,
7575
std::size_t table_id
7676
){
77-
std::cout << "Load dynamic lookup" << std::endl;
7877
std::set<std::vector<typename BlueprintFieldType::value_type>> result;
79-
BOOST_ASSERT(table_id < bp.lookup_tables().size());
78+
if( table_id > bp.lookup_tables().size() )
79+
std::cout << table_id << " >= " << bp.lookup_tables().size() << std::endl;
80+
BOOST_ASSERT(table_id <= bp.lookup_tables().size());
8081
auto &table = bp.lookup_tables()[table_id-1];
8182

82-
std::cout << "Table found" << table.tag_index << std::endl;
8383
crypto3::zk::snark::plonk_column<BlueprintFieldType> selector =
8484
assignments.crypto3::zk::snark::
8585
template plonk_assignment_table<BlueprintFieldType>::selector(table.tag_index);
8686

87-
std::cout << "Selector found" << std::endl;
8887
for( std::size_t selector_row = 0; selector_row < assignments.rows_amount(); selector_row++ ){
8988
if( selector_row < selector.size() && !selector[selector_row].is_zero() ){
9089
for( std::size_t op = 0; op < table.lookup_options.size(); op++){
@@ -98,7 +97,6 @@ namespace nil {
9897
}
9998
}
10099

101-
std::cout << "Dynamic lookup loaded" << std::endl;
102100
return result;
103101
}
104102

@@ -121,7 +119,6 @@ namespace nil {
121119
std::map<std::string, std::set<std::vector<typename BlueprintFieldType::value_type>>> used_dynamic_tables;
122120

123121
for (const auto& i : used_gates) {
124-
std::cout << "Gate " << i << std::endl;
125122
crypto3::zk::snark::plonk_column<BlueprintFieldType> selector =
126123
assignments.crypto3::zk::snark::
127124
template plonk_assignment_table<BlueprintFieldType>::selector(
@@ -158,25 +155,23 @@ namespace nil {
158155
}
159156

160157
for (const auto& i : used_lookup_gates) {
161-
std::cout << "Lookup gate " << i << std::endl;
162158
crypto3::zk::snark::plonk_column<BlueprintFieldType> selector =
163159
assignments.crypto3::zk::snark::
164160
template plonk_assignment_table<BlueprintFieldType>::selector(
165161
lookup_gates[i].tag_index);
166162

167163
for (const auto& selector_row : selector_rows) {
168164
if (selector_row < selector.size() && !selector[selector_row].is_zero()) {
169-
std::cout << "Selected row " << selector_row << std::endl;
170165
for (std::size_t j = 0; j < lookup_gates[i].constraints.size(); j++) {
171166
std::vector<typename BlueprintFieldType::value_type> input_values;
172167
input_values.reserve(lookup_gates[i].constraints[j].lookup_input.size());
173168
for (std::size_t k = 0; k < lookup_gates[i].constraints[j].lookup_input.size(); k++) {
174169
input_values.emplace_back(lookup_gates[i].constraints[j].lookup_input[k].evaluate(
175170
selector_row, assignments));
176171
}
172+
177173
const auto table_name =
178174
bp.get_reserved_indices_right().at(lookup_gates[i].constraints[j].table_id);
179-
std::cout << table_name << std::endl;
180175
try {
181176
if( bp.get_reserved_dynamic_tables().find(table_name) != bp.get_reserved_dynamic_tables().end() ){
182177
if( used_dynamic_tables.find(table_name) == used_dynamic_tables.end()){

libs/blueprint/include/nil/blueprint/zkevm/bytecode.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,17 @@ namespace nil {
342342
};
343343
std::size_t selector_id = bp.get_dynamic_table_definition("zkevm_bytecode")->lookup_table.tag_index;
344344

345+
// TODO: review after lookup argument update
346+
// It may be jsut constant for the fixed curve
347+
auto zerohash = keccak_component_hash<BlueprintFieldType>({});
348+
345349
lookup_constraint_type hash_table_constraint = {
346350
lookup_tables_indices.at("keccak_table"),
347351
{
348-
m.tag() * (1 - m.tag.next()),
349-
m.tag() * (1 - m.tag.next()) * m.value_rlc() + (1 - m.tag() * (1 - m.tag.next())) * 0x109057df9cba2ae4cc6f2c8c33de834267af65e2b2ea38088d571b0c4e5fcb5c_cppui_modular257,
350-
m.tag() * (1 - m.tag.next()) * m.hash_hi() + (1 - m.tag() * (1 - m.tag.next())) * 0x97cea80fc2260ca27ded02e6d09f19a3_cppui_modular257,
351-
m.tag() * (1 - m.tag.next()) * m.hash_lo() + (1 - m.tag() * (1 - m.tag.next())) * 0x9853f3bc764790709249eb48cc9375fd_cppui_modular257
352+
m.tag() + 1 - m.tag(), // TODO: update math::expression constructor with constant parameter
353+
m.tag() * (1 - m.tag.next()) * m.value_rlc(),
354+
m.tag() * (1 - m.tag.next()) * m.hash_hi() + (1 - m.tag() * (1 - m.tag.next())) * zerohash.first,
355+
m.tag() * (1 - m.tag.next()) * m.hash_lo() + (1 - m.tag() * (1 - m.tag.next())) * zerohash.second
352356
}
353357
};
354358

libs/blueprint/include/nil/blueprint/zkevm/bytecode_table.hpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,7 @@ namespace nil {
163163
return result;
164164
}
165165
};
166-
/*
167-
template<typename ContainerType>
168-
explicit zkevm_bytecode_table(ContainerType witness, std::size_t _max_bytecode_size) :
169-
component_type(witness, {}, {}, get_manifest()), max_bytecode_size(_max_bytecode_size)
170-
{};
171-
172-
template<typename WitnessContainerType, typename ConstantContainerType,
173-
typename PublicInputContainerType>
174-
zkevm_bytecode_table(WitnessContainerType witness, ConstantContainerType constant,
175-
PublicInputContainerType public_input,
176-
std::size_t _max_bytecode_size
177-
) : component_type(witness, constant, public_input, get_manifest()), max_bytecode_size(_max_bytecode_size) {};
178166

179-
zkevm_bytecode_table(
180-
std::initializer_list<typename component_type::witness_container_type::value_type> witnesses,
181-
std::initializer_list<typename component_type::constant_container_type::value_type>
182-
constants,
183-
std::initializer_list<typename component_type::public_input_container_type::value_type>
184-
public_inputs,
185-
std::size_t _max_bytecode_size
186-
) : component_type(witnesses, constants, public_inputs, get_manifest()), max_bytecode_size(_max_bytecode_size){};
187-
*/
188167
zkevm_bytecode_table(
189168
const typename component_type::witness_container_type &witnesses,
190169
const typename component_type::constant_container_type &constants,
@@ -269,7 +248,6 @@ namespace nil {
269248

270249
std::size_t selector_index = bp.get_dynamic_lookup_table_selector();
271250
assignment.enable_selector(selector_index, start_row_index, start_row_index + component.rows_amount - 1);
272-
std::cout << "Bytecode table selector index = " << selector_index << std::endl;
273251

274252
crypto3::zk::snark::plonk_lookup_table<BlueprintFieldType> bytecode_table;
275253
bytecode_table.tag_index = selector_index;

libs/blueprint/include/nil/blueprint/zkevm/state.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ namespace nil {
7171
state_var stack_size;
7272
state_var memory_size;
7373
state_var gas;
74-
state_var opcode;
74+
state_var opcode; // it's Id in the list of used opcodes
75+
state_var real_opcode; // real_opcode is real opcode that will be looked up in bytecode table
76+
state_var bytecode_hash_hi; // will be looked up in bytecode table
77+
state_var bytecode_hash_lo; // will be lookup up in bytecode table
7578

76-
state_var row_counter; // Decreasing row counter
77-
state_var step_start; // 1 in first line of new opcode, 0 otherwise
79+
state_var row_counter; // Decreasing row counter
80+
state_var step_start; // 1 in first line of new opcode, 0 otherwise
7881
state_var row_counter_inv;
7982
state_var last_row_indicator; // Do we really need it? I don't think so. Last opcode should be RETURN, err or padding.
8083
state_var opcode_parity; // opcode%2

0 commit comments

Comments
 (0)