forked from BitVM/BitVM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstd.rs
1084 lines (952 loc) · 33.9 KB
/
std.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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use bitcoin::script::read_scriptint;
use num_bigint::BigUint;
use num_traits::Num;
use std::cmp::Ordering;
use std::str::FromStr;
use crate::bigint::BigIntImpl;
use crate::pseudo::{push_to_stack, NMUL};
use crate::treepp::*;
/// Struct to store the information of each step in `transform_limbsize` function.
/// ## Fields:
/// - current_limb_remaining_bits: the number of bits left in the current source limb that is being processed.
/// - extract_window: the number of bits to extract from the current limb.
/// - drop_currentlimb: signals to drop the current limb and bring another from altstack.
/// - initiate_targetlimb: signals to start a new target limb.
#[derive(Debug)]
struct TransformStep {
current_limb_remaining_bits: u32,
extract_window: u32,
drop_currentlimb: bool,
initiate_targetlimb: bool,
}
impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
pub fn push_u32_le(v: &[u32]) -> Script {
let mut bits = vec![];
for elem in v.iter() {
for i in 0..32 {
bits.push((elem & (1 << i)) != 0);
}
}
bits.resize(N_BITS as usize, false);
let mut limbs = vec![];
for chunk in bits.chunks(LIMB_SIZE as usize) {
let mut chunk_vec = chunk.to_vec();
chunk_vec.resize(LIMB_SIZE as usize, false);
let mut elem = 0u32;
for (i, chunk_i) in chunk_vec.iter().enumerate() {
if *chunk_i {
elem += 1 << i;
}
}
limbs.push(elem);
}
limbs.reverse();
script! {
for limb in &limbs {
{ *limb }
}
{ push_to_stack(0,Self::N_LIMBS as usize - limbs.len()) }
}
}
pub fn read_u32_le(mut witness: Vec<Vec<u8>>) -> Vec<u32> {
assert_eq!(witness.len() as u32, Self::N_LIMBS);
witness.reverse();
let mut bits: Vec<bool> = vec![];
for element in witness.iter() {
let limb = read_scriptint(element).unwrap();
for i in 0..LIMB_SIZE {
bits.push((limb & (1 << i)) != 0);
}
}
bits.resize(N_BITS as usize, false);
let mut u32s = vec![];
for chunk in bits.chunks(32) {
let mut chunk_vec = chunk.to_vec();
chunk_vec.resize(32, false);
let mut elem = 0u32;
for i in 0..32_usize {
if chunk_vec[i] {
elem += 1 << i;
}
}
u32s.push(elem);
}
u32s
}
pub fn push_u64_le(v: &[u64]) -> Script {
let v = v
.iter()
.flat_map(|v| {
[
(v & 0xffffffffu64) as u32,
((v >> 32) & 0xffffffffu64) as u32,
]
})
.collect::<Vec<u32>>();
Self::push_u32_le(&v)
}
/// Zip the top two u{16N} elements
/// input: a0 ... a{N-1} b0 ... b{N-1}
/// output: a0 b0 ... ... a{N-1} b{N-1}
pub fn zip(mut a: u32, mut b: u32) -> Script {
a = (a + 1) * Self::N_LIMBS - 1;
b = (b + 1) * Self::N_LIMBS - 1;
assert_ne!(a, b);
if a < b {
script! {
for i in 0..Self::N_LIMBS {
{ a + i }
OP_ROLL
{ b }
OP_ROLL
}
}
} else {
script! {
for i in 0..Self::N_LIMBS {
{ a }
OP_ROLL
{ b + i + 1 }
OP_ROLL
}
}
}
}
pub fn copy_zip(mut a: u32, mut b: u32) -> Script {
a = (a + 1) * Self::N_LIMBS - 1;
b = (b + 1) * Self::N_LIMBS - 1;
script! {
for i in 0..Self::N_LIMBS {
{ a + i } OP_PICK { b + 1 + i } OP_PICK
}
}
}
pub fn dup_zip(mut a: u32) -> Script {
a = (a + 1) * Self::N_LIMBS - 1;
script! {
for i in 0..Self::N_LIMBS {
{ a + i } OP_ROLL OP_DUP
}
}
}
pub fn copy(mut a: u32) -> Script {
a = (a + 1) * Self::N_LIMBS - 1;
script! {
if a < 134 {
for _ in 0..Self::N_LIMBS {
{ a } OP_PICK
}
} else {
{ a + 1 }
for _ in 0..Self::N_LIMBS - 1 {
OP_DUP OP_PICK OP_SWAP
}
OP_1SUB OP_PICK
}
}
}
pub fn roll(mut a: u32) -> Script {
if a == 0 {
return script! {};
}
a = (a + 1) * Self::N_LIMBS - 1;
script! {
for _ in 0..Self::N_LIMBS {
{ a } OP_ROLL
}
}
}
pub fn drop() -> Script {
script! {
for _ in 0..Self::N_LIMBS / 2 {
OP_2DROP
}
if Self::N_LIMBS & 1 == 1 {
OP_DROP
}
}
}
pub fn push_dec(dec_string: &str) -> Script {
Self::push_u32_le(&BigUint::from_str(dec_string).unwrap().to_u32_digits())
}
pub fn push_hex(hex_string: &str) -> Script {
Self::push_u32_le(
&BigUint::from_str_radix(hex_string, 16)
.unwrap()
.to_u32_digits(),
)
}
#[inline]
pub fn push_zero() -> Script {
push_to_stack(0, Self::N_LIMBS as usize)
}
#[inline]
pub fn push_one() -> Script {
script! {
{ push_to_stack(0,(Self::N_LIMBS - 1) as usize) }
1
}
}
pub fn is_zero_keep_element(a: u32) -> Script {
let a = Self::N_LIMBS * a;
script! {
1
for i in 0..Self::N_LIMBS {
{ a + i+1 } OP_PICK
OP_NOT
OP_BOOLAND
}
}
}
pub fn is_zero(a: u32) -> Script {
let a = Self::N_LIMBS * a;
script! {
1
for _ in 0..Self::N_LIMBS {
{ a +1 } OP_ROLL
OP_NOT
OP_BOOLAND
}
}
}
pub fn is_one_keep_element(a: u32) -> Script {
let a = Self::N_LIMBS * a;
script! {
1
{ a + 1 } OP_PICK
1 OP_EQUAL OP_BOOLAND
for i in 1..Self::N_LIMBS {
{ a + i + 1 } OP_PICK
OP_NOT
OP_BOOLAND
}
}
}
pub fn is_one(a: u32) -> Script {
let a = Self::N_LIMBS * a;
script! {
1
{ a + 1 } OP_ROLL
1 OP_EQUAL OP_BOOLAND
for _ in 1..Self::N_LIMBS {
{ a + 1 } OP_ROLL
OP_NOT
OP_BOOLAND
}
}
}
pub fn toaltstack() -> Script {
script! {
for _ in 0..Self::N_LIMBS {
OP_TOALTSTACK
}
}
}
pub fn fromaltstack() -> Script {
script! {
for _ in 0..Self::N_LIMBS {
OP_FROMALTSTACK
}
}
}
pub fn is_negative(depth: u32) -> Script {
script! {
{ (1 + depth) * Self::N_LIMBS - 1 } OP_PICK
{ Self::HEAD_OFFSET >> 1 }
OP_GREATERTHANOREQUAL
}
}
pub fn is_positive(depth: u32) -> Script {
script! {
{ Self::is_zero_keep_element(depth) } OP_NOT
{ (1 + depth) * Self::N_LIMBS } OP_PICK
{ Self::HEAD_OFFSET >> 1 }
OP_LESSTHAN OP_BOOLAND
}
}
/// Resize positive numbers
///
/// # Note
///
/// Does not work for negative numbers
pub fn resize<const T_BITS: u32>() -> Script {
let n_limbs_self = N_BITS.div_ceil(LIMB_SIZE);
let n_limbs_target = T_BITS.div_ceil(LIMB_SIZE);
match n_limbs_target.cmp(&n_limbs_self) {
Ordering::Equal => script! {},
Ordering::Greater => {
let n_limbs_to_add = n_limbs_target - n_limbs_self;
script! {
if n_limbs_to_add > 0 {
{0} {crate::pseudo::OP_NDUP((n_limbs_to_add - 1) as usize)} // Pushing zeros to the stack
}
for _ in 0..n_limbs_self {
{ n_limbs_target - 1 } OP_ROLL
}
}
}
Ordering::Less => {
let n_limbs_to_remove = n_limbs_self - n_limbs_target;
script! {
for _ in 0..n_limbs_to_remove {
{ n_limbs_target } OP_ROLL OP_DROP
}
}
}
}
}
/// Generates a vector of TransformStep struct that encodes all the information needed to
/// convert BigInt form one limbsize represention (source) to another (target).
/// used as a helper function for `transform_limbsize`
fn get_transform_steps(source_limb_size: u32, target_limb_size: u32) -> Vec<TransformStep> {
//define an empty vector to store Transform steps
let mut transform_steps: Vec<TransformStep> = Vec::new();
// compute the number of limbs for target and source
let target_n_limbs = N_BITS.div_ceil(target_limb_size);
let mut target_limb_remaining_bits = Self::N_BITS - (target_n_limbs - 1) * target_limb_size;
let source_n_limbs = N_BITS.div_ceil(source_limb_size);
let source_head = Self::N_BITS - (source_n_limbs - 1) * source_limb_size;
// define a vector of limbsizes of source
let mut limb_sizes: Vec<u32> = Vec::with_capacity(source_n_limbs as usize);
let mut first_iter_flag = true;
for _ in 0..(source_n_limbs - 1) {
limb_sizes.push(source_limb_size);
}
limb_sizes.push(source_head);
//iterate until all limbs of source are processed
while !limb_sizes.is_empty() {
//iterate until the target limb is filled completely
while target_limb_remaining_bits > 0 {
let source_limb_last_idx = limb_sizes.len() - 1;
let source_limb_remaining_bits = limb_sizes[source_limb_last_idx];
match source_limb_remaining_bits.cmp(&target_limb_remaining_bits) {
Ordering::Less => {
transform_steps.push(TransformStep {
current_limb_remaining_bits: source_limb_remaining_bits,
extract_window: source_limb_remaining_bits,
drop_currentlimb: true,
initiate_targetlimb: first_iter_flag,
});
target_limb_remaining_bits -= source_limb_remaining_bits;
limb_sizes.pop();
}
Ordering::Equal => {
transform_steps.push(TransformStep {
current_limb_remaining_bits: source_limb_remaining_bits,
extract_window: target_limb_remaining_bits,
drop_currentlimb: true,
initiate_targetlimb: first_iter_flag,
});
target_limb_remaining_bits = 0;
limb_sizes.pop();
}
Ordering::Greater => {
transform_steps.push(TransformStep {
current_limb_remaining_bits: source_limb_remaining_bits,
extract_window: target_limb_remaining_bits,
drop_currentlimb: false,
initiate_targetlimb: first_iter_flag,
});
limb_sizes[source_limb_last_idx] =
source_limb_remaining_bits - target_limb_remaining_bits;
target_limb_remaining_bits = 0;
}
}
first_iter_flag = false;
}
target_limb_remaining_bits = target_limb_size;
first_iter_flag = true;
}
transform_steps
}
/// Transform Limbsize for BigInt
/// This function changes the representation of BigInt present on stack as multiple limbs of source limbsize to
/// any another limbsize within 1 and 31 (inclusive).
/// Specifically, This can be used to transform limbs into nibbles, limbs into bits ans vice-versa to aid optimizetions.
///
/// ## Assumptions:
/// - Does NOT do input validation.
/// - The message is placed such that LSB is on top of stack. (MSB pushed first)
///
/// ## Stack Effects:
/// The original BigInt which that was in stack is dropped
/// The same BigInt with target_limbsize is left on stack
///
/// ## Panics:
/// - If the source or target limb size lies outside of 0 to 31 (inclusive), fails with assertion error.
/// - If the source or target limb size is greater than number of bits, fails with assertion error.
/// - If the elements do not fit on the stack. (few satck elements are also used for intermediate computation).
/// - The number of bits in the BigInt must be 32 or larger.
pub fn transform_limbsize(source_limb_size: u32, target_limb_size: u32) -> Script {
// ensure that source and target limb sizes are between 0 and 31 inclusive
assert!(
source_limb_size < 32 && source_limb_size > 0,
"source limb size must lie between 1 and 31 inclusive"
);
assert!(
target_limb_size < 32 && target_limb_size > 0,
"target limb size must lie between 1 and 31 inclusive"
);
//ensure that source and target limb size aren't greater than N_BITS
assert!(
source_limb_size <= Self::N_BITS,
"source limb size mustn't be greater than number of bits in bigInt"
);
assert!(
target_limb_size <= Self::N_BITS,
"target limb size mustn't be greater than number of bits in bigInt"
);
//ensure that the N_BITS are larger than or equal to 32
assert!(
Self::N_BITS >= 32,
"The number of bits in BigInt must be atleast 32"
);
// if both source and target limb size are same, do nothing
if source_limb_size == target_limb_size {
script!()
} else {
let steps = Self::get_transform_steps(source_limb_size, target_limb_size);
let source_n_limbs = N_BITS.div_ceil(source_limb_size);
script!(
// send all limbs except the first to alt stack so that the MSB is handled first
for _ in 0..(source_n_limbs - 1){OP_TOALTSTACK}
for (index, step) in steps.iter().enumerate() {
{extract_digits(step.current_limb_remaining_bits, step.extract_window)}
if !step.initiate_targetlimb{
// add
OP_ROT
for _ in 0..step.extract_window {OP_DUP OP_ADD}
OP_ROT
OP_ADD
OP_SWAP
}
if step.drop_currentlimb{
OP_DROP
//except when its the last limb, we pull a new limb from altstack
if index != (steps.len() - 1){
OP_FROMALTSTACK
}
}
}
)
}
}
}
/// Extracts a window of bits from a u32 limb on top of stack
///
/// ## Assumptions;
/// Doesn't do input validation
/// All the bits before start_index must be 0 for the extract to work properly
///
/// ## Panics:
/// - If the start_index is not between the range 1 and 31 (inclusive), fails with assertion error
/// - If the window is larger than the start_index, fails with assertion error
///
/// ## Stack behaviour:
/// - extracts the desired window as a stack element
/// - leaves the original limb with extracted bits set to zero on top of stack
pub fn extract_digits(start_index: u32, window: u32) -> Script {
// doesnot work if start_index is 32
assert!(
start_index < 32 && start_index > 0,
"start_index must lie between 1 and 31 (inclusive)"
);
//panics if the window exceeds the number of bits on the left of start_index
assert!(
start_index >= window,
"not enough bits left of start_index to fill the window!"
);
script! {
0
OP_SWAP
for i in 0..window {
OP_TUCK
{ 1 << (start_index - i - 1) }
OP_GREATERTHANOREQUAL
OP_TUCK
OP_ADD
if i < window - 1 { { NMUL(2) } }
OP_ROT OP_ROT
OP_IF
{ 1 << (start_index - i - 1) }
OP_SUB
OP_ENDIF
}
}
}
#[cfg(test)]
mod test {
use crate::bigint::std::extract_digits;
use crate::bigint::{BigIntImpl, U254};
use crate::run;
use bitcoin_script::script;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
#[test]
fn test_zip() {
const N_BITS: u32 = 1450;
const N_U30_LIMBS: u32 = 50;
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..50 {
let mut v = vec![];
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
let mut expected = vec![];
for i in 0..N_U30_LIMBS {
expected.push(v[i as usize]);
expected.push(v[(N_U30_LIMBS + i) as usize]);
}
let script = script! {
for i in 0..N_U30_LIMBS * 2 {
{ v[i as usize] }
}
{ BigIntImpl::<N_BITS, 29>::zip(1, 0) }
for i in 0..N_U30_LIMBS * 2 {
{ expected[(N_U30_LIMBS * 2 - 1 - i) as usize] }
OP_EQUALVERIFY
}
OP_TRUE
};
run(script);
}
for _ in 0..50 {
let mut v = vec![];
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
let mut expected = vec![];
for i in 0..N_U30_LIMBS {
expected.push(v[(N_U30_LIMBS + i) as usize]);
expected.push(v[i as usize]);
}
let script = script! {
for i in 0..N_U30_LIMBS * 2 {
{ v[i as usize] }
}
{ BigIntImpl::<N_BITS, 29>::zip(0, 1) }
for i in 0..N_U30_LIMBS * 2 {
{ expected[(N_U30_LIMBS * 2 - 1 - i) as usize] }
OP_EQUALVERIFY
}
OP_TRUE
};
run(script);
}
}
#[test]
fn test_copy() {
println!("U254.copy(0): {} bytes", U254::copy(0).len());
println!("U254.copy(13): {} bytes", U254::copy(13).len());
println!("U254.copy(14): {} bytes", U254::copy(14).len());
const N_U30_LIMBS: u32 = 9;
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..50 {
let mut v = vec![];
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
let mut expected = vec![];
for i in 0..N_U30_LIMBS {
expected.push(v[i as usize]);
}
let script = script! {
for i in 0..N_U30_LIMBS * 2 {
{ v[i as usize] }
}
{ U254::copy(1) }
for i in 0..N_U30_LIMBS {
{ expected[(N_U30_LIMBS - 1 - i) as usize] }
OP_EQUALVERIFY
}
{ U254::drop() }
{ U254::drop() }
OP_TRUE
};
run(script);
}
}
#[test]
fn test_roll() {
const N_U30_LIMBS: u32 = 9;
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..50 {
let mut v = vec![];
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
let mut expected = vec![];
for i in 0..N_U30_LIMBS {
expected.push(v[i as usize]);
}
let script = script! {
for i in 0..N_U30_LIMBS * 2 {
{ v[i as usize] }
}
{ U254::roll(1) }
for i in 0..N_U30_LIMBS {
{ expected[(N_U30_LIMBS - 1 - i) as usize] }
OP_EQUALVERIFY
}
{ U254::drop() }
OP_TRUE
};
run(script);
}
}
#[test]
fn test_copy_zip() {
const N_U30_LIMBS: u32 = 9;
let mut prng = ChaCha20Rng::seed_from_u64(0);
for _ in 0..50 {
let mut v = vec![];
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
for _ in 0..N_U30_LIMBS {
v.push(prng.gen::<i32>());
}
let mut expected = vec![];
for i in 0..N_U30_LIMBS {
expected.push(v[i as usize]);
expected.push(v[(N_U30_LIMBS + i) as usize]);
}
let script = script! {
for i in 0..N_U30_LIMBS * 2 {
{ v[i as usize] }
}
{ U254::copy_zip(1, 0) }
for i in 0..N_U30_LIMBS * 2 {
{ expected[(N_U30_LIMBS * 2 - 1 - i) as usize] }
OP_EQUALVERIFY
}
{ U254::drop() }
{ U254::drop() }
OP_TRUE
};
run(script);
let mut expected = vec![];
for i in 0..N_U30_LIMBS {
expected.push(v[(N_U30_LIMBS + i) as usize]);
expected.push(v[i as usize]);
}
let script = script! {
for i in 0..N_U30_LIMBS * 2 {
{ v[i as usize] }
}
{ U254::copy_zip(0, 1) }
for i in 0..N_U30_LIMBS * 2 {
{ expected[(N_U30_LIMBS * 2 - 1 - i) as usize] }
OP_EQUALVERIFY
}
{ U254::drop() }
{ U254::drop() }
OP_TRUE
};
run(script);
let mut expected = vec![];
for i in 0..N_U30_LIMBS {
expected.push(v[i as usize]);
expected.push(v[i as usize]);
}
let script = script! {
for i in 0..N_U30_LIMBS * 2 {
{ v[i as usize] }
}
{ U254::copy_zip(1, 1) }
for i in 0..N_U30_LIMBS * 2 {
{ expected[(N_U30_LIMBS * 2 - 1 - i) as usize] }
OP_EQUALVERIFY
}
{ U254::drop() }
{ U254::drop() }
OP_TRUE
};
run(script);
let script = script! {
for i in 0..N_U30_LIMBS * 2 {
{ v[i as usize] }
}
{ U254::dup_zip(1) }
for i in 0..N_U30_LIMBS * 2 {
{ expected[(N_U30_LIMBS * 2 - 1 - i) as usize] }
OP_EQUALVERIFY
}
{ U254::drop() }
OP_TRUE
};
run(script);
}
}
#[test]
fn push_hex() {
run(script! {
{ U254::push_hex("30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47") }
{ 0x187cfd47 } OP_EQUALVERIFY // 410844487
{ 0x10460b6 } OP_EQUALVERIFY // 813838427
{ 0x1c72a34f } OP_EQUALVERIFY // 119318739
{ 0x2d522d0 } OP_EQUALVERIFY // 542811226
{ 0x1585d978 } OP_EQUALVERIFY // 22568343
{ 0x2db40c0 } OP_EQUALVERIFY // 18274822
{ 0xa6e141 } OP_EQUALVERIFY // 436378501
{ 0xe5c2634 } OP_EQUALVERIFY // 329037900
{ 0x30644e } OP_EQUAL // 12388
});
}
// test the extract window fn
#[test]
fn test_extract_window() {
let mut prng = ChaCha20Rng::seed_from_u64(8);
for _ in 0..100 {
// generate random start_index and window
let start_index = prng.gen_range(1..=31);
let window = prng.gen_range(1..=start_index);
// generate a random u32
let random_u32: u32 = prng.gen();
// compute the values by shifting
let initial_limb = random_u32 >> (32u32 - start_index);
let expected_window = initial_limb >> (start_index - window);
let modified_limb = if start_index == window {
0
} else {
(initial_limb << (32u32 - start_index + window)) >> (32u32 - start_index + window)
};
let script = script!(
{initial_limb}
{extract_digits(start_index,window)}
{modified_limb}
OP_EQUALVERIFY
{expected_window}
OP_EQUAL
);
let res = crate::execute_script(script.clone());
assert!(res.success);
}
}
// manual test of transform to and from U256.
#[test]
fn test_transform_to_and_from_u256() {
type U256 = BigIntImpl<256, 29>;
let script = script!(
{0b010101010010101010100101}
{0b10000000100000011111111011111}
{0b00000101000000100000000000000}
{0b01010101001010101000000000000}
{0b11010101001010101001010111111}
{0b11111111111000000000000000000}
{0b01010010101001010010010101001}
{0b00000000000000000000000000000}
{0b11111111111111111111111111111}
{U256::transform_limbsize(29, 4)}
{U256::transform_limbsize(4, 1)}
{U256::transform_limbsize(1, 31)}
{U256::transform_limbsize(31, 2)}
{U256::transform_limbsize(2, 10)}
{U256::transform_limbsize(10, 31)}
{U256::transform_limbsize(31, 27)}
{U256::transform_limbsize(27, 8)}
{U256::transform_limbsize(8, 9)}
{U256::transform_limbsize(9, 9)}
{U256::transform_limbsize(9, 4)}
{U256::transform_limbsize(4, 4)}
{U256::transform_limbsize(4,29)}
{0b010101010010101010100101}
{0b10000000100000011111111011111}
{0b00000101000000100000000000000}
{0b01010101001010101000000000000}
{0b11010101001010101001010111111}
{0b11111111111000000000000000000}
{0b01010010101001010010010101001}
{0b00000000000000000000000000000}
{0b11111111111111111111111111111}
for i in (2..10).rev(){
{i}
OP_ROLL
OP_EQUALVERIFY
}
OP_EQUAL
);
let res = crate::execute_script(script.clone());
assert!(res.success);
}
// Testing all ones manually for U1773
#[test]
fn test_transform_allones_to_and_from_u1773() {
type U1773 = BigIntImpl<1773, 21>;
let script = script!(
// push all ones in U1773 assuming limb size of 23
{0b11}
for _ in 0..77{
{0b11111111111111111111111}
}
{U1773::transform_limbsize(23,21)}
{U1773::transform_limbsize(21,2)}
{U1773::transform_limbsize(2,3)}
{U1773::transform_limbsize(3,19)}
{U1773::transform_limbsize(19,23)}
for _ in 0..77{
{0b11111111111111111111111}
OP_EQUALVERIFY
}
{0b11}
OP_EQUAL
);
let res = crate::execute_script(script.clone());
assert!(res.success);
}
// Testing all ones manually for U1773
#[test]
fn test_transform_allzeros_to_and_from_u1773() {
type U1773 = BigIntImpl<1773, 21>;
let script = script!(
// push all ones in U1773 assuming limb size of 23
{0b11}
for _ in 0..77{
{0b11111111111111111111111}
}
{U1773::transform_limbsize(23,21)}
{U1773::transform_limbsize(21,2)}
{U1773::transform_limbsize(2,3)}
{U1773::transform_limbsize(3,19)}
{U1773::transform_limbsize(19,23)}
// push the same input in reverse and verify
for _ in 0..77{
{0b11111111111111111111111}
OP_EQUALVERIFY
}
{0b11}
OP_EQUAL
);
let res = crate::execute_script(script.clone());
assert!(res.success);
}
// Testing all ones manually for U1773
#[test]
fn test_transform_allzeros_to_and_from_u876() {
type U876 = BigIntImpl<876, 14>;
let script = script!(
// push all zeros in U876 assuming limb size of 14
{0b00000000}
for _ in 0..62{
{0b00000000000000}
}
{U876::transform_limbsize(14, 9)}
{U876::transform_limbsize(9, 10)}
{U876::transform_limbsize(10, 31)}
{U876::transform_limbsize(31, 1)}
{U876::transform_limbsize(1, 2)}
{U876::transform_limbsize(2, 4)}
{U876::transform_limbsize(4, 8)}
{U876::transform_limbsize(8, 19)}
{U876::transform_limbsize(19, 27)}
{U876::transform_limbsize(27, 14)}
//push the same input in reverse and verify
for _ in 0..62{
{0b00000000000000}
OP_EQUALVERIFY
}
{0b00000000}
OP_EQUAL
);
let res = crate::execute_script(script.clone());
assert!(res.success);
}
#[test]
#[should_panic(expected = "source limb size must lie between 1 and 31 inclusive")]
fn test_source_limbsize_too_high() {
script!({ U254::transform_limbsize(32, 3) });
}
#[test]
#[should_panic(expected = "source limb size must lie between 1 and 31 inclusive")]
fn test_source_limbsize_too_low() {
script!({ U254::transform_limbsize(0, 29) });
}
#[test]
#[should_panic(expected = "target limb size must lie between 1 and 31 inclusive")]
fn test_target_limbsize_too_high() {
script!({ U254::transform_limbsize(29, 32) });
}