rippled
Loading...
Searching...
No Matches
hardened_hash_test.cpp
1#include <xrpl/basics/hardened_hash.h>
2#include <xrpl/beast/unit_test.h>
3
4#include <array>
5#include <cstdint>
6#include <iomanip>
7#include <unordered_map>
8#include <unordered_set>
9
10namespace ripple {
11namespace detail {
12
13template <class T>
15{
16private:
17 T t;
18
19public:
20 explicit test_user_type_member(T const& t_ = T()) : t(t_)
21 {
22 }
23
24 template <class Hasher>
25 friend void
26 hash_append(Hasher& h, test_user_type_member const& a) noexcept
27 {
29 hash_append(h, a.t);
30 }
31};
32
33template <class T>
35{
36private:
37 T t;
38
39public:
40 explicit test_user_type_free(T const& t_ = T()) : t(t_)
41 {
42 }
43
44 template <class Hasher>
45 friend void
46 hash_append(Hasher& h, test_user_type_free const& a) noexcept
47 {
49 hash_append(h, a.t);
50 }
51};
52
53} // namespace detail
54} // namespace ripple
55
56//------------------------------------------------------------------------------
57
58namespace ripple {
59
60namespace detail {
61
62template <class T>
64
65template <class T>
67
68template <class T>
71
72template <class T>
75
76} // namespace detail
77
78template <std::size_t Bits, class UInt = std::uint64_t>
80{
81private:
82 static_assert(
84 "UInt must be an unsigned integral type");
85
86 static_assert(
87 Bits % (8 * sizeof(UInt)) == 0,
88 "Bits must be a multiple of 8*sizeof(UInt)");
89
90 static_assert(
91 Bits >= (8 * sizeof(UInt)),
92 "Bits must be at least 8*sizeof(UInt)");
93
94 static std::size_t const size = Bits / (8 * sizeof(UInt));
95
97
98public:
99 using value_type = UInt;
100
101 static std::size_t const bits = Bits;
102 static std::size_t const bytes = bits / 8;
103
104 template <class Int>
105 static unsigned_integer
107 {
108 unsigned_integer result;
109 for (std::size_t i(1); i < size; ++i)
110 result.m_vec[i] = 0;
111 result.m_vec[0] = v;
112 return result;
113 }
114
115 void*
116 data() noexcept
117 {
118 return &m_vec[0];
119 }
120
121 void const*
122 data() const noexcept
123 {
124 return &m_vec[0];
125 }
126
127 template <class Hasher>
128 friend void
129 hash_append(Hasher& h, unsigned_integer const& a) noexcept
130 {
131 using beast::hash_append;
132 hash_append(h, a.m_vec);
133 }
134
137 {
138 for (std::size_t i(0); i < size; ++i)
139 s << std::hex << std::setfill('0') << std::setw(2 * sizeof(UInt))
140 << v.m_vec[i];
141 return s;
142 }
143};
144
146
147#ifndef __INTELLISENSE__
148static_assert(sha256_t::bits == 256, "sha256_t must have 256 bits");
149#endif
150
151} // namespace ripple
152
153//------------------------------------------------------------------------------
154
155namespace ripple {
156
158{
159public:
160 template <class T>
161 void
163 {
164 T t{};
165 hardened_hash<>()(t);
166 pass();
167 }
168
169 template <template <class T> class U>
170 void
172 {
173 check<U<bool>>();
174 check<U<char>>();
175 check<U<signed char>>();
176 check<U<unsigned char>>();
177 // These cause trouble for boost
178 // check <U <char16_t>> ();
179 // check <U <char32_t>> ();
180 check<U<wchar_t>>();
181 check<U<short>>();
182 check<U<unsigned short>>();
183 check<U<int>>();
184 check<U<unsigned int>>();
185 check<U<long>>();
186 check<U<long long>>();
187 check<U<unsigned long>>();
188 check<U<unsigned long long>>();
189 check<U<float>>();
190 check<U<double>>();
191 check<U<long double>>();
192 }
193
194 template <template <class T> class C>
195 void
197 {
198 {
199 C<detail::test_user_type_member<std::string>> c;
200 }
201
202 pass();
203
204 {
205 C<detail::test_user_type_free<std::string>> c;
206 }
207
208 pass();
209 }
210
211 void
213 {
214 testcase("user types");
215 check_user_type<detail::test_user_type_member>();
216 check_user_type<detail::test_user_type_free>();
217 }
218
219 void
221 {
222 testcase("containers");
223 check_container<detail::test_hardened_unordered_set>();
224 check_container<detail::test_hardened_unordered_map>();
225 check_container<detail::test_hardened_unordered_multiset>();
226 check_container<detail::test_hardened_unordered_multimap>();
227 }
228
229 void
230 run() override
231 {
234 }
235};
236
237BEAST_DEFINE_TESTSUITE(hardened_hash, basics, ripple);
238
239} // namespace ripple
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:508
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
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.
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
T setfill(T... args)
T setw(T... args)