BigInteger.php 127 KB

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