MP.H 7.9 KB

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