forked from BitVM/BitVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathu4_add.rs
More file actions
342 lines (314 loc) · 11 KB
/
u4_add.rs
File metadata and controls
342 lines (314 loc) · 11 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::treepp::*;
use bitcoin::opcodes::all::*;
use super::u4_std::{u4_drop, CalculateOffset};
/// Pushes the table for calculating the quotient, i.e. floor(x / 16) for x < 65. i.e. 15 (max u4) * 4 (max # numbers to sum) + 4 (max carry)
pub fn u4_push_quotient_table() -> Script {
script! {
OP_4
for i in (0..=3).rev() {
{ i }
OP_DUP
OP_2DUP
OP_3DUP
OP_3DUP
OP_3DUP
OP_3DUP
}
}
}
/// Pushes the table for calculating the quotient, i.e. floor(x / 16) for x < 80. i.e. 15 (max u4) * 5 (max # numbers to sum) + 4 (max carry)
pub fn u4_push_quotient_table_5() -> Script {
script! {
for i in (0..=4).rev() {
{ i }
OP_DUP
OP_2DUP
OP_3DUP
OP_3DUP
OP_3DUP
OP_3DUP
}
}
}
/// Drop quotient table
pub fn u4_drop_quotient_table() -> Script { u4_drop(65) }
/// Pushes the table for calculating the modulo, i.e. x % 16 for x < 65. i.e. 15 (max u4) * 4 (max # numbers to sum) + 4 (max carry)
pub fn u4_push_modulo_table() -> Script {
script! {
for i in (0..65).rev() {
{ i % 16 }
}
}
}
/// Pushes the table for calculating the modulo, i.e. x % 16 for x < 80. i.e. 15 (max u4) * 5 (max # numbers to sum) + 4 (max carry)
pub fn u4_push_modulo_table_5() -> Script {
script! {
for i in (0..80).rev() {
{ i % 16 }
}
}
}
/// Drops the modulo table
pub fn u4_drop_modulo_table() -> Script { u4_drop(65) }
/// Pushes both modulo and quotient tables for sums
pub fn u4_push_add_tables() -> Script {
script! {
{ u4_push_modulo_table() }
{ u4_push_quotient_table() }
}
}
/// Drops both modulo and quotient tables
pub fn u4_drop_add_tables() -> Script {
script! {
{ u4_drop_quotient_table() }
{ u4_drop_modulo_table() }
}
}
/// Arranges (zips) the given numbers (locations given by the parameters bases) each consisting of nibble_count u4's so each group of nibbles can be proccessed disceretly
/// Does not preserve order as it's used with commutative operations
/// Assuming x_i denoting the i-th part of the x-th number and bases have two numbers a and b (a < b):
/// Input: ... (a elements) a_0 a_1 a_2 a_3 ... (b - a - 1 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 elements and the rest of stack)
pub fn u4_arrange_nibbles(nibble_count: u32, mut bases: Vec<u32>) -> Script {
bases.sort();
bases.reverse();
for base_i in &mut bases {
*base_i += nibble_count - 1;
}
script! {
for i in 0..nibble_count {
for (n, base) in bases.iter().enumerate() {
{ (base - i) + ((n as u32 + 1) * (i + 1)) - 1 }
OP_ROLL
}
}
}
}
/// Calculates the modulo 16 of the u4 at the top of the stack also with the quotient, parameters being internal values
pub fn u4_add_carry_nested(current: u32, limit: u32) -> Script {
script! {
OP_DUP
OP_16
OP_GREATERTHANOREQUAL
OP_IF
OP_16
OP_SUB
if current + 1 == limit {
{ current }
} else {
{ u4_add_carry_nested(current+1, limit)}
}
OP_ELSE
{ current }
OP_ENDIF
}
}
/// Calculates the modulo 16 of the u4 at the top of the stack, parameters being internal values
pub fn u4_add_nested(current: u32, limit: u32) -> Script {
script! {
OP_DUP
OP_16
OP_GREATERTHANOREQUAL
OP_IF
OP_16
OP_SUB
if current + 1 < limit {
{ u4_add_nested(current + 1, limit)}
}
OP_ENDIF
}
}
/// Addition of zipped numbers consisting of nibble_count u4's, without the table
pub fn u4_add_no_table_internal(nibble_count: u32, number_count: u32) -> Script {
script! {
for i in 0..nibble_count {
for _ in 0..number_count-1 {
OP_ADD
}
if i < nibble_count - 1 {
{ u4_add_carry_nested(0, number_count) }
OP_SWAP
OP_TOALTSTACK
OP_ADD
} else {
{ u4_add_nested(0, number_count) }
OP_TOALTSTACK
}
}
}
}
/// Addition of zipped numbers consisting of nibble_count u4's
/// Requires the addition table and tables_offset to locate the table which should be equal to number of elements on top of the table including operating values
pub fn u4_add_internal(nibble_count: u32, number_count: u32, tables_offset: u32) -> Script {
assert!(number_count < 5);
let quotient_table_size = 65;
//extra size on the stack
let mut offset_calc: i32 = 0;
script! {
for i in 0..nibble_count {
//extra add to add the carry from previous addition
if i > 0 {
{ offset_calc.modify(OP_ADD) }
}
//add the column of nibbles (needs one less add than nibble count)
for _ in 0..number_count-1 {
{ offset_calc.modify(OP_ADD) }
}
// duplicate the result to be used to get the carry except for the last nibble
if i < nibble_count -1 {
{ offset_calc.modify( OP_DUP) }
}
//get the modulo of the addition
{ (offset_calc - 1) + tables_offset as i32 + quotient_table_size } // this adds 1 to the calc
OP_ADD // and this one consumes it
{ offset_calc.modify(OP_PICK) }
{ offset_calc.modify(OP_TOALTSTACK) }
//we don't care about the last carry
if i < nibble_count - 1 {
//obtain the quotinent to be used as carry for the next addition
{ (offset_calc - 1) + tables_offset as i32 }
OP_ADD
{ offset_calc.modify(OP_PICK) }
}
}
}
}
/// Addition of numbers consisting of nibble_count u4's in the parameter bases locations
/// Requires the addition table and tables_offset to locate the table which should be equal to number of elements on top of the table including operating values
pub fn u4_add_with_table(nibble_count: u32, bases: Vec<u32>, tables_offset: u32) -> Script {
let numbers = bases.len() as u32;
script! {
{ u4_arrange_nibbles(nibble_count, bases) }
{ u4_add_internal(nibble_count, numbers, tables_offset) }
}
}
/// Addition of numbers consisting of nibble_count u4's in the parameter bases locations, without the table
pub fn u4_add_no_table(nibble_count: u32, bases: Vec<u32>) -> Script {
let numbers = bases.len() as u32;
script! {
{ u4_arrange_nibbles(nibble_count, bases) }
{ u4_add_no_table_internal(nibble_count, numbers) }
}
}
/// Addition of numbers consisting of nibble_count u4's in the parameter bases locations
/// The overflowing bit (if exists) is omitted
pub fn u4_add(nibble_count: u32, bases: Vec<u32>, tables_offset: u32, use_add_table: bool) -> Script {
if use_add_table {
u4_add_with_table(nibble_count, bases, tables_offset)
} else {
u4_add_no_table(nibble_count, bases)
}
}
#[cfg(test)]
mod tests {
use crate::treepp::*;
use crate::u4::{u4_add::*, u4_std::u4_number_to_nibble};
use rand::Rng;
#[test]
fn test_calc() {
let x = u4_arrange_nibbles(8, vec![0, 1, 2, 4]);
println!("{}", x.len());
//let x = u4_add_with_table(8, vec![0, 8, 16, 24, 32], 100);
//println!("{}", x.len());
let x = u4_add_with_table(8, vec![0, 8, 16, 24], 100);
println!("{}", x.len());
let x = u4_add_no_table(8, vec![0, 8, 16, 24, 32]);
println!("{}", x.len());
let x = u4_add_no_table(8, vec![0, 8, 16, 24]);
println!("{}", x.len());
let x = u4_add_no_table(8, vec![0, 8, 16]);
println!("{}", x.len());
let x = u4_add_no_table(8, vec![0, 8]);
println!("{}", x.len());
}
#[test]
fn test_add_no_table() {
let calc = script! {
{ u4_add_no_table(8, vec![0, 8, 16, 24]) }
};
let mut rng = rand::thread_rng();
for _ in 0..1000 {
for len in 2..5 {
let vars: Vec<u32> = (0..len).map(|_| { rng.gen()}).collect();
let result = vars.iter().fold(0 as u64, |sum, &x| sum + x as u64) % ((1 as u64) << 32);
let script = script! {
for x in vars {
{ u4_number_to_nibble(x) }
}
{ u4_add_no_table(8, (0..).step_by(8).take(len.try_into().unwrap()).collect()) }
{ u4_number_to_nibble(result.try_into().unwrap()) }
for _ in 0..8 {
OP_FROMALTSTACK
}
for i in 0..8 {
{ 8 - i }
OP_ROLL
OP_EQUALVERIFY
}
OP_TRUE
};
run(script);
}
}
println!("{}", calc.len());
}
#[test]
fn test_add_with_table() {
let mut rng = rand::thread_rng();
for _ in 0..1000 {
for len in 2..5 {
let vars: Vec<u32> = (0..len).map(|_| { rng.gen()}).collect();
let result = vars.iter().fold(0 as u64, |sum, &x| sum + x as u64) % ((1 as u64) << 32);
let script = script! {
{ u4_push_add_tables() }
for x in vars {
{ u4_number_to_nibble(x) }
}
{ u4_add_with_table(8, (0..).step_by(8).take(len.try_into().unwrap()).collect(), len * 8) }
{ u4_drop_add_tables() }
{ u4_number_to_nibble(result.try_into().unwrap()) }
for _ in 0..8 {
OP_FROMALTSTACK
}
for i in 0..8 {
{ 8 - i }
OP_ROLL
OP_EQUALVERIFY
}
OP_TRUE
};
run(script);
}
}
}
#[test]
fn test_quotient() {
for i in 0..65 {
let script = script! {
{ u4_push_quotient_table() }
{ i as u32 }
OP_PICK
{ i / 16 }
OP_EQUALVERIFY
{ u4_drop_quotient_table() }
OP_TRUE
};
run(script);
}
}
#[test]
fn test_modulo() {
for i in 0..65 {
let script = script! {
{ u4_push_modulo_table() }
{ i as u32 }
OP_PICK
{ i % 16 }
OP_EQUALVERIFY
{ u4_drop_modulo_table() }
OP_TRUE
};
run(script);
}
}
}