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 xrpl {
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 xrpl
55
56//------------------------------------------------------------------------------
57
58namespace xrpl {
59
60namespace detail {
61
62template <class T>
64
65template <class T>
67
68template <class T>
70
71template <class T>
73
74} // namespace detail
75
76template <std::size_t Bits, class UInt = std::uint64_t>
78{
79private:
80 static_assert(
82 "UInt must be an unsigned integral type");
83
84 static_assert(Bits % (8 * sizeof(UInt)) == 0, "Bits must be a multiple of 8*sizeof(UInt)");
85
86 static_assert(Bits >= (8 * sizeof(UInt)), "Bits must be at least 8*sizeof(UInt)");
87
88 static std::size_t const size = Bits / (8 * sizeof(UInt));
89
91
92public:
93 using value_type = UInt;
94
95 static std::size_t const bits = Bits;
96 static std::size_t const bytes = bits / 8;
97
98 template <class Int>
99 static unsigned_integer
101 {
102 unsigned_integer result;
103 for (std::size_t i(1); i < size; ++i)
104 result.m_vec[i] = 0;
105 result.m_vec[0] = v;
106 return result;
107 }
108
109 void*
110 data() noexcept
111 {
112 return &m_vec[0];
113 }
114
115 void const*
116 data() const noexcept
117 {
118 return &m_vec[0];
119 }
120
121 template <class Hasher>
122 friend void
123 hash_append(Hasher& h, unsigned_integer const& a) noexcept
124 {
125 using beast::hash_append;
126 hash_append(h, a.m_vec);
127 }
128
131 {
132 for (std::size_t i(0); i < size; ++i)
133 s << std::hex << std::setfill('0') << std::setw(2 * sizeof(UInt)) << v.m_vec[i];
134 return s;
135 }
136};
137
139
140#ifndef __INTELLISENSE__
141static_assert(sha256_t::bits == 256, "sha256_t must have 256 bits");
142#endif
143
144} // namespace xrpl
145
146//------------------------------------------------------------------------------
147
148namespace xrpl {
149
151{
152public:
153 template <class T>
154 void
156 {
157 T t{};
158 hardened_hash<>()(t);
159 pass();
160 }
161
162 template <template <class T> class U>
163 void
165 {
166 check<U<bool>>();
167 check<U<char>>();
168 check<U<signed char>>();
169 check<U<unsigned char>>();
170 // These cause trouble for boost
171 // check <U <char16_t>> ();
172 // check <U <char32_t>> ();
173 check<U<wchar_t>>();
174 check<U<short>>();
175 check<U<unsigned short>>();
176 check<U<int>>();
177 check<U<unsigned int>>();
178 check<U<long>>();
179 check<U<long long>>();
180 check<U<unsigned long>>();
181 check<U<unsigned long long>>();
182 check<U<float>>();
183 check<U<double>>();
184 check<U<long double>>();
185 }
186
187 template <template <class T> class C>
188 void
190 {
191 {
192 C<detail::test_user_type_member<std::string>> c;
193 }
194
195 pass();
196
197 {
198 C<detail::test_user_type_free<std::string>> c;
199 }
200
201 pass();
202 }
203
204 void
206 {
207 testcase("user types");
208 check_user_type<detail::test_user_type_member>();
209 check_user_type<detail::test_user_type_free>();
210 }
211
212 void
214 {
215 testcase("containers");
216 check_container<detail::test_hardened_unordered_set>();
217 check_container<detail::test_hardened_unordered_map>();
218 check_container<detail::test_hardened_unordered_multiset>();
219 check_container<detail::test_hardened_unordered_multimap>();
220 }
221
222 void
223 run() override
224 {
227 }
228};
229
230BEAST_DEFINE_TESTSUITE(hardened_hash, basics, xrpl);
231
232} // namespace xrpl
A testsuite class.
Definition suite.h:51
void pass()
Record a successful test condition.
Definition suite.h:494
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:147
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.
void const * data() const noexcept
static std::size_t const size
static std::size_t const bytes
friend void hash_append(Hasher &h, unsigned_integer const &a) noexcept
static std::size_t const bits
friend std::ostream & operator<<(std::ostream &s, unsigned_integer const &v)
std::array< UInt, size > m_vec
static unsigned_integer from_number(Int v)
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:5
T setfill(T... args)
T setw(T... args)