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

famoser / elliptic / 16578839410

28 Jul 2025 07:51PM UTC coverage: 96.985% (+1.4%) from 95.58%
16578839410

Pull #15

github

web-flow
Merge 4e78e6e8a into d917b1701
Pull Request #15: Improve bernstein math

274 of 279 new or added lines in 32 files covered. (98.21%)

35 existing lines in 3 files now uncovered.

1383 of 1426 relevant lines covered (96.98%)

8.58 hits per line

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

0.0
/src/Math/Calculator/MGXCalculator.php
1
<?php
2

3
namespace Famoser\Elliptic\Math\Calculator;
4

5
use Famoser\Elliptic\Math\Calculator\Swapper\ScalarSwapper;
6

7
/**
8
 * implements https://datatracker.ietf.org/doc/html/rfc7748#section-5
9
 */
10
class MGXCalculator extends AbstractCalculator
11
{
12
    use ScalarSwapper;
13

UNCOV
14
    public function mul(\GMP $u, \GMP $factor): \GMP
×
15
    {
16
        // normalize to the element bit length to always execute the double-add loop a constant number of times
UNCOV
17
        $factorBits = gmp_strval($factor, 2);
×
UNCOV
18
        $normalizedFactorBits = str_pad($factorBits, $this->field->getElementBitLength(), '0', STR_PAD_LEFT);
×
19

20
        // precompute constants
UNCOV
21
        $a24 = gmp_div(gmp_sub($this->curve->getA(), 2), 4);
×
UNCOV
22
        $p2 = gmp_sub($this->curve->getP(), 2);
×
23

UNCOV
24
        $x1 = $u;
×
UNCOV
25
        $x2 = gmp_init(1);
×
UNCOV
26
        $z2 = gmp_init(0);
×
UNCOV
27
        $x3 = $u;
×
UNCOV
28
        $z3 = gmp_init(1);
×
UNCOV
29
        $swap = 0;
×
UNCOV
30
        for ($i = 0; $i < $this->field->getElementBitLength(); $i++) {
×
UNCOV
31
            $swap ^= (int)$normalizedFactorBits[$i];
×
UNCOV
32
            $this->conditionalSwapScalar($x2, $x3, $swap, $this->field->getElementBitLength());
×
UNCOV
33
            $this->conditionalSwapScalar($z2, $z3, $swap, $this->field->getElementBitLength());
×
UNCOV
34
            $swap = (int)$normalizedFactorBits[$i];
×
35

UNCOV
36
            $A = $this->field->add($x2, $z2);
×
NEW
37
            $AA = $this->field->sq($A);
×
UNCOV
38
            $B = $this->field->sub($x2, $z2);
×
NEW
39
            $BB = $this->field->sq($B);
×
UNCOV
40
            $E = $this->field->sub($AA, $BB);
×
UNCOV
41
            $C = $this->field->add($x3, $z3);
×
UNCOV
42
            $D = $this->field->sub($x3, $z3);
×
UNCOV
43
            $DA = $this->field->mul($D, $A);
×
UNCOV
44
            $CB = $this->field->mul($C, $B);
×
NEW
45
            $x3 = $this->field->sq(gmp_add($DA, $CB));
×
NEW
46
            $z3 = $this->field->mul($x1, $this->field->sq($this->field->sub($DA, $CB)));
×
UNCOV
47
            $x2 = $this->field->mul($AA, $BB);
×
UNCOV
48
            $z2 = $this->field->mul($E, $this->field->add($AA, $this->field->mul($a24, $E)));
×
49
        }
50

UNCOV
51
        $this->conditionalSwapScalar($x2, $x3, $swap, $this->field->getElementBitLength());
×
UNCOV
52
        $this->conditionalSwapScalar($z2, $z3, $swap, $this->field->getElementBitLength());
×
53

UNCOV
54
        return $this->field->mul($x2, gmp_powm($z2, $p2, $this->curve->getP()));
×
55
    }
56
}
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