sponge_construction.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2019 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Alexander Sokolov <asokolov@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_SPONGE_CONSTRUCTION_HPP
27 #define CRYPTO3_HASH_SPONGE_CONSTRUCTION_HPP
28 
29 #include <nil/crypto3/detail/static_digest.hpp>
30 #include <nil/crypto3/detail/pack.hpp>
31 
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace hashes {
51  template<typename Params,
52  typename IV,
53  typename Compressor,
54  typename Padding,
55  typename Finalizer = detail::nop_finalizer>
57  public:
58  typedef IV iv_generator;
59  typedef Compressor compressor_functor;
60  typedef Padding padding_functor;
61  typedef Finalizer finalizer_functor;
62 
63  typedef typename Params::digest_endian endian_type;
64 
65  constexpr static const std::size_t word_bits = compressor_functor::word_bits;
66  typedef typename compressor_functor::word_type word_type;
67 
68  constexpr static const std::size_t state_bits = compressor_functor::state_bits;
69  constexpr static const std::size_t state_words = compressor_functor::state_words;
70  typedef typename compressor_functor::state_type state_type;
71 
72  constexpr static const std::size_t block_bits = compressor_functor::block_bits;
73  constexpr static const std::size_t block_words = compressor_functor::block_words;
74  typedef typename compressor_functor::block_type block_type;
75 
76  constexpr static const std::size_t digest_bits = Params::digest_bits;
77  constexpr static const std::size_t digest_bytes = digest_bits / octet_bits;
78  constexpr static const std::size_t digest_words = digest_bits / word_bits;
80 
81  template<typename Integer = std::size_t>
82  inline sponge_construction &process_block(const block_type &block, Integer seen = Integer()) {
83  compressor_functor::process_block(state_, block);
84  return *this;
85  }
86 
88  std::size_t total_seen = std::size_t()) {
89  using namespace nil::crypto3::detail;
90 
91  block_type b = block;
92  std::size_t block_seen = total_seen % block_bits;
93  // Process block if it is full
94  if (total_seen && !block_seen)
95  process_block(b);
96 
97  std::size_t copy_seen = block_seen;
98  // Pad last message block
99  padding_functor padding;
100  padding(b, block_seen);
101  process_block(b);
102 
103  // Process additional block if not all bits were padded
104  if (!padding.is_last_block()) {
105  std::fill(b.begin(), b.end(), 0);
106  padding.process_last(b, copy_seen);
107  process_block(b);
108  }
109 
110  // Apply finalizer
111  finalizer_functor()(state_);
112 
113  // Convert digest to byte representation
114  std::array<octet_type, state_bits / octet_bits> d_full;
115  pack_from<endian_type, word_bits, octet_bits>(state_.begin(), state_.end(), d_full.begin());
116 
117  digest_type d;
118  std::copy(d_full.begin(), d_full.begin() + digest_bytes, d.begin());
119 
120  return d;
121  }
122 
124  reset();
125  }
126 
127  void reset(state_type const &s) {
128  state_ = s;
129  }
130 
131  void reset() {
132  iv_generator iv;
133  reset(iv());
134  }
135 
136  state_type const &state() const {
137  return state_;
138  }
139 
140  private:
141  state_type state_;
142  };
143 
144  } // namespace hashes
145  } // namespace crypto3
146 } // namespace nil
147 
148 #endif // CRYPTO3_HASH_SPONGE_CONSTRUCTION_HPP
Definition: sponge_construction.hpp:56
Params::digest_endian endian_type
Definition: sponge_construction.hpp:63
constexpr static const std::size_t digest_bits
Definition: sponge_construction.hpp:76
constexpr static const std::size_t digest_words
Definition: sponge_construction.hpp:78
compressor_functor::state_type state_type
Definition: sponge_construction.hpp:70
Finalizer finalizer_functor
Definition: sponge_construction.hpp:61
Compressor compressor_functor
Definition: sponge_construction.hpp:59
IV iv_generator
Definition: sponge_construction.hpp:58
constexpr static const std::size_t block_words
Definition: sponge_construction.hpp:73
compressor_functor::word_type word_type
Definition: sponge_construction.hpp:66
constexpr static const std::size_t digest_bytes
Definition: sponge_construction.hpp:77
constexpr static const std::size_t block_bits
Definition: sponge_construction.hpp:72
compressor_functor::block_type block_type
Definition: sponge_construction.hpp:74
static_digest< digest_bits > digest_type
Definition: sponge_construction.hpp:79
constexpr static const std::size_t state_bits
Definition: sponge_construction.hpp:68
state_type const & state() const
Definition: sponge_construction.hpp:136
void reset()
Definition: sponge_construction.hpp:131
Padding padding_functor
Definition: sponge_construction.hpp:60
void reset(state_type const &s)
Definition: sponge_construction.hpp:127
constexpr static const std::size_t word_bits
Definition: sponge_construction.hpp:65
constexpr static const std::size_t state_words
Definition: sponge_construction.hpp:69
sponge_construction & process_block(const block_type &block, Integer seen=Integer())
Definition: sponge_construction.hpp:82
digest_type digest(const block_type &block=block_type(), std::size_t total_seen=std::size_t())
Definition: sponge_construction.hpp:87
sponge_construction()
Definition: sponge_construction.hpp:123
Definition: block/include/nil/crypto3/detail/static_digest.hpp:72
constexpr matrix< T, N, M > fill(T value)
generates a matrix containing a single value
Definition: matrix/utility.hpp:102
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:33
boost::uint_t< octet_bits >::least octet_type
Definition: algebra/include/nil/crypto3/detail/octet.hpp:33
Definition: pair.hpp:31