37
37
#include < nil/blueprint/transpiler/templates/permutation_argument.hpp>
38
38
#include < nil/blueprint/transpiler/templates/lookup_argument.hpp>
39
39
#include < nil/blueprint/transpiler/templates/commitment_scheme.hpp>
40
+ #include < nil/blueprint/transpiler/templates/external_gate.hpp>
40
41
#include < nil/blueprint/transpiler/lpc_scheme_gen.hpp>
41
42
#include < nil/blueprint/transpiler/util.hpp>
42
43
@@ -50,6 +51,8 @@ namespace nil {
50
51
>::preprocessed_data_type::common_data_type;
51
52
52
53
using variable_type = nil::crypto3::zk::snark::plonk_variable<typename PlaceholderParams::field_type::value_type>;
54
+ using constraint_type = nil::crypto3::zk::snark::plonk_constraint<typename PlaceholderParams::field_type>;
55
+ using gate_type = nil::crypto3::zk::snark::plonk_gate<typename PlaceholderParams::field_type, constraint_type>;
53
56
using variable_indices_type = std::map<nil::crypto3::zk::snark::plonk_variable<typename PlaceholderParams::field_type::value_type>, std::size_t >;
54
57
using columns_rotations_type = std::array<std::set<int >, PlaceholderParams::total_columns>;
55
58
@@ -128,7 +131,7 @@ namespace nil {
128
131
129
132
std::string constraint_computation_code (
130
133
variable_indices_type &_var_indices,
131
- const typename nil::crypto3::zk::snark::plonk_constraint< typename PlaceholderParams::field_type> &constraint
134
+ const constraint_type &constraint
132
135
){
133
136
using variable_type = nil::crypto3::zk::snark::plonk_variable<typename PlaceholderParams::field_type::value_type>;
134
137
std::stringstream result;
@@ -194,84 +197,156 @@ namespace nil {
194
197
_var_indices = get_plonk_variable_indices (_common_data.columns_rotations );
195
198
}
196
199
197
- void print (){
198
- std::filesystem::create_directory (_folder_name);
199
- std::cout << " Generating verifier " << _test_name << std::endl;
200
+ void print_gate_file (std::string gate_computation_code, std::size_t gate_id){
201
+ std::string result = modular_external_gate_library_template;
202
+ boost::replace_all (result, " $TEST_NAME$" , _test_name);
203
+ boost::replace_all (result, " $GATE_LIB_ID$" , to_string (gate_id));
204
+ boost::replace_all (result, " $GATES_ASSEMBLY_CODE$" , gate_computation_code);
205
+ boost::replace_all (result, " $MODULUS$" , to_string (PlaceholderParams::field_type::modulus));
206
+
207
+ std::ofstream out;
208
+ out.open (_folder_name + " /gate_" + to_string (gate_id) + " .sol" );
209
+ out <<result;
210
+ out.close ();
211
+ }
212
+
213
+ std::string gate_computation_code (const gate_type& gate){
214
+ std::stringstream out;
215
+
216
+ out << " \t\t gate = 0;" << std::endl;
217
+ for (const auto &constraint: gate.constraints ){
218
+ out << constraint_computation_code (_var_indices, constraint);
219
+ out << " \t\t gate = addmod(gate, mulmod(theta_acc, sum, modulus), modulus);" << std::endl;
220
+ out << " \t\t theta_acc = mulmod(theta_acc, theta, modulus);" << std::endl;
221
+ }
222
+ variable_type sel_var (gate.selector_index , 0 , true , variable_type::column_type::selector);
223
+ out << " \t\t gate = mulmod(gate, basic_marshalling.get_uint256_be(blob, " << _var_indices.at (sel_var) * 0x20 << " ), modulus);" << std::endl;
224
+ out << " \t\t F = addmod(F, gate, modulus);" <<std::endl;
225
+ return out.str ();
226
+ }
227
+
228
+ void print_gate_libs_list (std::vector<std::size_t > gate_ids){
229
+ std::ofstream out;
230
+ out.open (_folder_name + " /gate_libs_list.json" );
231
+ out << " [" << std::endl;
232
+ for (std::size_t i=0 ; i < gate_ids.size (); i++){
233
+ out << " \" " << " gate_" << _test_name << " _" << gate_ids[i] << " \" " ;
234
+ if (i < gate_ids.size () - 1 ){
235
+ out << " ," ;
236
+ }
237
+ out << std::endl;
238
+ }
239
+ out << " ]" << std::endl;
240
+ out.close ();
241
+ }
200
242
201
- _gate_argument_code << " \t\t uint256 sum;" << std::endl;
202
- _gate_argument_code << " \t\t uint256 gate;" << std::endl;
203
- _gate_argument_code << " \t\t uint256 prod;" << std::endl;
204
- _gate_argument_code << " \t\t uint256 theta_acc=1;" << std::endl;
243
+ std::string print_gate_argument (){
244
+ std::stringstream gate_argument_str;
245
+ std::vector<std::string> gates_computation_code;
246
+ std::size_t i = 0 ;
247
+ std::vector<std::size_t > gate_ids;
248
+
249
+ for (const auto &gate: _constraint_system.gates ()){
250
+ std::string gate_eval_string = gate_call_template;
251
+ boost::replace_all (gate_eval_string, " $TEST_NAME$" , _test_name);
252
+ boost::replace_all (gate_eval_string, " $GATE_LIB_ID$" , to_string (i));
253
+ boost::replace_all (gate_eval_string, " $MODULUS$" , to_string (PlaceholderParams::field_type::modulus));
254
+ gate_argument_str << gate_eval_string << std::endl;
255
+ _gate_includes += " import \" ./gate_" + to_string (i) + " .sol\" ; \n " ;
256
+ gates_computation_code.push_back (gate_computation_code (gate));
257
+ print_gate_file (gates_computation_code[i], i);
258
+ gate_ids.push_back (i);
259
+ i++;
260
+ }
261
+ 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;
205
267
for(const auto &gate: _constraint_system.gates()){
206
- _gate_argument_code << " \t\t gate = 0;" << std::endl;
268
+ gate_argument_str << "\t\tgate = 0;" << std::endl;
207
269
for(const auto &constraint: gate.constraints){
208
- _gate_argument_code << constraint_computation_code (_var_indices, constraint);
209
- _gate_argument_code << " \t\t gate = addmod(gate, mulmod(theta_acc, sum, modulus), modulus);" << std::endl;
210
- _gate_argument_code << " \t\t theta_acc = mulmod(theta_acc, theta, modulus);" << std::endl;
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;
211
273
}
212
274
variable_type sel_var(gate.selector_index, 0, true, variable_type::column_type::selector);
213
- _gate_argument_code << " \t\t\t gate = mulmod(gate, basic_marshalling.get_uint256_be(blob, " << _var_indices.at (sel_var) * 0x20 << " ), modulus);" << std::endl;
214
- _gate_argument_code << " \t\t\t F = addmod(F, gate, modulus);" <<std::endl <<std::endl;
215
- }
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
+ }*/
278
+ return gate_argument_str.str ();
279
+ }
216
280
281
+ std::string print_lookup_argument (){
282
+ std::stringstream lookup_str;
217
283
std::size_t j = 0 ;
218
- std::stringstream _lookup_argument_code;
219
- _lookup_argument_code << " \t\t\t uint256 sum;" << std::endl;
220
- _lookup_argument_code << " \t\t\t uint256 prod;" << std::endl;
284
+
285
+ lookup_str << " \t\t\t uint256 sum;" << std::endl;
286
+ lookup_str << " \t\t\t uint256 prod;" << std::endl;
221
287
222
288
for (const auto &gate: _constraint_system.lookup_gates ()){
223
289
variable_type sel_var (gate.tag_index , 0 , true , variable_type::column_type::selector);
224
- _lookup_argument_code << " \t\t\t state.selector_value=basic_marshalling.get_uint256_be(blob, " << _var_indices.at (sel_var) * 0x20 << " );" << std::endl;
290
+ lookup_str << " \t\t\t state.selector_value=basic_marshalling.get_uint256_be(blob, " << _var_indices.at (sel_var) * 0x20 << " );" << std::endl;
225
291
for ( const auto &constraint: gate.constraints ){
226
292
variable_type sel_var (gate.tag_index , 0 , true , variable_type::column_type::selector);
227
- _lookup_argument_code <<
293
+ lookup_str <<
228
294
" \t\t\t l = mulmod( " << constraint.table_id << " ,state.selector_value, modulus);" << std::endl;
229
- _lookup_argument_code << " \t\t\t state.theta_acc=state.theta;" << std::endl;
295
+ lookup_str << " \t\t\t state.theta_acc=state.theta;" << std::endl;
230
296
for ( const auto &expression:constraint.lookup_input ){
231
- _lookup_argument_code << constraint_computation_code (_var_indices, expression) << std::endl << std::endl;
232
- _lookup_argument_code <<
297
+ lookup_str << constraint_computation_code (_var_indices, expression) << std::endl << std::endl;
298
+ lookup_str <<
233
299
" \t\t\t l = addmod( l, mulmod( mulmod(state.theta_acc, state.selector_value, modulus), sum, modulus), modulus);" << std::endl;
234
- _lookup_argument_code << " \t\t\t state.theta_acc = mulmod(state.theta_acc, state.theta, modulus);" << std::endl;
300
+ lookup_str << " \t\t\t state.theta_acc = mulmod(state.theta_acc, state.theta, modulus);" << std::endl;
235
301
}
236
- _lookup_argument_code << " state.g = mulmod(state.g, mulmod(addmod(1, state.beta, modulus), addmod(l,state.gamma, modulus), modulus), modulus);" << std::endl;
302
+ lookup_str << " state.g = mulmod(state.g, mulmod(addmod(1, state.beta, modulus), addmod(l,state.gamma, modulus), modulus), modulus);" << std::endl;
237
303
j++;
238
304
}
239
305
}
240
306
241
- _lookup_argument_code << std::endl;
307
+ lookup_str << std::endl;
242
308
j = 0 ;
243
309
std::size_t table_index = 1 ;
244
310
for (const auto &table: _constraint_system.lookup_tables ()){
245
311
variable_type sel_var (table.tag_index , 0 , true , variable_type::column_type::selector);
246
312
variable_type shifted_sel_var (table.tag_index , 1 , true , variable_type::column_type::selector);
247
- _lookup_argument_code << " \t\t state.selector_value=basic_marshalling.get_uint256_be(blob, " << _var_indices.at (sel_var) * 0x20 << " );" << std::endl;
248
- _lookup_argument_code << " \t\t state.shifted_selector_value=basic_marshalling.get_uint256_be(blob, " << _var_indices.at (shifted_sel_var) * 0x20 << " );" << std::endl;
313
+ lookup_str << " \t\t state.selector_value=basic_marshalling.get_uint256_be(blob, " << _var_indices.at (sel_var) * 0x20 << " );" << std::endl;
314
+ lookup_str << " \t\t state.shifted_selector_value=basic_marshalling.get_uint256_be(blob, " << _var_indices.at (shifted_sel_var) * 0x20 << " );" << std::endl;
249
315
250
316
for ( const auto &option: table.lookup_options ){
251
- _lookup_argument_code <<
317
+ lookup_str <<
252
318
" \t\t\t l= mulmod( " << table_index << " , state.selector_value, modulus);" << std::endl;
253
- _lookup_argument_code <<
319
+ lookup_str <<
254
320
" \t\t\t state.l_shifted = mulmod( " << table_index << " , state.shifted_selector_value, modulus);" << std::endl;
255
- _lookup_argument_code << " \t\t\t state.theta_acc=state.theta;" << std::endl;
321
+ lookup_str << " \t\t\t state.theta_acc=state.theta;" << std::endl;
256
322
for ( const auto &var: option ){
257
- _lookup_argument_code <<
323
+ lookup_str <<
258
324
" \t\t\t l= addmod( l, mulmod(state.selector_value, mulmod( state.theta_acc, basic_marshalling.get_uint256_be(blob, " << _var_indices.at (var) * 0x20 << " ), modulus), modulus), modulus);" << std::endl;
259
325
variable_type shifted_var = var;
260
326
shifted_var.rotation = 1 ;
261
- _lookup_argument_code <<
327
+ lookup_str <<
262
328
" \t\t\t state.l_shifted = addmod( state.l_shifted, mulmod(state.shifted_selector_value, mulmod( state.theta_acc, basic_marshalling.get_uint256_be(blob, " << _var_indices.at (shifted_var) * 0x20 << " ), modulus), modulus), modulus);" << std::endl;
263
- _lookup_argument_code << " \t\t\t state.theta_acc = mulmod(state.theta_acc, state.theta, modulus);" << std::endl;
329
+ lookup_str << " \t\t\t state.theta_acc = mulmod(state.theta_acc, state.theta, modulus);" << std::endl;
264
330
}
265
- _lookup_argument_code <<
331
+ lookup_str <<
266
332
" \t\t\t l= mulmod( l, state.mask, modulus);" << std::endl;
267
- _lookup_argument_code <<
333
+ lookup_str <<
268
334
" \t\t\t state.l_shifted = mulmod( state.l_shifted, state.shifted_mask, modulus);" << std::endl;
269
- _lookup_argument_code << " \t\t\t state.g = mulmod(state.g, addmod( state.factor, addmod(l, mulmod(state.beta, state.l_shifted, modulus), modulus), modulus), modulus);" << std::endl;
335
+ lookup_str << " \t\t\t state.g = mulmod(state.g, addmod( state.factor, addmod(l, mulmod(state.beta, state.l_shifted, modulus), modulus), modulus), modulus);" << std::endl;
270
336
j++;
271
337
}
272
338
table_index++;
273
339
}
274
- _lookup_argument_code << std::endl;
340
+ lookup_str << std::endl;
341
+ return lookup_str.str ();
342
+ }
343
+
344
+ void print (){
345
+ std::filesystem::create_directory (_folder_name);
346
+ std::cout << " Generating verifier " << _test_name << std::endl;
347
+
348
+ std::string gate_argument = print_gate_argument ();
349
+ std::string lookup_argument = print_lookup_argument ();
275
350
276
351
std::string commitment_code = generate_commitment_scheme_code<PlaceholderParams>(_common_data, _lpc_scheme);
277
352
@@ -296,8 +371,9 @@ namespace nil {
296
371
reps[" $ROWS_AMOUNT$" ] = to_string (_common_data.rows_amount );
297
372
reps[" $OMEGA$" ] = to_string (_common_data.basic_domain ->get_domain_element (1 ));
298
373
reps[" $ZERO_INDICES$" ] = zero_indices (_common_data.columns_rotations );
299
- reps[" $GATE_ARGUMENT_COMPUTATION$" ] = _gate_argument_code.str ();
300
- reps[" $LOOKUP_ARGUMENT_COMPUTATION$" ] = _lookup_argument_code.str ();
374
+ reps[" $GATE_ARGUMENT_COMPUTATION$" ] = gate_argument;
375
+ reps[" $GATE_INCLUDES$" ] = _gate_includes;
376
+ reps[" $LOOKUP_ARGUMENT_COMPUTATION$" ] = lookup_argument;
301
377
reps[" $COMMITMENT_CODE$" ] = commitment_code;
302
378
303
379
commitment_scheme_replaces<PlaceholderParams>(reps, _common_data, _lpc_scheme, _permutation_size, _use_lookups);
@@ -326,9 +402,9 @@ namespace nil {
326
402
std::size_t _variable_values_offset;
327
403
std::size_t _permutation_offset;
328
404
std::size_t _quotient_offset;
329
- std::stringstream _gate_argument_code;
330
- std::stringstream _lookup_argument_code;
331
405
variable_indices_type _var_indices;
406
+
407
+ std::string _gate_includes;
332
408
};
333
409
}
334
410
}
0 commit comments