MP.CPP 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732
  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.CPP 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 S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : MP.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 04/26/96 *
  30. * *
  31. * Last Update : July 2, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * _Byte_Precision -- Determines the number of bytes significant in long integer. *
  36. * memrev -- Reverse the byte order of the buffer specified. *
  37. * XMP_Abs -- Perform an absolute value on the specified MP number. *
  38. * XMP_Add -- Add two MP numbers with a carry option. *
  39. * XMP_Add_Int -- Add an integer to an MP number (with carry). *
  40. * XMP_Compare -- Compare one MP number with another. *
  41. * XMP_Count_Bits -- Count the total number of bits (precision) in MP number. *
  42. * XMP_Count_Bytes -- Counts the number of precision bytes in MP number. *
  43. * XMP_Dec -- Decrement the MP number by one. *
  44. * XMP_Decode_ASCII -- Convert ASCII into an MP number. *
  45. * XMP_DER_Decode -- Decode a DER number into an MP number. *
  46. * XMP_DER_Encode -- Encode a number into a buffer using DER. *
  47. * XMP_DER_Length_Encode -- Output the length of a DER block. *
  48. * XMP_Double_Mul -- Double precision MP multiply. *
  49. * XMP_Encode -- Encode MP number into buffer as compactly as possible. *
  50. * XMP_Fermat_Test -- Performs Fermat's Little Theorem on an MP number. *
  51. * XMP_Hybrid_Mul -- Special hybrid short multiply (with carry). *
  52. * XMP_Inc -- Increment an MP number by one. *
  53. * XMP_Init -- Initialize an MP number to a starting value. *
  54. * XMP_Inverse_A_Mod_B -- Inverts and performs modulus on an MP number. *
  55. * XMP_Is_Prime -- Determine if the specified MP number is prime. *
  56. * XMP_Is_Small_Prime -- Determine if MP number is a small prime. *
  57. * XMP_Mod_Mult -- Perform a multiply - modulus operation. *
  58. * XMP_Mod_Mult_Clear -- Remove temporary values from memory. *
  59. * XMP_Move -- Assign one MP number to another. *
  60. * XMP_Neg -- Negate the specified MP number. *
  61. * XMP_Not -- Perform bitwise NOT operation on MP number. *
  62. * XMP_Prepare_Modulus -- Prepare globals for modulus operation. *
  63. * XMP_Rabin_Miller_Test -- Performs the Rabin Miller test for primality. *
  64. * XMP_Randomize -- Generate a random MP number between the boundaries specified. *
  65. * XMP_Randomize -- Generate a random MP number. *
  66. * XMP_Reciprocal -- Compute the reciprocal (inverse) of the MP number. *
  67. * XMP_Rotate_Left -- Rotate specified MP number leftward. *
  68. * XMP_Shift_Left_Bits -- Shifts the MP number left by the specified bit count. *
  69. * XMP_Shift_Right_Bits -- Shift the MP number right by specified bit count. *
  70. * XMP_Signed_Decode -- Decode a number as if it were signed. *
  71. * XMP_Signed_Div -- Signed divide of one MP number into another. *
  72. * XMP_Signed_Mult -- A signed multiply between two MP numbers. *
  73. * XMP_Signed_Mult_Int -- Multiply an MP number by a signed simple integer. *
  74. * XMP_Significance -- Fetch the precision (bytes) of the MP number. *
  75. * XMP_Small_Divisors_Test -- Perform the small divisors test on an MP number. *
  76. * XMP_Sub -- Subtract one MP number from another (with borrow). *
  77. * XMP_Sub_Int -- Subtract an integer from an MP number (with borrow). *
  78. * XMP_Unsigned_Decode -- Decode a number as if it were unsigned. *
  79. * XMP_Unsigned_Div -- Unsigned divide of one MP number into another. *
  80. * XMP_Unsigned_Div_Int -- Perform a short integer divide into an MP number. *
  81. * XMP_Unsigned_Mult -- Multiply two unsigned MP numbers together. *
  82. * XMP_Unsigned_Mult_Int -- Multiply an MP number by a simple integer. *
  83. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  84. #include <stdlib.h>
  85. #include <string.h>
  86. #include <ctype.h>
  87. #include <assert.h>
  88. #include <limits.h>
  89. #include "mp.h"
  90. #ifndef __BORLANDC__
  91. #define min(a, b) (((a) < (b)) ? (a) : (b))
  92. #define _USERENTRY
  93. #endif
  94. /***********************************************************************************************
  95. * _Byte_Precision -- Determines the number of bytes significant in long integer. *
  96. * *
  97. * This utility routine will determine the number of precision bytes exist in the long *
  98. * integer specified. There are some optimizations that can occur if the byte precision *
  99. * is known. *
  100. * *
  101. * INPUT: value -- The value of the long integer that the byte precision will be calculated *
  102. * for. *
  103. * *
  104. * OUTPUT: Returns with the number of bytes that the long integer requires (at a minimum) *
  105. * to cover the precision of the integer. The minimum value will be 1, the maximum *
  106. * will be 4. *
  107. * *
  108. * WARNINGS: none *
  109. * *
  110. * HISTORY: *
  111. * 07/01/1996 JLB : Created. *
  112. *=============================================================================================*/
  113. static int _Byte_Precision(unsigned long value)
  114. {
  115. int byte_count;
  116. for (byte_count = sizeof(value); byte_count; byte_count--) {
  117. if (value >> ((byte_count-1)*8)) break;
  118. }
  119. return(byte_count);
  120. }
  121. /***********************************************************************************************
  122. * XMP_DER_Length_Encode -- Output the length of a DER block. *
  123. * *
  124. * This routine will output the length of the block using Distinguished Encoding Rules. *
  125. * The rest of the block must be filled in as appropriate. For data blocks that are less *
  126. * than 128 bytes long, the header consists of only one byte. Longer buffers lengths *
  127. * can consume up to 5 bytes (depends on magnitude of the length value). *
  128. * *
  129. * INPUT: length -- The length of the data block to be output. *
  130. * *
  131. * output -- Pointer to the memory block that will be set up. *
  132. * *
  133. * OUTPUT: Returns with the number of bytes (header) that was used to store the length *
  134. * value. Subsequent data must be placed after the header. *
  135. * *
  136. * WARNINGS: none *
  137. * *
  138. * HISTORY: *
  139. * 07/01/1996 JLB : Created. *
  140. *=============================================================================================*/
  141. int XMP_DER_Length_Encode(unsigned long length, unsigned char * output)
  142. {
  143. assert(output != NULL);
  144. int header_length = 0;
  145. if (length <= SCHAR_MAX) {
  146. output[header_length++] = (unsigned char)length;
  147. } else {
  148. output[header_length++] = (unsigned char)(_Byte_Precision(length) | 0x80);
  149. for (int byte_counter = _Byte_Precision(length); byte_counter; --byte_counter) {
  150. output[header_length++] = (unsigned char)(length >> ((byte_counter-1)*8));
  151. }
  152. }
  153. return(header_length);
  154. }
  155. /***********************************************************************************************
  156. * XMP_DER_Encode -- Encode a number into a buffer using DER. *
  157. * *
  158. * This routine is used to encode a number into a buffer using Distinguished Encoding *
  159. * Rules. The number of bytes used will be, typically, two bytes more than the number of *
  160. * precision bytes in the number. *
  161. * *
  162. * INPUT: from -- Pointer to the multi-precision number. *
  163. * *
  164. * output -- Pointer to the buffer that will hold the DER encoded number. *
  165. * *
  166. * precision-- The precision of the multi-precision number. *
  167. * *
  168. * OUTPUT: Returns with the number of bytes used in the output buffer. *
  169. * *
  170. * WARNINGS: Make sure the buffer is big enough to hold the DER encoded number. For safety *
  171. * make sure it is precision+6 bytes long. *
  172. * *
  173. * HISTORY: *
  174. * 07/01/1996 JLB : Created. *
  175. *=============================================================================================*/
  176. int XMP_DER_Encode(digit const * from, unsigned char * output, int precision)
  177. {
  178. assert(from != NULL);
  179. assert(output != NULL);
  180. assert(precision > 0);
  181. unsigned char buffer[MAX_UNIT_PRECISION*sizeof(digit)+1];
  182. int header_count = 0;
  183. unsigned number_count = XMP_Encode(buffer, from, precision);
  184. output[header_count++] = 0x02;
  185. header_count += XMP_DER_Length_Encode(number_count, &output[header_count]);
  186. memcpy(&output[header_count], buffer, number_count);
  187. return(header_count+number_count);
  188. }
  189. /***********************************************************************************************
  190. * XMP_DER_Decode -- Decode a DER number into an MP number. *
  191. * *
  192. * Use this routine to decode a Distinguished Encoding Rules number into a multi-precision *
  193. * number. This is the counterpart function to the XMP_DER_Encode() function. *
  194. * *
  195. * INPUT: result -- The buffer the hold the result MP number. *
  196. * *
  197. * input -- Pointer to the DER encoded number. *
  198. * *
  199. * precision -- The precision of the MP number. This is the maximum precision the *
  200. * DER number can be. *
  201. * *
  202. * OUTPUT: none *
  203. * *
  204. * WARNINGS: none *
  205. * *
  206. * HISTORY: *
  207. * 07/01/1996 JLB : Created. *
  208. *=============================================================================================*/
  209. void XMP_DER_Decode(digit * result, unsigned char const * input, int precision)
  210. {
  211. assert(result != NULL);
  212. assert(input != NULL);
  213. assert(precision > 0);
  214. if (*input++ == 0x02) {
  215. unsigned byte_count;
  216. if ((*input & 0x80) == 0) {
  217. byte_count = *input++;
  218. } else {
  219. int length = *input++ & 0x7f;
  220. if (length > 2) return;
  221. byte_count = *input++;
  222. if (length > 1) byte_count = (byte_count << 8) | *input++;
  223. }
  224. if (byte_count <= (precision * sizeof(digit))) {
  225. XMP_Signed_Decode(result, input, byte_count, precision);
  226. }
  227. }
  228. }
  229. /***********************************************************************************************
  230. * XMP_Encode -- Encode MP number into buffer. *
  231. * *
  232. * This routine will encode an multi-precision number into a buffer of specified length. *
  233. * The number of stored in "big endian" format with appropriate sign extension. *
  234. * *
  235. * INPUT: to -- Pointer to the buffer to place the number. *
  236. * *
  237. * tobytes -- The number of bytes to use in the destination buffer. *
  238. * *
  239. * from -- Pointer to the MP number to be encoded. *
  240. * *
  241. * precision-- The precision of the MP number. *
  242. * *
  243. * OUTPUT: Returns with the number of bytes placed into the destination buffer. *
  244. * *
  245. * WARNINGS: none *
  246. * *
  247. * HISTORY: *
  248. * 07/01/1996 JLB : Created. *
  249. *=============================================================================================*/
  250. unsigned XMP_Encode(unsigned char * to, unsigned tobytes, digit const * from, int precision)
  251. {
  252. assert(to != NULL);
  253. assert(from != NULL);
  254. assert(tobytes > 0);
  255. assert(precision > 0);
  256. unsigned frombytes = precision * sizeof(digit);
  257. unsigned char filler = (unsigned char)(XMP_Is_Negative(from, precision) ? 0xff : 0);
  258. int index;
  259. for (index = 0; index < (int)(tobytes-frombytes); index++) {
  260. *to++ = filler;
  261. }
  262. const unsigned char * fptr = ((const unsigned char *)from) + min(tobytes, frombytes);
  263. for (index = 0; index < (int)min(tobytes, frombytes); index++) {
  264. *to++ = *--fptr;
  265. }
  266. return(tobytes);
  267. }
  268. /***********************************************************************************************
  269. * XMP_Encode -- Encode MP number into buffer as compactly as possible. *
  270. * *
  271. * This routine will encode the MP number into the specified buffer. The number will be *
  272. * encoded using the least number of bytes possible. *
  273. * *
  274. * INPUT: to -- The buffer to encode the MP number into. *
  275. * *
  276. * from -- Pointer to the MP number to be encoded. *
  277. * *
  278. * precision-- The precision of the MP number. *
  279. * *
  280. * OUTPUT: Returns with the number of bytes used in the destination buffer to hold the *
  281. * encoded number. *
  282. * *
  283. * WARNINGS: Be sure the destination buffer is big enough to hold the encoded MP number. *
  284. * A safe size would be the precision plus one. *
  285. * *
  286. * HISTORY: *
  287. * 07/01/1996 JLB : Created. *
  288. *=============================================================================================*/
  289. #pragma warning 364 9
  290. unsigned XMP_Encode(unsigned char * to, digit const * from, int precision)
  291. {
  292. assert(to != NULL);
  293. assert(from != NULL);
  294. assert(precision > 0);
  295. bool is_negative = XMP_Is_Negative(from, precision);
  296. unsigned char filler = (unsigned char)(is_negative ? 0xff : 0);
  297. unsigned char * number_ptr;
  298. unsigned char * const end = (unsigned char *)from;
  299. for (number_ptr = (unsigned char *)end + precision - 1; number_ptr > (unsigned char *)end; number_ptr--) {
  300. if (*number_ptr != filler) break;
  301. }
  302. unsigned index = 0;
  303. if (((*number_ptr & 0x80) && !is_negative) || (!(*number_ptr & 0x80) && is_negative)) {
  304. to[index++] = filler;
  305. }
  306. to[index++] = *number_ptr;
  307. while (number_ptr != end) {
  308. to[index++] = *--number_ptr;
  309. }
  310. return(index);
  311. }
  312. /***********************************************************************************************
  313. * XMP_Signed_Decode -- Decode a number as if it were signed. *
  314. * *
  315. * Use this routine to convert a coded number back into an MP number. The coded number *
  316. * is presumed to be signed. *
  317. * *
  318. * INPUT: result -- Pointer to the buffer that will hold the decoded MP number. *
  319. * *
  320. * from -- Pointer to the encoded MP number. *
  321. * *
  322. * frombytes-- The number of bytes consumed by the encoded MP number. *
  323. * *
  324. * precision -- The precision of the MP number (maximum) of the result. *
  325. * *
  326. * OUTPUT: none *
  327. * *
  328. * WARNINGS: Be sure that the precision is sufficient to hold the decoded MP number. *
  329. * Otherwise, the result is undefined. *
  330. * *
  331. * HISTORY: *
  332. * 07/01/1996 JLB : Created. *
  333. *=============================================================================================*/
  334. void XMP_Signed_Decode(digit * result, const unsigned char * from, int frombytes, int precision)
  335. {
  336. assert(result != NULL);
  337. assert(from != NULL);
  338. assert(frombytes > 0);
  339. assert(precision > 0);
  340. unsigned char filler = (unsigned char)((*from & 0x80) ? 0xff : 0);
  341. int fillcount = precision * sizeof(digit) - frombytes;
  342. unsigned char * dest = (unsigned char *)&result[precision];
  343. /*
  344. ** Fill in any excess significant bytes.
  345. */
  346. int index;
  347. for (index = 0; index < fillcount; index++) {
  348. *--dest = filler;
  349. }
  350. /*
  351. ** Move in the remaining bytes.
  352. */
  353. for (index = 0; index < frombytes; index++) {
  354. *--dest = *from++;
  355. }
  356. }
  357. /***********************************************************************************************
  358. * XMP_Unsigned_Decode -- Decode a number as if it were unsigned. *
  359. * *
  360. * Use this routine to decode a MP number and treat it as if it were unsigned. *
  361. * *
  362. * INPUT: result -- Pointer to the buffer to hold the result MP number. *
  363. * *
  364. * from -- Pointer to the encoded MP number. *
  365. * *
  366. * frombytes-- The number of bytes in the encoded number. *
  367. * *
  368. * precision-- The precision of the result MP number -- maximum precision. *
  369. * *
  370. * OUTPUT: none *
  371. * *
  372. * WARNINGS: Be sure the result MP precision is sufficient to hold the decoded number or *
  373. * else the result is undefined. *
  374. * *
  375. * HISTORY: *
  376. * 07/01/1996 JLB : Created. *
  377. *=============================================================================================*/
  378. void XMP_Unsigned_Decode(digit * result, const unsigned char * from, int frombytes, int precision)
  379. {
  380. assert(result != NULL);
  381. assert(from != NULL);
  382. assert(frombytes > 0);
  383. assert(precision > 0);
  384. int fillcount = precision * sizeof(digit) - frombytes;
  385. unsigned char * dest = (unsigned char *)&result[precision];
  386. /*
  387. ** Fill in any excess significant bytes.
  388. */
  389. int index;
  390. for (index = 0; index < fillcount; index++) {
  391. *--dest = '\0';
  392. }
  393. /*
  394. ** Move in the remaining bytes.
  395. */
  396. for (index = 0; index < frombytes; index++) {
  397. *--dest = *from++;
  398. }
  399. }
  400. /***********************************************************************************************
  401. * XMP_Significance -- Fetch the precision (bytes) of the MP number. *
  402. * *
  403. * This routine will return with the precision of the MP number expressed as bytes. The *
  404. * MP number is presumed unsigned. *
  405. * *
  406. * INPUT: number -- Pointer to the MP number to examine. *
  407. * *
  408. * precision-- The precision of the MP number to examine. *
  409. * *
  410. * OUTPUT: Returns with the minimum number of bytes consumed by this MP number. *
  411. * *
  412. * WARNINGS: Passing a signed MP number to this routine will return an artificially greater *
  413. * precision than it really is. *
  414. * *
  415. * HISTORY: *
  416. * 07/01/1996 JLB : Created. *
  417. *=============================================================================================*/
  418. int XMP_Significance(const digit * number, int precision)
  419. {
  420. assert(number != NULL);
  421. assert(precision > 0);
  422. number += precision;
  423. do {
  424. if (*(--number)) break;
  425. } while (--precision);
  426. return(precision);
  427. }
  428. /***********************************************************************************************
  429. * XMP_Inc -- Increment an MP number by one. *
  430. * *
  431. * This will increment the MP number by one. *
  432. * *
  433. * INPUT: number -- Pointer to the MP number to increment. *
  434. * *
  435. * precision-- The precision of the MP number. *
  436. * *
  437. * OUTPUT: none *
  438. * *
  439. * WARNINGS: If the number wraps around the maximum precision, the results are undefined. *
  440. * *
  441. * HISTORY: *
  442. * 07/01/1996 JLB : Created. *
  443. *=============================================================================================*/
  444. void XMP_Inc(digit * number, int precision)
  445. {
  446. assert(number != NULL);
  447. assert(precision > 0);
  448. do {
  449. if (++(*number)) break;
  450. number++;
  451. } while (--precision);
  452. }
  453. /***********************************************************************************************
  454. * XMP_Dec -- Decrement the MP number by one. *
  455. * *
  456. * Use this routine to decrement the specified MP number by one. *
  457. * *
  458. * INPUT: number -- Pointer to the MP number to decrement. *
  459. * *
  460. * precision-- The precision of the MP number to decrement. *
  461. * *
  462. * OUTPUT: none *
  463. * *
  464. * WARNINGS: If the number wraps below zero, the results are undefined. *
  465. * *
  466. * HISTORY: *
  467. * 07/01/1996 JLB : Created. *
  468. *=============================================================================================*/
  469. void XMP_Dec(digit * number, int precision)
  470. {
  471. assert(number != NULL);
  472. assert(precision > 0);
  473. do {
  474. *number -= 1;
  475. if ((*number) != ~(digit)0) break;
  476. number++;
  477. } while (--precision);
  478. }
  479. /***********************************************************************************************
  480. * XMP_Neg -- Negate the specified MP number. *
  481. * *
  482. * This routine will negate (reverse sign) of the specified MP number. *
  483. * *
  484. * INPUT: number -- Pointer to the MP number to negate. *
  485. * *
  486. * precision-- The precision of the MP number. *
  487. * *
  488. * OUTPUT: none *
  489. * *
  490. * WARNINGS: none *
  491. * *
  492. * HISTORY: *
  493. * 07/01/1996 JLB : Created. *
  494. *=============================================================================================*/
  495. void XMP_Neg(digit * number, int precision)
  496. {
  497. assert(number != NULL);
  498. assert(precision > 0);
  499. XMP_Not(number, precision);
  500. XMP_Inc(number, precision);
  501. }
  502. /***********************************************************************************************
  503. * XMP_Abs -- Perform an absolute value on the specified MP number. *
  504. * *
  505. * This will perform the absolute value function on the specified MP number. That is, if *
  506. * the MP number is negative, it will be transformed into a positive number. If the number *
  507. * is already positive, then it will be left alone. *
  508. * *
  509. * INPUT: number -- Pointer to the MP number to ABS. *
  510. * *
  511. * precision-- The precision of the MP number. *
  512. * *
  513. * OUTPUT: none *
  514. * *
  515. * WARNINGS: none *
  516. * *
  517. * HISTORY: *
  518. * 07/01/1996 JLB : Created. *
  519. *=============================================================================================*/
  520. void XMP_Abs(digit * number, int precision)
  521. {
  522. assert(number != NULL);
  523. assert(precision > 0);
  524. if (XMP_Is_Negative(number, precision)) {
  525. XMP_Neg(number, precision);
  526. }
  527. }
  528. /***********************************************************************************************
  529. * XMP_Shift_Right_Bits -- Shift the MP number right by specified bit count. *
  530. * *
  531. * Use this routine to perform a right shift of the MP number by the number of bits *
  532. * specified. *
  533. * *
  534. * INPUT: number -- Pointer to the MP number to perform the shift upon. *
  535. * *
  536. * bits -- The number of bits to shift. *
  537. * *
  538. * precision-- The precision of the MP number. *
  539. * *
  540. * OUTPUT: none *
  541. * *
  542. * WARNINGS: This is an unsigned shift. *
  543. * *
  544. * HISTORY: *
  545. * 07/01/1996 JLB : Created. *
  546. *=============================================================================================*/
  547. void XMP_Shift_Right_Bits(digit * number, int bits, int precision)
  548. {
  549. assert(number != NULL);
  550. assert(bits >= 0);
  551. assert(precision > 0);
  552. if (bits == 0) return; /* shift zero bits is a no-op */
  553. /*
  554. ** If the shift is by whole bytes, then the shift operation can
  555. ** be performed very quickly.
  556. */
  557. if (bits == UNITSIZE) {
  558. number += precision;
  559. digit carry = 0;
  560. while (precision--) {
  561. number--;
  562. digit temp = *number;
  563. *number = carry;
  564. carry = temp;
  565. }
  566. return;
  567. }
  568. /*
  569. ** If the number of bits to shift is less than one byte, then the
  570. ** shift operation is a relatively simple "ripple" effect through
  571. ** the MP number buffer.
  572. */
  573. if (bits < UNITSIZE) {
  574. number += precision;
  575. digit carry = 0;
  576. digit bitmask = (1L << bits) - 1;
  577. int unbits = UNITSIZE - bits;
  578. while (precision--) {
  579. number--;
  580. digit temp = *number & bitmask;
  581. *number >>= bits;
  582. *number |= carry << unbits;
  583. carry = temp;
  584. }
  585. return;
  586. }
  587. /*
  588. ** General purpose slow right.
  589. */
  590. int digits_to_shift = bits / UNITSIZE;
  591. int bits_to_shift = bits % UNITSIZE;
  592. int index;
  593. for (index = digits_to_shift; index < (precision-1); index++) {
  594. *number = (*(number + digits_to_shift) >> bits_to_shift) | (*(number + (digits_to_shift + 1)) << (UNITSIZE - bits_to_shift));
  595. number++;
  596. }
  597. if (digits_to_shift < precision) {
  598. *number = (*(number + digits_to_shift) >> bits_to_shift);
  599. number++;
  600. }
  601. for (index= 0; index < min(digits_to_shift, precision); index++) {
  602. *number++ = 0;
  603. }
  604. }
  605. /***********************************************************************************************
  606. * XMP_Shift_Left_Bits -- Shifts the MP number left by the specified bit count. *
  607. * *
  608. * Use this routine to perform a left shift of the specified MP number. *
  609. * *
  610. * INPUT: number -- Pointer to the MP number to perform the shift operation on. *
  611. * *
  612. * bits -- The number of bits to shift the MP number leftward. *
  613. * *
  614. * precision-- The precision of the MP number. *
  615. * *
  616. * OUTPUT: none *
  617. * *
  618. * WARNINGS: none *
  619. * *
  620. * HISTORY: *
  621. * 07/01/1996 JLB : Created. *
  622. *=============================================================================================*/
  623. void XMP_Shift_Left_Bits(digit * number, int bits, int precision)
  624. {
  625. assert(number != NULL);
  626. assert(bits >= 0);
  627. assert(precision > 0);
  628. if (bits == 0) return; /* shift zero bits is a no-op */
  629. /*
  630. ** If the shift is by whole bytes, then the shift operation can
  631. ** be performed very quickly.
  632. */
  633. if (bits == UNITSIZE) {
  634. digit carry = 0;
  635. while (precision--) {
  636. digit temp = *number;
  637. *number = carry;
  638. carry = temp;
  639. number++;
  640. }
  641. return;
  642. }
  643. /*
  644. ** If the number of bits to shift is less than one byte, then the
  645. ** shift operation is a relatively simple "ripple" effect through
  646. ** the MP number buffer.
  647. */
  648. if (bits < UNITSIZE) {
  649. digit carry = 0;
  650. digit bitmask = ~(((digit)-1) >> bits);
  651. int unbits = UNITSIZE - bits; /* shift bits must be <= UNITSIZE */
  652. while (precision--) {
  653. digit temp = *number & bitmask;
  654. *number = (*number << bits) | (carry >> unbits);
  655. carry = temp;
  656. number++;
  657. }
  658. return;
  659. }
  660. /*
  661. ** General purpose slow left;
  662. */
  663. int digits_to_shift = bits / UNITSIZE;
  664. int bits_to_shift = bits % UNITSIZE;
  665. int index;
  666. number += precision-1;
  667. for (index = digits_to_shift; index < (precision-1); index++) {
  668. *number = (*(number - digits_to_shift) << bits_to_shift) | (*(number - (digits_to_shift + 1)) >> (UNITSIZE - bits_to_shift));
  669. number--;
  670. }
  671. if (digits_to_shift < precision) {
  672. *number = (*(number - digits_to_shift) << bits_to_shift);
  673. number--;
  674. }
  675. for (index = 0; index < min(digits_to_shift, precision); index++) {
  676. *number-- = 0;
  677. }
  678. }
  679. /***********************************************************************************************
  680. * XMP_Rotate_Left -- Rotate specified MP number leftward. *
  681. * *
  682. * This routine will rotate the MP number to the left by one bit. The rotation passes bits *
  683. * through a "carry" bit position. The initial value of this "carry" bit is passed to the *
  684. * routine and the final value is returned as the result. *
  685. * *
  686. * INPUT: number -- Pointer to the MP number to perform the left rotate upon. *
  687. * *
  688. * carry -- The initial value of the "carry" bit. *
  689. * *
  690. * precision-- The precision of the MP number specified. *
  691. * *
  692. * OUTPUT: Returns with the final value of the carry bit. This is the the bit value of the *
  693. * upper most bit of the MP number prior to the rotate operation. *
  694. * *
  695. * WARNINGS: none *
  696. * *
  697. * HISTORY: *
  698. * 07/01/1996 JLB : Created. *
  699. *=============================================================================================*/
  700. bool XMP_Rotate_Left(digit * number, bool carry, int precision)
  701. {
  702. assert(number != NULL);
  703. assert(precision > 0);
  704. while (precision--) {
  705. bool temp = ((*number & UPPER_MOST_BIT) != 0);
  706. *number = (*number << 1);
  707. if (carry) *number = *number + 1;
  708. carry = temp;
  709. number++;
  710. }
  711. return carry;
  712. }
  713. /***********************************************************************************************
  714. * XMP_Not -- Perform bitwise NOT operation on MP number. *
  715. * *
  716. * Perform a bitwise NOT operation. *
  717. * *
  718. * INPUT: number -- Pointer to the MP number to operate on. *
  719. * *
  720. * precision-- The precision of the MP number. *
  721. * *
  722. * OUTPUT: none *
  723. * *
  724. * WARNINGS: none *
  725. * *
  726. * HISTORY: *
  727. * 07/01/1996 JLB : Created. *
  728. *=============================================================================================*/
  729. void XMP_Not(digit * number, int precision)
  730. {
  731. assert(number != NULL);
  732. assert(precision > 0);
  733. for (int index = 0; index < precision; index++) {
  734. *number = ~(*number);
  735. number++;
  736. }
  737. }
  738. /***********************************************************************************************
  739. * XMP_Init -- Initialize an MP number to a starting value. *
  740. * *
  741. * This will initialize (assign) a number to an MP number. The initial value is limited *
  742. * to the precision allowed by a DIGIT type. *
  743. * *
  744. * INPUT: number -- Pointer to the MP number to initialize. *
  745. * *
  746. * value -- Initial integer value to assign to the MP number. *
  747. * *
  748. * precision-- The precision of the MP number. *
  749. * *
  750. * OUTPUT: none *
  751. * *
  752. * WARNINGS: none *
  753. * *
  754. * HISTORY: *
  755. * 07/01/1996 JLB : Created. *
  756. *=============================================================================================*/
  757. void XMP_Init(digit * number, digit value, int precision)
  758. {
  759. assert(number != NULL);
  760. assert(precision > 0);
  761. memset(number, '\0', precision * sizeof(digit));
  762. *number = value;
  763. }
  764. /***********************************************************************************************
  765. * XMP_Count_Bits -- Count the total number of bits (precision) in MP number. *
  766. * *
  767. * This routine will count the maximum number of bits used by this MP number. The result *
  768. * could be referred to as the "bit precision" of the MP number. *
  769. * *
  770. * INPUT: number -- Pointer to the MP number to examine. *
  771. * *
  772. * precision-- The (digit) precision of the MP number. *
  773. * *
  774. * OUTPUT: Returns with the number of significant bits in the MP number. *
  775. * *
  776. * WARNINGS: none *
  777. * *
  778. * HISTORY: *
  779. * 07/01/1996 JLB : Created. *
  780. *=============================================================================================*/
  781. unsigned XMP_Count_Bits(const digit * number, int precision)
  782. {
  783. assert(number != NULL);
  784. assert(precision > 0);
  785. int sub_precision = XMP_Significance(number, precision);
  786. if (!sub_precision) return(0);
  787. int total_bit_count = XMP_Digits_To_Bits(sub_precision);
  788. number += sub_precision-1;
  789. digit high_bit_mask = UPPER_MOST_BIT;
  790. while (!((*number) & high_bit_mask)) {
  791. high_bit_mask >>= 1;
  792. total_bit_count--;
  793. }
  794. return(total_bit_count);
  795. }
  796. /***********************************************************************************************
  797. * XMP_Count_Bytes -- Counts the number of precision bytes in MP number. *
  798. * *
  799. * This routine will scan the MP number and determine the number of bytes needed to *
  800. * represent the MP number. Consider the result the "byte precision" of the number. *
  801. * *
  802. * INPUT: number -- Pointer to the MP number to examine. *
  803. * *
  804. * precision-- Precision of the MP number. *
  805. * *
  806. * OUTPUT: Returns with the number of bytes required to represent the precision of the number.*
  807. * *
  808. * WARNINGS: none *
  809. * *
  810. * HISTORY: *
  811. * 07/01/1996 JLB : Created. *
  812. *=============================================================================================*/
  813. int XMP_Count_Bytes(const digit * number, int precision)
  814. {
  815. unsigned char * ptr = (unsigned char *)number;
  816. int count = 0;
  817. for (unsigned index = 0; index < precision*sizeof(digit); index++) {
  818. if (!*ptr) break;
  819. count++;
  820. ptr++;
  821. }
  822. return(count);
  823. }
  824. /***********************************************************************************************
  825. * XMP_Move -- Assign one MP number to another. *
  826. * *
  827. * This will move one MP number over the top of another. *
  828. * *
  829. * INPUT: dest -- Destination MP number (will get clobbered). *
  830. * *
  831. * source -- Source MP number. *
  832. * *
  833. * precision-- Precision of both MP numbers. *
  834. * *
  835. * OUTPUT: none *
  836. * *
  837. * WARNINGS: Both MP numbers must have the same precision. *
  838. * *
  839. * HISTORY: *
  840. * 07/01/1996 JLB : Created. *
  841. *=============================================================================================*/
  842. void XMP_Move(digit * dest, digit const * source, int precision)
  843. {
  844. memcpy(dest, source, precision * sizeof(digit));
  845. }
  846. /***********************************************************************************************
  847. * XMP_Compare -- Compare one MP number with another. *
  848. * *
  849. * This routine will compare two MP numbers. It will return a value indicating which MP *
  850. * number is greater or if they are equal. *
  851. * *
  852. * INPUT: left_number -- The left hand MP number. *
  853. * *
  854. * right_number-- The right hand MP number. *
  855. * *
  856. * precision -- The precision of the MP numbers. *
  857. * *
  858. * OUTPUT: Returns -1 if the left_number is less than the right_number. *
  859. * Returns 1 if the left_number is greater than the right number. *
  860. * Returns 0 if both numbers are identical. *
  861. * *
  862. * WARNINGS: Both numbers must have the same precision. *
  863. * *
  864. * HISTORY: *
  865. * 07/01/1996 JLB : Created. *
  866. *=============================================================================================*/
  867. int XMP_Compare(const digit * left_number, const digit * right_number, int precision)
  868. {
  869. left_number += precision-1;
  870. right_number += precision-1;
  871. do {
  872. if (*left_number < *right_number) return -1;
  873. if (*left_number > *right_number) return 1;
  874. left_number--;
  875. right_number--;
  876. } while (--precision);
  877. return 0;
  878. }
  879. /***********************************************************************************************
  880. * XMP_Add -- Add two MP numbers with a carry option. *
  881. * *
  882. * Use this routine to add one MP number to another. There is an optional "carry" value *
  883. * that (when true) will add an additional 1 to the result. *
  884. * *
  885. * INPUT: result -- Pointer to the MP buffer that will hold the result. This can be the *
  886. * same value as the left_number or right_number pointers. *
  887. * *
  888. * left_number -- The left hand MP number. *
  889. * *
  890. * right_number-- The right hand MP number. *
  891. * *
  892. * carry -- Optional carry flag (typically this will be false). *
  893. * *
  894. * precision -- The precision of the numbers involved. *
  895. * *
  896. * OUTPUT: Returns with the carry flag after the addition. If the value is true then an *
  897. * overflow occurred. *
  898. * *
  899. * WARNINGS: none *
  900. * *
  901. * HISTORY: *
  902. * 07/01/1996 JLB : Created. *
  903. *=============================================================================================*/
  904. bool XMP_Add(digit * result, const digit * left_number, const digit * right_number, bool carry, int precision)
  905. {
  906. while (precision--) {
  907. digit term = *left_number + *right_number;
  908. digit final = term + carry;
  909. carry = (term < *left_number || (carry && final == 0));
  910. right_number++;
  911. left_number++;
  912. *result++ = final;
  913. }
  914. return(carry);
  915. }
  916. /***********************************************************************************************
  917. * XMP_Add_Int -- Add an integer to an MP number (with carry). *
  918. * *
  919. * This routine will add an integer number to an MP number. There is an optional carry *
  920. * parameter. If the carry flag is true, and additional 1 will be added to the result. *
  921. * This routine is much faster than adding two MP numbers together. *
  922. * *
  923. * INPUT: result -- Pointer to the result MP number. This pointer can be the same as *
  924. * the left_number parameter. *
  925. * *
  926. * left_number -- Pointer to the left hand MP number. *
  927. * *
  928. * right_number-- The integer number to add to the left hand number. *
  929. * *
  930. * carry -- Input carry flag. If this is true, then an additional one will be *
  931. * added to the result. *
  932. * *
  933. * precision -- The precision of the MP numbers. *
  934. * *
  935. * OUTPUT: Returns with the result carry flag. A true value means the addition overflowed. *
  936. * *
  937. * WARNINGS: All MP numbers must share the same precision. Negative numbers are not *
  938. * supported. *
  939. * *
  940. * HISTORY: *
  941. * 07/01/1996 JLB : Created. *
  942. *=============================================================================================*/
  943. bool XMP_Add_Int(digit * result, const digit * left_number, digit right_number, bool carry, int precision)
  944. {
  945. while (precision--) {
  946. digit term = *left_number + right_number;
  947. digit final = term + carry;
  948. carry = (term < *left_number || (carry && final == 0));
  949. right_number = 0;
  950. left_number++;
  951. *result++ = final;
  952. }
  953. return(carry);
  954. }
  955. /***********************************************************************************************
  956. * XMP_Sub -- Subtract one MP number from another (with borrow). *
  957. * *
  958. * This routine will subtract one MP number from another. There is an optional borrow *
  959. * flag that can be specified. *
  960. * *
  961. * INPUT: result -- Pointer to the MP number that will hold the result. This pointer *
  962. * can be the same as the left_number or right_number parameters. *
  963. * *
  964. * left_number -- The left hand number (value will be subtracted from this). *
  965. * *
  966. * right_number-- The right hand number (the value to subtract from the left number) *
  967. * *
  968. * borrow -- The optional borrow flag. If this flag is true, the an extra one *
  969. * will be subtracted from the result. *
  970. * *
  971. * precision -- The precision of the MP numbers involved. *
  972. * *
  973. * OUTPUT: Returns with the borrow result flag. If the value is true, then an underflow *
  974. * occurred during subtraction. *
  975. * *
  976. * WARNINGS: All MP numbers must share the same precision. *
  977. * *
  978. * HISTORY: *
  979. * 07/01/1996 JLB : Created. *
  980. *=============================================================================================*/
  981. bool XMP_Sub(digit * result, const digit * left_number, const digit * right_number, bool borrow, int precision)
  982. {
  983. const unsigned short * left_number_ptr = (const unsigned short *)left_number;
  984. const unsigned short * right_number_ptr = (const unsigned short *)right_number;
  985. unsigned short * result_ptr = (unsigned short *)result;
  986. precision *= 2;
  987. while (precision--) {
  988. digit x = (digit) *left_number_ptr - (digit) *right_number_ptr - (digit) borrow;
  989. right_number_ptr++;
  990. left_number_ptr++;
  991. *result_ptr++ = (unsigned short)x;
  992. borrow = (((1L << 16) & x) != 0L);
  993. }
  994. return (borrow);
  995. }
  996. /***********************************************************************************************
  997. * XMP_Sub_Int -- Subtract an integer from an MP number (with borrow). *
  998. * *
  999. * This will subtract an integer from the specified MP number. There is an optional borrow *
  1000. * flag available. *
  1001. * *
  1002. * INPUT: result -- Pointer to the MP buffer that will hold the result. *
  1003. * *
  1004. * left_number -- Pointer to the MP number that will be subtracted FROM. *
  1005. * *
  1006. * right_number-- The integer to subtract from the left hand number. *
  1007. * *
  1008. * borrow -- The optional borrow flag. If this value is true, then an extra one *
  1009. * will be subtracted from the result. *
  1010. * *
  1011. * precision -- The precision of the MP numbers involved. *
  1012. * *
  1013. * OUTPUT: Returns with the borrow flag of the result. If this value is true, then an *
  1014. * underflow occurred during subtraction. *
  1015. * *
  1016. * WARNINGS: The precision must be identical between the MP numbers involved. *
  1017. * *
  1018. * HISTORY: *
  1019. * 07/01/1996 JLB : Created. *
  1020. *=============================================================================================*/
  1021. bool XMP_Sub_Int(digit * result, const digit * left_number, unsigned short right_number, bool borrow, int precision)
  1022. {
  1023. const unsigned short * left_number_ptr = (const unsigned short *)left_number;
  1024. unsigned short * result_ptr = (unsigned short *)result;
  1025. precision *= 2;
  1026. while (precision--) {
  1027. digit x = (digit) *left_number_ptr - right_number - borrow;
  1028. left_number_ptr++;
  1029. *result_ptr++ = (unsigned short)x;
  1030. borrow = (((1L << 16) & x) != 0L);
  1031. right_number = 0;
  1032. }
  1033. return (borrow);
  1034. }
  1035. /***********************************************************************************************
  1036. * XMP_Unsigned_Mult -- Multiply two unsigned MP numbers together. *
  1037. * *
  1038. * This routine will multiply two MP numbers together. The result will have the sum of the *
  1039. * significance of the two. *
  1040. * *
  1041. * INPUT: prod -- Pointer to the product MP buffer that will hold the result. *
  1042. * *
  1043. * multiplicand-- Pointer to the multiplicand MP number. *
  1044. * *
  1045. * multiplier -- Pointer to the multiplier MP number. *
  1046. * *
  1047. * precision -- The precision of the MP numbers. *
  1048. * *
  1049. * OUTPUT: none *
  1050. * *
  1051. * WARNINGS: Be sure the product will fit within the precision of the result. *
  1052. * *
  1053. * HISTORY: *
  1054. * 07/01/1996 JLB : Created. *
  1055. *=============================================================================================*/
  1056. int XMP_Unsigned_Mult(digit * prod, const digit * multiplicand, const digit * multiplier, int precision)
  1057. {
  1058. XMP_Init(prod, 0, precision);
  1059. /*
  1060. ** Multiplying by zero is always a zero product.
  1061. */
  1062. if (XMP_Test_Eq_Int(multiplicand, 0, precision) || XMP_Test_Eq_Int(multiplier, 0, precision)) {
  1063. return 0;
  1064. }
  1065. int total_bit_count = XMP_Count_Bits(multiplier, precision);
  1066. digit high_bit_mask = XMP_Bits_To_Mask(total_bit_count);
  1067. int sub_precision = XMP_Bits_To_Digits(total_bit_count);
  1068. if (!sub_precision) return(0);
  1069. multiplier += sub_precision;
  1070. while (total_bit_count--) {
  1071. XMP_Shift_Left_Bits(prod, 1, precision);
  1072. if ((*(multiplier-1)) & high_bit_mask) {
  1073. XMP_Add(prod, prod, multiplicand, 0, precision);
  1074. }
  1075. high_bit_mask >>= 1;
  1076. if (!high_bit_mask) {
  1077. high_bit_mask = UPPER_MOST_BIT;
  1078. multiplier--;
  1079. }
  1080. }
  1081. return 0;
  1082. }
  1083. /***********************************************************************************************
  1084. * XMP_Unsigned_Mult_Int -- Multiply an MP number by a simple integer. *
  1085. * *
  1086. * This is a very fast multiply since the multiplier is just an integer integral. *
  1087. * *
  1088. * INPUT: prod -- Pointer to the product MP number. *
  1089. * *
  1090. * multiplicand-- Pointer to the MP number that is the multiplicand. *
  1091. * *
  1092. * multiplier -- The integral integer that is the multiplier. *
  1093. * *
  1094. * precision -- The precision of the MP numbers. *
  1095. * *
  1096. * OUTPUT: none *
  1097. * *
  1098. * WARNINGS: The multiplier must fit in a signed integer (although it isn't a signed value). *
  1099. * *
  1100. * HISTORY: *
  1101. * 07/02/1996 JLB : Created. *
  1102. *=============================================================================================*/
  1103. int XMP_Unsigned_Mult_Int(digit * prod, const digit * multiplicand, short multiplier, int precision)
  1104. {
  1105. const unsigned short * m2 = (const unsigned short *)multiplicand;
  1106. unsigned short * pr = (unsigned short *)prod;
  1107. unsigned long carry = 0;
  1108. for (int i = 0; i < precision*2; ++i) {
  1109. unsigned long p = (((unsigned long)multiplier) * *m2) + carry;;
  1110. *pr = (unsigned short) p;
  1111. carry = p >> 16;
  1112. m2++;
  1113. pr++;
  1114. }
  1115. /* Add carry to the next higher word of product / dividend */
  1116. // *pr += (unsigned short)carry;
  1117. return(0);
  1118. }
  1119. /***********************************************************************************************
  1120. * XMP_Signed_Mult_Int -- Multiply an MP number by a signed simple integer. *
  1121. * *
  1122. * This will multiply the specified integer with the MP number. It is a much faster *
  1123. * multiply than when multiplying two MP numbers. *
  1124. * *
  1125. * INPUT: prod -- Pointer to the product MP number. *
  1126. * *
  1127. * multiplicand-- Pointer to the MP number that serves as the multiplicand. *
  1128. * *
  1129. * multiplier -- The simple integral integer used as the multiplier. *
  1130. * *
  1131. * precision -- The precision of the MP numbers involved. *
  1132. * *
  1133. * OUTPUT: none *
  1134. * *
  1135. * WARNINGS: The multiplier must fist within a signed short integer. *
  1136. * *
  1137. * HISTORY: *
  1138. * 07/02/1996 JLB : Created. *
  1139. *=============================================================================================*/
  1140. int XMP_Signed_Mult_Int(digit * prod, const digit * multiplicand, signed short multiplier, int precision)
  1141. {
  1142. if (XMP_Is_Negative(multiplicand, precision)) {
  1143. digit abs_multiplicand[MAX_UNIT_PRECISION];
  1144. XMP_Move(abs_multiplicand, multiplicand, precision);
  1145. XMP_Neg(abs_multiplicand, precision);
  1146. if (multiplier < 0) {
  1147. multiplier = (signed short)-multiplier;
  1148. XMP_Unsigned_Mult_Int(prod, abs_multiplicand, multiplier, precision);
  1149. } else {
  1150. XMP_Unsigned_Mult_Int(prod, abs_multiplicand, multiplier, precision);
  1151. XMP_Neg(prod, precision);
  1152. }
  1153. } else {
  1154. if (multiplier < 0) {
  1155. multiplier = (signed short)-multiplier;
  1156. XMP_Unsigned_Mult_Int(prod, multiplicand, multiplier, precision);
  1157. XMP_Neg(prod, precision);
  1158. } else {
  1159. XMP_Unsigned_Mult_Int(prod, multiplicand, multiplier, precision);
  1160. }
  1161. }
  1162. return(0);
  1163. }
  1164. /***********************************************************************************************
  1165. * XMP_Signed_Mult -- A signed multiply between two MP numbers. *
  1166. * *
  1167. * This routine will perform a multiply between two signed MP numbers. *
  1168. * *
  1169. * INPUT: prod -- Pointer to the product MP number buffer. *
  1170. * *
  1171. * multiplicand-- Pointer to the multiplicand MP number. *
  1172. * *
  1173. * multiplier -- Pointer to the multiplier MP number. *
  1174. * *
  1175. * precision -- The precision of the MP numbers involved. *
  1176. * *
  1177. * OUTPUT: none *
  1178. * *
  1179. * WARNINGS: This is not a very fast routine. *
  1180. * *
  1181. * HISTORY: *
  1182. * 07/02/1996 JLB : Created. *
  1183. *=============================================================================================*/
  1184. int XMP_Signed_Mult(digit * prod, const digit * multiplicand, const digit * multiplier, int precision)
  1185. {
  1186. if (XMP_Is_Negative(multiplicand, precision)) {
  1187. digit abs_multiplicand[MAX_UNIT_PRECISION];
  1188. XMP_Move(abs_multiplicand, multiplicand, precision);
  1189. XMP_Neg(abs_multiplicand, precision);
  1190. if (XMP_Is_Negative(multiplier, precision)) {
  1191. digit abs_multiplier[MAX_UNIT_PRECISION];
  1192. XMP_Move(abs_multiplier, multiplier, precision);
  1193. XMP_Neg(abs_multiplier, precision);
  1194. XMP_Unsigned_Mult(prod, abs_multiplicand, abs_multiplier, precision);
  1195. } else {
  1196. XMP_Unsigned_Mult(prod, abs_multiplicand, multiplier, precision);
  1197. XMP_Neg(prod, precision);
  1198. }
  1199. } else {
  1200. if (XMP_Is_Negative(multiplier, precision)) {
  1201. digit abs_multiplier[MAX_UNIT_PRECISION];
  1202. XMP_Move(abs_multiplier, multiplier, precision);
  1203. XMP_Neg(abs_multiplier, precision);
  1204. XMP_Unsigned_Mult(prod, multiplicand, abs_multiplier, precision);
  1205. XMP_Neg(prod, precision);
  1206. } else {
  1207. XMP_Unsigned_Mult(prod, multiplicand, multiplier, precision);
  1208. }
  1209. }
  1210. return(0);
  1211. }
  1212. /***********************************************************************************************
  1213. * XMP_Unsigned_Div_Int -- Perform a short integer divide into an MP number. *
  1214. * *
  1215. * This routine performs a fast divide of the specified MP dividend by a simple *
  1216. * short integer. The division is an UNSIGNED division however. *
  1217. * *
  1218. * INPUT: quotient -- Pointer to the MP number buffer where the quotient will go. *
  1219. * *
  1220. * dividend -- Pointer to the MP number that serves as the dividend. *
  1221. * *
  1222. * divisor -- The simple signed short integer that serves as the divisor. *
  1223. * *
  1224. * precision -- The precision that is used by the MP numbers involved. *
  1225. * *
  1226. * OUTPUT: Returns with the remainder of the division. *
  1227. * *
  1228. * WARNINGS: This is an UNSIGNED divide even. *
  1229. * *
  1230. * HISTORY: *
  1231. * 07/02/1996 JLB : Created. *
  1232. *=============================================================================================*/
  1233. unsigned short XMP_Unsigned_Div_Int(digit * quotient, digit const * dividend, unsigned short divisor, int precision)
  1234. {
  1235. if (!divisor) return 0; /* zero divisor means divide error */
  1236. unsigned short remainder = 0;
  1237. XMP_Init(quotient, 0, precision);
  1238. int total_bit_count = XMP_Count_Bits(dividend, precision);
  1239. int digit_precision = XMP_Bits_To_Digits(total_bit_count);
  1240. digit const * dividend_ptr = dividend + (digit_precision-1);
  1241. if (!digit_precision) return(0);
  1242. digit high_bit_mask = XMP_Bits_To_Mask(total_bit_count);
  1243. digit * quotient_ptr = quotient + (digit_precision-1);
  1244. while (total_bit_count--) {
  1245. remainder <<= 1;
  1246. if ((*dividend_ptr) & high_bit_mask) remainder++;
  1247. if (remainder >= divisor) {
  1248. remainder -= divisor;
  1249. *quotient_ptr |= high_bit_mask;
  1250. }
  1251. high_bit_mask >>= 1;
  1252. if (!high_bit_mask) {
  1253. high_bit_mask = UPPER_MOST_BIT;
  1254. --dividend_ptr;
  1255. --quotient_ptr;
  1256. }
  1257. }
  1258. return(remainder);
  1259. }
  1260. /***********************************************************************************************
  1261. * XMP_Unsigned_Div -- Unsigned divide of one MP number into another. *
  1262. * *
  1263. * This will perform the (dog slow) divide of one MP number into another. Because of the *
  1264. * slowness of this routine, both the quotient and the remainder are available as a *
  1265. * result of the operation. *
  1266. * *
  1267. * INPUT: remainder -- Pointer to the MP buffer that will hold the remainder of the divide.*
  1268. * *
  1269. * quotient -- Pointer to the MP buffer that will hold the quotient of the divide. *
  1270. * *
  1271. * dividend -- The MP dividend (numerator) number. *
  1272. * *
  1273. * divisor -- The MP divisor (denominator) number. *
  1274. * *
  1275. * precision -- The precision of the MP numbers involved. *
  1276. * *
  1277. * OUTPUT: none *
  1278. * *
  1279. * WARNINGS: This is very slow. *
  1280. * *
  1281. * HISTORY: *
  1282. * 07/02/1996 JLB : Created. *
  1283. *=============================================================================================*/
  1284. int XMP_Unsigned_Div(digit * remainder, digit * quotient, digit const * dividend, digit const * divisor, int precision)
  1285. {
  1286. // check for divide by zero.
  1287. if (XMP_Test_Eq_Int(divisor, 0, precision)) return(-1);
  1288. XMP_Init(remainder, 0, precision);
  1289. XMP_Init(quotient, 0, precision);
  1290. int total_bit_count = XMP_Count_Bits(dividend, precision);
  1291. int digit_precision = XMP_Bits_To_Digits(total_bit_count);
  1292. if (!digit_precision) return(0);
  1293. digit const * dividend_ptr = dividend + (digit_precision-1);
  1294. digit * quotient_ptr = quotient + (digit_precision-1);
  1295. digit high_bit_mask = XMP_Bits_To_Mask(total_bit_count);
  1296. while (total_bit_count--) {
  1297. XMP_Shift_Left_Bits(remainder, 1, precision);
  1298. if (((*dividend_ptr) & high_bit_mask) != 0) {
  1299. XMP_Inc(remainder, precision);
  1300. }
  1301. if (XMP_Compare(remainder, divisor, precision) >= 0) {
  1302. XMP_Sub(remainder, remainder, divisor, 0, precision);
  1303. *quotient_ptr |= high_bit_mask;
  1304. }
  1305. high_bit_mask >>= 1;
  1306. if (!high_bit_mask) {
  1307. high_bit_mask = UPPER_MOST_BIT;
  1308. dividend_ptr--;
  1309. quotient_ptr--;
  1310. }
  1311. }
  1312. return 0;
  1313. }
  1314. /***********************************************************************************************
  1315. * XMP_Signed_Div -- Signed divide of one MP number into another. *
  1316. * *
  1317. * This will perform a signed divide (very very slow) of one MP number into another. *
  1318. * Because of the slow nature of this routine, both the quotient and the remainder are *
  1319. * available as results. *
  1320. * *
  1321. * INPUT: remainder -- Pointer to the buffer that will hold the remainder of the divide. *
  1322. * *
  1323. * quotient -- Pointer to the buffer that will hold the quotient of the divide. *
  1324. * *
  1325. * dividend -- The dividend (numerator) MP number. *
  1326. * *
  1327. * divisor -- The divisor (denominator) MP number. *
  1328. * *
  1329. * precision -- The precision of the MP numbers involved. *
  1330. * *
  1331. * OUTPUT: none *
  1332. * *
  1333. * WARNINGS: This is very very slow. *
  1334. * *
  1335. * HISTORY: *
  1336. * 07/02/1996 JLB : Created. *
  1337. *=============================================================================================*/
  1338. void XMP_Signed_Div(digit * remainder, digit * quotient, digit const * dividend, digit const * divisor, int precision)
  1339. {
  1340. bool negative = false;
  1341. digit scratch_dividend[MAX_UNIT_PRECISION];
  1342. XMP_Move(scratch_dividend, dividend, precision);
  1343. digit scratch_divisor[MAX_UNIT_PRECISION];
  1344. XMP_Move(scratch_divisor, divisor, precision);
  1345. if (XMP_Is_Negative(scratch_dividend, precision)) {
  1346. XMP_Neg(scratch_dividend, precision);
  1347. negative = !negative;
  1348. }
  1349. if (XMP_Is_Negative(scratch_divisor, precision)) {
  1350. XMP_Neg(scratch_divisor, precision);
  1351. negative = !negative;
  1352. }
  1353. XMP_Unsigned_Div(remainder, quotient, scratch_dividend, scratch_divisor, precision);
  1354. if (negative) {
  1355. XMP_Neg(quotient, precision);
  1356. if (!XMP_Test_Eq_Int(remainder, 0, precision)) {
  1357. XMP_Dec(quotient, precision);
  1358. XMP_Neg(remainder, precision);
  1359. XMP_Add(remainder, remainder, scratch_divisor, 0, precision);
  1360. }
  1361. }
  1362. }
  1363. /***********************************************************************************************
  1364. * XMP_Inverse_A_Mod_B -- Inverts and performs modulus on an MP number. *
  1365. * *
  1366. * This is a utility routine that will perform an inverse on the MP number and then *
  1367. * perform a modulus of that number by another MP number. There are some algorithms that *
  1368. * require this process. *
  1369. * *
  1370. * INPUT: result -- Pointer to the MP buffer that will hold the result. *
  1371. * *
  1372. * number -- The MP number that will be inverted then modulo-ized. *
  1373. * *
  1374. * modulus -- The MP number to modulus the first number by. *
  1375. * *
  1376. * precision -- The precision of the MP numbers involved. *
  1377. * *
  1378. * OUTPUT: none *
  1379. * *
  1380. * WARNINGS: none *
  1381. * *
  1382. * HISTORY: *
  1383. * 07/02/1996 JLB : Created. *
  1384. *=============================================================================================*/
  1385. void XMP_Inverse_A_Mod_B(digit * result, digit const * number, digit const * modulus, int precision)
  1386. {
  1387. digit g[3][MAX_UNIT_PRECISION];
  1388. XMP_Move(g[0], modulus, precision);
  1389. XMP_Move(g[1], number, precision);
  1390. digit v[3][MAX_UNIT_PRECISION];
  1391. XMP_Init(v[0], 0, precision);
  1392. XMP_Init(v[1], 1, precision);
  1393. digit y[MAX_UNIT_PRECISION];
  1394. int i;
  1395. for (i = 1; !XMP_Test_Eq_Int(g[i%3], 0, precision); i++) {
  1396. XMP_Unsigned_Div(g[(i+1)%3], y, g[(i-1)%3], g[i%3], precision);
  1397. XMP_Unsigned_Mult(result, v[i%3], y, precision);
  1398. XMP_Sub(v[(i+1)%3], v[(i-1)%3], result, 0, precision);
  1399. }
  1400. if (XMP_Is_Negative(v[(i-1)%3], precision)) {
  1401. XMP_Add(v[(i-1)%3], v[(i-1)%3], modulus, 0, precision);
  1402. }
  1403. XMP_Move(result, v[(i-1)%3], precision);
  1404. }
  1405. /***********************************************************************************************
  1406. * XMP_Reciprocal -- Compute the reciprocal (inverse) of the MP number. *
  1407. * *
  1408. * Use this routine to determine the inverse of the specified MP number. The inverse is *
  1409. * defined as 1/number. *
  1410. * *
  1411. * INPUT: result -- Pointer to the result MP number buffer. *
  1412. * *
  1413. * number -- The number to be inverted. *
  1414. * *
  1415. * precision-- The precision of the MP number. *
  1416. * *
  1417. * OUTPUT: none *
  1418. * *
  1419. * WARNINGS: none *
  1420. * *
  1421. * HISTORY: *
  1422. * 07/02/1996 JLB : Created. *
  1423. *=============================================================================================*/
  1424. int XMP_Reciprocal(digit * quotient, const digit * divisor, int precision)
  1425. {
  1426. digit remainder[MAX_UNIT_PRECISION];
  1427. if (XMP_Test_Eq_Int(divisor, 0, precision)) return -1; /* zero divisor means divide error */
  1428. XMP_Init(remainder, 0, precision);
  1429. XMP_Init(quotient, 0, precision);
  1430. /* normalize and compute number of bits in quotient first */
  1431. unsigned total_bit_count = XMP_Count_Bits(divisor, precision);
  1432. digit high_bit_mask = XMP_Bits_To_Mask(total_bit_count + 1); /* bitmask within a single digit */
  1433. int sub_precision = XMP_Bits_To_Digits(total_bit_count + 1);
  1434. XMP_Set_Bit(remainder, total_bit_count - 1);
  1435. /* rescale quotient to precision of divisor bits */
  1436. quotient += sub_precision-1;
  1437. while (total_bit_count--) {
  1438. XMP_Shift_Left_Bits(remainder, 1, precision);
  1439. if (XMP_Compare(remainder, divisor, precision) >= 0) {
  1440. XMP_Sub(remainder, remainder, divisor, 0, precision);
  1441. *quotient |= high_bit_mask;
  1442. }
  1443. high_bit_mask >>= 1;
  1444. if (!high_bit_mask) {
  1445. high_bit_mask = UPPER_MOST_BIT;
  1446. quotient--;
  1447. }
  1448. }
  1449. XMP_Init(remainder, 0, precision);
  1450. return 0;
  1451. }
  1452. /***********************************************************************************************
  1453. * XMP_Decode_ASCII -- Convert ASCII into an MP number. *
  1454. * *
  1455. * This routine will convert a supplied ASCII string into an MP number. *
  1456. * *
  1457. * INPUT: str -- Pointer to the ASCII string that will be converted. *
  1458. * *
  1459. * mpn -- Pointer to the MP number buffer that will be initialized. *
  1460. * *
  1461. * precision -- The precision of the MP number. *
  1462. * *
  1463. * OUTPUT: none *
  1464. * *
  1465. * WARNINGS: none *
  1466. * *
  1467. * HISTORY: *
  1468. * 07/02/1996 JLB : Created. *
  1469. *=============================================================================================*/
  1470. void XMP_Decode_ASCII(char const * str, digit * mpn, int precision)
  1471. {
  1472. /*
  1473. ** Initialize the multiprecision number to zero. From this point
  1474. ** onward, this object can be manipulated as a regular number.
  1475. ** This is, in fact, what is done as the ascii string is parsed
  1476. ** into a working number.
  1477. */
  1478. XMP_Init(mpn, 0, precision);
  1479. /*
  1480. ** No string or zero length is considered '0'.
  1481. */
  1482. if (!str) return;
  1483. int i = strlen(str);
  1484. if (i == 0) return;
  1485. unsigned short radix; /* base 2-16 */
  1486. switch (toupper(str[i-1])) { /* classify radix select suffix character */
  1487. case '.':
  1488. radix = 10;
  1489. break;
  1490. case 'H':
  1491. radix = 16;
  1492. break;
  1493. case 'O':
  1494. radix = 8;
  1495. break;
  1496. case 'B': /* caution! 'b' is a hex digit! */
  1497. radix = 2;
  1498. break;
  1499. default:
  1500. radix = 10;
  1501. break;
  1502. }
  1503. bool minus = (*str == '-');
  1504. if (minus) str++;
  1505. digit c;
  1506. while ((c = (unsigned char)*str++) != 0) {
  1507. if (c == ',') continue; /* allow commas in number */
  1508. /*
  1509. ** If not a hexadecimal (highest base) digit then it is
  1510. ** clearly the end of the processable string. Bail out
  1511. ** of the scan loop.
  1512. */
  1513. if (!isxdigit((char)c)) break;
  1514. /*
  1515. ** Convert the character into an integer number 0 through 15.
  1516. */
  1517. if (isdigit((char)c)) {
  1518. c -= '0';
  1519. } else {
  1520. c = (unsigned char)(toupper((char)c) - 'A') + 10;
  1521. }
  1522. /*
  1523. ** If the integer digit is greater than the radix, then we
  1524. ** know that further processing should stop. This is the
  1525. ** end of the number string.
  1526. */
  1527. if (c >= radix) break; /* scan terminated by any non-digit */
  1528. XMP_Unsigned_Mult_Int(mpn, mpn, radix, precision);
  1529. XMP_Add_Int(mpn, mpn, c, 0, precision);
  1530. }
  1531. if (minus) {
  1532. XMP_Neg(mpn, precision);
  1533. }
  1534. }
  1535. /***********************************************************************************************
  1536. * XMP_Hybrid_Mul -- Special hybrid short multiply (with carry). *
  1537. * *
  1538. * Multiply the single-word multiplier times the multiprecision integer *
  1539. * in multiplicand, accumulating result in prod. The resulting *
  1540. * multiprecision prod will be 1 word longer than the multiplicand. *
  1541. * multiplicand is double precision words long. We add into prod, so caller *
  1542. * should zero it out first. For best results, this time-critical *
  1543. * function should be implemented in assembly. *
  1544. * NOTE: Unlike other functions in the multiprecision arithmetic *
  1545. * library, both multiplicand and prod are pointing at the LSB, *
  1546. * regardless of byte order of the machine. On an 80x86, this makes *
  1547. * no difference. But if this assembly function is implemented *
  1548. * on a 680x0, it becomes important. *
  1549. * *
  1550. * Note that this has been modified from the previous version to allow *
  1551. * better support for Smith's modmult: *
  1552. * The final carry bit is added to the existing product *
  1553. * array, rather than simply stored. *
  1554. * *
  1555. * INPUT: prod -- Pointer to the product MP number buffer. *
  1556. * *
  1557. * multiplicand -- Pointer to the multiplicand MP number. *
  1558. * *
  1559. * multiplier -- The short integer used as the multiplier. *
  1560. * *
  1561. * precision -- The precision of the MP number used. *
  1562. * *
  1563. * OUTPUT: none *
  1564. * *
  1565. * WARNINGS: The carry (if any) is added into the integer one beyond the end of the *
  1566. * product buffer. *
  1567. * *
  1568. * HISTORY: *
  1569. * 07/02/1996 JLB : Created. *
  1570. *=============================================================================================*/
  1571. void XMP_Hybrid_Mul(unsigned short * prod, unsigned short * multiplicand, unsigned short multiplier, int precision)
  1572. {
  1573. unsigned long carry = 0;
  1574. for (int i = 0; i < precision; ++i) {
  1575. unsigned long p = (unsigned long)multiplier * *multiplicand++;
  1576. p += *prod + carry;
  1577. *prod++ = (unsigned short) p;
  1578. carry = p >> 16;
  1579. }
  1580. /* Add carry to the next higher word of product / dividend */
  1581. *prod += (unsigned short) carry;
  1582. }
  1583. /***********************************************************************************************
  1584. * XMP_Double_Mul -- Double precision MP multiply. *
  1585. * *
  1586. * This will perform a double precision multiply of MP numbers. This means that the product *
  1587. * will be twice the precision of the components. *
  1588. * *
  1589. * INPUT: prod -- Pointer to the result buffer. This buffer must be able to hold *
  1590. * double the precision specified. *
  1591. * *
  1592. * multiplicand-- Pointer to the multiplicand MP number. *
  1593. * *
  1594. * multiplier -- Pointer to the multiplier number. *
  1595. * *
  1596. * precision -- The precision of the two component MP numbers. *
  1597. * *
  1598. * OUTPUT: none *
  1599. * *
  1600. * WARNINGS: Be sure the product buffer can hold a double precision number. *
  1601. * *
  1602. * HISTORY: *
  1603. * 07/02/1996 JLB : Created. *
  1604. *=============================================================================================*/
  1605. void XMP_Double_Mul(digit * prod, const digit * multiplicand, const digit * multiplier, int precision)
  1606. {
  1607. /*
  1608. ** Clear out the double precision product buffer.
  1609. */
  1610. XMP_Init(prod, 0, precision*2);
  1611. const unsigned short * multiplier_ptr = (const unsigned short *) multiplier;
  1612. unsigned short * product_ptr = (unsigned short *) prod;
  1613. // Multiply multiplicand by each word in multiplier, accumulating prod.
  1614. for (int i = 0; i < precision*2; ++i) {
  1615. XMP_Hybrid_Mul(product_ptr++, (unsigned short *)multiplicand, *multiplier_ptr++, precision*2);
  1616. }
  1617. }
  1618. static int _modulus_shift; // number of bits for recip scaling
  1619. static unsigned short _reciprical_high_digit; // MSdigit of scaled recip
  1620. static unsigned short _reciprical_low_digit; // LSdigit of scaled recip
  1621. static int _modulus_sub_precision; // length of modulus in MULTUNITs
  1622. static int _modulus_bit_count; // number of modulus significant bits
  1623. static digit _scratch_modulus[MAX_UNIT_PRECISION]; // modulus
  1624. // The double precision modulus staging buffer.
  1625. static digit _double_staging_number[MAX_UNIT_PRECISION * 2 + 2];
  1626. // most significant digits of modulus.
  1627. static digit _mod_quotient[4];
  1628. static digit _mod_divisor[4];
  1629. /***********************************************************************************************
  1630. * XMP_Prepare_Modulus -- Prepare globals for modulus operation. *
  1631. * *
  1632. * Calculate the reciprocal of modulus with a precision of two MULTUNITs. *
  1633. * Assumes that precision has already been adjusted to the *
  1634. * size of the modulus, plus SLOP_BITS. *
  1635. * *
  1636. * Note: This routine was designed to work with large values and *
  1637. * doesn't have the necessary testing or handling to work with a *
  1638. * modulus having less than three significant digits. For such cases, *
  1639. * the separate multiply and modulus routines can be used. *
  1640. * *
  1641. * INPUT: modulus -- Pointer to the modulus number. *
  1642. * *
  1643. * precision-- The precision of the modulus number. *
  1644. * *
  1645. * OUTPUT: none *
  1646. * *
  1647. * WARNINGS: none *
  1648. * *
  1649. * HISTORY: *
  1650. * 07/02/1996 JLB : Created. *
  1651. *=============================================================================================*/
  1652. int XMP_Prepare_Modulus(const digit * n_modulus, int precision)
  1653. {
  1654. XMP_Move(_scratch_modulus, n_modulus, precision);
  1655. _modulus_bit_count = XMP_Count_Bits(_scratch_modulus, precision);
  1656. _modulus_sub_precision = (_modulus_bit_count + 16 - 1) / 16;
  1657. /*
  1658. ** Keep 2*16 bits in _mod_divisor.
  1659. ** This will (normally) result in a reciprocal of 2*16+1 bits.
  1660. */
  1661. int sub_precision = XMP_Significance(_scratch_modulus, precision); // significant digits in modulus
  1662. XMP_Move(_mod_divisor, &_scratch_modulus[sub_precision-2], 2);
  1663. _modulus_shift = XMP_Count_Bits(_mod_divisor, 2) - 2 * 16;
  1664. XMP_Shift_Right_Bits(_mod_divisor, _modulus_shift, 2);
  1665. XMP_Reciprocal(_mod_quotient, _mod_divisor, 2);
  1666. XMP_Shift_Right_Bits(_mod_quotient, 1, 2);
  1667. /* Reduce to: 0 < _modulus_shift <= 16 */
  1668. _modulus_shift = ((_modulus_shift + (16 - 1)) % 16) + 1;
  1669. /* round up */
  1670. XMP_Inc(_mod_quotient, 2);
  1671. if (XMP_Count_Bits(_mod_quotient, 2) > 2 * 16) {
  1672. XMP_Shift_Right_Bits(_mod_quotient, 1, 2);
  1673. _modulus_shift--; /* now 0 <= _modulus_shift <= 16 */
  1674. }
  1675. unsigned short * mpm = (unsigned short *) _mod_quotient;
  1676. _reciprical_low_digit = *mpm++;
  1677. _reciprical_high_digit = *mpm;
  1678. return 0;
  1679. }
  1680. /***********************************************************************************************
  1681. * XMP_Mod_Mult -- Perform a multiply - modulus operation. *
  1682. * *
  1683. * This routine will combine a multiply and a modulus operation. This takes advantage of *
  1684. * a tremendous speed advantage possible if these two processes are combined rather than *
  1685. * being performed separately. *
  1686. * *
  1687. * INPUT: prod -- Pointer to the MP buffer that will hold the result. *
  1688. * *
  1689. * multiplicand-- The number to multiply. *
  1690. * *
  1691. * multiplier -- The number to multiply by. *
  1692. * *
  1693. * precision -- The precision of the MP numbers involved. *
  1694. * *
  1695. * OUTPUT: none *
  1696. * *
  1697. * WARNINGS: The modulus must already have been prepared by the routine XMP_Prepare_Modulus. *
  1698. * *
  1699. * HISTORY: *
  1700. * 07/02/1996 JLB : Created. *
  1701. *=============================================================================================*/
  1702. int XMP_Mod_Mult(digit * prod, const digit * multiplicand, const digit * multiplier, int precision)
  1703. {
  1704. XMP_Double_Mul(_double_staging_number, multiplicand, multiplier, precision);
  1705. int double_precision = precision * 2 + 1;
  1706. _double_staging_number[double_precision - 1] = 0; /* leading 0 digit */
  1707. /*
  1708. ** We now start working with MULTUNITs.
  1709. ** Determine the most significant MULTUNIT of the product so we don't
  1710. ** have to process leading zeros in our divide loop.
  1711. */
  1712. int dmi = XMP_Significance(_double_staging_number, double_precision) * 2; // number of significant MULTUNITs in product
  1713. if (dmi >= _modulus_sub_precision) {
  1714. /* Make dividend negative. This allows the use of mp_single_mul to
  1715. ** "subtract" the product of the modulus and the trial divisor
  1716. ** by actually adding to a negative dividend.
  1717. ** The one's complement of the dividend is used, since it causes
  1718. ** a zero value to be represented as all ones. This facilitates
  1719. ** testing the result for possible overflow, since a sign bit
  1720. ** indicates that no adjustment is necessary, and we should not
  1721. ** attempt to adjust if the result of the addition is zero.
  1722. */
  1723. XMP_Inc(_double_staging_number, double_precision);
  1724. XMP_Neg(_double_staging_number, double_precision);
  1725. int nqd = dmi + 1 - _modulus_sub_precision; // number of quotient digits remaining to be generated
  1726. /* Set msb, lsb, and normal ptrs of dividend */
  1727. unsigned short * dmph = ((unsigned short *)_double_staging_number) + dmi + 1; // points to one higher than precision would indicate
  1728. unsigned short * dmpl = dmph - _modulus_sub_precision;
  1729. /*
  1730. ** Divide loop.
  1731. ** Each iteration computes the next quotient MULTUNIT digit, then
  1732. ** multiplies the divisor (modulus) by the quotient digit and adds
  1733. ** it to the one's complement of the dividend (equivalent to
  1734. ** subtracting). If the product was greater than the remaining dividend,
  1735. ** we get a non-negative result, in which case we subtract off the
  1736. ** modulus to get the proper negative remainder.
  1737. */
  1738. for (; nqd; nqd--) {
  1739. --dmph;
  1740. --dmpl;
  1741. unsigned short q = mp_quo_digit(dmph); // trial quotient digit
  1742. if (q > 0) {
  1743. XMP_Hybrid_Mul(dmpl, (unsigned short *)_scratch_modulus, q, precision*2);
  1744. /* Perform correction if q too large.
  1745. ** This rarely occurs.
  1746. */
  1747. if (!(*dmph & SEMI_UPPER_MOST_BIT)) {
  1748. unsigned short * dmp = dmpl;
  1749. if (XMP_Sub((unsigned long *)dmp, (unsigned long *)dmp, _scratch_modulus, false, precision)) {
  1750. (*dmph)--;
  1751. }
  1752. }
  1753. }
  1754. }
  1755. /* d contains the one's complement of the remainder. */
  1756. XMP_Neg(_double_staging_number, precision);
  1757. XMP_Dec(_double_staging_number, precision);
  1758. }
  1759. XMP_Move(prod, _double_staging_number, precision);
  1760. return (0);
  1761. }
  1762. /***********************************************************************************************
  1763. * XMP_Mod_Mult_Clear -- Remove temporary values from memory. *
  1764. * *
  1765. * Smith's mp_modmult function leaves some internal arrays in memory, *
  1766. * so we have to call modmult_burn() at the end of mp_exponent_mod. *
  1767. * This is so that no cryptographically sensitive data is left in memory *
  1768. * after the program exits. *
  1769. * *
  1770. * INPUT: precision -- The precision of the numbers involved. *
  1771. * *
  1772. * OUTPUT: none *
  1773. * *
  1774. * WARNINGS: none *
  1775. * *
  1776. * HISTORY: *
  1777. * 07/02/1996 JLB : Created. *
  1778. *=============================================================================================*/
  1779. void XMP_Mod_Mult_Clear(int precision)
  1780. {
  1781. XMP_Init(_scratch_modulus, 0, precision);
  1782. XMP_Init(_double_staging_number, 0, precision);
  1783. XMP_Init(_mod_quotient, 0, ARRAY_SIZE(_mod_quotient));
  1784. XMP_Init(_mod_divisor, 0, ARRAY_SIZE(_mod_divisor));
  1785. _modulus_shift = _modulus_bit_count = 0;
  1786. _reciprical_high_digit = _reciprical_low_digit = 0;
  1787. _modulus_sub_precision = /*mutemp =*/ 0;
  1788. }
  1789. /*
  1790. ** The function mp_quo_digit is the heart of Smith's modulo reduction,
  1791. ** which uses a form of long division. It computes a trial quotient
  1792. ** "digit" (MULTUNIT-sized digit) by multiplying the three most
  1793. ** significant MULTUNITs of the dividend by the two most significant
  1794. ** MULTUNITs of the reciprocal of the modulus. Note that this function
  1795. ** requires that 16 * 2 <= sizeof(unsigned long).
  1796. **
  1797. ** An important part of this technique is that the quotient never be
  1798. ** too small, although it may occasionally be too large. This was
  1799. ** done to eliminate the need to check and correct for a remainder
  1800. ** exceeding the divisor. It is easier to check for a negative
  1801. ** remainder. The following technique rarely needs correction for
  1802. ** MULTUNITs of at least 16 bits.
  1803. **
  1804. ** The following routine has two implementations:
  1805. **
  1806. ** Parameter: dividend - points to the most significant MULTUNIT
  1807. ** of the dividend. Note that dividend actually contains the
  1808. ** one's complement of the actual dividend value (see comments for
  1809. ** XMP_Mod_Mult).
  1810. **
  1811. ** Return: the trial quotient digit resulting from dividing the first
  1812. ** three MULTUNITs at dividend by the upper two MULTUNITs of the
  1813. ** modulus.
  1814. */
  1815. unsigned short mp_quo_digit(unsigned short * dividend)
  1816. {
  1817. unsigned long q, q0, q1, q2;
  1818. /*
  1819. * Compute the least significant product group.
  1820. * The last terms of q1 and q2 perform upward rounding, which is
  1821. * needed to guarantee that the result not be too small.
  1822. */
  1823. q1 = (dividend[-2] ^ SEMI_MASK) * (unsigned long) _reciprical_high_digit + _reciprical_high_digit;
  1824. q2 = (dividend[-1] ^ SEMI_MASK) * (unsigned long) _reciprical_low_digit + (1L << 16);
  1825. q0 = (q1 >> 1) + (q2 >> 1) + 1;
  1826. /* Compute the middle significant product group. */
  1827. q1 = (dividend[-1] ^ SEMI_MASK) * (unsigned long) _reciprical_high_digit;
  1828. q2 = (dividend[0] ^ SEMI_MASK) * (unsigned long) _reciprical_low_digit;
  1829. q = (q0 >> 16) + (q1 >> 1) + (q2 >> 1) + 1;
  1830. /* Compute the most significant term and add in the others */
  1831. q = (q >> (16 - 2)) + (((dividend[0] ^ SEMI_MASK) * (unsigned long) _reciprical_high_digit) << 1);
  1832. q >>= _modulus_shift;
  1833. /* Prevent overflow and then wipe out the intermediate results. */
  1834. return (unsigned short) min(q, (unsigned long)(1L << 16) - 1);
  1835. }
  1836. /*
  1837. ** Russian peasant combined exponentiation/modulo algorithm.
  1838. ** Calls modmult instead of mult.
  1839. ** Computes: expout = (expin**exponent) mod modulus
  1840. ** WARNING: All the arguments must be less than the modulus!
  1841. */
  1842. int xmp_exponent_mod(digit * expout, const digit * expin, const digit * exponent_ptr, const digit * modulus, int precision)
  1843. {
  1844. digit product[MAX_UNIT_PRECISION];
  1845. XMP_Init(expout, 1, precision);
  1846. if (XMP_Test_Eq_Int(exponent_ptr, 0, precision)) {
  1847. if (XMP_Test_Eq_Int(expin, 0, precision)) {
  1848. return -1; /* 0 to the 0th power means return error */
  1849. }
  1850. return 0; /* otherwise, zero exponent means expout is 1 */
  1851. }
  1852. if (XMP_Test_Eq_Int(modulus, 0, precision)) {
  1853. return -2; /* zero modulus means error */
  1854. }
  1855. if (XMP_Compare(expin, modulus, precision) >= 0) {
  1856. return -3; /* if expin >= modulus, return error */
  1857. }
  1858. if (XMP_Compare(exponent_ptr, modulus, precision) >= 0) {
  1859. return -4; /* if exponent >= modulus, return error */
  1860. }
  1861. /* set smallest optimum precision for this modulus */
  1862. int limited_precision = XMP_Significance(modulus, precision);
  1863. if (XMP_Prepare_Modulus(modulus, limited_precision)) {
  1864. return -5; /* unstageable modulus (STEWART algorithm) */
  1865. }
  1866. /* normalize and compute number of bits in exponent first */
  1867. // int exp_precision = XMP_Significance(exponent_ptr, limited_precision);
  1868. // if (!exp_precision) return(0);
  1869. // int bits = XMP_Digits_To_Bits(exp_precision);
  1870. // exponent_ptr += (exp_precision-1);
  1871. // digit high_bit_mask = UPPER_MOST_BIT;
  1872. // while (! ((*exponent_ptr) & high_bit_mask)) {
  1873. // high_bit_mask >>= 1;
  1874. // bits--;
  1875. // }
  1876. int total_bit_count = XMP_Count_Bits(exponent_ptr, limited_precision);
  1877. int sub_precision = XMP_Bits_To_Digits(total_bit_count);
  1878. if (!sub_precision) return(0);
  1879. digit high_bit_mask = XMP_Bits_To_Mask(total_bit_count);
  1880. exponent_ptr += (sub_precision-1);
  1881. /* We can "optimize out" the first modsquare and modmult: */
  1882. total_bit_count--; /* We know for sure at this point that bits>0 */
  1883. XMP_Move(expout, expin, limited_precision);
  1884. high_bit_mask >>= 1;
  1885. if (!high_bit_mask) {
  1886. high_bit_mask = UPPER_MOST_BIT;
  1887. exponent_ptr--;
  1888. }
  1889. while (total_bit_count--) {
  1890. XMP_Mod_Mult(product, expout, expout, limited_precision);
  1891. if (((*exponent_ptr) & high_bit_mask)) {
  1892. XMP_Mod_Mult(expout, product, expin, limited_precision);
  1893. } else {
  1894. XMP_Move(expout, product, limited_precision);
  1895. }
  1896. high_bit_mask >>= 1;
  1897. if (!high_bit_mask) {
  1898. high_bit_mask = UPPER_MOST_BIT;
  1899. exponent_ptr--;
  1900. }
  1901. }
  1902. XMP_Init(product, 0, limited_precision);
  1903. XMP_Mod_Mult_Clear(limited_precision); /* ask mp_modmult to also burn its own evidence */
  1904. return 0;
  1905. }
  1906. /***********************************************************************************************
  1907. * memrev -- Reverse the byte order of the buffer specified. *
  1908. * *
  1909. * This routine will reverse the byte order in the buffer specified. *
  1910. * *
  1911. * INPUT: buffer -- Pointer to the buffer that will be reversed. *
  1912. * *
  1913. * length -- The length of the buffer. *
  1914. * *
  1915. * OUTPUT: none *
  1916. * *
  1917. * WARNINGS: none *
  1918. * *
  1919. * HISTORY: *
  1920. * 07/02/1996 JLB : Created. *
  1921. *=============================================================================================*/
  1922. void memrev(char * buffer, size_t length)
  1923. {
  1924. char * r2 = &(buffer[length - 1]);
  1925. while (buffer < r2) {
  1926. char b = *buffer;
  1927. *buffer++ = *r2;
  1928. *r2-- = b;
  1929. }
  1930. }
  1931. int _USERENTRY pfunc(const void * pkey, const void * base)
  1932. {
  1933. if (*(unsigned short *)pkey < *(unsigned short *)base) return(-1);
  1934. if (*(unsigned short *)pkey > *(unsigned short *)base) return(1);
  1935. return(0);
  1936. }
  1937. /***********************************************************************************************
  1938. * XMP_Is_Small_Prime -- Determine if MP number is a small prime. *
  1939. * *
  1940. * This routine will compare the MP number against all known small prime numbers. It will *
  1941. * return true if a match was found. *
  1942. * *
  1943. * INPUT: candidate -- Pointer to MP number that is to be tested. *
  1944. * *
  1945. * precision -- The precision of the MP number specified. *
  1946. * *
  1947. * OUTPUT: bool; Was the MP number a member of the small prime community? *
  1948. * *
  1949. * WARNINGS: none *
  1950. * *
  1951. * HISTORY: *
  1952. * 07/02/1996 JLB : Created. *
  1953. *=============================================================================================*/
  1954. bool XMP_Is_Small_Prime(const digit * candidate, int precision)
  1955. {
  1956. /*
  1957. ** If the number is too large for comparison to the known small primes table, then
  1958. ** bail immediately.
  1959. */
  1960. if (XMP_Significance(candidate, precision) > 1) return(false);
  1961. if (*candidate > primeTable[ARRAY_SIZE(primeTable)-1]) return false;
  1962. unsigned long * ptr = (unsigned long *)bsearch(&candidate, &primeTable[0], ARRAY_SIZE(primeTable), sizeof(primeTable[0]), pfunc);
  1963. return(ptr != NULL);
  1964. }
  1965. /***********************************************************************************************
  1966. * XMP_Small_Divisors_Test -- Perform the small divisors test on an MP number. *
  1967. * *
  1968. * This test for primality will divide an MP number by the set of small primes. If any of *
  1969. * these numbers divides evenly into the candidate number, then it is known that the *
  1970. * candidate is NOT prime. *
  1971. * *
  1972. * INPUT: candidate -- Pointer to the MP number that is to be tested. *
  1973. * *
  1974. * precision -- The precision of the MP number/ *
  1975. * *
  1976. * OUTPUT: bool; Did the MP number pass the small divisors test? *
  1977. * *
  1978. * WARNINGS: If the MP number passes, it doesn't mean that it is prime, just that is hasn't *
  1979. * yet been proven to be not prime. *
  1980. * *
  1981. * HISTORY: *
  1982. * 07/02/1996 JLB : Created. *
  1983. *=============================================================================================*/
  1984. bool XMP_Small_Divisors_Test(const digit * candidate, int precision)
  1985. {
  1986. digit quotient[MAX_UNIT_PRECISION];
  1987. for (unsigned i = 0; i < ARRAY_SIZE(primeTable); i++) {
  1988. if (XMP_Unsigned_Div_Int(quotient, candidate, primeTable[i], precision) == 0) return(false);
  1989. }
  1990. return(true);
  1991. }
  1992. /***********************************************************************************************
  1993. * XMP_Fermat_Test -- Performs Fermat's Little Theorem on an MP number. *
  1994. * *
  1995. * This is a more expensive but thorough test for primality. The aggressiveness of this *
  1996. * test can be controlled by the number of rounds specified. Four rounds is usually *
  1997. * sufficient. *
  1998. * *
  1999. * INPUT: candidate -- Pointer to the candidate MP number that is to be tested. *
  2000. * *
  2001. * rounds -- The number of rounds to test the MP number (keep it small). *
  2002. * *
  2003. * precision -- The precision of the MP number. *
  2004. * *
  2005. * OUTPUT: bool; Was the number not proven to be not prime. A FALSE means that it is not *
  2006. * prime. A TRUE means that it might be prime. *
  2007. * *
  2008. * WARNINGS: This takes a bit of time. The time it takes is directly controlled by the *
  2009. * number of rounds specified. Keep the number of rounds as small as possible. *
  2010. * *
  2011. * HISTORY: *
  2012. * 07/02/1996 JLB : Created. *
  2013. *=============================================================================================*/
  2014. bool XMP_Fermat_Test(const digit * candidate_prime, unsigned rounds, int precision)
  2015. {
  2016. assert(rounds < ARRAY_SIZE(primeTable));
  2017. digit term[MAX_UNIT_PRECISION];
  2018. XMP_Move(term, candidate_prime, precision);
  2019. XMP_Dec(term, precision);
  2020. for (unsigned i = 0; i < rounds; i++) {
  2021. // if ((x**(p-1)) mod p) != 1, then p is not prime
  2022. digit result[MAX_UNIT_PRECISION];
  2023. digit small_prime[MAX_UNIT_PRECISION];
  2024. XMP_Init(small_prime, primeTable[i], precision);
  2025. xmp_exponent_mod(result, small_prime, term, candidate_prime, precision);
  2026. if (!XMP_Test_Eq_Int(result, 1, precision)) return(false);
  2027. }
  2028. return(true);
  2029. }
  2030. /***********************************************************************************************
  2031. * XMP_Rabin_Miller_Test -- Performs the Rabin Miller test for primality. *
  2032. * *
  2033. * This test for primality is even more expensive the Fermat's Little Theorem. It doesn't *
  2034. * prove that a number is prime, but it can prove that it is not prime. *
  2035. * *
  2036. * INPUT: rng -- Reference to to a random number generator. *
  2037. * *
  2038. * candidate-- Pointer to the candidate MP number that is to be tested. *
  2039. * *
  2040. * rounds -- The number of test rounds to perform. *
  2041. * *
  2042. * precision-- The precision of the MP number specified. *
  2043. * *
  2044. * OUTPUT: bool; Was the number not proven to be not prime? A FALSE means that the number is *
  2045. * not prime. A TRUE means that it might be. *
  2046. * *
  2047. * WARNINGS: This routine takes a long time. Use as few rounds as possible. *
  2048. * *
  2049. * HISTORY: *
  2050. * 07/02/1996 JLB : Created. *
  2051. *=============================================================================================*/
  2052. bool XMP_Rabin_Miller_Test(Straw & rng, digit const * w, int rounds, int precision)
  2053. {
  2054. digit wminus1[MAX_UNIT_PRECISION];
  2055. XMP_Sub_Int(wminus1, w, 1, 0, precision);
  2056. unsigned maxbitprecision = precision * sizeof(digit) * 8;
  2057. unsigned a;
  2058. for (a = 0; a < maxbitprecision; a++) {
  2059. if (XMP_Test_Bit(wminus1, a)) {
  2060. break;
  2061. }
  2062. }
  2063. digit m[MAX_UNIT_PRECISION];
  2064. XMP_Move(m, wminus1, precision);
  2065. XMP_Shift_Right_Bits(wminus1, a, precision);
  2066. for (int i = 0; i < rounds; i++) {
  2067. digit b[MAX_UNIT_PRECISION];
  2068. digit temp[MAX_UNIT_PRECISION];
  2069. XMP_Init(temp, 2, precision);
  2070. XMP_Randomize(b, rng, temp, wminus1, precision);
  2071. digit z[MAX_UNIT_PRECISION];
  2072. xmp_exponent_mod(z, b, m, w, precision);
  2073. if (XMP_Test_Eq_Int(z, 1, precision) || XMP_Compare(z, wminus1, precision) == 0) {
  2074. continue; // passes this round
  2075. }
  2076. int j;
  2077. for (j = 1; j < a; j++) {
  2078. digit t2[MAX_UNIT_PRECISION];
  2079. xmp_exponent_mod(t2, z, temp, w, precision);
  2080. if (XMP_Compare(t2, wminus1, precision) == 0) {
  2081. break; // passed this round
  2082. }
  2083. if (XMP_Test_Eq_Int(z, 1, precision)) {
  2084. return false;
  2085. }
  2086. }
  2087. if (j == a) {
  2088. return false;
  2089. }
  2090. }
  2091. return true;
  2092. }
  2093. /***********************************************************************************************
  2094. * XMP_Randomize -- Generate a random MP number. *
  2095. * *
  2096. * This routine will generate a random MP number with the number of bits precision *
  2097. * specified. This is the starting point for generating large random prime numbers. It is *
  2098. * very important that the random number generated is truly random. *
  2099. * *
  2100. * INPUT: result -- Pointer to the buffer that will hold the MP number. *
  2101. * *
  2102. * rng -- Reference to a random number generator. *
  2103. * *
  2104. * total_bits-- The number of bits precision that the MP number must have. *
  2105. * *
  2106. * precision-- The precision of the MP number to be generated (maximum) *
  2107. * *
  2108. * OUTPUT: none *
  2109. * *
  2110. * WARNINGS: none *
  2111. * *
  2112. * HISTORY: *
  2113. * 07/02/1996 JLB : Created. *
  2114. *=============================================================================================*/
  2115. void XMP_Randomize(digit * result, Straw & rng, int total_bits, int precision)
  2116. {
  2117. assert(XMP_Bits_To_Digits(total_bits) <= MAX_UNIT_PRECISION);
  2118. total_bits = min(total_bits, precision * 32);
  2119. unsigned nbytes = total_bits/8 + 1;
  2120. XMP_Init(result, 0, precision);
  2121. rng.Get(result, nbytes);
  2122. ((unsigned char *)result)[nbytes-1] &= (unsigned char)(~((~0) << (total_bits % 8)));
  2123. }
  2124. /***********************************************************************************************
  2125. * XMP_Randomize -- Generate a random MP number between the boundaries specified. *
  2126. * *
  2127. * This routine will generate a random MP number but it will be bounded by the minimum *
  2128. * and maximum MP numbers specified. *
  2129. * *
  2130. * INPUT: result -- Pointer to the MP buffer that will hold the result. *
  2131. * *
  2132. * rng -- Reference to a random number generator to use. *
  2133. * *
  2134. * minval -- Minimum value allowed. *
  2135. * *
  2136. * maxval -- Maximum value allowed. *
  2137. * *
  2138. * precision -- The precision of the MP numbers involved. *
  2139. * *
  2140. * OUTPUT: none *
  2141. * *
  2142. * WARNINGS: none *
  2143. * *
  2144. * HISTORY: *
  2145. * 07/02/1996 JLB : Created. *
  2146. *=============================================================================================*/
  2147. void XMP_Randomize(digit * result, Straw & rng, digit const * minval, digit const * maxval, int precision)
  2148. {
  2149. digit range[MAX_UNIT_PRECISION];
  2150. XMP_Sub(range, maxval, minval, 0, precision);
  2151. unsigned int bit_count = XMP_Count_Bits(range, precision);
  2152. do {
  2153. XMP_Randomize(result, rng, bit_count, precision);
  2154. } while (XMP_Compare(result, range, precision) > 0);
  2155. XMP_Add(result, result, minval, 0, precision);
  2156. }
  2157. /***********************************************************************************************
  2158. * XMP_Is_Prime -- Determine if the specified MP number is prime. *
  2159. * *
  2160. * This routine will perform some checks to try and determine if the specified MP number *
  2161. * is a prime number. The result of this test is not 100% conclusive, but it is pretty *
  2162. * darn close. *
  2163. * *
  2164. * INPUT: prime -- Pointer to a candidate number to test for primality. *
  2165. * *
  2166. * precision-- The precision of the MP number specified. *
  2167. * *
  2168. * OUTPUT: bool; Was the number not proven to be not prime? If FALSE, then the number is *
  2169. * not prime. If TRUE, then it might be. *
  2170. * *
  2171. * WARNINGS: This can take a very very very very very long time. Especially for the larger *
  2172. * numbers. *
  2173. * *
  2174. * HISTORY: *
  2175. * 07/02/1996 JLB : Created. *
  2176. *=============================================================================================*/
  2177. bool XMP_Is_Prime(digit const * prime, int precision)
  2178. {
  2179. /*
  2180. ** Even numbers are ALWAYS not prime.
  2181. */
  2182. if (!(*prime & 0x01)) return(false);
  2183. /*
  2184. ** Compare the prime number against the exhaustive list of prime
  2185. ** numbers below 14 bits in size. If it finds a match, then
  2186. ** the number is a known prime.
  2187. */
  2188. if (XMP_Is_Small_Prime(prime, precision)) return(true);
  2189. /*
  2190. ** Perform the small divisors test. This is not exhaustive, but
  2191. ** will weed out a large percentage of non-prime numbers.
  2192. */
  2193. if (!XMP_Small_Divisors_Test(prime, precision)) return(false);
  2194. /*
  2195. ** Perform Fermat's Little Theorum on the candidate prime. Run
  2196. ** the theorum for several rounds to ensure a high degree of
  2197. ** confidence.
  2198. */
  2199. if (!XMP_Fermat_Test(prime, 2, precision)) return(false);
  2200. /*
  2201. ** If all of the above tests have not confirmed primality nor
  2202. ** confirmed non-primality, presume that the number must be prime.
  2203. */
  2204. return(true);
  2205. }
  2206. /*
  2207. ** Complete list of all prime numbers that are less than 32719 (inclusive).
  2208. */
  2209. unsigned short primeTable[3511] = {
  2210. 0x0002,0x0003,0x0005,0x0007,0x000B,0x000D,0x0011,0x0013,0x0017,0x001D,0x001F,0x0025,0x0029,0x002B,0x002F,0x0035,
  2211. 0x003B,0x003D,0x0043,0x0047,0x0049,0x004F,0x0053,0x0059,0x0061,0x0065,0x0067,0x006B,0x006D,0x0071,0x007F,0x0083,
  2212. 0x0089,0x008B,0x0095,0x0097,0x009D,0x00A3,0x00A7,0x00AD,0x00B3,0x00B5,0x00BF,0x00C1,0x00C5,0x00C7,0x00D3,0x00DF,
  2213. 0x00E3,0x00E5,0x00E9,0x00EF,0x00F1,0x00FB,0x0101,0x0107,0x010D,0x010F,0x0115,0x0119,0x011B,0x0125,0x0133,0x0137,
  2214. 0x0139,0x013D,0x014B,0x0151,0x015B,0x015D,0x0161,0x0167,0x016F,0x0175,0x017B,0x017F,0x0185,0x018D,0x0191,0x0199,
  2215. 0x01A3,0x01A5,0x01AF,0x01B1,0x01B7,0x01BB,0x01C1,0x01C9,0x01CD,0x01CF,0x01D3,0x01DF,0x01E7,0x01EB,0x01F3,0x01F7,
  2216. 0x01FD,0x0209,0x020B,0x021D,0x0223,0x022D,0x0233,0x0239,0x023B,0x0241,0x024B,0x0251,0x0257,0x0259,0x025F,0x0265,
  2217. 0x0269,0x026B,0x0277,0x0281,0x0283,0x0287,0x028D,0x0293,0x0295,0x02A1,0x02A5,0x02AB,0x02B3,0x02BD,0x02C5,0x02CF,
  2218. 0x02D7,0x02DD,0x02E3,0x02E7,0x02EF,0x02F5,0x02F9,0x0301,0x0305,0x0313,0x031D,0x0329,0x032B,0x0335,0x0337,0x033B,
  2219. 0x033D,0x0347,0x0355,0x0359,0x035B,0x035F,0x036D,0x0371,0x0373,0x0377,0x038B,0x038F,0x0397,0x03A1,0x03A9,0x03AD,
  2220. 0x03B3,0x03B9,0x03C7,0x03CB,0x03D1,0x03D7,0x03DF,0x03E5,0x03F1,0x03F5,0x03FB,0x03FD,0x0407,0x0409,0x040F,0x0419,
  2221. 0x041B,0x0425,0x0427,0x042D,0x043F,0x0443,0x0445,0x0449,0x044F,0x0455,0x045D,0x0463,0x0469,0x047F,0x0481,0x048B,
  2222. 0x0493,0x049D,0x04A3,0x04A9,0x04B1,0x04BD,0x04C1,0x04C7,0x04CD,0x04CF,0x04D5,0x04E1,0x04EB,0x04FD,0x04FF,0x0503,
  2223. 0x0509,0x050B,0x0511,0x0515,0x0517,0x051B,0x0527,0x0529,0x052F,0x0551,0x0557,0x055D,0x0565,0x0577,0x0581,0x058F,
  2224. 0x0593,0x0595,0x0599,0x059F,0x05A7,0x05AB,0x05AD,0x05B3,0x05BF,0x05C9,0x05CB,0x05CF,0x05D1,0x05D5,0x05DB,0x05E7,
  2225. 0x05F3,0x05FB,0x0607,0x060D,0x0611,0x0617,0x061F,0x0623,0x062B,0x062F,0x063D,0x0641,0x0647,0x0649,0x064D,0x0653,
  2226. 0x0655,0x065B,0x0665,0x0679,0x067F,0x0683,0x0685,0x069D,0x06A1,0x06A3,0x06AD,0x06B9,0x06BB,0x06C5,0x06CD,0x06D3,
  2227. 0x06D9,0x06DF,0x06F1,0x06F7,0x06FB,0x06FD,0x0709,0x0713,0x071F,0x0727,0x0737,0x0745,0x074B,0x074F,0x0751,0x0755,
  2228. 0x0757,0x0761,0x076D,0x0773,0x0779,0x078B,0x078D,0x079D,0x079F,0x07B5,0x07BB,0x07C3,0x07C9,0x07CD,0x07CF,0x07D3,
  2229. 0x07DB,0x07E1,0x07EB,0x07ED,0x07F7,0x0805,0x080F,0x0815,0x0821,0x0823,0x0827,0x0829,0x0833,0x083F,0x0841,0x0851,
  2230. 0x0853,0x0859,0x085D,0x085F,0x0869,0x0871,0x0883,0x089B,0x089F,0x08A5,0x08AD,0x08BD,0x08BF,0x08C3,0x08CB,0x08DB,
  2231. 0x08DD,0x08E1,0x08E9,0x08EF,0x08F5,0x08F9,0x0905,0x0907,0x091D,0x0923,0x0925,0x092B,0x092F,0x0935,0x0943,0x0949,
  2232. 0x094D,0x094F,0x0955,0x0959,0x095F,0x096B,0x0971,0x0977,0x0985,0x0989,0x098F,0x099B,0x09A3,0x09A9,0x09AD,0x09C7,
  2233. 0x09D9,0x09E3,0x09EB,0x09EF,0x09F5,0x09F7,0x09FD,0x0A13,0x0A1F,0x0A21,0x0A31,0x0A39,0x0A3D,0x0A49,0x0A57,0x0A61,
  2234. 0x0A63,0x0A67,0x0A6F,0x0A75,0x0A7B,0x0A7F,0x0A81,0x0A85,0x0A8B,0x0A93,0x0A97,0x0A99,0x0A9F,0x0AA9,0x0AAB,0x0AB5,
  2235. 0x0ABD,0x0AC1,0x0ACF,0x0AD9,0x0AE5,0x0AE7,0x0AED,0x0AF1,0x0AF3,0x0B03,0x0B11,0x0B15,0x0B1B,0x0B23,0x0B29,0x0B2D,
  2236. 0x0B3F,0x0B47,0x0B51,0x0B57,0x0B5D,0x0B65,0x0B6F,0x0B7B,0x0B89,0x0B8D,0x0B93,0x0B99,0x0B9B,0x0BB7,0x0BB9,0x0BC3,
  2237. 0x0BCB,0x0BCF,0x0BDD,0x0BE1,0x0BE9,0x0BF5,0x0BFB,0x0C07,0x0C0B,0x0C11,0x0C25,0x0C2F,0x0C31,0x0C41,0x0C5B,0x0C5F,
  2238. 0x0C61,0x0C6D,0x0C73,0x0C77,0x0C83,0x0C89,0x0C91,0x0C95,0x0C9D,0x0CB3,0x0CB5,0x0CB9,0x0CBB,0x0CC7,0x0CE3,0x0CE5,
  2239. 0x0CEB,0x0CF1,0x0CF7,0x0CFB,0x0D01,0x0D03,0x0D0F,0x0D13,0x0D1F,0x0D21,0x0D2B,0x0D2D,0x0D3D,0x0D3F,0x0D4F,0x0D55,
  2240. 0x0D69,0x0D79,0x0D81,0x0D85,0x0D87,0x0D8B,0x0D8D,0x0DA3,0x0DAB,0x0DB7,0x0DBD,0x0DC7,0x0DC9,0x0DCD,0x0DD3,0x0DD5,
  2241. 0x0DDB,0x0DE5,0x0DE7,0x0DF3,0x0DFD,0x0DFF,0x0E09,0x0E17,0x0E1D,0x0E21,0x0E27,0x0E2F,0x0E35,0x0E3B,0x0E4B,0x0E57,
  2242. 0x0E59,0x0E5D,0x0E6B,0x0E71,0x0E75,0x0E7D,0x0E87,0x0E8F,0x0E95,0x0E9B,0x0EB1,0x0EB7,0x0EB9,0x0EC3,0x0ED1,0x0ED5,
  2243. 0x0EDB,0x0EED,0x0EEF,0x0EF9,0x0F07,0x0F0B,0x0F0D,0x0F17,0x0F25,0x0F29,0x0F31,0x0F43,0x0F47,0x0F4D,0x0F4F,0x0F53,
  2244. 0x0F59,0x0F5B,0x0F67,0x0F6B,0x0F7F,0x0F95,0x0FA1,0x0FA3,0x0FA7,0x0FAD,0x0FB3,0x0FB5,0x0FBB,0x0FD1,0x0FD3,0x0FD9,
  2245. 0x0FE9,0x0FEF,0x0FFB,0x0FFD,0x1003,0x100F,0x101F,0x1021,0x1025,0x102B,0x1039,0x103D,0x103F,0x1051,0x1069,0x1073,
  2246. 0x1079,0x107B,0x1085,0x1087,0x1091,0x1093,0x109D,0x10A3,0x10A5,0x10AF,0x10B1,0x10BB,0x10C1,0x10C9,0x10E7,0x10F1,
  2247. 0x10F3,0x10FD,0x1105,0x110B,0x1115,0x1127,0x112D,0x1139,0x1145,0x1147,0x1159,0x115F,0x1163,0x1169,0x116F,0x1181,
  2248. 0x1183,0x118D,0x119B,0x11A1,0x11A5,0x11A7,0x11AB,0x11C3,0x11C5,0x11D1,0x11D7,0x11E7,0x11EF,0x11F5,0x11FB,0x120D,
  2249. 0x121D,0x121F,0x1223,0x1229,0x122B,0x1231,0x1237,0x1241,0x1247,0x1253,0x125F,0x1271,0x1273,0x1279,0x127D,0x128F,
  2250. 0x1297,0x12AF,0x12B3,0x12B5,0x12B9,0x12BF,0x12C1,0x12CD,0x12D1,0x12DF,0x12FD,0x1307,0x130D,0x1319,0x1327,0x132D,
  2251. 0x1337,0x1343,0x1345,0x1349,0x134F,0x1357,0x135D,0x1367,0x1369,0x136D,0x137B,0x1381,0x1387,0x138B,0x1391,0x1393,
  2252. 0x139D,0x139F,0x13AF,0x13BB,0x13C3,0x13D5,0x13D9,0x13DF,0x13EB,0x13ED,0x13F3,0x13F9,0x13FF,0x141B,0x1421,0x142F,
  2253. 0x1433,0x143B,0x1445,0x144D,0x1459,0x146B,0x146F,0x1471,0x1475,0x148D,0x1499,0x149F,0x14A1,0x14B1,0x14B7,0x14BD,
  2254. 0x14CB,0x14D5,0x14E3,0x14E7,0x1505,0x150B,0x1511,0x1517,0x151F,0x1525,0x1529,0x152B,0x1537,0x153D,0x1541,0x1543,
  2255. 0x1549,0x155F,0x1565,0x1567,0x156B,0x157D,0x157F,0x1583,0x158F,0x1591,0x1597,0x159B,0x15B5,0x15BB,0x15C1,0x15C5,
  2256. 0x15CD,0x15D7,0x15F7,0x1607,0x1609,0x160F,0x1613,0x1615,0x1619,0x161B,0x1625,0x1633,0x1639,0x163D,0x1645,0x164F,
  2257. 0x1655,0x1669,0x166D,0x166F,0x1675,0x1693,0x1697,0x169F,0x16A9,0x16AF,0x16B5,0x16BD,0x16C3,0x16CF,0x16D3,0x16D9,
  2258. 0x16DB,0x16E1,0x16E5,0x16EB,0x16ED,0x16F7,0x16F9,0x1709,0x170F,0x1723,0x1727,0x1733,0x1741,0x175D,0x1763,0x1777,
  2259. 0x177B,0x178D,0x1795,0x179B,0x179F,0x17A5,0x17B3,0x17B9,0x17BF,0x17C9,0x17CB,0x17D5,0x17E1,0x17E9,0x17F3,0x17F5,
  2260. 0x17FF,0x1807,0x1813,0x181D,0x1835,0x1837,0x183B,0x1843,0x1849,0x184D,0x1855,0x1867,0x1871,0x1877,0x187D,0x187F,
  2261. 0x1885,0x188F,0x189B,0x189D,0x18A7,0x18AD,0x18B3,0x18B9,0x18C1,0x18C7,0x18D1,0x18D7,0x18D9,0x18DF,0x18E5,0x18EB,
  2262. 0x18F5,0x18FD,0x1915,0x191B,0x1931,0x1933,0x1945,0x1949,0x1951,0x195B,0x1979,0x1981,0x1993,0x1997,0x1999,0x19A3,
  2263. 0x19A9,0x19AB,0x19B1,0x19B5,0x19C7,0x19CF,0x19DB,0x19ED,0x19FD,0x1A03,0x1A05,0x1A11,0x1A17,0x1A21,0x1A23,0x1A2D,
  2264. 0x1A2F,0x1A35,0x1A3F,0x1A4D,0x1A51,0x1A69,0x1A6B,0x1A7B,0x1A7D,0x1A87,0x1A89,0x1A93,0x1AA7,0x1AAB,0x1AAD,0x1AB1,
  2265. 0x1AB9,0x1AC9,0x1ACF,0x1AD5,0x1AD7,0x1AE3,0x1AF3,0x1AFB,0x1AFF,0x1B05,0x1B23,0x1B25,0x1B2F,0x1B31,0x1B37,0x1B3B,
  2266. 0x1B41,0x1B47,0x1B4F,0x1B55,0x1B59,0x1B65,0x1B6B,0x1B73,0x1B7F,0x1B83,0x1B91,0x1B9D,0x1BA7,0x1BBF,0x1BC5,0x1BD1,
  2267. 0x1BD7,0x1BD9,0x1BEF,0x1BF7,0x1C09,0x1C13,0x1C19,0x1C27,0x1C2B,0x1C2D,0x1C33,0x1C3D,0x1C45,0x1C4B,0x1C4F,0x1C55,
  2268. 0x1C73,0x1C81,0x1C8B,0x1C8D,0x1C99,0x1CA3,0x1CA5,0x1CB5,0x1CB7,0x1CC9,0x1CE1,0x1CF3,0x1CF9,0x1D09,0x1D1B,0x1D21,
  2269. 0x1D23,0x1D35,0x1D39,0x1D3F,0x1D41,0x1D4B,0x1D53,0x1D5D,0x1D63,0x1D69,0x1D71,0x1D75,0x1D7B,0x1D7D,0x1D87,0x1D89,
  2270. 0x1D95,0x1D99,0x1D9F,0x1DA5,0x1DA7,0x1DB3,0x1DB7,0x1DC5,0x1DD7,0x1DDB,0x1DE1,0x1DF5,0x1DF9,0x1E01,0x1E07,0x1E0B,
  2271. 0x1E13,0x1E17,0x1E25,0x1E2B,0x1E2F,0x1E3D,0x1E49,0x1E4D,0x1E4F,0x1E6D,0x1E71,0x1E89,0x1E8F,0x1E95,0x1EA1,0x1EAD,
  2272. 0x1EBB,0x1EC1,0x1EC5,0x1EC7,0x1ECB,0x1EDD,0x1EE3,0x1EEF,0x1EF7,0x1EFD,0x1F01,0x1F0D,0x1F0F,0x1F1B,0x1F39,0x1F49,
  2273. 0x1F4B,0x1F51,0x1F67,0x1F75,0x1F7B,0x1F85,0x1F91,0x1F97,0x1F99,0x1F9D,0x1FA5,0x1FAF,0x1FB5,0x1FBB,0x1FD3,0x1FE1,
  2274. 0x1FE7,0x1FEB,0x1FF3,0x1FFF,0x2011,0x201B,0x201D,0x2027,0x2029,0x202D,0x2033,0x2047,0x204D,0x2051,0x205F,0x2063,
  2275. 0x2065,0x2069,0x2077,0x207D,0x2089,0x20A1,0x20AB,0x20B1,0x20B9,0x20C3,0x20C5,0x20E3,0x20E7,0x20ED,0x20EF,0x20FB,
  2276. 0x20FF,0x210D,0x2113,0x2135,0x2141,0x2149,0x214F,0x2159,0x215B,0x215F,0x2173,0x217D,0x2185,0x2195,0x2197,0x21A1,
  2277. 0x21AF,0x21B3,0x21B5,0x21C1,0x21C7,0x21D7,0x21DD,0x21E5,0x21E9,0x21F1,0x21F5,0x21FB,0x2203,0x2209,0x220F,0x221B,
  2278. 0x2221,0x2225,0x222B,0x2231,0x2239,0x224B,0x224F,0x2263,0x2267,0x2273,0x2275,0x227F,0x2285,0x2287,0x2291,0x229D,
  2279. 0x229F,0x22A3,0x22B7,0x22BD,0x22DB,0x22E1,0x22E5,0x22ED,0x22F7,0x2303,0x2309,0x230B,0x2327,0x2329,0x232F,0x2333,
  2280. 0x2335,0x2345,0x2351,0x2353,0x2359,0x2363,0x236B,0x2383,0x238F,0x2395,0x23A7,0x23AD,0x23B1,0x23BF,0x23C5,0x23C9,
  2281. 0x23D5,0x23DD,0x23E3,0x23EF,0x23F3,0x23F9,0x2405,0x240B,0x2417,0x2419,0x2429,0x243D,0x2441,0x2443,0x244D,0x245F,
  2282. 0x2467,0x246B,0x2479,0x247D,0x247F,0x2485,0x249B,0x24A1,0x24AF,0x24B5,0x24BB,0x24C5,0x24CB,0x24CD,0x24D7,0x24D9,
  2283. 0x24DD,0x24DF,0x24F5,0x24F7,0x24FB,0x2501,0x2507,0x2513,0x2519,0x2527,0x2531,0x253D,0x2543,0x254B,0x254F,0x2573,
  2284. 0x2581,0x258D,0x2593,0x2597,0x259D,0x259F,0x25AB,0x25B1,0x25BD,0x25CD,0x25CF,0x25D9,0x25E1,0x25F7,0x25F9,0x2605,
  2285. 0x260B,0x260F,0x2615,0x2627,0x2629,0x2635,0x263B,0x263F,0x264B,0x2653,0x2659,0x2665,0x2669,0x266F,0x267B,0x2681,
  2286. 0x2683,0x268F,0x269B,0x269F,0x26AD,0x26B3,0x26C3,0x26C9,0x26CB,0x26D5,0x26DD,0x26EF,0x26F5,0x2717,0x2719,0x2735,
  2287. 0x2737,0x274D,0x2753,0x2755,0x275F,0x276B,0x276D,0x2773,0x2777,0x277F,0x2795,0x279B,0x279D,0x27A7,0x27AF,0x27B3,
  2288. 0x27B9,0x27C1,0x27C5,0x27D1,0x27E3,0x27EF,0x2803,0x2807,0x280D,0x2813,0x281B,0x281F,0x2821,0x2831,0x283D,0x283F,
  2289. 0x2849,0x2851,0x285B,0x285D,0x2861,0x2867,0x2875,0x2881,0x2897,0x289F,0x28BB,0x28BD,0x28C1,0x28D5,0x28D9,0x28DB,
  2290. 0x28DF,0x28ED,0x28F7,0x2903,0x2905,0x2911,0x2921,0x2923,0x293F,0x2947,0x295D,0x2965,0x2969,0x296F,0x2975,0x2983,
  2291. 0x2987,0x298F,0x299B,0x29A1,0x29A7,0x29AB,0x29BF,0x29C3,0x29D5,0x29D7,0x29E3,0x29E9,0x29ED,0x29F3,0x2A01,0x2A13,
  2292. 0x2A1D,0x2A25,0x2A2F,0x2A4F,0x2A55,0x2A5F,0x2A65,0x2A6B,0x2A6D,0x2A73,0x2A83,0x2A89,0x2A8B,0x2A97,0x2A9D,0x2AB9,
  2293. 0x2ABB,0x2AC5,0x2ACD,0x2ADD,0x2AE3,0x2AEB,0x2AF1,0x2AFB,0x2B13,0x2B27,0x2B31,0x2B33,0x2B3D,0x2B3F,0x2B4B,0x2B4F,
  2294. 0x2B55,0x2B69,0x2B6D,0x2B6F,0x2B7B,0x2B8D,0x2B97,0x2B99,0x2BA3,0x2BA5,0x2BA9,0x2BBD,0x2BCD,0x2BE7,0x2BEB,0x2BF3,
  2295. 0x2BF9,0x2BFD,0x2C09,0x2C0F,0x2C17,0x2C23,0x2C2F,0x2C35,0x2C39,0x2C41,0x2C57,0x2C59,0x2C69,0x2C77,0x2C81,0x2C87,
  2296. 0x2C93,0x2C9F,0x2CAD,0x2CB3,0x2CB7,0x2CCB,0x2CCF,0x2CDB,0x2CE1,0x2CE3,0x2CE9,0x2CEF,0x2CFF,0x2D07,0x2D1D,0x2D1F,
  2297. 0x2D3B,0x2D43,0x2D49,0x2D4D,0x2D61,0x2D65,0x2D71,0x2D89,0x2D9D,0x2DA1,0x2DA9,0x2DB3,0x2DB5,0x2DC5,0x2DC7,0x2DD3,
  2298. 0x2DDF,0x2E01,0x2E03,0x2E07,0x2E0D,0x2E19,0x2E1F,0x2E25,0x2E2D,0x2E33,0x2E37,0x2E39,0x2E3F,0x2E57,0x2E5B,0x2E6F,
  2299. 0x2E79,0x2E7F,0x2E85,0x2E93,0x2E97,0x2E9D,0x2EA3,0x2EA5,0x2EB1,0x2EB7,0x2EC1,0x2EC3,0x2ECD,0x2ED3,0x2EE7,0x2EEB,
  2300. 0x2F05,0x2F09,0x2F0B,0x2F11,0x2F27,0x2F29,0x2F41,0x2F45,0x2F4B,0x2F4D,0x2F51,0x2F57,0x2F6F,0x2F75,0x2F7D,0x2F81,
  2301. 0x2F83,0x2FA5,0x2FAB,0x2FB3,0x2FC3,0x2FCF,0x2FD1,0x2FDB,0x2FDD,0x2FE7,0x2FED,0x2FF5,0x2FF9,0x3001,0x300D,0x3023,
  2302. 0x3029,0x3037,0x303B,0x3055,0x3059,0x305B,0x3067,0x3071,0x3079,0x307D,0x3085,0x3091,0x3095,0x30A3,0x30A9,0x30B9,
  2303. 0x30BF,0x30C7,0x30CB,0x30D1,0x30D7,0x30DF,0x30E5,0x30EF,0x30FB,0x30FD,0x3103,0x3109,0x3119,0x3121,0x3127,0x312D,
  2304. 0x3139,0x3143,0x3145,0x314B,0x315D,0x3161,0x3167,0x316D,0x3173,0x317F,0x3191,0x3199,0x319F,0x31A9,0x31B1,0x31C3,
  2305. 0x31C7,0x31D5,0x31DB,0x31ED,0x31F7,0x31FF,0x3209,0x3215,0x3217,0x321D,0x3229,0x3235,0x3259,0x325D,0x3263,0x326B,
  2306. 0x326F,0x3275,0x3277,0x327B,0x328D,0x3299,0x329F,0x32A7,0x32AD,0x32B3,0x32B7,0x32C9,0x32CB,0x32CF,0x32D1,0x32E9,
  2307. 0x32ED,0x32F3,0x32F9,0x3307,0x3325,0x332B,0x332F,0x3335,0x3341,0x3347,0x335B,0x335F,0x3367,0x336B,0x3373,0x3379,
  2308. 0x337F,0x3383,0x33A1,0x33A3,0x33AD,0x33B9,0x33C1,0x33CB,0x33D3,0x33EB,0x33F1,0x33FD,0x3401,0x340F,0x3413,0x3419,
  2309. 0x341B,0x3437,0x3445,0x3455,0x3457,0x3463,0x3469,0x346D,0x3481,0x348B,0x3491,0x3497,0x349D,0x34A5,0x34AF,0x34BB,
  2310. 0x34C9,0x34D3,0x34E1,0x34F1,0x34FF,0x3509,0x3517,0x351D,0x352D,0x3533,0x353B,0x3541,0x3551,0x3565,0x356F,0x3571,
  2311. 0x3577,0x357B,0x357D,0x3581,0x358D,0x358F,0x3599,0x359B,0x35A1,0x35B7,0x35BD,0x35BF,0x35C3,0x35D5,0x35DD,0x35E7,
  2312. 0x35EF,0x3605,0x3607,0x3611,0x3623,0x3631,0x3635,0x3637,0x363B,0x364D,0x364F,0x3653,0x3659,0x3661,0x366B,0x366D,
  2313. 0x368B,0x368F,0x36AD,0x36AF,0x36B9,0x36BB,0x36CD,0x36D1,0x36E3,0x36E9,0x36F7,0x3701,0x3703,0x3707,0x371B,0x373F,
  2314. 0x3745,0x3749,0x374F,0x375D,0x3761,0x3775,0x377F,0x378D,0x37A3,0x37A9,0x37AB,0x37C9,0x37D5,0x37DF,0x37F1,0x37F3,
  2315. 0x37F7,0x3805,0x380B,0x3821,0x3833,0x3835,0x3841,0x3847,0x384B,0x3853,0x3857,0x385F,0x3865,0x386F,0x3871,0x387D,
  2316. 0x388F,0x3899,0x38A7,0x38B7,0x38C5,0x38C9,0x38CF,0x38D5,0x38D7,0x38DD,0x38E1,0x38E3,0x38FF,0x3901,0x391D,0x3923,
  2317. 0x3925,0x3929,0x392F,0x393D,0x3941,0x394D,0x395B,0x396B,0x3979,0x397D,0x3983,0x398B,0x3991,0x3995,0x399B,0x39A1,
  2318. 0x39A7,0x39AF,0x39B3,0x39BB,0x39BF,0x39CD,0x39DD,0x39E5,0x39EB,0x39EF,0x39FB,0x3A03,0x3A13,0x3A15,0x3A1F,0x3A27,
  2319. 0x3A2B,0x3A31,0x3A4B,0x3A51,0x3A5B,0x3A63,0x3A67,0x3A6D,0x3A79,0x3A87,0x3AA5,0x3AA9,0x3AB7,0x3ACD,0x3AD5,0x3AE1,
  2320. 0x3AE5,0x3AEB,0x3AF3,0x3AFD,0x3B03,0x3B11,0x3B1B,0x3B21,0x3B23,0x3B2D,0x3B39,0x3B45,0x3B53,0x3B59,0x3B5F,0x3B71,
  2321. 0x3B7B,0x3B81,0x3B89,0x3B9B,0x3B9F,0x3BA5,0x3BA7,0x3BAD,0x3BB7,0x3BB9,0x3BC3,0x3BCB,0x3BD1,0x3BD7,0x3BE1,0x3BE3,
  2322. 0x3BF5,0x3BFF,0x3C01,0x3C0D,0x3C11,0x3C17,0x3C1F,0x3C29,0x3C35,0x3C43,0x3C4F,0x3C53,0x3C5B,0x3C65,0x3C6B,0x3C71,
  2323. 0x3C85,0x3C89,0x3C97,0x3CA7,0x3CB5,0x3CBF,0x3CC7,0x3CD1,0x3CDD,0x3CDF,0x3CF1,0x3CF7,0x3D03,0x3D0D,0x3D19,0x3D1B,
  2324. 0x3D1F,0x3D21,0x3D2D,0x3D33,0x3D37,0x3D3F,0x3D43,0x3D6F,0x3D73,0x3D75,0x3D79,0x3D7B,0x3D85,0x3D91,0x3D97,0x3D9D,
  2325. 0x3DAB,0x3DAF,0x3DB5,0x3DBB,0x3DC1,0x3DC9,0x3DCF,0x3DF3,0x3E05,0x3E09,0x3E0F,0x3E11,0x3E1D,0x3E23,0x3E29,0x3E2F,
  2326. 0x3E33,0x3E41,0x3E57,0x3E63,0x3E65,0x3E77,0x3E81,0x3E87,0x3EA1,0x3EB9,0x3EBD,0x3EBF,0x3EC3,0x3EC5,0x3EC9,0x3ED7,
  2327. 0x3EDB,0x3EE1,0x3EE7,0x3EEF,0x3EFF,0x3F0B,0x3F0D,0x3F37,0x3F3B,0x3F3D,0x3F41,0x3F59,0x3F5F,0x3F65,0x3F67,0x3F79,
  2328. 0x3F7D,0x3F8B,0x3F91,0x3FAD,0x3FBF,0x3FCD,0x3FD3,0x3FDD,0x3FE9,0x3FEB,0x3FF1,0x3FFD,0x401B,0x4021,0x4025,0x402B,
  2329. 0x4031,0x403F,0x4043,0x4045,0x405D,0x4061,0x4067,0x406D,0x4087,0x4091,0x40A3,0x40A9,0x40B1,0x40B7,0x40BD,0x40DB,
  2330. 0x40DF,0x40EB,0x40F7,0x40F9,0x4109,0x410B,0x4111,0x4115,0x4121,0x4133,0x4135,0x413B,0x413F,0x4159,0x4165,0x416B,
  2331. 0x4177,0x417B,0x4193,0x41AB,0x41B7,0x41BD,0x41BF,0x41CB,0x41E7,0x41EF,0x41F3,0x41F9,0x4205,0x4207,0x4219,0x421F,
  2332. 0x4223,0x4229,0x422F,0x4243,0x4253,0x4255,0x425B,0x4261,0x4273,0x427D,0x4283,0x4285,0x4289,0x4291,0x4297,0x429D,
  2333. 0x42B5,0x42C5,0x42CB,0x42D3,0x42DD,0x42E3,0x42F1,0x4307,0x430F,0x431F,0x4325,0x4327,0x4333,0x4337,0x4339,0x434F,
  2334. 0x4357,0x4369,0x438B,0x438D,0x4393,0x43A5,0x43A9,0x43AF,0x43B5,0x43BD,0x43C7,0x43CF,0x43E1,0x43E7,0x43EB,0x43ED,
  2335. 0x43F1,0x43F9,0x4409,0x440B,0x4417,0x4423,0x4429,0x443B,0x443F,0x4445,0x444B,0x4451,0x4453,0x4459,0x4465,0x446F,
  2336. 0x4483,0x448F,0x44A1,0x44A5,0x44AB,0x44AD,0x44BD,0x44BF,0x44C9,0x44D7,0x44DB,0x44F9,0x44FB,0x4505,0x4511,0x4513,
  2337. 0x452B,0x4531,0x4541,0x4549,0x4553,0x4555,0x4561,0x4577,0x457D,0x457F,0x458F,0x45A3,0x45AD,0x45AF,0x45BB,0x45C7,
  2338. 0x45D9,0x45E3,0x45EF,0x45F5,0x45F7,0x4601,0x4603,0x4609,0x4613,0x4625,0x4627,0x4633,0x4639,0x463D,0x4643,0x4645,
  2339. 0x465D,0x4679,0x467B,0x467F,0x4681,0x468B,0x468D,0x469D,0x46A9,0x46B1,0x46C7,0x46C9,0x46CF,0x46D3,0x46D5,0x46DF,
  2340. 0x46E5,0x46F9,0x4705,0x470F,0x4717,0x4723,0x4729,0x472F,0x4735,0x4739,0x474B,0x474D,0x4751,0x475D,0x476F,0x4771,
  2341. 0x477D,0x4783,0x4787,0x4789,0x4799,0x47A5,0x47B1,0x47BF,0x47C3,0x47CB,0x47DD,0x47E1,0x47ED,0x47FB,0x4801,0x4807,
  2342. 0x480B,0x4813,0x4819,0x481D,0x4831,0x483D,0x4847,0x4855,0x4859,0x485B,0x486B,0x486D,0x4879,0x4897,0x489B,0x48A1,
  2343. 0x48B9,0x48CD,0x48E5,0x48EF,0x48F7,0x4903,0x490D,0x4919,0x491F,0x492B,0x4937,0x493D,0x4945,0x4955,0x4963,0x4969,
  2344. 0x496D,0x4973,0x4997,0x49AB,0x49B5,0x49D3,0x49DF,0x49E1,0x49E5,0x49E7,0x4A03,0x4A0F,0x4A1D,0x4A23,0x4A39,0x4A41,
  2345. 0x4A45,0x4A57,0x4A5D,0x4A6B,0x4A7D,0x4A81,0x4A87,0x4A89,0x4A8F,0x4AB1,0x4AC3,0x4AC5,0x4AD5,0x4ADB,0x4AED,0x4AEF,
  2346. 0x4B07,0x4B0B,0x4B0D,0x4B13,0x4B1F,0x4B25,0x4B31,0x4B3B,0x4B43,0x4B49,0x4B59,0x4B65,0x4B6D,0x4B77,0x4B85,0x4BAD,
  2347. 0x4BB3,0x4BB5,0x4BBB,0x4BBF,0x4BCB,0x4BD9,0x4BDD,0x4BDF,0x4BE3,0x4BE5,0x4BE9,0x4BF1,0x4BF7,0x4C01,0x4C07,0x4C0D,
  2348. 0x4C0F,0x4C15,0x4C1B,0x4C21,0x4C2D,0x4C33,0x4C4B,0x4C55,0x4C57,0x4C61,0x4C67,0x4C73,0x4C79,0x4C7F,0x4C8D,0x4C93,
  2349. 0x4C99,0x4CCD,0x4CE1,0x4CE7,0x4CF1,0x4CF3,0x4CFD,0x4D05,0x4D0F,0x4D1B,0x4D27,0x4D29,0x4D2F,0x4D33,0x4D41,0x4D51,
  2350. 0x4D59,0x4D65,0x4D6B,0x4D81,0x4D83,0x4D8D,0x4D95,0x4D9B,0x4DB1,0x4DB3,0x4DC9,0x4DCF,0x4DD7,0x4DE1,0x4DED,0x4DF9,
  2351. 0x4DFB,0x4E05,0x4E0B,0x4E17,0x4E19,0x4E1D,0x4E2B,0x4E35,0x4E37,0x4E3D,0x4E4F,0x4E53,0x4E5F,0x4E67,0x4E79,0x4E85,
  2352. 0x4E8B,0x4E91,0x4E95,0x4E9B,0x4EA1,0x4EAF,0x4EB3,0x4EB5,0x4EC1,0x4ECD,0x4ED1,0x4ED7,0x4EE9,0x4EFB,0x4F07,0x4F09,
  2353. 0x4F19,0x4F25,0x4F2D,0x4F3F,0x4F49,0x4F63,0x4F67,0x4F6D,0x4F75,0x4F7B,0x4F81,0x4F85,0x4F87,0x4F91,0x4FA5,0x4FA9,
  2354. 0x4FAF,0x4FB7,0x4FBB,0x4FCF,0x4FD9,0x4FDB,0x4FFD,0x4FFF,0x5003,0x501B,0x501D,0x5029,0x5035,0x503F,0x5045,0x5047,
  2355. 0x5053,0x5071,0x5077,0x5083,0x5093,0x509F,0x50A1,0x50B7,0x50C9,0x50D5,0x50E3,0x50ED,0x50EF,0x50FB,0x5107,0x510B,
  2356. 0x510D,0x5111,0x5117,0x5123,0x5125,0x5135,0x5147,0x5149,0x5171,0x5179,0x5189,0x518F,0x5197,0x51A1,0x51A3,0x51A7,
  2357. 0x51B9,0x51C1,0x51CB,0x51D3,0x51DF,0x51E3,0x51F5,0x51F7,0x5209,0x5213,0x5215,0x5219,0x521B,0x521F,0x5227,0x5243,
  2358. 0x5245,0x524B,0x5261,0x526D,0x5273,0x5281,0x5293,0x5297,0x529D,0x52A5,0x52AB,0x52B1,0x52BB,0x52C3,0x52C7,0x52C9,
  2359. 0x52DB,0x52E5,0x52EB,0x52FF,0x5315,0x531D,0x5323,0x5341,0x5345,0x5347,0x534B,0x535D,0x5363,0x5381,0x5383,0x5387,
  2360. 0x538F,0x5395,0x5399,0x539F,0x53AB,0x53B9,0x53DB,0x53E9,0x53EF,0x53F3,0x53F5,0x53FB,0x53FF,0x540D,0x5411,0x5413,
  2361. 0x5419,0x5435,0x5437,0x543B,0x5441,0x5449,0x5453,0x5455,0x545F,0x5461,0x546B,0x546D,0x5471,0x548F,0x5491,0x549D,
  2362. 0x54A9,0x54B3,0x54C5,0x54D1,0x54DF,0x54E9,0x54EB,0x54F7,0x54FD,0x5507,0x550D,0x551B,0x5527,0x552B,0x5539,0x553D,
  2363. 0x554F,0x5551,0x555B,0x5563,0x5567,0x556F,0x5579,0x5585,0x5597,0x55A9,0x55B1,0x55B7,0x55C9,0x55D9,0x55E7,0x55ED,
  2364. 0x55F3,0x55FD,0x560B,0x560F,0x5615,0x5617,0x5623,0x562F,0x5633,0x5639,0x563F,0x564B,0x564D,0x565D,0x565F,0x566B,
  2365. 0x5671,0x5675,0x5683,0x5689,0x568D,0x568F,0x569B,0x56AD,0x56B1,0x56D5,0x56E7,0x56F3,0x56FF,0x5701,0x5705,0x5707,
  2366. 0x570B,0x5713,0x571F,0x5723,0x5747,0x574D,0x575F,0x5761,0x576D,0x5777,0x577D,0x5789,0x57A1,0x57A9,0x57AF,0x57B5,
  2367. 0x57C5,0x57D1,0x57D3,0x57E5,0x57EF,0x5803,0x580D,0x580F,0x5815,0x5827,0x582B,0x582D,0x5855,0x585B,0x585D,0x586D,
  2368. 0x586F,0x5873,0x587B,0x588D,0x5897,0x58A3,0x58A9,0x58AB,0x58B5,0x58BD,0x58C1,0x58C7,0x58D3,0x58D5,0x58DF,0x58F1,
  2369. 0x58F9,0x58FF,0x5903,0x5917,0x591B,0x5921,0x5945,0x594B,0x594D,0x5957,0x595D,0x5975,0x597B,0x5989,0x5999,0x599F,
  2370. 0x59B1,0x59B3,0x59BD,0x59D1,0x59DB,0x59E3,0x59E9,0x59ED,0x59F3,0x59F5,0x59FF,0x5A01,0x5A0D,0x5A11,0x5A13,0x5A17,
  2371. 0x5A1F,0x5A29,0x5A2F,0x5A3B,0x5A4D,0x5A5B,0x5A67,0x5A77,0x5A7F,0x5A85,0x5A95,0x5A9D,0x5AA1,0x5AA3,0x5AA9,0x5ABB,
  2372. 0x5AD3,0x5AE5,0x5AEF,0x5AFB,0x5AFD,0x5B01,0x5B0F,0x5B19,0x5B1F,0x5B25,0x5B2B,0x5B3D,0x5B49,0x5B4B,0x5B67,0x5B79,
  2373. 0x5B87,0x5B97,0x5BA3,0x5BB1,0x5BC9,0x5BD5,0x5BEB,0x5BF1,0x5BF3,0x5BFD,0x5C05,0x5C09,0x5C0B,0x5C0F,0x5C1D,0x5C29,
  2374. 0x5C2F,0x5C33,0x5C39,0x5C47,0x5C4B,0x5C4D,0x5C51,0x5C6F,0x5C75,0x5C77,0x5C7D,0x5C87,0x5C89,0x5CA7,0x5CBD,0x5CBF,
  2375. 0x5CC3,0x5CC9,0x5CD1,0x5CD7,0x5CDD,0x5CED,0x5CF9,0x5D05,0x5D0B,0x5D13,0x5D17,0x5D19,0x5D31,0x5D3D,0x5D41,0x5D47,
  2376. 0x5D4F,0x5D55,0x5D5B,0x5D65,0x5D67,0x5D6D,0x5D79,0x5D95,0x5DA3,0x5DA9,0x5DAD,0x5DB9,0x5DC1,0x5DC7,0x5DD3,0x5DD7,
  2377. 0x5DDD,0x5DEB,0x5DF1,0x5DFD,0x5E07,0x5E0D,0x5E13,0x5E1B,0x5E21,0x5E27,0x5E2B,0x5E2D,0x5E31,0x5E39,0x5E45,0x5E49,
  2378. 0x5E57,0x5E69,0x5E73,0x5E75,0x5E85,0x5E8B,0x5E9F,0x5EA5,0x5EAF,0x5EB7,0x5EBB,0x5ED9,0x5EFD,0x5F09,0x5F11,0x5F27,
  2379. 0x5F33,0x5F35,0x5F3B,0x5F47,0x5F57,0x5F5D,0x5F63,0x5F65,0x5F77,0x5F7B,0x5F95,0x5F99,0x5FA1,0x5FB3,0x5FBD,0x5FC5,
  2380. 0x5FCF,0x5FD5,0x5FE3,0x5FE7,0x5FFB,0x6011,0x6023,0x602F,0x6037,0x6053,0x605F,0x6065,0x606B,0x6073,0x6079,0x6085,
  2381. 0x609D,0x60AD,0x60BB,0x60BF,0x60CD,0x60D9,0x60DF,0x60E9,0x60F5,0x6109,0x610F,0x6113,0x611B,0x612D,0x6139,0x614B,
  2382. 0x6155,0x6157,0x615B,0x616F,0x6179,0x6187,0x618B,0x6191,0x6193,0x619D,0x61B5,0x61C7,0x61C9,0x61CD,0x61E1,0x61F1,
  2383. 0x61FF,0x6209,0x6217,0x621D,0x6221,0x6227,0x623B,0x6241,0x624B,0x6251,0x6253,0x625F,0x6265,0x6283,0x628D,0x6295,
  2384. 0x629B,0x629F,0x62A5,0x62AD,0x62D5,0x62D7,0x62DB,0x62DD,0x62E9,0x62FB,0x62FF,0x6305,0x630D,0x6317,0x631D,0x632F,
  2385. 0x6341,0x6343,0x634F,0x635F,0x6367,0x636D,0x6371,0x6377,0x637D,0x637F,0x63B3,0x63C1,0x63C5,0x63D9,0x63E9,0x63EB,
  2386. 0x63EF,0x63F5,0x6401,0x6403,0x6409,0x6415,0x6421,0x6427,0x642B,0x6439,0x6443,0x6449,0x644F,0x645D,0x6467,0x6475,
  2387. 0x6485,0x648D,0x6493,0x649F,0x64A3,0x64AB,0x64C1,0x64C7,0x64C9,0x64DB,0x64F1,0x64F7,0x64F9,0x650B,0x6511,0x6521,
  2388. 0x652F,0x6539,0x653F,0x654B,0x654D,0x6553,0x6557,0x655F,0x6571,0x657D,0x658D,0x658F,0x6593,0x65A1,0x65A5,0x65AD,
  2389. 0x65B9,0x65C5,0x65E3,0x65F3,0x65FB,0x65FF,0x6601,0x6607,0x661D,0x6629,0x6631,0x663B,0x6641,0x6647,0x664D,0x665B,
  2390. 0x6661,0x6673,0x667D,0x6689,0x668B,0x6695,0x6697,0x669B,0x66B5,0x66B9,0x66C5,0x66CD,0x66D1,0x66E3,0x66EB,0x66F5,
  2391. 0x6703,0x6713,0x6719,0x671F,0x6727,0x6731,0x6737,0x673F,0x6745,0x6751,0x675B,0x676F,0x6779,0x6781,0x6785,0x6791,
  2392. 0x67AB,0x67BD,0x67C1,0x67CD,0x67DF,0x67E5,0x6803,0x6809,0x6811,0x6817,0x682D,0x6839,0x683B,0x683F,0x6845,0x684B,
  2393. 0x684D,0x6857,0x6859,0x685D,0x6863,0x6869,0x686B,0x6871,0x6887,0x6899,0x689F,0x68B1,0x68BD,0x68C5,0x68D1,0x68D7,
  2394. 0x68E1,0x68ED,0x68EF,0x68FF,0x6901,0x690B,0x690D,0x6917,0x6929,0x692F,0x6943,0x6947,0x6949,0x694F,0x6965,0x696B,
  2395. 0x6971,0x6983,0x6989,0x6997,0x69A3,0x69B3,0x69B5,0x69BB,0x69C1,0x69C5,0x69D3,0x69DF,0x69E3,0x69E5,0x69F7,0x6A07,
  2396. 0x6A2B,0x6A37,0x6A3D,0x6A4B,0x6A67,0x6A69,0x6A75,0x6A7B,0x6A87,0x6A8D,0x6A91,0x6A93,0x6AA3,0x6AC1,0x6AC9,0x6AE1,
  2397. 0x6AE7,0x6B05,0x6B0F,0x6B11,0x6B23,0x6B27,0x6B2D,0x6B39,0x6B41,0x6B57,0x6B59,0x6B5F,0x6B75,0x6B87,0x6B89,0x6B93,
  2398. 0x6B95,0x6B9F,0x6BBD,0x6BBF,0x6BDB,0x6BE1,0x6BEF,0x6BFF,0x6C05,0x6C19,0x6C29,0x6C2B,0x6C31,0x6C35,0x6C55,0x6C59,
  2399. 0x6C5B,0x6C5F,0x6C65,0x6C67,0x6C73,0x6C77,0x6C7D,0x6C83,0x6C8F,0x6C91,0x6C97,0x6C9B,0x6CA1,0x6CA9,0x6CAF,0x6CB3,
  2400. 0x6CC7,0x6CCB,0x6CEB,0x6CF5,0x6CFD,0x6D0D,0x6D0F,0x6D25,0x6D27,0x6D2B,0x6D31,0x6D39,0x6D3F,0x6D4F,0x6D5D,0x6D61,
  2401. 0x6D73,0x6D7B,0x6D7F,0x6D93,0x6D99,0x6DA5,0x6DB1,0x6DB7,0x6DC1,0x6DC3,0x6DCD,0x6DCF,0x6DDB,0x6DF7,0x6E03,0x6E15,
  2402. 0x6E17,0x6E29,0x6E33,0x6E3B,0x6E45,0x6E75,0x6E77,0x6E7B,0x6E81,0x6E89,0x6E93,0x6E95,0x6E9F,0x6EBD,0x6EBF,0x6EE3,
  2403. 0x6EE9,0x6EF3,0x6EF9,0x6EFB,0x6F0D,0x6F11,0x6F17,0x6F1F,0x6F2F,0x6F3D,0x6F4D,0x6F53,0x6F61,0x6F65,0x6F79,0x6F7D,
  2404. 0x6F83,0x6F85,0x6F8F,0x6F9B,0x6F9D,0x6FA3,0x6FAF,0x6FB5,0x6FBB,0x6FBF,0x6FCB,0x6FCD,0x6FD3,0x6FD7,0x6FE3,0x6FE9,
  2405. 0x6FF1,0x6FF5,0x6FF7,0x6FFD,0x700F,0x7019,0x701F,0x7027,0x7033,0x7039,0x704F,0x7051,0x7057,0x7063,0x7075,0x7079,
  2406. 0x7087,0x708D,0x7091,0x70A5,0x70AB,0x70BB,0x70C3,0x70C7,0x70CF,0x70E5,0x70ED,0x70F9,0x70FF,0x7105,0x7115,0x7121,
  2407. 0x7133,0x7151,0x7159,0x715D,0x715F,0x7163,0x7169,0x7183,0x7187,0x7195,0x71AD,0x71C3,0x71C9,0x71CB,0x71D1,0x71DB,
  2408. 0x71E1,0x71EF,0x71F5,0x71FB,0x7207,0x7211,0x7217,0x7219,0x7225,0x722F,0x723B,0x7243,0x7255,0x7267,0x7271,0x7277,
  2409. 0x727F,0x728F,0x7295,0x729B,0x72A3,0x72B3,0x72C7,0x72CB,0x72CD,0x72D7,0x72D9,0x72E3,0x72EF,0x72F5,0x72FD,0x7303,
  2410. 0x730D,0x7321,0x732B,0x733D,0x7357,0x735B,0x7361,0x737F,0x7381,0x7385,0x738D,0x7393,0x739F,0x73AB,0x73BD,0x73C1,
  2411. 0x73C9,0x73DF,0x73E5,0x73E7,0x73F3,0x7415,0x741B,0x742D,0x7439,0x743F,0x7441,0x745D,0x746B,0x747B,0x7489,0x748D,
  2412. 0x749B,0x74A7,0x74AB,0x74B1,0x74B7,0x74B9,0x74DD,0x74E1,0x74E7,0x74FB,0x7507,0x751F,0x7525,0x753B,0x753D,0x754D,
  2413. 0x755F,0x756B,0x7577,0x7589,0x758B,0x7591,0x7597,0x759D,0x75A1,0x75A7,0x75B5,0x75B9,0x75BB,0x75D1,0x75D9,0x75E5,
  2414. 0x75EB,0x75F5,0x75FB,0x7603,0x760F,0x7621,0x762D,0x7633,0x763D,0x763F,0x7655,0x7663,0x7669,0x766F,0x7673,0x7685,
  2415. 0x768B,0x769F,0x76B5,0x76B7,0x76C3,0x76DB,0x76DF,0x76F1,0x7703,0x7705,0x771B,0x771D,0x7721,0x772D,0x7735,0x7741,
  2416. 0x774B,0x7759,0x775D,0x775F,0x7771,0x7781,0x77A7,0x77AD,0x77B3,0x77B9,0x77C5,0x77CF,0x77D5,0x77E1,0x77E9,0x77EF,
  2417. 0x77F3,0x77F9,0x7807,0x7825,0x782B,0x7835,0x783D,0x7853,0x7859,0x7861,0x786D,0x7877,0x7879,0x7883,0x7885,0x788B,
  2418. 0x7895,0x7897,0x78A1,0x78AD,0x78BF,0x78D3,0x78D9,0x78DD,0x78E5,0x78FB,0x7901,0x7907,0x7925,0x792B,0x7939,0x793F,
  2419. 0x794B,0x7957,0x795D,0x7967,0x7969,0x7973,0x7991,0x7993,0x79A3,0x79AB,0x79AF,0x79B1,0x79B7,0x79C9,0x79CD,0x79CF,
  2420. 0x79D5,0x79D9,0x79F3,0x79F7,0x79FF,0x7A05,0x7A0F,0x7A11,0x7A15,0x7A1B,0x7A23,0x7A27,0x7A2D,0x7A4B,0x7A57,0x7A59,
  2421. 0x7A5F,0x7A65,0x7A69,0x7A7D,0x7A93,0x7A9B,0x7A9F,0x7AA1,0x7AA5,0x7AED,0x7AF5,0x7AF9,0x7B01,0x7B17,0x7B19,0x7B1D,
  2422. 0x7B2B,0x7B35,0x7B37,0x7B3B,0x7B4F,0x7B55,0x7B5F,0x7B71,0x7B77,0x7B8B,0x7B9B,0x7BA1,0x7BA9,0x7BAF,0x7BB3,0x7BC7,
  2423. 0x7BD3,0x7BE9,0x7BEB,0x7BEF,0x7BF1,0x7BFD,0x7C07,0x7C19,0x7C1B,0x7C31,0x7C37,0x7C49,0x7C67,0x7C69,0x7C73,0x7C81,
  2424. 0x7C8B,0x7C93,0x7CA3,0x7CD5,0x7CDB,0x7CE5,0x7CED,0x7CF7,0x7D03,0x7D09,0x7D1B,0x7D1D,0x7D33,0x7D39,0x7D3B,0x7D3F,
  2425. 0x7D45,0x7D4D,0x7D53,0x7D59,0x7D63,0x7D75,0x7D77,0x7D8D,0x7D8F,0x7D9F,0x7DAD,0x7DB7,0x7DBD,0x7DBF,0x7DCB,0x7DD5,
  2426. 0x7DE9,0x7DED,0x7DFB,0x7E01,0x7E05,0x7E29,0x7E2B,0x7E2F,0x7E35,0x7E41,0x7E43,0x7E47,0x7E55,0x7E61,0x7E67,0x7E6B,
  2427. 0x7E71,0x7E73,0x7E79,0x7E7D,0x7E91,0x7E9B,0x7E9D,0x7EA7,0x7EAD,0x7EB9,0x7EBB,0x7ED3,0x7EDF,0x7EEB,0x7EF1,0x7EF7,
  2428. 0x7EFB,0x7F13,0x7F15,0x7F19,0x7F31,0x7F33,0x7F39,0x7F3D,0x7F43,0x7F4B,0x7F5B,0x7F61,0x7F63,0x7F6D,0x7F79,0x7F87,
  2429. 0x7F8D,0x7FAF,0x7FB5,0x7FC3,0x7FC9,0x7FCD,0x7FCF
  2430. };