Passed
Branch master (c73d10)
by Stefan
02:51 queued 55s
created

UtilsCurveTest::test_create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SKien\Test\PNServer;
5
6
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase 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 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));
0 ignored issues
show
Bug introduced by
It seems like gmp_init(self::VALID_Y) can also be of type resource; however, parameter $y of SKien\PNServer\Utils\Curve::getPoint() 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 ignore-type  annotation

40
        $pt = $this->cv->getPoint(gmp_init(self::VALID_X), /** @scrutinizer ignore-type */ gmp_init(self::VALID_Y));
Loading history...
Bug introduced by
It seems like gmp_init(self::VALID_X) can also be of type resource; however, parameter $x of SKien\PNServer\Utils\Curve::getPoint() 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 ignore-type  annotation

40
        $pt = $this->cv->getPoint(/** @scrutinizer ignore-type */ gmp_init(self::VALID_X), gmp_init(self::VALID_Y));
Loading history...
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'));
0 ignored issues
show
Bug introduced by
It seems like gmp_init('23874367496743855') can also be of type resource; however, parameter $order of SKien\PNServer\Utils\Curve::getPoint() does only seem to accept GMP|null, 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 ignore-type  annotation

45
        $pt = $this->cv->getPoint(gmp_init(self::VALID_X), gmp_init(self::VALID_Y), /** @scrutinizer ignore-type */ gmp_init('23874367496743855'));
Loading history...
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));
0 ignored issues
show
Unused Code introduced by
The assignment to $pt is dead and can be removed.
Loading history...
51
    }
52
    
53
    public function test_getPublicKeyFrom() : void
54
    {
55
        $pt = $this->cv->getPublicKeyFrom(gmp_init(self::VALID_X), gmp_init(self::VALID_Y));
0 ignored issues
show
Bug introduced by
It seems like gmp_init(self::VALID_Y) can also be of type resource; however, parameter $y of SKien\PNServer\Utils\Curve::getPublicKeyFrom() 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 ignore-type  annotation

55
        $pt = $this->cv->getPublicKeyFrom(gmp_init(self::VALID_X), /** @scrutinizer ignore-type */ gmp_init(self::VALID_Y));
Loading history...
Bug introduced by
It seems like gmp_init(self::VALID_X) can also be of type resource; however, parameter $x of SKien\PNServer\Utils\Curve::getPublicKeyFrom() 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 ignore-type  annotation

55
        $pt = $this->cv->getPublicKeyFrom(/** @scrutinizer ignore-type */ gmp_init(self::VALID_X), gmp_init(self::VALID_Y));
Loading history...
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));
0 ignored issues
show
Unused Code introduced by
The assignment to $pt is dead and can be removed.
Loading history...
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)));
0 ignored issues
show
Bug introduced by
It seems like gmp_init(self::VALID_Y) can also be of type resource; however, parameter $y of SKien\PNServer\Utils\Curve::contains() 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 ignore-type  annotation

65
        $this->assertTrue($this->cv->contains(gmp_init(self::VALID_X), /** @scrutinizer ignore-type */ gmp_init(self::VALID_Y)));
Loading history...
Bug introduced by
It seems like gmp_init(self::VALID_X) can also be of type resource; however, parameter $x of SKien\PNServer\Utils\Curve::contains() 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 ignore-type  annotation

65
        $this->assertTrue($this->cv->contains(/** @scrutinizer ignore-type */ gmp_init(self::VALID_X), gmp_init(self::VALID_Y)));
Loading history...
66
    }
67
    
68
    public function test_add() : void
69
    {
70
        $pt1 = Point::create(gmp_init(10), gmp_init(20));
0 ignored issues
show
Bug introduced by
It seems like gmp_init(20) can also be of type resource; however, parameter $y of SKien\PNServer\Utils\Point::create() 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 ignore-type  annotation

70
        $pt1 = Point::create(gmp_init(10), /** @scrutinizer ignore-type */ gmp_init(20));
Loading history...
Bug introduced by
It seems like gmp_init(10) can also be of type resource; however, parameter $x of SKien\PNServer\Utils\Point::create() 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 ignore-type  annotation

70
        $pt1 = Point::create(/** @scrutinizer ignore-type */ gmp_init(10), gmp_init(20));
Loading history...
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));
0 ignored issues
show
Bug introduced by
It seems like gmp_init(2) can also be of type resource; however, parameter $n of SKien\PNServer\Utils\Curve::mul() 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 ignore-type  annotation

91
        $pt = $this->cv->mul(Point::create(gmp_init(self::VALID_X), gmp_init(self::VALID_Y)), /** @scrutinizer ignore-type */ gmp_init(2));
Loading history...
Bug introduced by
It seems like gmp_init(self::VALID_X) can also be of type resource; however, parameter $x of SKien\PNServer\Utils\Point::create() 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 ignore-type  annotation

91
        $pt = $this->cv->mul(Point::create(/** @scrutinizer ignore-type */ gmp_init(self::VALID_X), gmp_init(self::VALID_Y)), gmp_init(2));
Loading history...
Bug introduced by
It seems like gmp_init(self::VALID_Y) can also be of type resource; however, parameter $y of SKien\PNServer\Utils\Point::create() 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 ignore-type  annotation

91
        $pt = $this->cv->mul(Point::create(gmp_init(self::VALID_X), /** @scrutinizer ignore-type */ gmp_init(self::VALID_Y)), gmp_init(2));
Loading history...
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)));
0 ignored issues
show
Bug introduced by
It seems like gmp_init(self::VALID_X) can also be of type resource; however, parameter $x of SKien\PNServer\Utils\Point::create() 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 ignore-type  annotation

105
        $pt = $this->cv->getDouble(Point::create(/** @scrutinizer ignore-type */ gmp_init(self::VALID_X), gmp_init(self::VALID_Y)));
Loading history...
Bug introduced by
It seems like gmp_init(self::VALID_Y) can also be of type resource; however, parameter $y of SKien\PNServer\Utils\Point::create() 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 ignore-type  annotation

105
        $pt = $this->cv->getDouble(Point::create(gmp_init(self::VALID_X), /** @scrutinizer ignore-type */ gmp_init(self::VALID_Y)));
Loading history...
106
        $this->assertEquals(self::VALID_2X, gmp_strval($pt->getX()));
107
        $this->assertEquals(self::VALID_2Y, gmp_strval($pt->getY()));
108
    }
109
}
110
111