-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.rs
718 lines (604 loc) · 22.5 KB
/
test.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use crate::bigint::arithmetics::add::{
limb_add_carry, limb_doubling_initial_carry, limb_sub_carry,
};
use crate::bigint::implementation::NonNativeBigIntImpl;
use crate::bigint::window::NonNativeWindowedBigIntImpl;
use crate::bigint::{U254Windowed, U254_29x9Windowed, U508_29x18, U254, U508, U64};
use crate::traits::arithmeticable::Arithmeticable;
use crate::traits::comparable::Comparable;
use crate::traits::integer::{NonNativeInteger, NonNativeLimbInteger};
use crate::{debug::print_script_size, treepp::*};
use core::ops::{Add, Mul, Rem, Shl};
use konst::eq_str;
use num_bigint::{BigUint, RandomBits, ToBigUint};
use num_traits::One;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
use std::str::FromStr;
const LAZY_TESTS: bool = {
let lazy_tests = option_env!("LAZY_TESTS");
match lazy_tests {
None => true,
Some(lazy_tests) => match () {
_ if eq_str(lazy_tests, "true") => true,
_ if eq_str(lazy_tests, "false") => false,
_ => panic!("Please set LAZY_TESTS environment variable to true or false"),
},
}
};
const TESTS_NUM: usize = if LAZY_TESTS { 10 } else { 1000 };
#[test]
fn test_64_and_254_bit_add() {
print_script_size("254_bit_add", U254::OP_ADD(1, 0));
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let b: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = (a.clone() + b.clone()).rem(BigUint::one().shl(254));
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U254::OP_ADD(1, 0) }
{ U254::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U254::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
for _ in 0..TESTS_NUM {
let a: u64 = prng.gen();
let b: u64 = prng.gen();
let c = a.wrapping_add(b);
let script = script! {
{ U64::OP_PUSH_U64LESLICE(&[a]) }
{ U64::OP_PUSH_U64LESLICE(&[b]) }
{ U64::OP_ADD(1, 0) }
{ U64::OP_PUSH_U64LESLICE(&[c]) }
{ U64::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
#[test]
fn test_508_bit_add() {
#[allow(non_snake_case)]
print_script_size("508_bit_add", U508::OP_ADD(1, 0));
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(507));
let b: BigUint = prng.sample(RandomBits::new(507));
let c: BigUint = a.clone() + b.clone();
let script = script! {
{ U508::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U508::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U508::OP_ADD(1, 0) }
{ U508::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U508::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
#[test]
fn test_508_bit_add_no_overflow() {
#[allow(non_snake_case)]
print_script_size("508_bit_add_no_overflow", U508::OP_ADD_NOOVERFLOW(1, 0));
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(507));
let b: BigUint = prng.sample(RandomBits::new(507));
let c: BigUint = a.clone() + b.clone();
let script = script! {
{ U508::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U508::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U508::OP_ADD_NOOVERFLOW(1, 0) }
{ U508::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U508::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
#[test]
fn test_254_bit_sub() {
print_script_size("254_bit_sub", U254::OP_SUB(1, 0));
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let mut a: BigUint = prng.sample(RandomBits::new(254));
let mut b: BigUint = prng.sample(RandomBits::new(254));
if b > a {
std::mem::swap(&mut a, &mut b);
}
let c: BigUint = (a.clone() - b.clone()).rem(BigUint::one().shl(254));
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U254::OP_SUB(1, 0) }
{ U254::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U254::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
#[test]
fn test_64_and_254_bit_double() {
print_script_size("u254_double", U254::OP_2MUL(0));
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = (a.clone() + a.clone()).rem(BigUint::one().shl(254));
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254::OP_2MUL(0) }
{ U254::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U254::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
for _ in 0..TESTS_NUM {
let a: u64 = prng.gen();
let c = a.wrapping_add(a);
let script = script! {
{ U64::OP_PUSH_U64LESLICE(&[a]) }
{ U64::OP_2MUL(0) }
{ U64::OP_PUSH_U64LESLICE(&[c]) }
{ U64::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
#[test]
fn test_254_bit_double_no_overflow() {
#[allow(non_snake_case)]
print_script_size(
"u254_double_no_overflow",
U254::handle_OP_2MUL_NOOVERFLOW(0),
);
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(253));
let c: BigUint = a.clone() + a.clone();
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254::handle_OP_2MUL_NOOVERFLOW(0) }
{ U254::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U254::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
#[test]
fn test_16_mul() {
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = (16.to_biguint().unwrap() * a.clone()).rem(BigUint::one().shl(254));
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254::OP_2MUL(0) }
{ U254::OP_2MUL(0) }
{ U254::OP_2MUL(0) }
{ U254::OP_2MUL(0) }
{ U254::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U254::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
for _ in 0..TESTS_NUM {
let a: u64 = prng.gen();
let c = a.wrapping_add(a);
let script = script! {
{ U64::OP_PUSH_U64LESLICE(&[a]) }
{ U64::OP_2MUL(0) }
{ U64::OP_PUSH_U64LESLICE(&[c]) }
{ U64::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
#[test]
fn test_64_and_256_bit_2mul() {
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = (256.to_biguint().unwrap() * a.clone()).rem(BigUint::one().shl(254));
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
for _ in 0..8 {
{ U254::OP_2MUL(0) }
}
{ U254::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U254::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
for _ in 0..TESTS_NUM {
let a: u64 = prng.gen();
let c = a.wrapping_add(a);
let script = script! {
{ U64::OP_PUSH_U64LESLICE(&[a]) }
{ U64::OP_2MUL(0) }
{ U64::OP_PUSH_U64LESLICE(&[c]) }
{ U64::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
/// Tests the `limb_add_carry` method for adding two limbs.
///
/// It generates random 30-bit limbs and initializes a base of 2^30 and
/// then verifies the correctness of the output.
#[test]
fn test_limb_add_carry() {
const BASE: u32 = 1 << 30;
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
// Generate two limbs
let limb_1: u32 = prng.gen_range(0..1 << 30);
let limb_2: u32 = prng.gen_range(0..1 << 30);
let expected_carry = (limb_1 + limb_2 >= BASE) as u32;
let expected_sum = limb_1 + limb_2 - expected_carry * BASE;
let script = script! {
{ limb_1 }
{ limb_2 }
{ BASE }
{ limb_add_carry() }
{ expected_sum}
OP_EQUALVERIFY
{ expected_carry }
OP_EQUALVERIFY
{ BASE }
OP_EQUALVERIFY
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
/// Tests the `limb_double_carry` method for doubling the limb.
///
/// It generates a random 30-bit limb and initializes a base of 2^30 and
/// then verifies the correctness of the output.
#[test]
fn test_limb_doubling_initial_carry() {
const BASE: u32 = 1 << 30;
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
// Generate two limbs
let limb: u32 = prng.gen_range(0..1 << 30);
let expected_carry = (2 * limb >= BASE) as u32;
let expected_double = 2 * limb - expected_carry * BASE;
let script = script! {
{ limb }
{ BASE }
{ limb_doubling_initial_carry() }
{ expected_double }
OP_EQUALVERIFY
{ expected_carry }
OP_EQUALVERIFY
{ BASE }
OP_EQUALVERIFY
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
/// Tests the `limb_sub_carry` method for subtracting two limbs.
///
/// It generates random 30-bit limbs and initializes a base of 2^30 and
/// then verifies the correctness of the output.
#[test]
fn test_limb_sub_carry() {
const BASE: i32 = 1 << 30;
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
// Generate two limbs
let limb_1: i32 = prng.gen_range(0..1 << 30);
let limb_2: i32 = prng.gen_range(0..1 << 30);
let expected_carry = (limb_1 - limb_2 < 0) as i32;
let expected_sub = limb_1 - limb_2 + expected_carry * BASE;
let script = script! {
{ limb_1 }
{ limb_2 }
{ BASE }
{ limb_sub_carry() }
{ expected_sub}
OP_EQUALVERIFY
{ expected_carry }
OP_EQUALVERIFY
{ BASE }
OP_EQUALVERIFY
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
#[test]
fn test_64_and_254_bit_increment() {
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = (a.clone().add(BigUint::one())).rem(BigUint::one().shl(254));
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254::OP_ADD1() }
{ U254::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U254::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
for _ in 0..TESTS_NUM {
let a: u64 = prng.gen();
let c = a.wrapping_add(1u64);
let script = script! {
{ U64::OP_PUSH_U64LESLICE(&[a]) }
{ U64::OP_ADD1() }
{ U64::OP_PUSH_U64LESLICE(&[c]) }
{ U64::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
/// Tests the multiplication of two 254-bit numbers and two 64-bit numbers.
#[test]
fn test_64_and_254_bit_narrow_mul() {
print_script_size("254-bit narrow naive mul", U254::OP_MUL());
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let b: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = a.clone().mul(b.clone()).rem(BigUint::one().shl(254));
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U254::OP_MUL() }
{ U254::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U254::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(64));
let b: BigUint = prng.sample(RandomBits::new(64));
let c: BigUint = (a.clone().mul(b.clone())).rem(BigUint::one().shl(64));
let script = script! {
{ U64::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U64::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U64::OP_MUL() }
{ U64::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U64::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
/// Tests the widening multiplication of two 254-bit numbers using initial BitVM approach.
#[test]
fn test_254_bit_naive_widening_mul() {
print_script_size(
"254-bit widening naive mul",
U254::handle_OP_WIDENINGMUL::<U508>(),
);
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let b: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = a.clone() * b.clone();
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U254::OP_WIDENINGMUL::<U508>() }
{ U508::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U508::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
/// Tests the multiplication of two 254-bit numbers using w-width approach.
#[test]
fn test_254_bit_narrow_mul_w_width() {
const WIDTH: usize = 4;
type U254Windowed = NonNativeWindowedBigIntImpl<U254, WIDTH>;
print_script_size("254-bit w-width narrow mul", U254Windowed::OP_MUL());
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let b: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = a.clone().mul(b.clone()).rem(BigUint::one().shl(254));
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U254Windowed::OP_MUL() }
{ U254::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U254::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
// Tests the widening multiplication of two 254-bit integers to get a 508-bit one.
#[test]
fn test_254_bit_windowed_lazy_widening_mul() {
print_script_size(
"254-bit w-width lazy widening mul",
U254Windowed::handle_lazy_OP_WIDENINGMUL::<U508>(),
);
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(100));
let b: BigUint = prng.sample(RandomBits::new(100));
let c: BigUint = a.clone() * b.clone();
let script = script! {
{ U254Windowed::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254Windowed::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U254Windowed::handle_lazy_OP_WIDENINGMUL::<U508>() }
{ U508::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U508::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
/// Tests OP_PICKSTACK version when the top stack element is
/// larger than the ones that is being picked before it.
#[test]
fn test_op_pick_stack_and_add_different_bits() {
type U456 = NonNativeBigIntImpl<456, 30>;
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
// Generating 4 254-bit numbers and put 456-bit number in the front
let a1: BigUint = prng.sample(RandomBits::new(254));
let a2: BigUint = prng.sample(RandomBits::new(254));
let a3: BigUint = prng.sample(RandomBits::new(254));
let a4: BigUint = prng.sample(RandomBits::new(254));
let b: BigUint = prng.sample(RandomBits::new(456));
// Here, we:
// 1. Push a1, a2, a3, a4, b
// 2. Pick 4th element from the top (that is, a2)
// 3. Extending a2 to 456 bits
// 4. Pushing a2 again and verify that we indeed got a2 from picking
// 5. Dropping all remaining elements
let script = script! {
{ U254::OP_PUSH_U32LESLICE(&a1.to_u32_digits()) }
{ U254::OP_PUSH_U32LESLICE(&a2.to_u32_digits()) }
{ U254::OP_PUSH_U32LESLICE(&a3.to_u32_digits()) }
{ U254::OP_PUSH_U32LESLICE(&a4.to_u32_digits()) }
{ U456::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ 3 }
{ U254Windowed::handle_OP_PICKSTACK::<U456>() }
{ U254::OP_EXTEND::<U456>() }
{ U456::OP_PUSH_U32LESLICE(&a2.to_u32_digits()) }
{ U456::OP_EQUALVERIFY(1, 0) }
{ U456::OP_DROP() }
{ U254::OP_DROP() }
{ U254::OP_DROP() }
{ U254::OP_DROP() }
{ U254::OP_DROP() }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
/// Tests the intermediate step of the optimized multiplication which
/// multiplies by (1<<WIDTH) and adds precomputed 256-bit value to the
/// result.
#[test]
fn test_optimized_multiplication_step() {
type U268 = NonNativeBigIntImpl<268, 30>;
// UInt that is by 4 bits larger than the regular one.
// This is done due to the fact that we need to multiply
// by 16 on each step and therefore we need to allocate
// additional space before conducting operations
type U272 = NonNativeBigIntImpl<272, 30>;
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
// On each step we want to get 16*a + b, where a is initially
// 268 bits and later extended to 272 bits, and b is 256 bits.
let a: BigUint = prng.sample(RandomBits::new(268));
let b: BigUint = prng.sample(RandomBits::new(256));
let c: BigUint = a
.clone()
.mul(BigUint::from_str("16").unwrap())
.add(b.clone());
let script = script! {
{ U268::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U268::OP_EXTEND::<U272>() }
for _ in 0..4 {
{ U272::OP_2MUL_NOOVERFLOW(0) }
}
{ U268::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U254::OP_EXTEND::<U272>() }
{ U272::OP_ADD_NOOVERFLOW(0, 1) }
{ U272::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U272::OP_EQUALVERIFY(0, 1) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
/// Tests the multiplication of two 254-bit numbers using a
/// super optimized method.
#[test]
fn test_254_bit_windowed_widening_optimized_mul() {
print_script_size(
"254-bit optimized widening mul",
U254Windowed::OP_WIDENINGMUL::<U508>(),
);
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let b: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = a.clone() * b.clone();
let script = script! {
{ U254Windowed::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254Windowed::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U254Windowed::OP_WIDENINGMUL::<U508>() }
{ U508::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U508::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}
#[test]
fn test_254_bit_29_windowed_widening_optimized_mul() {
use super::u29x9::u29x9_mul_karazuba;
print_script_size("254-bit karatsuba widening mul", u29x9_mul_karazuba(1, 0));
print_script_size(
"254-bit 29 windowed widening mul",
U254_29x9Windowed::handle_optimized_OP_WIDENINGMUL(),
);
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..TESTS_NUM {
let a: BigUint = prng.sample(RandomBits::new(254));
let b: BigUint = prng.sample(RandomBits::new(254));
let c: BigUint = a.clone() * b.clone();
let script = script! {
{ U254_29x9Windowed::OP_PUSH_U32LESLICE(&a.to_u32_digits()) }
{ U254_29x9Windowed::OP_PUSH_U32LESLICE(&b.to_u32_digits()) }
{ U254_29x9Windowed::handle_optimized_OP_WIDENINGMUL() }
{ U508_29x18::OP_PUSH_U32LESLICE(&c.to_u32_digits()) }
{ U508_29x18::OP_EQUALVERIFY(1, 0) }
OP_TRUE
};
let exec_result = execute_script(script);
assert!(exec_result.success);
}
}