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

Commit 0f9e96f

Browse files
ETatuzovankaskov
authored andcommitted
Lookups splitted #21
1 parent 6f72251 commit 0f9e96f

File tree

4 files changed

+134
-29
lines changed

4 files changed

+134
-29
lines changed

include/nil/blueprint/transpiler/evm_verifier_gen.hpp

+76-18
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <nil/blueprint/transpiler/templates/lookup_argument.hpp>
3939
#include <nil/blueprint/transpiler/templates/commitment_scheme.hpp>
4040
#include <nil/blueprint/transpiler/templates/external_gate.hpp>
41+
#include <nil/blueprint/transpiler/templates/external_lookup.hpp>
4142
#include <nil/blueprint/transpiler/lpc_scheme_gen.hpp>
4243
#include <nil/blueprint/transpiler/util.hpp>
4344

@@ -52,7 +53,9 @@ namespace nil {
5253

5354
using variable_type = nil::crypto3::zk::snark::plonk_variable<typename PlaceholderParams::field_type::value_type>;
5455
using constraint_type = nil::crypto3::zk::snark::plonk_constraint<typename PlaceholderParams::field_type>;
56+
using lookup_constraint_type = nil::crypto3::zk::snark::plonk_lookup_constraint<typename PlaceholderParams::field_type>;
5557
using gate_type = nil::crypto3::zk::snark::plonk_gate<typename PlaceholderParams::field_type, constraint_type>;
58+
using lookup_gate_type = nil::crypto3::zk::snark::plonk_lookup_gate<typename PlaceholderParams::field_type, lookup_constraint_type>;
5659
using variable_indices_type = std::map<nil::crypto3::zk::snark::plonk_variable<typename PlaceholderParams::field_type::value_type>, std::size_t>;
5760
using columns_rotations_type = std::array<std::set<int>, PlaceholderParams::total_columns>;
5861

@@ -210,6 +213,19 @@ namespace nil {
210213
out.close();
211214
}
212215

216+
void print_lookup_file(std::string lookup_computation_code, std::size_t lookup_id){
217+
std::string result = modular_external_lookup_library_template;
218+
boost::replace_all(result, "$TEST_NAME$", _test_name);
219+
boost::replace_all(result, "$LOOKUP_LIB_ID$", to_string(lookup_id));
220+
boost::replace_all(result, "$LOOKUP_ASSEMBLY_CODE$", lookup_computation_code);
221+
boost::replace_all(result, "$MODULUS$", to_string(PlaceholderParams::field_type::modulus));
222+
223+
std::ofstream out;
224+
out.open(_folder_name + "/lookup_" + to_string(lookup_id) + ".sol");
225+
out <<result;
226+
out.close();
227+
}
228+
213229
std::string gate_computation_code(const gate_type& gate){
214230
std::stringstream out;
215231

@@ -225,6 +241,27 @@ namespace nil {
225241
return out.str();
226242
}
227243

244+
std::string lookup_computation_code(const lookup_gate_type& gate){
245+
std::stringstream out;
246+
247+
variable_type sel_var(gate.tag_index, 0, true, variable_type::column_type::selector);
248+
out << "\t\tselector_value=basic_marshalling.get_uint256_be(blob, " << _var_indices.at(sel_var) * 0x20 << ");" << std::endl;
249+
out << "\t\tg = 1;" << std::endl;
250+
for( const auto &constraint: gate.constraints ){
251+
variable_type sel_var(gate.tag_index, 0, true, variable_type::column_type::selector);
252+
out << "\t\tl = mulmod( " << constraint.table_id << ",selector_value, modulus);" << std::endl;
253+
out << "\t\ttheta_acc=theta;" << std::endl;
254+
for( const auto &expression:constraint.lookup_input ){
255+
out << constraint_computation_code(_var_indices, expression) << std::endl << std::endl;
256+
out << "\t\tl = addmod( l, mulmod( mulmod(theta_acc, selector_value, modulus), sum, modulus), modulus);" << std::endl;
257+
out << "\t\ttheta_acc = mulmod(theta_acc, theta, modulus);" << std::endl;
258+
}
259+
out << "\t\tg = mulmod(g, mulmod(addmod(1, beta, modulus), addmod(l,gamma, modulus), modulus), modulus);" << std::endl;
260+
}
261+
262+
return out.str();
263+
}
264+
228265
void print_gate_libs_list(std::vector<std::size_t> gate_ids){
229266
std::ofstream out;
230267
out.open(_folder_name + "/gate_libs_list.json");
@@ -240,6 +277,21 @@ namespace nil {
240277
out.close();
241278
}
242279

280+
void print_lookup_libs_list(std::vector<std::size_t> gate_ids){
281+
std::ofstream out;
282+
out.open(_folder_name + "/lookup_libs_list.json");
283+
out << "[" << std::endl;
284+
for(std::size_t i=0; i < gate_ids.size(); i++){
285+
out << "\"" << "lookup_" << _test_name << "_" << gate_ids[i] << "\"";
286+
if(i < gate_ids.size() - 1){
287+
out << ",";
288+
}
289+
out << std::endl;
290+
}
291+
out << "]" << std::endl;
292+
out.close();
293+
}
294+
243295
std::string print_gate_argument(){
244296
std::stringstream gate_argument_str;
245297
std::vector<std::string> gates_computation_code;
@@ -259,29 +311,32 @@ namespace nil {
259311
i++;
260312
}
261313
print_gate_libs_list(gate_ids);
262-
/*
263-
gate_argument_str << "\t\tuint256 sum;" << std::endl;
264-
gate_argument_str << "\t\tuint256 gate;" << std::endl;
265-
gate_argument_str << "\t\tuint256 prod;" << std::endl;
266-
gate_argument_str << "\t\tuint256 theta_acc=1;" << std::endl;
267-
for(const auto &gate: _constraint_system.gates()){
268-
gate_argument_str << "\t\tgate = 0;" << std::endl;
269-
for(const auto &constraint: gate.constraints){
270-
gate_argument_str << constraint_computation_code(_var_indices, constraint);
271-
gate_argument_str << "\t\tgate = addmod(gate, mulmod(theta_acc, sum, modulus), modulus);" << std::endl;
272-
gate_argument_str << "\t\ttheta_acc = mulmod(theta_acc, theta, modulus);" << std::endl;
273-
}
274-
variable_type sel_var(gate.selector_index, 0, true, variable_type::column_type::selector);
275-
gate_argument_str << "\t\t\tgate = mulmod(gate, basic_marshalling.get_uint256_be(blob, " << _var_indices.at(sel_var) * 0x20 << "), modulus);" << std::endl;
276-
gate_argument_str << "\t\t\tF = addmod(F, gate, modulus);" <<std::endl <<std::endl;
277-
}*/
314+
278315
return gate_argument_str.str();
279316
}
280317

281318
std::string print_lookup_argument(){
282319
std::stringstream lookup_str;
283320
std::size_t j = 0;
284-
321+
std::size_t i = 0;
322+
std::size_t cur = 0;
323+
std::vector<std::string> lookups_computation_code;
324+
std::vector<std::size_t> lookup_ids;
325+
326+
for(const auto &lookup_gate: _constraint_system.lookup_gates()){
327+
std::string lookup_eval_string = lookup_call_template;
328+
boost::replace_all(lookup_eval_string, "$TEST_NAME$", _test_name);
329+
boost::replace_all(lookup_eval_string, "$LOOKUP_LIB_ID$", to_string(i));
330+
boost::replace_all(lookup_eval_string, "$MODULUS$", to_string(PlaceholderParams::field_type::modulus));
331+
lookup_str << lookup_eval_string;
332+
lookup_ids.push_back(i);
333+
_lookup_includes += "import \"./lookup_" + to_string(i) + ".sol\"; \n";
334+
lookups_computation_code.push_back(lookup_computation_code(lookup_gate));
335+
print_lookup_file(lookups_computation_code[i], i);
336+
i++;
337+
}
338+
print_lookup_libs_list(lookup_ids);
339+
/*
285340
lookup_str << "\t\t\tuint256 sum;" << std::endl;
286341
lookup_str << "\t\t\tuint256 prod;" << std::endl;
287342
@@ -303,8 +358,8 @@ namespace nil {
303358
j++;
304359
}
305360
}
306-
307361
lookup_str << std::endl;
362+
*/
308363
j = 0;
309364
std::size_t table_index = 1;
310365
for(const auto &table: _constraint_system.lookup_tables()){
@@ -338,6 +393,7 @@ namespace nil {
338393
table_index++;
339394
}
340395
lookup_str << std::endl;
396+
341397
return lookup_str.str();
342398
}
343399

@@ -373,6 +429,7 @@ namespace nil {
373429
reps["$ZERO_INDICES$"] = zero_indices(_common_data.columns_rotations);
374430
reps["$GATE_ARGUMENT_COMPUTATION$"] = gate_argument;
375431
reps["$GATE_INCLUDES$"] = _gate_includes;
432+
reps["$LOOKUP_INCLUDES$"] = _lookup_includes;
376433
reps["$LOOKUP_ARGUMENT_COMPUTATION$"] = lookup_argument;
377434
reps["$COMMITMENT_CODE$"] = commitment_code;
378435

@@ -405,6 +462,7 @@ namespace nil {
405462
variable_indices_type _var_indices;
406463

407464
std::string _gate_includes;
465+
std::string _lookup_includes;
408466
};
409467
}
410468
}

include/nil/blueprint/transpiler/templates/external_gate.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ namespace nil {
2525
pragma solidity >=0.8.4;
2626
2727
import "../../../contracts/basic_marshalling.sol";
28-
import "./gate_argument.sol";
2928
3029
library gate_$TEST_NAME$_$GATE_LIB_ID${
3130
uint256 constant modulus = $MODULUS$;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef __MODULAR_EXTERNAL_LOOKUP_ARGUMENT_TEMPLATE_HPP__
2+
#define __MODULAR_EXTERNAL_LOOKUP_ARGUMENT_TEMPLATE_HPP__
3+
4+
#include <string>
5+
6+
namespace nil {
7+
namespace blueprint {
8+
std::string modular_external_lookup_library_template = R"(
9+
// SPDX-License-Identifier: Apache-2.0.
10+
//---------------------------------------------------------------------------//
11+
// Copyright (c) 2023 -- Generated by zkllvm-transpiler
12+
//
13+
// Licensed under the Apache License, Version 2.0 (the "License");
14+
// you may not use this file except in compliance with the License.
15+
// You may obtain a copy of the License at
16+
//
17+
// http://www.apache.org/licenses/LICENSE-2.0
18+
//
19+
// Unless required by applicable law or agreed to in writing, software
20+
// distributed under the License is distributed on an "AS IS" BASIS,
21+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
// See the License for the specific language governing permissions and
23+
// limitations under the License.
24+
//---------------------------------------------------------------------------//
25+
pragma solidity >=0.8.4;
26+
27+
import "../../../contracts/basic_marshalling.sol";
28+
29+
library lookup_$TEST_NAME$_$LOOKUP_LIB_ID${
30+
uint256 constant modulus = $MODULUS$;
31+
32+
function evaluate_gate_be(
33+
bytes calldata blob,
34+
uint256 theta,
35+
uint256 theta_acc,
36+
uint256 beta,
37+
uint256 gamma
38+
) external pure returns (uint256 g, uint256) {
39+
uint256 l;
40+
uint256 selector_value;
41+
uint256 sum;
42+
uint256 prod;
43+
44+
$LOOKUP_ASSEMBLY_CODE$
45+
return( g, theta_acc );
46+
}
47+
}
48+
)";
49+
}
50+
}
51+
52+
#endif //__EXTERNAL_GATE_ARGUMENT_TEMPLATE_HPP__

include/nil/blueprint/transpiler/templates/lookup_argument.hpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55

66
namespace nil {
77
namespace blueprint {
8+
std::string lookup_call_template =
9+
"\t\t\t(l, state.theta_acc) = lookup_$TEST_NAME$_$LOOKUP_LIB_ID$.evaluate_gate_be( blob, state.theta, state.theta_acc, state.beta, state.gamma );\n\t\t\tstate.g = mulmod(state.g, l, modulus);\n" ;
10+
811
std::string modular_dummy_lookup_argument_library_template = R"(
912
// SPDX-License-Identifier: Apache-2.0.
1013
//---------------------------------------------------------------------------//
1114
// Copyright (c) 2023 Generated by ZKLLVM-transpiler
1215
//
13-
// Licensed under the Apache License, Version 2.0 (the "License");
16+
// Licensed under the
17+
// License, Version 2.0 (the "License");
1418
// you may not use this file except in compliance with the License.
1519
// You may obtain a copy of the License at
1620
//
@@ -24,14 +28,6 @@ namespace nil {
2428
//---------------------------------------------------------------------------//
2529
pragma solidity >=0.8.4;
2630
27-
import "../../cryptography/transcript.sol";
28-
// Move away unused structures from types.sol
29-
import "../../types.sol";
30-
import "../../basic_marshalling.sol";
31-
import "../../cryptography/transcript.sol";
32-
import "../../interfaces/modular_lookup_argument.sol";
33-
import "hardhat/console.sol";
34-
3531
library modular_lookup_argument_$TEST_NAME${
3632
}
3733
)";
@@ -61,6 +57,7 @@ import "../../types.sol";
6157
import "../../basic_marshalling.sol";
6258
import "../../cryptography/transcript.sol";
6359
import "../../interfaces/modular_lookup_argument.sol";
60+
$LOOKUP_INCLUDES$
6461
import "hardhat/console.sol";
6562
6663
contract modular_lookup_argument_$TEST_NAME$ is ILookupArgument{
@@ -125,7 +122,6 @@ contract modular_lookup_argument_$TEST_NAME$ is ILookupArgument{
125122
state.beta = transcript.get_field_challenge(tr_state, modulus); //beta
126123
state.gamma = transcript.get_field_challenge(tr_state, modulus); //gamma
127124
state.factor = mulmod(addmod(1, state.beta, modulus), state.gamma, modulus);
128-
129125
$LOOKUP_ARGUMENT_COMPUTATION$
130126
}
131127
{

0 commit comments

Comments
 (0)