• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

famoser / elliptic / 16238255757

12 Jul 2025 01:00PM UTC coverage: 84.857% (-6.3%) from 91.134%
16238255757

Pull #12

github

web-flow
Merge 97611838a into 5ce1049c0
Pull Request #12: WIP: Add montgomery

270 of 376 new or added lines in 17 files covered. (71.81%)

2 existing lines in 1 file now uncovered.

947 of 1116 relevant lines covered (84.86%)

23.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.68
/src/Math/Calculator/Adder/MGUnsafeAdder.php
1
<?php
2

3
/** @noinspection DuplicatedCode */
4

5
namespace Famoser\Elliptic\Math\Calculator\Adder;
6

7
use Famoser\Elliptic\Primitives\Point;
8

9
/**
10
 * Implements algorithms proposed in https://www.hyperelliptic.org/EFD/g1p/auto-montgom.html
11
 * Merged with rules for edge-cases from https://www.secg.org/SEC1-Ver-1.0.pdf (2.2.1)
12
 */
13
trait MGUnsafeAdder
14
{
15
    public function add(Point $a, Point $b): Point
3✔
16
    {
17
        // rule 1 & 2
18
        if ($a->isInfinity()) {
3✔
19
            return clone $b;
2✔
20
        } elseif ($b->isInfinity()) {
3✔
21
            return clone $a;
2✔
22
        }
23

24
        if (gmp_cmp($a->x, $b->x) === 0) {
3✔
25
            // rule 3
26
            if (gmp_cmp($b->y, $a->y) !== 0) {
1✔
NEW
27
                return Point::createInfinity();
×
28
            }
29

30
            // rule 5
31
            return $this->double($a);
1✔
32
        }
33

34
        // rule 4 (note that a / b = a * b^-1)
35
        $lambda = $this->field->mul(
2✔
36
            gmp_sub($b->y, $a->y),
2✔
37
            /** @phpstan-ignore-next-line invert may return false; then this will crash (which is OK, because cannot recover anyway) */
38
            $this->field->invert(gmp_sub($b->x, $a->x))
2✔
39
        );
2✔
40

41
        $x = $this->field->sub(
2✔
42
            gmp_sub(
2✔
43
                gmp_sub(
2✔
44
                    gmp_mul(
2✔
45
                        $this->curve->getB(),
2✔
46
                        gmp_pow($lambda, 2)
2✔
47
                    ),
2✔
48
                    $this->curve->getA()
2✔
49
                ),
2✔
50
                $a->x
2✔
51
            ),
2✔
52
            $b->x
2✔
53
        );
2✔
54

55
        $y = $this->field->sub(
2✔
56
            gmp_mul(
2✔
57
                $lambda,
2✔
58
                gmp_sub($a->x, $x)
2✔
59
            ),
2✔
60
            $a->y
2✔
61
        );
2✔
62

63
        return new Point($x, $y);
2✔
64
    }
65

66
    public function double(Point $a): Point
4✔
67
    {
68
        if (gmp_cmp($a->y, 0) === 0) {
4✔
69
            return Point::createInfinity();
2✔
70
        }
71

72
        // rule 5 (note that a / b = a * b^-1)
73
        $lambda = $this->field->mul(
4✔
74
            gmp_add(
4✔
75
                gmp_add(
4✔
76
                    gmp_mul(
4✔
77
                        gmp_init(3),
4✔
78
                        gmp_pow($a->x, 2)
4✔
79
                    ),
4✔
80
                    gmp_mul(
4✔
81
                        gmp_mul(2, $this->curve->getA()),
4✔
82
                        $a->x
4✔
83
                    )
4✔
84
                ),
4✔
85
                1
4✔
86
            ),
4✔
87
            /** @phpstan-ignore-next-line invert may return false; then this will crash (which is OK, because cannot recover anyway) */
88
            $this->field->invert(
4✔
89
                gmp_mul(
4✔
90
                    gmp_mul(2, $this->curve->getB()),
4✔
91
                    $a->y
4✔
92
                )
4✔
93
            )
4✔
94
        );
4✔
95

96
        $x = $this->field->sub(
4✔
97
            gmp_sub(
4✔
98
                gmp_mul(
4✔
99
                    $this->curve->getB(),
4✔
100
                    gmp_pow($lambda, 2)
4✔
101
                ),
4✔
102
                $this->curve->getA()
4✔
103
            ),
4✔
104
            gmp_mul(2, $a->x)
4✔
105
        );
4✔
106

107
        $y = $this->field->sub(
4✔
108
            gmp_mul(
4✔
109
                $lambda,
4✔
110
                gmp_sub($a->x, $x)
4✔
111
            ),
4✔
112
            $a->y
4✔
113
        );
4✔
114

115
        return new Point($x, $y);
4✔
116
    }
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc