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
23#include <array>
24#include <cstdint>
25#include <iomanip>
26#include <unordered_map>
27#include <unordered_set>
28
29namespace ripple {
30namespace detail {
31
32template <class T>
34{
35private:
36 T t;
37
38public:
39 explicit test_user_type_member(T const& t_ = T()) : t(t_)
40 {
41 }
42
43 template <class Hasher>
44 friend void
45 hash_append(Hasher& h, test_user_type_member const& a) noexcept
46 {
48 hash_append(h, a.t);
49 }
50};
51
52template <class T>
54{
55private:
56 T t;
57
58public:
59 explicit test_user_type_free(T const& t_ = T()) : t(t_)
60 {
61 }
62
63 template <class Hasher>
64 friend void
65 hash_append(Hasher& h, test_user_type_free const& a) noexcept
66 {
68 hash_append(h, a.t);
69 }
70};
71
72} // namespace detail
73} // namespace ripple
74
75//------------------------------------------------------------------------------
76
77namespace ripple {
78
79namespace detail {
80
81template <class T>
83
84template <class T>
86
87template <class T>
90
91template <class T>
94
95} // namespace detail
96
97template <std::size_t Bits, class UInt = std::uint64_t>
99{
100private:
101 static_assert(
103 "UInt must be an unsigned integral type");
104
105 static_assert(
106 Bits % (8 * sizeof(UInt)) == 0,
107 "Bits must be a multiple of 8*sizeof(UInt)");
108
109 static_assert(
110 Bits >= (8 * sizeof(UInt)),
111 "Bits must be at least 8*sizeof(UInt)");
112
113 static std::size_t const size = Bits / (8 * sizeof(UInt));
114
116
117public:
118 using value_type = UInt;
119
120 static std::size_t const bits = Bits;
121 static std::size_t const bytes = bits / 8;
122
123 template <class Int>
124 static unsigned_integer
126 {
127 unsigned_integer result;
128 for (std::size_t i(1); i < size; ++i)
129 result.m_vec[i] = 0;
130 result.m_vec[0] = v;
131 return result;
132 }
133
134 void*
135 data() noexcept
136 {
137 return &m_vec[0];
138 }
139
140 void const*
141 data() const noexcept
142 {
143 return &m_vec[0];
144 }
145
146 template <class Hasher>
147 friend void
148 hash_append(Hasher& h, unsigned_integer const& a) noexcept
149 {
150 using beast::hash_append;
151 hash_append(h, a.m_vec);
152 }
153
156 {
157 for (std::size_t i(0); i < size; ++i)
158 s << std::hex << std::setfill('0') << std::setw(2 * sizeof(UInt))
159 << v.m_vec[i];
160 return s;
161 }
162};
163
165
166#ifndef __INTELLISENSE__
167static_assert(sha256_t::bits == 256, "sha256_t must have 256 bits");
168#endif
169
170} // namespace ripple
171
172//------------------------------------------------------------------------------
173
174namespace ripple {
175
177{
178public:
179 template <class T>
180 void
182 {
183 T t{};
184 hardened_hash<>()(t);
185 pass();
186 }
187
188 template <template <class T> class U>
189 void
191 {
192 check<U<bool>>();
193 check<U<char>>();
194 check<U<signed char>>();
195 check<U<unsigned char>>();
196 // These cause trouble for boost
197 // check <U <char16_t>> ();
198 // check <U <char32_t>> ();
199 check<U<wchar_t>>();
200 check<U<short>>();
201 check<U<unsigned short>>();
202 check<U<int>>();
203 check<U<unsigned int>>();
204 check<U<long>>();
205 check<U<long long>>();
206 check<U<unsigned long>>();
207 check<U<unsigned long long>>();
208 check<U<float>>();
209 check<U<double>>();
210 check<U<long double>>();
211 }
212
213 template <template <class T> class C>
214 void
216 {
217 {
218 C<detail::test_user_type_member<std::string>> c;
219 }
220
221 pass();
222
223 {
224 C<detail::test_user_type_free<std::string>> c;
225 }
226
227 pass();
228 }
229
230 void
232 {
233 testcase("user types");
234 check_user_type<detail::test_user_type_member>();
235 check_user_type<detail::test_user_type_free>();
236 }
237
238 void
240 {
241 testcase("containers");
242 check_container<detail::test_hardened_unordered_set>();
243 check_container<detail::test_hardened_unordered_map>();
244 check_container<detail::test_hardened_unordered_multiset>();
245 check_container<detail::test_hardened_unordered_multimap>();
246 }
247
248 void
249 run() override
250 {
253 }
254};
255
256BEAST_DEFINE_TESTSUITE(hardened_hash, basics, ripple);
257
258} // namespace ripple
A testsuite class.
Definition suite.h:55
void pass()
Record a successful test condition.
Definition suite.h:511
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
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:25
T setfill(T... args)
T setw(T... args)