base.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2019 Aleksey Moskvin <zerg1996@yandex.ru>
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_CODEC_BASE_HPP
27 #define CRYPTO3_CODEC_BASE_HPP
28 
29 #include <iterator>
30 #include <string>
31 
36 
38 
39 #include <boost/range/begin.hpp>
40 #include <boost/range/end.hpp>
41 
42 #include <boost/exception/exception.hpp>
43 #include <boost/exception/info.hpp>
44 #include <boost/throw_exception.hpp>
45 
46 namespace nil {
47  namespace crypto3 {
48  namespace codec {
49  namespace detail {
50  template<bool>
51  struct static_range;
52  }
53 
61  template<std::size_t Version, typename = detail::static_range<true>>
64 
70 
71  template<typename T>
72  void operator()(const T &block) {
73  typename T::const_iterator itr = block.cbegin();
74  while (*itr == '\0' && itr != block.cend()) {
75  ++itr;
76  }
77  leading_zeros = std::distance(block.begin(), itr);
78  }
79 
80  std::size_t leading_zeros;
81  };
82 
90  template<std::size_t Version, typename = detail::static_range<true>>
93 
99  }
100 
107  template<typename T>
108  void operator()(T &t) {
109  while (leading_zeros) {
110  t.push_back(policy_type::constants()[0]);
111  leading_zeros--;
112  }
113  }
114 
115  std::size_t leading_zeros;
116  };
117 
125  template<std::size_t Version>
126  struct base_encode_preprocessor<Version, detail::static_range<!(Version % 32)>> {
128 
130 
131  template<typename T>
132  void operator()(T &t) {}
133  };
134 
140  template<std::size_t Version>
141  struct base_encode_finalizer<Version, detail::static_range<!(Version % 32)>> {
143 
147  base_encode_finalizer(std::size_t input_remaining_bits = 0) : remaining_bits(input_remaining_bits) {
148  }
149 
156  template<typename T>
157  void operator()(T &t) {
158  for (typename T::iterator out = t.end() - 1; remaining_bits >= policy_type::padding_bits;
159  remaining_bits -= policy_type::padding_block_bits) {
160  *out-- = '=';
161  }
162  }
163 
164  std::size_t remaining_bits;
165  };
166 
172  template<std::size_t Version, typename = detail::static_range<true>>
175 
181 
182  template<typename T>
183  void operator()(const T &block) {
184  typename T::const_iterator itr = block.cbegin();
185  while (*itr == '1') {
186  ++itr;
187  }
188  leading_zeros = std::distance(block.begin(), itr);
189  }
190 
191  std::size_t leading_zeros;
192  };
193 
194  template<std::size_t Version>
195  struct base_decode_preprocessor<Version, detail::static_range<!(Version % 32)>> {
197 
199 
200  template<typename T>
201  void operator()(T &t) { }
202  };
203 
213  template<std::size_t Version, typename = detail::static_range<true>>
216 
222  }
223 
230  template<typename T>
231  void operator()(T &t) {
232  while (leading_zeros) {
233  t.push_back(policy_type::inverted_constants()['1']);
234  leading_zeros--;
235  }
236  }
237 
238  std::size_t leading_zeros;
239  };
240 
241  template<std::size_t Version>
242  struct base_decode_finalizer<Version, detail::static_range<!(Version % 32)>> {
244 
248  base_decode_finalizer(std::size_t input_remaining_bits = 0) : remaining_bits(input_remaining_bits) {
249  }
250 
257  template<typename T>
258  void operator()(T &t) {
259  int new_size = t.size();
260  for (typename T::iterator out = t.end() - 1; *out == '\0'; --out, --new_size) {}
261  t.resize(new_size);
262  }
263 
264  std::size_t remaining_bits;
265  };
266 
273  template<std::size_t Version, typename = detail::static_range<true>>
274  class base {
275  typedef typename detail::base_policy<Version> policy_type;
276 
277  public:
282 
285 
286  constexpr static const std::size_t encoded_value_bits = policy_type::encoded_value_bits;
287  typedef typename policy_type::encoded_value_type encoded_value_type;
288 
289  constexpr static const std::size_t decoded_value_bits = policy_type::decoded_value_bits;
290  typedef typename policy_type::decoded_value_type decoded_value_type;
291 
292  constexpr static const std::size_t encoded_block_values = policy_type::encoded_block_values;
293  constexpr static const std::size_t encoded_block_bits = policy_type::encoded_block_bits;
294  typedef typename policy_type::encoded_block_type encoded_block_type;
295 
296  constexpr static const std::size_t decoded_block_values = policy_type::decoded_block_values;
297  constexpr static const std::size_t decoded_block_bits = policy_type::decoded_block_bits;
298  typedef typename policy_type::decoded_block_type decoded_block_type;
299 
305  inline static encoded_block_type encode(const decoded_block_type &plaintext) {
306  return policy_type::encode_block(plaintext);
307  }
308 
314  inline static decoded_block_type decode(const encoded_block_type &encoded) {
315  return policy_type::decode_block(encoded);
316  }
317 
318  template<typename ProcessingMode>
320 
321  template<typename ProcessingMode, typename StateAccumulator, std::size_t ValueBits>
323  struct params_type {
325 
326  constexpr static const std::size_t value_bits = ValueBits;
327  constexpr static const std::size_t length_bits = ProcessingMode::input_block_bits;
328  };
329 
331  };
332  };
333 
340  template<std::size_t Version>
341  class base<Version, detail::static_range<!(Version % 32)>> {
342  typedef typename detail::base_policy<Version> policy_type;
343 
344  public:
349 
352 
353  constexpr static const std::size_t encoded_value_bits = policy_type::encoded_value_bits;
354  typedef typename policy_type::encoded_value_type encoded_value_type;
355 
356  constexpr static const std::size_t decoded_value_bits = policy_type::decoded_value_bits;
357  typedef typename policy_type::decoded_value_type decoded_value_type;
358 
359  constexpr static const std::size_t encoded_block_values = policy_type::encoded_block_values;
360  constexpr static const std::size_t encoded_block_bits = policy_type::encoded_block_bits;
361  typedef typename policy_type::encoded_block_type encoded_block_type;
362 
363  constexpr static const std::size_t decoded_block_values = policy_type::decoded_block_values;
364  constexpr static const std::size_t decoded_block_bits = policy_type::decoded_block_bits;
365  typedef typename policy_type::decoded_block_type decoded_block_type;
366 
372  inline static encoded_block_type encode(const decoded_block_type &plaintext) {
373  return policy_type::encode_block(plaintext);
374  }
375 
381  inline static decoded_block_type decode(const encoded_block_type &encoded) {
382  return policy_type::decode_block(encoded);
383  }
384 
385  template<typename ProcessingMode>
387 
388  template<typename ProcessingMode, typename StateAccumulator, std::size_t ValueBits>
389  struct stream_processor {
390  struct params_type {
392 
393  constexpr static const std::size_t value_bits = ValueBits;
394  constexpr static const std::size_t length_bits = ProcessingMode::input_block_bits;
395  };
396 
398  };
399  };
400 
405  typedef base<32> base32;
406 
411  typedef base<58> base58;
412 
417  typedef base<64> base64;
418  } // namespace codec
419  } // namespace crypto3
420 } // namespace nil
421 
422 #endif
policy_type::decoded_value_type decoded_value_type
Definition: base.hpp:357
policy_type::decoded_block_type decoded_block_type
Definition: base.hpp:365
policy_type::encoded_value_type encoded_value_type
Definition: base.hpp:354
detail::isomorphic_encoding_mode< base< Version > > stream_encoder_type
Definition: base.hpp:350
detail::isomorphic_decoding_mode< base< Version > > stream_decoder_type
Definition: base.hpp:351
static encoded_block_type encode(const decoded_block_type &plaintext)
Encodes single atomic data block.
Definition: base.hpp:372
policy_type::encoded_block_type encoded_block_type
Definition: base.hpp:361
base_encode_preprocessor< Version > encoding_preprocessor_type
Definition: base.hpp:347
static decoded_block_type decode(const encoded_block_type &encoded)
Decodes single atomic data block.
Definition: base.hpp:381
base_decode_preprocessor< Version > decoding_preprocessor_type
Definition: base.hpp:348
base_decode_finalizer< Version > decoding_finalizer_type
Definition: base.hpp:346
base_encode_finalizer< Version > encoding_finalizer_type
Definition: base.hpp:345
Base codec implements Base-family encoding. Meets the requirements of Codec.
Definition: base.hpp:274
constexpr static const std::size_t encoded_value_bits
Definition: base.hpp:286
constexpr static const std::size_t decoded_block_values
Definition: base.hpp:296
policy_type::encoded_block_type encoded_block_type
Definition: base.hpp:294
base_encode_finalizer< Version > encoding_finalizer_type
Definition: base.hpp:278
constexpr static const std::size_t encoded_block_bits
Definition: base.hpp:293
policy_type::encoded_value_type encoded_value_type
Definition: base.hpp:287
base_decode_finalizer< Version > decoding_finalizer_type
Definition: base.hpp:279
constexpr static const std::size_t decoded_block_bits
Definition: base.hpp:297
static encoded_block_type encode(const decoded_block_type &plaintext)
Encodes single atomic data block.
Definition: base.hpp:305
constexpr static const std::size_t encoded_block_values
Definition: base.hpp:292
base_decode_preprocessor< Version > decoding_preprocessor_type
Definition: base.hpp:281
static decoded_block_type decode(const encoded_block_type &encoded)
Decodes single atomic data block.
Definition: base.hpp:314
policy_type::decoded_block_type decoded_block_type
Definition: base.hpp:298
base_encode_preprocessor< Version > encoding_preprocessor_type
Definition: base.hpp:280
policy_type::decoded_value_type decoded_value_type
Definition: base.hpp:290
constexpr static const std::size_t decoded_value_bits
Definition: base.hpp:289
detail::isomorphic_encoding_mode< base< Version > > stream_encoder_type
Definition: base.hpp:283
detail::isomorphic_decoding_mode< base< Version > > stream_decoder_type
Definition: base.hpp:284
Definition: base_policy.hpp:410
base< 64 > base64
Type alias for base<64>
Definition: base.hpp:417
base< 32 > base32
Type alias for base<32>
Definition: base.hpp:405
base< 58 > base58
Type alias for base<58>
Definition: base.hpp:411
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
boost::mpl::apply< AccumulatorSet, tag::codec< Mode > >::type::result_type codec(const AccumulatorSet &acc)
Definition: accumulators/codec.hpp:261
Definition: pair.hpp:31
constexpr static const std::size_t value_bits
Definition: base.hpp:326
stream_endian::little_octet_big_bit endian_type
Definition: base.hpp:324
constexpr static const std::size_t length_bits
Definition: base.hpp:327
varlength_block_stream_processor< ProcessingMode, StateAccumulator, params_type > type
Definition: base.hpp:330
fixed_block_stream_processor< ProcessingMode, StateAccumulator, params_type > type
Definition: base.hpp:397
base_decode_finalizer(std::size_t input_remaining_bits=0)
Definition: base.hpp:248
void operator()(T &t)
Base decoder padding function. Fills remaining empty bits with '='.
Definition: base.hpp:258
Base decoder finalizer functor.
Definition: base.hpp:214
base_decode_finalizer(std::size_t leading_zeros)
Constructs base decoder finalizer.
Definition: base.hpp:221
detail::base_policy< Version > policy_type
Definition: base.hpp:215
std::size_t leading_zeros
Definition: base.hpp:238
void operator()(T &t)
Base decoder padding function. Fills remaining empty bits with '='.
Definition: base.hpp:231
detail::base_policy< Version > policy_type
Definition: base.hpp:174
base_decode_preprocessor(std::size_t leading_zeros=0)
Definition: base.hpp:180
void operator()(const T &block)
Definition: base.hpp:183
std::size_t leading_zeros
Definition: base.hpp:191
base_encode_finalizer(std::size_t input_remaining_bits=0)
Definition: base.hpp:147
std::size_t remaining_bits
Bits remaining unprocessed in block.
Definition: base.hpp:164
void operator()(T &t)
Base encoding padding function. Fills remaining empty bits with '='.
Definition: base.hpp:157
Base encoder finalizer functor.
Definition: base.hpp:91
void operator()(T &t)
Base encoding padding function. Fills remaining empty bits with '0'.
Definition: base.hpp:108
std::size_t leading_zeros
Definition: base.hpp:115
base_encode_finalizer(std::size_t leading_zeros=0)
Constructs the base encoder finalizer.
Definition: base.hpp:98
detail::base_policy< Version > policy_type
Definition: base.hpp:92
Base encoder preprocessor functor.
Definition: base.hpp:62
std::size_t leading_zeros
Definition: base.hpp:80
detail::base_policy< Version > policy_type
Definition: base.hpp:63
base_encode_preprocessor(std::size_t leading_zeros=0)
Constructs base encoder preprocessor.
Definition: base.hpp:69
void operator()(const T &block)
Definition: base.hpp:72
Definition: fixed_block_stream_processor.hpp:46
Definition: varlength_block_stream_processor.hpp:45
Definition: algebra/include/nil/crypto3/detail/stream_endian.hpp:45