to_curve_value.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2021 Ilias Khairullin <ilias@nil.foundation>
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_HASH_TO_CURVE_VALUE_HPP
27 #define CRYPTO3_HASH_TO_CURVE_VALUE_HPP
28 
29 #include <type_traits>
30 #include <iterator>
31 
32 #include <boost/assert.hpp>
33 #include <boost/concept_check.hpp>
34 
35 #include <boost/range/concepts.hpp>
36 
37 #include <boost/mpl/front.hpp>
38 #include <boost/mpl/apply.hpp>
39 
41 
42 namespace nil {
43  namespace crypto3 {
44  namespace hashes {
45  namespace detail {
46  template<typename PaddingAccumulator>
48  typedef PaddingAccumulator accumulator_set_type;
49  typedef
50  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
51 
53  }
54 
56  };
57 
58  template<typename PaddingAccumulator>
60  typedef PaddingAccumulator accumulator_set_type;
61  typedef
62  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
63 
65  accumulator_set(std::forward<accumulator_set_type>(acc)) {
66  }
67 
69  };
70 
71  template<typename PaddingStateImpl>
72  struct range_to_curve_impl : public PaddingStateImpl {
73  typedef PaddingStateImpl padding_state_impl_type;
74 
75  typedef typename padding_state_impl_type::accumulator_type accumulator_type;
76  typedef typename padding_state_impl_type::accumulator_set_type accumulator_set_type;
77 
78  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
80 
81  template<typename SinglePassRange>
82  range_to_curve_impl(const SinglePassRange &range, accumulator_set_type &&ise) :
83  PaddingStateImpl(std::forward<accumulator_set_type>(ise)) {
84  BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
85 
86  this->accumulator_set(range);
87  }
88 
89  template<typename InputIterator>
90  range_to_curve_impl(InputIterator first, InputIterator last, accumulator_set_type &&ise) :
91  PaddingStateImpl(std::forward<accumulator_set_type>(ise)) {
92  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
93 
94  this->accumulator_set(first, ::nil::crypto3::accumulators::iterator_last = last);
95  }
96 
97  template<typename OutputRange>
98  operator OutputRange() const {
99  result_type result =
100  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
101  return OutputRange(result.cbegin(), result.cend());
102  }
103 
104  operator result_type() const {
105  return boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
106  }
107 
108  operator accumulator_set_type &() const {
109  return this->accumulator_set;
110  }
111 
112 #ifdef CRYPTO3_ASCII_STRING_CODEC_OUTPUT
113  template<typename Char, typename CharTraits, typename Alloc>
114  operator std::basic_string<Char, CharTraits, Alloc>() const {
115  return std::to_string(
116  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set));
117  }
118 #endif
119  };
120 
121  template<typename PaddingStateImpl, typename OutputIterator>
122  struct itr_to_curve_impl : public PaddingStateImpl {
123  private:
124  mutable OutputIterator out;
125 
126  public:
127  typedef PaddingStateImpl padding_state_impl_type;
128 
129  typedef typename padding_state_impl_type::accumulator_type accumulator_type;
130  typedef typename padding_state_impl_type::accumulator_set_type accumulator_set_type;
131 
132  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
134 
135  template<typename SinglePassRange>
136  itr_to_curve_impl(const SinglePassRange &range, OutputIterator out, accumulator_set_type &&ise) :
137  PaddingStateImpl(std::forward<accumulator_set_type>(ise)), out(std::move(out)) {
138  BOOST_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
139  BOOST_CONCEPT_ASSERT((boost::OutputIteratorConcept<OutputIterator, result_type>));
140 
141  this->accumulator_set(range);
142  }
143 
144  template<typename InputIterator>
145  itr_to_curve_impl(InputIterator first, InputIterator last, OutputIterator out,
146  accumulator_set_type &&ise) :
147  PaddingStateImpl(std::forward<accumulator_set_type>(ise)),
148  out(std::move(out)) {
149  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
150  BOOST_CONCEPT_ASSERT((boost::OutputIteratorConcept<OutputIterator, result_type>));
151 
152  this->accumulator_set(first, ::nil::crypto3::accumulators::iterator_last = last);
153  }
154 
155  operator OutputIterator() const {
156  *out++ = boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
157  return out;
158  }
159  };
160  } // namespace detail
161  } // namespace hashes
162  } // namespace crypto3
163 } // namespace nil
164 
165 #endif // CRYPTO3_CODEC_POSTPROCESSOR_HPP
boost::accumulators::accumulator_set< digest< ProcessingMode::block_bits >, boost::accumulators::features< accumulators::tag::block< ProcessingMode > >, std::size_t > accumulator_set
Accumulator set with pre-defined block cipher accumulator params.
Definition: block/include/nil/crypto3/block/cipher_state.hpp:51
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
Definition: pair.hpp:31
Definition: hash_state.hpp:43
Definition: to_curve_value.hpp:122
padding_state_impl_type::accumulator_set_type accumulator_set_type
Definition: to_curve_value.hpp:130
itr_to_curve_impl(const SinglePassRange &range, OutputIterator out, accumulator_set_type &&ise)
Definition: to_curve_value.hpp:136
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: to_curve_value.hpp:133
padding_state_impl_type::accumulator_type accumulator_type
Definition: to_curve_value.hpp:129
itr_to_curve_impl(InputIterator first, InputIterator last, OutputIterator out, accumulator_set_type &&ise)
Definition: to_curve_value.hpp:145
PaddingStateImpl padding_state_impl_type
Definition: to_curve_value.hpp:127
Definition: to_curve_value.hpp:72
padding_state_impl_type::accumulator_set_type accumulator_set_type
Definition: to_curve_value.hpp:76
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: to_curve_value.hpp:79
padding_state_impl_type::accumulator_type accumulator_type
Definition: to_curve_value.hpp:75
range_to_curve_impl(InputIterator first, InputIterator last, accumulator_set_type &&ise)
Definition: to_curve_value.hpp:90
PaddingStateImpl padding_state_impl_type
Definition: to_curve_value.hpp:73
range_to_curve_impl(const SinglePassRange &range, accumulator_set_type &&ise)
Definition: to_curve_value.hpp:82
Definition: to_curve_value.hpp:47
PaddingAccumulator accumulator_set_type
Definition: to_curve_value.hpp:48
ref_to_curve_impl(accumulator_set_type &&acc)
Definition: to_curve_value.hpp:52
accumulator_set_type & accumulator_set
Definition: to_curve_value.hpp:55
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: to_curve_value.hpp:50
Definition: to_curve_value.hpp:59
value_to_curve_impl(accumulator_set_type &&acc)
Definition: to_curve_value.hpp:64
PaddingAccumulator accumulator_set_type
Definition: to_curve_value.hpp:60
accumulator_set_type accumulator_set
Definition: to_curve_value.hpp:68
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: to_curve_value.hpp:62