1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Test\PNServer; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use SKien\PNServer\Utils\Curve; |
8
|
|
|
use SKien\PNServer\Utils\NistCurve; |
9
|
|
|
use SKien\PNServer\Utils\Point; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Stefanius <[email protected]> |
13
|
|
|
* @copyright MIT License - see the LICENSE file for details |
14
|
|
|
*/ |
15
|
|
|
class UtilsCurveTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
const VALID_X = '47106871675546646998941975368965491997260522375820007229838508869987366040004'; |
18
|
|
|
const VALID_Y = '52376802520912566842951846993469970345548560339531881043822207040165842947197'; |
19
|
|
|
const VALID_2X = '98427826524936846061109340525176511327555564326016491717443420300780106750337'; |
20
|
|
|
const VALID_2Y = '84323609886562361330352263233225787919964540102923882392673776457782612785828'; |
21
|
|
|
const CURVE256 = 'curve(115792089210356248762697446949407573530086143415290314195533631308867097853948, 41058363725152142129326129780047268409114441015993725554835256314039467401291, 115792089210356248762697446949407573530086143415290314195533631308867097853951)'; |
22
|
|
|
|
23
|
|
|
protected Curve $cv; |
24
|
|
|
|
25
|
|
|
public function setUp() : void |
26
|
|
|
{ |
27
|
|
|
$this->cv = NistCurve::curve256(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function test_create() : void |
31
|
|
|
{ |
32
|
|
|
$this->assertEquals($this->cv->getSize(), 256); |
33
|
|
|
$this->assertEquals((int) $this->cv->getA(), 9223372036854775804); |
34
|
|
|
$this->assertEquals((int) $this->cv->getB(), 4309448131093880907); |
35
|
|
|
$this->assertEquals((int) $this->cv->getPrime(), 9223372036854775807); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function test_getPoint() : void |
39
|
|
|
{ |
40
|
|
|
$pt = $this->cv->getPoint(gmp_init(self::VALID_X), gmp_init(self::VALID_Y)); |
41
|
|
|
$this->assertEquals(self::VALID_X, gmp_strval($pt->getX())); |
42
|
|
|
$this->assertEquals(self::VALID_Y, gmp_strval($pt->getY())); |
43
|
|
|
|
44
|
|
|
// don't realy know what affect the $order param should bring... |
45
|
|
|
$pt = $this->cv->getPoint(gmp_init(self::VALID_X), gmp_init(self::VALID_Y), gmp_init('23874367496743855')); |
46
|
|
|
$this->assertEquals(self::VALID_X, gmp_strval($pt->getX())); |
47
|
|
|
$this->assertEquals(self::VALID_Y, gmp_strval($pt->getY())); |
48
|
|
|
|
49
|
|
|
$this->expectException('RuntimeException'); |
50
|
|
|
$pt = $this->cv->getPoint(gmp_init(0), gmp_init(0)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function test_getPublicKeyFrom() : void |
54
|
|
|
{ |
55
|
|
|
$pt = $this->cv->getPublicKeyFrom(gmp_init(self::VALID_X), gmp_init(self::VALID_Y)); |
56
|
|
|
$this->assertEquals(self::VALID_X, gmp_strval($pt->getX())); |
57
|
|
|
$this->assertEquals(self::VALID_Y, gmp_strval($pt->getY())); |
58
|
|
|
|
59
|
|
|
$this->expectException('RuntimeException'); |
60
|
|
|
$pt = $this->cv->getPublicKeyFrom(gmp_init(-1), gmp_init(0)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function test_contains() : void |
64
|
|
|
{ |
65
|
|
|
$this->assertTrue($this->cv->contains(gmp_init(self::VALID_X), gmp_init(self::VALID_Y))); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function test_add() : void |
69
|
|
|
{ |
70
|
|
|
$pt1 = Point::create(gmp_init(10), gmp_init(20)); |
71
|
|
|
$pt2 = Point::create(gmp_init(10), gmp_init(40)); |
72
|
|
|
// result must be $pt1 |
73
|
|
|
$pt = $this->cv->add($pt1, Point::infinity()); |
74
|
|
|
$this->assertSame((int) $pt1->getX(), (int) $pt->getX()); |
75
|
|
|
$this->assertSame((int) $pt1->getY(), (int) $pt->getY()); |
76
|
|
|
// result must be $pt2 |
77
|
|
|
$pt = $this->cv->add(Point::infinity(), $pt2); |
78
|
|
|
$this->assertSame((int) $pt2->getX(), (int) $pt->getX()); |
79
|
|
|
$this->assertSame((int) $pt2->getY(), (int) $pt->getY()); |
80
|
|
|
// result must be infinity |
81
|
|
|
$pt = $this->cv->add($pt1, $pt2); |
82
|
|
|
$this->assertTrue($pt->isInfinity()); |
83
|
|
|
|
84
|
|
|
$pt = $this->cv->add(Point::create(gmp_init(self::VALID_X), gmp_init(self::VALID_Y)), Point::create(gmp_init(self::VALID_X), gmp_init(self::VALID_Y))); |
85
|
|
|
$this->assertEquals(self::VALID_2X, gmp_strval($pt->getX())); |
86
|
|
|
$this->assertEquals(self::VALID_2Y, gmp_strval($pt->getY())); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function test_mul() : void |
90
|
|
|
{ |
91
|
|
|
$pt = $this->cv->mul(Point::create(gmp_init(self::VALID_X), gmp_init(self::VALID_Y)), gmp_init(2)); |
92
|
|
|
$this->assertEquals(self::VALID_2X, gmp_strval($pt->getX())); |
93
|
|
|
$this->assertEquals(self::VALID_2Y, gmp_strval($pt->getY())); |
94
|
|
|
$pt = $this->cv->mul(Point::infinity(), gmp_init(2)); |
95
|
|
|
$this->assertTrue($pt->isInfinity()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function test_toString() : void |
99
|
|
|
{ |
100
|
|
|
$this->assertEquals(self::CURVE256, $this->cv->__toString()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function test_getDouble() : void |
104
|
|
|
{ |
105
|
|
|
$pt = $this->cv->getDouble(Point::create(gmp_init(self::VALID_X), gmp_init(self::VALID_Y))); |
106
|
|
|
$this->assertEquals(self::VALID_2X, gmp_strval($pt->getX())); |
107
|
|
|
$this->assertEquals(self::VALID_2Y, gmp_strval($pt->getY())); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|