@@ -32,18 +32,18 @@ def drop(n: int = 1) -> CScript:
32
32
33
33
# x_0, x_1, ..., x_{n-1} -- sha256(x_0 || x_1), sha256(x_2 || x_3), ...
34
34
# if n is odd, the last element is copied unchanged
35
- def sha256cat (n : int ) -> CScript :
35
+ def reduce_merkle_layer (n : int ) -> CScript :
36
36
assert n >= 1
37
37
38
38
if n == 1 :
39
39
return CScript ([])
40
40
elif n == 2 :
41
41
return CScript ([OP_CAT , OP_SHA256 ])
42
42
if n % 2 == 1 :
43
- return CScript ([OP_TOALTSTACK , * sha256cat (n - 1 ), OP_FROMALTSTACK ])
43
+ return CScript ([OP_TOALTSTACK , * reduce_merkle_layer (n - 1 ), OP_FROMALTSTACK ])
44
44
else :
45
45
# compute the last pair, reduce to the case with one less pair
46
- return CScript ([OP_CAT , OP_SHA256 , OP_TOALTSTACK , * sha256cat (n - 2 ), OP_FROMALTSTACK ])
46
+ return CScript ([OP_CAT , OP_SHA256 , OP_TOALTSTACK , * reduce_merkle_layer (n - 2 ), OP_FROMALTSTACK ])
47
47
48
48
49
49
# x_0, x_1, ..., x_{n - 1} -- x_0, x_1, ..., x_{n - 1} root
@@ -55,7 +55,7 @@ def merkle_root(n_leaves: int) -> CScript:
55
55
ret = []
56
56
# compute layer by layer, from the bottom up to the root
57
57
while n_leaves > 1 :
58
- ret .extend (sha256cat (n_leaves ))
58
+ ret .extend (reduce_merkle_layer (n_leaves ))
59
59
n_leaves = (n_leaves + 1 ) // 2
60
60
return CScript (ret )
61
61
0 commit comments