int.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /VSS_Sync/wwlib/int.h $*
  25. * *
  26. * $Author:: Vss_sync $*
  27. * *
  28. * $Modtime:: 3/21/01 12:01p $*
  29. * *
  30. * $Revision:: 8 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if _MSC_VER >= 1000
  36. #pragma once
  37. #endif // _MSC_VER >= 1000
  38. #ifndef INT_H
  39. #define INT_H
  40. #include "mpmath.h"
  41. #include "straw.h"
  42. #include <assert.h>
  43. #include <limits.h>
  44. #include <memory.h>
  45. #ifdef __BORLANDC__
  46. #pragma warn -inl
  47. #endif
  48. template<class T> struct RemainderTable;
  49. template<class T> T Gcd(const T & a, const T & n);
  50. #ifdef _UNIX
  51. #define FN_TEMPLATE <>
  52. #else
  53. #define FN_TEMPLATE
  54. #endif
  55. template<int PRECISION>
  56. class Int {
  57. public:
  58. /*
  59. ** Constructors and initializers.
  60. */
  61. Int(void) {XMP_Init(&reg[0], 0, PRECISION);}
  62. Int(unsigned long value) {XMP_Init(&reg[0], value, PRECISION);}
  63. void Randomize(Straw & rng, int bitcount) {XMP_Randomize(&reg[0], rng, bitcount, PRECISION);}
  64. void Randomize(Straw & rng, const Int & minval, const Int & maxval) {XMP_Randomize_Bounded(&reg[0], rng, minval, maxval, PRECISION); reg[0] |= 1;}
  65. /*
  66. ** Convenient conversion operators to get at the underlying array of
  67. ** integers. Big number math is basically manipulation of arbitrary
  68. ** length arrays.
  69. */
  70. operator digit * () {return & reg[0];}
  71. operator const digit * () const {return & reg[0];}
  72. /*
  73. ** Array access operator (references bit position). Bit 0 is the first bit.
  74. */
  75. bool operator[](unsigned bit) const {return(XMP_Test_Bit(&reg[0], bit));}
  76. /*
  77. ** Unary operators.
  78. */
  79. Int & operator ++ (void) {XMP_Inc(&reg[0], PRECISION);return(*this);}
  80. Int & operator -- (void) {XMP_Dec(&reg[0], PRECISION);return(*this);}
  81. int operator ! (void) const {return(XMP_Test_Eq_Int(&reg[0], 0, PRECISION));}
  82. Int operator ~ (void) {XMP_Not(&reg[0], PRECISION);return(*this);}
  83. Int operator - (void) const {Int a = *this;a.Negate();return (a);}
  84. /*
  85. ** Attribute query functions.
  86. */
  87. int ByteCount(void) const {return(XMP_Count_Bytes(&reg[0], PRECISION));}
  88. int BitCount(void) const {return(XMP_Count_Bits(&reg[0], PRECISION));}
  89. bool Is_Negative(void) const {return(XMP_Is_Negative(&reg[0], PRECISION));}
  90. unsigned MaxBitPrecision() const {return PRECISION*(sizeof(unsigned long)*CHAR_BIT);}
  91. bool IsSmallPrime(void) const {return(XMP_Is_Small_Prime(&reg[0], PRECISION));}
  92. bool SmallDivisorsTest(void) const {return(XMP_Small_Divisors_Test(&reg[0], PRECISION));}
  93. bool FermatTest(unsigned rounds) const {return(XMP_Fermat_Test(&reg[0], rounds, PRECISION));}
  94. bool IsPrime(void) const {return(XMP_Is_Prime(&reg[0], PRECISION));}
  95. bool RabinMillerTest(Straw & rng, unsigned int rounds) const {return(XMP_Rabin_Miller_Test(rng, &reg[0], rounds, PRECISION));}
  96. /*
  97. ** 'in-place' binary operators.
  98. */
  99. Int & operator += (const Int & number) {Carry = XMP_Add(&reg[0], &reg[0], number, 0, PRECISION);return(*this);}
  100. Int & operator -= (const Int & number) {Borrow = XMP_Sub(&reg[0], &reg[0], number, 0, PRECISION);return(*this);}
  101. Int & operator *= (const Int & multiplier) {Remainder = *this;Error=XMP_Signed_Mult(&reg[0], Remainder, multiplier, PRECISION);return(*this);}
  102. Int & operator /= (const Int & t) {*this = (*this) / t;return *this;}
  103. Int & operator %= (const Int & t) {*this = (*this) % t;return *this;}
  104. Int & operator <<= (int bits) {XMP_Shift_Left_Bits(&reg[0], bits, PRECISION);return *this;}
  105. Int & operator >>= (int bits) {XMP_Shift_Right_Bits(&reg[0], bits, PRECISION);return *this;}
  106. /*
  107. ** Mathematical binary operators.
  108. */
  109. Int operator + (const Int & number) const {Int term;Carry = XMP_Add(term, &reg[0], number, 0, PRECISION);return(term);}
  110. Int operator + (unsigned short b) const {Int result;Carry=XMP_Add_Int(result, &reg[0], b, 0, PRECISION);return(result);}
  111. Int operator - (const Int & number) const {Int term;Borrow = XMP_Sub(term, &reg[0], number, 0, PRECISION);return(term);}
  112. Int operator - (unsigned short b) const {Int result;Borrow = XMP_Sub_Int(result, &reg[0], b, 0, PRECISION);return(result);}
  113. Int operator * (const Int & multiplier) const {Int result;Error=XMP_Signed_Mult(result, &reg[0], multiplier, PRECISION);return result;}
  114. Int operator * (unsigned short b) const {Int result;Error=XMP_Unsigned_Mult_Int(result, &reg[0], b, PRECISION);return(result);}
  115. Int operator / (const Int & divisor) const {Int quotient = *this;XMP_Signed_Div(Remainder, quotient, &reg[0], divisor, PRECISION);return (quotient);}
  116. Int operator / (unsigned long b) const {return(*this / Int<PRECISION>(b));}
  117. Int operator / (unsigned short divisor) const {Int quotient;Error=XMP_Unsigned_Div_Int(quotient, &reg[0], divisor, PRECISION);return(quotient);}
  118. Int operator % (const Int & divisor) const {Int remainder;XMP_Signed_Div(remainder, Remainder, &reg[0], divisor, PRECISION);return(remainder);}
  119. Int operator % (unsigned long b) const {return(*this % Int<PRECISION>(b));}
  120. unsigned short operator % (unsigned short divisor) const {return(XMP_Unsigned_Div_Int(Remainder, &reg[0], divisor, PRECISION));}
  121. /*
  122. ** Bitwise binary operators.
  123. */
  124. Int operator >> (int bits) const {Int result = *this; XMP_Shift_Right_Bits(result, bits, PRECISION);return result;}
  125. Int operator << (int bits) const {Int result = *this; XMP_Shift_Left_Bits(result, bits, PRECISION);return result;}
  126. /*
  127. ** Comparison binary operators.
  128. */
  129. int operator == (const Int &b) const {return (memcmp(&reg[0], &b.reg[0], (MAX_BIT_PRECISION/CHAR_BIT))==0);}
  130. int operator != (const Int& b) const {return !(*this == b);}
  131. int operator > (const Int & number) const {return(XMP_Compare(&reg[0], number, PRECISION) > 0);}
  132. int operator >= (const Int & number) const {return(XMP_Compare(&reg[0], number, PRECISION) >= 0);}
  133. int operator < (const Int & number) const {return(XMP_Compare(&reg[0], number, PRECISION) < 0);}
  134. int operator <= (const Int & number) const {return(XMP_Compare(&reg[0], number, PRECISION) <= 0);}
  135. /*
  136. ** Misc. mathematical and logical functions.
  137. */
  138. void Negate(void) {XMP_Neg(&reg[0], PRECISION);}
  139. Int Abs(void) {XMP_Abs(&reg[0], PRECISION);return(*this);}
  140. Int exp_b_mod_c(const Int & e, const Int & m) const {
  141. Int result;
  142. Error=XMP_Exponent_Mod(result, &reg[0], e, m, PRECISION);
  143. return result;
  144. }
  145. void Set_Bit(int index) { XMP_Set_Bit(&reg[0], index); }
  146. static Int Unsigned_Mult(Int const & multiplicand, Int const & multiplier) {Int product;Error=XMP_Unsigned_Mult(&product.reg[0], &multiplicand.reg[0], &multiplier.reg[0], PRECISION);return(product);}
  147. static void Unsigned_Divide(Int & remainder, Int & quotient, const Int & dividend, const Int & divisor) {Error=XMP_Unsigned_Div(remainder, quotient, dividend, divisor, PRECISION);}
  148. static void Signed_Divide(Int & remainder, Int & quotient, const Int & dividend, const Int & divisor) {XMP_Signed_Div(remainder, quotient, dividend, divisor, PRECISION);}
  149. Int Inverse(const Int & modulus) const {Int result;XMP_Inverse_A_Mod_B(result, &reg[0], modulus, PRECISION);return(result);}
  150. static Int Decode_ASCII(char const * string) {Int result;XMP_Decode_ASCII(string, result, PRECISION);return(result);}
  151. // Number (sign independand) inserted into buffer.
  152. int Encode(unsigned char *output) const {return(XMP_Encode(output, &reg[0], PRECISION));}
  153. int Encode(unsigned char * output, unsigned length) const {return(XMP_Encode_Bounded(output, length, &reg[0], PRECISION));}
  154. void Signed_Decode(const unsigned char * from, int frombytes) {XMP_Signed_Decode(&reg[0], from, frombytes, PRECISION);}
  155. void Unsigned_Decode(const unsigned char * from, int frombytes) {XMP_Unsigned_Decode(&reg[0], from, frombytes, PRECISION);}
  156. // encode Int using Distinguished Encoding Rules, returns size of output
  157. int DEREncode(unsigned char * output) const {return(XMP_DER_Encode(&reg[0], output, PRECISION));}
  158. void DERDecode(const unsigned char *input) {XMP_DER_Decode(&reg[0], input, PRECISION);}
  159. // Friend helper functions.
  160. friend Int<PRECISION> Gcd FN_TEMPLATE (const Int<PRECISION> &, const Int<PRECISION> &);
  161. static int Error;
  162. // Carry result from last addition.
  163. static bool Carry;
  164. // Borrow result from last subtraction.
  165. static bool Borrow;
  166. // Remainder value from the various division routines.
  167. static Int Remainder;
  168. digit reg[PRECISION];
  169. friend struct RemainderTable< Int<PRECISION> >;
  170. };
  171. template<class T>
  172. struct RemainderTable
  173. {
  174. RemainderTable(const T & p) : HasZeroEntry(false)
  175. {
  176. int primesize = XMP_Fetch_Prime_Size();
  177. unsigned short const * primetable = XMP_Fetch_Prime_Table();
  178. for (int i = 0; i < primesize; i++) {
  179. table[i] = p % primetable[i];
  180. }
  181. }
  182. bool HasZero() const {return(HasZeroEntry);}
  183. void Increment(unsigned short increment = 1)
  184. {
  185. int primesize = XMP_Fetch_Prime_Size();
  186. unsigned short const * primetable = XMP_Fetch_Prime_Table();
  187. HasZeroEntry = false;
  188. for (int i = 0; i < primesize; i++) {
  189. table[i] += increment;
  190. while (table[i] >= primetable[i]) {
  191. table[i] -= primetable[i];
  192. }
  193. HasZeroEntry = (HasZeroEntry || !table[i]);
  194. }
  195. }
  196. void Increment(const RemainderTable & rtQ)
  197. {
  198. HasZeroEntry = false;
  199. int primesize = XMP_Fetch_Prime_Size();
  200. unsigned short const * primetable = XMP_Fetch_Prime_Table();
  201. for (int i = 0; i < primesize; i++) {
  202. table[i] += rtQ.table[i];
  203. if (table[i] >= primetable[i]) {
  204. table[i] -= primetable[i];
  205. }
  206. HasZeroEntry = (HasZeroEntry || !table[i]);
  207. }
  208. }
  209. bool HasZeroEntry;
  210. unsigned short table[3511];
  211. };
  212. template<class T>
  213. T Gcd(const T & a, const T & n)
  214. {
  215. T g[3]={n, a, 0UL};
  216. unsigned int i = 1;
  217. while (!!g[i%3]) {
  218. g[(i+1)%3] = g[(i-1)%3] % g[i%3];
  219. i++;
  220. }
  221. return g[(i-1)%3];
  222. }
  223. #if defined(__WATCOMC__)
  224. #pragma warning 604 9
  225. #pragma warning 595 9
  226. #endif
  227. template<class T>
  228. T Generate_Prime(Straw & rng, int pbits, T const *)
  229. {
  230. T minQ = (T(1UL) << (unsigned short)(pbits-(unsigned short)2));
  231. T maxQ = ((T(1UL) << (unsigned short)(pbits-(unsigned short)1)) - (unsigned short)1);
  232. T q;
  233. T p;
  234. do {
  235. q.Randomize(rng, minQ, maxQ);
  236. p = (q*2) + (unsigned short)1;
  237. RemainderTable<T> rtQ(q);
  238. RemainderTable<T> rtP(p);
  239. while (rtQ.HasZero() || rtP.HasZero() || !q.IsPrime() || !p.IsPrime()) {
  240. q += 2;
  241. p += 4;
  242. if (q > maxQ) break;
  243. rtQ.Increment(2);
  244. rtP.Increment(4);
  245. }
  246. } while (q > maxQ);
  247. return(p);
  248. }
  249. #define UNITSIZE 32
  250. #define MAX_BIT_PRECISION 2048
  251. #define MAX_UNIT_PRECISION (MAX_BIT_PRECISION/UNITSIZE)
  252. typedef Int<MAX_UNIT_PRECISION> bignum;
  253. typedef Int<MAX_UNIT_PRECISION> BigInt;
  254. #endif