MP.H 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/MP.H 1 3/03/97 10:25a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : MP.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 04/26/96 *
  30. * *
  31. * Last Update : April 26, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #ifndef MP_H
  37. #define MP_H
  38. //lint -e740 -e534 -e537 -e760
  39. //lint -d_LINT=1
  40. /*
  41. ** The "bool" integral type was defined by the C++ committee in
  42. ** November of '94. Until the compiler supports this, use the following
  43. ** definition.
  44. */
  45. #ifndef __BORLANDC__
  46. #ifndef TRUE_FALSE_DEFINED
  47. #define TRUE_FALSE_DEFINED
  48. enum {false=0,true=1};
  49. typedef int bool;
  50. #endif
  51. #endif
  52. #include "straw.h"
  53. #include <stdlib.h>
  54. extern unsigned short primeTable[3511];
  55. #define digit unsigned long
  56. #define signeddigit signed long
  57. #define LOG_UNITSIZE 5
  58. #define UNITSIZE 32
  59. #define UPPER_MOST_BIT 0x80000000L
  60. #define SEMI_UPPER_MOST_BIT 0x8000
  61. #define SEMI_MASK ((unsigned short)~0)
  62. #define MAX_BIT_PRECISION 2048
  63. #define MAX_UNIT_PRECISION (MAX_BIT_PRECISION/UNITSIZE)
  64. #ifndef ARRAY_SIZE
  65. #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
  66. #endif
  67. int XMP_Significance(const digit * r, int precision);
  68. void XMP_Inc(digit * r, int precision);
  69. void XMP_Dec(digit * r, int precision);
  70. void XMP_Neg(digit * r, int precision);
  71. void XMP_Abs(digit * r, int precision);
  72. void XMP_Shift_Right_Bits(digit * r1, int bits, int precision);
  73. void XMP_Shift_Left_Bits(digit * r1, int bits, int precision);
  74. bool XMP_Rotate_Left(digit * r1, bool carry, int precision);
  75. void XMP_Not(digit * digit_ptr, int precision);
  76. void XMP_Init(digit * r, digit value, int precision);
  77. unsigned XMP_Count_Bits(const digit * r, int precision);
  78. int XMP_Count_Bytes(const digit * r, int precision);
  79. void XMP_Move(digit * dest, digit const * source, int precision);
  80. int XMP_Compare(const digit * r1, const digit * r2, int precision);
  81. bool XMP_Add(digit * result, const digit * r1, const digit * r2, bool carry, int precision);
  82. bool XMP_Add_Int(digit * result, const digit * r1, digit r2, bool carry, int precision);
  83. bool XMP_Sub(digit * result, const digit * r1, const digit * r2, bool borrow, int precision);
  84. bool XMP_Sub_Int(digit * result, const digit * r1, unsigned short r2, bool borrow, int precision);
  85. int XMP_Unsigned_Mult(digit * prod, const digit * multiplicand, const digit * multiplier, int precision);
  86. int XMP_Unsigned_Mult_Int(digit * prod, const digit * multiplicand, short multiplier, int precision);
  87. int XMP_Signed_Mult_Int(digit * prod, const digit * multiplicand, signed short multiplier, int precision);
  88. int XMP_Signed_Mult(digit * prod, const digit * multiplicand, const digit * multiplier, int precision);
  89. unsigned short XMP_Unsigned_Div_Int(digit * quotient, digit const * dividend, unsigned short divisor, int precision);
  90. int XMP_Unsigned_Div(digit * remainder, digit * quotient, digit const * dividend, digit const * divisor, int precision);
  91. void XMP_Signed_Div(digit * remainder, digit * quotient, digit const * dividend, digit const * divisor, int precision);
  92. int XMP_Reciprocal(digit * quotient, const digit * divisor, int precision);
  93. void XMP_Decode_ASCII(char const * str, digit * mpn, int precision);
  94. void xmp_single_mul(unsigned short * prod, unsigned short * multiplicand, unsigned short multiplier, int precision);
  95. void XMP_Double_Mul(digit * prod, const digit * multiplicand, const digit * multiplier, int precision);
  96. int xmp_stage_modulus(const digit * n_modulus, int precision);
  97. int XMP_Mod_Mult(digit * prod, const digit * multiplicand, const digit * multiplier, int precision);
  98. void XMP_Mod_Mult_Clear(int precision);
  99. unsigned short mp_quo_digit(unsigned short * dividend);
  100. int xmp_exponent_mod(digit * expout, const digit * expin, const digit * exponent_ptr, const digit * modulus, int precision);
  101. bool XMP_Is_Small_Prime(const digit * candidate, int precision);
  102. bool XMP_Small_Divisors_Test(const digit * candidate, int precision);
  103. bool XMP_Fermat_Test(const digit * candidate_prime, unsigned rounds, int precision);
  104. void XMP_Inverse_A_Mod_B(digit * result, digit const * number, digit const * modulus, int precision);
  105. void XMP_Signed_Decode(digit * result, const unsigned char * from, int frombytes, int precision);
  106. void XMP_Unsigned_Decode(digit * result, const unsigned char * from, int frombytes, int precision);
  107. unsigned XMP_Encode(unsigned char * to, digit const * from, int precision);
  108. unsigned XMP_Encode(unsigned char * to, unsigned tobytes, digit const * from, int precision);
  109. void XMP_Randomize(digit * result, Straw & rng, int nbits, int precision);
  110. void XMP_Randomize(digit * result, Straw & rng, digit const * min, digit const * max, int precision);
  111. bool XMP_Is_Prime(digit const * prime, int precision);
  112. bool XMP_Rabin_Miller_Test(Straw & rng, digit const * w, int rounds, int precision);
  113. int XMP_DER_Length_Encode(unsigned long length, unsigned char * output);
  114. int XMP_DER_Encode(digit const * from, unsigned char * output, int precision);
  115. void XMP_DER_Decode(digit * result, unsigned char const * input, int precision);
  116. inline int XMP_Digits_To_Bits(int digits)
  117. {
  118. return(digits << LOG_UNITSIZE);
  119. }
  120. inline int XMP_Bits_To_Digits(int bits)
  121. {
  122. return ((bits + (UNITSIZE-1)) / UNITSIZE);
  123. }
  124. inline digit XMP_Bits_To_Mask(int bits)
  125. {
  126. if (!bits) return(0);
  127. return(1 << ((bits-1) % UNITSIZE));
  128. }
  129. inline bool XMP_Is_Negative(const digit * r, int precision)
  130. {
  131. return((signeddigit) *(r + (precision-1)) < 0);
  132. }
  133. inline bool XMP_Test_Eq_Int(digit const * r, int i, int p)
  134. {
  135. return( (*r == i ) && XMP_Significance(r,p) <= 1 );
  136. }
  137. inline void XMP_Set_Bit(digit * r, unsigned bit)
  138. {
  139. r[bit >> LOG_UNITSIZE] |= ((digit)1 << (bit & (UNITSIZE-1)));
  140. }
  141. inline bool XMP_Test_Bit(const digit * r, unsigned bit)
  142. {
  143. return (r[bit >> LOG_UNITSIZE] & ((digit)1 << (bit & (UNITSIZE-1))));
  144. }
  145. // Misc functions.
  146. void memrev(char * buffer, size_t length);
  147. #endif