Debian Codegraph @

lib/3rdParty/composer/paragonie/sodium_compat/src/Core/X25519.php

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 if (class_exists('ParagonIE_Sodium_Core_X25519', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_X25519
10 */
11 abstract class ParagonIE_Sodium_Core_X25519 extends ParagonIE_Sodium_Core_Curve25519
12 {
13 /**
14 * Alters the objects passed to this method in place.
15 *
16 * @internal You should not use this directly from another application
17 *
18 * @param ParagonIE_Sodium_Core_Curve25519_Fe $f
19 * @param ParagonIE_Sodium_Core_Curve25519_Fe $g
20 * @param int $b
21 * @return void
22 */
23 public static function fe_cswap(
24 ParagonIE_Sodium_Core_Curve25519_Fe $f,
25 ParagonIE_Sodium_Core_Curve25519_Fe $g,
26 int $b = 0
27 ): void {
28 $b = -$b;
29 $x0 = ($f->e0 ^ $g->e0) & $b;
30 $x1 = ($f->e1 ^ $g->e1) & $b;
31 $x2 = ($f->e2 ^ $g->e2) & $b;
32 $x3 = ($f->e3 ^ $g->e3) & $b;
33 $x4 = ($f->e4 ^ $g->e4) & $b;
34 $x5 = ($f->e5 ^ $g->e5) & $b;
35 $x6 = ($f->e6 ^ $g->e6) & $b;
36 $x7 = ($f->e7 ^ $g->e7) & $b;
37 $x8 = ($f->e8 ^ $g->e8) & $b;
38 $x9 = ($f->e9 ^ $g->e9) & $b;
39 $f->e0 ^= $x0;
40 $f->e1 ^= $x1;
41 $f->e2 ^= $x2;
42 $f->e3 ^= $x3;
43 $f->e4 ^= $x4;
44 $f->e5 ^= $x5;
45 $f->e6 ^= $x6;
46 $f->e7 ^= $x7;
47 $f->e8 ^= $x8;
48 $f->e9 ^= $x9;
49 $g->e0 ^= $x0;
50 $g->e1 ^= $x1;
51 $g->e2 ^= $x2;
52 $g->e3 ^= $x3;
53 $g->e4 ^= $x4;
54 $g->e5 ^= $x5;
55 $g->e6 ^= $x6;
56 $g->e7 ^= $x7;
57 $g->e8 ^= $x8;
58 $g->e9 ^= $x9;
59 }
60
61 /**
62 * @internal You should not use this directly from another application
63 *
64 * @param ParagonIE_Sodium_Core_Curve25519_Fe $f
65 * @return ParagonIE_Sodium_Core_Curve25519_Fe
66 */
67 public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f): ParagonIE_Sodium_Core_Curve25519_Fe
68 {
69 $h0 = self::mul($f->e0, 121666, 17);
70 $h1 = self::mul($f->e1, 121666, 17);
71 $h2 = self::mul($f->e2, 121666, 17);
72 $h3 = self::mul($f->e3, 121666, 17);
73 $h4 = self::mul($f->e4, 121666, 17);
74 $h5 = self::mul($f->e5, 121666, 17);
75 $h6 = self::mul($f->e6, 121666, 17);
76 $h7 = self::mul($f->e7, 121666, 17);
77 $h8 = self::mul($f->e8, 121666, 17);
78 $h9 = self::mul($f->e9, 121666, 17);
79
80 $carry9 = ($h9 + (1 << 24)) >> 25;
81 $h0 += self::mul($carry9, 19, 5);
82 $h9 -= $carry9 << 25;
83
84 $carry1 = ($h1 + (1 << 24)) >> 25;
85 $h2 += $carry1;
86 $h1 -= $carry1 << 25;
87
88 $carry3 = ($h3 + (1 << 24)) >> 25;
89 $h4 += $carry3;
90 $h3 -= $carry3 << 25;
91
92 $carry5 = ($h5 + (1 << 24)) >> 25;
93 $h6 += $carry5;
94 $h5 -= $carry5 << 25;
95
96 $carry7 = ($h7 + (1 << 24)) >> 25;
97 $h8 += $carry7;
98 $h7 -= $carry7 << 25;
99
100
101 $carry0 = ($h0 + (1 << 25)) >> 26;
102 $h1 += $carry0;
103 $h0 -= $carry0 << 26;
104
105 $carry2 = ($h2 + (1 << 25)) >> 26;
106 $h3 += $carry2;
107 $h2 -= $carry2 << 26;
108
109 $carry4 = ($h4 + (1 << 25)) >> 26;
110 $h5 += $carry4;
111 $h4 -= $carry4 << 26;
112
113 $carry6 = ($h6 + (1 << 25)) >> 26;
114 $h7 += $carry6;
115 $h6 -= $carry6 << 26;
116
117 $carry8 = ($h8 + (1 << 25)) >> 26;
118 $h9 += $carry8;
119 $h8 -= $carry8 << 26;
120 return new ParagonIE_Sodium_Core_Curve25519_Fe($h0, $h1, $h2, $h3, $h4, $h5, $h6, $h7, $h8, $h9);
121 }
122
123 /**
124 * @internal You should not use this directly from another application
125 *
126 * Inline comments preceded by # are from libsodium's ref10 code.
127 *
128 * @param string $n
129 * @param string $p
130 * @return string
131 * @throws SodiumException
132 * @throws TypeError
133 */
134 public static function crypto_scalarmult_curve25519_ref10(
135 #[SensitiveParameter]
136 string $n,
137 #[SensitiveParameter]
138 string $p
139 ): string {
140 $e = $n;
141 $e[0] = self::intToChr(
142 self::chrToInt($e[0]) & 248
143 );
144 $e[31] = self::intToChr(
145 (self::chrToInt($e[31]) & 127) | 64
146 );
147 $x1 = self::fe_frombytes($p);
148 $x2 = self::fe_1();
149 $z2 = self::fe_0();
150 $x3 = clone $x1;
151 $z3 = self::fe_1();
152
153 $swap = 0;
154 for ($pos = 254; $pos >= 0; --$pos) {
155 # b = e[pos / 8] >> (pos & 7);
156 $b = self::chrToInt(
157 $e[$pos >> 3]
158 ) >> ($pos & 7);
159 $b &= 1;
160 $swap ^= $b;
161 self::fe_cswap($x2, $x3, $swap);
162 self::fe_cswap($z2, $z3, $swap);
163 $swap = $b;
164 $tmp0 = self::fe_sub($x3, $z3);
165 $tmp1 = self::fe_sub($x2, $z2);
166 $x2 = self::fe_add($x2, $z2);
167 $z2 = self::fe_add($x3, $z3);
168 $z3 = self::fe_mul($tmp0, $x2);
169 $z2 = self::fe_mul($z2, $tmp1);
170 $tmp0 = self::fe_sq($tmp1);
171 $tmp1 = self::fe_sq($x2);
172 $x3 = self::fe_add($z3, $z2);
173 $z2 = self::fe_sub($z3, $z2);
174 $x2 = self::fe_mul($tmp1, $tmp0);
175 $tmp1 = self::fe_sub($tmp1, $tmp0);
176 $z2 = self::fe_sq($z2);
177 $z3 = self::fe_mul121666($tmp1);
178 $x3 = self::fe_sq($x3);
179 $tmp0 = self::fe_add($tmp0, $z3);
180 $z3 = self::fe_mul($x1, $z2);
181 $z2 = self::fe_mul($tmp1, $tmp0);
182 }
183 self::fe_cswap($x2, $x3, $swap);
184 self::fe_cswap($z2, $z3, $swap);
185
186 $z2 = self::fe_invert($z2);
187 $x2 = self::fe_mul($x2, $z2);
188 return self::fe_tobytes($x2);
189 }
190
191 /**
192 * @internal You should not use this directly from another application
193 *
194 * @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY
195 * @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ
196 * @return ParagonIE_Sodium_Core_Curve25519_Fe
197 */
198 public static function edwards_to_montgomery(
199 ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY,
200 ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ
201 ): ParagonIE_Sodium_Core_Curve25519_Fe {
202 $tempX = self::fe_add($edwardsZ, $edwardsY);
203 $tempZ = self::fe_sub($edwardsZ, $edwardsY);
204 $tempZ = self::fe_invert($tempZ);
205 return self::fe_mul($tempX, $tempZ);
206 }
207
208 /**
209 * @internal You should not use this directly from another application
210 *
211 * @param string $n
212 * @return string
213 * @throws SodiumException
214 * @throws TypeError
215 */
216 public static function crypto_scalarmult_curve25519_ref10_base(
217 #[SensitiveParameter]
218 string $n
219 ): string {
220 $e = $n;
221 $e[0] = self::intToChr(
222 self::chrToInt($e[0]) & 248
223 );
224 $e[31] = self::intToChr(
225 (self::chrToInt($e[31]) & 127) | 64
226 );
227
228 $A = self::ge_scalarmult_base($e);
229 $pk = self::edwards_to_montgomery($A->Y, $A->Z);
230 return self::fe_tobytes($pk);
231 }
232 }