BigInteger.php 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP arbitrary precision integer arithmetic library.
  5. *
  6. * Supports base-2, base-10, base-16, and base-256 numbers. Uses the GMP or BCMath extensions, if available,
  7. * and an internal implementation, otherwise.
  8. *
  9. * PHP versions 4 and 5
  10. *
  11. * {@internal (all DocBlock comments regarding implementation - such as the one that follows - refer to the
  12. * {@link MATH_BIGINTEGER_MODE_INTERNAL MATH_BIGINTEGER_MODE_INTERNAL} mode)
  13. *
  14. * Math_BigInteger uses base-2**26 to perform operations such as multiplication and division and
  15. * base-2**52 (ie. two base 2**26 digits) to perform addition and subtraction. Because the largest possible
  16. * value when multiplying two base-2**26 numbers together is a base-2**52 number, double precision floating
  17. * point numbers - numbers that should be supported on most hardware and whose significand is 53 bits - are
  18. * used. As a consequence, bitwise operators such as >> and << cannot be used, nor can the modulo operator %,
  19. * which only supports integers. Although this fact will slow this library down, the fact that such a high
  20. * base is being used should more than compensate.
  21. *
  22. * When PHP version 6 is officially released, we'll be able to use 64-bit integers. This should, once again,
  23. * allow bitwise operators, and will increase the maximum possible base to 2**31 (or 2**62 for addition /
  24. * subtraction).
  25. *
  26. * Numbers are stored in {@link http://en.wikipedia.org/wiki/Endianness little endian} format. ie.
  27. * (new Math_BigInteger(pow(2, 26)))->value = array(0, 1)
  28. *
  29. * Useful resources are as follows:
  30. *
  31. * - {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf Handbook of Applied Cryptography (HAC)}
  32. * - {@link http://math.libtomcrypt.com/files/tommath.pdf Multi-Precision Math (MPM)}
  33. * - Java's BigInteger classes. See /j2se/src/share/classes/java/math in jdk-1_5_0-src-jrl.zip
  34. *
  35. * Here's an example of how to use this library:
  36. * <code>
  37. * <?php
  38. * include('Math/BigInteger.php');
  39. *
  40. * $a = new Math_BigInteger(2);
  41. * $b = new Math_BigInteger(3);
  42. *
  43. * $c = $a->add($b);
  44. *
  45. * echo $c->toString(); // outputs 5
  46. * ?>
  47. * </code>
  48. *
  49. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  50. * of this software and associated documentation files (the "Software"), to deal
  51. * in the Software without restriction, including without limitation the rights
  52. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  53. * copies of the Software, and to permit persons to whom the Software is
  54. * furnished to do so, subject to the following conditions:
  55. *
  56. * The above copyright notice and this permission notice shall be included in
  57. * all copies or substantial portions of the Software.
  58. *
  59. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  60. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  61. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  62. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  63. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  64. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  65. * THE SOFTWARE.
  66. *
  67. * @category Math
  68. * @package Math_BigInteger
  69. * @author Jim Wigginton <terrafrost@php.net>
  70. * @copyright MMVI Jim Wigginton
  71. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  72. * @link http://pear.php.net/package/Math_BigInteger
  73. */
  74. /**#@+
  75. * Reduction constants
  76. *
  77. * @access private
  78. * @see Math_BigInteger::_reduce()
  79. */
  80. /**
  81. * @see Math_BigInteger::_montgomery()
  82. * @see Math_BigInteger::_prepMontgomery()
  83. */
  84. define('MATH_BIGINTEGER_MONTGOMERY', 0);
  85. /**
  86. * @see Math_BigInteger::_barrett()
  87. */
  88. define('MATH_BIGINTEGER_BARRETT', 1);
  89. /**
  90. * @see Math_BigInteger::_mod2()
  91. */
  92. define('MATH_BIGINTEGER_POWEROF2', 2);
  93. /**
  94. * @see Math_BigInteger::_remainder()
  95. */
  96. define('MATH_BIGINTEGER_CLASSIC', 3);
  97. /**
  98. * @see Math_BigInteger::__clone()
  99. */
  100. define('MATH_BIGINTEGER_NONE', 4);
  101. /**#@-*/
  102. /**#@+
  103. * Array constants
  104. *
  105. * Rather than create a thousands and thousands of new Math_BigInteger objects in repeated function calls to add() and
  106. * multiply() or whatever, we'll just work directly on arrays, taking them in as parameters and returning them.
  107. *
  108. * @access private
  109. */
  110. /**
  111. * $result[MATH_BIGINTEGER_VALUE] contains the value.
  112. */
  113. define('MATH_BIGINTEGER_VALUE', 0);
  114. /**
  115. * $result[MATH_BIGINTEGER_SIGN] contains the sign.
  116. */
  117. define('MATH_BIGINTEGER_SIGN', 1);
  118. /**#@-*/
  119. /**#@+
  120. * @access private
  121. * @see Math_BigInteger::_montgomery()
  122. * @see Math_BigInteger::_barrett()
  123. */
  124. /**
  125. * Cache constants
  126. *
  127. * $cache[MATH_BIGINTEGER_VARIABLE] tells us whether or not the cached data is still valid.
  128. */
  129. define('MATH_BIGINTEGER_VARIABLE', 0);
  130. /**
  131. * $cache[MATH_BIGINTEGER_DATA] contains the cached data.
  132. */
  133. define('MATH_BIGINTEGER_DATA', 1);
  134. /**#@-*/
  135. /**#@+
  136. * Mode constants.
  137. *
  138. * @access private
  139. * @see Math_BigInteger::Math_BigInteger()
  140. */
  141. /**
  142. * To use the pure-PHP implementation
  143. */
  144. define('MATH_BIGINTEGER_MODE_INTERNAL', 1);
  145. /**
  146. * To use the BCMath library
  147. *
  148. * (if enabled; otherwise, the internal implementation will be used)
  149. */
  150. define('MATH_BIGINTEGER_MODE_BCMATH', 2);
  151. /**
  152. * To use the GMP library
  153. *
  154. * (if present; otherwise, either the BCMath or the internal implementation will be used)
  155. */
  156. define('MATH_BIGINTEGER_MODE_GMP', 3);
  157. /**#@-*/
  158. /**
  159. * Karatsuba Cutoff
  160. *
  161. * At what point do we switch between Karatsuba multiplication and schoolbook long multiplication?
  162. *
  163. * @access private
  164. */
  165. define('MATH_BIGINTEGER_KARATSUBA_CUTOFF', 25);
  166. /**
  167. * Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256
  168. * numbers.
  169. *
  170. * @author Jim Wigginton <terrafrost@php.net>
  171. * @version 1.0.0RC4
  172. * @access public
  173. * @package Math_BigInteger
  174. */
  175. class Math_BigInteger {
  176. /**
  177. * Holds the BigInteger's value.
  178. *
  179. * @var Array
  180. * @access private
  181. */
  182. var $value;
  183. /**
  184. * Holds the BigInteger's magnitude.
  185. *
  186. * @var Boolean
  187. * @access private
  188. */
  189. var $is_negative = false;
  190. /**
  191. * Random number generator function
  192. *
  193. * @see setRandomGenerator()
  194. * @access private
  195. */
  196. var $generator = 'mt_rand';
  197. /**
  198. * Precision
  199. *
  200. * @see setPrecision()
  201. * @access private
  202. */
  203. var $precision = -1;
  204. /**
  205. * Precision Bitmask
  206. *
  207. * @see setPrecision()
  208. * @access private
  209. */
  210. var $bitmask = false;
  211. /**
  212. * Mode independent value used for serialization.
  213. *
  214. * If the bcmath or gmp extensions are installed $this->value will be a non-serializable resource, hence the need for
  215. * a variable that'll be serializable regardless of whether or not extensions are being used. Unlike $this->value,
  216. * however, $this->hex is only calculated when $this->__sleep() is called.
  217. *
  218. * @see __sleep()
  219. * @see __wakeup()
  220. * @var String
  221. * @access private
  222. */
  223. var $hex;
  224. /**
  225. * Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers.
  226. *
  227. * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using
  228. * two's compliment. The sole exception to this is -10, which is treated the same as 10 is.
  229. *
  230. * Here's an example:
  231. * <code>
  232. * &lt;?php
  233. * include('Math/BigInteger.php');
  234. *
  235. * $a = new Math_BigInteger('0x32', 16); // 50 in base-16
  236. *
  237. * echo $a->toString(); // outputs 50
  238. * ?&gt;
  239. * </code>
  240. *
  241. * @param optional $x base-10 number or base-$base number if $base set.
  242. * @param optional integer $base
  243. * @return Math_BigInteger
  244. * @access public
  245. */
  246. function Math_BigInteger($x = 0, $base = 10)
  247. {
  248. if ( !defined('MATH_BIGINTEGER_MODE') ) {
  249. switch (true) {
  250. case extension_loaded('gmp'):
  251. define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_GMP);
  252. break;
  253. case extension_loaded('bcmath'):
  254. define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_BCMATH);
  255. break;
  256. default:
  257. define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_INTERNAL);
  258. }
  259. }
  260. if (function_exists('openssl_public_encrypt') && !defined('MATH_BIGINTEGER_OPENSSL_DISABLE') && !defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) {
  261. // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work
  262. ob_start();
  263. phpinfo();
  264. $content = ob_get_contents();
  265. ob_end_clean();
  266. preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches);
  267. $versions = array();
  268. if (!empty($matches[1])) {
  269. for ($i = 0; $i < count($matches[1]); $i++) {
  270. $versions[$matches[1][$i]] = trim(str_replace('=>', '', strip_tags($matches[2][$i])));
  271. }
  272. }
  273. // it doesn't appear that OpenSSL versions were reported upon until PHP 5.3+
  274. switch (true) {
  275. case !isset($versions['Header']):
  276. case !isset($versions['Library']):
  277. case $versions['Header'] == $versions['Library']:
  278. define('MATH_BIGINTEGER_OPENSSL_ENABLED', true);
  279. break;
  280. default:
  281. define('MATH_BIGINTEGER_OPENSSL_DISABLE', true);
  282. }
  283. }
  284. if (!defined('PHP_INT_SIZE')) {
  285. define('PHP_INT_SIZE', 4);
  286. }
  287. if (!defined('MATH_BIGINTEGER_BASE') && MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_INTERNAL) {
  288. switch (PHP_INT_SIZE) {
  289. case 8: // use 64-bit integers if int size is 8 bytes
  290. define('MATH_BIGINTEGER_BASE', 31);
  291. define('MATH_BIGINTEGER_BASE_FULL', 0x80000000);
  292. define('MATH_BIGINTEGER_MAX_DIGIT', 0x7FFFFFFF);
  293. define('MATH_BIGINTEGER_MSB', 0x40000000);
  294. // 10**9 is the closest we can get to 2**31 without passing it
  295. define('MATH_BIGINTEGER_MAX10', 1000000000);
  296. define('MATH_BIGINTEGER_MAX10_LEN', 9);
  297. // the largest digit that may be used in addition / subtraction
  298. define('MATH_BIGINTEGER_MAX_DIGIT2', pow(2, 62));
  299. break;
  300. //case 4: // use 64-bit floats if int size is 4 bytes
  301. default:
  302. define('MATH_BIGINTEGER_BASE', 26);
  303. define('MATH_BIGINTEGER_BASE_FULL', 0x4000000);
  304. define('MATH_BIGINTEGER_MAX_DIGIT', 0x3FFFFFF);
  305. define('MATH_BIGINTEGER_MSB', 0x2000000);
  306. // 10**7 is the closest to 2**26 without passing it
  307. define('MATH_BIGINTEGER_MAX10', 10000000);
  308. define('MATH_BIGINTEGER_MAX10_LEN', 7);
  309. // the largest digit that may be used in addition / subtraction
  310. // we do pow(2, 52) instead of using 4503599627370496 directly because some
  311. // PHP installations will truncate 4503599627370496.
  312. define('MATH_BIGINTEGER_MAX_DIGIT2', pow(2, 52));
  313. }
  314. }
  315. switch ( MATH_BIGINTEGER_MODE ) {
  316. case MATH_BIGINTEGER_MODE_GMP:
  317. if (is_resource($x) && get_resource_type($x) == 'GMP integer') {
  318. $this->value = $x;
  319. return;
  320. }
  321. $this->value = gmp_init(0);
  322. break;
  323. case MATH_BIGINTEGER_MODE_BCMATH:
  324. $this->value = '0';
  325. break;
  326. default:
  327. $this->value = array();
  328. }
  329. // '0' counts as empty() but when the base is 256 '0' is equal to ord('0') or 48
  330. // '0' is the only value like this per http://php.net/empty
  331. if (empty($x) && (abs($base) != 256 || $x !== '0')) {
  332. return;
  333. }
  334. switch ($base) {
  335. case -256:
  336. if (ord($x[0]) & 0x80) {
  337. $x = ~$x;
  338. $this->is_negative = true;
  339. }
  340. case 256:
  341. switch ( MATH_BIGINTEGER_MODE ) {
  342. case MATH_BIGINTEGER_MODE_GMP:
  343. $sign = $this->is_negative ? '-' : '';
  344. $this->value = gmp_init($sign . '0x' . bin2hex($x));
  345. break;
  346. case MATH_BIGINTEGER_MODE_BCMATH:
  347. // round $len to the nearest 4 (thanks, DavidMJ!)
  348. $len = (strlen($x) + 3) & 0xFFFFFFFC;
  349. $x = str_pad($x, $len, chr(0), STR_PAD_LEFT);
  350. for ($i = 0; $i < $len; $i+= 4) {
  351. $this->value = bcmul($this->value, '4294967296', 0); // 4294967296 == 2**32
  352. $this->value = bcadd($this->value, 0x1000000 * ord($x[$i]) + ((ord($x[$i + 1]) << 16) | (ord($x[$i + 2]) << 8) | ord($x[$i + 3])), 0);
  353. }
  354. if ($this->is_negative) {
  355. $this->value = '-' . $this->value;
  356. }
  357. break;
  358. // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb)
  359. default:
  360. while (strlen($x)) {
  361. $this->value[] = $this->_bytes2int($this->_base256_rshift($x, MATH_BIGINTEGER_BASE));
  362. }
  363. }
  364. if ($this->is_negative) {
  365. if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) {
  366. $this->is_negative = false;
  367. }
  368. $temp = $this->add(new Math_BigInteger('-1'));
  369. $this->value = $temp->value;
  370. }
  371. break;
  372. case 16:
  373. case -16:
  374. if ($base > 0 && $x[0] == '-') {
  375. $this->is_negative = true;
  376. $x = substr($x, 1);
  377. }
  378. $x = preg_replace('#^(?:0x)?([A-Fa-f0-9]*).*#', '$1', $x);
  379. $is_negative = false;
  380. if ($base < 0 && hexdec($x[0]) >= 8) {
  381. $this->is_negative = $is_negative = true;
  382. $x = bin2hex(~pack('H*', $x));
  383. }
  384. switch ( MATH_BIGINTEGER_MODE ) {
  385. case MATH_BIGINTEGER_MODE_GMP:
  386. $temp = $this->is_negative ? '-0x' . $x : '0x' . $x;
  387. $this->value = gmp_init($temp);
  388. $this->is_negative = false;
  389. break;
  390. case MATH_BIGINTEGER_MODE_BCMATH:
  391. $x = ( strlen($x) & 1 ) ? '0' . $x : $x;
  392. $temp = new Math_BigInteger(pack('H*', $x), 256);
  393. $this->value = $this->is_negative ? '-' . $temp->value : $temp->value;
  394. $this->is_negative = false;
  395. break;
  396. default:
  397. $x = ( strlen($x) & 1 ) ? '0' . $x : $x;
  398. $temp = new Math_BigInteger(pack('H*', $x), 256);
  399. $this->value = $temp->value;
  400. }
  401. if ($is_negative) {
  402. $temp = $this->add(new Math_BigInteger('-1'));
  403. $this->value = $temp->value;
  404. }
  405. break;
  406. case 10:
  407. case -10:
  408. // (?<!^)(?:-).*: find any -'s that aren't at the beginning and then any characters that follow that
  409. // (?<=^|-)0*: find any 0's that are preceded by the start of the string or by a - (ie. octals)
  410. // [^-0-9].*: find any non-numeric characters and then any characters that follow that
  411. $x = preg_replace('#(?<!^)(?:-).*|(?<=^|-)0*|[^-0-9].*#', '', $x);
  412. switch ( MATH_BIGINTEGER_MODE ) {
  413. case MATH_BIGINTEGER_MODE_GMP:
  414. $this->value = gmp_init($x);
  415. break;
  416. case MATH_BIGINTEGER_MODE_BCMATH:
  417. // explicitly casting $x to a string is necessary, here, since doing $x[0] on -1 yields different
  418. // results then doing it on '-1' does (modInverse does $x[0])
  419. $this->value = $x === '-' ? '0' : (string) $x;
  420. break;
  421. default:
  422. $temp = new Math_BigInteger();
  423. $multiplier = new Math_BigInteger();
  424. $multiplier->value = array(MATH_BIGINTEGER_MAX10);
  425. if ($x[0] == '-') {
  426. $this->is_negative = true;
  427. $x = substr($x, 1);
  428. }
  429. $x = str_pad($x, strlen($x) + ((MATH_BIGINTEGER_MAX10_LEN - 1) * strlen($x)) % MATH_BIGINTEGER_MAX10_LEN, 0, STR_PAD_LEFT);
  430. while (strlen($x)) {
  431. $temp = $temp->multiply($multiplier);
  432. $temp = $temp->add(new Math_BigInteger($this->_int2bytes(substr($x, 0, MATH_BIGINTEGER_MAX10_LEN)), 256));
  433. $x = substr($x, MATH_BIGINTEGER_MAX10_LEN);
  434. }
  435. $this->value = $temp->value;
  436. }
  437. break;
  438. case 2: // base-2 support originally implemented by Lluis Pamies - thanks!
  439. case -2:
  440. if ($base > 0 && $x[0] == '-') {
  441. $this->is_negative = true;
  442. $x = substr($x, 1);
  443. }
  444. $x = preg_replace('#^([01]*).*#', '$1', $x);
  445. $x = str_pad($x, strlen($x) + (3 * strlen($x)) % 4, 0, STR_PAD_LEFT);
  446. $str = '0x';
  447. while (strlen($x)) {
  448. $part = substr($x, 0, 4);
  449. $str.= dechex(bindec($part));
  450. $x = substr($x, 4);
  451. }
  452. if ($this->is_negative) {
  453. $str = '-' . $str;
  454. }
  455. $temp = new Math_BigInteger($str, 8 * $base); // ie. either -16 or +16
  456. $this->value = $temp->value;
  457. $this->is_negative = $temp->is_negative;
  458. break;
  459. default:
  460. // base not supported, so we'll let $this == 0
  461. }
  462. }
  463. /**
  464. * Converts a BigInteger to a byte string (eg. base-256).
  465. *
  466. * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're
  467. * saved as two's compliment.
  468. *
  469. * Here's an example:
  470. * <code>
  471. * <?php
  472. * include('Math/BigInteger.php');
  473. *
  474. * $a = new Math_BigInteger('65');
  475. *
  476. * echo $a->toBytes(); // outputs chr(65)
  477. * ?>
  478. * </code>
  479. *
  480. * @param Boolean $twos_compliment
  481. * @return String
  482. * @access public
  483. * @internal Converts a base-2**26 number to base-2**8
  484. */
  485. function toBytes($twos_compliment = false)
  486. {
  487. if ($twos_compliment) {
  488. $comparison = $this->compare(new Math_BigInteger());
  489. if ($comparison == 0) {
  490. return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : '';
  491. }
  492. $temp = $comparison < 0 ? $this->add(new Math_BigInteger(1)) : $this->copy();
  493. $bytes = $temp->toBytes();
  494. if (empty($bytes)) { // eg. if the number we're trying to convert is -1
  495. $bytes = chr(0);
  496. }
  497. if (ord($bytes[0]) & 0x80) {
  498. $bytes = chr(0) . $bytes;
  499. }
  500. return $comparison < 0 ? ~$bytes : $bytes;
  501. }
  502. switch ( MATH_BIGINTEGER_MODE ) {
  503. case MATH_BIGINTEGER_MODE_GMP:
  504. if (gmp_cmp($this->value, gmp_init(0)) == 0) {
  505. return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : '';
  506. }
  507. $temp = gmp_strval(gmp_abs($this->value), 16);
  508. $temp = ( strlen($temp) & 1 ) ? '0' . $temp : $temp;
  509. $temp = pack('H*', $temp);
  510. return $this->precision > 0 ?
  511. substr(str_pad($temp, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) :
  512. ltrim($temp, chr(0));
  513. case MATH_BIGINTEGER_MODE_BCMATH:
  514. if ($this->value === '0') {
  515. return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : '';
  516. }
  517. $value = '';
  518. $current = $this->value;
  519. if ($current[0] == '-') {
  520. $current = substr($current, 1);
  521. }
  522. while (bccomp($current, '0', 0) > 0) {
  523. $temp = bcmod($current, '16777216');
  524. $value = chr($temp >> 16) . chr($temp >> 8) . chr($temp) . $value;
  525. $current = bcdiv($current, '16777216', 0);
  526. }
  527. return $this->precision > 0 ?
  528. substr(str_pad($value, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) :
  529. ltrim($value, chr(0));
  530. }
  531. if (!count($this->value)) {
  532. return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : '';
  533. }
  534. $result = $this->_int2bytes($this->value[count($this->value) - 1]);
  535. $temp = $this->copy();
  536. for ($i = count($temp->value) - 2; $i >= 0; --$i) {
  537. $temp->_base256_lshift($result, MATH_BIGINTEGER_BASE);
  538. $result = $result | str_pad($temp->_int2bytes($temp->value[$i]), strlen($result), chr(0), STR_PAD_LEFT);
  539. }
  540. return $this->precision > 0 ?
  541. str_pad(substr($result, -(($this->precision + 7) >> 3)), ($this->precision + 7) >> 3, chr(0), STR_PAD_LEFT) :
  542. $result;
  543. }
  544. /**
  545. * Converts a BigInteger to a hex string (eg. base-16)).
  546. *
  547. * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're
  548. * saved as two's compliment.
  549. *
  550. * Here's an example:
  551. * <code>
  552. * <?php
  553. * include('Math/BigInteger.php');
  554. *
  555. * $a = new Math_BigInteger('65');
  556. *
  557. * echo $a->toHex(); // outputs '41'
  558. * ?>
  559. * </code>
  560. *
  561. * @param Boolean $twos_compliment
  562. * @return String
  563. * @access public
  564. * @internal Converts a base-2**26 number to base-2**8
  565. */
  566. function toHex($twos_compliment = false)
  567. {
  568. return bin2hex($this->toBytes($twos_compliment));
  569. }
  570. /**
  571. * Converts a BigInteger to a bit string (eg. base-2).
  572. *
  573. * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're
  574. * saved as two's compliment.
  575. *
  576. * Here's an example:
  577. * <code>
  578. * <?php
  579. * include('Math/BigInteger.php');
  580. *
  581. * $a = new Math_BigInteger('65');
  582. *
  583. * echo $a->toBits(); // outputs '1000001'
  584. * ?>
  585. * </code>
  586. *
  587. * @param Boolean $twos_compliment
  588. * @return String
  589. * @access public
  590. * @internal Converts a base-2**26 number to base-2**2
  591. */
  592. function toBits($twos_compliment = false)
  593. {
  594. $hex = $this->toHex($twos_compliment);
  595. $bits = '';
  596. for ($i = strlen($hex) - 8, $start = strlen($hex) & 7; $i >= $start; $i-=8) {
  597. $bits = str_pad(decbin(hexdec(substr($hex, $i, 8))), 32, '0', STR_PAD_LEFT) . $bits;
  598. }
  599. if ($start) { // hexdec('') == 0
  600. $bits = str_pad(decbin(hexdec(substr($hex, 0, $start))), 8, '0', STR_PAD_LEFT) . $bits;
  601. }
  602. $result = $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0');
  603. if ($twos_compliment && $this->compare(new Math_BigInteger()) > 0 && $this->precision <= 0) {
  604. return '0' . $result;
  605. }
  606. return $result;
  607. }
  608. /**
  609. * Converts a BigInteger to a base-10 number.
  610. *
  611. * Here's an example:
  612. * <code>
  613. * <?php
  614. * include('Math/BigInteger.php');
  615. *
  616. * $a = new Math_BigInteger('50');
  617. *
  618. * echo $a->toString(); // outputs 50
  619. * ?>
  620. * </code>
  621. *
  622. * @return String
  623. * @access public
  624. * @internal Converts a base-2**26 number to base-10**7 (which is pretty much base-10)
  625. */
  626. function toString()
  627. {
  628. switch ( MATH_BIGINTEGER_MODE ) {
  629. case MATH_BIGINTEGER_MODE_GMP:
  630. return gmp_strval($this->value);
  631. case MATH_BIGINTEGER_MODE_BCMATH:
  632. if ($this->value === '0') {
  633. return '0';
  634. }
  635. return ltrim($this->value, '0');
  636. }
  637. if (!count($this->value)) {
  638. return '0';
  639. }
  640. $temp = $this->copy();
  641. $temp->is_negative = false;
  642. $divisor = new Math_BigInteger();
  643. $divisor->value = array(MATH_BIGINTEGER_MAX10);
  644. $result = '';
  645. while (count($temp->value)) {
  646. list($temp, $mod) = $temp->divide($divisor);
  647. $result = str_pad(isset($mod->value[0]) ? $mod->value[0] : '', MATH_BIGINTEGER_MAX10_LEN, '0', STR_PAD_LEFT) . $result;
  648. }
  649. $result = ltrim($result, '0');
  650. if (empty($result)) {
  651. $result = '0';
  652. }
  653. if ($this->is_negative) {
  654. $result = '-' . $result;
  655. }
  656. return $result;
  657. }
  658. /**
  659. * Copy an object
  660. *
  661. * PHP5 passes objects by reference while PHP4 passes by value. As such, we need a function to guarantee
  662. * that all objects are passed by value, when appropriate. More information can be found here:
  663. *
  664. * {@link http://php.net/language.oop5.basic#51624}
  665. *
  666. * @access public
  667. * @see __clone()
  668. * @return Math_BigInteger
  669. */
  670. function copy()
  671. {
  672. $temp = new Math_BigInteger();
  673. $temp->value = $this->value;
  674. $temp->is_negative = $this->is_negative;
  675. $temp->generator = $this->generator;
  676. $temp->precision = $this->precision;
  677. $temp->bitmask = $this->bitmask;
  678. return $temp;
  679. }
  680. /**
  681. * __toString() magic method
  682. *
  683. * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call
  684. * toString().
  685. *
  686. * @access public
  687. * @internal Implemented per a suggestion by Techie-Michael - thanks!
  688. */
  689. function __toString()
  690. {
  691. return $this->toString();
  692. }
  693. /**
  694. * __clone() magic method
  695. *
  696. * Although you can call Math_BigInteger::__toString() directly in PHP5, you cannot call Math_BigInteger::__clone()
  697. * directly in PHP5. You can in PHP4 since it's not a magic method, but in PHP5, you have to call it by using the PHP5
  698. * only syntax of $y = clone $x. As such, if you're trying to write an application that works on both PHP4 and PHP5,
  699. * call Math_BigInteger::copy(), instead.
  700. *
  701. * @access public
  702. * @see copy()
  703. * @return Math_BigInteger
  704. */
  705. function __clone()
  706. {
  707. return $this->copy();
  708. }
  709. /**
  710. * __sleep() magic method
  711. *
  712. * Will be called, automatically, when serialize() is called on a Math_BigInteger object.
  713. *
  714. * @see __wakeup()
  715. * @access public
  716. */
  717. function __sleep()
  718. {
  719. $this->hex = $this->toHex(true);
  720. $vars = array('hex');
  721. if ($this->generator != 'mt_rand') {
  722. $vars[] = 'generator';
  723. }
  724. if ($this->precision > 0) {
  725. $vars[] = 'precision';
  726. }
  727. return $vars;
  728. }
  729. /**
  730. * __wakeup() magic method
  731. *
  732. * Will be called, automatically, when unserialize() is called on a Math_BigInteger object.
  733. *
  734. * @see __sleep()
  735. * @access public
  736. */
  737. function __wakeup()
  738. {
  739. $temp = new Math_BigInteger($this->hex, -16);
  740. $this->value = $temp->value;
  741. $this->is_negative = $temp->is_negative;
  742. $this->setRandomGenerator($this->generator);
  743. if ($this->precision > 0) {
  744. // recalculate $this->bitmask
  745. $this->setPrecision($this->precision);
  746. }
  747. }
  748. /**
  749. * Adds two BigIntegers.
  750. *
  751. * Here's an example:
  752. * <code>
  753. * <?php
  754. * include('Math/BigInteger.php');
  755. *
  756. * $a = new Math_BigInteger('10');
  757. * $b = new Math_BigInteger('20');
  758. *
  759. * $c = $a->add($b);
  760. *
  761. * echo $c->toString(); // outputs 30
  762. * ?>
  763. * </code>
  764. *
  765. * @param Math_BigInteger $y
  766. * @return Math_BigInteger
  767. * @access public
  768. * @internal Performs base-2**52 addition
  769. */
  770. function add($y)
  771. {
  772. switch ( MATH_BIGINTEGER_MODE ) {
  773. case MATH_BIGINTEGER_MODE_GMP:
  774. $temp = new Math_BigInteger();
  775. $temp->value = gmp_add($this->value, $y->value);
  776. return $this->_normalize($temp);
  777. case MATH_BIGINTEGER_MODE_BCMATH:
  778. $temp = new Math_BigInteger();
  779. $temp->value = bcadd($this->value, $y->value, 0);
  780. return $this->_normalize($temp);
  781. }
  782. $temp = $this->_add($this->value, $this->is_negative, $y->value, $y->is_negative);
  783. $result = new Math_BigInteger();
  784. $result->value = $temp[MATH_BIGINTEGER_VALUE];
  785. $result->is_negative = $temp[MATH_BIGINTEGER_SIGN];
  786. return $this->_normalize($result);
  787. }
  788. /**
  789. * Performs addition.
  790. *
  791. * @param Array $x_value
  792. * @param Boolean $x_negative
  793. * @param Array $y_value
  794. * @param Boolean $y_negative
  795. * @return Array
  796. * @access private
  797. */
  798. function _add($x_value, $x_negative, $y_value, $y_negative)
  799. {
  800. $x_size = count($x_value);
  801. $y_size = count($y_value);
  802. if ($x_size == 0) {
  803. return array(
  804. MATH_BIGINTEGER_VALUE => $y_value,
  805. MATH_BIGINTEGER_SIGN => $y_negative
  806. );
  807. } else if ($y_size == 0) {
  808. return array(
  809. MATH_BIGINTEGER_VALUE => $x_value,
  810. MATH_BIGINTEGER_SIGN => $x_negative
  811. );
  812. }
  813. // subtract, if appropriate
  814. if ( $x_negative != $y_negative ) {
  815. if ( $x_value == $y_value ) {
  816. return array(
  817. MATH_BIGINTEGER_VALUE => array(),
  818. MATH_BIGINTEGER_SIGN => false
  819. );
  820. }
  821. $temp = $this->_subtract($x_value, false, $y_value, false);
  822. $temp[MATH_BIGINTEGER_SIGN] = $this->_compare($x_value, false, $y_value, false) > 0 ?
  823. $x_negative : $y_negative;
  824. return $temp;
  825. }
  826. if ($x_size < $y_size) {
  827. $size = $x_size;
  828. $value = $y_value;
  829. } else {
  830. $size = $y_size;
  831. $value = $x_value;
  832. }
  833. $value[] = 0; // just in case the carry adds an extra digit
  834. $carry = 0;
  835. for ($i = 0, $j = 1; $j < $size; $i+=2, $j+=2) {
  836. $sum = $x_value[$j] * MATH_BIGINTEGER_BASE_FULL + $x_value[$i] + $y_value[$j] * MATH_BIGINTEGER_BASE_FULL + $y_value[$i] + $carry;
  837. $carry = $sum >= MATH_BIGINTEGER_MAX_DIGIT2; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1
  838. $sum = $carry ? $sum - MATH_BIGINTEGER_MAX_DIGIT2 : $sum;
  839. $temp = (int) ($sum / MATH_BIGINTEGER_BASE_FULL);
  840. $value[$i] = (int) ($sum - MATH_BIGINTEGER_BASE_FULL * $temp); // eg. a faster alternative to fmod($sum, 0x4000000)
  841. $value[$j] = $temp;
  842. }
  843. if ($j == $size) { // ie. if $y_size is odd
  844. $sum = $x_value[$i] + $y_value[$i] + $carry;
  845. $carry = $sum >= MATH_BIGINTEGER_BASE_FULL;
  846. $value[$i] = $carry ? $sum - MATH_BIGINTEGER_BASE_FULL : $sum;
  847. ++$i; // ie. let $i = $j since we've just done $value[$i]
  848. }
  849. if ($carry) {
  850. for (; $value[$i] == MATH_BIGINTEGER_MAX_DIGIT; ++$i) {
  851. $value[$i] = 0;
  852. }
  853. ++$value[$i];
  854. }
  855. return array(
  856. MATH_BIGINTEGER_VALUE => $this->_trim($value),
  857. MATH_BIGINTEGER_SIGN => $x_negative
  858. );
  859. }
  860. /**
  861. * Subtracts two BigIntegers.
  862. *
  863. * Here's an example:
  864. * <code>
  865. * <?php
  866. * include('Math/BigInteger.php');
  867. *
  868. * $a = new Math_BigInteger('10');
  869. * $b = new Math_BigInteger('20');
  870. *
  871. * $c = $a->subtract($b);
  872. *
  873. * echo $c->toString(); // outputs -10
  874. * ?>
  875. * </code>
  876. *
  877. * @param Math_BigInteger $y
  878. * @return Math_BigInteger
  879. * @access public
  880. * @internal Performs base-2**52 subtraction
  881. */
  882. function subtract($y)
  883. {
  884. switch ( MATH_BIGINTEGER_MODE ) {
  885. case MATH_BIGINTEGER_MODE_GMP:
  886. $temp = new Math_BigInteger();
  887. $temp->value = gmp_sub($this->value, $y->value);
  888. return $this->_normalize($temp);
  889. case MATH_BIGINTEGER_MODE_BCMATH:
  890. $temp = new Math_BigInteger();
  891. $temp->value = bcsub($this->value, $y->value, 0);
  892. return $this->_normalize($temp);
  893. }
  894. $temp = $this->_subtract($this->value, $this->is_negative, $y->value, $y->is_negative);
  895. $result = new Math_BigInteger();
  896. $result->value = $temp[MATH_BIGINTEGER_VALUE];
  897. $result->is_negative = $temp[MATH_BIGINTEGER_SIGN];
  898. return $this->_normalize($result);
  899. }
  900. /**
  901. * Performs subtraction.
  902. *
  903. * @param Array $x_value
  904. * @param Boolean $x_negative
  905. * @param Array $y_value
  906. * @param Boolean $y_negative
  907. * @return Array
  908. * @access private
  909. */
  910. function _subtract($x_value, $x_negative, $y_value, $y_negative)
  911. {
  912. $x_size = count($x_value);
  913. $y_size = count($y_value);
  914. if ($x_size == 0) {
  915. return array(
  916. MATH_BIGINTEGER_VALUE => $y_value,
  917. MATH_BIGINTEGER_SIGN => !$y_negative
  918. );
  919. } else if ($y_size == 0) {
  920. return array(
  921. MATH_BIGINTEGER_VALUE => $x_value,
  922. MATH_BIGINTEGER_SIGN => $x_negative
  923. );
  924. }
  925. // add, if appropriate (ie. -$x - +$y or +$x - -$y)
  926. if ( $x_negative != $y_negative ) {
  927. $temp = $this->_add($x_value, false, $y_value, false);
  928. $temp[MATH_BIGINTEGER_SIGN] = $x_negative;
  929. return $temp;
  930. }
  931. $diff = $this->_compare($x_value, $x_negative, $y_value, $y_negative);
  932. if ( !$diff ) {
  933. return array(
  934. MATH_BIGINTEGER_VALUE => array(),
  935. MATH_BIGINTEGER_SIGN => false
  936. );
  937. }
  938. // switch $x and $y around, if appropriate.
  939. if ( (!$x_negative && $diff < 0) || ($x_negative && $diff > 0) ) {
  940. $temp = $x_value;
  941. $x_value = $y_value;
  942. $y_value = $temp;
  943. $x_negative = !$x_negative;
  944. $x_size = count($x_value);
  945. $y_size = count($y_value);
  946. }
  947. // at this point, $x_value should be at least as big as - if not bigger than - $y_value
  948. $carry = 0;
  949. for ($i = 0, $j = 1; $j < $y_size; $i+=2, $j+=2) {
  950. $sum = $x_value[$j] * MATH_BIGINTEGER_BASE_FULL + $x_value[$i] - $y_value[$j] * MATH_BIGINTEGER_BASE_FULL - $y_value[$i] - $carry;
  951. $carry = $sum < 0; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1
  952. $sum = $carry ? $sum + MATH_BIGINTEGER_MAX_DIGIT2 : $sum;
  953. $temp = (int) ($sum / MATH_BIGINTEGER_BASE_FULL);
  954. $x_value[$i] = (int) ($sum - MATH_BIGINTEGER_BASE_FULL * $temp);
  955. $x_value[$j] = $temp;
  956. }
  957. if ($j == $y_size) { // ie. if $y_size is odd
  958. $sum = $x_value[$i] - $y_value[$i] - $carry;
  959. $carry = $sum < 0;
  960. $x_value[$i] = $carry ? $sum + MATH_BIGINTEGER_BASE_FULL : $sum;
  961. ++$i;
  962. }
  963. if ($carry) {
  964. for (; !$x_value[$i]; ++$i) {
  965. $x_value[$i] = MATH_BIGINTEGER_MAX_DIGIT;
  966. }
  967. --$x_value[$i];
  968. }
  969. return array(
  970. MATH_BIGINTEGER_VALUE => $this->_trim($x_value),
  971. MATH_BIGINTEGER_SIGN => $x_negative
  972. );
  973. }
  974. /**
  975. * Multiplies two BigIntegers
  976. *
  977. * Here's an example:
  978. * <code>
  979. * <?php
  980. * include('Math/BigInteger.php');
  981. *
  982. * $a = new Math_BigInteger('10');
  983. * $b = new Math_BigInteger('20');
  984. *
  985. * $c = $a->multiply($b);
  986. *
  987. * echo $c->toString(); // outputs 200
  988. * ?>
  989. * </code>
  990. *
  991. * @param Math_BigInteger $x
  992. * @return Math_BigInteger
  993. * @access public
  994. */
  995. function multiply($x)
  996. {
  997. switch ( MATH_BIGINTEGER_MODE ) {
  998. case MATH_BIGINTEGER_MODE_GMP:
  999. $temp = new Math_BigInteger();
  1000. $temp->value = gmp_mul($this->value, $x->value);
  1001. return $this->_normalize($temp);
  1002. case MATH_BIGINTEGER_MODE_BCMATH:
  1003. $temp = new Math_BigInteger();
  1004. $temp->value = bcmul($this->value, $x->value, 0);
  1005. return $this->_normalize($temp);
  1006. }
  1007. $temp = $this->_multiply($this->value, $this->is_negative, $x->value, $x->is_negative);
  1008. $product = new Math_BigInteger();
  1009. $product->value = $temp[MATH_BIGINTEGER_VALUE];
  1010. $product->is_negative = $temp[MATH_BIGINTEGER_SIGN];
  1011. return $this->_normalize($product);
  1012. }
  1013. /**
  1014. * Performs multiplication.
  1015. *
  1016. * @param Array $x_value
  1017. * @param Boolean $x_negative
  1018. * @param Array $y_value
  1019. * @param Boolean $y_negative
  1020. * @return Array
  1021. * @access private
  1022. */
  1023. function _multiply($x_value, $x_negative, $y_value, $y_negative)
  1024. {
  1025. //if ( $x_value == $y_value ) {
  1026. // return array(
  1027. // MATH_BIGINTEGER_VALUE => $this->_square($x_value),
  1028. // MATH_BIGINTEGER_SIGN => $x_sign != $y_value
  1029. // );
  1030. //}
  1031. $x_length = count($x_value);
  1032. $y_length = count($y_value);
  1033. if ( !$x_length || !$y_length ) { // a 0 is being multiplied
  1034. return array(
  1035. MATH_BIGINTEGER_VALUE => array(),
  1036. MATH_BIGINTEGER_SIGN => false
  1037. );
  1038. }
  1039. return array(
  1040. MATH_BIGINTEGER_VALUE => min($x_length, $y_length) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ?
  1041. $this->_trim($this->_regularMultiply($x_value, $y_value)) :
  1042. $this->_trim($this->_karatsuba($x_value, $y_value)),
  1043. MATH_BIGINTEGER_SIGN => $x_negative != $y_negative
  1044. );
  1045. }
  1046. /**
  1047. * Performs long multiplication on two BigIntegers
  1048. *
  1049. * Modeled after 'multiply' in MutableBigInteger.java.
  1050. *
  1051. * @param Array $x_value
  1052. * @param Array $y_value
  1053. * @return Array
  1054. * @access private
  1055. */
  1056. function _regularMultiply($x_value, $y_value)
  1057. {
  1058. $x_length = count($x_value);
  1059. $y_length = count($y_value);
  1060. if ( !$x_length || !$y_length ) { // a 0 is being multiplied
  1061. return array();
  1062. }
  1063. if ( $x_length < $y_length ) {
  1064. $temp = $x_value;
  1065. $x_value = $y_value;
  1066. $y_value = $temp;
  1067. $x_length = count($x_value);
  1068. $y_length = count($y_value);
  1069. }
  1070. $product_value = $this->_array_repeat(0, $x_length + $y_length);
  1071. // the following for loop could be removed if the for loop following it
  1072. // (the one with nested for loops) initially set $i to 0, but
  1073. // doing so would also make the result in one set of unnecessary adds,
  1074. // since on the outermost loops first pass, $product->value[$k] is going
  1075. // to always be 0
  1076. $carry = 0;
  1077. for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0
  1078. $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0
  1079. $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL);
  1080. $product_value[$j] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1081. }
  1082. $product_value[$j] = $carry;
  1083. // the above for loop is what the previous comment was talking about. the
  1084. // following for loop is the "one with nested for loops"
  1085. for ($i = 1; $i < $y_length; ++$i) {
  1086. $carry = 0;
  1087. for ($j = 0, $k = $i; $j < $x_length; ++$j, ++$k) {
  1088. $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry;
  1089. $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL);
  1090. $product_value[$k] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1091. }
  1092. $product_value[$k] = $carry;
  1093. }
  1094. return $product_value;
  1095. }
  1096. /**
  1097. * Performs Karatsuba multiplication on two BigIntegers
  1098. *
  1099. * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and
  1100. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=120 MPM 5.2.3}.
  1101. *
  1102. * @param Array $x_value
  1103. * @param Array $y_value
  1104. * @return Array
  1105. * @access private
  1106. */
  1107. function _karatsuba($x_value, $y_value)
  1108. {
  1109. $m = min(count($x_value) >> 1, count($y_value) >> 1);
  1110. if ($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF) {
  1111. return $this->_regularMultiply($x_value, $y_value);
  1112. }
  1113. $x1 = array_slice($x_value, $m);
  1114. $x0 = array_slice($x_value, 0, $m);
  1115. $y1 = array_slice($y_value, $m);
  1116. $y0 = array_slice($y_value, 0, $m);
  1117. $z2 = $this->_karatsuba($x1, $y1);
  1118. $z0 = $this->_karatsuba($x0, $y0);
  1119. $z1 = $this->_add($x1, false, $x0, false);
  1120. $temp = $this->_add($y1, false, $y0, false);
  1121. $z1 = $this->_karatsuba($z1[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_VALUE]);
  1122. $temp = $this->_add($z2, false, $z0, false);
  1123. $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false);
  1124. $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2);
  1125. $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]);
  1126. $xy = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]);
  1127. $xy = $this->_add($xy[MATH_BIGINTEGER_VALUE], $xy[MATH_BIGINTEGER_SIGN], $z0, false);
  1128. return $xy[MATH_BIGINTEGER_VALUE];
  1129. }
  1130. /**
  1131. * Performs squaring
  1132. *
  1133. * @param Array $x
  1134. * @return Array
  1135. * @access private
  1136. */
  1137. function _square($x = false)
  1138. {
  1139. return count($x) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ?
  1140. $this->_trim($this->_baseSquare($x)) :
  1141. $this->_trim($this->_karatsubaSquare($x));
  1142. }
  1143. /**
  1144. * Performs traditional squaring on two BigIntegers
  1145. *
  1146. * Squaring can be done faster than multiplying a number by itself can be. See
  1147. * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=7 HAC 14.2.4} /
  1148. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=141 MPM 5.3} for more information.
  1149. *
  1150. * @param Array $value
  1151. * @return Array
  1152. * @access private
  1153. */
  1154. function _baseSquare($value)
  1155. {
  1156. if ( empty($value) ) {
  1157. return array();
  1158. }
  1159. $square_value = $this->_array_repeat(0, 2 * count($value));
  1160. for ($i = 0, $max_index = count($value) - 1; $i <= $max_index; ++$i) {
  1161. $i2 = $i << 1;
  1162. $temp = $square_value[$i2] + $value[$i] * $value[$i];
  1163. $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL);
  1164. $square_value[$i2] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1165. // note how we start from $i+1 instead of 0 as we do in multiplication.
  1166. for ($j = $i + 1, $k = $i2 + 1; $j <= $max_index; ++$j, ++$k) {
  1167. $temp = $square_value[$k] + 2 * $value[$j] * $value[$i] + $carry;
  1168. $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL);
  1169. $square_value[$k] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1170. }
  1171. // the following line can yield values larger 2**15. at this point, PHP should switch
  1172. // over to floats.
  1173. $square_value[$i + $max_index + 1] = $carry;
  1174. }
  1175. return $square_value;
  1176. }
  1177. /**
  1178. * Performs Karatsuba "squaring" on two BigIntegers
  1179. *
  1180. * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and
  1181. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=151 MPM 5.3.4}.
  1182. *
  1183. * @param Array $value
  1184. * @return Array
  1185. * @access private
  1186. */
  1187. function _karatsubaSquare($value)
  1188. {
  1189. $m = count($value) >> 1;
  1190. if ($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF) {
  1191. return $this->_baseSquare($value);
  1192. }
  1193. $x1 = array_slice($value, $m);
  1194. $x0 = array_slice($value, 0, $m);
  1195. $z2 = $this->_karatsubaSquare($x1);
  1196. $z0 = $this->_karatsubaSquare($x0);
  1197. $z1 = $this->_add($x1, false, $x0, false);
  1198. $z1 = $this->_karatsubaSquare($z1[MATH_BIGINTEGER_VALUE]);
  1199. $temp = $this->_add($z2, false, $z0, false);
  1200. $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false);
  1201. $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2);
  1202. $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]);
  1203. $xx = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]);
  1204. $xx = $this->_add($xx[MATH_BIGINTEGER_VALUE], $xx[MATH_BIGINTEGER_SIGN], $z0, false);
  1205. return $xx[MATH_BIGINTEGER_VALUE];
  1206. }
  1207. /**
  1208. * Divides two BigIntegers.
  1209. *
  1210. * Returns an array whose first element contains the quotient and whose second element contains the
  1211. * "common residue". If the remainder would be positive, the "common residue" and the remainder are the
  1212. * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder
  1213. * and the divisor (basically, the "common residue" is the first positive modulo).
  1214. *
  1215. * Here's an example:
  1216. * <code>
  1217. * <?php
  1218. * include('Math/BigInteger.php');
  1219. *
  1220. * $a = new Math_BigInteger('10');
  1221. * $b = new Math_BigInteger('20');
  1222. *
  1223. * list($quotient, $remainder) = $a->divide($b);
  1224. *
  1225. * echo $quotient->toString(); // outputs 0
  1226. * echo "\r\n";
  1227. * echo $remainder->toString(); // outputs 10
  1228. * ?>
  1229. * </code>
  1230. *
  1231. * @param Math_BigInteger $y
  1232. * @return Array
  1233. * @access public
  1234. * @internal This function is based off of {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=9 HAC 14.20}.
  1235. */
  1236. function divide($y)
  1237. {
  1238. switch ( MATH_BIGINTEGER_MODE ) {
  1239. case MATH_BIGINTEGER_MODE_GMP:
  1240. $quotient = new Math_BigInteger();
  1241. $remainder = new Math_BigInteger();
  1242. list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value);
  1243. if (gmp_sign($remainder->value) < 0) {
  1244. $remainder->value = gmp_add($remainder->value, gmp_abs($y->value));
  1245. }
  1246. return array($this->_normalize($quotient), $this->_normalize($remainder));
  1247. case MATH_BIGINTEGER_MODE_BCMATH:
  1248. $quotient = new Math_BigInteger();
  1249. $remainder = new Math_BigInteger();
  1250. $quotient->value = bcdiv($this->value, $y->value, 0);
  1251. $remainder->value = bcmod($this->value, $y->value);
  1252. if ($remainder->value[0] == '-') {
  1253. $remainder->value = bcadd($remainder->value, $y->value[0] == '-' ? substr($y->value, 1) : $y->value, 0);
  1254. }
  1255. return array($this->_normalize($quotient), $this->_normalize($remainder));
  1256. }
  1257. if (count($y->value) == 1) {
  1258. list($q, $r) = $this->_divide_digit($this->value, $y->value[0]);
  1259. $quotient = new Math_BigInteger();
  1260. $remainder = new Math_BigInteger();
  1261. $quotient->value = $q;
  1262. $remainder->value = array($r);
  1263. $quotient->is_negative = $this->is_negative != $y->is_negative;
  1264. return array($this->_normalize($quotient), $this->_normalize($remainder));
  1265. }
  1266. static $zero;
  1267. if ( !isset($zero) ) {
  1268. $zero = new Math_BigInteger();
  1269. }
  1270. $x = $this->copy();
  1271. $y = $y->copy();
  1272. $x_sign = $x->is_negative;
  1273. $y_sign = $y->is_negative;
  1274. $x->is_negative = $y->is_negative = false;
  1275. $diff = $x->compare($y);
  1276. if ( !$diff ) {
  1277. $temp = new Math_BigInteger();
  1278. $temp->value = array(1);
  1279. $temp->is_negative = $x_sign != $y_sign;
  1280. return array($this->_normalize($temp), $this->_normalize(new Math_BigInteger()));
  1281. }
  1282. if ( $diff < 0 ) {
  1283. // if $x is negative, "add" $y.
  1284. if ( $x_sign ) {
  1285. $x = $y->subtract($x);
  1286. }
  1287. return array($this->_normalize(new Math_BigInteger()), $this->_normalize($x));
  1288. }
  1289. // normalize $x and $y as described in HAC 14.23 / 14.24
  1290. $msb = $y->value[count($y->value) - 1];
  1291. for ($shift = 0; !($msb & MATH_BIGINTEGER_MSB); ++$shift) {
  1292. $msb <<= 1;
  1293. }
  1294. $x->_lshift($shift);
  1295. $y->_lshift($shift);
  1296. $y_value = &$y->value;
  1297. $x_max = count($x->value) - 1;
  1298. $y_max = count($y->value) - 1;
  1299. $quotient = new Math_BigInteger();
  1300. $quotient_value = &$quotient->value;
  1301. $quotient_value = $this->_array_repeat(0, $x_max - $y_max + 1);
  1302. static $temp, $lhs, $rhs;
  1303. if (!isset($temp)) {
  1304. $temp = new Math_BigInteger();
  1305. $lhs = new Math_BigInteger();
  1306. $rhs = new Math_BigInteger();
  1307. }
  1308. $temp_value = &$temp->value;
  1309. $rhs_value = &$rhs->value;
  1310. // $temp = $y << ($x_max - $y_max-1) in base 2**26
  1311. $temp_value = array_merge($this->_array_repeat(0, $x_max - $y_max), $y_value);
  1312. while ( $x->compare($temp) >= 0 ) {
  1313. // calculate the "common residue"
  1314. ++$quotient_value[$x_max - $y_max];
  1315. $x = $x->subtract($temp);
  1316. $x_max = count($x->value) - 1;
  1317. }
  1318. for ($i = $x_max; $i >= $y_max + 1; --$i) {
  1319. $x_value = &$x->value;
  1320. $x_window = array(
  1321. isset($x_value[$i]) ? $x_value[$i] : 0,
  1322. isset($x_value[$i - 1]) ? $x_value[$i - 1] : 0,
  1323. isset($x_value[$i - 2]) ? $x_value[$i - 2] : 0
  1324. );
  1325. $y_window = array(
  1326. $y_value[$y_max],
  1327. ( $y_max > 0 ) ? $y_value[$y_max - 1] : 0
  1328. );
  1329. $q_index = $i - $y_max - 1;
  1330. if ($x_window[0] == $y_window[0]) {
  1331. $quotient_value[$q_index] = MATH_BIGINTEGER_MAX_DIGIT;
  1332. } else {
  1333. $quotient_value[$q_index] = (int) (
  1334. ($x_window[0] * MATH_BIGINTEGER_BASE_FULL + $x_window[1])
  1335. /
  1336. $y_window[0]
  1337. );
  1338. }
  1339. $temp_value = array($y_window[1], $y_window[0]);
  1340. $lhs->value = array($quotient_value[$q_index]);
  1341. $lhs = $lhs->multiply($temp);
  1342. $rhs_value = array($x_window[2], $x_window[1], $x_window[0]);
  1343. while ( $lhs->compare($rhs) > 0 ) {
  1344. --$quotient_value[$q_index];
  1345. $lhs->value = array($quotient_value[$q_index]);
  1346. $lhs = $lhs->multiply($temp);
  1347. }
  1348. $adjust = $this->_array_repeat(0, $q_index);
  1349. $temp_value = array($quotient_value[$q_index]);
  1350. $temp = $temp->multiply($y);
  1351. $temp_value = &$temp->value;
  1352. $temp_value = array_merge($adjust, $temp_value);
  1353. $x = $x->subtract($temp);
  1354. if ($x->compare($zero) < 0) {
  1355. $temp_value = array_merge($adjust, $y_value);
  1356. $x = $x->add($temp);
  1357. --$quotient_value[$q_index];
  1358. }
  1359. $x_max = count($x_value) - 1;
  1360. }
  1361. // unnormalize the remainder
  1362. $x->_rshift($shift);
  1363. $quotient->is_negative = $x_sign != $y_sign;
  1364. // calculate the "common residue", if appropriate
  1365. if ( $x_sign ) {
  1366. $y->_rshift($shift);
  1367. $x = $y->subtract($x);
  1368. }
  1369. return array($this->_normalize($quotient), $this->_normalize($x));
  1370. }
  1371. /**
  1372. * Divides a BigInteger by a regular integer
  1373. *
  1374. * abc / x = a00 / x + b0 / x + c / x
  1375. *
  1376. * @param Array $dividend
  1377. * @param Array $divisor
  1378. * @return Array
  1379. * @access private
  1380. */
  1381. function _divide_digit($dividend, $divisor)
  1382. {
  1383. $carry = 0;
  1384. $result = array();
  1385. for ($i = count($dividend) - 1; $i >= 0; --$i) {
  1386. $temp = MATH_BIGINTEGER_BASE_FULL * $carry + $dividend[$i];
  1387. $result[$i] = (int) ($temp / $divisor);
  1388. $carry = (int) ($temp - $divisor * $result[$i]);
  1389. }
  1390. return array($result, $carry);
  1391. }
  1392. /**
  1393. * Performs modular exponentiation.
  1394. *
  1395. * Here's an example:
  1396. * <code>
  1397. * <?php
  1398. * include('Math/BigInteger.php');
  1399. *
  1400. * $a = new Math_BigInteger('10');
  1401. * $b = new Math_BigInteger('20');
  1402. * $c = new Math_BigInteger('30');
  1403. *
  1404. * $c = $a->modPow($b, $c);
  1405. *
  1406. * echo $c->toString(); // outputs 10
  1407. * ?>
  1408. * </code>
  1409. *
  1410. * @param Math_BigInteger $e
  1411. * @param Math_BigInteger $n
  1412. * @return Math_BigInteger
  1413. * @access public
  1414. * @internal The most naive approach to modular exponentiation has very unreasonable requirements, and
  1415. * and although the approach involving repeated squaring does vastly better, it, too, is impractical
  1416. * for our purposes. The reason being that division - by far the most complicated and time-consuming
  1417. * of the basic operations (eg. +,-,*,/) - occurs multiple times within it.
  1418. *
  1419. * Modular reductions resolve this issue. Although an individual modular reduction takes more time
  1420. * then an individual division, when performed in succession (with the same modulo), they're a lot faster.
  1421. *
  1422. * The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction,
  1423. * although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the
  1424. * base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because
  1425. * the product of two odd numbers is odd), but what about when RSA isn't used?
  1426. *
  1427. * In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a
  1428. * Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the
  1429. * modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however,
  1430. * uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and
  1431. * the other, a power of two - and recombine them, later. This is the method that this modPow function uses.
  1432. * {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates.
  1433. */
  1434. function modPow($e, $n)
  1435. {
  1436. $n = $this->bitmask !== false && $this->bitmask->compare($n) < 0 ? $this->bitmask : $n->abs();
  1437. if ($e->compare(new Math_BigInteger()) < 0) {
  1438. $e = $e->abs();
  1439. $temp = $this->modInverse($n);
  1440. if ($temp === false) {
  1441. return false;
  1442. }
  1443. return $this->_normalize($temp->modPow($e, $n));
  1444. }
  1445. if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP ) {
  1446. $temp = new Math_BigInteger();
  1447. $temp->value = gmp_powm($this->value, $e->value, $n->value);
  1448. return $this->_normalize($temp);
  1449. }
  1450. if ($this->compare(new Math_BigInteger()) < 0 || $this->compare($n) > 0) {
  1451. list(, $temp) = $this->divide($n);
  1452. return $temp->modPow($e, $n);
  1453. }
  1454. if (defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) {
  1455. $components = array(
  1456. 'modulus' => $n->toBytes(true),
  1457. 'publicExponent' => $e->toBytes(true)
  1458. );
  1459. $components = array(
  1460. 'modulus' => pack('Ca*a*', 2, $this->_encodeASN1Length(strlen($components['modulus'])), $components['modulus']),
  1461. 'publicExponent' => pack('Ca*a*', 2, $this->_encodeASN1Length(strlen($components['publicExponent'])), $components['publicExponent'])
  1462. );
  1463. $RSAPublicKey = pack('Ca*a*a*',
  1464. 48, $this->_encodeASN1Length(strlen($components['modulus']) + strlen($components['publicExponent'])),
  1465. $components['modulus'], $components['publicExponent']
  1466. );
  1467. $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA
  1468. $RSAPublicKey = chr(0) . $RSAPublicKey;
  1469. $RSAPublicKey = chr(3) . $this->_encodeASN1Length(strlen($RSAPublicKey)) . $RSAPublicKey;
  1470. $encapsulated = pack('Ca*a*',
  1471. 48, $this->_encodeASN1Length(strlen($rsaOID . $RSAPublicKey)), $rsaOID . $RSAPublicKey
  1472. );
  1473. $RSAPublicKey = "-----BEGIN PUBLIC KEY-----\r\n" .
  1474. chunk_split(base64_encode($encapsulated)) .
  1475. '-----END PUBLIC KEY-----';
  1476. $plaintext = str_pad($this->toBytes(), strlen($n->toBytes(true)) - 1, "\0", STR_PAD_LEFT);
  1477. if (openssl_public_encrypt($plaintext, $result, $RSAPublicKey, OPENSSL_NO_PADDING)) {
  1478. return new Math_BigInteger($result, 256);
  1479. }
  1480. }
  1481. if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) {
  1482. $temp = new Math_BigInteger();
  1483. $temp->value = bcpowmod($this->value, $e->value, $n->value, 0);
  1484. return $this->_normalize($temp);
  1485. }
  1486. if ( empty($e->value) ) {
  1487. $temp = new Math_BigInteger();
  1488. $temp->value = array(1);
  1489. return $this->_normalize($temp);
  1490. }
  1491. if ( $e->value == array(1) ) {
  1492. list(, $temp) = $this->divide($n);
  1493. return $this->_normalize($temp);
  1494. }
  1495. if ( $e->value == array(2) ) {
  1496. $temp = new Math_BigInteger();
  1497. $temp->value = $this->_square($this->value);
  1498. list(, $temp) = $temp->divide($n);
  1499. return $this->_normalize($temp);
  1500. }
  1501. return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_BARRETT));
  1502. // is the modulo odd?
  1503. if ( $n->value[0] & 1 ) {
  1504. return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_MONTGOMERY));
  1505. }
  1506. // if it's not, it's even
  1507. // find the lowest set bit (eg. the max pow of 2 that divides $n)
  1508. for ($i = 0; $i < count($n->value); ++$i) {
  1509. if ( $n->value[$i] ) {
  1510. $temp = decbin($n->value[$i]);
  1511. $j = strlen($temp) - strrpos($temp, '1') - 1;
  1512. $j+= 26 * $i;
  1513. break;
  1514. }
  1515. }
  1516. // at this point, 2^$j * $n/(2^$j) == $n
  1517. $mod1 = $n->copy();
  1518. $mod1->_rshift($j);
  1519. $mod2 = new Math_BigInteger();
  1520. $mod2->value = array(1);
  1521. $mod2->_lshift($j);
  1522. $part1 = ( $mod1->value != array(1) ) ? $this->_slidingWindow($e, $mod1, MATH_BIGINTEGER_MONTGOMERY) : new Math_BigInteger();
  1523. $part2 = $this->_slidingWindow($e, $mod2, MATH_BIGINTEGER_POWEROF2);
  1524. $y1 = $mod2->modInverse($mod1);
  1525. $y2 = $mod1->modInverse($mod2);
  1526. $result = $part1->multiply($mod2);
  1527. $result = $result->multiply($y1);
  1528. $temp = $part2->multiply($mod1);
  1529. $temp = $temp->multiply($y2);
  1530. $result = $result->add($temp);
  1531. list(, $result) = $result->divide($n);
  1532. return $this->_normalize($result);
  1533. }
  1534. /**
  1535. * Performs modular exponentiation.
  1536. *
  1537. * Alias for Math_BigInteger::modPow()
  1538. *
  1539. * @param Math_BigInteger $e
  1540. * @param Math_BigInteger $n
  1541. * @return Math_BigInteger
  1542. * @access public
  1543. */
  1544. function powMod($e, $n)
  1545. {
  1546. return $this->modPow($e, $n);
  1547. }
  1548. /**
  1549. * Sliding Window k-ary Modular Exponentiation
  1550. *
  1551. * Based on {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=27 HAC 14.85} /
  1552. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=210 MPM 7.7}. In a departure from those algorithims,
  1553. * however, this function performs a modular reduction after every multiplication and squaring operation.
  1554. * As such, this function has the same preconditions that the reductions being used do.
  1555. *
  1556. * @param Math_BigInteger $e
  1557. * @param Math_BigInteger $n
  1558. * @param Integer $mode
  1559. * @return Math_BigInteger
  1560. * @access private
  1561. */
  1562. function _slidingWindow($e, $n, $mode)
  1563. {
  1564. static $window_ranges = array(7, 25, 81, 241, 673, 1793); // from BigInteger.java's oddModPow function
  1565. //static $window_ranges = array(0, 7, 36, 140, 450, 1303, 3529); // from MPM 7.3.1
  1566. $e_value = $e->value;
  1567. $e_length = count($e_value) - 1;
  1568. $e_bits = decbin($e_value[$e_length]);
  1569. for ($i = $e_length - 1; $i >= 0; --$i) {
  1570. $e_bits.= str_pad(decbin($e_value[$i]), MATH_BIGINTEGER_BASE, '0', STR_PAD_LEFT);
  1571. }
  1572. $e_length = strlen($e_bits);
  1573. // calculate the appropriate window size.
  1574. // $window_size == 3 if $window_ranges is between 25 and 81, for example.
  1575. for ($i = 0, $window_size = 1; $e_length > $window_ranges[$i] && $i < count($window_ranges); ++$window_size, ++$i);
  1576. $n_value = $n->value;
  1577. // precompute $this^0 through $this^$window_size
  1578. $powers = array();
  1579. $powers[1] = $this->_prepareReduce($this->value, $n_value, $mode);
  1580. $powers[2] = $this->_squareReduce($powers[1], $n_value, $mode);
  1581. // we do every other number since substr($e_bits, $i, $j+1) (see below) is supposed to end
  1582. // in a 1. ie. it's supposed to be odd.
  1583. $temp = 1 << ($window_size - 1);
  1584. for ($i = 1; $i < $temp; ++$i) {
  1585. $i2 = $i << 1;
  1586. $powers[$i2 + 1] = $this->_multiplyReduce($powers[$i2 - 1], $powers[2], $n_value, $mode);
  1587. }
  1588. $result = array(1);
  1589. $result = $this->_prepareReduce($result, $n_value, $mode);
  1590. for ($i = 0; $i < $e_length; ) {
  1591. if ( !$e_bits[$i] ) {
  1592. $result = $this->_squareReduce($result, $n_value, $mode);
  1593. ++$i;
  1594. } else {
  1595. for ($j = $window_size - 1; $j > 0; --$j) {
  1596. if ( !empty($e_bits[$i + $j]) ) {
  1597. break;
  1598. }
  1599. }
  1600. for ($k = 0; $k <= $j; ++$k) {// eg. the length of substr($e_bits, $i, $j+1)
  1601. $result = $this->_squareReduce($result, $n_value, $mode);
  1602. }
  1603. $result = $this->_multiplyReduce($result, $powers[bindec(substr($e_bits, $i, $j + 1))], $n_value, $mode);
  1604. $i+=$j + 1;
  1605. }
  1606. }
  1607. $temp = new Math_BigInteger();
  1608. $temp->value = $this->_reduce($result, $n_value, $mode);
  1609. return $temp;
  1610. }
  1611. /**
  1612. * Modular reduction
  1613. *
  1614. * For most $modes this will return the remainder.
  1615. *
  1616. * @see _slidingWindow()
  1617. * @access private
  1618. * @param Array $x
  1619. * @param Array $n
  1620. * @param Integer $mode
  1621. * @return Array
  1622. */
  1623. function _reduce($x, $n, $mode)
  1624. {
  1625. switch ($mode) {
  1626. case MATH_BIGINTEGER_MONTGOMERY:
  1627. return $this->_montgomery($x, $n);
  1628. case MATH_BIGINTEGER_BARRETT:
  1629. return $this->_barrett($x, $n);
  1630. case MATH_BIGINTEGER_POWEROF2:
  1631. $lhs = new Math_BigInteger();
  1632. $lhs->value = $x;
  1633. $rhs = new Math_BigInteger();
  1634. $rhs->value = $n;
  1635. return $x->_mod2($n);
  1636. case MATH_BIGINTEGER_CLASSIC:
  1637. $lhs = new Math_BigInteger();
  1638. $lhs->value = $x;
  1639. $rhs = new Math_BigInteger();
  1640. $rhs->value = $n;
  1641. list(, $temp) = $lhs->divide($rhs);
  1642. return $temp->value;
  1643. case MATH_BIGINTEGER_NONE:
  1644. return $x;
  1645. default:
  1646. // an invalid $mode was provided
  1647. }
  1648. }
  1649. /**
  1650. * Modular reduction preperation
  1651. *
  1652. * @see _slidingWindow()
  1653. * @access private
  1654. * @param Array $x
  1655. * @param Array $n
  1656. * @param Integer $mode
  1657. * @return Array
  1658. */
  1659. function _prepareReduce($x, $n, $mode)
  1660. {
  1661. if ($mode == MATH_BIGINTEGER_MONTGOMERY) {
  1662. return $this->_prepMontgomery($x, $n);
  1663. }
  1664. return $this->_reduce($x, $n, $mode);
  1665. }
  1666. /**
  1667. * Modular multiply
  1668. *
  1669. * @see _slidingWindow()
  1670. * @access private
  1671. * @param Array $x
  1672. * @param Array $y
  1673. * @param Array $n
  1674. * @param Integer $mode
  1675. * @return Array
  1676. */
  1677. function _multiplyReduce($x, $y, $n, $mode)
  1678. {
  1679. if ($mode == MATH_BIGINTEGER_MONTGOMERY) {
  1680. return $this->_montgomeryMultiply($x, $y, $n);
  1681. }
  1682. $temp = $this->_multiply($x, false, $y, false);
  1683. return $this->_reduce($temp[MATH_BIGINTEGER_VALUE], $n, $mode);
  1684. }
  1685. /**
  1686. * Modular square
  1687. *
  1688. * @see _slidingWindow()
  1689. * @access private
  1690. * @param Array $x
  1691. * @param Array $n
  1692. * @param Integer $mode
  1693. * @return Array
  1694. */
  1695. function _squareReduce($x, $n, $mode)
  1696. {
  1697. if ($mode == MATH_BIGINTEGER_MONTGOMERY) {
  1698. return $this->_montgomeryMultiply($x, $x, $n);
  1699. }
  1700. return $this->_reduce($this->_square($x), $n, $mode);
  1701. }
  1702. /**
  1703. * Modulos for Powers of Two
  1704. *
  1705. * Calculates $x%$n, where $n = 2**$e, for some $e. Since this is basically the same as doing $x & ($n-1),
  1706. * we'll just use this function as a wrapper for doing that.
  1707. *
  1708. * @see _slidingWindow()
  1709. * @access private
  1710. * @param Math_BigInteger
  1711. * @return Math_BigInteger
  1712. */
  1713. function _mod2($n)
  1714. {
  1715. $temp = new Math_BigInteger();
  1716. $temp->value = array(1);
  1717. return $this->bitwise_and($n->subtract($temp));
  1718. }
  1719. /**
  1720. * Barrett Modular Reduction
  1721. *
  1722. * See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=14 HAC 14.3.3} /
  1723. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=165 MPM 6.2.5} for more information. Modified slightly,
  1724. * so as not to require negative numbers (initially, this script didn't support negative numbers).
  1725. *
  1726. * Employs "folding", as described at
  1727. * {@link http://www.cosic.esat.kuleuven.be/publications/thesis-149.pdf#page=66 thesis-149.pdf#page=66}. To quote from
  1728. * it, "the idea [behind folding] is to find a value x' such that x (mod m) = x' (mod m), with x' being smaller than x."
  1729. *
  1730. * Unfortunately, the "Barrett Reduction with Folding" algorithm described in thesis-149.pdf is not, as written, all that
  1731. * usable on account of (1) its not using reasonable radix points as discussed in
  1732. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=162 MPM 6.2.2} and (2) the fact that, even with reasonable
  1733. * radix points, it only works when there are an even number of digits in the denominator. The reason for (2) is that
  1734. * (x >> 1) + (x >> 1) != x / 2 + x / 2. If x is even, they're the same, but if x is odd, they're not. See the in-line
  1735. * comments for details.
  1736. *
  1737. * @see _slidingWindow()
  1738. * @access private
  1739. * @param Array $n
  1740. * @param Array $m
  1741. * @return Array
  1742. */
  1743. function _barrett($n, $m)
  1744. {
  1745. static $cache = array(
  1746. MATH_BIGINTEGER_VARIABLE => array(),
  1747. MATH_BIGINTEGER_DATA => array()
  1748. );
  1749. $m_length = count($m);
  1750. // if ($this->_compare($n, $this->_square($m)) >= 0) {
  1751. if (count($n) > 2 * $m_length) {
  1752. $lhs = new Math_BigInteger();
  1753. $rhs = new Math_BigInteger();
  1754. $lhs->value = $n;
  1755. $rhs->value = $m;
  1756. list(, $temp) = $lhs->divide($rhs);
  1757. return $temp->value;
  1758. }
  1759. // if (m.length >> 1) + 2 <= m.length then m is too small and n can't be reduced
  1760. if ($m_length < 5) {
  1761. return $this->_regularBarrett($n, $m);
  1762. }
  1763. // n = 2 * m.length
  1764. if ( ($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) {
  1765. $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
  1766. $cache[MATH_BIGINTEGER_VARIABLE][] = $m;
  1767. $lhs = new Math_BigInteger();
  1768. $lhs_value = &$lhs->value;
  1769. $lhs_value = $this->_array_repeat(0, $m_length + ($m_length >> 1));
  1770. $lhs_value[] = 1;
  1771. $rhs = new Math_BigInteger();
  1772. $rhs->value = $m;
  1773. list($u, $m1) = $lhs->divide($rhs);
  1774. $u = $u->value;
  1775. $m1 = $m1->value;
  1776. $cache[MATH_BIGINTEGER_DATA][] = array(
  1777. 'u' => $u, // m.length >> 1 (technically (m.length >> 1) + 1)
  1778. 'm1'=> $m1 // m.length
  1779. );
  1780. } else {
  1781. extract($cache[MATH_BIGINTEGER_DATA][$key]);
  1782. }
  1783. $cutoff = $m_length + ($m_length >> 1);
  1784. $lsd = array_slice($n, 0, $cutoff); // m.length + (m.length >> 1)
  1785. $msd = array_slice($n, $cutoff); // m.length >> 1
  1786. $lsd = $this->_trim($lsd);
  1787. $temp = $this->_multiply($msd, false, $m1, false);
  1788. $n = $this->_add($lsd, false, $temp[MATH_BIGINTEGER_VALUE], false); // m.length + (m.length >> 1) + 1
  1789. if ($m_length & 1) {
  1790. return $this->_regularBarrett($n[MATH_BIGINTEGER_VALUE], $m);
  1791. }
  1792. // (m.length + (m.length >> 1) + 1) - (m.length - 1) == (m.length >> 1) + 2
  1793. $temp = array_slice($n[MATH_BIGINTEGER_VALUE], $m_length - 1);
  1794. // if even: ((m.length >> 1) + 2) + (m.length >> 1) == m.length + 2
  1795. // if odd: ((m.length >> 1) + 2) + (m.length >> 1) == (m.length - 1) + 2 == m.length + 1
  1796. $temp = $this->_multiply($temp, false, $u, false);
  1797. // if even: (m.length + 2) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) + 1
  1798. // if odd: (m.length + 1) - ((m.length >> 1) + 1) = m.length - (m.length >> 1)
  1799. $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], ($m_length >> 1) + 1);
  1800. // if even: (m.length - (m.length >> 1) + 1) + m.length = 2 * m.length - (m.length >> 1) + 1
  1801. // if odd: (m.length - (m.length >> 1)) + m.length = 2 * m.length - (m.length >> 1)
  1802. $temp = $this->_multiply($temp, false, $m, false);
  1803. // at this point, if m had an odd number of digits, we'd be subtracting a 2 * m.length - (m.length >> 1) digit
  1804. // number from a m.length + (m.length >> 1) + 1 digit number. ie. there'd be an extra digit and the while loop
  1805. // following this comment would loop a lot (hence our calling _regularBarrett() in that situation).
  1806. $result = $this->_subtract($n[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false);
  1807. while ($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false) >= 0) {
  1808. $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false);
  1809. }
  1810. return $result[MATH_BIGINTEGER_VALUE];
  1811. }
  1812. /**
  1813. * (Regular) Barrett Modular Reduction
  1814. *
  1815. * For numbers with more than four digits Math_BigInteger::_barrett() is faster. The difference between that and this
  1816. * is that this function does not fold the denominator into a smaller form.
  1817. *
  1818. * @see _slidingWindow()
  1819. * @access private
  1820. * @param Array $x
  1821. * @param Array $n
  1822. * @return Array
  1823. */
  1824. function _regularBarrett($x, $n)
  1825. {
  1826. static $cache = array(
  1827. MATH_BIGINTEGER_VARIABLE => array(),
  1828. MATH_BIGINTEGER_DATA => array()
  1829. );
  1830. $n_length = count($n);
  1831. if (count($x) > 2 * $n_length) {
  1832. $lhs = new Math_BigInteger();
  1833. $rhs = new Math_BigInteger();
  1834. $lhs->value = $x;
  1835. $rhs->value = $n;
  1836. list(, $temp) = $lhs->divide($rhs);
  1837. return $temp->value;
  1838. }
  1839. if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) {
  1840. $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
  1841. $cache[MATH_BIGINTEGER_VARIABLE][] = $n;
  1842. $lhs = new Math_BigInteger();
  1843. $lhs_value = &$lhs->value;
  1844. $lhs_value = $this->_array_repeat(0, 2 * $n_length);
  1845. $lhs_value[] = 1;
  1846. $rhs = new Math_BigInteger();
  1847. $rhs->value = $n;
  1848. list($temp, ) = $lhs->divide($rhs); // m.length
  1849. $cache[MATH_BIGINTEGER_DATA][] = $temp->value;
  1850. }
  1851. // 2 * m.length - (m.length - 1) = m.length + 1
  1852. $temp = array_slice($x, $n_length - 1);
  1853. // (m.length + 1) + m.length = 2 * m.length + 1
  1854. $temp = $this->_multiply($temp, false, $cache[MATH_BIGINTEGER_DATA][$key], false);
  1855. // (2 * m.length + 1) - (m.length - 1) = m.length + 2
  1856. $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], $n_length + 1);
  1857. // m.length + 1
  1858. $result = array_slice($x, 0, $n_length + 1);
  1859. // m.length + 1
  1860. $temp = $this->_multiplyLower($temp, false, $n, false, $n_length + 1);
  1861. // $temp == array_slice($temp->_multiply($temp, false, $n, false)->value, 0, $n_length + 1)
  1862. if ($this->_compare($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]) < 0) {
  1863. $corrector_value = $this->_array_repeat(0, $n_length + 1);
  1864. $corrector_value[] = 1;
  1865. $result = $this->_add($result, false, $corrector_value, false);
  1866. $result = $result[MATH_BIGINTEGER_VALUE];
  1867. }
  1868. // at this point, we're subtracting a number with m.length + 1 digits from another number with m.length + 1 digits
  1869. $result = $this->_subtract($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]);
  1870. while ($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false) > 0) {
  1871. $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false);
  1872. }
  1873. return $result[MATH_BIGINTEGER_VALUE];
  1874. }
  1875. /**
  1876. * Performs long multiplication up to $stop digits
  1877. *
  1878. * If you're going to be doing array_slice($product->value, 0, $stop), some cycles can be saved.
  1879. *
  1880. * @see _regularBarrett()
  1881. * @param Array $x_value
  1882. * @param Boolean $x_negative
  1883. * @param Array $y_value
  1884. * @param Boolean $y_negative
  1885. * @param Integer $stop
  1886. * @return Array
  1887. * @access private
  1888. */
  1889. function _multiplyLower($x_value, $x_negative, $y_value, $y_negative, $stop)
  1890. {
  1891. $x_length = count($x_value);
  1892. $y_length = count($y_value);
  1893. if ( !$x_length || !$y_length ) { // a 0 is being multiplied
  1894. return array(
  1895. MATH_BIGINTEGER_VALUE => array(),
  1896. MATH_BIGINTEGER_SIGN => false
  1897. );
  1898. }
  1899. if ( $x_length < $y_length ) {
  1900. $temp = $x_value;
  1901. $x_value = $y_value;
  1902. $y_value = $temp;
  1903. $x_length = count($x_value);
  1904. $y_length = count($y_value);
  1905. }
  1906. $product_value = $this->_array_repeat(0, $x_length + $y_length);
  1907. // the following for loop could be removed if the for loop following it
  1908. // (the one with nested for loops) initially set $i to 0, but
  1909. // doing so would also make the result in one set of unnecessary adds,
  1910. // since on the outermost loops first pass, $product->value[$k] is going
  1911. // to always be 0
  1912. $carry = 0;
  1913. for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0, $k = $i
  1914. $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0
  1915. $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL);
  1916. $product_value[$j] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1917. }
  1918. if ($j < $stop) {
  1919. $product_value[$j] = $carry;
  1920. }
  1921. // the above for loop is what the previous comment was talking about. the
  1922. // following for loop is the "one with nested for loops"
  1923. for ($i = 1; $i < $y_length; ++$i) {
  1924. $carry = 0;
  1925. for ($j = 0, $k = $i; $j < $x_length && $k < $stop; ++$j, ++$k) {
  1926. $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry;
  1927. $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL);
  1928. $product_value[$k] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1929. }
  1930. if ($k < $stop) {
  1931. $product_value[$k] = $carry;
  1932. }
  1933. }
  1934. return array(
  1935. MATH_BIGINTEGER_VALUE => $this->_trim($product_value),
  1936. MATH_BIGINTEGER_SIGN => $x_negative != $y_negative
  1937. );
  1938. }
  1939. /**
  1940. * Montgomery Modular Reduction
  1941. *
  1942. * ($x->_prepMontgomery($n))->_montgomery($n) yields $x % $n.
  1943. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=170 MPM 6.3} provides insights on how this can be
  1944. * improved upon (basically, by using the comba method). gcd($n, 2) must be equal to one for this function
  1945. * to work correctly.
  1946. *
  1947. * @see _prepMontgomery()
  1948. * @see _slidingWindow()
  1949. * @access private
  1950. * @param Array $x
  1951. * @param Array $n
  1952. * @return Array
  1953. */
  1954. function _montgomery($x, $n)
  1955. {
  1956. static $cache = array(
  1957. MATH_BIGINTEGER_VARIABLE => array(),
  1958. MATH_BIGINTEGER_DATA => array()
  1959. );
  1960. if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) {
  1961. $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
  1962. $cache[MATH_BIGINTEGER_VARIABLE][] = $x;
  1963. $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($n);
  1964. }
  1965. $k = count($n);
  1966. $result = array(MATH_BIGINTEGER_VALUE => $x);
  1967. for ($i = 0; $i < $k; ++$i) {
  1968. $temp = $result[MATH_BIGINTEGER_VALUE][$i] * $cache[MATH_BIGINTEGER_DATA][$key];
  1969. $temp = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * ((int) ($temp / MATH_BIGINTEGER_BASE_FULL)));
  1970. $temp = $this->_regularMultiply(array($temp), $n);
  1971. $temp = array_merge($this->_array_repeat(0, $i), $temp);
  1972. $result = $this->_add($result[MATH_BIGINTEGER_VALUE], false, $temp, false);
  1973. }
  1974. $result[MATH_BIGINTEGER_VALUE] = array_slice($result[MATH_BIGINTEGER_VALUE], $k);
  1975. if ($this->_compare($result, false, $n, false) >= 0) {
  1976. $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], false, $n, false);
  1977. }
  1978. return $result[MATH_BIGINTEGER_VALUE];
  1979. }
  1980. /**
  1981. * Montgomery Multiply
  1982. *
  1983. * Interleaves the montgomery reduction and long multiplication algorithms together as described in
  1984. * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=13 HAC 14.36}
  1985. *
  1986. * @see _prepMontgomery()
  1987. * @see _montgomery()
  1988. * @access private
  1989. * @param Array $x
  1990. * @param Array $y
  1991. * @param Array $m
  1992. * @return Array
  1993. */
  1994. function _montgomeryMultiply($x, $y, $m)
  1995. {
  1996. $temp = $this->_multiply($x, false, $y, false);
  1997. return $this->_montgomery($temp[MATH_BIGINTEGER_VALUE], $m);
  1998. static $cache = array(
  1999. MATH_BIGINTEGER_VARIABLE => array(),
  2000. MATH_BIGINTEGER_DATA => array()
  2001. );
  2002. if ( ($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) {
  2003. $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
  2004. $cache[MATH_BIGINTEGER_VARIABLE][] = $m;
  2005. $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($m);
  2006. }
  2007. $n = max(count($x), count($y), count($m));
  2008. $x = array_pad($x, $n, 0);
  2009. $y = array_pad($y, $n, 0);
  2010. $m = array_pad($m, $n, 0);
  2011. $a = array(MATH_BIGINTEGER_VALUE => $this->_array_repeat(0, $n + 1));
  2012. for ($i = 0; $i < $n; ++$i) {
  2013. $temp = $a[MATH_BIGINTEGER_VALUE][0] + $x[$i] * $y[0];
  2014. $temp = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * ((int) ($temp / MATH_BIGINTEGER_BASE_FULL)));
  2015. $temp = $temp * $cache[MATH_BIGINTEGER_DATA][$key];
  2016. $temp = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * ((int) ($temp / MATH_BIGINTEGER_BASE_FULL)));
  2017. $temp = $this->_add($this->_regularMultiply(array($x[$i]), $y), false, $this->_regularMultiply(array($temp), $m), false);
  2018. $a = $this->_add($a[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false);
  2019. $a[MATH_BIGINTEGER_VALUE] = array_slice($a[MATH_BIGINTEGER_VALUE], 1);
  2020. }
  2021. if ($this->_compare($a[MATH_BIGINTEGER_VALUE], false, $m, false) >= 0) {
  2022. $a = $this->_subtract($a[MATH_BIGINTEGER_VALUE], false, $m, false);
  2023. }
  2024. return $a[MATH_BIGINTEGER_VALUE];
  2025. }
  2026. /**
  2027. * Prepare a number for use in Montgomery Modular Reductions
  2028. *
  2029. * @see _montgomery()
  2030. * @see _slidingWindow()
  2031. * @access private
  2032. * @param Array $x
  2033. * @param Array $n
  2034. * @return Array
  2035. */
  2036. function _prepMontgomery($x, $n)
  2037. {
  2038. $lhs = new Math_BigInteger();
  2039. $lhs->value = array_merge($this->_array_repeat(0, count($n)), $x);
  2040. $rhs = new Math_BigInteger();
  2041. $rhs->value = $n;
  2042. list(, $temp) = $lhs->divide($rhs);
  2043. return $temp->value;
  2044. }
  2045. /**
  2046. * Modular Inverse of a number mod 2**26 (eg. 67108864)
  2047. *
  2048. * Based off of the bnpInvDigit function implemented and justified in the following URL:
  2049. *
  2050. * {@link http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js}
  2051. *
  2052. * The following URL provides more info:
  2053. *
  2054. * {@link http://groups.google.com/group/sci.crypt/msg/7a137205c1be7d85}
  2055. *
  2056. * As for why we do all the bitmasking... strange things can happen when converting from floats to ints. For
  2057. * instance, on some computers, var_dump((int) -4294967297) yields int(-1) and on others, it yields
  2058. * int(-2147483648). To avoid problems stemming from this, we use bitmasks to guarantee that ints aren't
  2059. * auto-converted to floats. The outermost bitmask is present because without it, there's no guarantee that
  2060. * the "residue" returned would be the so-called "common residue". We use fmod, in the last step, because the
  2061. * maximum possible $x is 26 bits and the maximum $result is 16 bits. Thus, we have to be able to handle up to
  2062. * 40 bits, which only 64-bit floating points will support.
  2063. *
  2064. * Thanks to Pedro Gimeno Fortea for input!
  2065. *
  2066. * @see _montgomery()
  2067. * @access private
  2068. * @param Array $x
  2069. * @return Integer
  2070. */
  2071. function _modInverse67108864($x) // 2**26 == 67,108,864
  2072. {
  2073. $x = -$x[0];
  2074. $result = $x & 0x3; // x**-1 mod 2**2
  2075. $result = ($result * (2 - $x * $result)) & 0xF; // x**-1 mod 2**4
  2076. $result = ($result * (2 - ($x & 0xFF) * $result)) & 0xFF; // x**-1 mod 2**8
  2077. $result = ($result * ((2 - ($x & 0xFFFF) * $result) & 0xFFFF)) & 0xFFFF; // x**-1 mod 2**16
  2078. $result = fmod($result * (2 - fmod($x * $result, MATH_BIGINTEGER_BASE_FULL)), MATH_BIGINTEGER_BASE_FULL); // x**-1 mod 2**26
  2079. return $result & MATH_BIGINTEGER_MAX_DIGIT;
  2080. }
  2081. /**
  2082. * Calculates modular inverses.
  2083. *
  2084. * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
  2085. *
  2086. * Here's an example:
  2087. * <code>
  2088. * <?php
  2089. * include('Math/BigInteger.php');
  2090. *
  2091. * $a = new Math_BigInteger(30);
  2092. * $b = new Math_BigInteger(17);
  2093. *
  2094. * $c = $a->modInverse($b);
  2095. * echo $c->toString(); // outputs 4
  2096. *
  2097. * echo "\r\n";
  2098. *
  2099. * $d = $a->multiply($c);
  2100. * list(, $d) = $d->divide($b);
  2101. * echo $d; // outputs 1 (as per the definition of modular inverse)
  2102. * ?>
  2103. * </code>
  2104. *
  2105. * @param Math_BigInteger $n
  2106. * @return mixed false, if no modular inverse exists, Math_BigInteger, otherwise.
  2107. * @access public
  2108. * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=21 HAC 14.64} for more information.
  2109. */
  2110. function modInverse($n)
  2111. {
  2112. switch ( MATH_BIGINTEGER_MODE ) {
  2113. case MATH_BIGINTEGER_MODE_GMP:
  2114. $temp = new Math_BigInteger();
  2115. $temp->value = gmp_invert($this->value, $n->value);
  2116. return ( $temp->value === false ) ? false : $this->_normalize($temp);
  2117. }
  2118. static $zero, $one;
  2119. if (!isset($zero)) {
  2120. $zero = new Math_BigInteger();
  2121. $one = new Math_BigInteger(1);
  2122. }
  2123. // $x mod -$n == $x mod $n.
  2124. $n = $n->abs();
  2125. if ($this->compare($zero) < 0) {
  2126. $temp = $this->abs();
  2127. $temp = $temp->modInverse($n);
  2128. return $this->_normalize($n->subtract($temp));
  2129. }
  2130. extract($this->extendedGCD($n));
  2131. if (!$gcd->equals($one)) {
  2132. return false;
  2133. }
  2134. $x = $x->compare($zero) < 0 ? $x->add($n) : $x;
  2135. return $this->compare($zero) < 0 ? $this->_normalize($n->subtract($x)) : $this->_normalize($x);
  2136. }
  2137. /**
  2138. * Calculates the greatest common divisor and Bezout's identity.
  2139. *
  2140. * Say you have 693 and 609. The GCD is 21. Bezout's identity states that there exist integers x and y such that
  2141. * 693*x + 609*y == 21. In point of fact, there are actually an infinite number of x and y combinations and which
  2142. * combination is returned is dependant upon which mode is in use. See
  2143. * {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bezout's identity - Wikipedia} for more information.
  2144. *
  2145. * Here's an example:
  2146. * <code>
  2147. * <?php
  2148. * include('Math/BigInteger.php');
  2149. *
  2150. * $a = new Math_BigInteger(693);
  2151. * $b = new Math_BigInteger(609);
  2152. *
  2153. * extract($a->extendedGCD($b));
  2154. *
  2155. * echo $gcd->toString() . "\r\n"; // outputs 21
  2156. * echo $a->toString() * $x->toString() + $b->toString() * $y->toString(); // outputs 21
  2157. * ?>
  2158. * </code>
  2159. *
  2160. * @param Math_BigInteger $n
  2161. * @return Math_BigInteger
  2162. * @access public
  2163. * @internal Calculates the GCD using the binary xGCD algorithim described in
  2164. * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=19 HAC 14.61}. As the text above 14.61 notes,
  2165. * the more traditional algorithim requires "relatively costly multiple-precision divisions".
  2166. */
  2167. function extendedGCD($n)
  2168. {
  2169. switch ( MATH_BIGINTEGER_MODE ) {
  2170. case MATH_BIGINTEGER_MODE_GMP:
  2171. extract(gmp_gcdext($this->value, $n->value));
  2172. return array(
  2173. 'gcd' => $this->_normalize(new Math_BigInteger($g)),
  2174. 'x' => $this->_normalize(new Math_BigInteger($s)),
  2175. 'y' => $this->_normalize(new Math_BigInteger($t))
  2176. );
  2177. case MATH_BIGINTEGER_MODE_BCMATH:
  2178. // it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works
  2179. // best when the base is a power of 2 and (2) i don't think it'd make much difference, anyway. as is,
  2180. // the basic extended euclidean algorithim is what we're using.
  2181. $u = $this->value;
  2182. $v = $n->value;
  2183. $a = '1';
  2184. $b = '0';
  2185. $c = '0';
  2186. $d = '1';
  2187. while (bccomp($v, '0', 0) != 0) {
  2188. $q = bcdiv($u, $v, 0);
  2189. $temp = $u;
  2190. $u = $v;
  2191. $v = bcsub($temp, bcmul($v, $q, 0), 0);
  2192. $temp = $a;
  2193. $a = $c;
  2194. $c = bcsub($temp, bcmul($a, $q, 0), 0);
  2195. $temp = $b;
  2196. $b = $d;
  2197. $d = bcsub($temp, bcmul($b, $q, 0), 0);
  2198. }
  2199. return array(
  2200. 'gcd' => $this->_normalize(new Math_BigInteger($u)),
  2201. 'x' => $this->_normalize(new Math_BigInteger($a)),
  2202. 'y' => $this->_normalize(new Math_BigInteger($b))
  2203. );
  2204. }
  2205. $y = $n->copy();
  2206. $x = $this->copy();
  2207. $g = new Math_BigInteger();
  2208. $g->value = array(1);
  2209. while ( !(($x->value[0] & 1)|| ($y->value[0] & 1)) ) {
  2210. $x->_rshift(1);
  2211. $y->_rshift(1);
  2212. $g->_lshift(1);
  2213. }
  2214. $u = $x->copy();
  2215. $v = $y->copy();
  2216. $a = new Math_BigInteger();
  2217. $b = new Math_BigInteger();
  2218. $c = new Math_BigInteger();
  2219. $d = new Math_BigInteger();
  2220. $a->value = $d->value = $g->value = array(1);
  2221. $b->value = $c->value = array();
  2222. while ( !empty($u->value) ) {
  2223. while ( !($u->value[0] & 1) ) {
  2224. $u->_rshift(1);
  2225. if ( (!empty($a->value) && ($a->value[0] & 1)) || (!empty($b->value) && ($b->value[0] & 1)) ) {
  2226. $a = $a->add($y);
  2227. $b = $b->subtract($x);
  2228. }
  2229. $a->_rshift(1);
  2230. $b->_rshift(1);
  2231. }
  2232. while ( !($v->value[0] & 1) ) {
  2233. $v->_rshift(1);
  2234. if ( (!empty($d->value) && ($d->value[0] & 1)) || (!empty($c->value) && ($c->value[0] & 1)) ) {
  2235. $c = $c->add($y);
  2236. $d = $d->subtract($x);
  2237. }
  2238. $c->_rshift(1);
  2239. $d->_rshift(1);
  2240. }
  2241. if ($u->compare($v) >= 0) {
  2242. $u = $u->subtract($v);
  2243. $a = $a->subtract($c);
  2244. $b = $b->subtract($d);
  2245. } else {
  2246. $v = $v->subtract($u);
  2247. $c = $c->subtract($a);
  2248. $d = $d->subtract($b);
  2249. }
  2250. }
  2251. return array(
  2252. 'gcd' => $this->_normalize($g->multiply($v)),
  2253. 'x' => $this->_normalize($c),
  2254. 'y' => $this->_normalize($d)
  2255. );
  2256. }
  2257. /**
  2258. * Calculates the greatest common divisor
  2259. *
  2260. * Say you have 693 and 609. The GCD is 21.
  2261. *
  2262. * Here's an example:
  2263. * <code>
  2264. * <?php
  2265. * include('Math/BigInteger.php');
  2266. *
  2267. * $a = new Math_BigInteger(693);
  2268. * $b = new Math_BigInteger(609);
  2269. *
  2270. * $gcd = a->extendedGCD($b);
  2271. *
  2272. * echo $gcd->toString() . "\r\n"; // outputs 21
  2273. * ?>
  2274. * </code>
  2275. *
  2276. * @param Math_BigInteger $n
  2277. * @return Math_BigInteger
  2278. * @access public
  2279. */
  2280. function gcd($n)
  2281. {
  2282. extract($this->extendedGCD($n));
  2283. return $gcd;
  2284. }
  2285. /**
  2286. * Absolute value.
  2287. *
  2288. * @return Math_BigInteger
  2289. * @access public
  2290. */
  2291. function abs()
  2292. {
  2293. $temp = new Math_BigInteger();
  2294. switch ( MATH_BIGINTEGER_MODE ) {
  2295. case MATH_BIGINTEGER_MODE_GMP:
  2296. $temp->value = gmp_abs($this->value);
  2297. break;
  2298. case MATH_BIGINTEGER_MODE_BCMATH:
  2299. $temp->value = (bccomp($this->value, '0', 0) < 0) ? substr($this->value, 1) : $this->value;
  2300. break;
  2301. default:
  2302. $temp->value = $this->value;
  2303. }
  2304. return $temp;
  2305. }
  2306. /**
  2307. * Compares two numbers.
  2308. *
  2309. * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is
  2310. * demonstrated thusly:
  2311. *
  2312. * $x > $y: $x->compare($y) > 0
  2313. * $x < $y: $x->compare($y) < 0
  2314. * $x == $y: $x->compare($y) == 0
  2315. *
  2316. * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y).
  2317. *
  2318. * @param Math_BigInteger $y
  2319. * @return Integer < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.
  2320. * @access public
  2321. * @see equals()
  2322. * @internal Could return $this->subtract($x), but that's not as fast as what we do do.
  2323. */
  2324. function compare($y)
  2325. {
  2326. switch ( MATH_BIGINTEGER_MODE ) {
  2327. case MATH_BIGINTEGER_MODE_GMP:
  2328. return gmp_cmp($this->value, $y->value);
  2329. case MATH_BIGINTEGER_MODE_BCMATH:
  2330. return bccomp($this->value, $y->value, 0);
  2331. }
  2332. return $this->_compare($this->value, $this->is_negative, $y->value, $y->is_negative);
  2333. }
  2334. /**
  2335. * Compares two numbers.
  2336. *
  2337. * @param Array $x_value
  2338. * @param Boolean $x_negative
  2339. * @param Array $y_value
  2340. * @param Boolean $y_negative
  2341. * @return Integer
  2342. * @see compare()
  2343. * @access private
  2344. */
  2345. function _compare($x_value, $x_negative, $y_value, $y_negative)
  2346. {
  2347. if ( $x_negative != $y_negative ) {
  2348. return ( !$x_negative && $y_negative ) ? 1 : -1;
  2349. }
  2350. $result = $x_negative ? -1 : 1;
  2351. if ( count($x_value) != count($y_value) ) {
  2352. return ( count($x_value) > count($y_value) ) ? $result : -$result;
  2353. }
  2354. $size = max(count($x_value), count($y_value));
  2355. $x_value = array_pad($x_value, $size, 0);
  2356. $y_value = array_pad($y_value, $size, 0);
  2357. for ($i = count($x_value) - 1; $i >= 0; --$i) {
  2358. if ($x_value[$i] != $y_value[$i]) {
  2359. return ( $x_value[$i] > $y_value[$i] ) ? $result : -$result;
  2360. }
  2361. }
  2362. return 0;
  2363. }
  2364. /**
  2365. * Tests the equality of two numbers.
  2366. *
  2367. * If you need to see if one number is greater than or less than another number, use Math_BigInteger::compare()
  2368. *
  2369. * @param Math_BigInteger $x
  2370. * @return Boolean
  2371. * @access public
  2372. * @see compare()
  2373. */
  2374. function equals($x)
  2375. {
  2376. switch ( MATH_BIGINTEGER_MODE ) {
  2377. case MATH_BIGINTEGER_MODE_GMP:
  2378. return gmp_cmp($this->value, $x->value) == 0;
  2379. default:
  2380. return $this->value === $x->value && $this->is_negative == $x->is_negative;
  2381. }
  2382. }
  2383. /**
  2384. * Set Precision
  2385. *
  2386. * Some bitwise operations give different results depending on the precision being used. Examples include left
  2387. * shift, not, and rotates.
  2388. *
  2389. * @param Integer $bits
  2390. * @access public
  2391. */
  2392. function setPrecision($bits)
  2393. {
  2394. $this->precision = $bits;
  2395. if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ) {
  2396. $this->bitmask = new Math_BigInteger(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256);
  2397. } else {
  2398. $this->bitmask = new Math_BigInteger(bcpow('2', $bits, 0));
  2399. }
  2400. $temp = $this->_normalize($this);
  2401. $this->value = $temp->value;
  2402. }
  2403. /**
  2404. * Logical And
  2405. *
  2406. * @param Math_BigInteger $x
  2407. * @access public
  2408. * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  2409. * @return Math_BigInteger
  2410. */
  2411. function bitwise_and($x)
  2412. {
  2413. switch ( MATH_BIGINTEGER_MODE ) {
  2414. case MATH_BIGINTEGER_MODE_GMP:
  2415. $temp = new Math_BigInteger();
  2416. $temp->value = gmp_and($this->value, $x->value);
  2417. return $this->_normalize($temp);
  2418. case MATH_BIGINTEGER_MODE_BCMATH:
  2419. $left = $this->toBytes();
  2420. $right = $x->toBytes();
  2421. $length = max(strlen($left), strlen($right));
  2422. $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
  2423. $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
  2424. return $this->_normalize(new Math_BigInteger($left & $right, 256));
  2425. }
  2426. $result = $this->copy();
  2427. $length = min(count($x->value), count($this->value));
  2428. $result->value = array_slice($result->value, 0, $length);
  2429. for ($i = 0; $i < $length; ++$i) {
  2430. $result->value[$i]&= $x->value[$i];
  2431. }
  2432. return $this->_normalize($result);
  2433. }
  2434. /**
  2435. * Logical Or
  2436. *
  2437. * @param Math_BigInteger $x
  2438. * @access public
  2439. * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  2440. * @return Math_BigInteger
  2441. */
  2442. function bitwise_or($x)
  2443. {
  2444. switch ( MATH_BIGINTEGER_MODE ) {
  2445. case MATH_BIGINTEGER_MODE_GMP:
  2446. $temp = new Math_BigInteger();
  2447. $temp->value = gmp_or($this->value, $x->value);
  2448. return $this->_normalize($temp);
  2449. case MATH_BIGINTEGER_MODE_BCMATH:
  2450. $left = $this->toBytes();
  2451. $right = $x->toBytes();
  2452. $length = max(strlen($left), strlen($right));
  2453. $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
  2454. $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
  2455. return $this->_normalize(new Math_BigInteger($left | $right, 256));
  2456. }
  2457. $length = max(count($this->value), count($x->value));
  2458. $result = $this->copy();
  2459. $result->value = array_pad($result->value, $length, 0);
  2460. $x->value = array_pad($x->value, $length, 0);
  2461. for ($i = 0; $i < $length; ++$i) {
  2462. $result->value[$i]|= $x->value[$i];
  2463. }
  2464. return $this->_normalize($result);
  2465. }
  2466. /**
  2467. * Logical Exclusive-Or
  2468. *
  2469. * @param Math_BigInteger $x
  2470. * @access public
  2471. * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  2472. * @return Math_BigInteger
  2473. */
  2474. function bitwise_xor($x)
  2475. {
  2476. switch ( MATH_BIGINTEGER_MODE ) {
  2477. case MATH_BIGINTEGER_MODE_GMP:
  2478. $temp = new Math_BigInteger();
  2479. $temp->value = gmp_xor($this->value, $x->value);
  2480. return $this->_normalize($temp);
  2481. case MATH_BIGINTEGER_MODE_BCMATH:
  2482. $left = $this->toBytes();
  2483. $right = $x->toBytes();
  2484. $length = max(strlen($left), strlen($right));
  2485. $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
  2486. $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
  2487. return $this->_normalize(new Math_BigInteger($left ^ $right, 256));
  2488. }
  2489. $length = max(count($this->value), count($x->value));
  2490. $result = $this->copy();
  2491. $result->value = array_pad($result->value, $length, 0);
  2492. $x->value = array_pad($x->value, $length, 0);
  2493. for ($i = 0; $i < $length; ++$i) {
  2494. $result->value[$i]^= $x->value[$i];
  2495. }
  2496. return $this->_normalize($result);
  2497. }
  2498. /**
  2499. * Logical Not
  2500. *
  2501. * @access public
  2502. * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  2503. * @return Math_BigInteger
  2504. */
  2505. function bitwise_not()
  2506. {
  2507. // calculuate "not" without regard to $this->precision
  2508. // (will always result in a smaller number. ie. ~1 isn't 1111 1110 - it's 0)
  2509. $temp = $this->toBytes();
  2510. $pre_msb = decbin(ord($temp[0]));
  2511. $temp = ~$temp;
  2512. $msb = decbin(ord($temp[0]));
  2513. if (strlen($msb) == 8) {
  2514. $msb = substr($msb, strpos($msb, '0'));
  2515. }
  2516. $temp[0] = chr(bindec($msb));
  2517. // see if we need to add extra leading 1's
  2518. $current_bits = strlen($pre_msb) + 8 * strlen($temp) - 8;
  2519. $new_bits = $this->precision - $current_bits;
  2520. if ($new_bits <= 0) {
  2521. return $this->_normalize(new Math_BigInteger($temp, 256));
  2522. }
  2523. // generate as many leading 1's as we need to.
  2524. $leading_ones = chr((1 << ($new_bits & 0x7)) - 1) . str_repeat(chr(0xFF), $new_bits >> 3);
  2525. $this->_base256_lshift($leading_ones, $current_bits);
  2526. $temp = str_pad($temp, ceil($this->bits / 8), chr(0), STR_PAD_LEFT);
  2527. return $this->_normalize(new Math_BigInteger($leading_ones | $temp, 256));
  2528. }
  2529. /**
  2530. * Logical Right Shift
  2531. *
  2532. * Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift.
  2533. *
  2534. * @param Integer $shift
  2535. * @return Math_BigInteger
  2536. * @access public
  2537. * @internal The only version that yields any speed increases is the internal version.
  2538. */
  2539. function bitwise_rightShift($shift)
  2540. {
  2541. $temp = new Math_BigInteger();
  2542. switch ( MATH_BIGINTEGER_MODE ) {
  2543. case MATH_BIGINTEGER_MODE_GMP:
  2544. static $two;
  2545. if (!isset($two)) {
  2546. $two = gmp_init('2');
  2547. }
  2548. $temp->value = gmp_div_q($this->value, gmp_pow($two, $shift));
  2549. break;
  2550. case MATH_BIGINTEGER_MODE_BCMATH:
  2551. $temp->value = bcdiv($this->value, bcpow('2', $shift, 0), 0);
  2552. break;
  2553. default: // could just replace _lshift with this, but then all _lshift() calls would need to be rewritten
  2554. // and I don't want to do that...
  2555. $temp->value = $this->value;
  2556. $temp->_rshift($shift);
  2557. }
  2558. return $this->_normalize($temp);
  2559. }
  2560. /**
  2561. * Logical Left Shift
  2562. *
  2563. * Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift.
  2564. *
  2565. * @param Integer $shift
  2566. * @return Math_BigInteger
  2567. * @access public
  2568. * @internal The only version that yields any speed increases is the internal version.
  2569. */
  2570. function bitwise_leftShift($shift)
  2571. {
  2572. $temp = new Math_BigInteger();
  2573. switch ( MATH_BIGINTEGER_MODE ) {
  2574. case MATH_BIGINTEGER_MODE_GMP:
  2575. static $two;
  2576. if (!isset($two)) {
  2577. $two = gmp_init('2');
  2578. }
  2579. $temp->value = gmp_mul($this->value, gmp_pow($two, $shift));
  2580. break;
  2581. case MATH_BIGINTEGER_MODE_BCMATH:
  2582. $temp->value = bcmul($this->value, bcpow('2', $shift, 0), 0);
  2583. break;
  2584. default: // could just replace _rshift with this, but then all _lshift() calls would need to be rewritten
  2585. // and I don't want to do that...
  2586. $temp->value = $this->value;
  2587. $temp->_lshift($shift);
  2588. }
  2589. return $this->_normalize($temp);
  2590. }
  2591. /**
  2592. * Logical Left Rotate
  2593. *
  2594. * Instead of the top x bits being dropped they're appended to the shifted bit string.
  2595. *
  2596. * @param Integer $shift
  2597. * @return Math_BigInteger
  2598. * @access public
  2599. */
  2600. function bitwise_leftRotate($shift)
  2601. {
  2602. $bits = $this->toBytes();
  2603. if ($this->precision > 0) {
  2604. $precision = $this->precision;
  2605. if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) {
  2606. $mask = $this->bitmask->subtract(new Math_BigInteger(1));
  2607. $mask = $mask->toBytes();
  2608. } else {
  2609. $mask = $this->bitmask->toBytes();
  2610. }
  2611. } else {
  2612. $temp = ord($bits[0]);
  2613. for ($i = 0; $temp >> $i; ++$i);
  2614. $precision = 8 * strlen($bits) - 8 + $i;
  2615. $mask = chr((1 << ($precision & 0x7)) - 1) . str_repeat(chr(0xFF), $precision >> 3);
  2616. }
  2617. if ($shift < 0) {
  2618. $shift+= $precision;
  2619. }
  2620. $shift%= $precision;
  2621. if (!$shift) {
  2622. return $this->copy();
  2623. }
  2624. $left = $this->bitwise_leftShift($shift);
  2625. $left = $left->bitwise_and(new Math_BigInteger($mask, 256));
  2626. $right = $this->bitwise_rightShift($precision - $shift);
  2627. $result = MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ? $left->bitwise_or($right) : $left->add($right);
  2628. return $this->_normalize($result);
  2629. }
  2630. /**
  2631. * Logical Right Rotate
  2632. *
  2633. * Instead of the bottom x bits being dropped they're prepended to the shifted bit string.
  2634. *
  2635. * @param Integer $shift
  2636. * @return Math_BigInteger
  2637. * @access public
  2638. */
  2639. function bitwise_rightRotate($shift)
  2640. {
  2641. return $this->bitwise_leftRotate(-$shift);
  2642. }
  2643. /**
  2644. * Set random number generator function
  2645. *
  2646. * This function is deprecated.
  2647. *
  2648. * @param String $generator
  2649. * @access public
  2650. */
  2651. function setRandomGenerator($generator)
  2652. {
  2653. }
  2654. /**
  2655. * Generates a random BigInteger
  2656. *
  2657. * Byte length is equal to $length. Uses crypt_random if it's loaded and mt_rand if it's not.
  2658. *
  2659. * @param Integer $length
  2660. * @return Math_BigInteger
  2661. * @access private
  2662. */
  2663. function _random_number_helper($size)
  2664. {
  2665. $crypt_random = function_exists('crypt_random_string') || (!class_exists('Crypt_Random') && function_exists('crypt_random_string'));
  2666. if ($crypt_random) {
  2667. $random = crypt_random_string($size);
  2668. } else {
  2669. $random = '';
  2670. if ($size & 1) {
  2671. $random.= chr(mt_rand(0, 255));
  2672. }
  2673. $blocks = $size >> 1;
  2674. for ($i = 0; $i < $blocks; ++$i) {
  2675. // mt_rand(-2147483648, 0x7FFFFFFF) always produces -2147483648 on some systems
  2676. $random.= pack('n', mt_rand(0, 0xFFFF));
  2677. }
  2678. }
  2679. return new Math_BigInteger($random, 256);
  2680. }
  2681. /**
  2682. * Generate a random number
  2683. *
  2684. * @param optional Integer $min
  2685. * @param optional Integer $max
  2686. * @return Math_BigInteger
  2687. * @access public
  2688. */
  2689. function random($min = false, $max = false)
  2690. {
  2691. if ($min === false) {
  2692. $min = new Math_BigInteger(0);
  2693. }
  2694. if ($max === false) {
  2695. $max = new Math_BigInteger(0x7FFFFFFF);
  2696. }
  2697. $compare = $max->compare($min);
  2698. if (!$compare) {
  2699. return $this->_normalize($min);
  2700. } else if ($compare < 0) {
  2701. // if $min is bigger then $max, swap $min and $max
  2702. $temp = $max;
  2703. $max = $min;
  2704. $min = $temp;
  2705. }
  2706. static $one;
  2707. if (!isset($one)) {
  2708. $one = new Math_BigInteger(1);
  2709. }
  2710. $max = $max->subtract($min->subtract($one));
  2711. $size = strlen(ltrim($max->toBytes(), chr(0)));
  2712. /*
  2713. doing $random % $max doesn't work because some numbers will be more likely to occur than others.
  2714. eg. if $max is 140 and $random's max is 255 then that'd mean both $random = 5 and $random = 145
  2715. would produce 5 whereas the only value of random that could produce 139 would be 139. ie.
  2716. not all numbers would be equally likely. some would be more likely than others.
  2717. creating a whole new random number until you find one that is within the range doesn't work
  2718. because, for sufficiently small ranges, the likelihood that you'd get a number within that range
  2719. would be pretty small. eg. with $random's max being 255 and if your $max being 1 the probability
  2720. would be pretty high that $random would be greater than $max.
  2721. phpseclib works around this using the technique described here:
  2722. http://crypto.stackexchange.com/questions/5708/creating-a-small-number-from-a-cryptographically-secure-random-string
  2723. */
  2724. $random_max = new Math_BigInteger(chr(1) . str_repeat("\0", $size), 256);
  2725. $random = $this->_random_number_helper($size);
  2726. list($max_multiple) = $random_max->divide($max);
  2727. $max_multiple = $max_multiple->multiply($max);
  2728. while ($random->compare($max_multiple) >= 0) {
  2729. $random = $random->subtract($max_multiple);
  2730. $random_max = $random_max->subtract($max_multiple);
  2731. $random = $random->bitwise_leftShift(8);
  2732. $random = $random->add($this->_random_number_helper(1));
  2733. $random_max = $random_max->bitwise_leftShift(8);
  2734. list($max_multiple) = $random_max->divide($max);
  2735. $max_multiple = $max_multiple->multiply($max);
  2736. }
  2737. list(, $random) = $random->divide($max);
  2738. return $this->_normalize($random->add($min));
  2739. }
  2740. /**
  2741. * Generate a random prime number.
  2742. *
  2743. * If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed,
  2744. * give up and return false.
  2745. *
  2746. * @param optional Integer $min
  2747. * @param optional Integer $max
  2748. * @param optional Integer $timeout
  2749. * @return Math_BigInteger
  2750. * @access public
  2751. * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}.
  2752. */
  2753. function randomPrime($min = false, $max = false, $timeout = false)
  2754. {
  2755. if ($min === false) {
  2756. $min = new Math_BigInteger(0);
  2757. }
  2758. if ($max === false) {
  2759. $max = new Math_BigInteger(0x7FFFFFFF);
  2760. }
  2761. $compare = $max->compare($min);
  2762. if (!$compare) {
  2763. return $min->isPrime() ? $min : false;
  2764. } else if ($compare < 0) {
  2765. // if $min is bigger then $max, swap $min and $max
  2766. $temp = $max;
  2767. $max = $min;
  2768. $min = $temp;
  2769. }
  2770. static $one, $two;
  2771. if (!isset($one)) {
  2772. $one = new Math_BigInteger(1);
  2773. $two = new Math_BigInteger(2);
  2774. }
  2775. $start = time();
  2776. $x = $this->random($min, $max);
  2777. // gmp_nextprime() requires PHP 5 >= 5.2.0 per <http://php.net/gmp-nextprime>.
  2778. if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && function_exists('gmp_nextprime') ) {
  2779. $p = new Math_BigInteger();
  2780. $p->value = gmp_nextprime($x->value);
  2781. if ($p->compare($max) <= 0) {
  2782. return $p;
  2783. }
  2784. if (!$min->equals($x)) {
  2785. $x = $x->subtract($one);
  2786. }
  2787. return $x->randomPrime($min, $x);
  2788. }
  2789. if ($x->equals($two)) {
  2790. return $x;
  2791. }
  2792. $x->_make_odd();
  2793. if ($x->compare($max) > 0) {
  2794. // if $x > $max then $max is even and if $min == $max then no prime number exists between the specified range
  2795. if ($min->equals($max)) {
  2796. return false;
  2797. }
  2798. $x = $min->copy();
  2799. $x->_make_odd();
  2800. }
  2801. $initial_x = $x->copy();
  2802. while (true) {
  2803. if ($timeout !== false && time() - $start > $timeout) {
  2804. return false;
  2805. }
  2806. if ($x->isPrime()) {
  2807. return $x;
  2808. }
  2809. $x = $x->add($two);
  2810. if ($x->compare($max) > 0) {
  2811. $x = $min->copy();
  2812. if ($x->equals($two)) {
  2813. return $x;
  2814. }
  2815. $x->_make_odd();
  2816. }
  2817. if ($x->equals($initial_x)) {
  2818. return false;
  2819. }
  2820. }
  2821. }
  2822. /**
  2823. * Make the current number odd
  2824. *
  2825. * If the current number is odd it'll be unchanged. If it's even, one will be added to it.
  2826. *
  2827. * @see randomPrime()
  2828. * @access private
  2829. */
  2830. function _make_odd()
  2831. {
  2832. switch ( MATH_BIGINTEGER_MODE ) {
  2833. case MATH_BIGINTEGER_MODE_GMP:
  2834. gmp_setbit($this->value, 0);
  2835. break;
  2836. case MATH_BIGINTEGER_MODE_BCMATH:
  2837. if ($this->value[strlen($this->value) - 1] % 2 == 0) {
  2838. $this->value = bcadd($this->value, '1');
  2839. }
  2840. break;
  2841. default:
  2842. $this->value[0] |= 1;
  2843. }
  2844. }
  2845. /**
  2846. * Checks a numer to see if it's prime
  2847. *
  2848. * Assuming the $t parameter is not set, this function has an error rate of 2**-80. The main motivation for the
  2849. * $t parameter is distributability. Math_BigInteger::randomPrime() can be distributed accross multiple pageloads
  2850. * on a website instead of just one.
  2851. *
  2852. * @param optional Integer $t
  2853. * @return Boolean
  2854. * @access public
  2855. * @internal Uses the
  2856. * {@link http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Miller-Rabin primality test}. See
  2857. * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=8 HAC 4.24}.
  2858. */
  2859. function isPrime($t = false)
  2860. {
  2861. $length = strlen($this->toBytes());
  2862. if (!$t) {
  2863. // see HAC 4.49 "Note (controlling the error probability)"
  2864. if ($length >= 163) { $t = 2; } // floor(1300 / 8)
  2865. else if ($length >= 106) { $t = 3; } // floor( 850 / 8)
  2866. else if ($length >= 81 ) { $t = 4; } // floor( 650 / 8)
  2867. else if ($length >= 68 ) { $t = 5; } // floor( 550 / 8)
  2868. else if ($length >= 56 ) { $t = 6; } // floor( 450 / 8)
  2869. else if ($length >= 50 ) { $t = 7; } // floor( 400 / 8)
  2870. else if ($length >= 43 ) { $t = 8; } // floor( 350 / 8)
  2871. else if ($length >= 37 ) { $t = 9; } // floor( 300 / 8)
  2872. else if ($length >= 31 ) { $t = 12; } // floor( 250 / 8)
  2873. else if ($length >= 25 ) { $t = 15; } // floor( 200 / 8)
  2874. else if ($length >= 18 ) { $t = 18; } // floor( 150 / 8)
  2875. else { $t = 27; }
  2876. }
  2877. // ie. gmp_testbit($this, 0)
  2878. // ie. isEven() or !isOdd()
  2879. switch ( MATH_BIGINTEGER_MODE ) {
  2880. case MATH_BIGINTEGER_MODE_GMP:
  2881. return gmp_prob_prime($this->value, $t) != 0;
  2882. case MATH_BIGINTEGER_MODE_BCMATH:
  2883. if ($this->value === '2') {
  2884. return true;
  2885. }
  2886. if ($this->value[strlen($this->value) - 1] % 2 == 0) {
  2887. return false;
  2888. }
  2889. break;
  2890. default:
  2891. if ($this->value == array(2)) {
  2892. return true;
  2893. }
  2894. if (~$this->value[0] & 1) {
  2895. return false;
  2896. }
  2897. }
  2898. static $primes, $zero, $one, $two;
  2899. if (!isset($primes)) {
  2900. $primes = array(
  2901. 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
  2902. 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
  2903. 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227,
  2904. 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
  2905. 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
  2906. 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509,
  2907. 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617,
  2908. 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727,
  2909. 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
  2910. 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947,
  2911. 953, 967, 971, 977, 983, 991, 997
  2912. );
  2913. if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) {
  2914. for ($i = 0; $i < count($primes); ++$i) {
  2915. $primes[$i] = new Math_BigInteger($primes[$i]);
  2916. }
  2917. }
  2918. $zero = new Math_BigInteger();
  2919. $one = new Math_BigInteger(1);
  2920. $two = new Math_BigInteger(2);
  2921. }
  2922. if ($this->equals($one)) {
  2923. return false;
  2924. }
  2925. // see HAC 4.4.1 "Random search for probable primes"
  2926. if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) {
  2927. foreach ($primes as $prime) {
  2928. list(, $r) = $this->divide($prime);
  2929. if ($r->equals($zero)) {
  2930. return $this->equals($prime);
  2931. }
  2932. }
  2933. } else {
  2934. $value = $this->value;
  2935. foreach ($primes as $prime) {
  2936. list(, $r) = $this->_divide_digit($value, $prime);
  2937. if (!$r) {
  2938. return count($value) == 1 && $value[0] == $prime;
  2939. }
  2940. }
  2941. }
  2942. $n = $this->copy();
  2943. $n_1 = $n->subtract($one);
  2944. $n_2 = $n->subtract($two);
  2945. $r = $n_1->copy();
  2946. $r_value = $r->value;
  2947. // ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s));
  2948. if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) {
  2949. $s = 0;
  2950. // if $n was 1, $r would be 0 and this would be an infinite loop, hence our $this->equals($one) check earlier
  2951. while ($r->value[strlen($r->value) - 1] % 2 == 0) {
  2952. $r->value = bcdiv($r->value, '2', 0);
  2953. ++$s;
  2954. }
  2955. } else {
  2956. for ($i = 0, $r_length = count($r_value); $i < $r_length; ++$i) {
  2957. $temp = ~$r_value[$i] & 0xFFFFFF;
  2958. for ($j = 1; ($temp >> $j) & 1; ++$j);
  2959. if ($j != 25) {
  2960. break;
  2961. }
  2962. }
  2963. $s = 26 * $i + $j - 1;
  2964. $r->_rshift($s);
  2965. }
  2966. for ($i = 0; $i < $t; ++$i) {
  2967. $a = $this->random($two, $n_2);
  2968. $y = $a->modPow($r, $n);
  2969. if (!$y->equals($one) && !$y->equals($n_1)) {
  2970. for ($j = 1; $j < $s && !$y->equals($n_1); ++$j) {
  2971. $y = $y->modPow($two, $n);
  2972. if ($y->equals($one)) {
  2973. return false;
  2974. }
  2975. }
  2976. if (!$y->equals($n_1)) {
  2977. return false;
  2978. }
  2979. }
  2980. }
  2981. return true;
  2982. }
  2983. /**
  2984. * Logical Left Shift
  2985. *
  2986. * Shifts BigInteger's by $shift bits.
  2987. *
  2988. * @param Integer $shift
  2989. * @access private
  2990. */
  2991. function _lshift($shift)
  2992. {
  2993. if ( $shift == 0 ) {
  2994. return;
  2995. }
  2996. $num_digits = (int) ($shift / MATH_BIGINTEGER_BASE);
  2997. $shift %= MATH_BIGINTEGER_BASE;
  2998. $shift = 1 << $shift;
  2999. $carry = 0;
  3000. for ($i = 0; $i < count($this->value); ++$i) {
  3001. $temp = $this->value[$i] * $shift + $carry;
  3002. $carry = (int) ($temp / MATH_BIGINTEGER_BASE_FULL);
  3003. $this->value[$i] = (int) ($temp - $carry * MATH_BIGINTEGER_BASE_FULL);
  3004. }
  3005. if ( $carry ) {
  3006. $this->value[] = $carry;
  3007. }
  3008. while ($num_digits--) {
  3009. array_unshift($this->value, 0);
  3010. }
  3011. }
  3012. /**
  3013. * Logical Right Shift
  3014. *
  3015. * Shifts BigInteger's by $shift bits.
  3016. *
  3017. * @param Integer $shift
  3018. * @access private
  3019. */
  3020. function _rshift($shift)
  3021. {
  3022. if ($shift == 0) {
  3023. return;
  3024. }
  3025. $num_digits = (int) ($shift / MATH_BIGINTEGER_BASE);
  3026. $shift %= MATH_BIGINTEGER_BASE;
  3027. $carry_shift = MATH_BIGINTEGER_BASE - $shift;
  3028. $carry_mask = (1 << $shift) - 1;
  3029. if ( $num_digits ) {
  3030. $this->value = array_slice($this->value, $num_digits);
  3031. }
  3032. $carry = 0;
  3033. for ($i = count($this->value) - 1; $i >= 0; --$i) {
  3034. $temp = $this->value[$i] >> $shift | $carry;
  3035. $carry = ($this->value[$i] & $carry_mask) << $carry_shift;
  3036. $this->value[$i] = $temp;
  3037. }
  3038. $this->value = $this->_trim($this->value);
  3039. }
  3040. /**
  3041. * Normalize
  3042. *
  3043. * Removes leading zeros and truncates (if necessary) to maintain the appropriate precision
  3044. *
  3045. * @param Math_BigInteger
  3046. * @return Math_BigInteger
  3047. * @see _trim()
  3048. * @access private
  3049. */
  3050. function _normalize($result)
  3051. {
  3052. $result->precision = $this->precision;
  3053. $result->bitmask = $this->bitmask;
  3054. switch ( MATH_BIGINTEGER_MODE ) {
  3055. case MATH_BIGINTEGER_MODE_GMP:
  3056. if (!empty($result->bitmask->value)) {
  3057. $result->value = gmp_and($result->value, $result->bitmask->value);
  3058. }
  3059. return $result;
  3060. case MATH_BIGINTEGER_MODE_BCMATH:
  3061. if (!empty($result->bitmask->value)) {
  3062. $result->value = bcmod($result->value, $result->bitmask->value);
  3063. }
  3064. return $result;
  3065. }
  3066. $value = &$result->value;
  3067. if ( !count($value) ) {
  3068. return $result;
  3069. }
  3070. $value = $this->_trim($value);
  3071. if (!empty($result->bitmask->value)) {
  3072. $length = min(count($value), count($this->bitmask->value));
  3073. $value = array_slice($value, 0, $length);
  3074. for ($i = 0; $i < $length; ++$i) {
  3075. $value[$i] = $value[$i] & $this->bitmask->value[$i];
  3076. }
  3077. }
  3078. return $result;
  3079. }
  3080. /**
  3081. * Trim
  3082. *
  3083. * Removes leading zeros
  3084. *
  3085. * @param Array $value
  3086. * @return Math_BigInteger
  3087. * @access private
  3088. */
  3089. function _trim($value)
  3090. {
  3091. for ($i = count($value) - 1; $i >= 0; --$i) {
  3092. if ( $value[$i] ) {
  3093. break;
  3094. }
  3095. unset($value[$i]);
  3096. }
  3097. return $value;
  3098. }
  3099. /**
  3100. * Array Repeat
  3101. *
  3102. * @param $input Array
  3103. * @param $multiplier mixed
  3104. * @return Array
  3105. * @access private
  3106. */
  3107. function _array_repeat($input, $multiplier)
  3108. {
  3109. return ($multiplier) ? array_fill(0, $multiplier, $input) : array();
  3110. }
  3111. /**
  3112. * Logical Left Shift
  3113. *
  3114. * Shifts binary strings $shift bits, essentially multiplying by 2**$shift.
  3115. *
  3116. * @param $x String
  3117. * @param $shift Integer
  3118. * @return String
  3119. * @access private
  3120. */
  3121. function _base256_lshift(&$x, $shift)
  3122. {
  3123. if ($shift == 0) {
  3124. return;
  3125. }
  3126. $num_bytes = $shift >> 3; // eg. floor($shift/8)
  3127. $shift &= 7; // eg. $shift % 8
  3128. $carry = 0;
  3129. for ($i = strlen($x) - 1; $i >= 0; --$i) {
  3130. $temp = ord($x[$i]) << $shift | $carry;
  3131. $x[$i] = chr($temp);
  3132. $carry = $temp >> 8;
  3133. }
  3134. $carry = ($carry != 0) ? chr($carry) : '';
  3135. $x = $carry . $x . str_repeat(chr(0), $num_bytes);
  3136. }
  3137. /**
  3138. * Logical Right Shift
  3139. *
  3140. * Shifts binary strings $shift bits, essentially dividing by 2**$shift and returning the remainder.
  3141. *
  3142. * @param $x String
  3143. * @param $shift Integer
  3144. * @return String
  3145. * @access private
  3146. */
  3147. function _base256_rshift(&$x, $shift)
  3148. {
  3149. if ($shift == 0) {
  3150. $x = ltrim($x, chr(0));
  3151. return '';
  3152. }
  3153. $num_bytes = $shift >> 3; // eg. floor($shift/8)
  3154. $shift &= 7; // eg. $shift % 8
  3155. $remainder = '';
  3156. if ($num_bytes) {
  3157. $start = $num_bytes > strlen($x) ? -strlen($x) : -$num_bytes;
  3158. $remainder = substr($x, $start);
  3159. $x = substr($x, 0, -$num_bytes);
  3160. }
  3161. $carry = 0;
  3162. $carry_shift = 8 - $shift;
  3163. for ($i = 0; $i < strlen($x); ++$i) {
  3164. $temp = (ord($x[$i]) >> $shift) | $carry;
  3165. $carry = (ord($x[$i]) << $carry_shift) & 0xFF;
  3166. $x[$i] = chr($temp);
  3167. }
  3168. $x = ltrim($x, chr(0));
  3169. $remainder = chr($carry >> $carry_shift) . $remainder;
  3170. return ltrim($remainder, chr(0));
  3171. }
  3172. // one quirk about how the following functions are implemented is that PHP defines N to be an unsigned long
  3173. // at 32-bits, while java's longs are 64-bits.
  3174. /**
  3175. * Converts 32-bit integers to bytes.
  3176. *
  3177. * @param Integer $x
  3178. * @return String
  3179. * @access private
  3180. */
  3181. function _int2bytes($x)
  3182. {
  3183. return ltrim(pack('N', $x), chr(0));
  3184. }
  3185. /**
  3186. * Converts bytes to 32-bit integers
  3187. *
  3188. * @param String $x
  3189. * @return Integer
  3190. * @access private
  3191. */
  3192. function _bytes2int($x)
  3193. {
  3194. $temp = unpack('Nint', str_pad($x, 4, chr(0), STR_PAD_LEFT));
  3195. return $temp['int'];
  3196. }
  3197. /**
  3198. * DER-encode an integer
  3199. *
  3200. * The ability to DER-encode integers is needed to create RSA public keys for use with OpenSSL
  3201. *
  3202. * @see modPow()
  3203. * @access private
  3204. * @param Integer $length
  3205. * @return String
  3206. */
  3207. function _encodeASN1Length($length)
  3208. {
  3209. if ($length <= 0x7F) {
  3210. return chr($length);
  3211. }
  3212. $temp = ltrim(pack('N', $length), chr(0));
  3213. return pack('Ca*', 0x80 | strlen($temp), $temp);
  3214. }
  3215. }