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