Debian Codegraph @

tests/unit_tests/crypto.cpp

raw symbols SCIP
1 // Copyright (c) 2017-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include <cstdint>
30 #include <gtest/gtest.h>
31 #include <memory>
32 #include <sstream>
33 #include <string>
34
35 #include "cryptonote_basic/cryptonote_basic_impl.h"
36 #include "cryptonote_basic/merge_mining.h"
37
38 namespace
39 {
40 static constexpr const std::uint8_t source[] = {
41 0x8b, 0x65, 0x59, 0x70, 0x15, 0x37, 0x99, 0xaf, 0x2a, 0xea, 0xdc, 0x9f, 0xf1, 0xad, 0xd0, 0xea,
42 0x6c, 0x72, 0x51, 0xd5, 0x41, 0x54, 0xcf, 0xa9, 0x2c, 0x17, 0x3a, 0x0d, 0xd3, 0x9c, 0x1f, 0x94,
43 0x6c, 0x72, 0x51, 0xd5, 0x41, 0x54, 0xcf, 0xa9, 0x2c, 0x17, 0x3a, 0x0d, 0xd3, 0x9c, 0x1f, 0x94,
44 0x8b, 0x65, 0x59, 0x70, 0x15, 0x37, 0x99, 0xaf, 0x2a, 0xea, 0xdc, 0x9f, 0xf1, 0xad, 0xd0, 0xea
45 };
46
47 static constexpr const char expected[] =
48 "8b655970153799af2aeadc9ff1add0ea6c7251d54154cfa92c173a0dd39c1f94"
49 "6c7251d54154cfa92c173a0dd39c1f948b655970153799af2aeadc9ff1add0ea";
50
51 template<typename T> void *addressof(T &t) { return &t; }
52 template<> void *addressof(crypto::secret_key &k) { return addressof(unwrap(unwrap(k))); }
53
54 template<typename T>
55 bool is_formatted()
56 {
57 T value{};
58
59 static_assert(alignof(T) == 1, "T must have 1 byte alignment");
60 static_assert(sizeof(T) <= sizeof(source), "T is too large for source");
61 static_assert(sizeof(T) * 2 <= sizeof(expected), "T is too large for destination");
62 std::memcpy(addressof(value), source, sizeof(T));
63
64 std::stringstream out;
65 out << "BEGIN" << value << "END";
66 return out.str() == "BEGIN<" + std::string{expected, sizeof(T) * 2} + ">END";
67 }
68 }
69
70 TEST(Crypto, Ostream)
71 {
72 EXPECT_TRUE(is_formatted<crypto::hash8>());
73 EXPECT_TRUE(is_formatted<crypto::hash>());
74 EXPECT_TRUE(is_formatted<crypto::public_key>());
75 EXPECT_TRUE(is_formatted<crypto::signature>());
76 EXPECT_TRUE(is_formatted<crypto::key_derivation>());
77 EXPECT_TRUE(is_formatted<crypto::key_image>());
78 EXPECT_TRUE(is_formatted<rct::key>());
79 }
80
81 TEST(Crypto, null_keys)
82 {
83 char zero[32];
84 memset(zero, 0, 32);
85 ASSERT_EQ(memcmp(crypto::null_skey.data, zero, 32), 0);
86 ASSERT_EQ(memcmp(crypto::null_pkey.data, zero, 32), 0);
87 }
88
89 TEST(Crypto, verify_32)
90 {
91 // all bytes are treated the same, so we can brute force just one byte
92 unsigned char k0[32] = {0}, k1[32] = {0};
93 for (unsigned int i0 = 0; i0 < 256; ++i0)
94 {
95 k0[0] = i0;
96 for (unsigned int i1 = 0; i1 < 256; ++i1)
97 {
98 k1[0] = i1;
99 ASSERT_EQ(!crypto_verify_32(k0, k1), i0 == i1);
100 }
101 }
102 }
103
104 TEST(Crypto, tree_branch)
105 {
106 crypto::hash inputs[6];
107 crypto::hash branch[8];
108 crypto::hash root, root2;
109 size_t depth;
110 uint32_t path, path2;
111
112 auto hasher = [](const crypto::hash &h0, const crypto::hash &h1) -> crypto::hash
113 {
114 char buffer[64];
115 memcpy(buffer, &h0, 32);
116 memcpy(buffer + 32, &h1, 32);
117 crypto::hash res;
118 cn_fast_hash(buffer, 64, res);
119 return res;
120 };
121
122 for (int n = 0; n < 6; ++n)
123 {
124 memset(&inputs[n], 0, 32);
125 inputs[n].data[0] = n + 1;
126 }
127
128 // empty
129 ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 0, crypto::null_hash.data, (char(*)[32])branch, &depth, &path));
130
131 // one, matching
132 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 1, inputs[0].data, (char(*)[32])branch, &depth, &path));
133 ASSERT_EQ(depth, 0);
134 ASSERT_EQ(path, 0);
135 ASSERT_TRUE(crypto::tree_path(1, 0, &path2));
136 ASSERT_EQ(path, path2);
137 crypto::tree_hash((const char(*)[32])inputs, 1, root.data);
138 ASSERT_EQ(root, inputs[0]);
139 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
140 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
141 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
142
143 // one, not found
144 ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 1, inputs[1].data, (char(*)[32])branch, &depth, &path));
145
146 // two, index 0
147 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 2, inputs[0].data, (char(*)[32])branch, &depth, &path));
148 ASSERT_EQ(depth, 1);
149 ASSERT_EQ(path, 0);
150 ASSERT_TRUE(crypto::tree_path(2, 0, &path2));
151 ASSERT_EQ(path, path2);
152 ASSERT_EQ(branch[0], inputs[1]);
153 crypto::tree_hash((const char(*)[32])inputs, 2, root.data);
154 ASSERT_EQ(root, hasher(inputs[0], inputs[1]));
155 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
156 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
157 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
158 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
159
160 // two, index 1
161 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 2, inputs[1].data, (char(*)[32])branch, &depth, &path));
162 ASSERT_EQ(depth, 1);
163 ASSERT_EQ(path, 1);
164 ASSERT_TRUE(crypto::tree_path(2, 1, &path2));
165 ASSERT_EQ(path, path2);
166 ASSERT_EQ(branch[0], inputs[0]);
167 crypto::tree_hash((const char(*)[32])inputs, 2, root.data);
168 ASSERT_EQ(root, hasher(inputs[0], inputs[1]));
169 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
170 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
171 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
172 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
173
174 // two, not found
175 ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 2, inputs[2].data, (char(*)[32])branch, &depth, &path));
176
177 // a b c 0
178 // x y
179 // z
180
181 // three, index 0
182 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 3, inputs[0].data, (char(*)[32])branch, &depth, &path));
183 ASSERT_GE(depth, 1);
184 ASSERT_LE(depth, 2);
185 ASSERT_TRUE(crypto::tree_path(3, 0, &path2));
186 ASSERT_EQ(path, path2);
187 crypto::tree_hash((const char(*)[32])inputs, 3, root.data);
188 ASSERT_EQ(root, hasher(inputs[0], hasher(inputs[1], inputs[2])));
189 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
190 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
191 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
192 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
193 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
194
195 // three, index 1
196 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 3, inputs[1].data, (char(*)[32])branch, &depth, &path));
197 ASSERT_GE(depth, 1);
198 ASSERT_LE(depth, 2);
199 ASSERT_TRUE(crypto::tree_path(3, 1, &path2));
200 ASSERT_EQ(path, path2);
201 crypto::tree_hash((const char(*)[32])inputs, 3, root.data);
202 ASSERT_EQ(root, hasher(inputs[0], hasher(inputs[1], inputs[2])));
203 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
204 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
205 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
206 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
207 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
208
209 // three, index 2
210 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 3, inputs[2].data, (char(*)[32])branch, &depth, &path));
211 ASSERT_GE(depth, 1);
212 ASSERT_LE(depth, 2);
213 ASSERT_TRUE(crypto::tree_path(3, 2, &path2));
214 ASSERT_EQ(path, path2);
215 crypto::tree_hash((const char(*)[32])inputs, 3, root.data);
216 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
217 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
218 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
219 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
220 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
221 ASSERT_TRUE(crypto::tree_branch_hash(inputs[2].data, (const char(*)[32])branch, depth, path, root2.data));
222 ASSERT_EQ(root, root2);
223
224 // three, not found
225 ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 3, inputs[3].data, (char(*)[32])branch, &depth, &path));
226
227 // a b c d e 0 0 0
228 // x y
229 // z
230 // w
231
232 // five, index 0
233 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 5, inputs[0].data, (char(*)[32])branch, &depth, &path));
234 ASSERT_GE(depth, 2);
235 ASSERT_LE(depth, 3);
236 ASSERT_TRUE(crypto::tree_path(5, 0, &path2));
237 ASSERT_EQ(path, path2);
238 crypto::tree_hash((const char(*)[32])inputs, 5, root.data);
239 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
240 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
241 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
242 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
243 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path));
244 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[5].data, root.data, (const char(*)[32])branch, depth, path));
245 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
246
247 // five, index 1
248 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 5, inputs[1].data, (char(*)[32])branch, &depth, &path));
249 ASSERT_GE(depth, 2);
250 ASSERT_LE(depth, 3);
251 ASSERT_TRUE(crypto::tree_path(5, 1, &path2));
252 ASSERT_EQ(path, path2);
253 crypto::tree_hash((const char(*)[32])inputs, 5, root.data);
254 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
255 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
256 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
257 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
258 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path));
259 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[5].data, root.data, (const char(*)[32])branch, depth, path));
260 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
261
262 // five, index 2
263 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 5, inputs[2].data, (char(*)[32])branch, &depth, &path));
264 ASSERT_GE(depth, 2);
265 ASSERT_LE(depth, 3);
266 ASSERT_TRUE(crypto::tree_path(5, 2, &path2));
267 ASSERT_EQ(path, path2);
268 crypto::tree_hash((const char(*)[32])inputs, 5, root.data);
269 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
270 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
271 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
272 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
273 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path));
274 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[5].data, root.data, (const char(*)[32])branch, depth, path));
275 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
276
277 // five, index 4
278 ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 5, inputs[4].data, (char(*)[32])branch, &depth, &path));
279 ASSERT_GE(depth, 2);
280 ASSERT_LE(depth, 3);
281 ASSERT_TRUE(crypto::tree_path(5, 4, &path2));
282 ASSERT_EQ(path, path2);
283 crypto::tree_hash((const char(*)[32])inputs, 5, root.data);
284 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
285 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
286 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
287 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
288 ASSERT_TRUE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path));
289 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[5].data, root.data, (const char(*)[32])branch, depth, path));
290 ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
291
292 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth - 1, path));
293 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth + 1, path));
294 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path ^ 1));
295 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path ^ 2));
296 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path ^ 3));
297 ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])(branch + 1), depth, path));
298
299 // five, not found
300 ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 5, crypto::null_hash.data, (char(*)[32])branch, &depth, &path));
301
302 // depth encoding roundtrip
303 for (uint32_t n_chains = 1; n_chains <= 65; ++n_chains)
304 {
305 for (uint32_t nonce = 0; nonce < 1024; ++nonce)
306 {
307 const uint32_t depth = cryptonote::encode_mm_depth(n_chains, nonce);
308 uint32_t n_chains_2, nonce_2;
309 ASSERT_TRUE(cryptonote::decode_mm_depth(depth, n_chains_2, nonce_2));
310 ASSERT_EQ(n_chains, n_chains_2);
311 ASSERT_EQ(nonce, nonce_2);
312 }
313 }
314 }