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

Commit 16b612d

Browse files
authored
46 check values in etha point cherry pick for 39 (#57)
* remove utils.sol if no helper functions generated #45 NilFoundation/evm-placeholder-verification#81 * Added check values for eta points #46 * Correct keccak computation #46 * Code cleanup #46 * Removed recursive tests * Tests are now working #46
1 parent a7ca0e5 commit 16b612d

File tree

3 files changed

+72
-37
lines changed

3 files changed

+72
-37
lines changed

include/nil/blueprint/transpiler/evm_verifier_gen.hpp

+39-17
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,9 @@ namespace nil {
567567

568568
i = 0;
569569
for (const auto& gate: _constraint_system.gates()) {
570-
std::cout << "Gate " << i << std::endl;
571570
variable_type sel_var(gate.selector_index, 0, true, variable_type::column_type::selector);
572571
std::size_t j = 0;
573572
for (const auto& constraint: gate.constraints) {
574-
std::cout << "Constraint " << j << std::endl;
575573
std::string code = constraint_computation_code_optimized(_var_indices, constraint);
576574
std::size_t cost = estimate_constraint_cost(code);
577575
std::size_t selector_index = _var_indices.at(sel_var)*0x20;
@@ -784,63 +782,87 @@ namespace nil {
784782
}
785783

786784
std::string eta_point_verification_code() {
787-
std::cout << "Generating eta point verification code" << std::endl;
788785
std::stringstream result;
789786
auto fixed_poly_values = _common_data.commitment_scheme_data;
790-
787+
791788
std::size_t poly_points = 2;
792-
789+
793790
if (fixed_poly_values.size() == 0)
794791
return "";
792+
793+
result << "\t\t{" << std::endl;
794+
result << "\t\t\tuint256 poly_at_eta;" << std::endl;
795+
796+
result << "\t\t\t/* 1 - 2*permutation_size */" << std::endl;
797+
std::vector<std::uint8_t> eta_buf;
795798

796-
result << "\t\t/*{ " << std::endl;
797-
result << "\t\t\tbool b = true;" << std::endl;
799+
std::size_t poly_points = 2*_permutation_size;
800+
/* special_selectors */
801+
poly_points += 2;
802+
poly_points += PlaceholderParams::arithmetization_params::constant_columns;
803+
poly_points += PlaceholderParams::arithmetization_params::selector_columns;
804+
eta_buf.resize( 32*poly_points );
805+
806+
std::array<std::uint8_t, 0> empty;
807+
auto writer = eta_buf.begin();
808+
809+
result << "\t\t/* eta points check */" << std::endl;
810+
result << "\t\t{" << std::endl;
811+
result << "\t\t\tuint256[" << poly_points << "] memory points;" << std::endl;
798812

799-
result << "\t\t\t// 1 - 2*permutation_size " << std::endl;
800813
std::size_t i = 0, j = 0;
801814
std::size_t point_offset = 8;
802815

803816
while (j < 2*_permutation_size) {
804-
result << "\t\t\tb = b && (basic_marshalling.get_uint256_be(blob, " << point_offset+(poly_points-1)*32 << ") != " << std::showbase<< std::hex << fixed_poly_values[0][i] << ");" << std::endl;
817+
result << "\t\t\tpoly_at_eta = basic_marshalling.get_uint256_be(blob, " << point_offset+(poly_points-1)*32 << ");" << "// " << i << std::endl;
818+
result << "\t\t\tif(poly_at_eta != " << std::showbase<< std::hex << fixed_poly_values[0][i] << ") return false;" << std::endl;
805819
point_offset += 32*poly_points;
806820
++i;
807821
++j;
808822
}
809823

810-
result << "\t\t\t// 2 - special selectors " << std::endl;
824+
result << "\t\t\t/* 2 - special selectors */" << std::endl;
811825
poly_points = 3;
812826
j = 0;
813827
while (j < 2) {
814-
result << "\t\t\tb = b && (basic_marshalling.get_uint256_be(blob, " << point_offset+(poly_points-1)*32 << ") != " << std::showbase<< std::hex << fixed_poly_values[0][i] << ");" << std::endl;
828+
result << "\t\t\tpoly_at_eta = basic_marshalling.get_uint256_be(blob, " << point_offset+(poly_points-1)*32 << ");" << "// " << i << std::endl;
829+
result << "\t\t\tif(poly_at_eta != " << std::showbase<< std::hex << fixed_poly_values[0][i] << ") return false;" << std::endl;
815830
point_offset += 32*poly_points;
816831
++i;
817832
++j;
818833
}
819834

820835
std::size_t column_rotation_offset = PlaceholderParams::witness_columns + PlaceholderParams::public_input_columns;
821-
result << "\t\t\t// - constant columns " << std::endl;
836+
result << "\t\t\t/* 3 - constant columns */" << std::endl;
822837
j = 0;
823838
while (j < PlaceholderParams::arithmetization_params::constant_columns) {
824839
poly_points = _common_data.columns_rotations[column_rotation_offset + j].size()+1;
825-
result << "\t\t\tb= b & (basic_marshalling.get_uint256_be(blob, " << point_offset+(poly_points-1)*32 << ") != " << std::showbase<< std::hex << fixed_poly_values[0][i] << ");" << std::endl;
840+
result << "\t\t\tpoly_at_eta = basic_marshalling.get_uint256_be(blob, " << point_offset+(poly_points-1)*32 << ");" << "// " << i << std::endl;
841+
result << "\t\t\tif(poly_at_eta != " << std::showbase<< std::hex << fixed_poly_values[0][i] << ") return false;" << std::endl;
826842
point_offset += 32*poly_points;
827843
++i;
828844
++j;
829845
}
830846

831-
result << "\t\t\t// 4 - selector columns" << std::endl;
847+
result << "\t\t\t/* 4 - selector columns */" << std::endl;
832848
column_rotation_offset = PlaceholderParams::witness_columns + PlaceholderParams::public_input_columns + PlaceholderParams::constant_columns;
833849
j = 0;
834850
while (j < PlaceholderParams::arithmetization_params::selector_columns) {
835851
poly_points = _common_data.columns_rotations[column_rotation_offset + j].size()+1;
836-
result << "\t\t\tb = b & (basic_marshalling.get_uint256_be(blob, " << point_offset+(poly_points-1)*32 << ") != " << std::showbase<< std::hex << fixed_poly_values[0][i] << ");" << std::endl;
852+
result << "\t\t\tpoly_at_eta = basic_marshalling.get_uint256_be(blob, " << point_offset+(poly_points-1)*32 << ");" << "// " << i << std::endl;
853+
result << "\t\t\tif(poly_at_eta != " << std::showbase<< std::hex << fixed_poly_values[0][i] << ") return false;" << std::endl;
837854
point_offset += 32*(poly_points);
838855
++i;
839856
++j;
840857
}
841858

842-
result << "\t\t\tif(!b) return false;" << std::endl;
843-
result << "\t\t} */" << std::endl;
859+
eta_hash::digest_type hash_result = crypto3::hash<eta_hash>(eta_buf);
860+
result << "\t\t\t/* Check keccak(points) */" << std::endl;
861+
result << "\t\t\tif ( bytes32(0x" << std::to_string(hash_result).data() << ") != keccak256(abi.encode(points))) {" << std::endl;
862+
result << "\t\t\t\treturn false;" << std::endl;
863+
result << "\t\t\t}" << std::endl;
864+
result << "\t\t}" << std::endl;
865+
844866
return result.str();
845867
}
846868

test/detail/circuits.hpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ namespace nil {
240240
template<typename FieldType>
241241
circuit_description<FieldType, placeholder_circuit_params<FieldType, arithmetization_params_t>, 5, 4>
242242
circuit_test_t(
243-
typename FieldType::value_type pi0 = 0,
244-
typename nil::crypto3::random::algebraic_engine<FieldType> alg_rnd = nil::crypto3::random::algebraic_engine<FieldType>(),
245-
boost::random::mt11213b rnd = boost::random::mt11213b()
243+
typename FieldType::value_type pi0,// = 0,
244+
typename nil::crypto3::random::algebraic_engine<FieldType> alg_rnd, //= nil::crypto3::random::algebraic_engine<FieldType>(),
245+
boost::random::mt11213b rnd// = boost::random::mt11213b()
246246
) {
247247
using assignment_type = typename FieldType::value_type;
248248

@@ -268,16 +268,16 @@ namespace nil {
268268

269269
// init values
270270
typename FieldType::value_type one = FieldType::value_type::one();
271-
table[0][0] = algebra::random_element<FieldType>();
272-
table[1][0] = algebra::random_element<FieldType>();
273-
table[2][0] = algebra::random_element<FieldType>();
271+
table[0][0] = alg_rnd();
272+
table[1][0] = alg_rnd();
273+
table[2][0] = alg_rnd();
274274
table[3][0] = pi0;
275275
q_add[0] = FieldType::value_type::zero();
276276
q_mul[0] = FieldType::value_type::zero();
277277

278278
// fill rows with ADD gate
279279
for (std::size_t i = 1; i < 3; i++) {
280-
table[0][i] = algebra::random_element<FieldType>();
280+
table[0][i] = alg_rnd();
281281
table[1][i] = table[2][i - 1];
282282
table[2][i] = table[0][i] + table[1][i];
283283
table[3][i] = FieldType::value_type::zero();
@@ -293,7 +293,7 @@ namespace nil {
293293

294294
// fill rows with MUL gate
295295
for (std::size_t i = 3; i < 5; i++) {
296-
table[0][i] = algebra::random_element<FieldType>();
296+
table[0][i] = alg_rnd();
297297
table[1][i] = table[3][0];
298298
table[2][i] = table[0][i] * table[1][i] + table[0][i - 1];
299299
table[3][i] = FieldType::value_type::zero();
@@ -357,7 +357,7 @@ namespace nil {
357357

358358
std::vector<plonk_constraint<FieldType>> mul_gate_costraints {mul_constraint};
359359
plonk_gate<FieldType, plonk_constraint<FieldType>> mul_gate(1, mul_gate_costraints);
360-
//test_circuit.gates.push_back(mul_gate);
360+
test_circuit.gates.push_back(mul_gate);
361361

362362
return test_circuit;
363363
}

test/transpiler.cpp

+24-11
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
#include <nil/crypto3/random/algebraic_engine.hpp>
7272

7373
#include <nil/blueprint/transpiler/evm_verifier_gen.hpp>
74-
#include <nil/blueprint/transpiler/recursive_verifier_generator.hpp>
74+
// #include <nil/blueprint/transpiler/recursive_verifier_generator.hpp>
7575

7676
#include "./detail/circuits.hpp"
7777

@@ -120,6 +120,7 @@ typename fri_type::params_type create_fri_params(std::size_t degree_log, const i
120120

121121
return params;
122122
}
123+
123124
// *******************************************************************************
124125
// * Randomness setup
125126
// *******************************************************************************/
@@ -132,7 +133,7 @@ nil::crypto3::random::algebraic_engine<FieldType> test_global_alg_rnd_engine;
132133
struct test_initializer {
133134
// Enumerate all fields used in tests;
134135
using field1_type = algebra::curves::pallas::base_field_type;
135-
136+
using field2_type = algebra::curves::bls12<381>::scalar_field_type;
136137
test_initializer() {
137138
test_global_seed = 0;
138139

@@ -155,6 +156,7 @@ struct test_initializer {
155156
BOOST_TEST_MESSAGE("test_global_seed = " << test_global_seed);
156157
test_global_rnd_engine = boost::random::mt11213b(test_global_seed);
157158
test_global_alg_rnd_engine<field1_type> = nil::crypto3::random::algebraic_engine<field1_type>(test_global_seed);
159+
test_global_alg_rnd_engine<field2_type> = nil::crypto3::random::algebraic_engine<field2_type>(test_global_seed);
158160
}
159161

160162
void setup() {
@@ -176,7 +178,6 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit1)
176178
using merkle_hash_type = hashes::keccak_1600<256>;
177179
using transcript_hash_type = hashes::keccak_1600<256>;
178180
constexpr static const std::size_t table_rows_log = 4;
179-
constexpr static const std::size_t table_rows = 1 << table_rows_log;
180181

181182
struct placeholder_test_params {
182183
constexpr static const std::size_t table_rows = 1 << table_rows_log;
@@ -192,7 +193,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit1)
192193
using arithmetization_params =
193194
plonk_arithmetization_params<witness_columns, public_input_columns, constant_columns, selector_columns>;
194195

195-
constexpr static const std::size_t lambda = 1;
196+
constexpr static const std::size_t lambda = 40;
196197
constexpr static const std::size_t m = 2;
197198
};
198199
typedef placeholder_circuit_params<field_type, typename placeholder_test_params::arithmetization_params> circuit_params;
@@ -207,16 +208,17 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit1)
207208
crypto3::zk::commitments::proof_of_work<transcript_hash_type, std::uint32_t, 0xFFFF8000 >
208209
>;
209210

210-
211211
using lpc_type = commitments::list_polynomial_commitment<field_type, lpc_params_type>;
212212
using lpc_scheme_type = typename commitments::lpc_commitment_scheme<lpc_type>;
213213
using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params<circuit_params, lpc_scheme_type>;
214214
using policy_type = zk::snark::detail::placeholder_policy<field_type, lpc_placeholder_params_type>;
215+
215216
BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
216217
auto circuit = circuit_test_1<field_type>(test_global_alg_rnd_engine<field_type>);
218+
217219
plonk_table_description<field_type, typename circuit_params::arithmetization_params> desc;
218220

219-
desc.rows_amount = table_rows;
221+
desc.rows_amount = placeholder_test_params::table_rows;
220222
desc.usable_rows_amount = placeholder_test_params::usable_rows;
221223

222224
typename policy_type::constraint_system_type constraint_system(
@@ -227,7 +229,6 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
227229
);
228230
typename policy_type::variable_assignment_type assignments = circuit.table;
229231

230-
231232
std::vector<std::size_t> columns_with_copy_constraints = {0, 1, 2, 3};
232233

233234

@@ -261,7 +262,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit2)
261262
using curve_type = algebra::curves::bls12<381>;
262263
using field_type = typename curve_type::scalar_field_type;
263264

264-
constexpr static const std::size_t table_rows_log = 4;
265+
constexpr static const std::size_t table_rows_log = 3;
265266
constexpr static const std::size_t table_rows = 1 << table_rows_log;
266267
constexpr static const std::size_t permutation_size = 4;
267268
constexpr static const std::size_t usable_rows = (1 << table_rows_log) - 3;
@@ -300,6 +301,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit2)
300301
using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params<circuit_t_params, lpc_scheme_type>;
301302

302303
using policy_type = zk::snark::detail::placeholder_policy<field_type, circuit_t_params>;
304+
303305
BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
304306
auto pi0 = test_global_alg_rnd_engine<field_type>();
305307
auto circuit = circuit_test_t<field_type>(pi0, test_global_alg_rnd_engine<field_type>, test_global_rnd_engine);
@@ -316,7 +318,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
316318
);
317319
typename policy_type::variable_assignment_type assignments = circuit.table;
318320

319-
std::vector<std::size_t> columns_with_copy_constraints = {0, 1, 2, 3};
321+
std::vector<std::size_t> columns_with_copy_constraints = {0, 1, 2, 3};
320322

321323
bool verifier_res;
322324

@@ -384,8 +386,10 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3)
384386
using lpc_scheme_type = typename commitments::lpc_commitment_scheme<lpc_type>;
385387
using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params<circuit_params, lpc_scheme_type>;
386388
using policy_type = zk::snark::detail::placeholder_policy<field_type, circuit_params>;
389+
387390
BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
388-
auto circuit = circuit_test_3<field_type>();
391+
auto circuit = circuit_test_3<field_type>(test_global_alg_rnd_engine<field_type>, test_global_rnd_engine);
392+
389393
plonk_table_description<field_type, typename circuit_params::arithmetization_params> desc;
390394

391395
desc.rows_amount = table_rows;
@@ -461,10 +465,12 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4)
461465
using lpc_scheme_type = typename commitments::lpc_commitment_scheme<lpc_type>;
462466
using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params<circuit_params, lpc_scheme_type>;
463467
using policy_type = zk::snark::detail::placeholder_policy<field_type, circuit_params>;
468+
464469
BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
465-
auto circuit = circuit_test_4<field_type>(test_global_alg_rnd_engine<field_type>, test_global_rnd_engine);
470+
auto circuit = circuit_test_4<field_type>(test_global_alg_rnd_engine<field_type>);
466471

467472
plonk_table_description<field_type, typename circuit_params::arithmetization_params> desc;
473+
468474
desc.rows_amount = table_rows;
469475
desc.usable_rows_amount = usable_rows;
470476

@@ -540,10 +546,12 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit6)
540546
using lpc_scheme_type = typename commitments::lpc_commitment_scheme<lpc_type>;
541547
using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params<circuit_params, lpc_scheme_type>;
542548
using policy_type = zk::snark::detail::placeholder_policy<field_type, circuit_params>;
549+
543550
BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
544551
auto circuit = circuit_test_6<field_type>(test_global_alg_rnd_engine<field_type>, test_global_rnd_engine);
545552

546553
plonk_table_description<field_type, typename circuit_params::arithmetization_params> desc;
554+
547555
desc.rows_amount = table_rows;
548556
desc.usable_rows_amount = usable_rows;
549557

@@ -563,6 +571,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
563571
typename placeholder_public_preprocessor<field_type, lpc_placeholder_params_type>::preprocessed_data_type
564572
preprocessed_public_data = placeholder_public_preprocessor<field_type, lpc_placeholder_params_type>::process(
565573
constraint_system, assignments.public_table(), desc, lpc_scheme, columns_with_copy_constraints.size());
574+
566575
auto printer = nil::blueprint::evm_verifier_printer<lpc_placeholder_params_type>(
567576
constraint_system,
568577
preprocessed_public_data.common_data,
@@ -619,6 +628,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit7)
619628
using lpc_scheme_type = typename commitments::lpc_commitment_scheme<lpc_type>;
620629
using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params<circuit_params, lpc_scheme_type>;
621630
using policy_type = zk::snark::detail::placeholder_policy<field_type, circuit_params>;
631+
622632
BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
623633
auto circuit = circuit_test_7<field_type>(test_global_alg_rnd_engine<field_type>, test_global_rnd_engine);
624634
plonk_table_description<field_type, typename circuit_params::arithmetization_params> desc;
@@ -639,6 +649,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
639649
lpc_scheme_type lpc_scheme(fri_params);
640650

641651
std::vector<std::size_t> columns_with_copy_constraints = {0, 1, 2, 3};
652+
642653
transcript_type transcript;
643654

644655
typename placeholder_public_preprocessor<field_type, lpc_placeholder_params_type>::preprocessed_data_type
@@ -661,6 +672,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
661672
}
662673
BOOST_AUTO_TEST_SUITE_END()
663674

675+
#if 0
664676
BOOST_AUTO_TEST_SUITE(recursive_circuit1)
665677
using Endianness = nil::marshalling::option::big_endian;
666678
using TTypeBase = nil::marshalling::field_type<Endianness>;
@@ -1278,3 +1290,4 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) {
12781290
}
12791291
}
12801292
BOOST_AUTO_TEST_SUITE_END()
1293+
#endif

0 commit comments

Comments
 (0)