EccFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 6
c 3
b 0
f 0
dl 0
loc 59
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createCurve() 0 3 2
A getAdapter() 0 3 1
A getSigner() 0 3 2
A getNistCurves() 0 3 2
A getSecgCurves() 0 3 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Mdanter\Ecc;
5
6
use Mdanter\Ecc\Crypto\Signature\Signer;
7
use Mdanter\Ecc\Curves\NistCurve;
8
use Mdanter\Ecc\Curves\SecgCurve;
9
use Mdanter\Ecc\Math\GmpMathInterface;
10
use Mdanter\Ecc\Math\MathAdapterFactory;
11
use Mdanter\Ecc\Primitives\CurveFp;
12
use Mdanter\Ecc\Primitives\CurveFpInterface;
13
use Mdanter\Ecc\Primitives\CurveParameters;
14
15
/**
16
 * Static factory class providing factory methods to work with NIST and SECG recommended curves.
17
 */
18
class EccFactory
19
{
20
    /**
21
     * Selects and creates the most appropriate adapter for the running environment.
22
     *
23
     * @param bool $debug [optional] Set to true to get a trace of all mathematical operations
24
     *
25
     * @throws \RuntimeException
26
     * @return GmpMathInterface
27
     */
28
    public static function getAdapter(bool $debug = false): GmpMathInterface
29
    {
30
        return MathAdapterFactory::getAdapter($debug);
31
    }
32
33
    /**
34
     * Returns a factory to create NIST Recommended curves and generators.
35
     *
36
     * @param  GmpMathInterface $adapter [optional] Defaults to the return value of EccFactory::getAdapter().
37
     * @return NistCurve
38
     */
39
    public static function getNistCurves(GmpMathInterface $adapter = null): NistCurve
40
    {
41
        return new NistCurve($adapter ?: self::getAdapter());
42
    }
43
44
    /**
45
     * Returns a factory to return SECG Recommended curves and generators.
46
     *
47
     * @param  GmpMathInterface $adapter [optional] Defaults to the return value of EccFactory::getAdapter().
48
     * @return SecgCurve
49
     */
50
    public static function getSecgCurves(GmpMathInterface $adapter = null): SecgCurve
51
    {
52
        return new SecgCurve($adapter ?: self::getAdapter());
53
    }
54
55
    /**
56
     * Creates a new curve from arbitrary parameters.
57
     *
58
     * @param  int              $bitSize
59
     * @param  \GMP             $prime
60
     * @param  \GMP             $a
61
     * @param  \GMP             $b
62
     * @param  GmpMathInterface $adapter [optional] Defaults to the return value of EccFactory::getAdapter().
63
     * @return CurveFpInterface
64
     */
65
    public static function createCurve(int $bitSize, \GMP $prime, \GMP $a, \GMP $b, GmpMathInterface $adapter = null): CurveFpInterface
66
    {
67
        return new CurveFp(new CurveParameters($bitSize, $prime, $a, $b), $adapter ?: self::getAdapter());
68
    }
69
70
    /**
71
     * @param  GmpMathInterface $adapter [optional] Defaults to the return value of EccFactory::getAdapteR()
72
     * @return Signer
73
     */
74
    public static function getSigner(GmpMathInterface $adapter = null): Signer
75
    {
76
        return new Signer($adapter ?: self::getAdapter());
77
    }
78
}
79