rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/hardened_hash.h>
21#include <xrpl/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
30namespace ripple {
31namespace detail {
32
33template <class T>
35{
36private:
37 T t;
38
39public:
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 {
49 hash_append(h, a.t);
50 }
51};
52
53template <class T>
55{
56private:
57 T t;
58
59public:
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 {
69 hash_append(h, a.t);
70 }
71};
72
73} // namespace detail
74} // namespace ripple
75
76//------------------------------------------------------------------------------
77
78namespace ripple {
79
80namespace detail {
81
82template <class T>
84
85template <class T>
87
88template <class T>
91
92template <class T>
95
96} // namespace detail
97
98template <std::size_t Bits, class UInt = std::uint64_t>
100{
101private:
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
118public:
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
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
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__
168static_assert(sha256_t::bits == 256, "sha256_t must have 256 bits");
169#endif
170
171} // namespace ripple
172
173//------------------------------------------------------------------------------
174
175namespace ripple {
176
178{
179public:
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 {
254 }
255};
256
257BEAST_DEFINE_TESTSUITE(hardened_hash, basics, ripple);
258
259} // namespace ripple
A testsuite class.
Definition: suite.h:53
void pass()
Record a successful test condition.
Definition: suite.h:509
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:153
friend void hash_append(Hasher &h, test_user_type_free const &a) noexcept
friend void hash_append(Hasher &h, test_user_type_member const &a) noexcept
void run() override
Runs the suite.
Seed functor once per construction.
Definition: hardened_hash.h:97
static std::size_t const bytes
friend void hash_append(Hasher &h, unsigned_integer const &a) noexcept
void const * data() const noexcept
friend std::ostream & operator<<(std::ostream &s, unsigned_integer const &v)
std::array< UInt, size > m_vec
static unsigned_integer from_number(Int v)
static std::size_t const size
static std::size_t const bits
T hex(T... args)
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
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T setfill(T... args)
T setw(T... args)