rippled
hardened_hash_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/hardened_hash.h>
21 #include <ripple/beast/unit_test.h>
22 #include <boost/functional/hash.hpp>
23 #include <array>
24 #include <cstdint>
25 #include <functional>
26 #include <iomanip>
27 #include <unordered_map>
28 #include <unordered_set>
29 
30 namespace ripple {
31 namespace detail {
32 
33 template <class T>
35 {
36 private:
37  T t;
38 
39 public:
40  explicit test_user_type_member(T const& t_ = T()) : t(t_)
41  {
42  }
43 
44  template <class Hasher>
45  friend void
46  hash_append(Hasher& h, test_user_type_member const& a) noexcept
47  {
48  using beast::hash_append;
49  hash_append(h, a.t);
50  }
51 };
52 
53 template <class T>
55 {
56 private:
57  T t;
58 
59 public:
60  explicit test_user_type_free(T const& t_ = T()) : t(t_)
61  {
62  }
63 
64  template <class Hasher>
65  friend void
66  hash_append(Hasher& h, test_user_type_free const& a) noexcept
67  {
68  using beast::hash_append;
69  hash_append(h, a.t);
70  }
71 };
72 
73 } // namespace detail
74 } // namespace ripple
75 
76 //------------------------------------------------------------------------------
77 
78 namespace ripple {
79 
80 namespace detail {
81 
82 template <class T>
84 
85 template <class T>
87 
88 template <class T>
91 
92 template <class T>
95 
96 } // namespace detail
97 
98 template <std::size_t Bits, class UInt = std::uint64_t>
100 {
101 private:
102  static_assert(
104  "UInt must be an unsigned integral type");
105 
106  static_assert(
107  Bits % (8 * sizeof(UInt)) == 0,
108  "Bits must be a multiple of 8*sizeof(UInt)");
109 
110  static_assert(
111  Bits >= (8 * sizeof(UInt)),
112  "Bits must be at least 8*sizeof(UInt)");
113 
114  static std::size_t const size = Bits / (8 * sizeof(UInt));
115 
117 
118 public:
119  using value_type = UInt;
120 
121  static std::size_t const bits = Bits;
122  static std::size_t const bytes = bits / 8;
123 
124  template <class Int>
125  static unsigned_integer
126  from_number(Int v)
127  {
128  unsigned_integer result;
129  for (std::size_t i(1); i < size; ++i)
130  result.m_vec[i] = 0;
131  result.m_vec[0] = v;
132  return result;
133  }
134 
135  void*
136  data() noexcept
137  {
138  return &m_vec[0];
139  }
140 
141  void const*
142  data() const noexcept
143  {
144  return &m_vec[0];
145  }
146 
147  template <class Hasher>
148  friend void
149  hash_append(Hasher& h, unsigned_integer const& a) noexcept
150  {
151  using beast::hash_append;
152  hash_append(h, a.m_vec);
153  }
154 
155  friend std::ostream&
157  {
158  for (std::size_t i(0); i < size; ++i)
159  s << std::hex << std::setfill('0') << std::setw(2 * sizeof(UInt))
160  << v.m_vec[i];
161  return s;
162  }
163 };
164 
166 
167 #ifndef __INTELLISENSE__
168 static_assert(sha256_t::bits == 256, "sha256_t must have 256 bits");
169 #endif
170 
171 } // namespace ripple
172 
173 //------------------------------------------------------------------------------
174 
175 namespace ripple {
176 
177 class hardened_hash_test : public beast::unit_test::suite
178 {
179 public:
180  template <class T>
181  void
183  {
184  T t{};
185  hardened_hash<>()(t);
186  pass();
187  }
188 
189  template <template <class T> class U>
190  void
192  {
193  check<U<bool>>();
194  check<U<char>>();
195  check<U<signed char>>();
196  check<U<unsigned char>>();
197  // These cause trouble for boost
198  // check <U <char16_t>> ();
199  // check <U <char32_t>> ();
200  check<U<wchar_t>>();
201  check<U<short>>();
202  check<U<unsigned short>>();
203  check<U<int>>();
204  check<U<unsigned int>>();
205  check<U<long>>();
206  check<U<long long>>();
207  check<U<unsigned long>>();
208  check<U<unsigned long long>>();
209  check<U<float>>();
210  check<U<double>>();
211  check<U<long double>>();
212  }
213 
214  template <template <class T> class C>
215  void
217  {
218  {
219  C<detail::test_user_type_member<std::string>> c;
220  }
221 
222  pass();
223 
224  {
225  C<detail::test_user_type_free<std::string>> c;
226  }
227 
228  pass();
229  }
230 
231  void
233  {
234  testcase("user types");
235  check_user_type<detail::test_user_type_member>();
236  check_user_type<detail::test_user_type_free>();
237  }
238 
239  void
241  {
242  testcase("containers");
243  check_container<detail::test_hardened_unordered_set>();
244  check_container<detail::test_hardened_unordered_map>();
245  check_container<detail::test_hardened_unordered_multiset>();
246  check_container<detail::test_hardened_unordered_multimap>();
247  }
248 
249  void
250  run() override
251  {
252  test_user_types();
253  test_containers();
254  }
255 };
256 
257 BEAST_DEFINE_TESTSUITE(hardened_hash, basics, ripple);
258 
259 } // namespace ripple
std::is_unsigned
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::hardened_hash_test::test_containers
void test_containers()
Definition: hardened_hash_test.cpp:240
functional
unordered_set
ripple::unsigned_integer::bytes
static const std::size_t bytes
Definition: hardened_hash_test.cpp:122
ripple::unsigned_integer::hash_append
friend void hash_append(Hasher &h, unsigned_integer const &a) noexcept
Definition: hardened_hash_test.cpp:149
ripple::unsigned_integer::value_type
UInt value_type
Definition: hardened_hash_test.cpp:119
std::setfill
T setfill(T... args)
ripple::unsigned_integer::operator<<
friend std::ostream & operator<<(std::ostream &s, unsigned_integer const &v)
Definition: hardened_hash_test.cpp:156
ripple::detail::test_user_type_member
Definition: hardened_hash_test.cpp:34
ripple::unsigned_integer::data
void const * data() const noexcept
Definition: hardened_hash_test.cpp:142
ripple::detail::test_user_type_free
Definition: hardened_hash_test.cpp:54
ripple::unsigned_integer::size
static const std::size_t size
Definition: hardened_hash_test.cpp:114
ripple::hardened_hash_test::check_container
void check_container()
Definition: hardened_hash_test.cpp:216
std::unordered_multimap
STL class.
ripple::unsigned_integer
Definition: hardened_hash_test.cpp:99
std::hex
T hex(T... args)
ripple::detail::test_user_type_member::t
T t
Definition: hardened_hash_test.cpp:37
ripple::hardened_hash_test::test_user_types
void test_user_types()
Definition: hardened_hash_test.cpp:232
std::ostream
STL class.
ripple::hardened_hash_test::check
void check()
Definition: hardened_hash_test.cpp:182
array
ripple::hardened_hash
Seed functor once per construction.
Definition: hardened_hash.h:96
cstdint
ripple::detail::test_user_type_free::test_user_type_free
test_user_type_free(T const &t_=T())
Definition: hardened_hash_test.cpp:60
std::is_integral
ripple::unsigned_integer::m_vec
std::array< UInt, size > m_vec
Definition: hardened_hash_test.cpp:116
ripple::unsigned_integer::bits
static const std::size_t bits
Definition: hardened_hash_test.cpp:121
std::unordered_multiset
STL class.
ripple::hardened_hash_test::run
void run() override
Definition: hardened_hash_test.cpp:250
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::test_user_type_free::hash_append
friend void hash_append(Hasher &h, test_user_type_free const &a) noexcept
Definition: hardened_hash_test.cpp:66
iomanip
ripple::unsigned_integer::from_number
static unsigned_integer from_number(Int v)
Definition: hardened_hash_test.cpp:126
ripple::detail::test_user_type_member::test_user_type_member
test_user_type_member(T const &t_=T())
Definition: hardened_hash_test.cpp:40
ripple::detail::test_user_type_free::t
T t
Definition: hardened_hash_test.cpp:57
beast::hash_append
std::enable_if_t< is_contiguously_hashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Definition: hash_append.h:236
ripple::hardened_hash_test::check_user_type
void check_user_type()
Definition: hardened_hash_test.cpp:191
ripple::hardened_hash_test
Definition: hardened_hash_test.cpp:177
std::size_t
std::setw
T setw(T... args)
ripple::detail::test_user_type_member::hash_append
friend void hash_append(Hasher &h, test_user_type_member const &a) noexcept
Definition: hardened_hash_test.cpp:46
ripple::unsigned_integer::data
void * data() noexcept
Definition: hardened_hash_test.cpp:136
unordered_map