-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBigInteger.cs
1841 lines (1718 loc) · 60.7 KB
/
BigInteger.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace SM23Crypto
{
public class BigInteger
{
internal static readonly int[][] primeLists = new int[64][]
{
new int[8]{ 3, 5, 7, 11, 13, 17, 19, 23 },
new int[5]{ 29, 31, 37, 41, 43 },
new int[5]{ 47, 53, 59, 61, 67 },
new int[4]{ 71, 73, 79, 83 },
new int[4]{ 89, 97, 101, 103 },
new int[4]{ 107, 109, 113, (int) sbyte.MaxValue },
new int[4]{ 131, 137, 139, 149 },
new int[4]{ 151, 157, 163, 167 },
new int[4]{ 173, 179, 181, 191 },
new int[4]{ 193, 197, 199, 211 },
new int[3]{ 223, 227, 229 },
new int[3]{ 233, 239, 241 },
new int[3]{ 251, 257, 263 },
new int[3]{ 269, 271, 277 },
new int[3]{ 281, 283, 293 },
new int[3]{ 307, 311, 313 },
new int[3]{ 317, 331, 337 },
new int[3]{ 347, 349, 353 },
new int[3]{ 359, 367, 373 },
new int[3]{ 379, 383, 389 },
new int[3]{ 397, 401, 409 },
new int[3]{ 419, 421, 431 },
new int[3]{ 433, 439, 443 },
new int[3]{ 449, 457, 461 },
new int[3]{ 463, 467, 479 },
new int[3]{ 487, 491, 499 },
new int[3]{ 503, 509, 521 },
new int[3]{ 523, 541, 547 },
new int[3]{ 557, 563, 569 },
new int[3]{ 571, 577, 587 },
new int[3]{ 593, 599, 601 },
new int[3]{ 607, 613, 617 },
new int[3]{ 619, 631, 641 },
new int[3]{ 643, 647, 653 },
new int[3]{ 659, 661, 673 },
new int[3]{ 677, 683, 691 },
new int[3]{ 701, 709, 719 },
new int[3]{ 727, 733, 739 },
new int[3]{ 743, 751, 757 },
new int[3]{ 761, 769, 773 },
new int[3]{ 787, 797, 809 },
new int[3]{ 811, 821, 823 },
new int[3]{ 827, 829, 839 },
new int[3]{ 853, 857, 859 },
new int[3]{ 863, 877, 881 },
new int[3]{ 883, 887, 907 },
new int[3]{ 911, 919, 929 },
new int[3]{ 937, 941, 947 },
new int[3]{ 953, 967, 971 },
new int[3]{ 977, 983, 991 },
new int[3]{ 997, 1009, 1013 },
new int[3]{ 1019, 1021, 1031 },
new int[3]{ 1033, 1039, 1049 },
new int[3]{ 1051, 1061, 1063 },
new int[3]{ 1069, 1087, 1091 },
new int[3]{ 1093, 1097, 1103 },
new int[3]{ 1109, 1117, 1123 },
new int[3]{ 1129, 1151, 1153 },
new int[3]{ 1163, 1171, 1181 },
new int[3]{ 1187, 1193, 1201 },
new int[3]{ 1213, 1217, 1223 },
new int[3]{ 1229, 1231, 1237 },
new int[3]{ 1249, 1259, 1277 },
new int[3]{ 1279, 1283, 1289 }
};
internal static readonly int[] primeProducts;
private const long IMASK = 4294967295;
private const ulong UIMASK = 4294967295;
private static readonly int[] ZeroMagnitude = new int[0];
private static readonly byte[] ZeroEncoding = new byte[0];
private static readonly BigInteger[] SMALL_CONSTANTS = new BigInteger[17];
public static readonly BigInteger Zero;
public static readonly BigInteger One;
public static readonly BigInteger Two;
public static readonly BigInteger Three;
public static readonly BigInteger Four;
public static readonly BigInteger Ten;
private static readonly byte[] BitLengthTable = new byte[256]
{
(byte) 0,
(byte) 1,
(byte) 2,
(byte) 2,
(byte) 3,
(byte) 3,
(byte) 3,
(byte) 3,
(byte) 4,
(byte) 4,
(byte) 4,
(byte) 4,
(byte) 4,
(byte) 4,
(byte) 4,
(byte) 4,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 5,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 6,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 7,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8,
(byte) 8
};
private const int chunk2 = 1;
private const int chunk8 = 1;
private const int chunk10 = 19;
private const int chunk16 = 16;
private static readonly BigInteger radix2;
private static readonly BigInteger radix2E;
private static readonly BigInteger radix8;
private static readonly BigInteger radix8E;
private static readonly BigInteger radix10;
private static readonly BigInteger radix10E;
private static readonly BigInteger radix16;
private static readonly BigInteger radix16E;
// private static readonly SecureRandom RandomSource = new SecureRandom();
private static readonly int[] ExpWindowThresholds = new int[8]
{
7,
25,
81,
241,
673,
1793,
4609,
int.MaxValue
};
private const int BitsPerByte = 8;
private const int BitsPerInt = 32;
private const int BytesPerInt = 4;
private int[] magnitude;
private int sign;
private int nBits = -1;
private int nBitLength = -1;
private int mQuote;
static BigInteger()
{
BigInteger.Zero = new BigInteger(0, BigInteger.ZeroMagnitude, false);
BigInteger.Zero.nBits = 0;
BigInteger.Zero.nBitLength = 0;
BigInteger.SMALL_CONSTANTS[0] = BigInteger.Zero;
for (uint index = 1; (long)index < (long)BigInteger.SMALL_CONSTANTS.Length; ++index)
BigInteger.SMALL_CONSTANTS[(int)index] = BigInteger.CreateUValueOf((ulong)index);
BigInteger.One = BigInteger.SMALL_CONSTANTS[1];
BigInteger.Two = BigInteger.SMALL_CONSTANTS[2];
BigInteger.Three = BigInteger.SMALL_CONSTANTS[3];
BigInteger.Four = BigInteger.SMALL_CONSTANTS[4];
BigInteger.Ten = BigInteger.SMALL_CONSTANTS[10];
BigInteger.radix2 = BigInteger.ValueOf(2L);
BigInteger.radix2E = BigInteger.radix2.Pow(1);
BigInteger.radix8 = BigInteger.ValueOf(8L);
BigInteger.radix8E = BigInteger.radix8.Pow(1);
BigInteger.radix10 = BigInteger.ValueOf(10L);
BigInteger.radix10E = BigInteger.radix10.Pow(19);
BigInteger.radix16 = BigInteger.ValueOf(16L);
BigInteger.radix16E = BigInteger.radix16.Pow(16);
BigInteger.primeProducts = new int[BigInteger.primeLists.Length];
for (int index1 = 0; index1 < BigInteger.primeLists.Length; ++index1)
{
int[] primeList = BigInteger.primeLists[index1];
int num = primeList[0];
for (int index2 = 1; index2 < primeList.Length; ++index2)
num *= primeList[index2];
BigInteger.primeProducts[index1] = num;
}
}
private static int GetByteLength(int nBits) => (nBits + 8 - 1) / 8;
// public static BigInteger Arbitrary(int sizeInBits) => new BigInteger(sizeInBits, (Random) BigInteger.RandomSource);
private BigInteger(int signum, int[] mag, bool checkMag)
{
if (checkMag)
{
int sourceIndex = 0;
while (sourceIndex < mag.Length && mag[sourceIndex] == 0)
++sourceIndex;
if (sourceIndex == mag.Length)
{
this.sign = 0;
this.magnitude = BigInteger.ZeroMagnitude;
}
else
{
this.sign = signum;
if (sourceIndex == 0)
{
this.magnitude = mag;
}
else
{
this.magnitude = new int[mag.Length - sourceIndex];
Array.Copy((Array)mag, sourceIndex, (Array)this.magnitude, 0, this.magnitude.Length);
}
}
}
else
{
this.sign = signum;
this.magnitude = mag;
}
}
public BigInteger(string value)
: this(value, 10)
{
}
public BigInteger(string str, int radix)
{
if (str.Length == 0)
throw new FormatException("Zero length BigInteger");
NumberStyles style;
int length;
BigInteger bigInteger1;
BigInteger val;
switch (radix)
{
case 2:
style = NumberStyles.Integer;
length = 1;
bigInteger1 = BigInteger.radix2;
val = BigInteger.radix2E;
break;
case 8:
style = NumberStyles.Integer;
length = 1;
bigInteger1 = BigInteger.radix8;
val = BigInteger.radix8E;
break;
case 10:
style = NumberStyles.Integer;
length = 19;
bigInteger1 = BigInteger.radix10;
val = BigInteger.radix10E;
break;
case 16:
style = NumberStyles.AllowHexSpecifier;
length = 16;
bigInteger1 = BigInteger.radix16;
val = BigInteger.radix16E;
break;
default:
throw new FormatException("Only bases 2, 8, 10, or 16 allowed");
}
int num1 = 0;
this.sign = 1;
if (str[0] == '-')
{
if (str.Length == 1)
throw new FormatException("Zero length BigInteger");
this.sign = -1;
num1 = 1;
}
while (num1 < str.Length && int.Parse(str[num1].ToString(), style) == 0)
++num1;
if (num1 >= str.Length)
{
this.sign = 0;
this.magnitude = BigInteger.ZeroMagnitude;
}
else
{
BigInteger bigInteger2 = BigInteger.Zero;
int num2 = num1 + length;
if (num2 <= str.Length)
{
do
{
string s = str.Substring(num1, length);
ulong num3 = ulong.Parse(s, style);
BigInteger uvalueOf = BigInteger.CreateUValueOf(num3);
BigInteger bigInteger3;
switch (radix)
{
case 2:
if (num3 >= 2UL)
throw new FormatException("Bad character in radix 2 string: " + s);
bigInteger3 = bigInteger2.ShiftLeft(1);
break;
case 8:
if (num3 >= 8UL)
throw new FormatException("Bad character in radix 8 string: " + s);
bigInteger3 = bigInteger2.ShiftLeft(3);
break;
case 16:
bigInteger3 = bigInteger2.ShiftLeft(64);
break;
default:
bigInteger3 = bigInteger2.Multiply(val);
break;
}
bigInteger2 = bigInteger3.Add(uvalueOf);
num1 = num2;
num2 += length;
}
while (num2 <= str.Length);
}
if (num1 < str.Length)
{
string s = str.Substring(num1);
BigInteger uvalueOf = BigInteger.CreateUValueOf(ulong.Parse(s, style));
if (bigInteger2.sign > 0)
{
switch (radix)
{
case 2:
case 8:
bigInteger2 = bigInteger2.Add(uvalueOf);
break;
case 16:
bigInteger2 = bigInteger2.ShiftLeft(s.Length << 2);
goto case 2;
default:
bigInteger2 = bigInteger2.Multiply(bigInteger1.Pow(s.Length));
goto case 2;
}
}
else
bigInteger2 = uvalueOf;
}
this.magnitude = bigInteger2.magnitude;
}
}
public BigInteger(byte[] bytes)
: this(bytes, 0, bytes.Length)
{
}
public BigInteger(byte[] bytes, int offset, int length)
{
if (length == 0)
throw new FormatException("Zero length BigInteger");
if ((sbyte)bytes[offset] < (sbyte)0)
{
this.sign = -1;
int num = offset + length;
int index1 = offset;
while (index1 < num && bytes[index1] == byte.MaxValue)
++index1;
if (index1 >= num)
{
this.magnitude = BigInteger.One.magnitude;
}
else
{
int length1 = num - index1;
byte[] bytes1 = new byte[length1];
int index2 = 0;
while (index2 < length1)
bytes1[index2++] = (byte)(~bytes[index1++]);
while (bytes1[--index2] == byte.MaxValue)
bytes1[index2] = (byte)0;
++bytes1[index2];
this.magnitude = BigInteger.MakeMagnitude(bytes1, 0, bytes1.Length);
}
}
else
{
this.magnitude = BigInteger.MakeMagnitude(bytes, offset, length);
this.sign = this.magnitude.Length != 0 ? 1 : 0;
}
}
private static int[] MakeMagnitude(byte[] bytes, int offset, int length)
{
int num1 = offset + length;
int index1 = offset;
while (index1 < num1 && bytes[index1] == (byte)0)
++index1;
if (index1 >= num1)
return BigInteger.ZeroMagnitude;
int length1 = (num1 - index1 + 3) / 4;
int num2 = (num1 - index1) % 4;
if (num2 == 0)
num2 = 4;
if (length1 < 1)
return BigInteger.ZeroMagnitude;
int[] numArray = new int[length1];
int num3 = 0;
int index2 = 0;
for (int index3 = index1; index3 < num1; ++index3)
{
num3 = num3 << 8 | (int)bytes[index3] & (int)byte.MaxValue;
--num2;
if (num2 <= 0)
{
numArray[index2] = num3;
++index2;
num2 = 4;
num3 = 0;
}
}
if (index2 < numArray.Length)
numArray[index2] = num3;
return numArray;
}
public BigInteger(int sign, byte[] bytes)
: this(sign, bytes, 0, bytes.Length)
{
}
public BigInteger(int sign, byte[] bytes, int offset, int length)
{
if (sign < -1 || sign > 1)
throw new FormatException("Invalid sign value");
if (sign == 0)
{
this.sign = 0;
this.magnitude = BigInteger.ZeroMagnitude;
}
else
{
this.magnitude = BigInteger.MakeMagnitude(bytes, offset, length);
this.sign = this.magnitude.Length < 1 ? 0 : sign;
}
}
public BigInteger Abs() => this.sign < 0 ? this.Negate() : this;
private static int[] AddMagnitudes(int[] a, int[] b)
{
int index = a.Length - 1;
int num1 = b.Length - 1;
long num2 = 0;
while (num1 >= 0)
{
long num3 = num2 + ((long)(uint)a[index] + (long)(uint)b[num1--]);
a[index--] = (int)num3;
num2 = (long)((ulong)num3 >> 32);
}
if (num2 != 0L)
{
while (index >= 0 && ++a[index--] == 0)
;
}
return a;
}
public BigInteger Add(BigInteger value)
{
if (this.sign == 0)
return value;
if (this.sign == value.sign)
return this.AddToMagnitude(value.magnitude);
if (value.sign == 0)
return this;
return value.sign < 0 ? this.Subtract(value.Negate()) : value.Subtract(this.Negate());
}
private BigInteger AddToMagnitude(int[] magToAdd)
{
int[] numArray;
int[] b;
if (this.magnitude.Length < magToAdd.Length)
{
numArray = magToAdd;
b = this.magnitude;
}
else
{
numArray = this.magnitude;
b = magToAdd;
}
uint maxValue = uint.MaxValue;
if (numArray.Length == b.Length)
maxValue -= (uint)b[0];
bool checkMag = (uint)numArray[0] >= maxValue;
int[] a;
if (checkMag)
{
a = new int[numArray.Length + 1];
numArray.CopyTo((Array)a, 1);
}
else
a = (int[])numArray.Clone();
return new BigInteger(this.sign, BigInteger.AddMagnitudes(a, b), checkMag);
}
public BigInteger And(BigInteger value)
{
if (this.sign == 0 || value.sign == 0)
return BigInteger.Zero;
int[] numArray1 = this.sign > 0 ? this.magnitude : this.Add(BigInteger.One).magnitude;
int[] numArray2 = value.sign > 0 ? value.magnitude : value.Add(BigInteger.One).magnitude;
bool flag = this.sign < 0 && value.sign < 0;
int[] mag = new int[System.Math.Max(numArray1.Length, numArray2.Length)];
int num1 = mag.Length - numArray1.Length;
int num2 = mag.Length - numArray2.Length;
for (int index = 0; index < mag.Length; ++index)
{
int num3 = index >= num1 ? numArray1[index - num1] : 0;
int num4 = index >= num2 ? numArray2[index - num2] : 0;
if (this.sign < 0)
num3 = ~num3;
if (value.sign < 0)
num4 = ~num4;
mag[index] = num3 & num4;
if (flag)
mag[index] = ~mag[index];
}
BigInteger bigInteger = new BigInteger(1, mag, true);
if (flag)
bigInteger = bigInteger.Not();
return bigInteger;
}
public BigInteger(int sizeInBits, Random random)
{
if (sizeInBits < 0)
throw new ArgumentException("sizeInBits must be non-negative");
this.nBits = -1;
this.nBitLength = -1;
if (sizeInBits == 0)
{
this.sign = 0;
this.magnitude = BigInteger.ZeroMagnitude;
}
else
{
int byteLength = BigInteger.GetByteLength(sizeInBits);
byte[] numArray = new byte[byteLength];
random.NextBytes(numArray);
int num = 8 * byteLength - sizeInBits;
numArray[0] &= (byte)((uint)byte.MaxValue >> num);
this.magnitude = BigInteger.MakeMagnitude(numArray, 0, numArray.Length);
this.sign = this.magnitude.Length < 1 ? 0 : 1;
}
}
public static int BitCnt(int i)
{
uint num1 = (uint)i;
uint num2 = num1 - (num1 >> 1 & 1431655765U);
uint num3 = (uint)(((int)num2 & 858993459) + ((int)(num2 >> 2) & 858993459));
uint num4 = (uint)((int)num3 + (int)(num3 >> 4) & 252645135);
uint num5 = num4 + (num4 >> 8);
return (int)(num5 + (num5 >> 16) & 63U);
}
private static int CalcBitLength(int sign, int indx, int[] mag)
{
for (; indx < mag.Length; ++indx)
{
if (mag[indx] != 0)
{
int num1 = 32 * (mag.Length - indx - 1);
int w = mag[indx];
int num2 = num1 + BigInteger.BitLen(w);
if (sign < 0 && (w & -w) == w)
{
while (++indx < mag.Length)
{
if (mag[indx] != 0)
goto label_8;
}
--num2;
}
label_8:
return num2;
}
}
return 0;
}
public int BitLength
{
get
{
if (this.nBitLength == -1)
this.nBitLength = this.sign == 0 ? 0 : BigInteger.CalcBitLength(this.sign, 0, this.magnitude);
return this.nBitLength;
}
}
internal static int BitLen(int w)
{
uint index1 = (uint)w;
uint index2 = index1 >> 24;
if (index2 != 0U)
return 24 + (int)BigInteger.BitLengthTable[(int)index2];
uint index3 = index1 >> 16;
if (index3 != 0U)
return 16 + (int)BigInteger.BitLengthTable[(int)index3];
uint index4 = index1 >> 8;
return index4 != 0U ? 8 + (int)BigInteger.BitLengthTable[(int)index4] : (int)BigInteger.BitLengthTable[(int)index1];
}
private bool QuickPow2Check() => this.sign > 0 && this.nBits == 1;
public int CompareTo(object obj) => this.CompareTo((BigInteger)obj);
private static int CompareTo(int xIndx, int[] x, int yIndx, int[] y)
{
while (xIndx != x.Length && x[xIndx] == 0)
++xIndx;
while (yIndx != y.Length && y[yIndx] == 0)
++yIndx;
return BigInteger.CompareNoLeadingZeroes(xIndx, x, yIndx, y);
}
private static int CompareNoLeadingZeroes(int xIndx, int[] x, int yIndx, int[] y)
{
int num1 = x.Length - y.Length - (xIndx - yIndx);
if (num1 != 0)
return num1 >= 0 ? 1 : -1;
while (xIndx < x.Length)
{
uint num2 = (uint)x[xIndx++];
uint num3 = (uint)y[yIndx++];
if ((int)num2 != (int)num3)
return num2 >= num3 ? 1 : -1;
}
return 0;
}
public int CompareTo(BigInteger value)
{
if (this.sign < value.sign)
return -1;
if (this.sign > value.sign)
return 1;
return this.sign != 0 ? this.sign * BigInteger.CompareNoLeadingZeroes(0, this.magnitude, 0, value.magnitude) : 0;
}
private int[] Divide(int[] x, int[] y)
{
int index1 = 0;
while (index1 < x.Length && x[index1] == 0)
++index1;
int index2 = 0;
while (index2 < y.Length && y[index2] == 0)
++index2;
int num1 = BigInteger.CompareNoLeadingZeroes(index1, x, index2, y);
int[] a;
if (num1 > 0)
{
int num2 = BigInteger.CalcBitLength(1, index2, y);
int num3 = BigInteger.CalcBitLength(1, index1, x);
int n1 = num3 - num2;
int start = 0;
int index3 = 0;
int num4 = num2;
int[] numArray1;
int[] numArray2;
if (n1 > 0)
{
numArray1 = new int[(n1 >> 5) + 1];
numArray1[0] = 1 << n1 % 32;
numArray2 = BigInteger.ShiftLeft(y, n1);
num4 += n1;
}
else
{
numArray1 = new int[1] { 1 };
int length = y.Length - index2;
numArray2 = new int[length];
Array.Copy((Array)y, index2, (Array)numArray2, 0, length);
}
a = new int[numArray1.Length];
label_11:
if (num4 < num3 || BigInteger.CompareNoLeadingZeroes(index1, x, index3, numArray2) >= 0)
{
BigInteger.Subtract(index1, x, index3, numArray2);
BigInteger.AddMagnitudes(a, numArray1);
while (x[index1] == 0)
{
if (++index1 == x.Length)
return a;
}
num3 = 32 * (x.Length - index1 - 1) + BigInteger.BitLen(x[index1]);
if (num3 <= num2)
{
if (num3 < num2)
return a;
num1 = BigInteger.CompareNoLeadingZeroes(index1, x, index2, y);
if (num1 <= 0)
goto label_30;
}
}
int n2 = num4 - num3;
if (n2 == 1 && (uint)numArray2[index3] >> 1 > (uint)x[index1])
++n2;
if (n2 < 2)
{
BigInteger.ShiftRightOneInPlace(index3, numArray2);
--num4;
BigInteger.ShiftRightOneInPlace(start, numArray1);
}
else
{
BigInteger.ShiftRightInPlace(index3, numArray2, n2);
num4 -= n2;
BigInteger.ShiftRightInPlace(start, numArray1, n2);
}
while (numArray2[index3] == 0)
++index3;
while (numArray1[start] == 0)
++start;
goto label_11;
}
else
a = new int[1];
label_30:
if (num1 == 0)
{
BigInteger.AddMagnitudes(a, BigInteger.One.magnitude);
Array.Clear((Array)x, index1, x.Length - index1);
}
return a;
}
public BigInteger[] DivideAndRemainder(BigInteger val)
{
if (val.sign == 0)
throw new ArithmeticException("Division by zero error");
BigInteger[] bigIntegerArray = new BigInteger[2];
if (this.sign == 0)
{
bigIntegerArray[0] = BigInteger.Zero;
bigIntegerArray[1] = BigInteger.Zero;
}
else if (val.QuickPow2Check())
{
int n = val.Abs().BitLength - 1;
BigInteger bigInteger = this.Abs().ShiftRight(n);
int[] mag = this.LastNBits(n);
bigIntegerArray[0] = val.sign == this.sign ? bigInteger : bigInteger.Negate();
bigIntegerArray[1] = new BigInteger(this.sign, mag, true);
}
else
{
int[] numArray = (int[])this.magnitude.Clone();
int[] mag = this.Divide(numArray, val.magnitude);
bigIntegerArray[0] = new BigInteger(this.sign * val.sign, mag, true);
bigIntegerArray[1] = new BigInteger(this.sign, numArray, true);
}
return bigIntegerArray;
}
public override bool Equals(object obj)
{
if (obj == this)
return true;
return obj is BigInteger x && this.sign == x.sign && this.IsEqualMagnitude(x);
}