AbstractEd25519   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 49
c 1
b 0
f 0
dl 0
loc 148
ccs 63
cts 63
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A sel25519() 0 7 2
A par25519() 0 6 1
A inv25519() 0 10 4
A cswap() 0 4 2
A pow2523() 0 12 3
A pack25519() 0 24 4
A unpack25519() 0 6 2
A set25519() 0 4 2
A neq25519() 0 8 1
1
<?php
2
3
/**
4
 * Copyright (c) 2020 UMI
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
declare(strict_types=1);
26
27
namespace UmiTop\UmiCore\Util\Ed25519;
28
29
/**
30
 * Class AbstractEd25519
31
 * Implementation derived from TweetNaCl version 20140427.
32
 * @see http://tweetnacl.cr.yp.to/
33
 * @package UmiTop\UmiCore\Util\Ed25519
34
 * @SuppressWarnings(PHPMD.ShortMethodName)
35
 * @SuppressWarnings(PHPMD.ShortVariable)
36
 */
37
abstract class AbstractEd25519 extends AbstractBase
38
{
39
    /**
40
     * @param array<int, array<int, int>> $p
41
     * @param array<int, array<int, int>> $q
42
     * @param int $b
43
     * @return void
44
     */
45 12
    protected function cswap(array &$p, array &$q, int $b): void
46
    {
47 12
        for ($i = 0; $i < 4; $i++) {
48 12
            $this->sel25519($p[$i], $q[$i], $b);
49
        }
50 12
    }
51
52
    /**
53
     * @param array<int, int> $o
54
     * @param array<int, int> $i
55
     * @return void
56
     */
57 12
    protected function inv25519(array &$o, array $i): void
58
    {
59 12
        $c = $i;
60 12
        for ($a = 253; $a >= 0; $a--) {
61 12
            $this->fnM($c, $c, $c);
62 12
            if ($a != 2 && $a != 4) {
63 12
                $this->fnM($c, $c, $i);
64
            }
65
        }
66 12
        $o = $c;
67 12
    }
68
69
    /**
70
     * @param array<int, int> $a
71
     * @param array<int, int> $b
72
     * @return bool
73
     */
74 6
    protected function neq25519(array $a, array $b): bool
75
    {
76 6
        $c = $d = str_repeat("\x0", 32);
77
78 6
        $this->pack25519($c, $a);
79 6
        $this->pack25519($d, $b);
80
81 6
        return $this->cryptoVerify32($c, $d);
82
    }
83
84
    /**
85
     * @param string $o
86
     * @param array<int, int> $n
87
     * @return void
88
     */
89 12
    protected function pack25519(string &$o, array $n): void
90
    {
91 12
        $m = array_fill(0, 16, 0);
92 12
        $t = $n;
93
94 12
        $this->car25519($t);
95 12
        $this->car25519($t);
96 12
        $this->car25519($t);
97
98 12
        for ($j = 0; $j < 2; $j++) {
99 12
            $m[0] = $t[0] - 0xffed;
100 12
            for ($i = 1; $i < 15; $i++) {
101 12
                $m[$i] = $t[$i] - 0xffff - (($m[$i - 1] >> 16) & 1);
102 12
                $m[$i - 1] &= 0xffff;
103
            }
104 12
            $m[15] = $t[15] - 0x7fff - (($m[14] >> 16) & 1);
105 12
            $b = ($m[15] >> 16) & 1;
106 12
            $m[14] &= 0xffff;
107 12
            $this->sel25519($t, $m, 1 - $b);
108
        }
109
110 12
        for ($i = 0; $i < 16; $i++) {
111 12
            $o[2 * $i] = chr($t[$i] & 0xff);
112 12
            $o[2 * $i + 1] = chr($t[$i] >> 8);
113
        }
114 12
    }
115
116
    /**
117
     * @param array<int, int> $a
118
     * @return int
119
     */
120 12
    protected function par25519(array $a): int
121
    {
122 12
        $d = str_repeat("\x0", 32);
123 12
        $this->pack25519($d, $a);
124
125 12
        return ord($d[0]) & 1;
126
    }
127
128
    /**
129
     * @param array<int, int> $o
130
     * @param array<int, int> $i
131
     * @return void
132
     */
133 6
    protected function pow2523(array &$o, array $i): void
134
    {
135 6
        $c = $i;
136
137 6
        for ($a = 250; $a >= 0; $a--) {
138 6
            $this->fnM($c, $c, $c);
139 6
            if ($a != 1) {
140 6
                $this->fnM($c, $c, $i);
141
            }
142
        }
143
144 6
        $o = $c;
145 6
    }
146
147
    /**
148
     * @param array<int, int> $r
149
     * @param array<int, int> $a
150
     * @return void
151
     */
152 12
    protected function set25519(array &$r, array $a): void
153
    {
154 12
        for ($i = 0; $i < 16; $i++) {
155 12
            $r[$i] = $a[$i];
156
        }
157 12
    }
158
159
    /**
160
     * @param array<int, int> $o
161
     * @param string $n
162
     * @return void
163
     */
164 6
    protected function unpack25519(array &$o, string $n): void
165
    {
166 6
        for ($i = 0; $i < 16; $i++) {
167 6
            $o[$i] = ord($n[2 * $i]) + (ord($n[2 * $i + 1]) << 8);
168
        }
169 6
        $o[15] &= 0x7fff;
170 6
    }
171
172
    /**
173
     * @param array<int, int> $p
174
     * @param array<int, int> $q
175
     * @param int $b
176
     * @return void
177
     */
178 12
    private function sel25519(array &$p, array &$q, int $b): void
179
    {
180 12
        $c = ~($b - 1);
181 12
        for ($i = 0; $i < 16; $i++) {
182 12
            $ttt = $c & ($p[$i] ^ $q[$i]);
183 12
            $p[$i] ^= $ttt;
184 12
            $q[$i] ^= $ttt;
185
        }
186 12
    }
187
}
188