From 23486885730acc2fcaf97fce3c271620752598e3 Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov <145333445+vo-nil@users.noreply.github.com> Date: Wed, 8 Nov 2023 12:26:20 +0300 Subject: [PATCH] Added grinding mask check in EVM code #49 (#50) * Added grinding mask check in EVM code #49 * added example of custom mask #49 NilFoundation/crypto3-zk-marshalling#53 NilFoundation/evm-placeholder-verification#84 --- include/nil/blueprint/transpiler/lpc_scheme_gen.hpp | 5 +++++ .../nil/blueprint/transpiler/templates/commitment_scheme.hpp | 5 +++-- test/transpiler.cpp | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp b/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp index 92fd5301cc..a13f1f6b39 100644 --- a/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp +++ b/include/nil/blueprint/transpiler/lpc_scheme_gen.hpp @@ -176,7 +176,12 @@ namespace nil { replacements["$POINTS_INITIALIZATION$"] = points_initializer.str(); replacements["$ETHA$"] = to_string(etha); if( PlaceholderParams::commitment_scheme_type::fri_type::use_grinding){ + auto params = PlaceholderParams::commitment_scheme_type::fri_type::grinding_type::get_params(); + uint32_t mask_value = params.template get("mask", 0); + std::stringstream mask_value_hex; + mask_value_hex << std::hex << std::showbase << std::setw(8) << std::setfill('0') << mask_value; replacements["$GRINDING_CHECK$"] = modular_commitment_grinding_check_template; + replacements["$GRINDING_MASK$"] = mask_value_hex.str(); } else { replacements["$GRINDING_CHECK$"] = ""; } diff --git a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp index 4901886ea2..b9b9bf563f 100644 --- a/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp +++ b/include/nil/blueprint/transpiler/templates/commitment_scheme.hpp @@ -8,8 +8,9 @@ namespace nil { std::string modular_commitment_grinding_check_template = R"( bytes calldata proof_of_work = blob[blob.length - 4:]; transcript.update_transcript(tr_state, proof_of_work); - transcript.get_integral_challenge_be(tr_state, 4); - )"; + uint256 p_o_w = transcript.get_integral_challenge_be(tr_state, 4); + if (p_o_w & $GRINDING_MASK$ != 0) return false; +)"; std::string modular_commitment_library_template = R"( // SPDX-License-Identifier: Apache-2.0. diff --git a/test/transpiler.cpp b/test/transpiler.cpp index 040b30e52b..b98d256efb 100644 --- a/test/transpiler.cpp +++ b/test/transpiler.cpp @@ -63,6 +63,7 @@ #include #include #include +#include #include #include @@ -200,9 +201,11 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit1) transcript_hash_type, placeholder_test_params::lambda, placeholder_test_params::m, - true + true, + crypto3::zk::commitments::proof_of_work >; + using lpc_type = commitments::list_polynomial_commitment; using lpc_scheme_type = typename commitments::lpc_commitment_scheme; using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params;