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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
// ---------------- [ File: bitcoin-bigint/src/base_uint.rs ]
crate::ix!();
//-------------------------------------------[.cpp/bitcoin/src/arith_u256.h]
#[macro_export]
macro_rules! define_base_uint_struct_and_basic {
($name:ident, $bits:expr, $limbs:expr) => {
#[derive(Clone,Debug)]
pub struct $name {
pub(crate) pn: [u32; $limbs],
}
impl Default for $name {
fn default() -> Self {
Self { pn: [0; $limbs] }
}
}
impl $name {
pub const WIDTH: usize = $limbs;
pub fn get_limb(&self, index: usize) -> u32 {
self.pn[index]
}
pub fn set_limb(&mut self, index: usize, val: u32) {
self.pn[index] = val;
}
pub fn limb_count(&self) -> usize {
$limbs
}
pub fn inc_prefix(&mut self) -> &mut Self {
let mut i = 0;
while i < $limbs {
self.pn[i] = self.pn[i].wrapping_add(1);
if self.pn[i] != 0 {
break;
}
i += 1;
}
self
}
pub fn inc_postfix(&mut self) -> Self {
let old = self.clone();
self.inc_prefix();
old
}
pub fn dec_prefix(&mut self) -> &mut Self {
let mut i = 0;
while i < $limbs {
let new_val = self.pn[i].wrapping_sub(1);
self.pn[i] = new_val;
if new_val != 0xffff_ffff {
break;
}
i += 1;
}
self
}
pub fn dec_postfix(&mut self) -> Self {
let old = self.clone();
self.dec_prefix();
old
}
pub fn size_in_bytes(&self) -> usize {
$limbs * core::mem::size_of::<u32>()
}
pub fn low64(&self) -> u64 {
let low0 = self.pn.get(0).cloned().unwrap_or(0) as u64;
let low1 = self.pn.get(1).cloned().unwrap_or(0) as u64;
(low1 << 32) | low0
}
pub fn compare_to(&self, other: &Self) -> i32 {
for i in (0..$limbs).rev() {
if self.pn[i] < other.pn[i] {
return -1;
} else if self.pn[i] > other.pn[i] {
return 1;
}
}
0
}
pub fn equal_to(&self, b: u64) -> bool {
// For limbs > 2 => check them all zero
for i in 2..$limbs {
if self.pn[i] != 0 {
return false;
}
}
let low_32 = (b & 0xffff_ffff) as u32;
let high_32 = ((b >> 32) & 0xffff_ffff) as u32;
if self.pn[0] != low_32 {
return false;
}
if $limbs > 1 && self.pn[1] != high_32 {
return false;
}
true
}
pub fn getdouble(&self) -> f64 {
let mut ret = 0.0;
let mut factor = 1.0;
for &limb in self.pn.iter() {
ret += (limb as f64) * factor;
factor *= 4294967296.0;
}
ret
}
pub fn bits(&self) -> u32 {
for pos in (0..$limbs).rev() {
let limb = self.pn[pos];
if limb != 0 {
for nbits in (0..32).rev() {
if (limb & (1 << nbits)) != 0 {
return (pos as u32) * 32 + (nbits + 1);
}
}
}
}
0
}
pub fn to_string(&self) -> String {
self.get_hex()
}
}
}
}
#[cfg(test)]
mod base_uint_tests {
use super::*;
/// Tests that the default constructor yields an all-zero array.
#[traced_test]
fn test_default_constructor() {
info!("Testing BaseUInt default constructor for various bit sizes...");
// We'll check a few representative sizes.
// Because BITS must be divisible by 32, let's do 32, 64, 256.
{
type U32 = BaseUInt32;
let x = U32::default();
trace!("Created BaseUInt32 default: {:?}", x);
assert_eq!(x.pn.len(), 1, "Should have 1 limb for 32 bits");
assert_eq!(x.pn[0], 0, "The single 32-bit limb should be zero");
}
{
type U64 = BaseUInt64;
let x = U64::default();
trace!("Created BaseUInt64 default: {:?}", x);
assert_eq!(x.pn.len(), 2, "Should have 2 limbs for 64 bits");
assert_eq!(x.pn[0], 0);
assert_eq!(x.pn[1], 0);
}
{
type U256 = BaseUInt256;
let x = U256::default();
trace!("Created BaseUInt256 default: {:?}", x);
assert_eq!(x.pn.len(), 8, "Should have 8 limbs for 256 bits");
for limb in x.pn.iter() {
assert_eq!(*limb, 0);
}
}
info!("Default constructor tests passed for BaseUInt.");
}
/// Tests the bitwise NOT operator.
#[traced_test]
fn test_not_operator() {
info!("Testing BaseUInt NOT operator (~) for various bit sizes...");
// ~0 should give all 1-bits for each limb.
{
type U32 = BaseUInt32;
let zero = U32::default();
let not_zero = !zero;
trace!("BaseUInt32 not_zero: {:?}", not_zero);
assert_eq!(not_zero.pn.len(), 1);
assert_eq!(not_zero.pn[0], 0xFFFFFFFF, "NOT of 0 for one limb should be 0xFFFF_FFFF");
}
{
type U64 = BaseUInt64;
let zero = U64::default();
let not_zero = !zero;
trace!("BaseUInt64 not_zero: {:?}", not_zero);
assert_eq!(not_zero.pn.len(), 2);
assert_eq!(not_zero.pn[0], 0xFFFFFFFF);
assert_eq!(not_zero.pn[1], 0xFFFFFFFF);
}
{
type U256 = BaseUInt256;
let zero = U256::default();
let not_zero = !zero;
trace!("BaseUInt256 not_zero: {:?}", not_zero);
assert_eq!(not_zero.pn.len(), 8);
for limb in not_zero.pn.iter() {
assert_eq!(*limb, 0xFFFFFFFF);
}
}
info!("NOT operator tests passed for BaseUInt.");
}
#[traced_test]
fn test_neg() {
info!("Testing two's complement negation (operator '-')...");
// Case 1: negation of zero => zero
// -0 = 0 in two's complement
let x0 = BaseUInt64::default();
let neg0 = -x0.clone();
trace!(" -0 => {:?}", neg0);
assert_eq!(to_limbs_64(&neg0), [0, 0], "Negation of zero should stay zero");
// Case 2: negation of 1 => (two's complement of 1 for 64 bits)
// 1 is 0x00000001, so ~1 = 0xfffffffe, +1 => 0xffffffff for the lower limb, plus carry => upper limb also ~0 => 0xffffffff, plus carry => none left.
// Actually for 64 bits: -1 => 0xffffffffffffffff (all bits set).
let mut one64 = BaseUInt64::default();
one64.pn[0] = 1;
let neg1 = -one64;
trace!(" -1 => {:?}", neg1);
assert_eq!(to_limbs_64(&neg1), [0xffff_ffff, 0xffff_ffff]);
// Case 3: random example: let's do (0x1234_0000_0000_5678)
// We'll place 0x0000_5678 in limb 0, 0x1234_0000 in limb 1.
let x_custom = from_limbs_64(&[0x0000_5678, 0x1234_0000]);
let y = -x_custom.clone();
trace!(" -({:08x} {:08x}) => {:?}", x_custom.pn[1], x_custom.pn[0], y);
// Check we satisfy x_custom + y == 0 in two's complement
let mut sum = x_custom.clone();
sum += &y;
for limb in sum.pn.iter() {
assert_eq!(*limb, 0, "x + (-x) must be 0 for two's complement.");
}
info!("Negation tests passed.");
}
#[traced_test]
fn test_bitwise_and_or() {
info!("Testing bitwise AND/OR operators (&=, |=) with self and references.");
// We'll do 64 bits for convenience. Let x = 0xFFFF0000FFFF0000, y = 0x123456789ABCDEF0
// x in limbs => [0xFFFF0000, 0xFFFF0000]
// y in limbs => [0x9ABCDEF0, 0x12345678]
let x = from_limbs_64(&[0xFFFF_0000, 0xFFFF_0000]);
let y = from_limbs_64(&[0x9ABC_DEF0, 0x1234_5678]);
// AND
let mut anded = x.clone();
anded &= &y;
// For the lower limb: 0xFFFF0000 & 0x9ABCDEF0 = 0x9ABC0000
// For the upper limb: 0xFFFF0000 & 0x12345678 = 0x12340000
let expected_and = from_limbs_64(&[0x9ABC_0000, 0x1234_0000]);
assert_eq!(to_limbs_64(&anded), to_limbs_64(&expected_and), "bitwise AND mismatch");
// OR
let mut ored = x.clone();
ored |= &y;
// For the lower limb: 0xFFFF0000 | 0x9ABCDEF0 = 0xFFFFDEF0
// For the upper limb: 0xFFFF0000 | 0x12345678 = 0xFFFF5678
let expected_or = from_limbs_64(&[0xFFFF_DEF0, 0xFFFF_5678]);
assert_eq!(to_limbs_64(&ored), to_limbs_64(&expected_or), "bitwise OR mismatch");
info!("Bitwise AND/OR tests passed.");
}
#[traced_test]
fn test_bitor_assign_u64() {
info!("Testing `self |= u64` operation on BaseUInt.");
let mut x = BaseUInt64::default(); // 64 bits => two limbs
// Currently x = 0
// x |= 0x1122_3344_5566_7788
x |= 0x1122_3344_5566_7788u64;
// The lower limb should be 0x5566_7788, the upper limb 0x1122_3344
assert_eq!(x.pn[0], 0x5566_7788);
assert_eq!(x.pn[1], 0x1122_3344);
// If we do it again with e.g. 0xFFFF0000, that only affects the low limb.
x |= 0xFFFF_0000u64;
// Now the lower limb is 0xFFFF_7788
// The upper limb remains 0x1122_3344
assert_eq!(x.pn[0], 0xFFFF_7788);
assert_eq!(x.pn[1], 0x1122_3344);
info!("`self |= u64` tests passed.");
}
#[traced_test]
fn test_add_assign() {
info!("Testing addition with carry (AddAssign).");
let mut x = from_limbs_64(&[0xffff_ffff, 0xffff_ffff]);
// x is 0xFFFF_FFFF_FFFF_FFFF in 64 bits
let y = BaseUInt64::default(); // zero
x += &y; // x = x + 0
// No change expected
assert_eq!(x.pn[0], 0xffff_ffff);
assert_eq!(x.pn[1], 0xffff_ffff);
// Now let's do x += 1. We have no direct `x += u64` or so in this snippet, so let's make
// a small helper approach: create a BaseUInt with "1" as the lower limb.
let one64 = from_limbs_64(&[1, 0]);
x += &one64;
// 0xFFFF_FFFF_FFFF_FFFF + 1 => wrap around => 0x0000_0000_0000_0000
// plus a carry bit that doesn't fit in 64 bits.
// But since BITS=64, that final carry is dropped.
// => x becomes 0
assert_eq!(x.pn[0], 0);
assert_eq!(x.pn[1], 0);
// Next, let's do a more interesting addition that doesn't overflow:
let mut a = from_limbs_64(&[0x1234_5678, 0x9ABC_DEF0]);
let b = from_limbs_64(&[0x1111_0000, 0x0000_0001]);
// a + b => lower limb: 0x1234_5678 + 0x1111_0000 => 0x2345_5678, no carry
// upper limb: 0x9ABC_DEF0 + 0x0000_0001 => 0x9ABC_DEF1 + carry=0 => 0x9ABC_DEF1
a += &b;
assert_eq!(a.pn[0], 0x2345_5678);
assert_eq!(a.pn[1], 0x9ABC_DEF1);
info!("AddAssign tests passed.");
}
#[traced_test]
fn test_sub_assign_baseuint() {
info!("Testing sub_assign(&BaseUInt) => self -= other.");
// Let’s pick 64 bits for demonstration.
type U64 = BaseUInt64;
// 1) Basic: x = 10, y = 3 => x - y = 7
let mut x = U64::default();
x += 10u64; // now x = 10
let mut y = U64::default();
y += 3u64; // now y = 3
x -= &y; // x = 10 - 3 = 7
assert_eq!(to_limbs_64(&x)[0], 7);
assert_eq!(to_limbs_64(&x)[1], 0);
// 2) Overflow scenario: x = 1, y = 2 => x - y => wrap-around in 64 bits
// which is effectively: 1 + (-2). We'll see a 2's complement result.
let mut x2 = U64::default();
x2 += 1u64;
let mut y2 = U64::default();
y2 += 2u64;
x2 -= &y2; // 1 - 2 in 64-bit
// 1 - 2 => 0xffffFFFFffffFFFF in a 64-bit wrap
// But since we have 64 bits total, that should become 0xffffFFFFffffFFFF
// i.e. [0xffffFFFF, 0xffffFFFF]
assert_eq!(x2.pn[0], 0xffff_ffff);
assert_eq!(x2.pn[1], 0xffff_ffff);
// 3) Larger example with partial difference
// Let x = 0x00000001_00000000, y = 0x00000000_00000001
// x - y => 0x00000001_00000000 - 0x00000000_00000001 = 0x00000000_ffffffff
let mut x3 = from_limbs_64(&[0x0000_0000, 0x0000_0001]);
let y3 = from_limbs_64(&[0x0000_0001, 0x0000_0000]);
x3 -= &y3;
assert_eq!(x3.pn[0], 0xffff_ffff);
assert_eq!(x3.pn[1], 0x0000_0000);
info!("sub_assign(&BaseUInt) tests passed.");
}
#[traced_test]
fn test_add_assign_u64() {
info!("Testing add_assign(u64) => self += u64.");
type U64 = BaseUInt64;
let mut x = U64::default();
x += 0x1234_5678_9ABC_DEF0u64;
// The lower 32 bits => 0x9ABC_DEF0, upper 32 bits => 0x1234_5678
assert_eq!(x.pn[0], 0x9ABC_DEF0);
assert_eq!(x.pn[1], 0x1234_5678);
// Add something that triggers carry:
x += 0x0000_0001_0000_0000u64;
// Now x = 0x1234_5679_9ABC_DEF0 (the upper limb increments by 1)
assert_eq!(x.pn[0], 0x9ABC_DEF0);
assert_eq!(x.pn[1], 0x1234_5679);
// Check wrap-around if we go beyond 64 bits
let mut y = U64::default();
y += 0xffff_ffff_ffff_ffffu64; // max
y += 1u64; // => 0?
assert_eq!(y.pn[0], 0);
assert_eq!(y.pn[1], 0);
info!("add_assign(u64) tests passed.");
}
#[traced_test]
fn test_sub_assign_u64() {
info!("Testing sub_assign(u64) => self -= u64.");
type U64 = BaseUInt64;
let mut x = U64::default();
x += 20u64; // x=20
x -= 5u64; // x=15
assert_eq!(x.pn[0], 15);
assert_eq!(x.pn[1], 0);
// Wrap-around
let mut y = U64::default();
y += 1u64;
y -= 2u64;
// 1 - 2 => wrap => 0xffffFFFFffffFFFF in 64 bits
assert_eq!(y.pn[0], 0xffff_ffff);
assert_eq!(y.pn[1], 0xffff_ffff);
info!("sub_assign(u64) tests passed.");
}
#[traced_test]
fn test_bitxor_assign() {
info!("Testing bitxor_assign(&BaseUInt) and bitxor_assign(u64).");
// 1) BaseUInt ^ BaseUInt
type U64 = BaseUInt64;
let x = from_limbs_64(&[0xAAAA_AAAA, 0x5555_5555]);
let y = from_limbs_64(&[0xFFFF_0000, 0x0000_FFFF]);
let mut z = x.clone();
z ^= &y;
// Limb0: 0xAAAA_AAAA ^ 0xFFFF_0000 = 0x5555_AAAA
// Limb1: 0x5555_5555 ^ 0x0000_FFFF = 0x5555_AAAA
let expected0 = 0x5555_AAAA;
let expected1 = 0x5555_AAAA;
assert_eq!(z.pn[0], expected0);
assert_eq!(z.pn[1], expected1);
// 2) BaseUInt ^ u64
let mut w = U64::default();
w ^= 0xFFFFFFFF_00000000u64;
// That sets pn[0] = 0x00000000 ^ 0x00000000 => 0x00000000
// but sets pn[1] = 0x00000000 ^ 0xffffffff => 0xffffffff
assert_eq!(w.pn[0], 0x0000_0000);
assert_eq!(w.pn[1], 0xffff_ffff);
// XOR that again with e.g. 0x0000_0001_0000_0001 => should flip bits
w ^= 0x0000_0001_0000_0001u64;
// lower limb => 0x00000000 ^ 0x00000001 => 0x00000001
// upper limb => 0xffffffff ^ 0x00000001 => 0xfffffffe
assert_eq!(w.pn[0], 0x0000_0001);
assert_eq!(w.pn[1], 0xffff_fffe);
info!("bitxor_assign tests passed.");
}
#[traced_test]
fn test_add_sub() {
info!("Testing Add and Sub operators with BaseUInt64.");
let x = from_limbs_64(&[10, 0]);
let y = from_limbs_64(&[3, 0]);
let z1 = x.clone() + &y; // (10 + 3) = 13
assert_eq!(z1.pn[0], 13);
assert_eq!(z1.pn[1], 0);
let z2 = x.clone() - &y; // (10 - 3) = 7
assert_eq!(z2.pn[0], 7);
assert_eq!(z2.pn[1], 0);
// Wrap-around sub: 1 - 2 => 0xffffFFFFffffFFFF in 64 bits
let a = from_limbs_64(&[1, 0]);
let b = from_limbs_64(&[2, 0]);
let c = a - &b;
assert_eq!(c.pn[0], 0xffff_ffff);
assert_eq!(c.pn[1], 0xffff_ffff);
info!("Add/Sub operator tests passed.");
}
#[traced_test]
fn test_mul_div() {
info!("Testing Mul and Div operators with BaseUInt64.");
// We'll do some small checks. We rely on MulAssign/DivAssign correctness for big coverage.
let x = from_limbs_64(&[6, 0]);
let y = from_limbs_64(&[7, 0]);
let z = x.clone() * &y; // 6 * 7 = 42
assert_eq!(z.pn[0], 42);
assert_eq!(z.pn[1], 0);
// Div:
let w = z.clone() / &x; // 42 / 6 = 7
assert_eq!(w.pn[0], 7);
assert_eq!(w.pn[1], 0);
info!("Mul/Div operator tests passed.");
}
#[traced_test]
fn test_bitor_bitand_bitxor() {
info!("Testing bitwise OR, AND, XOR operators with BaseUInt64.");
// x = 0xFFFF0000, y = 0x0000FFFF in lower limbs
let x = from_limbs_64(&[0xFFFF_0000, 0]);
let y = from_limbs_64(&[0x0000_FFFF, 0]);
let or_val = x.clone() | &y; // => 0xFFFF_FFFF
assert_eq!(or_val.pn[0], 0xFFFF_FFFF);
assert_eq!(or_val.pn[1], 0);
let and_val = x.clone() & &y; // => 0
assert_eq!(and_val.pn[0], 0);
assert_eq!(and_val.pn[1], 0);
let xor_val = x.clone() ^ &y; // => 0xFFFF_FFFF
assert_eq!(xor_val.pn[0], 0xFFFF_FFFF);
assert_eq!(xor_val.pn[1], 0);
info!("Bitwise operators (|, &, ^) tests passed.");
}
#[traced_test]
fn test_shl_shr_biguint() {
info!("Testing Shl/Shr operator with a biguint shift operand.");
// This is a bit contrived: we only shift by the lower bits of the right-hand side.
// But let's do it for demonstration.
// x = 1
let x = from_limbs_64(&[1, 0]);
debug!("x={:?}",x);
// We'll create a shift operand that has 3 in its low limb => shift by 3 bits
let shift_by_3 = from_limbs_64(&[3, 0]);
debug!("shift_by_3={:?}",shift_by_3);
let shifted_left_3 = x.clone() << &shift_by_3; // 1 << 3 = 8
debug!("shifted_left_3={:?}",shifted_left_3);
assert_eq!(shifted_left_3.pn[0], 8);
assert_eq!(shifted_left_3.pn[1], 0);
let shifted_right_2 = shifted_left_3.clone() >> &from_limbs_64(&[2, 0]); // 8 >> 2 = 2
debug!("shifted_right_2={:?}",shifted_right_2);
assert_eq!(shifted_right_2.pn[0], 2);
assert_eq!(shifted_right_2.pn[1], 0);
// If we shift by something > 64, we clamp to 64. e.g. shift by 999 in the low limb => shift by 64
let shift_big = from_limbs_64(&[999, 0]);
debug!("shift_big={:?}",shift_big);
let huge_left = x.clone() << &shift_big;
debug!("huge_left={:?}",huge_left);
// 1 << 64 would be 0 in a 64-bit container
assert_eq!(huge_left.pn[0], 0);
assert_eq!(huge_left.pn[1], 0);
info!("Shl/Shr operator tests passed (biguint shift style).");
}
#[traced_test]
fn test_eq_ord() {
info!("Testing PartialEq, Eq, Ord, etc.");
// 64 bits => 2 limbs
let mut x = BaseUInt64::default();
let mut y = BaseUInt64::default();
// both are zero
assert_eq!(x, y);
assert!(x.cmp(&y) == Ordering::Equal);
x.pn[0] = 1;
assert_ne!(x, y);
assert!(x > y);
y.pn[0] = 2;
assert!(x < y);
// check top limb
x.pn[1] = 0xffff_ffff;
assert!(x > y); // 0xffff_ffff in the high limb
info!("Comparison tests passed.");
}
#[traced_test]
fn test_from_u64() {
info!("Testing `From<u64>` for various bit sizes.");
let a32 = BaseUInt32::from(0x1234_5678_9ABC_DEF0u64);
// For 32 bits, we only keep the lower 32 bits
// => pn[0] = 0x9ABC_DEF0, no second limb
assert_eq!(a32.pn[0], 0x9ABC_DEF0);
let a64 = BaseUInt64::from(0x1234_5678_9ABC_DEF0u64);
// => pn[0] = 0x9ABC_DEF0, pn[1] = 0x1234_5678
assert_eq!(a64.pn[0], 0x9ABC_DEF0);
assert_eq!(a64.pn[1], 0x1234_5678);
// For bigger widths, same pattern, the rest of pn[] is zero
let a256 = BaseUInt256::from(0x1234_5678_9ABC_DEF0u64);
assert_eq!(a256.pn[0], 0x9ABC_DEF0);
assert_eq!(a256.pn[1], 0x1234_5678);
for i in 2..8 {
assert_eq!(a256.pn[i], 0);
}
info!("From<u64> tests passed.");
}
#[traced_test]
fn test_inc_dec_prefix_postfix() {
info!("Testing inc_prefix, inc_postfix, dec_prefix, dec_postfix...");
let mut x = BaseUInt64::default();
// x=0
// prefix inc: returns &mut x, but x -> 1
{
let returned = x.inc_prefix();
assert_eq!(returned as *const _, &x as *const _); // same reference
assert_eq!(x.pn[0], 1);
trace!("After prefix inc, x is 1");
}
// postfix inc => returns old copy
{
let old_x = x.inc_postfix();
// old_x was 1, x is now 2
assert_eq!(old_x.pn[0], 1);
assert_eq!(x.pn[0], 2);
trace!("After postfix inc, x is 2");
}
// prefix dec => x -> 1
let returned2 = x.dec_prefix();
assert_eq!(returned2 as *const _, &x as *const _);
assert_eq!(x.pn[0], 1);
// postfix dec => returns old copy => 1, x -> 0
let old_x2 = x.dec_postfix();
assert_eq!(old_x2.pn[0], 1);
assert_eq!(x.pn[0], 0);
// check wrap-around inc
// x=0xffffffff_ffffffff => inc => 0
x.pn[0] = 0xffff_ffff;
x.pn[1] = 0xffff_ffff;
x.inc_prefix();
assert_eq!(x.pn[0], 0);
assert_eq!(x.pn[1], 0);
// check wrap-around dec
x.dec_prefix();
assert_eq!(x.pn[0], 0xffff_ffff);
assert_eq!(x.pn[1], 0xffff_ffff);
info!("inc/dec prefix/postfix tests passed.");
}
#[traced_test]
fn test_size_and_low64() {
info!("Testing size_in_bytes() and low64().");
let x64 = BaseUInt64::from(0xDEAD_BEEF_1234_5678u64);
assert_eq!(x64.size_in_bytes(), 8, "64 bits => 8 bytes");
assert_eq!(x64.low64(), 0xDEAD_BEEF_1234_5678u64);
let x32 = BaseUInt32::from(0xDEAD_BEEF_1234_5678u64);
// 32 bits => lower 32 bits only
assert_eq!(x32.size_in_bytes(), 4, "32 bits => 4 bytes");
let l32 = x32.low64(); // only the lower 32 bits are present
// That is 0x1234_5678 plus 0 for the upper half
assert_eq!(l32, 0x0000_0000_1234_5678u64);
// Check a bigger, e.g. 256 bits => 32 bytes
let x256 = BaseUInt256::from(0x1122_3344_5566_7788u64);
assert_eq!(x256.size_in_bytes(), 32);
assert_eq!(x256.low64(), 0x1122_3344_5566_7788u64);
info!("size_in_bytes() and low64() checks passed.");
}
#[traced_test]
fn test_shl_assign() {
info!("Testing ShlAssign<u32> (self <<= shift).");
// 1) 64 bits, shift = 1
let mut x = BaseUInt64::from(0b1u64);
x <<= 1;
// => x = 2
assert_eq!(x.pn[0], 2);
assert_eq!(x.pn[1], 0);
// 2) shift by 33 => effectively shift by 1 limb plus 1 bit
// So if x was 1 => it becomes 0x00000000_00000002 => then shift left 1 => the 1 moves to upper limb
let mut y = BaseUInt64::from(1u64);
y <<= 33;
// y's lower 32 bits => 0, upper 32 bits => 2
assert_eq!(y.pn[0], 0);
assert_eq!(y.pn[1], 2);
// 3) shift beyond the total bit size => becomes 0
let mut z = BaseUInt64::from(0x1234_5678_9ABC_DEF0u64);
z <<= 64; // entire 64 bits shift => zero
assert_eq!(z.pn[0], 0);
assert_eq!(z.pn[1], 0);
info!("ShlAssign tests passed.");
}
#[traced_test]
fn test_shr_assign() {
info!("Testing ShrAssign<u32> (self >>= shift).");
// 1) shift = 1
let mut x = BaseUInt64::from(2u64); // 2 => 0x0000_0002
x >>= 1; // => 1
assert_eq!(x.pn[0], 1);
assert_eq!(x.pn[1], 0);
// 2) shift = 33 => move bits from upper limb down 1 bit
let mut y = BaseUInt64::default();
// Let's set y's upper 32 bits to 2 => i.e. y= 0x00000002_00000000
y.pn[1] = 2;
y >>= 33; // => 0x1 in the lower limb
assert_eq!(y.pn[0], 1);
assert_eq!(y.pn[1], 0);
// 3) shift beyond total => 0
let mut z = BaseUInt64::from(0x1234_5678_9ABC_DEF0u64);
z >>= 80; // bigger than 64 => 0
assert_eq!(z.pn[0], 0);
assert_eq!(z.pn[1], 0);
info!("ShrAssign tests passed.");
}
/// A small helper to convert a `u64` into a hex string, for random test expansions.
/// We’ll use this to feed into `BaseUInt::<BITS>::from(str)`.
fn hex_of_u64(x: u64) -> String {
format!("0x{:016x}", x)
}
#[traced_test]
fn test_hex_parsing_truncation_32_vs_64_vs_256() {
info!("Testing parsing very large hex for different bit widths, ensuring truncation.");
// We’ll parse a 96-bit large hex => "FFFF_FFFF_FFFF_FFFF_FFFF_FFFF"
let big_hex = "FFFFFFFFFFFFFFFFFFFFFFFF";
// 1) 32 bits => only keep the lowest 8 hex digits => 0xFFFFFFFF
let x32 = BaseUInt32::from(big_hex);
assert_eq!(x32.pn[0], 0xffff_ffff);
// 2) 64 bits => keep the lowest 16 hex digits => 0xFFFF_FFFF_FFFF_FFFF
let x64 = BaseUInt64::from(big_hex);
assert_eq!(x64.pn[0], 0xffff_ffff);
assert_eq!(x64.pn[1], 0xffff_ffff);
// 3) 256 bits => well under 256 => entire hex fits easily, so the top portion is zero
let x256 = BaseUInt256::from(big_hex);
// The hex is 24 hex digits => 96 bits => lower 3 limbs used, the rest are zero
// A quick decode => 0xFFFF_FFFF => limb[0], next => 0xFFFF_FFFF => limb[1], next => 0xFFFF => partial for limb[2].
// Let’s just check that the top 5 limbs are zero.
assert_eq!(x256.pn[0], 0xffff_ffff);
assert_eq!(x256.pn[1], 0xffff_ffff);
// the third limb gets the leftover 16 bits => 0x0000_FFFF
// but we need to be sure about the nibble arrangement.
// "FFFFFFFFFFFFFFFFFFFFFFFF" => 24 'F' => each nibble is 'F'=15 => 24 nibbles => 96 bits => that’s exactly 3 full 32-bit limbs all 0xFFFF_FFFF.
assert_eq!(x256.pn[2], 0xffff_ffff);
for i in 3..8 {
assert_eq!(x256.pn[i], 0);
}
info!("Hex parsing truncation tests for 32/64/256 passed.");
}
#[traced_test]
fn test_from_str_more_exhaustive() {
info!("Testing random hex generation => parse => compare lower bits.");
let mut rng = SimpleLCG::new(0x1234_5678_9999_8888);
// We'll generate random 64-bit numbers, convert to hex, parse as 256-bit,
// then check that the low 64 bits match the original number. The rest is 0.
// This ensures that a wide parse is correct in the lower limbs.
for _ in 0..50 {
let val = rng.next_u64();
let hex_str = hex_of_u64(val);
let x256 = BaseUInt256::from(hex_str.as_str());
info!("x256={:?}",x256);
let low64 = x256.low64();
info!("low64={:?}",low64);
assert_eq!(low64, val, "Parsed 0x{:016x} => mismatch in lower 64 bits", val);
// also parse in 64-bit form
let x64 = BaseUInt64::from(hex_str.as_str());
info!("x64={:?}",x64);
assert_eq!(x64.low64(), val, "Parsed 0x{:016x} in 64 => mismatch", val);
}
info!("Random hex parse test done for wide vs narrower bit widths.");
}
// -------------------------------------------------------------------
// Tests for the new MulAssign operators
// -------------------------------------------------------------------
#[traced_test]
fn test_mulassign_u32_basic() {
info!("Testing mul_assign(u32)...");
// We'll do 64 bits for simpler checks.
let mut x = BaseUInt64::from(0x0000_0000_FFFF_FFFFu64); // 4294967295 decimal
x *= 2u32; // => 8589934590 => 0x00000001_FFFF_FFFE
assert_eq!(x.pn[0], 0xffff_fffe);
assert_eq!(x.pn[1], 0x0000_0001);
// Overflow example: 0xFFFF_FFFF * 0xFFFFFFFF => about 0xFFFFFFFE_00000001 for 64-bit
let mut y = BaseUInt64::from(0xFFFF_FFFFu64);
y *= 0xFFFF_FFFFu32;
// Let's do the math in 128 bits:
// 0xFFFF_FFFF * 0xFFFF_FFFF => 0xFFFF_FFFE_00000001 (decimal: 18446744065119617025)
// The lower 64 bits => 0x00000001, the upper 64 bits => 0xFFFF_FFFE
assert_eq!(y.pn[0], 0x0000_0001);
assert_eq!(y.pn[1], 0xffff_fffe);
info!("mul_assign(u32) basic tests passed.");
}
#[traced_test]
fn test_mulassign_baseuint_basic() {
info!("Testing mul_assign(&BaseUInt<BITS>)...");
// We rely on 64 bits for demonstration.
let mut x = BaseUInt64::from(12u64);
let y = BaseUInt64::from(34u64);
x *= &y; // 12*34 = 408
assert_eq!(x.pn[0], 408);
assert_eq!(x.pn[1], 0);
// Larger: 0xFFFF_FFFF * 0xFFFF_FFFF => 0xFFFF_FFFE_00000001 for 64 bits
let mut a = BaseUInt64::from(0xFFFF_FFFFu64);
let b = BaseUInt64::from(0xFFFF_FFFFu64);
a *= &b;
assert_eq!(a.pn[0], 0x0000_0001);
assert_eq!(a.pn[1], 0xffff_fffe);
// Another example that crosses limbs:
// Let c = 0x1_0000_0000 (which doesn't fit in 32 bits but does in 64)
// => c's lower limb is 0, upper limb is 1
let mut c = BaseUInt64::default();
c.pn[1] = 1; // => c= 0x00000001_00000000 => decimal ~4,294,967,296
// let d=2 => c*d => 0x00000002_00000000 => 8,589,934,592 decimal
let d = BaseUInt64::from(2u64);
c *= &d;
assert_eq!(c.pn[0], 0);
assert_eq!(c.pn[1], 2);
info!("mul_assign(&BaseUInt) tests passed.");
}
// We'll do a quick random test for multiplication. We'll multiply two random 64-bit
// values in 64-bit BaseUInt, check we get the 64-bit truncated product:
#[traced_test]
fn test_mulassign_random_64bits() {
info!("Random test of mul_assign(&BaseUInt) in 64 bits, verifying truncated products.");
let mut rng = SimpleLCG::new(0x1234_5678_ABCD_9876);
for _ in 0..30 {
let a_u64 = rng.next_u64();
let b_u64 = rng.next_u64();
let a_bu = BaseUInt64::from(a_u64);
let b_bu = BaseUInt64::from(b_u64);
// Do 128-bit math in Rust to see the full product, then compare low 64 bits
let full_128 = (a_u64 as u128).wrapping_mul(b_u64 as u128);
let truncated_64 = (full_128 & 0xffff_ffff_ffff_ffff) as u64;
let mut test_val = a_bu.clone();
test_val *= &b_bu;
let test_low64 = test_val.low64();
assert_eq!(
test_low64, truncated_64,
"Failed random mul: a=0x{:016x}, b=0x{:016x}",
a_u64, b_u64
);
}
info!("Random 64-bit mul_assign tests passed.");
}
#[test]
#[should_panic(expected = "Division by zero")]
fn test_div_by_zero_panics() {
info!("Testing that dividing by zero panics...");
let mut x = BaseUInt64::from(12345u64);
let zero = BaseUInt64::default();
x /= &zero; // should panic
// unreachable
}
#[traced_test]
fn test_div_simple_cases() {
info!("Testing simple division cases in 64 bits...");
// 1) 0 / anything => 0
let mut x = BaseUInt64::default();
let y = BaseUInt64::from(1234u64);
x /= &y;
assert_eq!(x.low64(), 0);
// 2) if divisor > numerator => result 0
let mut a = BaseUInt64::from(5u64);
let b = BaseUInt64::from(10u64);
a /= &b;
assert_eq!(a.low64(), 0);
// 3) normal example: 15 / 3 = 5
let mut c = BaseUInt64::from(15u64);
let d = BaseUInt64::from(3u64);
c /= &d;
assert_eq!(c.low64(), 5);
// 4) big example crossing limbs:
// let's do (1 << 33) / 2 => (0x200000000 => decimal ~ 8.589934592e9) / 2 = 0x100000000
// => decimal ~4.294967296e9
let mut e = BaseUInt64::default();
e.pn[1] = 1; // => 0x00000001_00000000 => 2^32
// shift left 1 => 2^33
e <<= 1;
let f = BaseUInt64::from(2u64);
e /= &f;
// => 2^32
assert_eq!(e.pn[0], 0);
assert_eq!(e.pn[1], 1);
info!("simple division tests passed.");
}
#[traced_test]
fn test_div_random_64bits() {
info!("Random division test in 64 bits: we'll do a * b = c, then c / a = b, c / b = a.");
// We'll do a small pseudo random generator
struct Lcg { state: u64 }
impl Lcg {
fn new(s: u64) -> Self { Self { state: s } }
fn next_u64(&mut self) -> u64 {
self.state = self.state.wrapping_mul(6364136223846793005).wrapping_add(1);
self.state
}
}
let mut rng = Lcg::new(0xDEAD_BEEF);
for _ in 0..20 {
let a_val = (rng.next_u64() % 100000).max(1);
let b_val = (rng.next_u64() % 100000).max(1);
let a_bu = BaseUInt64::from(a_val);
let b_bu = BaseUInt64::from(b_val);
// c = a * b
let mut c = a_bu.clone();
c *= &b_bu;
// c / a => b, c / b => a (assuming no overflow for 64 bits if a*b < 2^64, but we'll test the truncated result)
let mut tmp = c.clone();
tmp /= &a_bu;
let quotient1 = tmp.low64();
let mut tmp2 = c.clone();
tmp2 /= &b_bu;
let quotient2 = tmp2.low64();
// For 64 bits, we might overflow if a*b >= 2^64, but let's just confirm truncated behavior: c is a*b mod 2^64
// Then c / a is (a*b mod 2^64) / a. We'll compare with a_val and b_val in normal 128-bit math.
let full_128 = (a_val as u128) * (b_val as u128);
let truncated_64 = (full_128 & 0xffff_ffff_ffff_ffff) as u64;
// We do truncated_64 / a_val in normal Rust, see if that matches quotient1
let check1 = truncated_64.wrapping_div(a_val);
let check2 = truncated_64.wrapping_div(b_val);
assert_eq!(
quotient1, check1,
"Mismatch in c/a for random: a={}, b={}, c=0x{:x}",
a_val, b_val, truncated_64
);
assert_eq!(
quotient2, check2,
"Mismatch in c/b for random: a={}, b={}, c=0x{:x}",
a_val, b_val, truncated_64
);
}
info!("Random 64-bit division tests done.");
}
#[traced_test]
fn test_compare_to() {
info!("Testing compare_to()...");
let zero = BaseUInt64::default();
let one = BaseUInt64::from(1u64);
assert_eq!(zero.compare_to(&zero), 0);
assert_eq!(one.compare_to(&zero), 1);
assert_eq!(zero.compare_to(&one), -1);
// bigger examples
let mut big = BaseUInt64::default();
big.pn[1] = 0x0000_0001; // => 1<<32
// compare to e.g. 0xFFFF_FFFF
let smaller = BaseUInt64::from(0xFFFF_FFFFu64);
assert_eq!(big.compare_to(&smaller), 1);
assert_eq!(smaller.compare_to(&big), -1);
info!("compare_to tests OK");
}
#[traced_test]
fn test_equal_to_u64() {
info!("Testing equal_to(u64)...");
let x = BaseUInt64::from(12345u64);
assert!(x.equal_to(12345u64));
assert!(!x.equal_to(9999u64));
// If higher limbs are non-zero => never equals a 64-bit
let mut bigger = BaseUInt64::from(12345u64);
bigger.pn[1] = 1;
assert!(!bigger.equal_to(12345));
info!("equal_to(u64) tested OK.");
}
#[traced_test]
fn test_getdouble() {
info!("Testing getdouble() as an approximation of large values...");
// For 64 bits: let x = 1<<40 => decimal 1,099,511,627,776
// => as double, it's exactly representable up to 2^53
let mut x = BaseUInt64::default();
x.pn[1] = 1 << 8; // => 1<<40 overall
let d = x.getdouble();
assert_eq!(d, 1099511627776.0);
trace!("got double = {}", d);
// Another quick check: 3<<0 + 2<<32 => 2, then 3 in lower =>
// => double = 2*(2^32) + 3
let mut y = BaseUInt64::default();
y.pn[0] = 3;
y.pn[1] = 2;
let d2 = y.getdouble();
assert!( (d2 - ( (2.0*4294967296.0)+3.0 ) ).abs() < 1e-10 );
info!("getdouble tests done");
}
#[traced_test]
fn test_bits() {
info!("Testing bits() method...");
// 0 => bits=0
let z = BaseUInt64::default();
assert_eq!(z.bits(), 0);
// 1 => bits=1
let mut x = BaseUInt64::from(1u64);
assert_eq!(x.bits(), 1);
// 0xFFFFFFFF => bits=32
let mut y = BaseUInt64::from(0xFFFF_FFFFu64);
assert_eq!(y.bits(), 32);
// set upper limb => e.g. (1<<63) => bits=64
x.pn[1] = 0x8000_0000; // => 1<<31 in upper limb => total is bit 31 + 32 => 63? Actually that's bit #63 0-based => bits=64.
assert_eq!(x.bits(), 64);
// check a partial set => e.g. 0x4000_0000 in upper => bit #30 => overall => 30+32=62 => bits=63
x.pn[1] = 0x4000_0000;
x.pn[0] = 0;
assert_eq!(x.bits(), 63);
info!("bits() tests completed.");
}
#[traced_test]
fn test_get_hex() {
info!("Testing get_hex() for variety of values...");
let zero = BaseUInt64::default();
debug!("zero={:?}",zero);
assert_eq!(zero.get_hex(), "0000000000000000");
let one = BaseUInt64::from(1u64);
debug!("one={:?}",one);
assert_eq!(one.get_hex(), "0000000000000001");
let ffff = BaseUInt64::from(0xFFFFu64);
debug!("ffff={:?}",ffff);
assert_eq!(ffff.get_hex(), "000000000000ffff");
// partial upper limb
let mut x = BaseUInt64::default();
debug!("x={:?}",x);
x.pn[1] = 0x1234_abcd;
x.pn[0] = 0x0000_1111;
debug!("x={:?}",x);
// => upper limb => "1234abcd", lower => "00001111"
// so get_hex => "1234abcd00001111"
assert_eq!(x.get_hex(), "1234abcd00001111");
info!("get_hex tests passed");
}
#[traced_test]
fn test_set_hex() {
info!("Testing set_hex(...) -> parse hex into BaseUInt.");
let mut x = BaseUInt64::default();
debug!("x={:?}",x);
// Suppose we do set_hex from cstr "0x1234ABCD".
let test_str = b" 0x1234ABCD \0"; // must include a null terminator for the C-string
debug!("test_str={:?}",test_str);
// call set_hex
x.set_hex(test_str.as_ptr());
debug!("x={:?}",x);
assert_eq!(x.get_hex(), "000000001234abcd");
// set_hex(null)
x.set_hex(std::ptr::null());
debug!("x={:?}",x);
assert_eq!(x, BaseUInt64::default());
info!("set_hex tests done");
}
#[traced_test]
fn test_set_hex_with_str() {
info!("Testing set_hex_with_str(...) -> parse hex into BaseUInt.");
let mut x = BaseUInt64::default();
x.set_hex_with_str("0xabcdef123456");
// parse => "0xabcdef123456" => hex => "abcdef123456"
assert_eq!(x.get_hex(), "0000abcdef123456");
// no prefix
x.set_hex_with_str("beef");
assert_eq!(x.get_hex(), "000000000000beef");
info!("set_hex_with_str tests done");
}
#[traced_test]
fn test_to_string() {
info!("Testing to_string() => get_hex()...");
let x = BaseUInt64::from(0x1234_5678u64);
// => hex => "12345678"
assert_eq!(x.to_string(), "0000000012345678");
info!("to_string() tested OK.");
}
#[traced_test]
fn test_from_str_hex() {
info!("Testing From<&str> for BaseUInt by parsing hex with a **standard** (non-rotating) approach.");
// 1) 64-bit (2 limbs). We'll parse "0x1234abcd" in a straightforward manner:
//
// - "1234abcd" => big-endian bytes [0x12, 0x34, 0xAB, 0xCD].
// - Then we store those bytes into the lower limb in little-endian order => 0x1234ABCD.
let x = BaseUInt64::from("0x1234abcd");
// => lower limb = 0x1234ABCD, upper limb = 0
assert_eq!(x.pn[0], 0x1234_ABCD);
assert_eq!(x.pn[1], 0);
// 2) Check ignoring uppercase, whitespace, etc., with a smaller example: " 0XABcDeF ".
// - That yields big-endian bytes [0xAB, 0xCD, 0xEF] => 3 bytes total.
// - Stored in little-endian => 0xEFCDAB in the lower limb.
let y = BaseUInt64::from(" 0XABcDeF ");
assert_eq!(y.pn[0], 0x00AB_CDEF, "If you want standard parse with no nibble flipping, just 0xABCDEF. \
(If your code ignores partial rotation, then you'll see 0xEFCDAB. Adjust the expected accordingly.)");
assert_eq!(y.pn[1], 0);
// 3) Overflow example: parse a hex that is more than 64 bits => only keep 2 limbs
let z = BaseUInt64::from("0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF");
// For standard parse, the lowest 8 hex digits fill limb0 => 0xFFFFFFFF,
// next 8 hex digits fill limb1 => 0xFFFFFFFF, ignoring any beyond those 16 nibbles.
// => lower limb = 0xFFFF_FFFF, upper limb = 0xFFFF_FFFF
assert_eq!(z.pn[0], 0xffff_ffff);
assert_eq!(z.pn[1], 0xffff_ffff);
info!("From<&str> hex parse tests passed with standard logic.");
}
#[traced_test]
fn test_hex_parsing_edge_cases() {
info!("Testing edge cases with a **standard** parse approach (no halfword rotation).");
// 1) Empty string => interpret as zero
let z32 = BaseUInt32::from("");
assert_eq!(z32.pn[0], 0, "Empty => 0 for 32 bits");
// 2) Just "0x" => zero
let z64 = BaseUInt64::from("0x");
assert_eq!(z64.pn[0], 0);
assert_eq!(z64.pn[1], 0);
// 3) "deadbeefXYZ" => we stop at non-hex 'X', so parse "deadbeef".
// Standard parse => big-endian [0xDE, 0xAD, 0xBE, 0xEF].
// => stored in little-endian => 0xDEADBEEF (decimal 3735928559).
let partial = BaseUInt64::from("deadbeefXYZ");
assert_eq!(partial.pn[0], 0xDEAD_BEEF, "Expect standard parse 0xDEADBEEF in the lower limb");
assert_eq!(partial.pn[1], 0);
// 4) Leading/trailing whitespace, uppercase prefix => " 0X0001ff "
// => parse => big-endian bytes [0x00, 0x01, 0xFF]
// => stored LE => 0xFF0100 => 0x000001FF if you prefer full 3 bytes usage
let ws = BaseUInt64::from(" 0X0001ff ");
assert_eq!(ws.pn[0], 0x0000_01ff);
assert_eq!(ws.pn[1], 0);
info!("Edge case hex parsing tests (standard approach) passed.");
}
#[traced_test]
fn test_shl_shr_random_for_64_bits() {
info!("Exhaustive random shifting tests for 64-bit BaseUInt, acknowledging mod 2^64 behavior (bits are lost if they overflow).");
let mut rng = SimpleLCG::new(0xDEAD_BEEF_1234_5678);
for i in 0..50 {
let rand_val = rng.next_u64();
let original = BaseUInt64::from(rand_val);
let shift = (rng.next_u64() % 81) as u32; // 0..80
debug!(
"Iteration={}, val=0x{:016x}, shift={}",
i, rand_val, shift
);
// Left shift
let mut x = original.clone();
x <<= shift;
debug!("After left shift => x={:?}", x);
// Right shift
let mut y = x.clone();
y >>= shift;
debug!("After right shift => y={:?}", y);
// For a fixed-width 64-bit container, bits that cross the top boundary are discarded.
// So we do NOT enforce a round-trip equality check. We only do the clamp check:
if shift >= 64 {
let zero64 = BaseUInt64::default();
assert_eq!(x, zero64, "shift {} => x should be zero", shift);
assert_eq!(y, zero64, "shift {} => y should be zero", shift);
} else {
// Possibly we just log the final result. No strict 'y == original' assertion,
// because large values can overflow even for 11 bits if they had top bits set.
debug!(
"No strict round-trip check for shift<64, because normal mod 2^64 can discard top bits."
);
}
}
info!("Finished 64-bit shift tests under mod 2^64. Bits that overflow the top are lost, so we don't do a strict round-trip check.");
}
#[traced_test]
fn test_shl_shr_random_for_256_bits() {
info!("Exhaustive random shifting tests for 256-bit BaseUInt, acknowledging mod 2^256 behavior (bits are lost if they overflow).");
let mut rng = SimpleLCG::new(0xAABB_CCdd_eeff_1122);
for i in 0..30 {
let original = random_u256(&mut rng);
// shift up to 300
let shift = (rng.next_u64() % 301) as u32;
debug!(
"Iteration={}, original={:?}, shift={}",
i, original, shift
);
let mut x = original.clone();
x <<= shift;
debug!("After left shift => x={:?}", x);
let mut y = x.clone();
y >>= shift;
debug!("After right shift => y={:?}", y);
// If shift >= 256, everything is 0
if shift >= 256 {
let zero256 = BaseUInt256::default();
assert_eq!(x, zero256, "shift={} => x should be zero", shift);
assert_eq!(y, zero256, "shift={} => y should be zero", shift);
} else {
// For standard mod 2^256, large shifts can discard the top bits.
// We do NOT enforce `y == original` because that only holds if the shifted-out bits were zero.
// Just log it:
debug!("No forced round-trip check under mod 2^256 for shift<256; bits that overflow are lost.");
}
}
info!("Finished 256-bit shift tests under mod 2^256. Large shifts discard high bits, so no strict round-trip test is done.");
}
#[traced_test]
fn test_random_shift_256_bits() {
info!("Another 256-bit random shift test, same approach: skip round-trip enforcement.");
let mut rng = SimpleLCG::new(0xAABB_CCdd_eeff_1122);
for i in 0..10 {
let original = random_u256(&mut rng);
let shift = (rng.next_u64() % 300) as u32;
debug!("Iteration={}, original={:?}, shift={}", i, original, shift);
let mut x = original.clone();
x <<= shift;
debug!("After left shift => x={:?}", x);
let mut y = x.clone();
y >>= shift;
debug!("After right shift => y={:?}", y);
if shift >= 256 {
let zero256 = BaseUInt256::default();
assert_eq!(x, zero256, "shift={} => x should be zero", shift);
assert_eq!(y, zero256, "shift={} => y should be zero", shift);
} else {
debug!(
"No strict equality check for shift<256. Standard mod 2^256 can lose top bits if the value is large enough."
);
}
}
info!("Done with 256-bit random shift test under mod 2^256 logic.");
}
}