-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbignum.h
4020 lines (3836 loc) · 113 KB
/
bignum.h
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
/*
* Copyright (c) 2024 Stefan Uhrig
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
//------------------------------------------------------------------------------
/*******************************************************************************
* @mainpage A C++-11 single-header generic arbitrary-precision number library
*
* This is a C++-11 compliant generic single-header arbitrary-precision number
* library offering natural (@ref bn::Unsigned), integer (@ref bn::Signed) and
* rational (@ref bn::Rational) number types and corresponding mathematical
* operations.
*
* Design goals of this library are
* - intuitive and easy usage
* - providing all functionality in a single header so that it can easily be
* consumed by C++ projects
* - maximum compatibility with C++-11 compliant compilers and all platforms
*
* This library does not contain optimized algorithms for any particular
* platform and uses mainly naive "schoolbook" methods. With regards to
* performance, it cannot compete with mature arbitrary-precision libraries like
* GMP.
*
* The given time complexity is the worst case complexity. n is the number of
* digits in a number. If multiple numbers are involved in an operation, n is
* chosen to be the number of digits in the longest number.
*
* If you'd like to improve the performance on a particular architecture, you
* can customize the digit type and the seven primitive operations on which all
* algorithms are based.
******************************************************************************/
//------------------------------------------------------------------------------
#ifndef BN_BIGNUM_H
#define BN_BIGNUM_H
//------------------------------------------------------------------------------
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <new>
#include <ostream>
#include <random>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
//------------------------------------------------------------------------------
namespace bn {
//------------------------------------------------------------------------------
namespace impl {
//------------------------------------------------------------------------------
// Digit type.
//
// If your platform offers wider types, widening the type might improve
// performance.
//------------------------------------------------------------------------------
#if defined DIGIT_T && defined DDIGIT_T
using digit_t = DIGIT_T;
#else
using digit_t = std::uint32_t;
#endif
//------------------------------------------------------------------------------
static_assert(std::is_unsigned<digit_t>::value, "digit_t must be unsigned");
static_assert(!std::is_const<digit_t>::value, "digit_t must not be const");
static_assert(!std::is_same<bool, digit_t>::value, "digit_t must not be bool");
//------------------------------------------------------------------------------
constexpr unsigned bitsPerDigit = 8 * sizeof(digit_t);
//------------------------------------------------------------------------------
inline constexpr unsigned computeMaxDecDigitsPerDigit(digit_t val)
{
return (val >= 10) ? 1 + computeMaxDecDigitsPerDigit(val / 10) : 0;
}
constexpr unsigned maxDecDigitsPerDigit =
computeMaxDecDigitsPerDigit(std::numeric_limits<digit_t>::max());
//------------------------------------------------------------------------------
inline constexpr digit_t computeMaxPow10PerDigit(digit_t val)
{
return (val >= 10) ? 10 * computeMaxPow10PerDigit(val / 10) : 1;
}
constexpr digit_t maxPow10PerDigit =
computeMaxPow10PerDigit(std::numeric_limits<digit_t>::max());
//------------------------------------------------------------------------------
// The seven primitive operations on which all algorithms are based.
//------------------------------------------------------------------------------
/*
* Computes a + b + carry.
*
* @param a A digit.
* @param b A digit.
* @param carry The carry flag. Must be updated to reflect whether the addition
* overflowed.
* @return Returns the result of a + b + carry.
*/
digit_t addCarry(digit_t a, digit_t b, bool& carry);
/*
* Computes a - b - borrow.
*
* @param a A digit.
* @param b A digit.
* @param borrow The borrow flag. Must be updated to reflect whether the
* subtraction underflowed.
* @return Returns the result of a - b - borrow.
*/
digit_t subBorrow(digit_t a, digit_t b, bool& borrow);
/*
* Computes a*b + carry.
*
* @param a A digit.
* @param b A digit.
* @param carry The carry. Must be updated to the new carry value, which is
* (a*b + carry) / base.
* @return Returns (a*b + carry) % base.
*/
digit_t multiplyAdd(digit_t a, digit_t b, digit_t& carry);
/*
* Computes a*b + c + carry.
*
* @param a A digit.
* @param b A digit.
* @param c A digit.
* @param carry The carry. Must be updated to the new carry value, which is
* (a*b + c + carry) / base.
* @return Returns (a*b + c + carry) % base.
*/
digit_t multiplyAdd2(digit_t a, digit_t b, digit_t c, digit_t& carry);
/*
* Computes (remainder*base + a) / b.
*
* The quotient will fit into one digit.
*
* @param a
* @param b
* @param remainder The remainder. Must be updated to the new remainder value,
* which is ((remainder*base + a) % b.
* @return Returns the quotient, which (remainder*base + a) / b.
*/
digit_t divideRemainder(digit_t a, digit_t b, digit_t& remainder);
/*
* Counts the number of leading zero bits in the passed digit.
*
* @param val A digit. Will not be 0.
* @return Returns the number of leading zero bits in the passed digit.
*/
std::size_t countLeadingZeroes(digit_t val);
/*
* Counts the number of trailing zero bits in the passed digit.
*
* @param val A digit. Will not be 0.
* @return Returns the number of trailing zero bits in the passed digit.
*/
std::size_t countTrailingZeroes(digit_t val);
//------------------------------------------------------------------------------
template<typename T>
typename std::enable_if<std::is_unsigned<T>::value, std::size_t>::type
countLeadingZeroes(T val);
//------------------------------------------------------------------------------
template<typename T>
typename std::enable_if<std::is_unsigned<T>::value, std::size_t>::type
countTrailingZeroes(T val);
//------------------------------------------------------------------------------
class Store;
//------------------------------------------------------------------------------
} // namespace impl
//------------------------------------------------------------------------------
template<typename T>
struct EnableUserDefinedIntegral;
class Unsigned;
class Signed;
class Rational;
//------------------------------------------------------------------------------
namespace impl {
//------------------------------------------------------------------------------
// class Store
//------------------------------------------------------------------------------
class Store final
{
public:
static constexpr std::size_t smemsize =
(64 - 2 * sizeof(std::size_t) - sizeof(void*)) / sizeof(impl::digit_t);
public:
Store() noexcept;
Store(const Store& other);
Store(Store&& other) noexcept;
~Store();
Store& operator=(const Store& other);
Store& operator=(Store&& other) noexcept;
public:
std::size_t size() const noexcept;
const impl::digit_t& operator[](std::size_t i) const noexcept;
impl::digit_t& operator[](std::size_t i) noexcept;
void resize(std::size_t newsize);
private:
static void deallocate(void* ptr);
static impl::digit_t* alloc_digits(std::size_t count);
private:
impl::digit_t smem[smemsize];
impl::digit_t* mem;
std::size_t cap;
std::size_t sz;
};
//------------------------------------------------------------------------------
} // namespace impl
//------------------------------------------------------------------------------
/*******************************************************************************
* A natural number of arbitrary precision.
******************************************************************************/
class Unsigned final
{
public:
/**
* Default constructor.
*
* The number is initialized to 0.
*
* @par Runtime complexity
* O(1)
*/
Unsigned() noexcept;
/**
* Copy constructor.
*
* @param other The number to copy.
*
* @par Runtime complexity
* O(n)
*/
Unsigned(const Unsigned& other);
/**
* Move constructor.
*
* @param other The number to move.
*
* @par Runtime complexity
* O(1)
*/
Unsigned(Unsigned&& other) noexcept;
/**
* Constructs a number from signed 32-bit integer.
*
* @param i The integer to construct the number from.
*
* @exception std::invalid_argument Thrown if i is negative.
*
* @par Runtime complexity
* O(1)
*/
Unsigned(std::int32_t i);
/**
* Constructs a number from an unsigned 32-bit integer.
*
* @param i The unsigned integer to construct the number from.
*
* @par Runtime complexity
* O(1)
*/
Unsigned(std::uint32_t i);
/**
* Constructs a number from signed 64-bit integer.
*
* @param i The integer to construct the number from. Will throw a
*
* @exception std::invalid_argument Thrown if i is negative.
*
* @par Runtime complexity
* O(1)
*/
Unsigned(std::int64_t i);
/**
* Constructs a number from an unsigned 64-bit integer.
*
* @param i The unsigned integer to construct the number from.
*
* @par Runtime complexity
* O(1)
*/
Unsigned(std::uint64_t i);
/**
* Constructs a number from a user-defined integral.
*
* @tparam T The type to create the number from. The type must be right-
shiftable and castable to bn::impl::digit_t.
* @param i The integral to create the number from. Must not be negative.
*/
template<
typename T,
typename std::enable_if<EnableUserDefinedIntegral<T>::value, bool>::
type = true>
explicit Unsigned(T i);
/**
* Constructs a number from a null-terminated string.
*
* @param dec The null-terminated string to construct the number from.
*
* @exception std::invalid_argument Thrown if the passed string is empty or
* contains a character that is not a
* digit from 0 to 9.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned(const char* dec);
/**
* Copy assigns another number to this number.
*
* @param other Another number.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned& operator=(const Unsigned& other);
/**
* Move assigns another number to this number.
*
* @param other Another number.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(1)
*/
Unsigned& operator=(Unsigned&& other) noexcept;
/**
* Generates a number consisting of the given number of random bits.
*
* The number is not guaranteed to have the given number of bits as the
* random leading bits could be 0.
*
* @tparam Generator The type of random engine to use (e.g. std::mt19937).
* @param numBits The number of random bits to generate.
* @param gen Reference to the random engine to use.
* @return Returns the generated random number.
*
* @par Runtime complexity
* O(n)
*/
template<typename Generator>
static Unsigned random(std::size_t numBits, Generator& gen);
public:
/**
* Pre-increment operator.
*
* Increases this number by 1.
*
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned& operator++();
/**
* Post-increment operator.
*
* Increases this number by 1.
*
* @return Returns the value of the number before increasing.
*
* @par Runtime complexity
* O(n)
*/
Unsigned operator++(int);
/**
* Pre-decrement operator.
*
* Decreases this number by 1.
*
* @return Returns a reference to this number.
*
* @exception std::logic_error Thrown if this number is 0.
*
* @par Runtime complexity
* O(n)
*/
Unsigned& operator--();
/**
* Post-decrement operator.
*
* Decreases this number by 1.
*
* @return Returns the value of the number before decreasing.
*
* @exception std::logic_error Thrown if this number is 0.
*
* @par Runtime complexity
* O(n)
*/
Unsigned operator--(int);
/**
* Bitwise OR operator.
*
* @param v Number to OR this number with.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned& operator|=(const Unsigned& v);
/**
* Bitwise AND operator.
*
* @param v Number to AND this number with.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned& operator&=(const Unsigned& v);
/**
* Bitwise XOR operator.
*
* @param v Number to XOR this number with.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned& operator^=(const Unsigned& v);
/**
* Bitwise left shift.
*
* @param s The number of bits to shift to the left.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned& operator<<=(std::size_t s);
/**
* Bitwise right shift.
*
* @param s The number of bits to shift to the left.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned& operator>>=(std::size_t s);
/**
* Adds the passed number to this number.
*
* @param v The number to add.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned& operator+=(const Unsigned& v);
/**
* Substracts the passed number to this number.
*
* @param v The number to subtract.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n)
*
* @exception std::invalid_argument Thrown if the v is greater than this
* number.
*/
Unsigned& operator-=(const Unsigned& v);
/**
* Multiplies this number with the passed number.
*
* @param v The number to multiply with.
* @return Returns a reference to this number.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned& operator*=(const Unsigned& v);
/**
* Divides this number by the passed number.
*
* @param v The number to divide by.
* @return Returns a reference to this number.
*
* @exception std::invalid_argument Thrown if v is 0.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned& operator/=(const Unsigned& v);
/**
* Computes the remainder when dividing this number by the passed number.
*
* @param v The number to divide by.
* @return Returns a reference to this number.
*
* @exception std::invalid_argument Thrown if v is 0.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned& operator%=(const Unsigned& v);
/**
* Divides this number by the passed number and returns the remainder of the
* division.
*
* @param v The number to divide by.
* @return Returns the number of the division.
*
* @exception std::invalid_argument Thrown if v is 0.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned div(const Unsigned& v);
/**
* Checks whether this number is 0.
*
* @return Returns true if this number is 0, false otherwise.
*
* @par Runtime complexity
* O(1)
*/
bool empty() const;
/**
* Returns the number of bits this number consists of.
*
* The number of bits is equal to the position of the highest bit plus one.
*
* @return Returns the number of bits this number consists of.
*
* @par Runtime complexity
* O(1)
*/
std::size_t bits() const;
/**
* Returns the number of trailing zero bits.
*
* If this number is 0, the function will return 0.
*
* @return Returns the number of trailing zero bits.
*
* @par Runtime complexity
* O(n)
*/
std::size_t ctz() const;
/**
* Converts this number to an unsigned 64-bit integer.
*
* @return This number as an unsigned 64-bit integer.
*
* @exception std::overflow_error Thrown if this number does not fit in an
* unsigned 64-bit integer.
*
* @par Runtime complexity
* O(1)
*/
explicit operator std::uint64_t() const;
/**
* Returns the string representation of this number in base 10.
*
* @return Returns the string representation of this number in base 10.
*
* @par Runtime complexity
* O(n^2)
*/
std::string str() const;
/**
* Returns the number of digits in this number, which is the number of
* bn::impl::digit_t elements in this number.
*
* @return Returns the number of digits in this number, which is the number
* of bn::impl::digit_t elements in this number.
*
* @par Runtime complexity
* O(1)
*/
std::size_t digits() const;
public:
struct QR;
private:
template<int S, typename T>
static typename std::enable_if<S >= 8 * sizeof(T), T>::type
safeRightShift(T val);
template<int S, typename T>
static typename std::enable_if
< S<8 * sizeof(T), T>::type safeRightShift(T val);
template<typename T>
void initFromIntegral(T val);
template<std::size_t BPD, typename Generator>
static typename std::enable_if<(BPD < 8 * sizeof(unsigned)), Unsigned>::type
generateRandom(std::size_t numBits, Generator& gen);
template<std::size_t BPD, typename Generator>
static
typename std::enable_if<(BPD == 8 * sizeof(unsigned)), Unsigned>::type
generateRandom(std::size_t numBits, Generator& gen);
template<std::size_t BPD, typename Generator>
static typename std::enable_if<(BPD > 8 * sizeof(unsigned)), Unsigned>::type
generateRandom(std::size_t numBits, Generator& gen);
void randomGenMaskHighest(std::size_t mb);
std::size_t countLeadingZeroes() const;
void addDigit(impl::digit_t d);
void subtractDigit(impl::digit_t d);
void multiplyByDigit(impl::digit_t d);
impl::digit_t divideByDigitReturnRem(impl::digit_t d);
static impl::digit_t findDivQuotient(
impl::digit_t un,
impl::digit_t un1,
impl::digit_t un2,
impl::digit_t vn1,
impl::digit_t vn2);
static impl::digit_t findDivQuotient(
const Unsigned& u,
std::size_t lz,
impl::digit_t vn1,
impl::digit_t vn2,
std::size_t j);
void removeLeadingZeroDigits();
private:
friend bool operator==(const Unsigned& u, const Unsigned& v);
friend bool operator!=(const Unsigned& u, const Unsigned& v);
friend bool operator<(const Unsigned& u, const Unsigned& v);
friend bool operator>=(const Unsigned& u, const Unsigned& v);
friend bool operator>(const Unsigned& u, const Unsigned& v);
friend bool operator<=(const Unsigned& u, const Unsigned& v);
friend Unsigned operator|(const Unsigned& u, const Unsigned& v);
friend Unsigned operator&(const Unsigned& u, const Unsigned& v);
friend Unsigned operator^(const Unsigned& u, const Unsigned& v);
friend Unsigned operator<<(const Unsigned& u, std::size_t s);
friend Unsigned operator>>(const Unsigned& u, std::size_t s);
friend Unsigned operator+(const Unsigned& u, const Unsigned& v);
friend Unsigned operator-(const Unsigned& u, const Unsigned& v);
friend Unsigned operator*(const Unsigned& u, const Unsigned& v);
friend Unsigned operator/(const Unsigned& u, const Unsigned& v);
friend Unsigned operator%(const Unsigned& u, const Unsigned& v);
friend Unsigned::QR div(const Unsigned& u, const Unsigned& v);
friend Unsigned
powmod(const Unsigned& u, Unsigned exp, const Unsigned& mod);
friend class Rational;
private:
impl::Store digit;
};
/*******************************************************************************
* Result of a division.
******************************************************************************/
struct Unsigned::QR
{
/// The quotient.
Unsigned quot;
/// The remainder.
Unsigned rem;
};
/**
* Equal comparison.
*
* @param u First number.
* @param v Second number.
* @return Returns true if the numbers are equal, false otherwise.
*
* @par Runtime complexity
* O(n)
*/
bool operator==(const Unsigned& u, const Unsigned& v);
/**
* Inequal comparison.
*
* @param u First number.
* @param v Second number.
* @return Returns true if the number are not equal, false otherwise.
*
* @par Runtime complexity
* O(n)
*/
bool operator!=(const Unsigned& u, const Unsigned& v);
/**
* Less than comparison.
*
* @param u First number.
* @param v Second number.
* @return Returns true if the first number is less than the second number,
* false otherwise.
*
* @par Runtime complexity
* O(n)
*/
bool operator<(const Unsigned& u, const Unsigned& v);
/**
* Greater than or equal comparison.
*
* @param u First number.
* @param v Second number.
* @return Returns true if the first number is greater than or equal to the
* second number, false otherwise.
*
* @par Runtime complexity
* O(n)
*/
bool operator>=(const Unsigned& u, const Unsigned& v);
/**
* Greater than comparison.
*
* @param u First number.
* @param v Second number.
* @return Returns true if the first number is greater than the second number,
* false otherwise.
*
* @par Runtime complexity
* O(n)
*/
bool operator>(const Unsigned& u, const Unsigned& v);
/**
* Less than or equal comparison.
*
* @param u First number.
* @param v Second number.
* @return Returns true if the first number is less than or equal to the
* second number, false otherwise.
*
* @par Runtime complexity
* O(n)
*/
bool operator<=(const Unsigned& u, const Unsigned& v);
/**
* Bitwise OR operator.
*
* @param u First number.
* @param v Second number.
* @return Returns the result of a bitwise OR of both numbers.
*
* @par Runtime complexity
* O(n)
*/
Unsigned operator|(const Unsigned& u, const Unsigned& v);
/**
* Bitwise AND operator.
*
* @param u First number.
* @param v Second number.
* @return Returns the result of a bitwise AND of both numbers.
*
* @par Runtime complexity
* O(n)
*/
Unsigned operator&(const Unsigned& u, const Unsigned& v);
/**
* Bitwise XOR operator.
*
* @param u First number.
* @param v Second number.
* @return Returns the result of a bitwise XOR of both numbers.
*
* @par Runtime complexity
* O(n)
*/
Unsigned operator^(const Unsigned& u, const Unsigned& v);
/**
* Bitwise left shift operator.
*
* @param u The number to shift.
* @param s The number of bits to shift to the left.
* @return Returns the shifted number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned operator<<(const Unsigned& u, std::size_t s);
/**
* Bitwise right shift operator.
*
* @param u The number to shift.
* @param s The number of bits to shift to the right.
* @return Returns the shifted number.
*
* @par Runtime complexity
* O(n)
*/
Unsigned operator>>(const Unsigned& u, std::size_t s);
/**
* Adds two numbers.
*
* @param u First summand.
* @param v Second summand.
* @return Returns the sum.
*
* @par Runtime complexity
* O(n)
*/
Unsigned operator+(const Unsigned& u, const Unsigned& v);
/**
* Subtracts a number from a number.
*
* @param u Minuend.
* @param v Subtrahend.
* @return Returns the difference.
*
* @exception std::invalid_argument Thrown if the subtrahend is greater than
* the minuend.
*
* @par Runtime complexity
* O(n)
*/
Unsigned operator-(const Unsigned& u, const Unsigned& v);
/**
* Multiplies two numbers with each other.
*
* @param u First factor.
* @param v Second factor.
* @return Returns the product.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned operator*(const Unsigned& u, const Unsigned& v);
/**
* Divides a number by another.
*
* @param u Divident.
* @param v Divisor.
* @return Returns the quotient.
*
* @exception std::invalid_argument Thrown if the divisor is 0.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned operator/(const Unsigned& u, const Unsigned& v);
/**
* Computes the remainder of a division.
*
* @param u Divident.
* @param v Divisor.
* @return Returns the remainder.
*
* @exception std::invalid_argument Thrown if the divisor is 0.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned operator%(const Unsigned& u, const Unsigned& v);
/**
* Divides a number by another.
*
* @param u Divident.
* @param v Divisor.
* @return Returns the quotient and remainder.
*
* @exception std::invalid_argument Thrown if the divisor is 0.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned::QR div(const Unsigned& u, const Unsigned& v);
/**
* Computes a power using fast exponentation.
*
* @param u The base.
* @param exp The exponent.
* @return Returns the power.
*
* @par Runtime complexity
* O(n^2*exp^3)
*/
Unsigned pow(const Unsigned& u, std::size_t exp);
/**
* Computes a power modulo a number using fast exponentation.
*
* @param u The base.
* @param exp The exponent.
* @param mod The modulus.
* @return Returns the power.
*
* @par Runtime complexity
* O(exp*mod^2)
*/
Unsigned powmod(const Unsigned& u, Unsigned exp, const Unsigned& mod);
/**
* Computes the rounded-down square root.
*
* @param u A number.
* @return The rounded-down square root.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned sqrt(const Unsigned& u);
/**
* Computes the greatest common divisor of two numbers using the Euclidean
* algorithm.
*
* If one of the numbers is 0, the other number is returned.
*
* @param u The first number.
* @param v The seond number.
* @return Returns the greatest common divisor.
*
* @par Runtime complexity
* O(n^2)
*/
Unsigned egcd(const Unsigned& u, const Unsigned& v);
/**
* Computes the greatest common divisor of two numbers using a binary algorithm.
*
* If one of the numbers is 0, the other number is returned.
*
* @param u The first number.
* @param v The seond number.
* @return Returns the greatest common divisor.
*
* @par Runtime complexity