-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathint_mul_windowed.rs
466 lines (370 loc) · 17.1 KB
/
int_mul_windowed.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! This module contains the test script
//! for performing the multiplication of two large integers
//! (exceeding standard Bitcoin 31-bit integers)
use bitcoin_splitter::split::script::{IOPair, SplitableScript};
use bitcoin_utils::treepp::*;
use bitcoin_window_mul::{
bigint::{implementation::NonNativeBigIntImpl, window::NonNativeWindowedBigIntImpl},
traits::integer::{NonNativeInteger, NonNativeLimbInteger},
};
use num_bigint::{BigUint, RandomBits};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
// TODO(@ZamDimon): Use typenum to enforce DOUBLE_N_BITS = 2 * N_BITS
/// Script that performs the addition of two BigInt integers
/// Make sure the second argument is double the size of the first argument
pub struct BigIntWideningMulScript<const N_BITS: usize, const DOUBLE_N_BITS: usize>;
/// Type alias for U254 windowed multiplication
pub type U254MulScript = BigIntWideningMulScript<254, 508>;
/// Type alias for U32 windowed multiplication
pub type U32MulScript = BigIntWideningMulScript<32, 64>;
/// The limb size used to represent an integer. In practice,
/// 29 is the most reliable limb size.
const LIMB_SIZE: usize = 29;
/// Window size. In practice, 4 is the most reliable window size.
const WINDOW_SIZE: usize = 4;
impl<const N_BITS: usize, const DOUBLE_N_BITS: usize>
BigIntWideningMulScript<N_BITS, DOUBLE_N_BITS>
{
/// Limb size in bits to represent an integer
pub const LIMB_SIZE: usize = LIMB_SIZE;
/// Window size for the windowed multiplication
pub const WINDOW_SIZE: usize = WINDOW_SIZE;
}
impl<const N_BITS: usize, const DOUBLE_N_BITS: usize> SplitableScript
for BigIntWideningMulScript<N_BITS, DOUBLE_N_BITS>
{
/// Input size is double the number of limbs of BigInteger since we are multiplying two numbers
const INPUT_SIZE: usize = 2 * usize::div_ceil(N_BITS, Self::LIMB_SIZE);
/// Output size is the number of limbs of an integer with double the bitsize
const OUTPUT_SIZE: usize = usize::div_ceil(2 * N_BITS, Self::LIMB_SIZE);
fn script() -> Script {
// NOTE: Construction below is super weird, but it is the only way to make it work
NonNativeWindowedBigIntImpl::<NonNativeBigIntImpl::<N_BITS, LIMB_SIZE>, WINDOW_SIZE>::OP_WIDENINGMUL::<NonNativeWindowedBigIntImpl::<NonNativeBigIntImpl::<DOUBLE_N_BITS, LIMB_SIZE>, WINDOW_SIZE>>()
}
fn generate_valid_io_pair() -> IOPair {
let mut prng = ChaCha20Rng::seed_from_u64(0);
// Generate two random 254-bit numbers and calculate their sum
let num_1: BigUint = prng.sample(RandomBits::new(N_BITS as u64));
let num_2: BigUint = prng.sample(RandomBits::new(N_BITS as u64));
let product: BigUint = num_1.clone() * num_2.clone();
IOPair {
input: script! {
{ NonNativeBigIntImpl::<N_BITS, LIMB_SIZE>::OP_PUSH_U32LESLICE(&num_1.to_u32_digits()) }
{ NonNativeBigIntImpl::<N_BITS, LIMB_SIZE>::OP_PUSH_U32LESLICE(&num_2.to_u32_digits()) }
},
output: NonNativeBigIntImpl::<DOUBLE_N_BITS, LIMB_SIZE>::OP_PUSH_U32LESLICE(
&product.to_u32_digits(),
),
}
}
fn generate_invalid_io_pair() -> IOPair {
let mut prng = ChaCha20Rng::seed_from_u64(0);
// Generate two random 254-bit numbers and calculate their sum
let num_1: BigUint = prng.sample(RandomBits::new(N_BITS as u64));
let num_2: BigUint = prng.sample(RandomBits::new(N_BITS as u64));
let mut product: BigUint = num_1.clone() * num_2.clone();
// Flip a random bit in the product
let bit_to_flip = prng.gen_range(0..product.bits());
product.set_bit(bit_to_flip, !product.bit(bit_to_flip));
IOPair {
input: script! {
{ NonNativeBigIntImpl::<N_BITS, LIMB_SIZE>::OP_PUSH_U32LESLICE(&num_1.to_u32_digits()) }
{ NonNativeBigIntImpl::<N_BITS, LIMB_SIZE>::OP_PUSH_U32LESLICE(&num_2.to_u32_digits()) }
},
output: NonNativeBigIntImpl::<DOUBLE_N_BITS, LIMB_SIZE>::OP_PUSH_U32LESLICE(
&product.to_u32_digits(),
),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use bitcoin_splitter::split::core::SplitType;
use bitcoin_utils::{comparison::OP_LONGEQUALVERIFY, stack_to_script};
use bitcoin_window_mul::{bigint::U64, traits::comparable::Comparable};
#[test]
fn test_u254_verify() {
assert!(U254MulScript::verify_random());
}
#[test]
fn test_u254_invalid_generate() {
let IOPair { input, output } = U254MulScript::generate_invalid_io_pair();
assert!(
!U254MulScript::verify(input.clone(), output.clone()),
"input/output is correct"
);
}
#[test]
fn test_u254_naive_split_correctness() {
// Generating a random valid input for the script and the script itself
let IOPair { input, output } = U254MulScript::generate_valid_io_pair();
assert!(
U254MulScript::verify(input.clone(), output.clone()),
"input/output is not correct"
);
// Splitting the script into shards
let split_result = U254MulScript::default_split(input.clone(), SplitType::ByInstructions);
// Now, we are going to concatenate all the shards and verify that the script is also correct
let verification_script = script! {
{ input }
for shard in split_result.shards {
{ shard }
}
{ output }
// Now, we need to verify that the output is correct.
{ OP_LONGEQUALVERIFY(U254MulScript::OUTPUT_SIZE) }
OP_TRUE
};
let result = execute_script(verification_script);
assert!(result.success, "Verification has failed");
}
#[test]
fn test_u254_naive_split() {
// First, we generate the pair of input and output scripts
let IOPair { input, output } = U254MulScript::generate_valid_io_pair();
// Splitting the script into shards
let split_result = U254MulScript::default_split(input, SplitType::ByInstructions);
for shard in split_result.shards.iter() {
println!("Shard: {:?}", shard.len());
}
// Debugging the split result
println!("Split result: {:?}", split_result);
// Checking the last state (which must be equal to the result of the multiplication)
let last_state = split_result.must_last_state();
// Altstack must be empty
assert!(last_state.altstack.is_empty(), "altstack is not empty!");
// The element of the mainstack must be equal to the actual output
let verification_script = script! {
{ stack_to_script(&last_state.stack) }
{ output }
{ NonNativeBigIntImpl::<508, LIMB_SIZE>::OP_EQUAL(0, 1) }
};
let result = execute_script(verification_script);
assert!(result.success, "verification has failed");
// Printing
for (i, state) in split_result.intermediate_states.iter().enumerate() {
println!(
"Intermediate state #{}: {:?}",
i,
state.stack.len() + state.altstack.len()
);
}
// Now, we debug the total size of the states
let total_size = split_result.total_states_size();
println!("Total size of the states: {} bytes", total_size);
}
#[test]
fn test_u254_split_each_shard() {
// First, we generate the pair of input and output scripts
let IOPair { input, output: _ } = U254MulScript::generate_valid_io_pair();
// Splitting the script into shards
let split_result = U254MulScript::default_split(input.clone(), SplitType::ByInstructions);
for i in 0..split_result.len() {
// Forming first two inputs. Note that the first input is the input script itself
// while the second input is the output of the previous shard
let mut first_input = input.clone();
if i > 0 {
first_input = split_result.intermediate_states[i - 1].inject_script();
}
let second_input = split_result.intermediate_states[i].inject_script();
// Forming the function
let function = split_result.shards[i].clone();
let verification_script = script! {
{ second_input }
{ first_input }
{ function }
// Verifying that the output in mainstack is correct
{ OP_LONGEQUALVERIFY(split_result.intermediate_states[i].stack.len()) }
// Verifying that the output in altstack is correct
// Pushing elements to the mainstack
for _ in 0..2*split_result.intermediate_states[i].altstack.len() {
OP_FROMALTSTACK
}
// Verifying that altstack elements are correct
{ OP_LONGEQUALVERIFY(split_result.intermediate_states[i].altstack.len()) }
OP_TRUE
};
let result = execute_script(verification_script);
assert!(result.success, "verification has failed");
}
}
#[test]
fn test_u254_split_to_u32() {
// First, we generate the pair of input and output scripts
let IOPair { input, output: _ } = U254MulScript::generate_valid_io_pair();
// Splitting the script into shards
let split_result = U254MulScript::default_split(input.clone(), SplitType::ByInstructions);
for i in 0..split_result.len() {
// Forming first two inputs. Note that the first input is the input script itself
// while the second input is the output of the previous shard
let mut first_input = input.clone();
if i > 0 {
first_input = split_result.intermediate_states[i - 1]
.to_bytes()
.inject_script();
}
let second_input = split_result.intermediate_states[i]
.to_bytes()
.inject_script();
// Forming the function
let function = split_result.shards[i].clone();
let verification_script = script! {
{ second_input }
{ first_input }
{ function }
// Verifying that the output in mainstack is correct
{ OP_LONGEQUALVERIFY(split_result.intermediate_states[i].stack.len()) }
// Verifying that the output in altstack is correct
// Pushing elements to the mainstack
for _ in 0..2*split_result.intermediate_states[i].altstack.len() {
OP_FROMALTSTACK
}
// Verifying that altstack elements are correct
{ OP_LONGEQUALVERIFY(split_result.intermediate_states[i].altstack.len()) }
OP_TRUE
};
let result = execute_script(verification_script);
assert!(result.success, "verification has failed");
}
}
#[test]
#[ignore = "too-large computation, run separately"]
fn test_u254_fuzzy_split() {
// First, we generate the pair of input and output scripts
let IOPair { input, output } = U254MulScript::generate_valid_io_pair();
// Splitting the script into shards
let split_result = U254MulScript::fuzzy_split(input, SplitType::ByInstructions);
for shard in split_result.shards.iter() {
println!("Shard: {:?}", shard.len());
}
// Debugging the split result
println!("Split result: {:?}", split_result);
// Checking the last state (which must be equal to the result of the multiplication)
let last_state = split_result.must_last_state();
// Altstack must be empty
assert!(last_state.altstack.is_empty(), "altstack is not empty!");
// The element of the mainstack must be equal to the actual output
let verification_script = script! {
{ stack_to_script(&last_state.stack) }
{ output }
{ NonNativeBigIntImpl::<508, LIMB_SIZE>::OP_EQUAL(0, 1) }
};
let result = execute_script(verification_script);
assert!(result.success, "verification has failed");
// Printing
for (i, state) in split_result.intermediate_states.iter().enumerate() {
println!(
"Intermediate state #{}: {:?}",
i,
state.stack.len() + state.altstack.len()
);
}
// Now, we debug the total size of the states
let total_size = split_result.total_states_size();
println!("Total size of the states: {} bytes", total_size);
}
#[test]
fn test_u32_verify() {
assert!(U32MulScript::verify_random());
}
#[test]
fn test_u32_naive_split_correctness() {
// Generating a random valid input for the script and the script itself
let IOPair { input, output } = U32MulScript::generate_valid_io_pair();
assert!(
U32MulScript::verify(input.clone(), output.clone()),
"input/output is not correct"
);
// Splitting the script into shards
let split_result = U32MulScript::default_split(input.clone(), SplitType::ByInstructions);
// Now, we are going to concatenate all the shards and verify that the script is also correct
let verification_script = script! {
{ input }
for shard in split_result.shards {
{ shard }
}
{ output }
// Now, we need to verify that the output is correct.
{ OP_LONGEQUALVERIFY(U32MulScript::OUTPUT_SIZE) }
OP_TRUE
};
let result = execute_script(verification_script);
assert!(result.success, "Verification has failed");
}
#[test]
fn test_u32_naive_split() {
const SPLIT_SIZE: usize = 590;
// Printing the size of the script
println!("Size of the script: {} bytes", U32MulScript::script().len());
// First, we generate the pair of input and output scripts
let IOPair { input, output } = U32MulScript::generate_valid_io_pair();
// Splitting the script into shards
let split_result = U32MulScript::split(input, SplitType::ByInstructions, SPLIT_SIZE);
for shard in split_result.shards.iter() {
println!("Shard: {:?}", shard.len());
}
// Debugging the split result
println!("Split result: {:?}", split_result);
// Checking the last state (which must be equal to the result of the multiplication)
let last_state = split_result.must_last_state();
// Altstack must be empty
assert!(last_state.altstack.is_empty(), "altstack is not empty!");
// The element of the mainstack must be equal to the actual output
let verification_script = script! {
{ stack_to_script(&last_state.stack) }
{ output }
{ NonNativeBigIntImpl::<64, LIMB_SIZE>::OP_EQUAL(0, 1) }
};
let result = execute_script(verification_script);
assert!(result.success, "verification has failed");
// Printing
for (i, state) in split_result.intermediate_states.iter().enumerate() {
println!(
"Intermediate state #{}: {:?}",
i,
state.stack.len() + state.altstack.len()
);
}
// Now, we debug the total size of the states
let total_size = split_result.total_states_size();
println!("Total size of the states: {} bytes", total_size);
}
#[test]
#[ignore = "too-large computation, run separately"]
fn test_u32_fuzzy_split() {
// First, we generate the pair of input and output scripts
let IOPair { input, output } = U32MulScript::generate_valid_io_pair();
// Splitting the script into shards
let split_result = U32MulScript::fuzzy_split(input, SplitType::ByInstructions);
for shard in split_result.shards.iter() {
println!("Shard: {:?}", shard.len());
}
// Debugging the split result
println!("Split result: {:?}", split_result);
// Checking the last state (which must be equal to the result of the multiplication)
let last_state = split_result.must_last_state();
// Altstack must be empty
assert!(last_state.altstack.is_empty(), "altstack is not empty!");
// The element of the mainstack must be equal to the actual output
let verification_script = script! {
{ stack_to_script(&last_state.stack) }
{ output }
{ U64::OP_EQUAL(0, 1) }
};
let result = execute_script(verification_script);
assert!(result.success, "verification has failed");
// Printing
for (i, state) in split_result.intermediate_states.iter().enumerate() {
println!(
"Intermediate state #{}: {:?}",
i,
state.stack.len() + state.altstack.len()
);
}
// Now, we debug the total size of the states
let total_size = split_result.total_states_size();
println!("Total size of the states: {} bytes", total_size);
}
}