Debian Codegraph @

src/cryptonote_basic/merge_mining.cpp

raw symbols SCIP
1 // Copyright (c) 2020-2022, The Monero Project
2
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include <string.h>
31 #include "misc_log_ex.h"
32 #include "int-util.h"
33 #include "crypto/crypto.h"
34 #include "common/util.h"
35 #include "merge_mining.h"
36
37 using namespace epee;
38
39 #undef MONERO_DEFAULT_LOG_CATEGORY
40 #define MONERO_DEFAULT_LOG_CATEGORY "cn.mm"
41
42 using namespace crypto;
43
44 namespace cryptonote
45 {
46
47 //---------------------------------------------------------------
48 uint32_t get_aux_slot(const crypto::hash &id, uint32_t nonce, uint32_t n_aux_chains)
49 {
50 CHECK_AND_ASSERT_THROW_MES(n_aux_chains > 0, "n_aux_chains is 0");
51
52 uint8_t buf[HASH_SIZE + sizeof(uint32_t) + 1];
53 memcpy(buf, &id, HASH_SIZE);
54 uint32_t v = SWAP32LE(nonce);
55 memcpy(buf + HASH_SIZE, &v, sizeof(uint32_t));
56 buf[HASH_SIZE + sizeof(uint32_t)] = config::HASH_KEY_MM_SLOT;
57
58 crypto::hash res;
59 tools::sha256sum(buf, sizeof(buf), res);
60 v = *((const uint32_t*)&res);
61 return SWAP32LE(v) % n_aux_chains;
62 }
63 //---------------------------------------------------------------
64 uint32_t get_path_from_aux_slot(uint32_t slot, uint32_t n_aux_chains)
65 {
66 CHECK_AND_ASSERT_THROW_MES(n_aux_chains > 0, "n_aux_chains is 0");
67 CHECK_AND_ASSERT_THROW_MES(slot < n_aux_chains, "slot >= n_aux_chains");
68
69 uint32_t path = 0;
70 CHECK_AND_ASSERT_THROW_MES(tree_path(n_aux_chains, slot, &path), "Failed to get path from aux slot");
71 return path;
72 }
73 //---------------------------------------------------------------
74 uint32_t encode_mm_depth(uint32_t n_aux_chains, uint32_t nonce)
75 {
76 CHECK_AND_ASSERT_THROW_MES(n_aux_chains > 0, "n_aux_chains is 0");
77
78 // how many bits to we need to representing n_aux_chains - 1
79 uint32_t n_bits = 1;
80 while ((1u << n_bits) < n_aux_chains && n_bits < 16)
81 ++n_bits;
82 CHECK_AND_ASSERT_THROW_MES(n_bits <= 16, "Way too many bits required");
83
84 const uint32_t depth = (n_bits - 1) | ((n_aux_chains - 1) << 3) | (nonce << (3 + n_bits));
85 return depth;
86 }
87 //---------------------------------------------------------------
88 bool decode_mm_depth(uint32_t depth, uint32_t &n_aux_chains, uint32_t &nonce)
89 {
90 const uint32_t n_bits = 1 + (depth & 7);
91 n_aux_chains = 1 + (depth >> 3 & ((1 << n_bits) - 1));
92 nonce = depth >> (3 + n_bits);
93 return true;
94 }
95 //---------------------------------------------------------------
96 }