• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-system / 15380319301

01 Jun 2025 10:56PM UTC coverage: 81.132% (-0.08%) from 81.214%
15380319301

push

github

web-flow
Merge pull request #1695 from evoskuil/master

Don't validate clean stack for taproot prevalid, refactor.

7 of 62 new or added lines in 8 files covered. (11.29%)

2 existing lines in 1 file now uncovered.

10432 of 12858 relevant lines covered (81.13%)

3686925.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/chain/taproot.cpp
1
/**
2
 * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/system/chain/taproot.hpp>
20

21
#include <algorithm>
22
#include <bitcoin/system/chain/annex.hpp>
23
#include <bitcoin/system/chain/script.hpp>
24
#include <bitcoin/system/chain/tapscript.hpp>
25
#include <bitcoin/system/crypto/crypto.hpp>
26
#include <bitcoin/system/data/data.hpp>
27
#include <bitcoin/system/define.hpp>
28
#include <bitcoin/system/hash/hash.hpp>
29
#include <bitcoin/system/stream/stream.hpp>
30

31
namespace libbitcoin {
32
namespace system {
33
namespace chain {
34

35
// protected
36
// ----------------------------------------------------------------------------
37

NEW
38
hash_digest taproot::merkle_root(const tapscript::keys_t& keys, size_t count,
×
39
    const hash_digest& tapleaf_hash) NOEXCEPT
40
{
41
    hash_digest hash{ tapleaf_hash };
×
NEW
42
    for (size_t key{}; key < count; ++key)
×
NEW
43
        hash = sorted_branch_hash(hash, keys.at(key));
×
44

45
    return hash;
×
46
}
47

48
hash_digest taproot::sorted_branch_hash(const hash_digest& left,
×
49
    const hash_digest& right) NOEXCEPT
50
{
51
    return std::lexicographical_compare(left.begin(), left.end(),
×
52
        right.begin(), right.end()) ? branch_hash(left, right) :
×
53
        branch_hash(right, left);
×
54
}
55

56
// TapBranch
57
hash_digest taproot::branch_hash(const hash_digest& first,
×
58
    const hash_digest& second) NOEXCEPT
59
{
60
    hash_digest out{};
×
61
    stream::out::fast stream{ out };
×
62
    hash::sha256t::fast<"TapBranch"> sink{ stream };
×
63
    sink.write_bytes(first);
×
64
    sink.write_bytes(second);
×
65
    sink.flush();
×
66
    return out;
×
67
}
×
68

69
// TapTweak
70
hash_digest taproot::tweak_hash(const ec_xonly& key,
×
71
    const hash_digest& merkle) NOEXCEPT
72
{
73
    hash_digest out{};
×
74
    stream::out::fast stream{ out };
×
75
    hash::sha256t::fast<"TapTweak"> sink{ stream };
×
76
    sink.write_bytes(key);
×
77
    sink.write_bytes(merkle);
×
78
    sink.flush();
×
79
    return out;
×
80
}
×
81

82
// public
83
// ----------------------------------------------------------------------------
84

85
// TapLeaf
86
hash_digest taproot::leaf_hash(uint8_t version,
×
87
    const script& script) NOEXCEPT
88
{
89
    hash_digest out{};
×
90
    stream::out::fast stream{ out };
×
91
    hash::sha256t::fast<"TapLeaf"> sink{ stream };
×
92
    sink.write_byte(version);
×
93
    script.to_data(sink, true);
×
94
    sink.flush();
×
95
    return out;
×
96
}
×
97

98
// static
99
bool taproot::drop_annex(chunk_cptrs& stack) NOEXCEPT
×
100
{
NEW
101
    if (!annex::is_annex_pattern(stack))
×
102
        return false;
103

NEW
104
    stack.pop_back();
×
NEW
105
    return true;
×
106
}
107

NEW
108
bool taproot::verify_commit(const tapscript& control, const ec_xonly& out_key,
×
109
    const hash_digest& leaf) NOEXCEPT
110
{
NEW
111
    using namespace schnorr;
×
NEW
112
    const auto root = merkle_root(control.keys(), control.count(), leaf);
×
NEW
113
    const auto tweak = tweak_hash(control.key(), root);
×
NEW
114
    return verify_commitment(control.key(), tweak, out_key, control.parity());
×
115
}
116

117
} // namespace chain
118
} // namespace system
119
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc