Skip to content

Commit

Permalink
Apply clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass authored and nkaskov committed Nov 10, 2023
1 parent 8f6300c commit a9b220b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 77 deletions.
31 changes: 16 additions & 15 deletions include/nil/blueprint/basic_non_native_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@

#include <nil/blueprint/detail/basic_non_native_policy.hpp>


namespace nil {
namespace blueprint {
namespace detail {

template<typename BlueprintFieldType, typename OperatingFieldType>
struct basic_non_native_policy_field_type : public basic_non_native_policy_field_type_base<BlueprintFieldType, OperatingFieldType, chopped_lengths_storage<>> {
};
struct basic_non_native_policy_field_type
: public basic_non_native_policy_field_type_base<BlueprintFieldType, OperatingFieldType,
chopped_lengths_storage<>> { };

/*
* 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<typename crypto3::algebra::curves::pallas::base_field_type, typename crypto3::algebra::curves::ed25519::base_field_type, chopped_lengths_storage<58, 66, 66, 66>> {
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<
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;

};

/*
Expand All @@ -62,20 +63,21 @@ namespace nil {
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
typename crypto3::algebra::curves::ed25519::scalar_field_type> {

using non_native_var_type = crypto3::zk::snark::plonk_variable<typename crypto3::algebra::curves::pallas::base_field_type::value_type>;
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<typename crypto3::algebra::curves::pallas::base_field_type, typename crypto3::algebra::curves::pallas::scalar_field_type, chopped_lengths_storage<2, 254>> {
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<
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;

};

// /*
Expand All @@ -85,7 +87,6 @@ namespace nil {
struct basic_non_native_policy_field_type<BlueprintFieldType, BlueprintFieldType> {

using value_type = crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>;

};
} // namespace detail

Expand Down
70 changes: 37 additions & 33 deletions include/nil/blueprint/detail/basic_non_native_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <nil/marshalling/status_type.hpp>
#include <nil/crypto3/marshalling/multiprecision/types/bitfield.hpp>


namespace nil {
namespace blueprint {
namespace detail {
Expand All @@ -50,8 +49,10 @@ namespace nil {
using native_field_t = BlueprintFieldType;
using var_t = crypto3::zk::snark::plonk_variable<typename native_field_t::value_type>;

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<typename native_field_t::value_type, chopped_elements_amount>;
using non_native_var_t = std::array<var_t, chopped_elements_amount>;
Expand All @@ -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<chopped_elements_amount>{});
return convert_to_chopped_value_type(members, std::make_index_sequence<chopped_elements_amount> {});
}

private:
using be_field_base_t = marshalling::field_type<marshalling::option::big_endian>;

template<std::size_t bit_length>
using intermediate_t =
crypto3::marshalling::types::pure_field_element<be_field_base_t,
typename native_field_t::value_type,
marshalling::option::fixed_bit_length<bit_length>>;

// We need to reverse the lengths, because that's how the serialization works. Fields are written from
// right to left
template<std::size_t Index>
using intermediate_for_index_t =
intermediate_t<chopped_lengths_storage::values[chopped_elements_amount - Index - 1]>;

template<std::size_t... Indices>
static constexpr std::tuple<intermediate_for_index_t<Indices>...>
generate_bitfield_tuple(std::index_sequence<Indices...>) {
return {};
}

private:
using be_field_base_t = marshalling::field_type<marshalling::option::big_endian>;

template <std::size_t bit_length>
using intermediate_t = crypto3::marshalling::types::pure_field_element<
be_field_base_t,
typename native_field_t::value_type,
marshalling::option::fixed_bit_length<bit_length>
>;

// We need to reverse the lengths, because that's how the serialization works. Fields are written from right to left
template <std::size_t Index>
using intermediate_for_index_t = intermediate_t<chopped_lengths_storage::values[chopped_elements_amount-Index-1]>;

template <std::size_t... Indices>
static constexpr std::tuple<intermediate_for_index_t<Indices>...> generate_bitfield_tuple(std::index_sequence<Indices...>) {
return {};
}

using chopping_field = nil::crypto3::marshalling::types::bitfield<
be_field_base_t,
decltype(generate_bitfield_tuple(std::make_index_sequence<chopped_elements_amount>{}))
>;

template <std::size_t... Indices>
static chopped_value_type convert_to_chopped_value_type(const typename chopping_field::value_type& members, std::index_sequence<Indices...>) {
return {std::get<Indices>(members).value()...};
}
using chopping_field = nil::crypto3::marshalling::types::bitfield<
be_field_base_t,
decltype(generate_bitfield_tuple(std::make_index_sequence<chopped_elements_amount> {}))>;

template<std::size_t... Indices>
static chopped_value_type
convert_to_chopped_value_type(const typename chopping_field::value_type &members,
std::index_sequence<Indices...>) {
return {std::get<Indices>(members).value()...};
}
};

} // namespace detail
} // namespace blueprint
} // namespace blueprint
} // namespace nil

#endif // CRYPTO3_BLUEPRINT_BASIC_NON_NATIVE_POLICY_DETAIL_HPP
39 changes: 10 additions & 29 deletions test/algebra/fields/plonk/non_native/non_native_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,39 @@
#include <nil/blueprint/basic_non_native_policy.hpp>
#include <../test/algebra/fields/plonk/non_native/glue_non_native.hpp>


using namespace nil;

template<typename NativeFieldType, typename NonNativeFieldType>
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<NativeFieldType, NonNativeFieldType>;
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<NativeFieldType, NonNativeFieldType>(chopping_result)) == non_native_field_el);
}

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<native_field_type, non_native_field_type>(0x0);
test_chopping<native_field_type, non_native_field_type>(
0x0
);
test_chopping<native_field_type, non_native_field_type>(
0x274dbce8d15179969bc0d49fa725bddf9de555e0ba6a693c6adb52fc9ee7a82c_cppui252
);
test_chopping<native_field_type, non_native_field_type>(
0x5ce98c61b05f47fe2eae9a542bd99f6b2e78246231640b54595febfd51eb853_cppui252
);
0x274dbce8d15179969bc0d49fa725bddf9de555e0ba6a693c6adb52fc9ee7a82c_cppui252);
test_chopping<native_field_type, non_native_field_type>(
0x2ad46cbfb78773b6254adc1d80c6efa02f3bf948c37e5a2222136421d7bec942_cppui252
);
0x5ce98c61b05f47fe2eae9a542bd99f6b2e78246231640b54595febfd51eb853_cppui252);
test_chopping<native_field_type, non_native_field_type>(
0x14e9693f16d75f7065ce51e1f46ae6c60841ca1e0cf264eda26398e36ca2ed69_cppui252
);
0x2ad46cbfb78773b6254adc1d80c6efa02f3bf948c37e5a2222136421d7bec942_cppui252);
test_chopping<native_field_type, non_native_field_type>(
non_native_field_type::modulus - 1
);
0x14e9693f16d75f7065ce51e1f46ae6c60841ca1e0cf264eda26398e36ca2ed69_cppui252);
test_chopping<native_field_type, non_native_field_type>(non_native_field_type::modulus - 1);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit a9b220b

Please sign in to comment.