1 | <?php |
||||
2 | declare(strict_types=1); |
||||
3 | |||||
4 | namespace Mdanter\Ecc\Serializer\Point; |
||||
5 | |||||
6 | use Mdanter\Ecc\Math\GmpMathInterface; |
||||
7 | use Mdanter\Ecc\Primitives\CurveFpInterface; |
||||
8 | use Mdanter\Ecc\Primitives\PointInterface; |
||||
9 | use Mdanter\Ecc\Serializer\Util\CurveOidMapper; |
||||
10 | |||||
11 | class CompressedPointSerializer implements PointSerializerInterface |
||||
12 | { |
||||
13 | /** |
||||
14 | * @var GmpMathInterface |
||||
15 | */ |
||||
16 | private $adapter; |
||||
17 | |||||
18 | /** |
||||
19 | * @var \Mdanter\Ecc\Math\NumberTheory |
||||
20 | */ |
||||
21 | private $theory; |
||||
22 | |||||
23 | /** |
||||
24 | * CompressedPointSerializer constructor. |
||||
25 | * @param GmpMathInterface $adapter |
||||
26 | */ |
||||
27 | public function __construct(GmpMathInterface $adapter) |
||||
28 | { |
||||
29 | $this->adapter = $adapter; |
||||
30 | $this->theory = $adapter->getNumberTheory(); |
||||
31 | } |
||||
32 | |||||
33 | /** |
||||
34 | * @param PointInterface $point |
||||
35 | * @return string |
||||
36 | */ |
||||
37 | public function getPrefix(PointInterface $point): string |
||||
38 | { |
||||
39 | if ($this->adapter->equals($this->adapter->mod($point->getY(), gmp_init(2, 10)), gmp_init(0))) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() It seems like
gmp_init(0) can also be of type resource ; however, parameter $other of Mdanter\Ecc\Math\GmpMathInterface::equals() does only seem to accept GMP , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
40 | return '02'; |
||||
41 | } else { |
||||
42 | return '03'; |
||||
43 | } |
||||
44 | } |
||||
45 | |||||
46 | /** |
||||
47 | * @param PointInterface $point |
||||
48 | * @return string |
||||
49 | */ |
||||
50 | public function serialize(PointInterface $point): string |
||||
51 | { |
||||
52 | $length = CurveOidMapper::getByteSize($point->getCurve()) * 2; |
||||
53 | |||||
54 | $hexString = $this->getPrefix($point); |
||||
55 | $hexString .= str_pad(gmp_strval($point->getX(), 16), $length, '0', STR_PAD_LEFT); |
||||
56 | |||||
57 | return $hexString; |
||||
58 | } |
||||
59 | |||||
60 | /** |
||||
61 | * @param CurveFpInterface $curve |
||||
62 | * @param string $data - hex serialized compressed point |
||||
63 | * @return PointInterface |
||||
64 | */ |
||||
65 | public function unserialize(CurveFpInterface $curve, string $data): PointInterface |
||||
66 | { |
||||
67 | $prefix = substr($data, 0, 2); |
||||
68 | if ($prefix !== '03' && $prefix !== '02') { |
||||
69 | throw new \InvalidArgumentException('Invalid data: only compressed keys are supported.'); |
||||
70 | } |
||||
71 | |||||
72 | $x = gmp_init(substr($data, 2), 16); |
||||
73 | $y = $curve->recoverYfromX($prefix === '03', $x); |
||||
0 ignored issues
–
show
It seems like
$x can also be of type resource ; however, parameter $x of Mdanter\Ecc\Primitives\C...erface::recoverYfromX() does only seem to accept GMP , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
74 | |||||
75 | return $curve->getPoint($x, $y); |
||||
0 ignored issues
–
show
It seems like
$x can also be of type resource ; however, parameter $x of Mdanter\Ecc\Primitives\C...FpInterface::getPoint() does only seem to accept GMP , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
76 | } |
||||
77 | } |
||||
78 |