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

NumberSize   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 27
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A bnNumBits() 0 15 3
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