BigInteger.php 129 KB

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