first commit

This commit is contained in:
annnj-company
2026-04-17 18:29:53 +08:00
parent e49fa5a215
commit 130c1026c4
5615 changed files with 1639145 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace Mdanter\Ecc\Curves;
use Mdanter\Ecc\Exception\UnknownCurveException;
use Mdanter\Ecc\Exception\UnsupportedCurveException;
use Mdanter\Ecc\Math\GmpMathInterface;
use Mdanter\Ecc\Math\MathAdapterFactory;
use Mdanter\Ecc\Primitives\GeneratorPoint;
class CurveFactory
{
/**
* @param string $name
* @return NamedCurveFp
*/
public static function getCurveByName(string $name): NamedCurveFp
{
$adapter = MathAdapterFactory::getAdapter();
$nistFactory = self::getNistFactory($adapter);
$secpFactory = self::getSecpFactory($adapter);
switch ($name) {
case NistCurve::NAME_P192:
return $nistFactory->curve192();
case NistCurve::NAME_P224:
return $nistFactory->curve224();
case NistCurve::NAME_P256:
return $nistFactory->curve256();
case NistCurve::NAME_P384:
return $nistFactory->curve384();
case NistCurve::NAME_P521:
return $nistFactory->curve521();
case SecgCurve::NAME_SECP_112R1:
return $secpFactory->curve112r1();
case SecgCurve::NAME_SECP_192K1:
return $secpFactory->curve192k1();
case SecgCurve::NAME_SECP_256K1:
return $secpFactory->curve256k1();
case SecgCurve::NAME_SECP_256R1:
return $secpFactory->curve256r1();
case SecgCurve::NAME_SECP_384R1:
return $secpFactory->curve384r1();
default:
$error = new UnsupportedCurveException('Unknown curve.');
$error->setCurveName($name);
throw $error;
}
}
/**
* @param string $name
* @return GeneratorPoint
*/
public static function getGeneratorByName(string $name): GeneratorPoint
{
$adapter = MathAdapterFactory::getAdapter();
$nistFactory = self::getNistFactory($adapter);
$secpFactory = self::getSecpFactory($adapter);
switch ($name) {
case NistCurve::NAME_P192:
return $nistFactory->generator192();
case NistCurve::NAME_P224:
return $nistFactory->generator224();
case NistCurve::NAME_P256:
return $nistFactory->generator256();
case NistCurve::NAME_P384:
return $nistFactory->generator384();
case NistCurve::NAME_P521:
return $nistFactory->generator521();
case SecgCurve::NAME_SECP_112R1:
return $secpFactory->generator112r1();
case SecgCurve::NAME_SECP_192K1:
return $secpFactory->generator192k1();
case SecgCurve::NAME_SECP_256K1:
return $secpFactory->generator256k1();
case SecgCurve::NAME_SECP_256R1:
return $secpFactory->generator256r1();
case SecgCurve::NAME_SECP_384R1:
return $secpFactory->generator384r1();
default:
$error = new UnsupportedCurveException('Unknown generator.');
$error->setCurveName($name);
throw $error;
}
}
/**
* @param GmpMathInterface $math
* @return NistCurve
*/
private static function getNistFactory(GmpMathInterface $math): NistCurve
{
return new NistCurve($math);
}
/**
* @param GmpMathInterface $math
* @return SecgCurve
*/
private static function getSecpFactory(GmpMathInterface $math): SecgCurve
{
return new SecgCurve($math);
}
}

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Mdanter\Ecc\Curves;
use Mdanter\Ecc\Math\GmpMathInterface;
use Mdanter\Ecc\Primitives\CurveFp;
use Mdanter\Ecc\Primitives\CurveParameters;
class NamedCurveFp extends CurveFp
{
/**
* @var string
*/
private $name;
/**
* @param string $name
* @param CurveParameters $parameters
* @param GmpMathInterface $adapter
*/
public function __construct(string $name, CurveParameters $parameters, GmpMathInterface $adapter)
{
$this->name = $name;
parent::__construct($parameters, $adapter);
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}

View File

@@ -0,0 +1,221 @@
<?php
declare(strict_types=1);
namespace Mdanter\Ecc\Curves;
use Mdanter\Ecc\Math\GmpMathInterface;
use Mdanter\Ecc\Primitives\CurveParameters;
use Mdanter\Ecc\Primitives\GeneratorPoint;
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
/**
* *********************************************************************
* Copyright (C) 2012 Matyas Danter
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
* ***********************************************************************
*/
/**
* This class encapsulates the NIST recommended curves
* - fields are Mersenne primes, i.e.
* for some p, Mersenne_prine = 2^p - 1
*/
class NistCurve
{
const NAME_P192 = 'nistp192';
const NAME_P224 = 'nistp224';
const NAME_P256 = 'nistp256';
const NAME_P384 = 'nistp384';
const NAME_P521 = 'nistp521';
/**
* @var GmpMathInterface
*/
private $adapter;
/**
* @param GmpMathInterface $adapter
*/
public function __construct(GmpMathInterface $adapter)
{
$this->adapter = $adapter;
}
/**
* Returns an NIST P-192 curve.
*
* @return NamedCurveFp
*/
public function curve192(): NamedCurveFp
{
$p = gmp_init('6277101735386680763835789423207666416083908700390324961279', 10);
$b = gmp_init('64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1', 16);
$parameters = new CurveParameters(192, $p, gmp_init('-3', 10), $b);
return new NamedCurveFp(self::NAME_P192, $parameters, $this->adapter);
}
/**
* Returns an NIST P-192 generator.
*
* @param RandomNumberGeneratorInterface $randomGenerator
* @return GeneratorPoint
*/
public function generator192(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve192();
$order = gmp_init('6277101735386680763835789423176059013767194773182842284081', 10);
$x = gmp_init('188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012', 16);
$y = gmp_init('07192b95ffc8da78631011ed6b24cdd573f977a11e794811', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
/**
* Returns an NIST P-224 curve
*
* @return NamedCurveFp
*/
public function curve224(): NamedCurveFp
{
$p = gmp_init('26959946667150639794667015087019630673557916260026308143510066298881', 10);
$b = gmp_init('b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4', 16);
$parameters = new CurveParameters(224, $p, gmp_init(-3, 10), $b);
return new NamedCurveFp(self::NAME_P224, $parameters, $this->adapter);
}
/**
* Returns an NIST P-224 generator.
*
* @param RandomNumberGeneratorInterface $randomGenerator
* @return GeneratorPoint
*/
public function generator224(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve224();
$order = gmp_init('26959946667150639794667015087019625940457807714424391721682722368061', 10);
$x = gmp_init('b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21', 16);
$y = gmp_init('bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
/**
* Returns an NIST P-256 curve.
*
* @return NamedCurveFp
*/
public function curve256(): NamedCurveFp
{
$p = gmp_init('115792089210356248762697446949407573530086143415290314195533631308867097853951', 10);
$b = gmp_init('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b', 16);
$parameters = new CurveParameters(256, $p, gmp_init(-3, 10), $b);
return new NamedCurveFp(self::NAME_P256, $parameters, $this->adapter);
}
/**
* Returns an NIST P-256 generator.
*
* @param RandomNumberGeneratorInterface $randomGenerator
* @return GeneratorPoint
*/
public function generator256(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve256();
$order = gmp_init('115792089210356248762697446949407573529996955224135760342422259061068512044369', 10);
$x = gmp_init('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296', 16);
$y = gmp_init('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
/**
* Returns an NIST P-384 curve.
*
* @return NamedCurveFp
*/
public function curve384(): NamedCurveFp
{
$p = gmp_init('39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319', 10);
$b = gmp_init('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef', 16);
$parameters = new CurveParameters(384, $p, gmp_init(-3, 10), $b);
return new NamedCurveFp(self::NAME_P384, $parameters, $this->adapter);
}
/**
* Returns an NIST P-384 generator.
*
* @param RandomNumberGeneratorInterface $randomGenerator
* @return GeneratorPoint
*/
public function generator384(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve384();
$order = gmp_init('39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643', 10);
$x = gmp_init('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7', 16);
$y = gmp_init('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
/**
* Returns an NIST P-521 curve.
*
* @return NamedCurveFp
*/
public function curve521(): NamedCurveFp
{
$p = gmp_init('6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151', 10);
$b = gmp_init('0x051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00', 16);
$parameters = new CurveParameters(521, $p, gmp_init(-3, 10), $b);
return new NamedCurveFp(self::NAME_P521, $parameters, $this->adapter);
}
/**
* Returns an NIST P-521 generator.
*
* @param RandomNumberGeneratorInterface $randomGenerator
* @return GeneratorPoint
*/
public function generator521(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve521();
$order = gmp_init('6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449', 10);
$x = gmp_init('0xc6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66', 16);
$y = gmp_init('0x11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
}

View File

@@ -0,0 +1,203 @@
<?php
declare(strict_types=1);
namespace Mdanter\Ecc\Curves;
use Mdanter\Ecc\Math\GmpMathInterface;
use Mdanter\Ecc\Primitives\CurveParameters;
use Mdanter\Ecc\Primitives\GeneratorPoint;
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
/**
* *********************************************************************
* Copyright (C) 2012 Matyas Danter
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
* ***********************************************************************
*/
/**
*
*/
class SecgCurve
{
/**
* @var GmpMathInterface
*/
private $adapter;
const NAME_SECP_112R1 = 'secp112r1';
const NAME_SECP_192K1 = 'secp192k1';
const NAME_SECP_256K1 = 'secp256k1';
const NAME_SECP_256R1 = 'secp256r1';
const NAME_SECP_384R1 = 'secp384r1';
/**
* @param GmpMathInterface $adapter
*/
public function __construct(GmpMathInterface $adapter)
{
$this->adapter = $adapter;
}
/**
* @return NamedCurveFp
*/
public function curve112r1(): NamedCurveFp
{
$p = gmp_init('0xDB7C2ABF62E35E668076BEAD208B', 16);
$a = gmp_init('0xDB7C2ABF62E35E668076BEAD2088', 16);
$b = gmp_init('0x659EF8BA043916EEDE8911702B22', 16);
$parameters = new CurveParameters(112, $p, $a, $b);
return new NamedCurveFp(self::NAME_SECP_112R1, $parameters, $this->adapter);
}
/**
* @param RandomNumberGeneratorInterface $randomGenerator
* @return GeneratorPoint
*/
public function generator112r1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve112r1();
$order = gmp_init('0xDB7C2ABF62E35E7628DFAC6561C5', 16);
$x = gmp_init('0x09487239995A5EE76B55F9C2F098', 16);
$y = gmp_init('0xA89CE5AF8724C0A23E0E0FF77500', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
/**
* @return NamedCurveFp
*/
public function curve192k1(): NamedCurveFp
{
$p = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37', 16);
$a = gmp_init(0, 10);
$b = gmp_init(3, 10);
$parameters = new CurveParameters(192, $p, $a, $b);
return new NamedCurveFp(self::NAME_SECP_192K1, $parameters, $this->adapter);
}
/**
* @param RandomNumberGeneratorInterface $randomGenerator
* @return \Mdanter\Ecc\Primitives\GeneratorPoint
*/
public function generator192k1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve192k1();
$order = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D', 16);
$x = gmp_init('0xDB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D', 16);
$y = gmp_init('0x9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
/**
* @return NamedCurveFp
*/
public function curve256k1(): NamedCurveFp
{
$p = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16);
$a = gmp_init(0, 10);
$b = gmp_init(7, 10);
$parameters = new CurveParameters(256, $p, $a, $b);
return new NamedCurveFp(self::NAME_SECP_256K1, $parameters, $this->adapter);
}
/**
* @param RandomNumberGeneratorInterface $randomGenerator
* @return GeneratorPoint
*/
public function generator256k1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve256k1();
$order = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16);
$x = gmp_init('0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 16);
$y = gmp_init('0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
/**
* @return NamedCurveFp
*/
public function curve256r1(): NamedCurveFp
{
$p = gmp_init('0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF', 16);
$a = gmp_init('0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC', 16);
$b = gmp_init('0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B', 16);
$parameters = new CurveParameters(256, $p, $a, $b);
return new NamedCurveFp(self::NAME_SECP_256R1, $parameters, $this->adapter);
}
/**
* @param RandomNumberGeneratorInterface $randomGenerator
* @return GeneratorPoint
*/
public function generator256r1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve256r1();
$order = gmp_init('0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551', 16);
$x = gmp_init('0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296', 16);
$y = gmp_init('0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
/**
* @return NamedCurveFp
*/
public function curve384r1(): NamedCurveFp
{
$p = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF', 16);
$a = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC', 16);
$b = gmp_init('0xB3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF', 16);
$parameters = new CurveParameters(384, $p, $a, $b);
return new NamedCurveFp(self::NAME_SECP_384R1, $parameters, $this->adapter);
}
/**
* @param RandomNumberGeneratorInterface $randomGenerator
* @return GeneratorPoint
*/
public function generator384r1(RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
{
$curve = $this->curve384r1();
$order = gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973', 16);
$x = gmp_init('0xAA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7', 16);
$y = gmp_init('0x3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F', 16);
return $curve->getGenerator($x, $y, $order, $randomGenerator);
}
}