GmpBigIntegerFactory.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Anton E. Lebedevich                             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class GmpBigIntegerFactory extends BigNumberFactory
00016     {
00020         public static function me()
00021         {
00022             return Singleton::getInstance(__CLASS__);
00023         }
00024         
00028         public function makeNumber($number, $base = 10)
00029         {
00030             return GmpBigInteger::make($number, $base);
00031         }
00032         
00036         public function makeFromBinary($binary)
00037         {
00038             return GmpBigInteger::makeFromBinary($binary);
00039         }
00040         
00044         public function makeRandom($stop, RandomSource $source)
00045         {
00046             if (is_string($stop)) {
00047                 $stop = $this->makeNumber($stop);
00048             } elseif (
00049                 $stop instanceof BigInteger
00050                 && !$stop instanceof GmpBigInteger
00051             ) {
00052                 $stop = $this->makeNumber($stop->toString());
00053             }
00054             
00055             Assert::isTrue($stop instanceof GmpBigInteger);
00056             
00057             $numBytes = ceil(log($stop->floatValue(), 2) / 8);
00058             
00059             return $this->
00060                 makeFromBinary("\x00".$source->getBytes($numBytes))->
00061                 mod($stop);
00062         }
00063     }
00064 ?>