CurveFactory::getSecpFactory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Mdanter\Ecc\Curves;
5
6
use Mdanter\Ecc\Exception\UnknownCurveException;
0 ignored issues
show
Bug introduced by
The type Mdanter\Ecc\Exception\UnknownCurveException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Mdanter\Ecc\Exception\UnsupportedCurveException;
8
use Mdanter\Ecc\Math\GmpMathInterface;
9
use Mdanter\Ecc\Math\MathAdapterFactory;
10
use Mdanter\Ecc\Primitives\GeneratorPoint;
11
12
class CurveFactory
13
{
14
    /**
15
     * @param string $name
16
     * @return NamedCurveFp
17
     */
18
    public static function getCurveByName(string $name): NamedCurveFp
19
    {
20
        $adapter = MathAdapterFactory::getAdapter();
21
        $nistFactory = self::getNistFactory($adapter);
22
        $secpFactory = self::getSecpFactory($adapter);
23
24
        switch ($name) {
25
            case NistCurve::NAME_P192:
26
                return $nistFactory->curve192();
27
            case NistCurve::NAME_P224:
28
                return $nistFactory->curve224();
29
            case NistCurve::NAME_P256:
30
                return $nistFactory->curve256();
31
            case NistCurve::NAME_P384:
32
                return $nistFactory->curve384();
33
            case NistCurve::NAME_P521:
34
                return $nistFactory->curve521();
35
            case SecgCurve::NAME_SECP_112R1:
36
                return $secpFactory->curve112r1();
37
            case SecgCurve::NAME_SECP_192K1:
38
                return $secpFactory->curve192k1();
39
            case SecgCurve::NAME_SECP_256K1:
40
                return $secpFactory->curve256k1();
41
            case SecgCurve::NAME_SECP_256R1:
42
                return $secpFactory->curve256r1();
43
            case SecgCurve::NAME_SECP_384R1:
44
                return $secpFactory->curve384r1();
45
            default:
46
                $error = new UnsupportedCurveException('Unknown curve.');
47
                $error->setCurveName($name);
48
                throw $error;
49
        }
50
    }
51
52
    /**
53
     * @param string $name
54
     * @return GeneratorPoint
55
     */
56
    public static function getGeneratorByName(string $name): GeneratorPoint
57
    {
58
        $adapter = MathAdapterFactory::getAdapter();
59
        $nistFactory = self::getNistFactory($adapter);
60
        $secpFactory = self::getSecpFactory($adapter);
61
62
        switch ($name) {
63
            case NistCurve::NAME_P192:
64
                return $nistFactory->generator192();
65
            case NistCurve::NAME_P224:
66
                return $nistFactory->generator224();
67
            case NistCurve::NAME_P256:
68
                return $nistFactory->generator256();
69
            case NistCurve::NAME_P384:
70
                return $nistFactory->generator384();
71
            case NistCurve::NAME_P521:
72
                return $nistFactory->generator521();
73
            case SecgCurve::NAME_SECP_112R1:
74
                return $secpFactory->generator112r1();
75
            case SecgCurve::NAME_SECP_192K1:
76
                return $secpFactory->generator192k1();
77
            case SecgCurve::NAME_SECP_256K1:
78
                return $secpFactory->generator256k1();
79
            case SecgCurve::NAME_SECP_256R1:
80
                return $secpFactory->generator256r1();
81
            case SecgCurve::NAME_SECP_384R1:
82
                return $secpFactory->generator384r1();
83
            default:
84
                $error = new UnsupportedCurveException('Unknown generator.');
85
                $error->setCurveName($name);
86
                throw $error;
87
        }
88
    }
89
90
    /**
91
     * @param GmpMathInterface $math
92
     * @return NistCurve
93
     */
94
    private static function getNistFactory(GmpMathInterface $math): NistCurve
95
    {
96
        return new NistCurve($math);
97
    }
98
99
    /**
100
     * @param GmpMathInterface $math
101
     * @return SecgCurve
102
     */
103
    private static function getSecpFactory(GmpMathInterface $math): SecgCurve
104
    {
105
        return new SecgCurve($math);
106
    }
107
}
108