Issues (139)

src/Random/RandomNumberGenerator.php (1 issue)

1
<?php
2
declare(strict_types=1);
3
4
namespace Mdanter\Ecc\Random;
5
6
use Mdanter\Ecc\Math\GmpMathInterface;
7
use Mdanter\Ecc\Util\NumberSize;
8
9
class RandomNumberGenerator implements RandomNumberGeneratorInterface
10
{
11
    /**
12
     * @var GmpMathInterface
13
     */
14
    private $adapter;
15
16
    /**
17
     * RandomNumberGenerator constructor.
18
     * @param GmpMathInterface $adapter
19
     */
20
    public function __construct(GmpMathInterface $adapter)
21
    {
22
        $this->adapter = $adapter;
23
    }
24
25
    /**
26
     * @param \GMP $max
27
     * @return \GMP
28
     */
29
    public function generate(\GMP $max): \GMP
30
    {
31
        $numBits = NumberSize::bnNumBits($this->adapter, $max);
32
        $numBytes = (int) ceil($numBits / 8);
33
        // Generate an integer of size >= $numBits
34
        $bytes = random_bytes($numBytes);
35
        $value = $this->adapter->stringToInt($bytes);
36
37
        $mask = gmp_sub(gmp_pow(2, $numBits), 1);
38
        $integer = gmp_and($value, $mask);
39
40
        return $integer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $integer could return the type resource which is incompatible with the type-hinted return GMP. Consider adding an additional type-check to rule them out.
Loading history...
41
    }
42
}
43