diff --git a/include/nil/blueprint/basic_non_native_policy.hpp b/include/nil/blueprint/basic_non_native_policy.hpp index 23ac3ce3b..1bdc2daca 100644 --- a/include/nil/blueprint/basic_non_native_policy.hpp +++ b/include/nil/blueprint/basic_non_native_policy.hpp @@ -33,26 +33,27 @@ #include - namespace nil { namespace blueprint { namespace detail { template - struct basic_non_native_policy_field_type : public basic_non_native_policy_field_type_base> { - }; + struct basic_non_native_policy_field_type + : public basic_non_native_policy_field_type_base> { }; /* * Specialization for non-native Ed25519 base field element on Pallas base field */ template<> - struct basic_non_native_policy_field_type< - typename crypto3::algebra::curves::pallas::base_field_type, - typename crypto3::algebra::curves::ed25519::base_field_type - > : public basic_non_native_policy_field_type_base> { + struct basic_non_native_policy_field_type + : public basic_non_native_policy_field_type_base< + typename crypto3::algebra::curves::pallas::base_field_type, + typename crypto3::algebra::curves::ed25519::base_field_type, + chopped_lengths_storage<58, 66, 66, 66>> { using basic_non_native_policy_field_type_base::chopped_value_type; - }; /* @@ -62,20 +63,21 @@ namespace nil { struct basic_non_native_policy_field_type { - using non_native_var_type = crypto3::zk::snark::plonk_variable; + using non_native_var_type = crypto3::zk::snark::plonk_variable< + typename crypto3::algebra::curves::pallas::base_field_type::value_type>; }; // /* // * Specialization for non-native Pallas scalar field element on Pallas base field // */ template<> - struct basic_non_native_policy_field_type< - typename crypto3::algebra::curves::pallas::base_field_type, - typename crypto3::algebra::curves::pallas::scalar_field_type - > : public basic_non_native_policy_field_type_base> { + struct basic_non_native_policy_field_type + : public basic_non_native_policy_field_type_base< + typename crypto3::algebra::curves::pallas::base_field_type, + typename crypto3::algebra::curves::pallas::scalar_field_type, chopped_lengths_storage<2, 254>> { using basic_non_native_policy_field_type_base::chopped_value_type; - }; // /* @@ -85,7 +87,6 @@ namespace nil { struct basic_non_native_policy_field_type { using value_type = crypto3::zk::snark::plonk_variable; - }; } // namespace detail diff --git a/include/nil/blueprint/detail/basic_non_native_policy.hpp b/include/nil/blueprint/detail/basic_non_native_policy.hpp index 19609be35..819744bc8 100644 --- a/include/nil/blueprint/detail/basic_non_native_policy.hpp +++ b/include/nil/blueprint/detail/basic_non_native_policy.hpp @@ -34,7 +34,6 @@ #include #include - namespace nil { namespace blueprint { namespace detail { @@ -50,8 +49,10 @@ namespace nil { using native_field_t = BlueprintFieldType; using var_t = crypto3::zk::snark::plonk_variable; - static constexpr std::size_t chopped_elements_amount = sizeof(chopped_lengths_storage::values)/sizeof(std::size_t); - static_assert(chopped_elements_amount != 0, "native_bit_lengths must be specialized for the field types"); + static constexpr std::size_t chopped_elements_amount = + sizeof(chopped_lengths_storage::values) / sizeof(std::size_t); + static_assert(chopped_elements_amount != 0, + "native_bit_lengths must be specialized for the field types"); using chopped_value_type = std::array; using non_native_var_t = std::array; @@ -69,41 +70,44 @@ namespace nil { // TODO: Check status here? auto &members = chopping_field_instance.value(); - return convert_to_chopped_value_type(members, std::make_index_sequence{}); + return convert_to_chopped_value_type(members, std::make_index_sequence {}); + } + + private: + using be_field_base_t = marshalling::field_type; + + template + using intermediate_t = + crypto3::marshalling::types::pure_field_element>; + + // We need to reverse the lengths, because that's how the serialization works. Fields are written from + // right to left + template + using intermediate_for_index_t = + intermediate_t; + + template + static constexpr std::tuple...> + generate_bitfield_tuple(std::index_sequence) { + return {}; } - private: - using be_field_base_t = marshalling::field_type; - - template - using intermediate_t = crypto3::marshalling::types::pure_field_element< - be_field_base_t, - typename native_field_t::value_type, - marshalling::option::fixed_bit_length - >; - - // We need to reverse the lengths, because that's how the serialization works. Fields are written from right to left - template - using intermediate_for_index_t = intermediate_t; - - template - static constexpr std::tuple...> generate_bitfield_tuple(std::index_sequence) { - return {}; - } - - using chopping_field = nil::crypto3::marshalling::types::bitfield< - be_field_base_t, - decltype(generate_bitfield_tuple(std::make_index_sequence{})) - >; - - template - static chopped_value_type convert_to_chopped_value_type(const typename chopping_field::value_type& members, std::index_sequence) { - return {std::get(members).value()...}; - } + using chopping_field = nil::crypto3::marshalling::types::bitfield< + be_field_base_t, + decltype(generate_bitfield_tuple(std::make_index_sequence {}))>; + + template + static chopped_value_type + convert_to_chopped_value_type(const typename chopping_field::value_type &members, + std::index_sequence) { + return {std::get(members).value()...}; + } }; } // namespace detail - } // namespace blueprint + } // namespace blueprint } // namespace nil #endif // CRYPTO3_BLUEPRINT_BASIC_NON_NATIVE_POLICY_DETAIL_HPP diff --git a/test/algebra/fields/plonk/non_native/non_native_policy.cpp b/test/algebra/fields/plonk/non_native/non_native_policy.cpp index 4f6bf1bad..3340317cf 100644 --- a/test/algebra/fields/plonk/non_native/non_native_policy.cpp +++ b/test/algebra/fields/plonk/non_native/non_native_policy.cpp @@ -7,33 +7,22 @@ #include #include <../test/algebra/fields/plonk/non_native/glue_non_native.hpp> - using namespace nil; template -void test_chopping( - const typename NonNativeFieldType::value_type& non_native_field_el -) { +void test_chopping(const typename NonNativeFieldType::value_type &non_native_field_el) { using non_native_policy_type = - blueprint::detail::basic_non_native_policy_field_type< - NativeFieldType, - NonNativeFieldType - >; + blueprint::detail::basic_non_native_policy_field_type; using chunked_non_native_type = typename non_native_policy_type::chopped_value_type; auto chopping_result = non_native_policy_type::chop_non_native(non_native_field_el); - std::cout << std::hex; + std::cout << std::hex; for (std::size_t i = 0; i < 4; i++) { std::cout << chopping_result[i].data << " "; } - assert( - (glue_non_native< - NativeFieldType, - NonNativeFieldType - >(chopping_result)) == non_native_field_el - ); + assert((glue_non_native(chopping_result)) == non_native_field_el); } BOOST_AUTO_TEST_SUITE(blueprint_non_native_policy_test_suite) @@ -41,24 +30,16 @@ BOOST_AUTO_TEST_SUITE(blueprint_non_native_policy_test_suite) BOOST_AUTO_TEST_CASE(blueprint_non_native_policy_25519) { using non_native_field_type = typename crypto3::algebra::curves::ed25519::base_field_type; using native_field_type = crypto3::algebra::curves::pallas::base_field_type; + test_chopping(0x0); test_chopping( - 0x0 - ); - test_chopping( - 0x274dbce8d15179969bc0d49fa725bddf9de555e0ba6a693c6adb52fc9ee7a82c_cppui252 - ); - test_chopping( - 0x5ce98c61b05f47fe2eae9a542bd99f6b2e78246231640b54595febfd51eb853_cppui252 - ); + 0x274dbce8d15179969bc0d49fa725bddf9de555e0ba6a693c6adb52fc9ee7a82c_cppui252); test_chopping( - 0x2ad46cbfb78773b6254adc1d80c6efa02f3bf948c37e5a2222136421d7bec942_cppui252 - ); + 0x5ce98c61b05f47fe2eae9a542bd99f6b2e78246231640b54595febfd51eb853_cppui252); test_chopping( - 0x14e9693f16d75f7065ce51e1f46ae6c60841ca1e0cf264eda26398e36ca2ed69_cppui252 - ); + 0x2ad46cbfb78773b6254adc1d80c6efa02f3bf948c37e5a2222136421d7bec942_cppui252); test_chopping( - non_native_field_type::modulus - 1 - ); + 0x14e9693f16d75f7065ce51e1f46ae6c60841ca1e0cf264eda26398e36ca2ed69_cppui252); + test_chopping(non_native_field_type::modulus - 1); } BOOST_AUTO_TEST_SUITE_END()