Skip to content

Commit 4774c5c

Browse files
committed
Add zero sized sieve bypass.
1 parent fe9d1a2 commit 4774c5c

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

include/bitcoin/database/impl/primitives/sieve.ipp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ TEMPLATE
8888
constexpr bool CLASS::screened(sieve_t fingerprint) const NOEXCEPT
8989
{
9090
using namespace system;
91+
if constexpr (is_zero(limit))
92+
return true;
93+
9194
const auto row = shift_right(sieve_, screen_bits);
9295
if (row == limit)
9396
{
@@ -114,6 +117,9 @@ TEMPLATE
114117
constexpr bool CLASS::screen(sieve_t fingerprint) NOEXCEPT
115118
{
116119
using namespace system;
120+
if constexpr (is_zero(limit))
121+
return false;
122+
117123
auto row = shift_right(sieve_, screen_bits);
118124
if (row == limit)
119125
{

test/primitives/sieve.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ BOOST_AUTO_TEST_SUITE(sieve_tests)
2222

2323
using namespace system;
2424

25+
BOOST_AUTO_TEST_CASE(sieve__screen__zeroed__always_screened)
26+
{
27+
// Ensure default/nop behavior.
28+
static_assert(sieve<0, 0>{}.screened(42));
29+
static_assert(sieve<1, 0>{}.screened(42));
30+
static_assert(!sieve<1, 1>{}.screened(42));
31+
32+
sieve<0, 0> sieve0{};
33+
BOOST_REQUIRE(sieve0.screened(42));
34+
BOOST_REQUIRE(!sieve0.screen(42));
35+
36+
sieve<1, 0> sieve1{};
37+
BOOST_REQUIRE(sieve1.screened(42));
38+
BOOST_REQUIRE(!sieve1.screen(42));
39+
40+
sieve<1, 1> sieve2{};
41+
BOOST_REQUIRE(!sieve2.screened(42));
42+
BOOST_REQUIRE(sieve2.screen(42));
43+
}
44+
2545
#if defined(HAVE_SLOW_TESTS)
2646

2747
BOOST_AUTO_TEST_CASE(sieve__screen__4_bits_forward__16_screens)

0 commit comments

Comments
 (0)