mpmath.cpp 165 KB

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