forked from BitVM/BitVM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathu32_rrot.rs
267 lines (238 loc) · 5.72 KB
/
u32_rrot.rs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use crate::treepp::*;
/// Right rotation of an u32 element by 16 bits
pub fn u32_rrot16() -> Script {
script! {
OP_2SWAP
}
}
/// Right rotation of an u32 element by 8 bits
pub fn u32_rrot8() -> Script {
script! {
OP_2SWAP
3 OP_ROLL
}
}
/// Right rotation of the i-th u8 element by 7 bits
pub fn u8_rrot7(i: u32) -> Script {
let roll_script = match i {
0 => script! {},
1 => script! { OP_SWAP },
2 => script! { OP_ROT },
_ => script! { {i} OP_ROLL },
};
script! {
{ roll_script }
128
OP_2DUP
OP_GREATERTHANOREQUAL
OP_IF
OP_SUB
1
OP_ELSE
OP_DROP
0
OP_ENDIF
}
}
/// Right rotation of an u32 element by 7 bits
pub fn u32_rrot7() -> Script {
script! {
// First Byte
{u8_rrot7(0)}
// Second byte
{u8_rrot7(2)}
OP_TOALTSTACK
OP_DUP
OP_ADD
OP_ADD
OP_FROMALTSTACK
// Third byte
{u8_rrot7(3)}
OP_TOALTSTACK
OP_DUP
OP_ADD
OP_ADD
OP_FROMALTSTACK
// Fourth byte
{u8_rrot7(4)}
OP_TOALTSTACK
OP_DUP
OP_ADD
OP_ADD
OP_FROMALTSTACK
// Close the circle
4 OP_ROLL
OP_DUP
OP_ADD
OP_ADD
OP_SWAP
OP_2SWAP
OP_SWAP
}
}
/// Extracts (puts it at the top of the stack) the most significant bit of the u8 number and multiplies it by 2 modulo 256
pub fn u8_extract_1bit() -> Script {
script! {
OP_DUP
OP_ADD
256
OP_2DUP
OP_GREATERTHANOREQUAL
OP_IF
OP_SUB
1
OP_ELSE
OP_DROP
0
OP_ENDIF
}
}
/// Extracts (puts them at the top of the stack as the sum) the h most significant bits of the u8 number and multiplies it by 2^h modulo 256
pub fn u8_extract_hbit(hbit: usize) -> Script {
assert!((1..8).contains(&hbit));
if hbit == 1 {
return u8_extract_1bit();
}
let x: u32 = 1 << (hbit - 1);
script! {
0
OP_TOALTSTACK
for i in 0..hbit
{
128
OP_2DUP
OP_GREATERTHANOREQUAL
OP_IF
OP_SUB
OP_FROMALTSTACK
{ x >> i }
OP_ADD
OP_TOALTSTACK
OP_DUP
OP_ENDIF
OP_DROP
OP_DUP
OP_ADD
}
OP_FROMALTSTACK
}
}
/// Reorders (reverse and rotate) the bytes of an u32 number, assuming the starting order is 1 2 3 4 (4 being at the top):
/// if offset is 0, then reorder is 4 3 2 1
/// if offset is 1, then reorder is 1 4 3 2
/// if offset is 2, then reorder is 2 1 4 3
/// if offset is 3, then reorder is 3 2 1 4
pub fn byte_reorder(offset: usize) -> Script {
assert!((0..4).contains(&offset));
if offset == 0 {
script! {
OP_SWAP
OP_2SWAP
OP_SWAP
}
} else if offset == 1 {
return script! {
OP_SWAP
OP_ROT
};
} else if offset == 2 {
return script! {
OP_SWAP
OP_2SWAP
OP_SWAP
OP_2SWAP
};
} else /* if offset == 3 */ {
return script! {
OP_SWAP
OP_ROT
OP_2SWAP
};
}
}
/// Rotates the bits of a u32 number by rot_num
pub fn u32_rrot(rot_num: usize) -> Script {
assert!((0..32).contains(&rot_num));
let specific_optimize: Option<Script> = match rot_num {
0 => script! {}.into(), // 0
7 => script! {u32_rrot7}.into(), // 76
8 => script! {u32_rrot8}.into(), // 3
16 => script! {u32_rrot16}.into(), // 1
23 => script! {u32_rrot16 u32_rrot7}.into(),
24 => script! {3 OP_ROLL}.into(), // 2
_ => None,
};
if let Some(res) = specific_optimize {
return res;
}
let remainder: usize = rot_num % 8;
let hbit: usize = 8 - remainder;
let offset: usize = (rot_num - remainder) / 8;
script! {
{u8_extract_hbit(hbit)}
OP_ROT {u8_extract_hbit(hbit)}
4 OP_ROLL {u8_extract_hbit(hbit)}
6 OP_ROLL {u8_extract_hbit(hbit)}
7 OP_ROLL
OP_ADD
OP_TOALTSTACK
OP_ADD
OP_TOALTSTACK
OP_ADD
OP_TOALTSTACK
OP_ADD
OP_TOALTSTACK
OP_FROMALTSTACK
OP_FROMALTSTACK
OP_FROMALTSTACK
OP_FROMALTSTACK
{byte_reorder(offset)}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::u32::u32_std::*;
use rand::Rng;
fn rrot(x: u32, n: usize) -> u32 {
if n == 0 {
return x;
}
(x >> n) | (x << (32 - n))
}
#[test]
fn test_rrot() {
for i in 0..32 {
println!("u32_rrot({}): {} bytes", i, u32_rrot(i).len());
}
let mut rng = rand::thread_rng();
for _ in 0..1000 {
let x: u32 = rng.gen();
for i in 0..32 {
let script = script! {
{u32_push(x)}
{u32_rrot(i)}
{u32_push(rrot(x, i))}
{u32_equal()}
};
run(script);
}
}
}
#[test]
fn test_extract_hbit() {
for x in 0..256 {
for h in 1..8 {
let script = script! {
{ x }
{ u8_extract_hbit(h) }
{ x >> (8 - h) }
OP_EQUALVERIFY
{ (x << h) % 256 }
OP_EQUAL
};
run(script);
}
}
}
}