Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/psbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,15 +561,31 @@ static int map_leaf_hashes_verify(const unsigned char *key, size_t key_len,
* <8-bit depth> <8-bit leaf version> <compact-size scriptlen> <script>. */
static int taproot_tree_value_verify(const unsigned char *val, size_t val_len)
{
/* branch[d] is set while a node at depth d waits for its sibling */
bool branch[WALLY_DESCRIPTOR_TAPTREE_MAX_DEPTH + 1];
size_t branch_len = 0, depth;

if (!val || !val_len)
return WALLY_EINVAL;
while (val_len) {
uint64_t script_len;
size_t vlen;
if (val_len < 3)
return WALLY_EINVAL; /* depth(1) + leaf version(1) + >=1 script-len byte */
if (val[0] > WALLY_DESCRIPTOR_TAPTREE_MAX_DEPTH)
depth = val[0];
Comment thread
bitcoisas marked this conversation as resolved.
Resolved
if (depth > WALLY_DESCRIPTOR_TAPTREE_MAX_DEPTH)
return WALLY_EINVAL; /* depth exceeds the BIP-341 maximum */
if (depth + 1 < branch_len)
return WALLY_EINVAL; /* leaf above an unfinished branch */
while (branch_len > depth && branch[depth]) {
--branch_len; /* combine with its sibling */
if (!depth)
return WALLY_EINVAL; /* leaf after the tree is complete */
--depth;
}
while (branch_len <= depth)
branch[branch_len++] = false;
branch[depth] = true;
if ((val[1] & 1u) || val[1] == 0x50u)
return WALLY_EINVAL; /* leaf version parity bit set, or annex tag */
val += 2;
Expand All @@ -585,7 +601,7 @@ static int taproot_tree_value_verify(const unsigned char *val, size_t val_len)
val += script_len;
val_len -= script_len;
}
return WALLY_OK;
return branch_len == 1 && branch[0] ? WALLY_OK : WALLY_EINVAL;
}

/* Integer-keyed map of PSBT_OUT_TAP_TREE values (key is NULL). */
Expand Down
22 changes: 20 additions & 2 deletions src/test/test_psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,23 @@ def test_taproot_tree(self):
ret = wally_map_add_integer(tree, 1, value, len(value))
self.assertEqual(ret, WALLY_EINVAL)

value = leaf(1, 0xc0, b'\x51') + leaf(2, 0xc0, b'') + leaf(128, 0xc0, b'\x52')
ret = wally_map_add_integer(tree, 1, value, len(value))
# The leaves must form a complete tree in depth-first order
tree_of = lambda depths: b''.join(leaf(d, 0xc0, b'\x51') for d in depths)
for depths, expected in [
([0], WALLY_OK),
([1, 1], WALLY_OK),
([1, 2, 2], WALLY_OK),
([1], WALLY_EINVAL), # Missing sibling
([0, 0], WALLY_EINVAL), # Leaf after the tree is complete
([2, 1, 2], WALLY_EINVAL), # Leaf above an unfinished branch
]:
value = tree_of(depths)
ret = wally_map_replace_integer(tree, 1, value, len(value))
self.assertEqual(ret, expected)

value = leaf(1, 0xc0, b'\x51') + leaf(2, 0xc0, b'') + \
tree_of(range(3, 128)) + leaf(128, 0xc0, b'\x52') * 2
ret = wally_map_replace_integer(tree, 1, value, len(value))
self.assertEqual(ret, WALLY_OK)

# Valid trees round-trip, invalid trees fail to parse
Expand All @@ -409,6 +424,9 @@ def test_taproot_tree(self):
invalid = bytes(buf).replace(value, leaf(1, 0xc1, b'\x51') + value[4:])
ret = wally_psbt_from_bytes(invalid, len(invalid), 0, pointer(wally_psbt()))
self.assertEqual(ret, WALLY_EINVAL)
invalid = bytes(buf).replace(value, bytes([2]) + value[1:])
ret = wally_psbt_from_bytes(invalid, len(invalid), 0, pointer(wally_psbt()))
self.assertEqual(ret, WALLY_EINVAL)
wally_psbt_free(psbt)

def test_psbt(self):
Expand Down