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

famoser / elliptic / 16238145859

12 Jul 2025 12:46PM UTC coverage: 84.857% (-6.3%) from 91.134%
16238145859

Pull #12

github

web-flow
Merge fe4ab825d 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

0.0
/src/Serializer/Decoder/RFC7784Decoder.php
1
<?php
2

3
namespace Famoser\Elliptic\Serializer\Decoder;
4

5
/**
6
 * implements the decoding described in RFC 7784
7
 */
8
class RFC7784Decoder
9
{
10
    /**
11
     * @return int[]
12
     */
NEW
13
    private function decodeHexToBytes(string $hex): array
×
14
    {
NEW
15
        $cleanedHex = preg_replace('/\s+/', '', $hex);
×
16

17
        /** @phpstan-ignore-next-line */
NEW
18
        $list = unpack('C*', hex2bin($cleanedHex));
×
19

20
        /** @var int[]|false $list */
NEW
21
        if (!$list) {
×
NEW
22
            throw new \InvalidArgumentException('Invalid hex string');
×
23
        }
24

NEW
25
        return array_values($list);
×
26
    }
27

28
    /**
29
     * @param int[] $b
30
     */
NEW
31
    private function decodeLittleEndian(array $b, int $bits): \GMP
×
32
    {
NEW
33
        $sum = gmp_init(0);
×
NEW
34
        $bytes = intdiv($bits + 7, 8);
×
35

NEW
36
        for ($i = 0; $i < $bytes; $i++) {
×
NEW
37
            $value = gmp_mul(gmp_init($b[$i]), gmp_pow(2, 8 * $i));
×
NEW
38
            $sum = gmp_add($sum, $value);
×
39
        }
40

NEW
41
        return $sum;
×
42
    }
43

NEW
44
    public function decodeUCoordinate(string $uHex, int $bits): \GMP
×
45
    {
NEW
46
        $u_list = $this->decodeHexToBytes($uHex);
×
47

48
        // Ignore any unused bits
NEW
49
        if ($bits % 8) {
×
NEW
50
            $u_list[count($u_list) - 1] &= (1 << ($bits % 8)) - 1;
×
51
        }
52

NEW
53
        return $this->decodeLittleEndian($u_list, $bits);
×
54
    }
55

NEW
56
    public function decodeScalar25519(string $k): \GMP
×
57
    {
NEW
58
        $k_list = $this->decodeHexToBytes($k);
×
59

60
        // Apply the bit masks
NEW
61
        $k_list[0] &= 248;
×
NEW
62
        $k_list[31] &= 127;
×
NEW
63
        $k_list[31] |= 64;
×
64

NEW
65
        return $this->decodeLittleEndian($k_list, 255);
×
66
    }
67

NEW
68
    public function decodeScalar448(string $k): \GMP
×
69
    {
NEW
70
        $k_list = $this->decodeHexToBytes($k);
×
71

72
        // Apply the bit masks
NEW
73
        $k_list[0] &= 252;
×
NEW
74
        $k_list[55] |= 128;
×
75

NEW
76
        return $this->decodeLittleEndian($k_list, 448);
×
77
    }
78
}
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

© 2026 Coveralls, Inc