Skip to content

Commit 297367b

Browse files
committed
crypto: replace CountBits with std::bit_width
bit_width is a drop-in replacement with an exact meaning in c++, so there is no need to continue testing/fuzzing/benchmarking.
1 parent 52f9bba commit 297367b

File tree

5 files changed

+5
-34
lines changed

5 files changed

+5
-34
lines changed

src/crypto/common.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <compat/endian.h>
99

10-
#include <bit>
1110
#include <cstdint>
1211
#include <cstring>
1312

@@ -83,10 +82,4 @@ void static inline WriteBE64(unsigned char* ptr, uint64_t x)
8382
memcpy(ptr, &v, 8);
8483
}
8584

86-
/** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
87-
uint64_t static inline CountBits(uint64_t x)
88-
{
89-
return std::bit_width(x);
90-
}
91-
9285
#endif // BITCOIN_CRYPTO_COMMON_H

src/random.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <span.h>
1212
#include <uint256.h>
1313

14+
#include <bit>
1415
#include <cassert>
1516
#include <chrono>
1617
#include <cstdint>
@@ -203,7 +204,7 @@ class FastRandomContext
203204
{
204205
assert(range);
205206
--range;
206-
int bits = CountBits(range);
207+
int bits = std::bit_width(range);
207208
while (true) {
208209
uint64_t ret = randbits(bits);
209210
if (ret <= range) return ret;

src/test/crypto_tests.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,28 +1060,6 @@ BOOST_AUTO_TEST_CASE(hkdf_hmac_sha256_l32_tests)
10601060
"8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d");
10611061
}
10621062

1063-
BOOST_AUTO_TEST_CASE(countbits_tests)
1064-
{
1065-
FastRandomContext ctx;
1066-
for (unsigned int i = 0; i <= 64; ++i) {
1067-
if (i == 0) {
1068-
// Check handling of zero.
1069-
BOOST_CHECK_EQUAL(CountBits(0), 0U);
1070-
} else if (i < 10) {
1071-
for (uint64_t j = uint64_t{1} << (i - 1); (j >> i) == 0; ++j) {
1072-
// Exhaustively test up to 10 bits
1073-
BOOST_CHECK_EQUAL(CountBits(j), i);
1074-
}
1075-
} else {
1076-
for (int k = 0; k < 1000; k++) {
1077-
// Randomly test 1000 samples of each length above 10 bits.
1078-
uint64_t j = (uint64_t{1}) << (i - 1) | ctx.randbits(i - 1);
1079-
BOOST_CHECK_EQUAL(CountBits(j), i);
1080-
}
1081-
}
1082-
}
1083-
}
1084-
10851063
BOOST_AUTO_TEST_CASE(sha256d64)
10861064
{
10871065
for (int i = 0; i <= 32; ++i) {

src/test/fuzz/integer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ FUZZ_TARGET(integer, .init = initialize_integer)
8080
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
8181
const std::vector<uint256> v256{u256, u256_min, u256_max};
8282
(void)ComputeMerkleRoot(v256);
83-
(void)CountBits(u64);
8483
(void)DecompressAmount(u64);
8584
{
8685
if (std::optional<CAmount> parsed = ParseMoney(FormatMoney(i64))) {

src/util/asmap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
#include <util/asmap.h>
66

77
#include <clientversion.h>
8-
#include <crypto/common.h>
98
#include <logging.h>
109
#include <serialize.h>
1110
#include <streams.h>
1211
#include <util/fs.h>
1312

1413
#include <algorithm>
14+
#include <bit>
1515
#include <cassert>
1616
#include <cstdio>
1717
#include <utility>
@@ -111,7 +111,7 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
111111
} else if (opcode == Instruction::MATCH) {
112112
match = DecodeMatch(pos, endpos);
113113
if (match == INVALID) break; // Match bits straddle EOF
114-
matchlen = CountBits(match) - 1;
114+
matchlen = std::bit_width(match) - 1;
115115
if (bits < matchlen) break; // Not enough input bits
116116
for (uint32_t bit = 0; bit < matchlen; bit++) {
117117
if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
@@ -175,7 +175,7 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
175175
} else if (opcode == Instruction::MATCH) {
176176
uint32_t match = DecodeMatch(pos, endpos);
177177
if (match == INVALID) return false; // Match bits straddle EOF
178-
int matchlen = CountBits(match) - 1;
178+
int matchlen = std::bit_width(match) - 1;
179179
if (prevopcode != Instruction::MATCH) had_incomplete_match = false;
180180
if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete
181181
had_incomplete_match = (matchlen < 8);

0 commit comments

Comments
 (0)