block/include/nil/crypto3/block/cipher_value.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Nikita Kaskov <nbering@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_BLOCK_CIPHER_VALUE_HPP
27 #define CRYPTO3_BLOCK_CIPHER_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 
39 
40 #include <nil/crypto3/detail/digest.hpp>
41 
42 namespace nil {
43  namespace crypto3 {
44  namespace block {
45  namespace detail {
46  template<typename CipherAccumulator>
47  struct ref_cipher_impl {
48  typedef CipherAccumulator accumulator_set_type;
49  typedef
50  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
51 
52  typedef typename accumulator_type::mode_type mode_type;
53  typedef typename mode_type::cipher_type cipher_type;
54 
56  }
57 
59  };
60 
61  template<typename CipherAccumulator>
63  typedef CipherAccumulator accumulator_set_type;
64  typedef
65  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
66 
67  typedef typename accumulator_type::mode_type mode_type;
68  typedef typename mode_type::cipher_type cipher_type;
69 
71  }
72 
74  };
75 
76  template<typename CipherStateImpl>
77  struct range_cipher_impl : public CipherStateImpl {
78  typedef CipherStateImpl cipher_state_impl_type;
79 
80  typedef typename cipher_state_impl_type::accumulator_type accumulator_type;
81  typedef typename cipher_state_impl_type::accumulator_set_type accumulator_set_type;
82 
83  typedef typename cipher_state_impl_type::mode_type mode_type;
84  typedef typename cipher_state_impl_type::cipher_type cipher_type;
85 
86  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
88 
89  template<typename SinglePassRange>
90  range_cipher_impl(const SinglePassRange &range, const accumulator_set_type &ise) :
91  CipherStateImpl(ise) {
92  BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
93 
94  typedef
95  typename std::iterator_traits<typename SinglePassRange::iterator>::value_type value_type;
96  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
97  typedef typename cipher_type::template stream_processor<
99  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
100  stream_processor;
101 
102  stream_processor(this->accumulator_set)(range.begin(), range.end());
103  }
104 
105  template<typename InputIterator>
106  range_cipher_impl(InputIterator first, InputIterator last, const accumulator_set_type &ise) :
107  CipherStateImpl(ise) {
108  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
109 
110  typedef typename std::iterator_traits<InputIterator>::value_type value_type;
111  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
112  typedef typename cipher_type::template stream_processor<
114  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
115  stream_processor;
116 
117  stream_processor(this->accumulator_set)(first, last);
118  }
119 
120  template<typename T, std::size_t Size>
121  inline operator std::array<T, Size>() const {
122  result_type result =
123  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
124  std::array<T, Size> out;
125  std::copy(result.begin(), result.end(), out.end());
126  return out;
127  }
128 
129  template<typename T, std::size_t Size>
130  inline operator boost::array<T, Size>() const {
131  result_type result =
132  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
133  boost::array<T, Size> out;
134  std::copy(result.begin(), result.end(), out.end());
135  return out;
136  }
137 
138  template<typename OutputRange>
139  operator OutputRange() const {
140  result_type result =
141  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
142  return OutputRange(result.cbegin(), result.cend());
143  }
144 
145  operator result_type() const {
146  return boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
147  }
148 
149  operator accumulator_set_type() const {
150  return this->accumulator_set;
151  }
152 
153 #ifndef CRYPTO3_RAW_HASH_STRING_OUTPUT
154 
155  template<typename Char, typename CharTraits, typename Alloc>
156  operator std::basic_string<Char, CharTraits, Alloc>() const {
157  return std::to_string(
158  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set));
159  }
160 
161 #endif
162  };
163 
164  template<typename CipherStateImpl, typename OutputIterator>
165  struct itr_cipher_impl : public CipherStateImpl {
166  private:
167  mutable OutputIterator out;
168 
169  public:
170  typedef CipherStateImpl cipher_state_impl_type;
171 
172  typedef typename cipher_state_impl_type::accumulator_type accumulator_type;
173  typedef typename cipher_state_impl_type::accumulator_set_type accumulator_set_type;
174 
175  typedef typename cipher_state_impl_type::mode_type mode_type;
176  typedef typename cipher_state_impl_type::cipher_type cipher_type;
177 
178  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
180 
181  template<typename SinglePassRange>
182  itr_cipher_impl(const SinglePassRange &range, OutputIterator out, const accumulator_set_type &ise) :
183  CipherStateImpl(ise), out(std::move(out)) {
184  BOOST_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
185 
186  typedef
187  typename std::iterator_traits<typename SinglePassRange::iterator>::value_type value_type;
188  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
189  typedef typename cipher_type::template stream_processor<
191  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
192  stream_processor;
193 
194  stream_processor(this->accumulator_set)(range.begin(), range.end());
195  }
196 
197  template<typename InputIterator>
198  itr_cipher_impl(InputIterator first, InputIterator last, OutputIterator out,
199  const accumulator_set_type &ise) :
200  CipherStateImpl(ise),
201  out(std::move(out)) {
202  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
203 
204  typedef typename std::iterator_traits<InputIterator>::value_type value_type;
205  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
206  typedef typename cipher_type::template stream_processor<
208  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
209  stream_processor;
210 
211  stream_processor(this->accumulator_set)(first, last);
212  }
213 
214  operator OutputIterator() const {
215  result_type result =
216  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
217 
218  return std::move(result.cbegin(), result.cend(), out);
219  }
220  };
221  } // namespace detail
222  } // namespace block
223  } // namespace crypto3
224 } // namespace nil
225 
226 #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
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
Definition: pair.hpp:31
Definition: block/include/nil/crypto3/block/cipher_value.hpp:165
itr_cipher_impl(InputIterator first, InputIterator last, OutputIterator out, const accumulator_set_type &ise)
Definition: block/include/nil/crypto3/block/cipher_value.hpp:198
cipher_state_impl_type::accumulator_type accumulator_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:172
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:179
cipher_state_impl_type::accumulator_set_type accumulator_set_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:173
itr_cipher_impl(const SinglePassRange &range, OutputIterator out, const accumulator_set_type &ise)
Definition: block/include/nil/crypto3/block/cipher_value.hpp:182
cipher_state_impl_type::mode_type mode_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:175
cipher_state_impl_type::cipher_type cipher_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:176
CipherStateImpl cipher_state_impl_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:170
Definition: block/include/nil/crypto3/block/cipher_value.hpp:77
cipher_state_impl_type::accumulator_set_type accumulator_set_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:81
cipher_state_impl_type::cipher_type cipher_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:84
CipherStateImpl cipher_state_impl_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:78
cipher_state_impl_type::mode_type mode_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:83
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:87
range_cipher_impl(InputIterator first, InputIterator last, const accumulator_set_type &ise)
Definition: block/include/nil/crypto3/block/cipher_value.hpp:106
range_cipher_impl(const SinglePassRange &range, const accumulator_set_type &ise)
Definition: block/include/nil/crypto3/block/cipher_value.hpp:90
cipher_state_impl_type::accumulator_type accumulator_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:80
Definition: block/include/nil/crypto3/block/cipher_value.hpp:47
accumulator_type::mode_type mode_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:52
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:50
mode_type::cipher_type cipher_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:53
ref_cipher_impl(const accumulator_set_type &acc)
Definition: block/include/nil/crypto3/block/cipher_value.hpp:55
CipherAccumulator accumulator_set_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:48
accumulator_set_type & accumulator_set
Definition: block/include/nil/crypto3/block/cipher_value.hpp:58
Definition: block/include/nil/crypto3/block/cipher_value.hpp:62
value_cipher_impl(const accumulator_set_type &acc)
Definition: block/include/nil/crypto3/block/cipher_value.hpp:70
accumulator_set_type accumulator_set
Definition: block/include/nil/crypto3/block/cipher_value.hpp:73
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:65
accumulator_type::mode_type mode_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:67
mode_type::cipher_type cipher_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:68
CipherAccumulator accumulator_set_type
Definition: block/include/nil/crypto3/block/cipher_value.hpp:63