Failed Conditions
Push — v7 ( a687dc...e264c8 )
by Florent
02:28
created

NumberSize::bnNumBits()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace Jose\Component\Core\Util\Ecc\Util;
4
5
use Jose\Component\Core\Util\Ecc\Math\GmpMath;
6
7
final class NumberSize
8
{
9
    /**
10
     * Returns the number of bits used to store this number. Non-singicant upper bits are not counted.
11
     *
12
     * @param  GmpMath $adapter
13
     * @param  \GMP             $x
14
     * @return number
15
     *
16
     * @link https://www.openssl.org/docs/crypto/BN_num_bytes.html
17
     */
18
    public static function bnNumBits(GmpMath $adapter, \GMP $x)
19
    {
20
        $zero = gmp_init(0, 10);
21
        if ($adapter->equals($x, $zero)) {
22
            return 0;
23
        }
24
25
        $log2 = 0;
26
        while (false === $adapter->equals($x, $zero)) {
27
            $x = $adapter->rightShift($x, 1);
28
            $log2++;
29
        }
30
31
        return $log2 ;
32
    }
33
}
34