Skip to content

Commit 2eed71e

Browse files
committed
cryptonote_basic: fix add_extra_nonce_to_tx_extra() length
Reviewed-by: selsta <selsta@sent.at> Reviewed-by: SChernykh
1 parent bba6aa5 commit 2eed71e

File tree

5 files changed

+362
-4
lines changed

5 files changed

+362
-4
lines changed

src/common/varint.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,14 @@ namespace tools {
125125
int read_varint(InputIt &&first, InputIt &&last, T &i) {
126126
return read_varint<std::numeric_limits<T>::digits>(std::forward<InputIt>(first), std::forward<InputIt>(last), i);
127127
}
128+
129+
template <typename T, typename = std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>>>
130+
constexpr std::size_t get_varint_byte_size(T val) {
131+
std::size_t bytes = 0;
132+
do {
133+
++bytes;
134+
val >>= 7;
135+
} while (val);
136+
return bytes;
137+
}
128138
}

src/cryptonote_basic/cryptonote_format_utils.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,15 +718,19 @@ namespace cryptonote
718718
{
719719
CHECK_AND_ASSERT_MES(extra_nonce.size() <= TX_EXTRA_NONCE_MAX_COUNT, false, "extra nonce could be 255 bytes max");
720720
size_t start_pos = tx_extra.size();
721-
tx_extra.resize(tx_extra.size() + 2 + extra_nonce.size());
721+
const std::size_t len_varint_bytes = tools::get_varint_byte_size(extra_nonce.size());
722+
tx_extra.resize(tx_extra.size() + 1 + len_varint_bytes + extra_nonce.size());
722723
//write tag
723724
tx_extra[start_pos] = TX_EXTRA_NONCE;
724725
//write len
725726
++start_pos;
726-
tx_extra[start_pos] = static_cast<uint8_t>(extra_nonce.size());
727+
unsigned char * vp = tx_extra.data() + start_pos;
728+
tools::write_varint(vp, extra_nonce.size());
729+
assert(vp == tx_extra.data() + tx_extra.size() - extra_nonce.size());
727730
//write data
728-
++start_pos;
729-
memcpy(&tx_extra[start_pos], extra_nonce.data(), extra_nonce.size());
731+
start_pos += len_varint_bytes;
732+
if (!extra_nonce.empty())
733+
memcpy(&tx_extra[start_pos], extra_nonce.data(), extra_nonce.size());
730734
return true;
731735
}
732736
//---------------------------------------------------------------

tests/unit_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ set(unit_tests_sources
4242
checkpoints.cpp
4343
command_line.cpp
4444
crypto.cpp
45+
cryptonote_format_utils.cpp
4546
decompose_amount_into_digits.cpp
4647
device.cpp
4748
difficulty.cpp
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
// Copyright (c) 2025, 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 "gtest/gtest.h"
30+
31+
#include "crypto/generators.h"
32+
#include "cryptonote_basic/cryptonote_format_utils.h"
33+
#include "serialization/binary_utils.h"
34+
#include "serialization/string.h"
35+
36+
TEST(cn_format_utils, add_extra_nonce_to_tx_extra)
37+
{
38+
static constexpr std::size_t max_nonce_size = TX_EXTRA_NONCE_MAX_COUNT + 1; // we *can* test higher if desired
39+
40+
for (int empty_prefix = 0; empty_prefix < 2; ++empty_prefix)
41+
{
42+
std::vector<std::uint8_t> extra_prefix;
43+
if (!empty_prefix)
44+
cryptonote::add_tx_pub_key_to_extra(extra_prefix, crypto::get_H());
45+
46+
std::vector<std::uint8_t> extra;
47+
std::string nonce;
48+
std::vector<cryptonote::tx_extra_field> tx_extra_fields;
49+
extra.reserve(extra_prefix.size() + max_nonce_size + 1 + 10);
50+
nonce.reserve(max_nonce_size);
51+
tx_extra_fields.reserve(2);
52+
for (std::size_t nonce_size = 0; nonce_size <= max_nonce_size; ++nonce_size)
53+
{
54+
extra = extra_prefix;
55+
nonce.resize(nonce_size);
56+
if (nonce.size())
57+
memset(&nonce[0], '%', nonce.size());
58+
tx_extra_fields.clear();
59+
60+
const std::size_t expected_extra_size = extra_prefix.size() + 1
61+
+ tools::get_varint_byte_size(nonce_size) + nonce_size;
62+
const bool expected_success = nonce_size <= TX_EXTRA_NONCE_MAX_COUNT;
63+
64+
// add nonce and do detailed test
65+
const bool add_success = cryptonote::add_extra_nonce_to_tx_extra(extra, nonce);
66+
ASSERT_EQ(expected_success, add_success);
67+
if (!expected_success)
68+
continue;
69+
ASSERT_EQ(expected_extra_size, extra.size());
70+
ASSERT_EQ(0, memcmp(extra_prefix.data(), extra.data(), extra_prefix.size()));
71+
const std::uint8_t *p = extra.data() + extra_prefix.size();
72+
ASSERT_EQ(TX_EXTRA_NONCE, *p);
73+
++p;
74+
std::size_t read_nonce_size = 0;
75+
const int varint_size = tools::read_varint((const uint8_t*)(p), // copy p
76+
(const uint8_t*) extra.data() + extra.size(),
77+
read_nonce_size);
78+
ASSERT_EQ(tools::get_varint_byte_size(nonce_size), varint_size);
79+
p += varint_size;
80+
for (std::size_t i = 0; i < nonce_size; ++i)
81+
{
82+
ASSERT_EQ('%', *p);
83+
++p;
84+
}
85+
ASSERT_EQ(extra.data() + extra.size(), p);
86+
87+
// do integration test with higher-level tx_extra parsing code
88+
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
89+
if (empty_prefix)
90+
{
91+
ASSERT_EQ(1, tx_extra_fields.size());
92+
const auto &nonce_field = boost::get<cryptonote::tx_extra_nonce>(tx_extra_fields.at(0));
93+
ASSERT_EQ(nonce, nonce_field.nonce);
94+
}
95+
else
96+
{
97+
ASSERT_EQ(2, tx_extra_fields.size());
98+
const auto &pk_field = boost::get<cryptonote::tx_extra_pub_key>(tx_extra_fields.at(0));
99+
ASSERT_EQ(crypto::get_H(), pk_field.pub_key);
100+
const auto &nonce_field = boost::get<cryptonote::tx_extra_nonce>(tx_extra_fields.at(1));
101+
ASSERT_EQ(nonce, nonce_field.nonce);
102+
}
103+
}
104+
}
105+
}
106+
107+
TEST(cn_format_utils, add_mm_merkle_root_to_tx_extra)
108+
{
109+
const std::vector<std::uint64_t> depths{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 63, 64, 127, 128, 16383, 16384};
110+
111+
const crypto::hash mm_merkle_root = crypto::rand<crypto::hash>();
112+
113+
for (int empty_prefix = 0; empty_prefix < 2; ++empty_prefix)
114+
{
115+
std::vector<std::uint8_t> extra_prefix;
116+
if (!empty_prefix)
117+
cryptonote::add_tx_pub_key_to_extra(extra_prefix, crypto::get_H());
118+
119+
std::vector<std::uint8_t> extra;
120+
std::vector<cryptonote::tx_extra_field> tx_extra_fields;
121+
extra.reserve(extra_prefix.size() + 1 + 1 + 10 + 32);
122+
tx_extra_fields.reserve(2);
123+
for (std::uint64_t mm_merkle_tree_depth : depths)
124+
{
125+
extra = extra_prefix;
126+
tx_extra_fields.clear();
127+
128+
const std::size_t expected_extra_size = extra_prefix.size() + 1 + 1
129+
+ tools::get_varint_byte_size(mm_merkle_tree_depth) + 32;
130+
131+
// add nonce and do detailed test
132+
const bool add_success = cryptonote::add_mm_merkle_root_to_tx_extra(extra, mm_merkle_root, mm_merkle_tree_depth);
133+
ASSERT_TRUE(add_success);
134+
ASSERT_EQ(expected_extra_size, extra.size());
135+
ASSERT_EQ(0, memcmp(extra_prefix.data(), extra.data(), extra_prefix.size()));
136+
const std::uint8_t *p = extra.data() + extra_prefix.size();
137+
ASSERT_EQ(TX_EXTRA_MERGE_MINING_TAG, *p);
138+
++p;
139+
ASSERT_EQ(32 + tools::get_varint_byte_size(mm_merkle_tree_depth), *p);
140+
++p;
141+
std::uint64_t read_depth = 0;
142+
const int varint_size = tools::read_varint((const uint8_t*)(p), // copy p
143+
(const uint8_t*) extra.data() + extra.size(),
144+
read_depth);
145+
ASSERT_EQ(tools::get_varint_byte_size(mm_merkle_tree_depth), varint_size);
146+
ASSERT_EQ(mm_merkle_tree_depth, read_depth);
147+
p += varint_size;
148+
ASSERT_EQ(0, memcmp(p, mm_merkle_root.data, sizeof(mm_merkle_root)));
149+
p += sizeof(crypto::hash);
150+
ASSERT_EQ(extra.data() + extra.size(), p);
151+
152+
// do integration test with higher-level tx_extra parsing code
153+
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
154+
if (empty_prefix)
155+
{
156+
ASSERT_EQ(1, tx_extra_fields.size());
157+
const auto &mm_field = boost::get<cryptonote::tx_extra_merge_mining_tag>(tx_extra_fields.at(0));
158+
ASSERT_EQ(mm_merkle_root, mm_field.merkle_root);
159+
ASSERT_EQ(mm_merkle_tree_depth, mm_field.depth);
160+
}
161+
else
162+
{
163+
ASSERT_EQ(2, tx_extra_fields.size());
164+
const auto &pk_field = boost::get<cryptonote::tx_extra_pub_key>(tx_extra_fields.at(0));
165+
ASSERT_EQ(crypto::get_H(), pk_field.pub_key);
166+
const auto &mm_field = boost::get<cryptonote::tx_extra_merge_mining_tag>(tx_extra_fields.at(1));
167+
ASSERT_EQ(mm_merkle_root, mm_field.merkle_root);
168+
ASSERT_EQ(mm_merkle_tree_depth, mm_field.depth);
169+
}
170+
}
171+
}
172+
}
173+
174+
TEST(cn_format_utils, tx_extra_merge_mining_tag_store_load)
175+
{
176+
const std::vector<std::uint64_t> depths{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 63, 64, 127, 128, 16383, 16384};
177+
178+
const crypto::hash mm_merkle_root = crypto::rand<crypto::hash>();
179+
180+
for (int empty_prefix = 0; empty_prefix < 2; ++empty_prefix)
181+
{
182+
std::vector<std::uint8_t> extra_prefix;
183+
if (!empty_prefix)
184+
cryptonote::add_tx_pub_key_to_extra(extra_prefix, crypto::get_H());
185+
186+
std::vector<std::uint8_t> extra;
187+
std::vector<cryptonote::tx_extra_field> tx_extra_fields;
188+
extra.reserve(extra_prefix.size() + 1 + 1 + 10 + 32);
189+
tx_extra_fields.reserve(2);
190+
for (std::uint64_t mm_merkle_tree_depth : depths)
191+
{
192+
extra = extra_prefix;
193+
tx_extra_fields.clear();
194+
195+
const std::size_t expected_extra_size = extra_prefix.size() + 1 + 1
196+
+ tools::get_varint_byte_size(mm_merkle_tree_depth) + 32;
197+
198+
// add nonce and do detailed test
199+
cryptonote::tx_extra_merge_mining_tag mm;
200+
mm.depth = mm_merkle_tree_depth;
201+
mm.merkle_root = mm_merkle_root;
202+
cryptonote::tx_extra_field extra_field = mm;
203+
std::string mm_blob;
204+
ASSERT_TRUE(::serialization::dump_binary(extra_field, mm_blob));
205+
extra.resize(extra.size() + mm_blob.size());
206+
memcpy(extra.data() + extra.size() - mm_blob.size(), mm_blob.data(), mm_blob.size());
207+
ASSERT_EQ(expected_extra_size, extra.size());
208+
ASSERT_EQ(0, memcmp(extra_prefix.data(), extra.data(), extra_prefix.size()));
209+
const std::uint8_t *p = extra.data() + extra_prefix.size();
210+
ASSERT_EQ(TX_EXTRA_MERGE_MINING_TAG, *p);
211+
++p;
212+
ASSERT_EQ(32 + tools::get_varint_byte_size(mm_merkle_tree_depth), *p);
213+
++p;
214+
std::uint64_t read_depth = 0;
215+
const int varint_size = tools::read_varint((const uint8_t*)(p), // copy p
216+
(const uint8_t*) extra.data() + extra.size(),
217+
read_depth);
218+
ASSERT_EQ(tools::get_varint_byte_size(mm_merkle_tree_depth), varint_size);
219+
ASSERT_EQ(mm_merkle_tree_depth, read_depth);
220+
p += varint_size;
221+
ASSERT_EQ(0, memcmp(p, mm_merkle_root.data, sizeof(mm_merkle_root)));
222+
p += sizeof(crypto::hash);
223+
ASSERT_EQ(extra.data() + extra.size(), p);
224+
225+
// do integration test with higher-level tx_extra parsing code
226+
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
227+
if (empty_prefix)
228+
{
229+
ASSERT_EQ(1, tx_extra_fields.size());
230+
const auto &mm_field = boost::get<cryptonote::tx_extra_merge_mining_tag>(tx_extra_fields.at(0));
231+
ASSERT_EQ(mm_merkle_root, mm_field.merkle_root);
232+
ASSERT_EQ(mm_merkle_tree_depth, mm_field.depth);
233+
}
234+
else
235+
{
236+
ASSERT_EQ(2, tx_extra_fields.size());
237+
const auto &pk_field = boost::get<cryptonote::tx_extra_pub_key>(tx_extra_fields.at(0));
238+
ASSERT_EQ(crypto::get_H(), pk_field.pub_key);
239+
const auto &mm_field = boost::get<cryptonote::tx_extra_merge_mining_tag>(tx_extra_fields.at(1));
240+
ASSERT_EQ(mm_merkle_root, mm_field.merkle_root);
241+
ASSERT_EQ(mm_merkle_tree_depth, mm_field.depth);
242+
}
243+
}
244+
}
245+
}

tests/unit_tests/varint.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,101 @@ TEST(varint, equal)
6262
ASSERT_TRUE(idx2 == idx);
6363
}
6464
}
65+
66+
TEST(varint, max_uint64_t_bytes)
67+
{
68+
unsigned char bytes[100]{};
69+
unsigned char *p = bytes;
70+
tools::write_varint(p, std::numeric_limits<std::uint64_t>::max());
71+
std::size_t i = 0;
72+
while (bytes[i]) ++i;
73+
ASSERT_EQ(10, i); // tests size of max 64-bit uint
74+
ASSERT_EQ(bytes + i, p); // tests iterator is modified in-place
75+
}
76+
77+
template <typename T>
78+
static void subtest_varint_byte_size_for_single_value(const std::size_t expected_bytes, const T val)
79+
{
80+
static_assert(std::is_integral_v<T> && std::is_unsigned_v<T>);
81+
static constexpr int DIGITS = std::numeric_limits<T>::digits;
82+
static_assert(DIGITS == CHAR_BIT * sizeof(T));
83+
static_assert(sizeof(T));
84+
static_assert(sizeof(T) <= 8); // 64-bit or less
85+
86+
unsigned char bytes[100]{};
87+
unsigned char *p = bytes;
88+
tools::write_varint(p, val);
89+
ASSERT_EQ(expected_bytes, p - bytes);
90+
ASSERT_EQ(expected_bytes, tools::get_varint_byte_size(val));
91+
}
92+
93+
template <typename T>
94+
static void subtest_varint_byte_size_for_type()
95+
{
96+
static_assert(std::is_integral_v<T> && std::is_unsigned_v<T>);
97+
static constexpr int DIGITS = std::numeric_limits<T>::digits;
98+
static_assert(DIGITS == CHAR_BIT * sizeof(T));
99+
static_assert(sizeof(T));
100+
101+
for (T n = 0; n < 128; ++n)
102+
{
103+
subtest_varint_byte_size_for_single_value(1, n);
104+
}
105+
106+
for (T n = 128; n < 255; ++n)
107+
{
108+
subtest_varint_byte_size_for_single_value(2, n);
109+
}
110+
111+
subtest_varint_byte_size_for_single_value(2, T{255});
112+
113+
if constexpr (DIGITS > 8)
114+
{
115+
static_assert(DIGITS >= 16);
116+
for (T n = 256; n < 16384; ++n)
117+
{
118+
subtest_varint_byte_size_for_single_value(2, n);
119+
}
120+
121+
for (T n = 16384; n < 65535; ++n)
122+
{
123+
subtest_varint_byte_size_for_single_value(3, n);
124+
}
125+
126+
subtest_varint_byte_size_for_single_value(3, T{65535});
127+
128+
if constexpr (DIGITS > 16)
129+
{
130+
static_assert(DIGITS >= 32);
131+
subtest_varint_byte_size_for_single_value(3, T{2097151});
132+
subtest_varint_byte_size_for_single_value(4, T{2097152});
133+
subtest_varint_byte_size_for_single_value(4, T{268435455});
134+
subtest_varint_byte_size_for_single_value(5, T{268435456});
135+
subtest_varint_byte_size_for_single_value(5, T{4294967295});
136+
137+
if constexpr (DIGITS > 32)
138+
{
139+
static_assert(DIGITS >= 64);
140+
subtest_varint_byte_size_for_single_value(5, T{34359738367});
141+
subtest_varint_byte_size_for_single_value(6, T{34359738368});
142+
subtest_varint_byte_size_for_single_value(6, T{4398046511103});
143+
subtest_varint_byte_size_for_single_value(7, T{4398046511104});
144+
subtest_varint_byte_size_for_single_value(7, T{562949953421311});
145+
subtest_varint_byte_size_for_single_value(8, T{562949953421312});
146+
subtest_varint_byte_size_for_single_value(8, T{72057594037927935});
147+
subtest_varint_byte_size_for_single_value(9, T{72057594037927936});
148+
subtest_varint_byte_size_for_single_value(9, T{9223372036854775807});
149+
subtest_varint_byte_size_for_single_value(10, T{9223372036854775808ull});
150+
subtest_varint_byte_size_for_single_value(10, T{18446744073709551615ull});
151+
}
152+
}
153+
}
154+
};
155+
156+
TEST(varint, get_varint_byte_size)
157+
{
158+
subtest_varint_byte_size_for_type<std::uint8_t>();
159+
subtest_varint_byte_size_for_type<std::uint16_t>();
160+
subtest_varint_byte_size_for_type<std::uint32_t>();
161+
subtest_varint_byte_size_for_type<std::uint64_t>();
162+
}

0 commit comments

Comments
 (0)