INT.H 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/INT.H 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : INT.H *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 04/26/96 *
  26. * *
  27. * Last Update : April 26, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  32. #ifndef INT_H
  33. #define INT_H
  34. #include <memory.h>
  35. #include <limits.h>
  36. #include <assert.h>
  37. #include "mp.h"
  38. #include "straw.h"
  39. //#pragma warn -inl
  40. template<int PRECISION>
  41. class Int {
  42. public:
  43. /*
  44. ** Constructors and initializers.
  45. */
  46. Int(void) {XMP_Init(&reg[0], 0, PRECISION);}
  47. Int(unsigned long value) {XMP_Init(&reg[0], value, PRECISION);}
  48. void Randomize(Straw & rng, int bitcount) {XMP_Randomize(&reg[0], rng, bitcount, PRECISION);}
  49. void Randomize(Straw & rng, const Int & minval, const Int & maxval) {XMP_Randomize(&reg[0], rng, minval, maxval, PRECISION); reg[0] |= 1;}
  50. /*
  51. ** Convenient conversion operators to get at the underlying array of
  52. ** integers. Big number math is basically manipulation of arbitrary
  53. ** length arrays.
  54. */
  55. operator digit * () {return & reg[0];}
  56. operator const digit * () const {return & reg[0];}
  57. /*
  58. ** Array access operator (references bit position). Bit 0 is the first bit.
  59. */
  60. bool operator[](unsigned bit) const {return(XMP_Test_Bit(&reg[0], bit));}
  61. /*
  62. ** Unary operators.
  63. */
  64. Int & operator ++ (void) {XMP_Inc(&reg[0], PRECISION);return(*this);}
  65. Int & operator -- (void) {XMP_Dec(&reg[0], PRECISION);return(*this);}
  66. int operator ! (void) const {return(XMP_Test_Eq_Int(&reg[0], 0, PRECISION));}
  67. Int operator ~ (void) {XMP_Not(&reg[0], PRECISION);return(*this);}
  68. Int operator - (void) const {Int a = *this;a.Negate();return (a);}
  69. /*
  70. ** Attribute query functions.
  71. */
  72. int ByteCount(void) const {return(XMP_Count_Bytes(&reg[0], PRECISION));}
  73. int BitCount(void) const {return(XMP_Count_Bits(&reg[0], PRECISION));}
  74. bool Is_Negative(void) const {return(XMP_Is_Negative(&reg[0], PRECISION));}
  75. unsigned MaxBitPrecision() const {return PRECISION*(sizeof(unsigned long)*CHAR_BIT);}
  76. bool IsSmallPrime(void) const {return(XMP_Is_Small_Prime(&reg[0], PRECISION));}
  77. bool SmallDivisorsTest(void) const {return(XMP_Small_Divisors_Test(&reg[0], PRECISION));}
  78. bool FermatTest(unsigned rounds) const {return(XMP_Fermat_Test(&reg[0], rounds, PRECISION));}
  79. bool IsPrime(void) const {return(XMP_Is_Prime(&reg[0], PRECISION));}
  80. bool RabinMillerTest(Straw & rng, unsigned int rounds) const {return(XMP_Rabin_Miller_Test(rng, &reg[0], rounds, PRECISION));}
  81. /*
  82. ** 'in-place' binary operators.
  83. */
  84. Int & operator += (const Int & number) {Carry = XMP_Add(&reg[0], &reg[0], number, 0, PRECISION);return(*this);}
  85. Int & operator -= (const Int & number) {Borrow = XMP_Sub(&reg[0], &reg[0], number, 0, PRECISION);return(*this);}
  86. Int & operator *= (const Int & multiplier) {Remainder = *this;Error=XMP_Signed_Mult(&reg[0], Remainder, multiplier, PRECISION);return(*this);}
  87. Int & operator /= (const Int & t) {*this = (*this) / t;return *this;}
  88. Int & operator %= (const Int & t) {*this = (*this) % t;return *this;}
  89. Int & operator <<= (int bits) {XMP_Shift_Left_Bits(&reg[0], bits, PRECISION);return *this;}
  90. Int & operator >>= (int bits) {XMP_Shift_Right_Bits(&reg[0], bits, PRECISION);return *this;}
  91. /*
  92. ** Mathematical binary operators.
  93. */
  94. Int operator + (const Int & number) const {Int term;Carry = XMP_Add(term, &reg[0], number, 0, PRECISION);return(term);}
  95. Int operator + (unsigned short b) const {Int result;Carry=XMP_Add_Int(result, &reg[0], b, 0, PRECISION);return(result);}
  96. // friend Int<PRECISION> operator + (digit b, const Int<PRECISION> & a) {return(Int<PRECISION>(b) + a);}
  97. Int operator - (const Int & number) const {Int term;Borrow = XMP_Sub(term, &reg[0], number, 0, PRECISION);return(term);}
  98. Int operator - (unsigned short b) const {Int result;Borrow = XMP_Sub_Int(result, &reg[0], b, 0, PRECISION);return(result);}
  99. // friend Int<PRECISION> operator - (digit b, const Int<PRECISION> & a) {return(Int<PRECISION>(b) - a);}
  100. Int operator * (const Int & multiplier) const {Int result;Error=XMP_Signed_Mult(result, &reg[0], multiplier, PRECISION);return result;}
  101. Int operator * (unsigned short b) const {Int result;Error=XMP_Unsigned_Mult_Int(result, &reg[0], b, PRECISION);return(result);}
  102. // friend Int<PRECISION> operator * (digit b, const Int<PRECISION> & a) {return(Int<PRECISION>(b) * a);}
  103. Int operator / (const Int & divisor) const {Int quotient = *this;XMP_Signed_Div(Remainder, quotient, &reg[0], divisor, PRECISION);return (quotient);}
  104. Int operator / (unsigned long b) const {return(*this / Int<PRECISION>(b));}
  105. Int operator / (unsigned short divisor) const {Int quotient;Error=XMP_Unsigned_Div_Int(quotient, &reg[0], divisor, PRECISION);return(quotient);}
  106. // friend Int<PRECISION> operator / (digit a, const Int<PRECISION> & b) {return(Int<PRECISION>(a) / b);}
  107. Int operator % (const Int & divisor) const {Int remainder;XMP_Signed_Div(remainder, Remainder, &reg[0], divisor, PRECISION);return(remainder);}
  108. Int operator % (unsigned long b) const {return(*this % Int<PRECISION>(b));}
  109. unsigned short operator % (unsigned short divisor) const {return(XMP_Unsigned_Div_Int(Remainder, &reg[0], divisor, PRECISION));}
  110. // friend Int<PRECISION> operator % (digit a, const Int<PRECISION> & b) {return(Int<PRECISION>(a) % b);}
  111. /*
  112. ** Bitwise binary operators.
  113. */
  114. Int operator >> (int bits) const {Int result = *this; XMP_Shift_Right_Bits(result, bits, PRECISION);return result;}
  115. Int operator << (int bits) const {Int result = *this; XMP_Shift_Left_Bits(result, bits, PRECISION);return result;}
  116. /*
  117. ** Comparison binary operators.
  118. */
  119. int operator == (const Int &b) const {return (memcmp(&reg[0], &b.reg[0], (MAX_BIT_PRECISION/CHAR_BIT))==0);}
  120. int operator != (const Int& b) const {return !(*this == b);}
  121. int operator > (const Int & number) const {return(XMP_Compare(&reg[0], number, PRECISION) > 0);}
  122. int operator >= (const Int & number) const {return(XMP_Compare(&reg[0], number, PRECISION) >= 0);}
  123. int operator < (const Int & number) const {return(XMP_Compare(&reg[0], number, PRECISION) < 0);}
  124. int operator <= (const Int & number) const {return(XMP_Compare(&reg[0], number, PRECISION) <= 0);}
  125. /*
  126. ** Misc. mathematical and logical functions.
  127. */
  128. void Negate(void) {XMP_Neg(&reg[0], PRECISION);}
  129. Int Abs(void) {XMP_Abs(&reg[0], PRECISION);return(*this);}
  130. Int times_b_mod_c(Int const & multiplier, Int const & modulus) const {
  131. Int result;
  132. Error=xmp_stage_modulus(modulus, PRECISION);
  133. Error=XMP_Mod_Mult(result, &reg[0], multiplier, PRECISION);
  134. XMP_Mod_Mult_Clear(PRECISION);
  135. return result;
  136. }
  137. Int exp_b_mod_c(const Int & e, const Int & m) const {
  138. Int result;
  139. Error=xmp_exponent_mod(result, &reg[0], e, m, PRECISION);
  140. return result;
  141. }
  142. 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);}
  143. static void Unsigned_Divide(Int & remainder, Int & quotient, const Int & dividend, const Int & divisor) {Error=XMP_Unsigned_Div(remainder, quotient, dividend, divisor, PRECISION);}
  144. static void Signed_Divide(Int & remainder, Int & quotient, const Int & dividend, const Int & divisor) {XMP_Signed_Div(remainder, quotient, dividend, divisor, PRECISION);}
  145. Int Inverse(const Int & modulus) const {Int result;XMP_Inverse_A_Mod_B(result, &reg[0], modulus, PRECISION);return(result);}
  146. static Int Decode_ASCII(char const * string) {Int result;XMP_Decode_ASCII(string, result, PRECISION);return(result);}
  147. // Number (sign independand) inserted into buffer.
  148. int Encode(unsigned char *output) const {return(XMP_Encode(output, &reg[0], PRECISION));}
  149. int Encode(unsigned char * output, unsigned length) const {return(XMP_Encode(output, length, &reg[0], PRECISION));}
  150. void Signed_Decode(const unsigned char * from, int frombytes) {XMP_Signed_Decode(&reg[0], from, frombytes, PRECISION);}
  151. void Unsigned_Decode(const unsigned char * from, int frombytes) {XMP_Unsigned_Decode(&reg[0], from, frombytes, PRECISION);}
  152. // encode Int using Distinguished Encoding Rules, returns size of output
  153. int DEREncode(unsigned char * output) const {return(XMP_DER_Encode(&reg[0], output, PRECISION));}
  154. void DERDecode(const unsigned char *input) {XMP_DER_Decode(&reg[0], input, PRECISION);}
  155. // Friend helper functions.
  156. friend Int<PRECISION> Generate_Prime(Straw & rng, int pbits, Int<PRECISION> const * = 0);
  157. friend Int<PRECISION> Gcd(const Int<PRECISION> & a, const Int<PRECISION> & b);
  158. // friend bool NextPrime(Int<PRECISION> & p, const Int<PRECISION> & max, bool blumInt=false);
  159. // friend Int<PRECISION> a_exp_b_mod_pq(const Int<PRECISION> & a, const Int<PRECISION> & ep, const Int<PRECISION> & eq, const Int<PRECISION> & p, const Int<PRECISION> & q, const Int<PRECISION> & u);
  160. static int Error;
  161. // Carry result from last addition.
  162. static bool Carry;
  163. // Borrow result from last subtraction.
  164. static bool Borrow;
  165. // Remainder value from the various division routines.
  166. static Int Remainder;
  167. private:
  168. digit reg[PRECISION];
  169. struct RemainderTable
  170. {
  171. RemainderTable(const Int<PRECISION> & p) : HasZeroEntry(false)
  172. {
  173. for (unsigned i = 0; i < ARRAY_SIZE(primeTable); i++) {
  174. table[i] = p % primeTable[i];
  175. }
  176. }
  177. bool HasZero() const {return(HasZeroEntry);}
  178. void Increment(unsigned short increment = 1)
  179. {
  180. HasZeroEntry = false;
  181. for (unsigned int i = 0; i < ARRAY_SIZE(primeTable); i++) {
  182. table[i] += increment;
  183. while (table[i] >= primeTable[i]) {
  184. table[i] -= primeTable[i];
  185. }
  186. HasZeroEntry = (HasZeroEntry || !table[i]);
  187. }
  188. }
  189. void Increment(const RemainderTable & rtQ)
  190. {
  191. HasZeroEntry = false;
  192. for (unsigned int i = 0; i < ARRAY_SIZE(primeTable); i++) {
  193. table[i] += rtQ.table[i];
  194. if (table[i] >= primeTable[i]) {
  195. table[i] -= primeTable[i];
  196. }
  197. HasZeroEntry = (HasZeroEntry || !table[i]);
  198. }
  199. }
  200. bool HasZeroEntry;
  201. unsigned short table[ARRAY_SIZE(primeTable)];
  202. };
  203. };
  204. template<class T>
  205. T Gcd(const T & a, const T & n)
  206. {
  207. T g[3]={n, a, 0UL};
  208. unsigned int i = 1;
  209. while (!!g[i%3]) {
  210. g[(i+1)%3] = g[(i-1)%3] % g[i%3];
  211. i++;
  212. }
  213. return g[(i-1)%3];
  214. }
  215. //#pragma warning 604 9
  216. //#pragma warning 595 9
  217. template<class T>
  218. T Generate_Prime(Straw & rng, int pbits, T const *)
  219. {
  220. T minQ = (T(1UL) << (unsigned short)(pbits-(unsigned short)2));
  221. T maxQ = ((T(1UL) << (unsigned short)(pbits-(unsigned short)1)) - (unsigned short)1);
  222. T q;
  223. T p;
  224. do {
  225. q.Randomize(rng, minQ, maxQ);
  226. p = (q*2) + (unsigned short)1;
  227. T::RemainderTable rtQ(q);
  228. T::RemainderTable rtP(p);
  229. while (rtQ.HasZero() || rtP.HasZero() || !q.IsPrime() || !p.IsPrime()) {
  230. q += 2;
  231. p += 4;
  232. if (q > maxQ) break;
  233. rtQ.Increment(2);
  234. rtP.Increment(4);
  235. }
  236. } while (q > maxQ);
  237. return(p);
  238. }
  239. typedef Int<MAX_UNIT_PRECISION> bignum;
  240. typedef Int<MAX_UNIT_PRECISION> BigInt;
  241. //BigInt Gcd(const BigInt & a, const BigInt & n);
  242. //BigInt Generate_Prime(RandomNumberGenerator & rng, int pbits, BigInt const * dummy);
  243. #endif