HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/quadcode/vendor/starkbank/ecdsa/src/utils/integer.php
<?php

namespace EllipticCurve\Utils;

use GMP;
use Exception;
use ValueError;


class Integer
{
    public static function modulo($x, $n)
    {
        $mod = gmp_div_r($x, $n);
        
        if ($mod < 0) {
            $mod += $n;
        }
        
        return $mod;
    }

    public static function toBigInt($value, $base=0)
    {
        return ($value instanceof GMP) ? $value : gmp_init($value, $base);
    }

    /**
    Return integer x in the range: min <= x <= max

    ## Parameters (required):
        - min: minimum value of the integer
        - max: maximum value of the integer
        
    ## Return:
     */
    public static function between($min, $max)
    {
        $range = $max - $min;
        if ($range < 0) {
            throw new ValueError("Argument #1 (\$min) must be less than or equal to argument #2 (\$max)");
        }

        if ($range == 0) {
            return $min;
        }

        $randomInt = Integer::generate($range);
        while ($randomInt > $range) {
            $randomInt = Integer::generate($range);
        }

        return gmp_add($randomInt, $min);
    }

    private static function generate($max)
    {
        $bitsNeeded = Integer::bitLength($max, 2);
        $bytesNeeded = intdiv($bitsNeeded, 8) + 1;
        
        $randomBytes = random_bytes($bytesNeeded);
        $randomHexadecimal = Binary::hexFromByteString($randomBytes);
        $randomBits = substr(Binary::bitsFromHex($randomHexadecimal), 0, $bitsNeeded);

        $randomInt = Integer::toBigInt($randomBits, 2);
        return $randomInt;
    }

    private static function bitLength($number)
    {
        for ($power = 1; $power < PHP_INT_MAX; $power++) {
            if($number >= gmp_pow(2, $power - 1) and $number < gmp_pow(2, $power))
                return $power;
        }
        throw new Exception("Bit length calculation exceeded limit.");
    }
}