Skip to content

Commit c9d3df2

Browse files
committed
cryptonote_basic: fix add_extra_nonce_to_tx_extra() length
1 parent bba6aa5 commit c9d3df2

File tree

5 files changed

+206
-7
lines changed

5 files changed

+206
-7
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -717,16 +717,17 @@ namespace cryptonote
717717
bool add_extra_nonce_to_tx_extra(std::vector<uint8_t>& tx_extra, const blobdata& extra_nonce)
718718
{
719719
CHECK_AND_ASSERT_MES(extra_nonce.size() <= TX_EXTRA_NONCE_MAX_COUNT, false, "extra nonce could be 255 bytes max");
720-
size_t start_pos = tx_extra.size();
721-
tx_extra.resize(tx_extra.size() + 2 + extra_nonce.size());
720+
unsigned char *p = tx_extra.data() + tx_extra.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
723-
tx_extra[start_pos] = TX_EXTRA_NONCE;
724+
*p = TX_EXTRA_NONCE;
725+
++p;
724726
//write len
725-
++start_pos;
726-
tx_extra[start_pos] = static_cast<uint8_t>(extra_nonce.size());
727+
tools::write_varint(p, extra_nonce.size());
728+
assert(p == tx_extra.data() + tx_extra.size() - extra_nonce.size());
727729
//write data
728-
++start_pos;
729-
memcpy(&tx_extra[start_pos], extra_nonce.data(), extra_nonce.size());
730+
memcpy(p, extra_nonce.data(), extra_nonce.size());
730731
return true;
731732
}
732733
//---------------------------------------------------------------

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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
34+
TEST(cn_format_utils, add_extra_nonce_to_tx_extra)
35+
{
36+
static constexpr std::size_t max_nonce_size = TX_EXTRA_NONCE_MAX_COUNT + 1; // we *can* test higher if desired
37+
38+
std::vector<std::uint8_t> extra_prefix;
39+
cryptonote::add_tx_pub_key_to_extra(extra_prefix, crypto::get_H());
40+
41+
std::vector<std::uint8_t> extra;
42+
std::string nonce;
43+
std::vector<cryptonote::tx_extra_field> tx_extra_fields;
44+
extra.reserve(extra_prefix.size() + max_nonce_size + 1 + 10);
45+
nonce.reserve(max_nonce_size);
46+
tx_extra_fields.reserve(2);
47+
for (std::size_t nonce_size = 0; nonce_size <= max_nonce_size; ++nonce_size)
48+
{
49+
extra = extra_prefix;
50+
nonce.resize(nonce_size);
51+
memset(&nonce[0], '%', nonce.size());
52+
tx_extra_fields.clear();
53+
54+
const std::size_t expected_extra_size = extra_prefix.size() + 1
55+
+ tools::get_varint_byte_size(nonce_size) + nonce_size;
56+
const bool expected_success = nonce_size <= TX_EXTRA_NONCE_MAX_COUNT;
57+
58+
// add nonce and do detailed test
59+
const bool add_success = cryptonote::add_extra_nonce_to_tx_extra(extra, nonce);
60+
ASSERT_EQ(expected_success, add_success);
61+
if (!expected_success)
62+
continue;
63+
ASSERT_EQ(expected_extra_size, extra.size());
64+
ASSERT_EQ(0, memcmp(extra_prefix.data(), extra.data(), extra_prefix.size()));
65+
const std::uint8_t *p = extra.data() + extra_prefix.size();
66+
ASSERT_EQ(TX_EXTRA_NONCE, *p);
67+
++p;
68+
std::size_t read_nonce_size = 0;
69+
const int varint_size = tools::read_varint((const uint8_t*)(p), // copy p
70+
(const uint8_t*) extra.data() + extra.size(),
71+
read_nonce_size);
72+
ASSERT_EQ(tools::get_varint_byte_size(nonce_size), varint_size);
73+
p += varint_size;
74+
for (std::size_t i = 0; i < nonce_size; ++i)
75+
{
76+
ASSERT_EQ('%', *p);
77+
++p;
78+
}
79+
ASSERT_EQ(extra.data() + extra.size(), p);
80+
81+
// do integration test with higher-level tx_extra parsing code
82+
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
83+
ASSERT_EQ(2, tx_extra_fields.size());
84+
const auto &pk_field = boost::get<cryptonote::tx_extra_pub_key>(tx_extra_fields.at(0));
85+
ASSERT_EQ(crypto::get_H(), pk_field.pub_key);
86+
const auto &nonce_field = boost::get<cryptonote::tx_extra_nonce>(tx_extra_fields.at(1));
87+
ASSERT_EQ(nonce, nonce_field.nonce);
88+
}
89+
}

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)