forked from BitVM/BitVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathu32_zip.rs
More file actions
62 lines (54 loc) · 1.9 KB
/
Copy pathu32_zip.rs
File metadata and controls
62 lines (54 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::treepp::{script, Script};
/// Zips the a-th and b-th u32 elements from the top (without preserving order)
/// Assuming a is smaller than b and x_i denoting the i-th part of the x-th number:
/// Input: ... (a u32 elements) a_0 a_1 a_2 a_3 ... (b - a - 1 u32 elements) b_0 b_1 b_2 b_3
/// Output: b_0 a_0 b_1 a_1 b_2 a_2 b_3 a_3 ... (b - 1 u32 elements and rest of the stack)
pub fn u32_zip(mut a: u32, mut b: u32) -> Script {
assert_ne!(a, b);
if a > b {
(a, b) = (b, a);
}
a = (a + 1) * 4 - 1;
b = (b + 1) * 4 - 1;
script! {
{a} OP_ROLL {b} OP_ROLL
{a+1} OP_ROLL {b} OP_ROLL
{a+2} OP_ROLL {b} OP_ROLL
{a+3} OP_ROLL {b} OP_ROLL
}
}
/// Zips the a-th and b-th u32 elements from the top and keep the one chosen (given as the first parameter) in the stack (without preserving order)
/// Assuming a is smaller than b and x_i denoting the i-th part of the x-th number:
/// Input: ... (a u32 elements) a_0 a_1 a_2 a_3 ... (b - a - 1 u32 elements) b_0 b_1 b_2 b_3
/// Output: b_0 a_0 b_1 a_1 b_2 a_2 b_3 a_3 ... (b u32 elements including the element that is chosen to stay and rest of the stack)
pub fn u32_copy_zip(a: u32, b: u32) -> Script {
if a < b {
_u32_copy_zip(a, b)
} else {
_u32_zip_copy(b, a)
}
}
/// Helper for u32_copy_zip
pub fn _u32_copy_zip(mut a: u32, mut b: u32) -> Script {
assert!(a < b);
a = (a + 1) * 4 - 1;
b = (b + 1) * 4 - 1;
script! {
{a} OP_PICK {b+1} OP_ROLL
{a+1} OP_PICK {b+2} OP_ROLL
{a+2} OP_PICK {b+3} OP_ROLL
{a+3} OP_PICK {b+4} OP_ROLL
}
}
///Helper for u32_copy_zip
pub fn _u32_zip_copy(mut a: u32, mut b: u32) -> Script {
assert!(a < b);
a = (a + 1) * 4 - 1;
b = (b + 1) * 4 - 1;
script! {
{a} OP_ROLL {b} OP_PICK
{a+1} OP_ROLL {b} OP_PICK
{a+2} OP_ROLL {b} OP_PICK
{a+3} OP_ROLL {b} OP_PICK
}
}