Coverage Report

Created: 2024-10-21 15:10

/root/bitcoin/src/test/fuzz/feefrac.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2024 The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <util/feefrac.h>
6
#include <test/fuzz/FuzzedDataProvider.h>
7
#include <test/fuzz/fuzz.h>
8
#include <test/fuzz/util.h>
9
10
#include <compare>
11
#include <cstdint>
12
#include <iostream>
13
14
namespace {
15
16
/** Compute a * b, represented in 4x32 bits, highest limb first. */
17
std::array<uint32_t, 4> Mul128(uint64_t a, uint64_t b)
18
0
{
19
0
    std::array<uint32_t, 4> ret{0, 0, 0, 0};
20
21
    /** Perform ret += v << (32 * pos), at 128-bit precision. */
22
0
    auto add_fn = [&](uint64_t v, int pos) {
23
0
        uint64_t accum{0};
24
0
        for (int i = 0; i + pos < 4; ++i) {
25
            // Add current value at limb pos in ret.
26
0
            accum += ret[3 - pos - i];
27
            // Add low or high half of v.
28
0
            if (i == 0) accum += v & 0xffffffff;
29
0
            if (i == 1) accum += v >> 32;
30
            // Store lower half of result in limb pos in ret.
31
0
            ret[3 - pos - i] = accum & 0xffffffff;
32
            // Leave carry in accum.
33
0
            accum >>= 32;
34
0
        }
35
        // Make sure no overflow.
36
0
        assert(accum == 0);
37
0
    };
38
39
    // Multiply the 4 individual limbs (schoolbook multiply, with base 2^32).
40
0
    add_fn((a & 0xffffffff) * (b & 0xffffffff), 0);
41
0
    add_fn((a >> 32) * (b & 0xffffffff), 1);
42
0
    add_fn((a & 0xffffffff) * (b >> 32), 1);
43
0
    add_fn((a >> 32) * (b >> 32), 2);
44
0
    return ret;
45
0
}
46
47
/* comparison helper for std::array */
48
0
std::strong_ordering compare_arrays(const std::array<uint32_t, 4>& a, const std::array<uint32_t, 4>& b) {
49
0
    for (size_t i = 0; i < a.size(); ++i) {
50
0
        if (a[i] != b[i]) return a[i] <=> b[i];
51
0
    }
52
0
    return std::strong_ordering::equal;
53
0
}
54
55
std::strong_ordering MulCompare(int64_t a1, int64_t a2, int64_t b1, int64_t b2)
56
0
{
57
    // Compute and compare signs.
58
0
    int sign_a = (a1 == 0 ? 0 : a1 < 0 ? -1 : 1) * (a2 == 0 ? 0 : a2 < 0 ? -1 : 1);
59
0
    int sign_b = (b1 == 0 ? 0 : b1 < 0 ? -1 : 1) * (b2 == 0 ? 0 : b2 < 0 ? -1 : 1);
60
0
    if (sign_a != sign_b) return sign_a <=> sign_b;
61
62
    // Compute absolute values.
63
0
    uint64_t abs_a1 = static_cast<uint64_t>(a1), abs_a2 = static_cast<uint64_t>(a2);
64
0
    uint64_t abs_b1 = static_cast<uint64_t>(b1), abs_b2 = static_cast<uint64_t>(b2);
65
    // Use (~x + 1) instead of the equivalent (-x) to silence the linter; mod 2^64 behavior is
66
    // intentional here.
67
0
    if (a1 < 0) abs_a1 = ~abs_a1 + 1;
68
0
    if (a2 < 0) abs_a2 = ~abs_a2 + 1;
69
0
    if (b1 < 0) abs_b1 = ~abs_b1 + 1;
70
0
    if (b2 < 0) abs_b2 = ~abs_b2 + 1;
71
72
    // Compute products of absolute values.
73
0
    auto mul_abs_a = Mul128(abs_a1, abs_a2);
74
0
    auto mul_abs_b = Mul128(abs_b1, abs_b2);
75
0
    if (sign_a < 0) {
76
0
        return compare_arrays(mul_abs_b, mul_abs_a);
77
0
    } else {
78
0
        return compare_arrays(mul_abs_a, mul_abs_b);
79
0
    }
80
0
}
81
82
} // namespace
83
84
FUZZ_TARGET(feefrac)
85
0
{
86
0
    FuzzedDataProvider provider(buffer.data(), buffer.size());
87
88
0
    int64_t f1 = provider.ConsumeIntegral<int64_t>();
89
0
    int32_t s1 = provider.ConsumeIntegral<int32_t>();
90
0
    if (s1 == 0) f1 = 0;
91
0
    FeeFrac fr1(f1, s1);
92
0
    assert(fr1.IsEmpty() == (s1 == 0));
93
94
0
    int64_t f2 = provider.ConsumeIntegral<int64_t>();
95
0
    int32_t s2 = provider.ConsumeIntegral<int32_t>();
96
0
    if (s2 == 0) f2 = 0;
97
0
    FeeFrac fr2(f2, s2);
98
0
    assert(fr2.IsEmpty() == (s2 == 0));
99
100
    // Feerate comparisons
101
0
    auto cmp_feerate = MulCompare(f1, s2, f2, s1);
102
0
    assert(FeeRateCompare(fr1, fr2) == cmp_feerate);
103
0
    assert((fr1 << fr2) == std::is_lt(cmp_feerate));
104
0
    assert((fr1 >> fr2) == std::is_gt(cmp_feerate));
105
106
    // Compare with manual invocation of FeeFrac::Mul.
107
0
    auto cmp_mul = FeeFrac::Mul(f1, s2) <=> FeeFrac::Mul(f2, s1);
108
0
    assert(cmp_mul == cmp_feerate);
109
110
    // Same, but using FeeFrac::MulFallback.
111
0
    auto cmp_fallback = FeeFrac::MulFallback(f1, s2) <=> FeeFrac::MulFallback(f2, s1);
112
0
    assert(cmp_fallback == cmp_feerate);
113
114
    // Total order comparisons
115
0
    auto cmp_total = std::is_eq(cmp_feerate) ? (s2 <=> s1) : cmp_feerate;
116
0
    assert((fr1 <=> fr2) == cmp_total);
117
0
    assert((fr1 < fr2) == std::is_lt(cmp_total));
118
0
    assert((fr1 > fr2) == std::is_gt(cmp_total));
119
0
    assert((fr1 <= fr2) == std::is_lteq(cmp_total));
120
0
    assert((fr1 >= fr2) == std::is_gteq(cmp_total));
121
0
    assert((fr1 == fr2) == std::is_eq(cmp_total));
122
0
    assert((fr1 != fr2) == std::is_neq(cmp_total));
123
0
}