shanecurran /
phpecc
| 1 | <?php |
||||||||
| 2 | declare(strict_types=1); |
||||||||
| 3 | |||||||||
| 4 | namespace Mdanter\Ecc\Curves; |
||||||||
| 5 | |||||||||
| 6 | use Mdanter\Ecc\Math\GmpMathInterface; |
||||||||
| 7 | use Mdanter\Ecc\Primitives\CurveParameters; |
||||||||
| 8 | use Mdanter\Ecc\Primitives\GeneratorPoint; |
||||||||
| 9 | use Mdanter\Ecc\Random\RandomNumberGeneratorInterface; |
||||||||
| 10 | |||||||||
| 11 | /** |
||||||||
| 12 | * ********************************************************************* |
||||||||
| 13 | * Copyright (C) 2012 Matyas Danter |
||||||||
| 14 | * |
||||||||
| 15 | * Permission is hereby granted, free of charge, to any person obtaining |
||||||||
| 16 | * a copy of this software and associated documentation files (the "Software"), |
||||||||
| 17 | * to deal in the Software without restriction, including without limitation |
||||||||
| 18 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||||||
| 19 | * and/or sell copies of the Software, and to permit persons to whom the |
||||||||
| 20 | * Software is furnished to do so, subject to the following conditions: |
||||||||
| 21 | * |
||||||||
| 22 | * The above copyright notice and this permission notice shall be included |
||||||||
| 23 | * in all copies or substantial portions of the Software. |
||||||||
| 24 | * |
||||||||
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
||||||||
| 26 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||||
| 27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||||||
| 28 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
||||||||
| 29 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
||||||||
| 30 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
||||||||
| 31 | * OTHER DEALINGS IN THE SOFTWARE. |
||||||||
| 32 | * *********************************************************************** |
||||||||
| 33 | */ |
||||||||
| 34 | |||||||||
| 35 | /** |
||||||||
| 36 | * |
||||||||
| 37 | */ |
||||||||
| 38 | class SecgCurve |
||||||||
| 39 | { |
||||||||
| 40 | /** |
||||||||
| 41 | * @var GmpMathInterface |
||||||||
| 42 | */ |
||||||||
| 43 | private $adapter; |
||||||||
| 44 | |||||||||
| 45 | const NAME_SECP_112R1 = 'secp112r1'; |
||||||||
| 46 | const NAME_SECP_192K1 = 'secp192k1'; |
||||||||
| 47 | const NAME_SECP_256K1 = 'secp256k1'; |
||||||||
| 48 | const NAME_SECP_256R1 = 'secp256r1'; |
||||||||
| 49 | const NAME_SECP_384R1 = 'secp384r1'; |
||||||||
| 50 | |||||||||
| 51 | /** |
||||||||
| 52 | * @param GmpMathInterface $adapter |
||||||||
| 53 | */ |
||||||||
| 54 | public function __construct(GmpMathInterface $adapter) |
||||||||
| 55 | { |
||||||||
| 56 | $this->adapter = $adapter; |
||||||||
| 57 | } |
||||||||
| 58 | |||||||||
| 59 | /** |
||||||||
| 60 | * @return NamedCurveFp |
||||||||
| 61 | */ |
||||||||
| 62 | public function curve112r1(): NamedCurveFp |
||||||||
| 63 | { |
||||||||
| 64 | $p = gmp_init('0xDB7C2ABF62E35E668076BEAD208B', 16); |
||||||||
| 65 | $a = gmp_init('0xDB7C2ABF62E35E668076BEAD2088', 16); |
||||||||
| 66 | $b = gmp_init('0x659EF8BA043916EEDE8911702B22', 16); |
||||||||
| 67 | |||||||||
| 68 | $parameters = new CurveParameters(112, $p, $a, $b); |
||||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
$b can also be of type resource; however, parameter $b of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
It seems like
$p can also be of type resource; however, parameter $prime of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
|
|||||||||
| 69 | |||||||||
| 70 | return new NamedCurveFp(self::NAME_SECP_112R1, $parameters, $this->adapter); |
||||||||
| 71 | } |
||||||||
| 72 | |||||||||
| 73 | /** |
||||||||
| 74 | * @param RandomNumberGeneratorInterface $randomGenerator |
||||||||
| 75 | * @return GeneratorPoint |
||||||||
| 76 | */ |
||||||||
| 77 | public function generator112r1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint |
||||||||
| 78 | { |
||||||||
| 79 | $curve = $this->curve112r1(); |
||||||||
| 80 | |||||||||
| 81 | $order = gmp_init('0xDB7C2ABF62E35E7628DFAC6561C5', 16); |
||||||||
| 82 | $x = gmp_init('0x09487239995A5EE76B55F9C2F098', 16); |
||||||||
| 83 | $y = gmp_init('0xA89CE5AF8724C0A23E0E0FF77500', 16); |
||||||||
| 84 | |||||||||
| 85 | return $curve->getGenerator($x, $y, $order, $randomGenerator); |
||||||||
|
0 ignored issues
–
show
It seems like
$order can also be of type resource; however, parameter $order of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$x can also be of type resource; however, parameter $x of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$y can also be of type resource; however, parameter $y of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
|
|||||||||
| 86 | } |
||||||||
| 87 | |||||||||
| 88 | /** |
||||||||
| 89 | * @return NamedCurveFp |
||||||||
| 90 | */ |
||||||||
| 91 | public function curve192k1(): NamedCurveFp |
||||||||
| 92 | { |
||||||||
| 93 | $p = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37', 16); |
||||||||
| 94 | $a = gmp_init(0, 10); |
||||||||
| 95 | $b = gmp_init(3, 10); |
||||||||
| 96 | |||||||||
| 97 | $parameters = new CurveParameters(192, $p, $a, $b); |
||||||||
|
0 ignored issues
–
show
It seems like
$b can also be of type resource; however, parameter $b of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
It seems like
$p can also be of type resource; however, parameter $prime of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
It seems like
$a can also be of type resource; however, parameter $a of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
|
|||||||||
| 98 | |||||||||
| 99 | return new NamedCurveFp(self::NAME_SECP_192K1, $parameters, $this->adapter); |
||||||||
| 100 | } |
||||||||
| 101 | |||||||||
| 102 | /** |
||||||||
| 103 | * @param RandomNumberGeneratorInterface $randomGenerator |
||||||||
| 104 | * @return \Mdanter\Ecc\Primitives\GeneratorPoint |
||||||||
| 105 | */ |
||||||||
| 106 | public function generator192k1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint |
||||||||
| 107 | { |
||||||||
| 108 | $curve = $this->curve192k1(); |
||||||||
| 109 | |||||||||
| 110 | $order = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D', 16); |
||||||||
| 111 | $x = gmp_init('0xDB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D', 16); |
||||||||
| 112 | $y = gmp_init('0x9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D', 16); |
||||||||
| 113 | |||||||||
| 114 | return $curve->getGenerator($x, $y, $order, $randomGenerator); |
||||||||
|
0 ignored issues
–
show
It seems like
$order can also be of type resource; however, parameter $order of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$x can also be of type resource; however, parameter $x of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$y can also be of type resource; however, parameter $y of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
|
|||||||||
| 115 | } |
||||||||
| 116 | |||||||||
| 117 | /** |
||||||||
| 118 | * @return NamedCurveFp |
||||||||
| 119 | */ |
||||||||
| 120 | public function curve256k1(): NamedCurveFp |
||||||||
| 121 | { |
||||||||
| 122 | $p = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16); |
||||||||
| 123 | $a = gmp_init(0, 10); |
||||||||
| 124 | $b = gmp_init(7, 10); |
||||||||
| 125 | |||||||||
| 126 | $parameters = new CurveParameters(256, $p, $a, $b); |
||||||||
|
0 ignored issues
–
show
It seems like
$p can also be of type resource; however, parameter $prime of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
It seems like
$a can also be of type resource; however, parameter $a of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
It seems like
$b can also be of type resource; however, parameter $b of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
|
|||||||||
| 127 | |||||||||
| 128 | return new NamedCurveFp(self::NAME_SECP_256K1, $parameters, $this->adapter); |
||||||||
| 129 | } |
||||||||
| 130 | |||||||||
| 131 | /** |
||||||||
| 132 | * @param RandomNumberGeneratorInterface $randomGenerator |
||||||||
| 133 | * @return GeneratorPoint |
||||||||
| 134 | */ |
||||||||
| 135 | public function generator256k1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint |
||||||||
| 136 | { |
||||||||
| 137 | $curve = $this->curve256k1(); |
||||||||
| 138 | |||||||||
| 139 | $order = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16); |
||||||||
| 140 | $x = gmp_init('0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 16); |
||||||||
| 141 | $y = gmp_init('0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8', 16); |
||||||||
| 142 | |||||||||
| 143 | return $curve->getGenerator($x, $y, $order, $randomGenerator); |
||||||||
|
0 ignored issues
–
show
It seems like
$order can also be of type resource; however, parameter $order of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$x can also be of type resource; however, parameter $x of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$y can also be of type resource; however, parameter $y of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
|
|||||||||
| 144 | } |
||||||||
| 145 | |||||||||
| 146 | /** |
||||||||
| 147 | * @return NamedCurveFp |
||||||||
| 148 | */ |
||||||||
| 149 | public function curve256r1(): NamedCurveFp |
||||||||
| 150 | { |
||||||||
| 151 | $p = gmp_init('0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF', 16); |
||||||||
| 152 | $a = gmp_init('0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC', 16); |
||||||||
| 153 | $b = gmp_init('0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B', 16); |
||||||||
| 154 | |||||||||
| 155 | $parameters = new CurveParameters(256, $p, $a, $b); |
||||||||
|
0 ignored issues
–
show
It seems like
$b can also be of type resource; however, parameter $b of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
It seems like
$p can also be of type resource; however, parameter $prime of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
It seems like
$a can also be of type resource; however, parameter $a of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
|
|||||||||
| 156 | |||||||||
| 157 | return new NamedCurveFp(self::NAME_SECP_256R1, $parameters, $this->adapter); |
||||||||
| 158 | } |
||||||||
| 159 | |||||||||
| 160 | /** |
||||||||
| 161 | * @param RandomNumberGeneratorInterface $randomGenerator |
||||||||
| 162 | * @return GeneratorPoint |
||||||||
| 163 | */ |
||||||||
| 164 | public function generator256r1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint |
||||||||
| 165 | { |
||||||||
| 166 | $curve = $this->curve256r1(); |
||||||||
| 167 | |||||||||
| 168 | $order = gmp_init('0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551', 16); |
||||||||
| 169 | $x = gmp_init('0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296', 16); |
||||||||
| 170 | $y = gmp_init('0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5', 16); |
||||||||
| 171 | |||||||||
| 172 | return $curve->getGenerator($x, $y, $order, $randomGenerator); |
||||||||
|
0 ignored issues
–
show
It seems like
$order can also be of type resource; however, parameter $order of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$y can also be of type resource; however, parameter $y of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$x can also be of type resource; however, parameter $x of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
|
|||||||||
| 173 | } |
||||||||
| 174 | |||||||||
| 175 | /** |
||||||||
| 176 | * @return NamedCurveFp |
||||||||
| 177 | */ |
||||||||
| 178 | public function curve384r1(): NamedCurveFp |
||||||||
| 179 | { |
||||||||
| 180 | $p = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF', 16); |
||||||||
| 181 | $a = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC', 16); |
||||||||
| 182 | $b = gmp_init('0xB3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF', 16); |
||||||||
| 183 | |||||||||
| 184 | $parameters = new CurveParameters(384, $p, $a, $b); |
||||||||
|
0 ignored issues
–
show
It seems like
$a can also be of type resource; however, parameter $a of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
It seems like
$b can also be of type resource; however, parameter $b of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
It seems like
$p can also be of type resource; however, parameter $prime of Mdanter\Ecc\Primitives\C...rameters::__construct() 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
Loading history...
|
|||||||||
| 185 | |||||||||
| 186 | return new NamedCurveFp(self::NAME_SECP_384R1, $parameters, $this->adapter); |
||||||||
| 187 | } |
||||||||
| 188 | |||||||||
| 189 | /** |
||||||||
| 190 | * @param RandomNumberGeneratorInterface $randomGenerator |
||||||||
| 191 | * @return GeneratorPoint |
||||||||
| 192 | */ |
||||||||
| 193 | public function generator384r1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint |
||||||||
| 194 | { |
||||||||
| 195 | $curve = $this->curve384r1(); |
||||||||
| 196 | |||||||||
| 197 | $order = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973', 16); |
||||||||
| 198 | $x = gmp_init('0xAA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7', 16); |
||||||||
| 199 | $y = gmp_init('0x3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F', 16); |
||||||||
| 200 | |||||||||
| 201 | return $curve->getGenerator($x, $y, $order, $randomGenerator); |
||||||||
|
0 ignored issues
–
show
It seems like
$order can also be of type resource; however, parameter $order of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$x can also be of type resource; however, parameter $x of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
It seems like
$y can also be of type resource; however, parameter $y of Mdanter\Ecc\Primitives\CurveFp::getGenerator() 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
Loading history...
|
|||||||||
| 202 | } |
||||||||
| 203 | } |
||||||||
| 204 |