38
38
#include < nil/blueprint/transpiler/templates/lookup_argument.hpp>
39
39
#include < nil/blueprint/transpiler/templates/commitment_scheme.hpp>
40
40
#include < nil/blueprint/transpiler/templates/external_gate.hpp>
41
+ #include < nil/blueprint/transpiler/templates/external_lookup.hpp>
41
42
#include < nil/blueprint/transpiler/lpc_scheme_gen.hpp>
42
43
#include < nil/blueprint/transpiler/util.hpp>
43
44
@@ -52,7 +53,9 @@ namespace nil {
52
53
53
54
using variable_type = nil::crypto3::zk::snark::plonk_variable<typename PlaceholderParams::field_type::value_type>;
54
55
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>;
55
57
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>;
56
59
using variable_indices_type = std::map<nil::crypto3::zk::snark::plonk_variable<typename PlaceholderParams::field_type::value_type>, std::size_t >;
57
60
using columns_rotations_type = std::array<std::set<int >, PlaceholderParams::total_columns>;
58
61
@@ -210,6 +213,19 @@ namespace nil {
210
213
out.close ();
211
214
}
212
215
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
+
213
229
std::string gate_computation_code (const gate_type& gate){
214
230
std::stringstream out;
215
231
@@ -225,6 +241,27 @@ namespace nil {
225
241
return out.str ();
226
242
}
227
243
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\t selector_value=basic_marshalling.get_uint256_be(blob, " << _var_indices.at (sel_var) * 0x20 << " );" << std::endl;
249
+ out << " \t\t g = 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\t l = mulmod( " << constraint.table_id << " ,selector_value, modulus);" << std::endl;
253
+ out << " \t\t theta_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\t l = addmod( l, mulmod( mulmod(theta_acc, selector_value, modulus), sum, modulus), modulus);" << std::endl;
257
+ out << " \t\t theta_acc = mulmod(theta_acc, theta, modulus);" << std::endl;
258
+ }
259
+ out << " \t\t g = mulmod(g, mulmod(addmod(1, beta, modulus), addmod(l,gamma, modulus), modulus), modulus);" << std::endl;
260
+ }
261
+
262
+ return out.str ();
263
+ }
264
+
228
265
void print_gate_libs_list (std::vector<std::size_t > gate_ids){
229
266
std::ofstream out;
230
267
out.open (_folder_name + " /gate_libs_list.json" );
@@ -240,6 +277,21 @@ namespace nil {
240
277
out.close ();
241
278
}
242
279
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
+
243
295
std::string print_gate_argument (){
244
296
std::stringstream gate_argument_str;
245
297
std::vector<std::string> gates_computation_code;
@@ -259,29 +311,32 @@ namespace nil {
259
311
i++;
260
312
}
261
313
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
+
278
315
return gate_argument_str.str ();
279
316
}
280
317
281
318
std::string print_lookup_argument (){
282
319
std::stringstream lookup_str;
283
320
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
+ /*
285
340
lookup_str << "\t\t\tuint256 sum;" << std::endl;
286
341
lookup_str << "\t\t\tuint256 prod;" << std::endl;
287
342
@@ -303,8 +358,8 @@ namespace nil {
303
358
j++;
304
359
}
305
360
}
306
-
307
361
lookup_str << std::endl;
362
+ */
308
363
j = 0 ;
309
364
std::size_t table_index = 1 ;
310
365
for (const auto &table: _constraint_system.lookup_tables ()){
@@ -338,6 +393,7 @@ namespace nil {
338
393
table_index++;
339
394
}
340
395
lookup_str << std::endl;
396
+
341
397
return lookup_str.str ();
342
398
}
343
399
@@ -373,6 +429,7 @@ namespace nil {
373
429
reps[" $ZERO_INDICES$" ] = zero_indices (_common_data.columns_rotations );
374
430
reps[" $GATE_ARGUMENT_COMPUTATION$" ] = gate_argument;
375
431
reps[" $GATE_INCLUDES$" ] = _gate_includes;
432
+ reps[" $LOOKUP_INCLUDES$" ] = _lookup_includes;
376
433
reps[" $LOOKUP_ARGUMENT_COMPUTATION$" ] = lookup_argument;
377
434
reps[" $COMMITMENT_CODE$" ] = commitment_code;
378
435
@@ -405,6 +462,7 @@ namespace nil {
405
462
variable_indices_type _var_indices;
406
463
407
464
std::string _gate_includes;
465
+ std::string _lookup_includes;
408
466
};
409
467
}
410
468
}
0 commit comments