|
| 1 | +//---------------------------------------------------------------------------// |
| 2 | +// Copyright (c) 2020-2022 Mikhail Komarov <[email protected]> |
| 3 | +// Copyright (c) 2020-2022 Nikita Kaskov <[email protected]> |
| 4 | +// |
| 5 | +// MIT License |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | +//---------------------------------------------------------------------------// |
| 25 | + |
| 26 | +#ifndef CRYPTO3_BLUEPRINT_BASIC_NON_NATIVE_POLICY_DETAIL_HPP |
| 27 | +#define CRYPTO3_BLUEPRINT_BASIC_NON_NATIVE_POLICY_DETAIL_HPP |
| 28 | + |
| 29 | +#include <array> |
| 30 | + |
| 31 | +#include <nil/marshalling/algorithms/pack.hpp> |
| 32 | +#include <nil/marshalling/field_type.hpp> |
| 33 | +#include <nil/marshalling/options.hpp> |
| 34 | +#include <nil/marshalling/status_type.hpp> |
| 35 | +#include <nil/crypto3/marshalling/multiprecision/types/bitfield.hpp> |
| 36 | + |
| 37 | + |
| 38 | +namespace nil { |
| 39 | + namespace blueprint { |
| 40 | + namespace detail { |
| 41 | + |
| 42 | + template<std::size_t... Ns> |
| 43 | + struct chopped_lengths_storage { |
| 44 | + static constexpr std::size_t values[] = {Ns...}; |
| 45 | + }; |
| 46 | + |
| 47 | + template<typename BlueprintFieldType, typename OperatingFieldType, typename chopped_lengths_storage> |
| 48 | + struct basic_non_native_policy_field_type_base { |
| 49 | + using non_native_field_t = OperatingFieldType; |
| 50 | + using native_field_t = BlueprintFieldType; |
| 51 | + using var_t = crypto3::zk::snark::plonk_variable<typename native_field_t::value_type>; |
| 52 | + |
| 53 | + static constexpr std::size_t chopped_elements_amount = sizeof(chopped_lengths_storage::values)/sizeof(std::size_t); |
| 54 | + static_assert(chopped_elements_amount != 0, "native_bit_lengths must be specialized for the field types"); |
| 55 | + |
| 56 | + using chopped_value_type = std::array<typename native_field_t::value_type, chopped_elements_amount>; |
| 57 | + using non_native_var_t = std::array<var_t, chopped_elements_amount>; |
| 58 | + |
| 59 | + static chopped_value_type chop_non_native(typename non_native_field_t::value_type input) { |
| 60 | + using unit_type = unsigned char; |
| 61 | + nil::marshalling::status_type status; |
| 62 | + |
| 63 | + std::vector<unit_type> cv = marshalling::pack<marshalling::option::big_endian>(input, status); |
| 64 | + |
| 65 | + // TODO: Check status here? |
| 66 | + |
| 67 | + chopping_field chopping_field_instance = marshalling::pack(input, status); |
| 68 | + |
| 69 | + // TODO: Check status here? |
| 70 | + |
| 71 | + auto &members = chopping_field_instance.value(); |
| 72 | + return convert_to_chopped_value_type(members, std::make_index_sequence<chopped_elements_amount>{}); |
| 73 | + } |
| 74 | + |
| 75 | + private: |
| 76 | + using be_field_base_t = marshalling::field_type<marshalling::option::big_endian>; |
| 77 | + |
| 78 | + template <std::size_t bit_length> |
| 79 | + using intermediate_t = crypto3::marshalling::types::pure_field_element< |
| 80 | + be_field_base_t, |
| 81 | + typename native_field_t::value_type, |
| 82 | + marshalling::option::fixed_bit_length<bit_length> |
| 83 | + >; |
| 84 | + |
| 85 | + // We need to reverse the lengths, because that's how the serialization works. Fields are written from right to left |
| 86 | + template <std::size_t Index> |
| 87 | + using intermediate_for_index_t = intermediate_t<chopped_lengths_storage::values[chopped_elements_amount-Index-1]>; |
| 88 | + |
| 89 | + template <std::size_t... Indices> |
| 90 | + static constexpr std::tuple<intermediate_for_index_t<Indices>...> generate_bitfield_tuple(std::index_sequence<Indices...>) { |
| 91 | + return {}; |
| 92 | + } |
| 93 | + |
| 94 | + using chopping_field = nil::crypto3::marshalling::types::bitfield< |
| 95 | + be_field_base_t, |
| 96 | + decltype(generate_bitfield_tuple(std::make_index_sequence<chopped_elements_amount>{})) |
| 97 | + >; |
| 98 | + |
| 99 | + template <std::size_t... Indices> |
| 100 | + static chopped_value_type convert_to_chopped_value_type(const typename chopping_field::value_type& members, std::index_sequence<Indices...>) { |
| 101 | + return {std::get<Indices>(members).value()...}; |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + } // namespace detail |
| 106 | + } // namespace blueprint |
| 107 | +} // namespace nil |
| 108 | + |
| 109 | +#endif // CRYPTO3_BLUEPRINT_BASIC_NON_NATIVE_POLICY_DETAIL_HPP |
0 commit comments