Skip to content

Commit

Permalink
crypto: replace CountBits with std::bit_width
Browse files Browse the repository at this point in the history
bit_width is a drop-in replacement with an exact meaning in c++, so there is
no need to continue testing/fuzzing.
  • Loading branch information
theuni committed Dec 13, 2023
1 parent b9b40cd commit 271a07d
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 34 deletions.
7 changes: 0 additions & 7 deletions src/crypto/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <config/bitcoin-config.h>
#endif

#include <bit>
#include <stdint.h>
#include <string.h>

Expand Down Expand Up @@ -87,10 +86,4 @@ void static inline WriteBE64(unsigned char* ptr, uint64_t x)
memcpy(ptr, &v, 8);
}

/** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
uint64_t static inline CountBits(uint64_t x)
{
return std::bit_width(x);
}

#endif // BITCOIN_CRYPTO_COMMON_H
2 changes: 1 addition & 1 deletion src/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class FastRandomContext
{
assert(range);
--range;
int bits = CountBits(range);
int bits = std::bit_width(range);
while (true) {
uint64_t ret = randbits(bits);
if (ret <= range) return ret;
Expand Down
22 changes: 0 additions & 22 deletions src/test/crypto_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,28 +1060,6 @@ BOOST_AUTO_TEST_CASE(hkdf_hmac_sha256_l32_tests)
"8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d");
}

BOOST_AUTO_TEST_CASE(countbits_tests)
{
FastRandomContext ctx;
for (unsigned int i = 0; i <= 64; ++i) {
if (i == 0) {
// Check handling of zero.
BOOST_CHECK_EQUAL(CountBits(0), 0U);
} else if (i < 10) {
for (uint64_t j = uint64_t{1} << (i - 1); (j >> i) == 0; ++j) {
// Exhaustively test up to 10 bits
BOOST_CHECK_EQUAL(CountBits(j), i);
}
} else {
for (int k = 0; k < 1000; k++) {
// Randomly test 1000 samples of each length above 10 bits.
uint64_t j = (uint64_t{1}) << (i - 1) | ctx.randbits(i - 1);
BOOST_CHECK_EQUAL(CountBits(j), i);
}
}
}
}

BOOST_AUTO_TEST_CASE(sha256d64)
{
for (int i = 0; i <= 32; ++i) {
Expand Down
1 change: 0 additions & 1 deletion src/test/fuzz/integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ FUZZ_TARGET(integer, .init = initialize_integer)
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
const std::vector<uint256> v256{u256, u256_min, u256_max};
(void)ComputeMerkleRoot(v256);
(void)CountBits(u64);
(void)DecompressAmount(u64);
{
if (std::optional<CAmount> parsed = ParseMoney(FormatMoney(i64))) {
Expand Down
6 changes: 3 additions & 3 deletions src/util/asmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <util/asmap.h>

#include <clientversion.h>
#include <crypto/common.h>
#include <logging.h>
#include <serialize.h>
#include <streams.h>
#include <util/fs.h>

#include <algorithm>
#include <bit>
#include <cassert>
#include <cstdio>
#include <utility>
Expand Down Expand Up @@ -111,7 +111,7 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
} else if (opcode == Instruction::MATCH) {
match = DecodeMatch(pos, endpos);
if (match == INVALID) break; // Match bits straddle EOF
matchlen = CountBits(match) - 1;
matchlen = std::bit_width(match) - 1;
if (bits < matchlen) break; // Not enough input bits
for (uint32_t bit = 0; bit < matchlen; bit++) {
if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
Expand Down Expand Up @@ -175,7 +175,7 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
} else if (opcode == Instruction::MATCH) {
uint32_t match = DecodeMatch(pos, endpos);
if (match == INVALID) return false; // Match bits straddle EOF
int matchlen = CountBits(match) - 1;
int matchlen = std::bit_width(match) - 1;
if (prevopcode != Instruction::MATCH) had_incomplete_match = false;
if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete
had_incomplete_match = (matchlen < 8);
Expand Down

0 comments on commit 271a07d

Please sign in to comment.