@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Ed25519', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,474 +9,474 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_Ed25519 extends ParagonIE_Sodium_Core32_Curve25519 |
11 | 11 | { |
12 | - const KEYPAIR_BYTES = 96; |
|
13 | - const SEED_BYTES = 32; |
|
14 | - |
|
15 | - /** |
|
16 | - * @internal You should not use this directly from another application |
|
17 | - * |
|
18 | - * @return string (96 bytes) |
|
19 | - * @throws Exception |
|
20 | - * @throws SodiumException |
|
21 | - * @throws TypeError |
|
22 | - */ |
|
23 | - public static function keypair() |
|
24 | - { |
|
25 | - $seed = random_bytes(self::SEED_BYTES); |
|
26 | - $pk = ''; |
|
27 | - $sk = ''; |
|
28 | - self::seed_keypair($pk, $sk, $seed); |
|
29 | - return $sk . $pk; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * @internal You should not use this directly from another application |
|
34 | - * |
|
35 | - * @param string $pk |
|
36 | - * @param string $sk |
|
37 | - * @param string $seed |
|
38 | - * @return string |
|
39 | - * @throws SodiumException |
|
40 | - * @throws TypeError |
|
41 | - */ |
|
42 | - public static function seed_keypair(&$pk, &$sk, $seed) |
|
43 | - { |
|
44 | - if (self::strlen($seed) !== self::SEED_BYTES) { |
|
45 | - throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
|
46 | - } |
|
47 | - |
|
48 | - /** @var string $pk */ |
|
49 | - $pk = self::publickey_from_secretkey($seed); |
|
50 | - $sk = $seed . $pk; |
|
51 | - return $sk; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @internal You should not use this directly from another application |
|
56 | - * |
|
57 | - * @param string $keypair |
|
58 | - * @return string |
|
59 | - * @throws TypeError |
|
60 | - */ |
|
61 | - public static function secretkey($keypair) |
|
62 | - { |
|
63 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
64 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
65 | - } |
|
66 | - return self::substr($keypair, 0, 64); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @internal You should not use this directly from another application |
|
71 | - * |
|
72 | - * @param string $keypair |
|
73 | - * @return string |
|
74 | - * @throws RangeException |
|
75 | - * @throws TypeError |
|
76 | - */ |
|
77 | - public static function publickey($keypair) |
|
78 | - { |
|
79 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
80 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
81 | - } |
|
82 | - return self::substr($keypair, 64, 32); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @internal You should not use this directly from another application |
|
87 | - * |
|
88 | - * @param string $sk |
|
89 | - * @return string |
|
90 | - * @throws SodiumException |
|
91 | - * @throws TypeError |
|
92 | - */ |
|
93 | - public static function publickey_from_secretkey($sk) |
|
94 | - { |
|
95 | - /** @var string $sk */ |
|
96 | - $sk = hash('sha512', self::substr($sk, 0, 32), true); |
|
97 | - $sk[0] = self::intToChr( |
|
98 | - self::chrToInt($sk[0]) & 248 |
|
99 | - ); |
|
100 | - $sk[31] = self::intToChr( |
|
101 | - (self::chrToInt($sk[31]) & 63) | 64 |
|
102 | - ); |
|
103 | - return self::sk_to_pk($sk); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @param string $pk |
|
108 | - * @return string |
|
109 | - * @throws SodiumException |
|
110 | - * @throws TypeError |
|
111 | - */ |
|
112 | - public static function pk_to_curve25519($pk) |
|
113 | - { |
|
114 | - if (self::small_order($pk)) { |
|
115 | - throw new SodiumException('Public key is on a small order'); |
|
116 | - } |
|
117 | - $A = self::ge_frombytes_negate_vartime($pk); |
|
118 | - $p1 = self::ge_mul_l($A); |
|
119 | - if (!self::fe_isnonzero($p1->X)) { |
|
120 | - throw new SodiumException('Unexpected zero result'); |
|
121 | - } |
|
122 | - |
|
123 | - # fe_1(one_minus_y); |
|
124 | - # fe_sub(one_minus_y, one_minus_y, A.Y); |
|
125 | - # fe_invert(one_minus_y, one_minus_y); |
|
126 | - $one_minux_y = self::fe_invert( |
|
127 | - self::fe_sub( |
|
128 | - self::fe_1(), |
|
129 | - $A->Y |
|
130 | - ) |
|
131 | - ); |
|
132 | - |
|
133 | - |
|
134 | - # fe_1(x); |
|
135 | - # fe_add(x, x, A.Y); |
|
136 | - # fe_mul(x, x, one_minus_y); |
|
137 | - $x = self::fe_mul( |
|
138 | - self::fe_add(self::fe_1(), $A->Y), |
|
139 | - $one_minux_y |
|
140 | - ); |
|
141 | - |
|
142 | - # fe_tobytes(curve25519_pk, x); |
|
143 | - return self::fe_tobytes($x); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @internal You should not use this directly from another application |
|
148 | - * |
|
149 | - * @param string $sk |
|
150 | - * @return string |
|
151 | - * @throws SodiumException |
|
152 | - * @throws TypeError |
|
153 | - */ |
|
154 | - public static function sk_to_pk($sk) |
|
155 | - { |
|
156 | - return self::ge_p3_tobytes( |
|
157 | - self::ge_scalarmult_base( |
|
158 | - self::substr($sk, 0, 32) |
|
159 | - ) |
|
160 | - ); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @internal You should not use this directly from another application |
|
165 | - * |
|
166 | - * @param string $message |
|
167 | - * @param string $sk |
|
168 | - * @return string |
|
169 | - * @throws SodiumException |
|
170 | - * @throws TypeError |
|
171 | - */ |
|
172 | - public static function sign($message, $sk) |
|
173 | - { |
|
174 | - /** @var string $signature */ |
|
175 | - $signature = self::sign_detached($message, $sk); |
|
176 | - return $signature . $message; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @internal You should not use this directly from another application |
|
181 | - * |
|
182 | - * @param string $message A signed message |
|
183 | - * @param string $pk Public key |
|
184 | - * @return string Message (without signature) |
|
185 | - * @throws SodiumException |
|
186 | - * @throws TypeError |
|
187 | - */ |
|
188 | - public static function sign_open($message, $pk) |
|
189 | - { |
|
190 | - /** @var string $signature */ |
|
191 | - $signature = self::substr($message, 0, 64); |
|
192 | - |
|
193 | - /** @var string $message */ |
|
194 | - $message = self::substr($message, 64); |
|
195 | - |
|
196 | - if (self::verify_detached($signature, $message, $pk)) { |
|
197 | - return $message; |
|
198 | - } |
|
199 | - throw new SodiumException('Invalid signature'); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @internal You should not use this directly from another application |
|
204 | - * |
|
205 | - * @param string $message |
|
206 | - * @param string $sk |
|
207 | - * @return string |
|
208 | - * @throws SodiumException |
|
209 | - * @throws TypeError |
|
210 | - * @psalm-suppress PossiblyInvalidArgument |
|
211 | - */ |
|
212 | - public static function sign_detached($message, $sk) |
|
213 | - { |
|
214 | - # crypto_hash_sha512(az, sk, 32); |
|
215 | - $az = hash('sha512', self::substr($sk, 0, 32), true); |
|
216 | - |
|
217 | - # az[0] &= 248; |
|
218 | - # az[31] &= 63; |
|
219 | - # az[31] |= 64; |
|
220 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
221 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
222 | - |
|
223 | - # crypto_hash_sha512_init(&hs); |
|
224 | - # crypto_hash_sha512_update(&hs, az + 32, 32); |
|
225 | - # crypto_hash_sha512_update(&hs, m, mlen); |
|
226 | - # crypto_hash_sha512_final(&hs, nonce); |
|
227 | - $hs = hash_init('sha512'); |
|
228 | - self::hash_update($hs, self::substr($az, 32, 32)); |
|
229 | - self::hash_update($hs, $message); |
|
230 | - $nonceHash = hash_final($hs, true); |
|
231 | - |
|
232 | - # memmove(sig + 32, sk + 32, 32); |
|
233 | - $pk = self::substr($sk, 32, 32); |
|
234 | - |
|
235 | - # sc_reduce(nonce); |
|
236 | - # ge_scalarmult_base(&R, nonce); |
|
237 | - # ge_p3_tobytes(sig, &R); |
|
238 | - $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
239 | - $sig = self::ge_p3_tobytes( |
|
240 | - self::ge_scalarmult_base($nonce) |
|
241 | - ); |
|
242 | - |
|
243 | - # crypto_hash_sha512_init(&hs); |
|
244 | - # crypto_hash_sha512_update(&hs, sig, 64); |
|
245 | - # crypto_hash_sha512_update(&hs, m, mlen); |
|
246 | - # crypto_hash_sha512_final(&hs, hram); |
|
247 | - $hs = hash_init('sha512'); |
|
248 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
249 | - self::hash_update($hs, self::substr($pk, 0, 32)); |
|
250 | - self::hash_update($hs, $message); |
|
251 | - $hramHash = hash_final($hs, true); |
|
252 | - |
|
253 | - # sc_reduce(hram); |
|
254 | - # sc_muladd(sig + 32, hram, az, nonce); |
|
255 | - $hram = self::sc_reduce($hramHash); |
|
256 | - $sigAfter = self::sc_muladd($hram, $az, $nonce); |
|
257 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
258 | - |
|
259 | - try { |
|
260 | - ParagonIE_Sodium_Compat::memzero($az); |
|
261 | - } catch (SodiumException $ex) { |
|
262 | - $az = null; |
|
263 | - } |
|
264 | - return $sig; |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * @internal You should not use this directly from another application |
|
269 | - * |
|
270 | - * @param string $sig |
|
271 | - * @param string $message |
|
272 | - * @param string $pk |
|
273 | - * @return bool |
|
274 | - * @throws SodiumException |
|
275 | - * @throws TypeError |
|
276 | - */ |
|
277 | - public static function verify_detached($sig, $message, $pk) |
|
278 | - { |
|
279 | - if (self::strlen($sig) < 64) { |
|
280 | - throw new SodiumException('Signature is too short'); |
|
281 | - } |
|
282 | - if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
283 | - throw new SodiumException('S < L - Invalid signature'); |
|
284 | - } |
|
285 | - if (self::small_order($sig)) { |
|
286 | - throw new SodiumException('Signature is on too small of an order'); |
|
287 | - } |
|
288 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
289 | - throw new SodiumException('Invalid signature'); |
|
290 | - } |
|
291 | - $d = 0; |
|
292 | - for ($i = 0; $i < 32; ++$i) { |
|
293 | - $d |= self::chrToInt($pk[$i]); |
|
294 | - } |
|
295 | - if ($d === 0) { |
|
296 | - throw new SodiumException('All zero public key'); |
|
297 | - } |
|
298 | - |
|
299 | - /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
300 | - $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
301 | - |
|
302 | - // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
303 | - ParagonIE_Sodium_Compat::$fastMult = true; |
|
304 | - |
|
305 | - /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
|
306 | - $A = self::ge_frombytes_negate_vartime($pk); |
|
307 | - |
|
308 | - /** @var string $hDigest */ |
|
309 | - $hDigest = hash( |
|
310 | - 'sha512', |
|
311 | - self::substr($sig, 0, 32) . |
|
312 | - self::substr($pk, 0, 32) . |
|
313 | - $message, |
|
314 | - true |
|
315 | - ); |
|
316 | - |
|
317 | - /** @var string $h */ |
|
318 | - $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
319 | - |
|
320 | - /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ |
|
321 | - $R = self::ge_double_scalarmult_vartime( |
|
322 | - $h, |
|
323 | - $A, |
|
324 | - self::substr($sig, 32) |
|
325 | - ); |
|
326 | - |
|
327 | - /** @var string $rcheck */ |
|
328 | - $rcheck = self::ge_tobytes($R); |
|
329 | - |
|
330 | - // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
331 | - ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
332 | - |
|
333 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * @internal You should not use this directly from another application |
|
338 | - * |
|
339 | - * @param string $S |
|
340 | - * @return bool |
|
341 | - * @throws SodiumException |
|
342 | - * @throws TypeError |
|
343 | - */ |
|
344 | - public static function check_S_lt_L($S) |
|
345 | - { |
|
346 | - if (self::strlen($S) < 32) { |
|
347 | - throw new SodiumException('Signature must be 32 bytes'); |
|
348 | - } |
|
349 | - static $L = array( |
|
350 | - 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
|
351 | - 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
|
352 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
353 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 |
|
354 | - ); |
|
355 | - /** @var array<int, int> $L */ |
|
356 | - $c = 0; |
|
357 | - $n = 1; |
|
358 | - $i = 32; |
|
359 | - |
|
360 | - do { |
|
361 | - --$i; |
|
362 | - $x = self::chrToInt($S[$i]); |
|
363 | - $c |= ( |
|
364 | - (($x - $L[$i]) >> 8) & $n |
|
365 | - ); |
|
366 | - $n &= ( |
|
367 | - (($x ^ $L[$i]) - 1) >> 8 |
|
368 | - ); |
|
369 | - } while ($i !== 0); |
|
370 | - |
|
371 | - return $c === 0; |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * @param string $R |
|
376 | - * @return bool |
|
377 | - * @throws SodiumException |
|
378 | - * @throws TypeError |
|
379 | - */ |
|
380 | - public static function small_order($R) |
|
381 | - { |
|
382 | - static $blocklist = array( |
|
383 | - /* 0 (order 4) */ |
|
384 | - array( |
|
385 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
386 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
387 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
388 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
389 | - ), |
|
390 | - /* 1 (order 1) */ |
|
391 | - array( |
|
392 | - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
393 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
394 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
395 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
396 | - ), |
|
397 | - /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
398 | - array( |
|
399 | - 0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
400 | - 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
401 | - 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
402 | - 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05 |
|
403 | - ), |
|
404 | - /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
405 | - array( |
|
406 | - 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
407 | - 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
408 | - 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
409 | - 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a |
|
410 | - ), |
|
411 | - /* p-1 (order 2) */ |
|
412 | - array( |
|
413 | - 0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
414 | - 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
415 | - 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
416 | - 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85 |
|
417 | - ), |
|
418 | - /* p (order 4) */ |
|
419 | - array( |
|
420 | - 0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
421 | - 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
422 | - 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
423 | - 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa |
|
424 | - ), |
|
425 | - /* p+1 (order 1) */ |
|
426 | - array( |
|
427 | - 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
428 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
429 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
430 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
431 | - ), |
|
432 | - /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
433 | - array( |
|
434 | - 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
435 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
436 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
437 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
438 | - ), |
|
439 | - /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
440 | - array( |
|
441 | - 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
442 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
443 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
444 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
445 | - ), |
|
446 | - /* 2p-1 (order 2) */ |
|
447 | - array( |
|
448 | - 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
449 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
450 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
451 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
452 | - ), |
|
453 | - /* 2p (order 4) */ |
|
454 | - array( |
|
455 | - 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
456 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
457 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
458 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
459 | - ), |
|
460 | - /* 2p+1 (order 1) */ |
|
461 | - array( |
|
462 | - 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
463 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
464 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
465 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
466 | - ) |
|
467 | - ); |
|
468 | - /** @var array<int, array<int, int>> $blocklist */ |
|
469 | - $countBlocklist = count($blocklist); |
|
470 | - |
|
471 | - for ($i = 0; $i < $countBlocklist; ++$i) { |
|
472 | - $c = 0; |
|
473 | - for ($j = 0; $j < 32; ++$j) { |
|
474 | - $c |= self::chrToInt($R[$j]) ^ $blocklist[$i][$j]; |
|
475 | - } |
|
476 | - if ($c === 0) { |
|
477 | - return true; |
|
478 | - } |
|
479 | - } |
|
480 | - return false; |
|
481 | - } |
|
12 | + const KEYPAIR_BYTES = 96; |
|
13 | + const SEED_BYTES = 32; |
|
14 | + |
|
15 | + /** |
|
16 | + * @internal You should not use this directly from another application |
|
17 | + * |
|
18 | + * @return string (96 bytes) |
|
19 | + * @throws Exception |
|
20 | + * @throws SodiumException |
|
21 | + * @throws TypeError |
|
22 | + */ |
|
23 | + public static function keypair() |
|
24 | + { |
|
25 | + $seed = random_bytes(self::SEED_BYTES); |
|
26 | + $pk = ''; |
|
27 | + $sk = ''; |
|
28 | + self::seed_keypair($pk, $sk, $seed); |
|
29 | + return $sk . $pk; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * @internal You should not use this directly from another application |
|
34 | + * |
|
35 | + * @param string $pk |
|
36 | + * @param string $sk |
|
37 | + * @param string $seed |
|
38 | + * @return string |
|
39 | + * @throws SodiumException |
|
40 | + * @throws TypeError |
|
41 | + */ |
|
42 | + public static function seed_keypair(&$pk, &$sk, $seed) |
|
43 | + { |
|
44 | + if (self::strlen($seed) !== self::SEED_BYTES) { |
|
45 | + throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
|
46 | + } |
|
47 | + |
|
48 | + /** @var string $pk */ |
|
49 | + $pk = self::publickey_from_secretkey($seed); |
|
50 | + $sk = $seed . $pk; |
|
51 | + return $sk; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @internal You should not use this directly from another application |
|
56 | + * |
|
57 | + * @param string $keypair |
|
58 | + * @return string |
|
59 | + * @throws TypeError |
|
60 | + */ |
|
61 | + public static function secretkey($keypair) |
|
62 | + { |
|
63 | + if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
64 | + throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
65 | + } |
|
66 | + return self::substr($keypair, 0, 64); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @internal You should not use this directly from another application |
|
71 | + * |
|
72 | + * @param string $keypair |
|
73 | + * @return string |
|
74 | + * @throws RangeException |
|
75 | + * @throws TypeError |
|
76 | + */ |
|
77 | + public static function publickey($keypair) |
|
78 | + { |
|
79 | + if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
80 | + throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
81 | + } |
|
82 | + return self::substr($keypair, 64, 32); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @internal You should not use this directly from another application |
|
87 | + * |
|
88 | + * @param string $sk |
|
89 | + * @return string |
|
90 | + * @throws SodiumException |
|
91 | + * @throws TypeError |
|
92 | + */ |
|
93 | + public static function publickey_from_secretkey($sk) |
|
94 | + { |
|
95 | + /** @var string $sk */ |
|
96 | + $sk = hash('sha512', self::substr($sk, 0, 32), true); |
|
97 | + $sk[0] = self::intToChr( |
|
98 | + self::chrToInt($sk[0]) & 248 |
|
99 | + ); |
|
100 | + $sk[31] = self::intToChr( |
|
101 | + (self::chrToInt($sk[31]) & 63) | 64 |
|
102 | + ); |
|
103 | + return self::sk_to_pk($sk); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @param string $pk |
|
108 | + * @return string |
|
109 | + * @throws SodiumException |
|
110 | + * @throws TypeError |
|
111 | + */ |
|
112 | + public static function pk_to_curve25519($pk) |
|
113 | + { |
|
114 | + if (self::small_order($pk)) { |
|
115 | + throw new SodiumException('Public key is on a small order'); |
|
116 | + } |
|
117 | + $A = self::ge_frombytes_negate_vartime($pk); |
|
118 | + $p1 = self::ge_mul_l($A); |
|
119 | + if (!self::fe_isnonzero($p1->X)) { |
|
120 | + throw new SodiumException('Unexpected zero result'); |
|
121 | + } |
|
122 | + |
|
123 | + # fe_1(one_minus_y); |
|
124 | + # fe_sub(one_minus_y, one_minus_y, A.Y); |
|
125 | + # fe_invert(one_minus_y, one_minus_y); |
|
126 | + $one_minux_y = self::fe_invert( |
|
127 | + self::fe_sub( |
|
128 | + self::fe_1(), |
|
129 | + $A->Y |
|
130 | + ) |
|
131 | + ); |
|
132 | + |
|
133 | + |
|
134 | + # fe_1(x); |
|
135 | + # fe_add(x, x, A.Y); |
|
136 | + # fe_mul(x, x, one_minus_y); |
|
137 | + $x = self::fe_mul( |
|
138 | + self::fe_add(self::fe_1(), $A->Y), |
|
139 | + $one_minux_y |
|
140 | + ); |
|
141 | + |
|
142 | + # fe_tobytes(curve25519_pk, x); |
|
143 | + return self::fe_tobytes($x); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @internal You should not use this directly from another application |
|
148 | + * |
|
149 | + * @param string $sk |
|
150 | + * @return string |
|
151 | + * @throws SodiumException |
|
152 | + * @throws TypeError |
|
153 | + */ |
|
154 | + public static function sk_to_pk($sk) |
|
155 | + { |
|
156 | + return self::ge_p3_tobytes( |
|
157 | + self::ge_scalarmult_base( |
|
158 | + self::substr($sk, 0, 32) |
|
159 | + ) |
|
160 | + ); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @internal You should not use this directly from another application |
|
165 | + * |
|
166 | + * @param string $message |
|
167 | + * @param string $sk |
|
168 | + * @return string |
|
169 | + * @throws SodiumException |
|
170 | + * @throws TypeError |
|
171 | + */ |
|
172 | + public static function sign($message, $sk) |
|
173 | + { |
|
174 | + /** @var string $signature */ |
|
175 | + $signature = self::sign_detached($message, $sk); |
|
176 | + return $signature . $message; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @internal You should not use this directly from another application |
|
181 | + * |
|
182 | + * @param string $message A signed message |
|
183 | + * @param string $pk Public key |
|
184 | + * @return string Message (without signature) |
|
185 | + * @throws SodiumException |
|
186 | + * @throws TypeError |
|
187 | + */ |
|
188 | + public static function sign_open($message, $pk) |
|
189 | + { |
|
190 | + /** @var string $signature */ |
|
191 | + $signature = self::substr($message, 0, 64); |
|
192 | + |
|
193 | + /** @var string $message */ |
|
194 | + $message = self::substr($message, 64); |
|
195 | + |
|
196 | + if (self::verify_detached($signature, $message, $pk)) { |
|
197 | + return $message; |
|
198 | + } |
|
199 | + throw new SodiumException('Invalid signature'); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @internal You should not use this directly from another application |
|
204 | + * |
|
205 | + * @param string $message |
|
206 | + * @param string $sk |
|
207 | + * @return string |
|
208 | + * @throws SodiumException |
|
209 | + * @throws TypeError |
|
210 | + * @psalm-suppress PossiblyInvalidArgument |
|
211 | + */ |
|
212 | + public static function sign_detached($message, $sk) |
|
213 | + { |
|
214 | + # crypto_hash_sha512(az, sk, 32); |
|
215 | + $az = hash('sha512', self::substr($sk, 0, 32), true); |
|
216 | + |
|
217 | + # az[0] &= 248; |
|
218 | + # az[31] &= 63; |
|
219 | + # az[31] |= 64; |
|
220 | + $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
221 | + $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
222 | + |
|
223 | + # crypto_hash_sha512_init(&hs); |
|
224 | + # crypto_hash_sha512_update(&hs, az + 32, 32); |
|
225 | + # crypto_hash_sha512_update(&hs, m, mlen); |
|
226 | + # crypto_hash_sha512_final(&hs, nonce); |
|
227 | + $hs = hash_init('sha512'); |
|
228 | + self::hash_update($hs, self::substr($az, 32, 32)); |
|
229 | + self::hash_update($hs, $message); |
|
230 | + $nonceHash = hash_final($hs, true); |
|
231 | + |
|
232 | + # memmove(sig + 32, sk + 32, 32); |
|
233 | + $pk = self::substr($sk, 32, 32); |
|
234 | + |
|
235 | + # sc_reduce(nonce); |
|
236 | + # ge_scalarmult_base(&R, nonce); |
|
237 | + # ge_p3_tobytes(sig, &R); |
|
238 | + $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
239 | + $sig = self::ge_p3_tobytes( |
|
240 | + self::ge_scalarmult_base($nonce) |
|
241 | + ); |
|
242 | + |
|
243 | + # crypto_hash_sha512_init(&hs); |
|
244 | + # crypto_hash_sha512_update(&hs, sig, 64); |
|
245 | + # crypto_hash_sha512_update(&hs, m, mlen); |
|
246 | + # crypto_hash_sha512_final(&hs, hram); |
|
247 | + $hs = hash_init('sha512'); |
|
248 | + self::hash_update($hs, self::substr($sig, 0, 32)); |
|
249 | + self::hash_update($hs, self::substr($pk, 0, 32)); |
|
250 | + self::hash_update($hs, $message); |
|
251 | + $hramHash = hash_final($hs, true); |
|
252 | + |
|
253 | + # sc_reduce(hram); |
|
254 | + # sc_muladd(sig + 32, hram, az, nonce); |
|
255 | + $hram = self::sc_reduce($hramHash); |
|
256 | + $sigAfter = self::sc_muladd($hram, $az, $nonce); |
|
257 | + $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
258 | + |
|
259 | + try { |
|
260 | + ParagonIE_Sodium_Compat::memzero($az); |
|
261 | + } catch (SodiumException $ex) { |
|
262 | + $az = null; |
|
263 | + } |
|
264 | + return $sig; |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * @internal You should not use this directly from another application |
|
269 | + * |
|
270 | + * @param string $sig |
|
271 | + * @param string $message |
|
272 | + * @param string $pk |
|
273 | + * @return bool |
|
274 | + * @throws SodiumException |
|
275 | + * @throws TypeError |
|
276 | + */ |
|
277 | + public static function verify_detached($sig, $message, $pk) |
|
278 | + { |
|
279 | + if (self::strlen($sig) < 64) { |
|
280 | + throw new SodiumException('Signature is too short'); |
|
281 | + } |
|
282 | + if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
283 | + throw new SodiumException('S < L - Invalid signature'); |
|
284 | + } |
|
285 | + if (self::small_order($sig)) { |
|
286 | + throw new SodiumException('Signature is on too small of an order'); |
|
287 | + } |
|
288 | + if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
289 | + throw new SodiumException('Invalid signature'); |
|
290 | + } |
|
291 | + $d = 0; |
|
292 | + for ($i = 0; $i < 32; ++$i) { |
|
293 | + $d |= self::chrToInt($pk[$i]); |
|
294 | + } |
|
295 | + if ($d === 0) { |
|
296 | + throw new SodiumException('All zero public key'); |
|
297 | + } |
|
298 | + |
|
299 | + /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
300 | + $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
301 | + |
|
302 | + // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
303 | + ParagonIE_Sodium_Compat::$fastMult = true; |
|
304 | + |
|
305 | + /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
|
306 | + $A = self::ge_frombytes_negate_vartime($pk); |
|
307 | + |
|
308 | + /** @var string $hDigest */ |
|
309 | + $hDigest = hash( |
|
310 | + 'sha512', |
|
311 | + self::substr($sig, 0, 32) . |
|
312 | + self::substr($pk, 0, 32) . |
|
313 | + $message, |
|
314 | + true |
|
315 | + ); |
|
316 | + |
|
317 | + /** @var string $h */ |
|
318 | + $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
319 | + |
|
320 | + /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ |
|
321 | + $R = self::ge_double_scalarmult_vartime( |
|
322 | + $h, |
|
323 | + $A, |
|
324 | + self::substr($sig, 32) |
|
325 | + ); |
|
326 | + |
|
327 | + /** @var string $rcheck */ |
|
328 | + $rcheck = self::ge_tobytes($R); |
|
329 | + |
|
330 | + // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
331 | + ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
332 | + |
|
333 | + return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * @internal You should not use this directly from another application |
|
338 | + * |
|
339 | + * @param string $S |
|
340 | + * @return bool |
|
341 | + * @throws SodiumException |
|
342 | + * @throws TypeError |
|
343 | + */ |
|
344 | + public static function check_S_lt_L($S) |
|
345 | + { |
|
346 | + if (self::strlen($S) < 32) { |
|
347 | + throw new SodiumException('Signature must be 32 bytes'); |
|
348 | + } |
|
349 | + static $L = array( |
|
350 | + 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
|
351 | + 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
|
352 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
353 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 |
|
354 | + ); |
|
355 | + /** @var array<int, int> $L */ |
|
356 | + $c = 0; |
|
357 | + $n = 1; |
|
358 | + $i = 32; |
|
359 | + |
|
360 | + do { |
|
361 | + --$i; |
|
362 | + $x = self::chrToInt($S[$i]); |
|
363 | + $c |= ( |
|
364 | + (($x - $L[$i]) >> 8) & $n |
|
365 | + ); |
|
366 | + $n &= ( |
|
367 | + (($x ^ $L[$i]) - 1) >> 8 |
|
368 | + ); |
|
369 | + } while ($i !== 0); |
|
370 | + |
|
371 | + return $c === 0; |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * @param string $R |
|
376 | + * @return bool |
|
377 | + * @throws SodiumException |
|
378 | + * @throws TypeError |
|
379 | + */ |
|
380 | + public static function small_order($R) |
|
381 | + { |
|
382 | + static $blocklist = array( |
|
383 | + /* 0 (order 4) */ |
|
384 | + array( |
|
385 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
386 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
387 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
388 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
389 | + ), |
|
390 | + /* 1 (order 1) */ |
|
391 | + array( |
|
392 | + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
393 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
394 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
395 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
396 | + ), |
|
397 | + /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
398 | + array( |
|
399 | + 0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
400 | + 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
401 | + 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
402 | + 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05 |
|
403 | + ), |
|
404 | + /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
405 | + array( |
|
406 | + 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
407 | + 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
408 | + 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
409 | + 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a |
|
410 | + ), |
|
411 | + /* p-1 (order 2) */ |
|
412 | + array( |
|
413 | + 0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
414 | + 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
415 | + 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
416 | + 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85 |
|
417 | + ), |
|
418 | + /* p (order 4) */ |
|
419 | + array( |
|
420 | + 0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
421 | + 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
422 | + 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
423 | + 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa |
|
424 | + ), |
|
425 | + /* p+1 (order 1) */ |
|
426 | + array( |
|
427 | + 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
428 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
429 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
430 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
431 | + ), |
|
432 | + /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
433 | + array( |
|
434 | + 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
435 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
436 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
437 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
438 | + ), |
|
439 | + /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
440 | + array( |
|
441 | + 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
442 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
443 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
444 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
445 | + ), |
|
446 | + /* 2p-1 (order 2) */ |
|
447 | + array( |
|
448 | + 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
449 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
450 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
451 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
452 | + ), |
|
453 | + /* 2p (order 4) */ |
|
454 | + array( |
|
455 | + 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
456 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
457 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
458 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
459 | + ), |
|
460 | + /* 2p+1 (order 1) */ |
|
461 | + array( |
|
462 | + 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
463 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
464 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
465 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
466 | + ) |
|
467 | + ); |
|
468 | + /** @var array<int, array<int, int>> $blocklist */ |
|
469 | + $countBlocklist = count($blocklist); |
|
470 | + |
|
471 | + for ($i = 0; $i < $countBlocklist; ++$i) { |
|
472 | + $c = 0; |
|
473 | + for ($j = 0; $j < 32; ++$j) { |
|
474 | + $c |= self::chrToInt($R[$j]) ^ $blocklist[$i][$j]; |
|
475 | + } |
|
476 | + if ($c === 0) { |
|
477 | + return true; |
|
478 | + } |
|
479 | + } |
|
480 | + return false; |
|
481 | + } |
|
482 | 482 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_Ed25519', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_Ed25519', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -22,10 +22,10 @@ discard block |
||
22 | 22 | */ |
23 | 23 | public static function keypair() |
24 | 24 | { |
25 | - $seed = random_bytes(self::SEED_BYTES); |
|
25 | + $seed = random_bytes( self::SEED_BYTES ); |
|
26 | 26 | $pk = ''; |
27 | 27 | $sk = ''; |
28 | - self::seed_keypair($pk, $sk, $seed); |
|
28 | + self::seed_keypair( $pk, $sk, $seed ); |
|
29 | 29 | return $sk . $pk; |
30 | 30 | } |
31 | 31 | |
@@ -39,14 +39,14 @@ discard block |
||
39 | 39 | * @throws SodiumException |
40 | 40 | * @throws TypeError |
41 | 41 | */ |
42 | - public static function seed_keypair(&$pk, &$sk, $seed) |
|
42 | + public static function seed_keypair( &$pk, &$sk, $seed ) |
|
43 | 43 | { |
44 | - if (self::strlen($seed) !== self::SEED_BYTES) { |
|
45 | - throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
|
44 | + if ( self::strlen( $seed ) !== self::SEED_BYTES ) { |
|
45 | + throw new RangeException( 'crypto_sign keypair seed must be 32 bytes long' ); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | /** @var string $pk */ |
49 | - $pk = self::publickey_from_secretkey($seed); |
|
49 | + $pk = self::publickey_from_secretkey( $seed ); |
|
50 | 50 | $sk = $seed . $pk; |
51 | 51 | return $sk; |
52 | 52 | } |
@@ -58,12 +58,12 @@ discard block |
||
58 | 58 | * @return string |
59 | 59 | * @throws TypeError |
60 | 60 | */ |
61 | - public static function secretkey($keypair) |
|
61 | + public static function secretkey( $keypair ) |
|
62 | 62 | { |
63 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
64 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
63 | + if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) { |
|
64 | + throw new RangeException( 'crypto_sign keypair must be 96 bytes long' ); |
|
65 | 65 | } |
66 | - return self::substr($keypair, 0, 64); |
|
66 | + return self::substr( $keypair, 0, 64 ); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
@@ -74,12 +74,12 @@ discard block |
||
74 | 74 | * @throws RangeException |
75 | 75 | * @throws TypeError |
76 | 76 | */ |
77 | - public static function publickey($keypair) |
|
77 | + public static function publickey( $keypair ) |
|
78 | 78 | { |
79 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
80 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
79 | + if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) { |
|
80 | + throw new RangeException( 'crypto_sign keypair must be 96 bytes long' ); |
|
81 | 81 | } |
82 | - return self::substr($keypair, 64, 32); |
|
82 | + return self::substr( $keypair, 64, 32 ); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -90,17 +90,17 @@ discard block |
||
90 | 90 | * @throws SodiumException |
91 | 91 | * @throws TypeError |
92 | 92 | */ |
93 | - public static function publickey_from_secretkey($sk) |
|
93 | + public static function publickey_from_secretkey( $sk ) |
|
94 | 94 | { |
95 | 95 | /** @var string $sk */ |
96 | - $sk = hash('sha512', self::substr($sk, 0, 32), true); |
|
97 | - $sk[0] = self::intToChr( |
|
98 | - self::chrToInt($sk[0]) & 248 |
|
96 | + $sk = hash( 'sha512', self::substr( $sk, 0, 32 ), true ); |
|
97 | + $sk[ 0 ] = self::intToChr( |
|
98 | + self::chrToInt( $sk[ 0 ] ) & 248 |
|
99 | 99 | ); |
100 | - $sk[31] = self::intToChr( |
|
101 | - (self::chrToInt($sk[31]) & 63) | 64 |
|
100 | + $sk[ 31 ] = self::intToChr( |
|
101 | + ( self::chrToInt( $sk[ 31 ] ) & 63 ) | 64 |
|
102 | 102 | ); |
103 | - return self::sk_to_pk($sk); |
|
103 | + return self::sk_to_pk( $sk ); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
@@ -109,15 +109,15 @@ discard block |
||
109 | 109 | * @throws SodiumException |
110 | 110 | * @throws TypeError |
111 | 111 | */ |
112 | - public static function pk_to_curve25519($pk) |
|
112 | + public static function pk_to_curve25519( $pk ) |
|
113 | 113 | { |
114 | - if (self::small_order($pk)) { |
|
115 | - throw new SodiumException('Public key is on a small order'); |
|
114 | + if ( self::small_order( $pk ) ) { |
|
115 | + throw new SodiumException( 'Public key is on a small order' ); |
|
116 | 116 | } |
117 | - $A = self::ge_frombytes_negate_vartime($pk); |
|
118 | - $p1 = self::ge_mul_l($A); |
|
119 | - if (!self::fe_isnonzero($p1->X)) { |
|
120 | - throw new SodiumException('Unexpected zero result'); |
|
117 | + $A = self::ge_frombytes_negate_vartime( $pk ); |
|
118 | + $p1 = self::ge_mul_l( $A ); |
|
119 | + if ( ! self::fe_isnonzero( $p1->X ) ) { |
|
120 | + throw new SodiumException( 'Unexpected zero result' ); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | # fe_1(one_minus_y); |
@@ -135,12 +135,12 @@ discard block |
||
135 | 135 | # fe_add(x, x, A.Y); |
136 | 136 | # fe_mul(x, x, one_minus_y); |
137 | 137 | $x = self::fe_mul( |
138 | - self::fe_add(self::fe_1(), $A->Y), |
|
138 | + self::fe_add( self::fe_1(), $A->Y ), |
|
139 | 139 | $one_minux_y |
140 | 140 | ); |
141 | 141 | |
142 | 142 | # fe_tobytes(curve25519_pk, x); |
143 | - return self::fe_tobytes($x); |
|
143 | + return self::fe_tobytes( $x ); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | /** |
@@ -151,11 +151,11 @@ discard block |
||
151 | 151 | * @throws SodiumException |
152 | 152 | * @throws TypeError |
153 | 153 | */ |
154 | - public static function sk_to_pk($sk) |
|
154 | + public static function sk_to_pk( $sk ) |
|
155 | 155 | { |
156 | 156 | return self::ge_p3_tobytes( |
157 | 157 | self::ge_scalarmult_base( |
158 | - self::substr($sk, 0, 32) |
|
158 | + self::substr( $sk, 0, 32 ) |
|
159 | 159 | ) |
160 | 160 | ); |
161 | 161 | } |
@@ -169,10 +169,10 @@ discard block |
||
169 | 169 | * @throws SodiumException |
170 | 170 | * @throws TypeError |
171 | 171 | */ |
172 | - public static function sign($message, $sk) |
|
172 | + public static function sign( $message, $sk ) |
|
173 | 173 | { |
174 | 174 | /** @var string $signature */ |
175 | - $signature = self::sign_detached($message, $sk); |
|
175 | + $signature = self::sign_detached( $message, $sk ); |
|
176 | 176 | return $signature . $message; |
177 | 177 | } |
178 | 178 | |
@@ -185,18 +185,18 @@ discard block |
||
185 | 185 | * @throws SodiumException |
186 | 186 | * @throws TypeError |
187 | 187 | */ |
188 | - public static function sign_open($message, $pk) |
|
188 | + public static function sign_open( $message, $pk ) |
|
189 | 189 | { |
190 | 190 | /** @var string $signature */ |
191 | - $signature = self::substr($message, 0, 64); |
|
191 | + $signature = self::substr( $message, 0, 64 ); |
|
192 | 192 | |
193 | 193 | /** @var string $message */ |
194 | - $message = self::substr($message, 64); |
|
194 | + $message = self::substr( $message, 64 ); |
|
195 | 195 | |
196 | - if (self::verify_detached($signature, $message, $pk)) { |
|
196 | + if ( self::verify_detached( $signature, $message, $pk ) ) { |
|
197 | 197 | return $message; |
198 | 198 | } |
199 | - throw new SodiumException('Invalid signature'); |
|
199 | + throw new SodiumException( 'Invalid signature' ); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | /** |
@@ -209,56 +209,56 @@ discard block |
||
209 | 209 | * @throws TypeError |
210 | 210 | * @psalm-suppress PossiblyInvalidArgument |
211 | 211 | */ |
212 | - public static function sign_detached($message, $sk) |
|
212 | + public static function sign_detached( $message, $sk ) |
|
213 | 213 | { |
214 | 214 | # crypto_hash_sha512(az, sk, 32); |
215 | - $az = hash('sha512', self::substr($sk, 0, 32), true); |
|
215 | + $az = hash( 'sha512', self::substr( $sk, 0, 32 ), true ); |
|
216 | 216 | |
217 | 217 | # az[0] &= 248; |
218 | 218 | # az[31] &= 63; |
219 | 219 | # az[31] |= 64; |
220 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
221 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
220 | + $az[ 0 ] = self::intToChr( self::chrToInt( $az[ 0 ] ) & 248 ); |
|
221 | + $az[ 31 ] = self::intToChr( ( self::chrToInt( $az[ 31 ] ) & 63 ) | 64 ); |
|
222 | 222 | |
223 | 223 | # crypto_hash_sha512_init(&hs); |
224 | 224 | # crypto_hash_sha512_update(&hs, az + 32, 32); |
225 | 225 | # crypto_hash_sha512_update(&hs, m, mlen); |
226 | 226 | # crypto_hash_sha512_final(&hs, nonce); |
227 | - $hs = hash_init('sha512'); |
|
228 | - self::hash_update($hs, self::substr($az, 32, 32)); |
|
229 | - self::hash_update($hs, $message); |
|
230 | - $nonceHash = hash_final($hs, true); |
|
227 | + $hs = hash_init( 'sha512' ); |
|
228 | + self::hash_update( $hs, self::substr( $az, 32, 32 ) ); |
|
229 | + self::hash_update( $hs, $message ); |
|
230 | + $nonceHash = hash_final( $hs, true ); |
|
231 | 231 | |
232 | 232 | # memmove(sig + 32, sk + 32, 32); |
233 | - $pk = self::substr($sk, 32, 32); |
|
233 | + $pk = self::substr( $sk, 32, 32 ); |
|
234 | 234 | |
235 | 235 | # sc_reduce(nonce); |
236 | 236 | # ge_scalarmult_base(&R, nonce); |
237 | 237 | # ge_p3_tobytes(sig, &R); |
238 | - $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
238 | + $nonce = self::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 ); |
|
239 | 239 | $sig = self::ge_p3_tobytes( |
240 | - self::ge_scalarmult_base($nonce) |
|
240 | + self::ge_scalarmult_base( $nonce ) |
|
241 | 241 | ); |
242 | 242 | |
243 | 243 | # crypto_hash_sha512_init(&hs); |
244 | 244 | # crypto_hash_sha512_update(&hs, sig, 64); |
245 | 245 | # crypto_hash_sha512_update(&hs, m, mlen); |
246 | 246 | # crypto_hash_sha512_final(&hs, hram); |
247 | - $hs = hash_init('sha512'); |
|
248 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
249 | - self::hash_update($hs, self::substr($pk, 0, 32)); |
|
250 | - self::hash_update($hs, $message); |
|
251 | - $hramHash = hash_final($hs, true); |
|
247 | + $hs = hash_init( 'sha512' ); |
|
248 | + self::hash_update( $hs, self::substr( $sig, 0, 32 ) ); |
|
249 | + self::hash_update( $hs, self::substr( $pk, 0, 32 ) ); |
|
250 | + self::hash_update( $hs, $message ); |
|
251 | + $hramHash = hash_final( $hs, true ); |
|
252 | 252 | |
253 | 253 | # sc_reduce(hram); |
254 | 254 | # sc_muladd(sig + 32, hram, az, nonce); |
255 | - $hram = self::sc_reduce($hramHash); |
|
256 | - $sigAfter = self::sc_muladd($hram, $az, $nonce); |
|
257 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
255 | + $hram = self::sc_reduce( $hramHash ); |
|
256 | + $sigAfter = self::sc_muladd( $hram, $az, $nonce ); |
|
257 | + $sig = self::substr( $sig, 0, 32 ) . self::substr( $sigAfter, 0, 32 ); |
|
258 | 258 | |
259 | 259 | try { |
260 | - ParagonIE_Sodium_Compat::memzero($az); |
|
261 | - } catch (SodiumException $ex) { |
|
260 | + ParagonIE_Sodium_Compat::memzero( $az ); |
|
261 | + } catch ( SodiumException $ex ) { |
|
262 | 262 | $az = null; |
263 | 263 | } |
264 | 264 | return $sig; |
@@ -274,26 +274,26 @@ discard block |
||
274 | 274 | * @throws SodiumException |
275 | 275 | * @throws TypeError |
276 | 276 | */ |
277 | - public static function verify_detached($sig, $message, $pk) |
|
277 | + public static function verify_detached( $sig, $message, $pk ) |
|
278 | 278 | { |
279 | - if (self::strlen($sig) < 64) { |
|
280 | - throw new SodiumException('Signature is too short'); |
|
279 | + if ( self::strlen( $sig ) < 64 ) { |
|
280 | + throw new SodiumException( 'Signature is too short' ); |
|
281 | 281 | } |
282 | - if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
283 | - throw new SodiumException('S < L - Invalid signature'); |
|
282 | + if ( ( self::chrToInt( $sig[ 63 ] ) & 240 ) && self::check_S_lt_L( self::substr( $sig, 32, 32 ) ) ) { |
|
283 | + throw new SodiumException( 'S < L - Invalid signature' ); |
|
284 | 284 | } |
285 | - if (self::small_order($sig)) { |
|
286 | - throw new SodiumException('Signature is on too small of an order'); |
|
285 | + if ( self::small_order( $sig ) ) { |
|
286 | + throw new SodiumException( 'Signature is on too small of an order' ); |
|
287 | 287 | } |
288 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
289 | - throw new SodiumException('Invalid signature'); |
|
288 | + if ( ( self::chrToInt( $sig[ 63 ] ) & 224 ) !== 0 ) { |
|
289 | + throw new SodiumException( 'Invalid signature' ); |
|
290 | 290 | } |
291 | 291 | $d = 0; |
292 | - for ($i = 0; $i < 32; ++$i) { |
|
293 | - $d |= self::chrToInt($pk[$i]); |
|
292 | + for ( $i = 0; $i < 32; ++$i ) { |
|
293 | + $d |= self::chrToInt( $pk[ $i ] ); |
|
294 | 294 | } |
295 | - if ($d === 0) { |
|
296 | - throw new SodiumException('All zero public key'); |
|
295 | + if ( $d === 0 ) { |
|
296 | + throw new SodiumException( 'All zero public key' ); |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
@@ -303,34 +303,34 @@ discard block |
||
303 | 303 | ParagonIE_Sodium_Compat::$fastMult = true; |
304 | 304 | |
305 | 305 | /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
306 | - $A = self::ge_frombytes_negate_vartime($pk); |
|
306 | + $A = self::ge_frombytes_negate_vartime( $pk ); |
|
307 | 307 | |
308 | 308 | /** @var string $hDigest */ |
309 | 309 | $hDigest = hash( |
310 | 310 | 'sha512', |
311 | - self::substr($sig, 0, 32) . |
|
312 | - self::substr($pk, 0, 32) . |
|
311 | + self::substr( $sig, 0, 32 ) . |
|
312 | + self::substr( $pk, 0, 32 ) . |
|
313 | 313 | $message, |
314 | 314 | true |
315 | 315 | ); |
316 | 316 | |
317 | 317 | /** @var string $h */ |
318 | - $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
318 | + $h = self::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 ); |
|
319 | 319 | |
320 | 320 | /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ |
321 | 321 | $R = self::ge_double_scalarmult_vartime( |
322 | 322 | $h, |
323 | 323 | $A, |
324 | - self::substr($sig, 32) |
|
324 | + self::substr( $sig, 32 ) |
|
325 | 325 | ); |
326 | 326 | |
327 | 327 | /** @var string $rcheck */ |
328 | - $rcheck = self::ge_tobytes($R); |
|
328 | + $rcheck = self::ge_tobytes( $R ); |
|
329 | 329 | |
330 | 330 | // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
331 | 331 | ParagonIE_Sodium_Compat::$fastMult = $orig; |
332 | 332 | |
333 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
333 | + return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) ); |
|
334 | 334 | } |
335 | 335 | |
336 | 336 | /** |
@@ -341,10 +341,10 @@ discard block |
||
341 | 341 | * @throws SodiumException |
342 | 342 | * @throws TypeError |
343 | 343 | */ |
344 | - public static function check_S_lt_L($S) |
|
344 | + public static function check_S_lt_L( $S ) |
|
345 | 345 | { |
346 | - if (self::strlen($S) < 32) { |
|
347 | - throw new SodiumException('Signature must be 32 bytes'); |
|
346 | + if ( self::strlen( $S ) < 32 ) { |
|
347 | + throw new SodiumException( 'Signature must be 32 bytes' ); |
|
348 | 348 | } |
349 | 349 | static $L = array( |
350 | 350 | 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
@@ -359,14 +359,14 @@ discard block |
||
359 | 359 | |
360 | 360 | do { |
361 | 361 | --$i; |
362 | - $x = self::chrToInt($S[$i]); |
|
362 | + $x = self::chrToInt( $S[ $i ] ); |
|
363 | 363 | $c |= ( |
364 | - (($x - $L[$i]) >> 8) & $n |
|
364 | + ( ( $x - $L[ $i ] ) >> 8 ) & $n |
|
365 | 365 | ); |
366 | 366 | $n &= ( |
367 | - (($x ^ $L[$i]) - 1) >> 8 |
|
367 | + ( ( $x ^ $L[ $i ] ) - 1 ) >> 8 |
|
368 | 368 | ); |
369 | - } while ($i !== 0); |
|
369 | + } while ( $i !== 0 ); |
|
370 | 370 | |
371 | 371 | return $c === 0; |
372 | 372 | } |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | * @throws SodiumException |
378 | 378 | * @throws TypeError |
379 | 379 | */ |
380 | - public static function small_order($R) |
|
380 | + public static function small_order( $R ) |
|
381 | 381 | { |
382 | 382 | static $blocklist = array( |
383 | 383 | /* 0 (order 4) */ |
@@ -466,14 +466,14 @@ discard block |
||
466 | 466 | ) |
467 | 467 | ); |
468 | 468 | /** @var array<int, array<int, int>> $blocklist */ |
469 | - $countBlocklist = count($blocklist); |
|
469 | + $countBlocklist = count( $blocklist ); |
|
470 | 470 | |
471 | - for ($i = 0; $i < $countBlocklist; ++$i) { |
|
471 | + for ( $i = 0; $i < $countBlocklist; ++$i ) { |
|
472 | 472 | $c = 0; |
473 | - for ($j = 0; $j < 32; ++$j) { |
|
474 | - $c |= self::chrToInt($R[$j]) ^ $blocklist[$i][$j]; |
|
473 | + for ( $j = 0; $j < 32; ++$j ) { |
|
474 | + $c |= self::chrToInt( $R[ $j ] ) ^ $blocklist[ $i ][ $j ]; |
|
475 | 475 | } |
476 | - if ($c === 0) { |
|
476 | + if ( $c === 0 ) { |
|
477 | 477 | return true; |
478 | 478 | } |
479 | 479 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_Ed25519 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core32_Ed25519 extends ParagonIE_Sodium_Core32_Curve25519 |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core32_Ed25519 extends ParagonIE_Sodium_Core32_Curve25519 { |
|
12 | 11 | const KEYPAIR_BYTES = 96; |
13 | 12 | const SEED_BYTES = 32; |
14 | 13 | |
@@ -20,8 +19,7 @@ discard block |
||
20 | 19 | * @throws SodiumException |
21 | 20 | * @throws TypeError |
22 | 21 | */ |
23 | - public static function keypair() |
|
24 | - { |
|
22 | + public static function keypair() { |
|
25 | 23 | $seed = random_bytes(self::SEED_BYTES); |
26 | 24 | $pk = ''; |
27 | 25 | $sk = ''; |
@@ -39,8 +37,7 @@ discard block |
||
39 | 37 | * @throws SodiumException |
40 | 38 | * @throws TypeError |
41 | 39 | */ |
42 | - public static function seed_keypair(&$pk, &$sk, $seed) |
|
43 | - { |
|
40 | + public static function seed_keypair(&$pk, &$sk, $seed) { |
|
44 | 41 | if (self::strlen($seed) !== self::SEED_BYTES) { |
45 | 42 | throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
46 | 43 | } |
@@ -58,8 +55,7 @@ discard block |
||
58 | 55 | * @return string |
59 | 56 | * @throws TypeError |
60 | 57 | */ |
61 | - public static function secretkey($keypair) |
|
62 | - { |
|
58 | + public static function secretkey($keypair) { |
|
63 | 59 | if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
64 | 60 | throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
65 | 61 | } |
@@ -74,8 +70,7 @@ discard block |
||
74 | 70 | * @throws RangeException |
75 | 71 | * @throws TypeError |
76 | 72 | */ |
77 | - public static function publickey($keypair) |
|
78 | - { |
|
73 | + public static function publickey($keypair) { |
|
79 | 74 | if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
80 | 75 | throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
81 | 76 | } |
@@ -90,8 +85,7 @@ discard block |
||
90 | 85 | * @throws SodiumException |
91 | 86 | * @throws TypeError |
92 | 87 | */ |
93 | - public static function publickey_from_secretkey($sk) |
|
94 | - { |
|
88 | + public static function publickey_from_secretkey($sk) { |
|
95 | 89 | /** @var string $sk */ |
96 | 90 | $sk = hash('sha512', self::substr($sk, 0, 32), true); |
97 | 91 | $sk[0] = self::intToChr( |
@@ -109,8 +103,7 @@ discard block |
||
109 | 103 | * @throws SodiumException |
110 | 104 | * @throws TypeError |
111 | 105 | */ |
112 | - public static function pk_to_curve25519($pk) |
|
113 | - { |
|
106 | + public static function pk_to_curve25519($pk) { |
|
114 | 107 | if (self::small_order($pk)) { |
115 | 108 | throw new SodiumException('Public key is on a small order'); |
116 | 109 | } |
@@ -151,8 +144,7 @@ discard block |
||
151 | 144 | * @throws SodiumException |
152 | 145 | * @throws TypeError |
153 | 146 | */ |
154 | - public static function sk_to_pk($sk) |
|
155 | - { |
|
147 | + public static function sk_to_pk($sk) { |
|
156 | 148 | return self::ge_p3_tobytes( |
157 | 149 | self::ge_scalarmult_base( |
158 | 150 | self::substr($sk, 0, 32) |
@@ -169,8 +161,7 @@ discard block |
||
169 | 161 | * @throws SodiumException |
170 | 162 | * @throws TypeError |
171 | 163 | */ |
172 | - public static function sign($message, $sk) |
|
173 | - { |
|
164 | + public static function sign($message, $sk) { |
|
174 | 165 | /** @var string $signature */ |
175 | 166 | $signature = self::sign_detached($message, $sk); |
176 | 167 | return $signature . $message; |
@@ -185,8 +176,7 @@ discard block |
||
185 | 176 | * @throws SodiumException |
186 | 177 | * @throws TypeError |
187 | 178 | */ |
188 | - public static function sign_open($message, $pk) |
|
189 | - { |
|
179 | + public static function sign_open($message, $pk) { |
|
190 | 180 | /** @var string $signature */ |
191 | 181 | $signature = self::substr($message, 0, 64); |
192 | 182 | |
@@ -209,8 +199,7 @@ discard block |
||
209 | 199 | * @throws TypeError |
210 | 200 | * @psalm-suppress PossiblyInvalidArgument |
211 | 201 | */ |
212 | - public static function sign_detached($message, $sk) |
|
213 | - { |
|
202 | + public static function sign_detached($message, $sk) { |
|
214 | 203 | # crypto_hash_sha512(az, sk, 32); |
215 | 204 | $az = hash('sha512', self::substr($sk, 0, 32), true); |
216 | 205 | |
@@ -274,8 +263,7 @@ discard block |
||
274 | 263 | * @throws SodiumException |
275 | 264 | * @throws TypeError |
276 | 265 | */ |
277 | - public static function verify_detached($sig, $message, $pk) |
|
278 | - { |
|
266 | + public static function verify_detached($sig, $message, $pk) { |
|
279 | 267 | if (self::strlen($sig) < 64) { |
280 | 268 | throw new SodiumException('Signature is too short'); |
281 | 269 | } |
@@ -341,8 +329,7 @@ discard block |
||
341 | 329 | * @throws SodiumException |
342 | 330 | * @throws TypeError |
343 | 331 | */ |
344 | - public static function check_S_lt_L($S) |
|
345 | - { |
|
332 | + public static function check_S_lt_L($S) { |
|
346 | 333 | if (self::strlen($S) < 32) { |
347 | 334 | throw new SodiumException('Signature must be 32 bytes'); |
348 | 335 | } |
@@ -377,8 +364,7 @@ discard block |
||
377 | 364 | * @throws SodiumException |
378 | 365 | * @throws TypeError |
379 | 366 | */ |
380 | - public static function small_order($R) |
|
381 | - { |
|
367 | + public static function small_order($R) { |
|
382 | 368 | static $blocklist = array( |
383 | 369 | /* 0 (order 4) */ |
384 | 370 | array( |
@@ -1,7 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Util', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_Util', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_Util', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 |
@@ -7,7 +7,6 @@ |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_Util |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core32_Util extends ParagonIE_Sodium_Core_Util |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core32_Util extends ParagonIE_Sodium_Core_Util { |
|
12 | 11 | |
13 | 12 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_SipHash', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,228 +11,228 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class ParagonIE_Sodium_Core32_SipHash extends ParagonIE_Sodium_Core32_Util |
13 | 13 | { |
14 | - /** |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param array<int, ParagonIE_Sodium_Core32_Int64> $v |
|
18 | - * @return array<int, ParagonIE_Sodium_Core32_Int64> |
|
19 | - */ |
|
20 | - public static function sipRound(array $v) |
|
21 | - { |
|
22 | - # v0 += v1; |
|
23 | - $v[0] = $v[0]->addInt64($v[1]); |
|
24 | - |
|
25 | - # v1 = ROTL(v1, 13); |
|
26 | - $v[1] = $v[1]->rotateLeft(13); |
|
27 | - |
|
28 | - # v1 ^= v0; |
|
29 | - $v[1] = $v[1]->xorInt64($v[0]); |
|
30 | - |
|
31 | - # v0=ROTL(v0,32); |
|
32 | - $v[0] = $v[0]->rotateLeft(32); |
|
33 | - |
|
34 | - # v2 += v3; |
|
35 | - $v[2] = $v[2]->addInt64($v[3]); |
|
36 | - |
|
37 | - # v3=ROTL(v3,16); |
|
38 | - $v[3] = $v[3]->rotateLeft(16); |
|
39 | - |
|
40 | - # v3 ^= v2; |
|
41 | - $v[3] = $v[3]->xorInt64($v[2]); |
|
42 | - |
|
43 | - # v0 += v3; |
|
44 | - $v[0] = $v[0]->addInt64($v[3]); |
|
45 | - |
|
46 | - # v3=ROTL(v3,21); |
|
47 | - $v[3] = $v[3]->rotateLeft(21); |
|
48 | - |
|
49 | - # v3 ^= v0; |
|
50 | - $v[3] = $v[3]->xorInt64($v[0]); |
|
51 | - |
|
52 | - # v2 += v1; |
|
53 | - $v[2] = $v[2]->addInt64($v[1]); |
|
54 | - |
|
55 | - # v1=ROTL(v1,17); |
|
56 | - $v[1] = $v[1]->rotateLeft(17); |
|
57 | - |
|
58 | - # v1 ^= v2; |
|
59 | - $v[1] = $v[1]->xorInt64($v[2]); |
|
60 | - |
|
61 | - # v2=ROTL(v2,32) |
|
62 | - $v[2] = $v[2]->rotateLeft(32); |
|
63 | - |
|
64 | - return $v; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @internal You should not use this directly from another application |
|
69 | - * |
|
70 | - * @param string $in |
|
71 | - * @param string $key |
|
72 | - * @return string |
|
73 | - * @throws SodiumException |
|
74 | - * @throws TypeError |
|
75 | - */ |
|
76 | - public static function sipHash24($in, $key) |
|
77 | - { |
|
78 | - $inlen = self::strlen($in); |
|
79 | - |
|
80 | - # /* "somepseudorandomlygeneratedbytes" */ |
|
81 | - # u64 v0 = 0x736f6d6570736575ULL; |
|
82 | - # u64 v1 = 0x646f72616e646f6dULL; |
|
83 | - # u64 v2 = 0x6c7967656e657261ULL; |
|
84 | - # u64 v3 = 0x7465646279746573ULL; |
|
85 | - $v = array( |
|
86 | - new ParagonIE_Sodium_Core32_Int64( |
|
87 | - array(0x736f, 0x6d65, 0x7073, 0x6575) |
|
88 | - ), |
|
89 | - new ParagonIE_Sodium_Core32_Int64( |
|
90 | - array(0x646f, 0x7261, 0x6e64, 0x6f6d) |
|
91 | - ), |
|
92 | - new ParagonIE_Sodium_Core32_Int64( |
|
93 | - array(0x6c79, 0x6765, 0x6e65, 0x7261) |
|
94 | - ), |
|
95 | - new ParagonIE_Sodium_Core32_Int64( |
|
96 | - array(0x7465, 0x6462, 0x7974, 0x6573) |
|
97 | - ) |
|
98 | - ); |
|
99 | - |
|
100 | - # u64 k0 = LOAD64_LE( k ); |
|
101 | - # u64 k1 = LOAD64_LE( k + 8 ); |
|
102 | - $k = array( |
|
103 | - ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
104 | - self::substr($key, 0, 8) |
|
105 | - ), |
|
106 | - ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
107 | - self::substr($key, 8, 8) |
|
108 | - ) |
|
109 | - ); |
|
110 | - |
|
111 | - # b = ( ( u64 )inlen ) << 56; |
|
112 | - $b = new ParagonIE_Sodium_Core32_Int64( |
|
113 | - array(($inlen << 8) & 0xffff, 0, 0, 0) |
|
114 | - ); |
|
115 | - |
|
116 | - # v3 ^= k1; |
|
117 | - $v[3] = $v[3]->xorInt64($k[1]); |
|
118 | - # v2 ^= k0; |
|
119 | - $v[2] = $v[2]->xorInt64($k[0]); |
|
120 | - # v1 ^= k1; |
|
121 | - $v[1] = $v[1]->xorInt64($k[1]); |
|
122 | - # v0 ^= k0; |
|
123 | - $v[0] = $v[0]->xorInt64($k[0]); |
|
124 | - |
|
125 | - $left = $inlen; |
|
126 | - # for ( ; in != end; in += 8 ) |
|
127 | - while ($left >= 8) { |
|
128 | - # m = LOAD64_LE( in ); |
|
129 | - $m = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
130 | - self::substr($in, 0, 8) |
|
131 | - ); |
|
132 | - |
|
133 | - # v3 ^= m; |
|
134 | - $v[3] = $v[3]->xorInt64($m); |
|
135 | - |
|
136 | - # SIPROUND; |
|
137 | - # SIPROUND; |
|
138 | - $v = self::sipRound($v); |
|
139 | - $v = self::sipRound($v); |
|
140 | - |
|
141 | - # v0 ^= m; |
|
142 | - $v[0] = $v[0]->xorInt64($m); |
|
143 | - |
|
144 | - $in = self::substr($in, 8); |
|
145 | - $left -= 8; |
|
146 | - } |
|
147 | - |
|
148 | - # switch( left ) |
|
149 | - # { |
|
150 | - # case 7: b |= ( ( u64 )in[ 6] ) << 48; |
|
151 | - # case 6: b |= ( ( u64 )in[ 5] ) << 40; |
|
152 | - # case 5: b |= ( ( u64 )in[ 4] ) << 32; |
|
153 | - # case 4: b |= ( ( u64 )in[ 3] ) << 24; |
|
154 | - # case 3: b |= ( ( u64 )in[ 2] ) << 16; |
|
155 | - # case 2: b |= ( ( u64 )in[ 1] ) << 8; |
|
156 | - # case 1: b |= ( ( u64 )in[ 0] ); break; |
|
157 | - # case 0: break; |
|
158 | - # } |
|
159 | - switch ($left) { |
|
160 | - case 7: |
|
161 | - $b = $b->orInt64( |
|
162 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
163 | - 0, self::chrToInt($in[6]) << 16 |
|
164 | - ) |
|
165 | - ); |
|
166 | - case 6: |
|
167 | - $b = $b->orInt64( |
|
168 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
169 | - 0, self::chrToInt($in[5]) << 8 |
|
170 | - ) |
|
171 | - ); |
|
172 | - case 5: |
|
173 | - $b = $b->orInt64( |
|
174 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
175 | - 0, self::chrToInt($in[4]) |
|
176 | - ) |
|
177 | - ); |
|
178 | - case 4: |
|
179 | - $b = $b->orInt64( |
|
180 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
181 | - self::chrToInt($in[3]) << 24, 0 |
|
182 | - ) |
|
183 | - ); |
|
184 | - case 3: |
|
185 | - $b = $b->orInt64( |
|
186 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
187 | - self::chrToInt($in[2]) << 16, 0 |
|
188 | - ) |
|
189 | - ); |
|
190 | - case 2: |
|
191 | - $b = $b->orInt64( |
|
192 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
193 | - self::chrToInt($in[1]) << 8, 0 |
|
194 | - ) |
|
195 | - ); |
|
196 | - case 1: |
|
197 | - $b = $b->orInt64( |
|
198 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
199 | - self::chrToInt($in[0]), 0 |
|
200 | - ) |
|
201 | - ); |
|
202 | - case 0: |
|
203 | - break; |
|
204 | - } |
|
205 | - |
|
206 | - # v3 ^= b; |
|
207 | - $v[3] = $v[3]->xorInt64($b); |
|
208 | - |
|
209 | - # SIPROUND; |
|
210 | - # SIPROUND; |
|
211 | - $v = self::sipRound($v); |
|
212 | - $v = self::sipRound($v); |
|
213 | - |
|
214 | - # v0 ^= b; |
|
215 | - $v[0] = $v[0]->xorInt64($b); |
|
216 | - |
|
217 | - // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
|
218 | - # v2 ^= 0xff; |
|
219 | - $v[2]->limbs[3] ^= 0xff; |
|
220 | - |
|
221 | - # SIPROUND; |
|
222 | - # SIPROUND; |
|
223 | - # SIPROUND; |
|
224 | - # SIPROUND; |
|
225 | - $v = self::sipRound($v); |
|
226 | - $v = self::sipRound($v); |
|
227 | - $v = self::sipRound($v); |
|
228 | - $v = self::sipRound($v); |
|
229 | - |
|
230 | - # b = v0 ^ v1 ^ v2 ^ v3; |
|
231 | - # STORE64_LE( out, b ); |
|
232 | - return $v[0] |
|
233 | - ->xorInt64($v[1]) |
|
234 | - ->xorInt64($v[2]) |
|
235 | - ->xorInt64($v[3]) |
|
236 | - ->toReverseString(); |
|
237 | - } |
|
14 | + /** |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param array<int, ParagonIE_Sodium_Core32_Int64> $v |
|
18 | + * @return array<int, ParagonIE_Sodium_Core32_Int64> |
|
19 | + */ |
|
20 | + public static function sipRound(array $v) |
|
21 | + { |
|
22 | + # v0 += v1; |
|
23 | + $v[0] = $v[0]->addInt64($v[1]); |
|
24 | + |
|
25 | + # v1 = ROTL(v1, 13); |
|
26 | + $v[1] = $v[1]->rotateLeft(13); |
|
27 | + |
|
28 | + # v1 ^= v0; |
|
29 | + $v[1] = $v[1]->xorInt64($v[0]); |
|
30 | + |
|
31 | + # v0=ROTL(v0,32); |
|
32 | + $v[0] = $v[0]->rotateLeft(32); |
|
33 | + |
|
34 | + # v2 += v3; |
|
35 | + $v[2] = $v[2]->addInt64($v[3]); |
|
36 | + |
|
37 | + # v3=ROTL(v3,16); |
|
38 | + $v[3] = $v[3]->rotateLeft(16); |
|
39 | + |
|
40 | + # v3 ^= v2; |
|
41 | + $v[3] = $v[3]->xorInt64($v[2]); |
|
42 | + |
|
43 | + # v0 += v3; |
|
44 | + $v[0] = $v[0]->addInt64($v[3]); |
|
45 | + |
|
46 | + # v3=ROTL(v3,21); |
|
47 | + $v[3] = $v[3]->rotateLeft(21); |
|
48 | + |
|
49 | + # v3 ^= v0; |
|
50 | + $v[3] = $v[3]->xorInt64($v[0]); |
|
51 | + |
|
52 | + # v2 += v1; |
|
53 | + $v[2] = $v[2]->addInt64($v[1]); |
|
54 | + |
|
55 | + # v1=ROTL(v1,17); |
|
56 | + $v[1] = $v[1]->rotateLeft(17); |
|
57 | + |
|
58 | + # v1 ^= v2; |
|
59 | + $v[1] = $v[1]->xorInt64($v[2]); |
|
60 | + |
|
61 | + # v2=ROTL(v2,32) |
|
62 | + $v[2] = $v[2]->rotateLeft(32); |
|
63 | + |
|
64 | + return $v; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @internal You should not use this directly from another application |
|
69 | + * |
|
70 | + * @param string $in |
|
71 | + * @param string $key |
|
72 | + * @return string |
|
73 | + * @throws SodiumException |
|
74 | + * @throws TypeError |
|
75 | + */ |
|
76 | + public static function sipHash24($in, $key) |
|
77 | + { |
|
78 | + $inlen = self::strlen($in); |
|
79 | + |
|
80 | + # /* "somepseudorandomlygeneratedbytes" */ |
|
81 | + # u64 v0 = 0x736f6d6570736575ULL; |
|
82 | + # u64 v1 = 0x646f72616e646f6dULL; |
|
83 | + # u64 v2 = 0x6c7967656e657261ULL; |
|
84 | + # u64 v3 = 0x7465646279746573ULL; |
|
85 | + $v = array( |
|
86 | + new ParagonIE_Sodium_Core32_Int64( |
|
87 | + array(0x736f, 0x6d65, 0x7073, 0x6575) |
|
88 | + ), |
|
89 | + new ParagonIE_Sodium_Core32_Int64( |
|
90 | + array(0x646f, 0x7261, 0x6e64, 0x6f6d) |
|
91 | + ), |
|
92 | + new ParagonIE_Sodium_Core32_Int64( |
|
93 | + array(0x6c79, 0x6765, 0x6e65, 0x7261) |
|
94 | + ), |
|
95 | + new ParagonIE_Sodium_Core32_Int64( |
|
96 | + array(0x7465, 0x6462, 0x7974, 0x6573) |
|
97 | + ) |
|
98 | + ); |
|
99 | + |
|
100 | + # u64 k0 = LOAD64_LE( k ); |
|
101 | + # u64 k1 = LOAD64_LE( k + 8 ); |
|
102 | + $k = array( |
|
103 | + ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
104 | + self::substr($key, 0, 8) |
|
105 | + ), |
|
106 | + ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
107 | + self::substr($key, 8, 8) |
|
108 | + ) |
|
109 | + ); |
|
110 | + |
|
111 | + # b = ( ( u64 )inlen ) << 56; |
|
112 | + $b = new ParagonIE_Sodium_Core32_Int64( |
|
113 | + array(($inlen << 8) & 0xffff, 0, 0, 0) |
|
114 | + ); |
|
115 | + |
|
116 | + # v3 ^= k1; |
|
117 | + $v[3] = $v[3]->xorInt64($k[1]); |
|
118 | + # v2 ^= k0; |
|
119 | + $v[2] = $v[2]->xorInt64($k[0]); |
|
120 | + # v1 ^= k1; |
|
121 | + $v[1] = $v[1]->xorInt64($k[1]); |
|
122 | + # v0 ^= k0; |
|
123 | + $v[0] = $v[0]->xorInt64($k[0]); |
|
124 | + |
|
125 | + $left = $inlen; |
|
126 | + # for ( ; in != end; in += 8 ) |
|
127 | + while ($left >= 8) { |
|
128 | + # m = LOAD64_LE( in ); |
|
129 | + $m = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
130 | + self::substr($in, 0, 8) |
|
131 | + ); |
|
132 | + |
|
133 | + # v3 ^= m; |
|
134 | + $v[3] = $v[3]->xorInt64($m); |
|
135 | + |
|
136 | + # SIPROUND; |
|
137 | + # SIPROUND; |
|
138 | + $v = self::sipRound($v); |
|
139 | + $v = self::sipRound($v); |
|
140 | + |
|
141 | + # v0 ^= m; |
|
142 | + $v[0] = $v[0]->xorInt64($m); |
|
143 | + |
|
144 | + $in = self::substr($in, 8); |
|
145 | + $left -= 8; |
|
146 | + } |
|
147 | + |
|
148 | + # switch( left ) |
|
149 | + # { |
|
150 | + # case 7: b |= ( ( u64 )in[ 6] ) << 48; |
|
151 | + # case 6: b |= ( ( u64 )in[ 5] ) << 40; |
|
152 | + # case 5: b |= ( ( u64 )in[ 4] ) << 32; |
|
153 | + # case 4: b |= ( ( u64 )in[ 3] ) << 24; |
|
154 | + # case 3: b |= ( ( u64 )in[ 2] ) << 16; |
|
155 | + # case 2: b |= ( ( u64 )in[ 1] ) << 8; |
|
156 | + # case 1: b |= ( ( u64 )in[ 0] ); break; |
|
157 | + # case 0: break; |
|
158 | + # } |
|
159 | + switch ($left) { |
|
160 | + case 7: |
|
161 | + $b = $b->orInt64( |
|
162 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
163 | + 0, self::chrToInt($in[6]) << 16 |
|
164 | + ) |
|
165 | + ); |
|
166 | + case 6: |
|
167 | + $b = $b->orInt64( |
|
168 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
169 | + 0, self::chrToInt($in[5]) << 8 |
|
170 | + ) |
|
171 | + ); |
|
172 | + case 5: |
|
173 | + $b = $b->orInt64( |
|
174 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
175 | + 0, self::chrToInt($in[4]) |
|
176 | + ) |
|
177 | + ); |
|
178 | + case 4: |
|
179 | + $b = $b->orInt64( |
|
180 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
181 | + self::chrToInt($in[3]) << 24, 0 |
|
182 | + ) |
|
183 | + ); |
|
184 | + case 3: |
|
185 | + $b = $b->orInt64( |
|
186 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
187 | + self::chrToInt($in[2]) << 16, 0 |
|
188 | + ) |
|
189 | + ); |
|
190 | + case 2: |
|
191 | + $b = $b->orInt64( |
|
192 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
193 | + self::chrToInt($in[1]) << 8, 0 |
|
194 | + ) |
|
195 | + ); |
|
196 | + case 1: |
|
197 | + $b = $b->orInt64( |
|
198 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
199 | + self::chrToInt($in[0]), 0 |
|
200 | + ) |
|
201 | + ); |
|
202 | + case 0: |
|
203 | + break; |
|
204 | + } |
|
205 | + |
|
206 | + # v3 ^= b; |
|
207 | + $v[3] = $v[3]->xorInt64($b); |
|
208 | + |
|
209 | + # SIPROUND; |
|
210 | + # SIPROUND; |
|
211 | + $v = self::sipRound($v); |
|
212 | + $v = self::sipRound($v); |
|
213 | + |
|
214 | + # v0 ^= b; |
|
215 | + $v[0] = $v[0]->xorInt64($b); |
|
216 | + |
|
217 | + // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
|
218 | + # v2 ^= 0xff; |
|
219 | + $v[2]->limbs[3] ^= 0xff; |
|
220 | + |
|
221 | + # SIPROUND; |
|
222 | + # SIPROUND; |
|
223 | + # SIPROUND; |
|
224 | + # SIPROUND; |
|
225 | + $v = self::sipRound($v); |
|
226 | + $v = self::sipRound($v); |
|
227 | + $v = self::sipRound($v); |
|
228 | + $v = self::sipRound($v); |
|
229 | + |
|
230 | + # b = v0 ^ v1 ^ v2 ^ v3; |
|
231 | + # STORE64_LE( out, b ); |
|
232 | + return $v[0] |
|
233 | + ->xorInt64($v[1]) |
|
234 | + ->xorInt64($v[2]) |
|
235 | + ->xorInt64($v[3]) |
|
236 | + ->toReverseString(); |
|
237 | + } |
|
238 | 238 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_SipHash', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_SipHash', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -17,49 +17,49 @@ discard block |
||
17 | 17 | * @param array<int, ParagonIE_Sodium_Core32_Int64> $v |
18 | 18 | * @return array<int, ParagonIE_Sodium_Core32_Int64> |
19 | 19 | */ |
20 | - public static function sipRound(array $v) |
|
20 | + public static function sipRound( array $v ) |
|
21 | 21 | { |
22 | 22 | # v0 += v1; |
23 | - $v[0] = $v[0]->addInt64($v[1]); |
|
23 | + $v[ 0 ] = $v[ 0 ]->addInt64( $v[ 1 ] ); |
|
24 | 24 | |
25 | 25 | # v1 = ROTL(v1, 13); |
26 | - $v[1] = $v[1]->rotateLeft(13); |
|
26 | + $v[ 1 ] = $v[ 1 ]->rotateLeft( 13 ); |
|
27 | 27 | |
28 | 28 | # v1 ^= v0; |
29 | - $v[1] = $v[1]->xorInt64($v[0]); |
|
29 | + $v[ 1 ] = $v[ 1 ]->xorInt64( $v[ 0 ] ); |
|
30 | 30 | |
31 | 31 | # v0=ROTL(v0,32); |
32 | - $v[0] = $v[0]->rotateLeft(32); |
|
32 | + $v[ 0 ] = $v[ 0 ]->rotateLeft( 32 ); |
|
33 | 33 | |
34 | 34 | # v2 += v3; |
35 | - $v[2] = $v[2]->addInt64($v[3]); |
|
35 | + $v[ 2 ] = $v[ 2 ]->addInt64( $v[ 3 ] ); |
|
36 | 36 | |
37 | 37 | # v3=ROTL(v3,16); |
38 | - $v[3] = $v[3]->rotateLeft(16); |
|
38 | + $v[ 3 ] = $v[ 3 ]->rotateLeft( 16 ); |
|
39 | 39 | |
40 | 40 | # v3 ^= v2; |
41 | - $v[3] = $v[3]->xorInt64($v[2]); |
|
41 | + $v[ 3 ] = $v[ 3 ]->xorInt64( $v[ 2 ] ); |
|
42 | 42 | |
43 | 43 | # v0 += v3; |
44 | - $v[0] = $v[0]->addInt64($v[3]); |
|
44 | + $v[ 0 ] = $v[ 0 ]->addInt64( $v[ 3 ] ); |
|
45 | 45 | |
46 | 46 | # v3=ROTL(v3,21); |
47 | - $v[3] = $v[3]->rotateLeft(21); |
|
47 | + $v[ 3 ] = $v[ 3 ]->rotateLeft( 21 ); |
|
48 | 48 | |
49 | 49 | # v3 ^= v0; |
50 | - $v[3] = $v[3]->xorInt64($v[0]); |
|
50 | + $v[ 3 ] = $v[ 3 ]->xorInt64( $v[ 0 ] ); |
|
51 | 51 | |
52 | 52 | # v2 += v1; |
53 | - $v[2] = $v[2]->addInt64($v[1]); |
|
53 | + $v[ 2 ] = $v[ 2 ]->addInt64( $v[ 1 ] ); |
|
54 | 54 | |
55 | 55 | # v1=ROTL(v1,17); |
56 | - $v[1] = $v[1]->rotateLeft(17); |
|
56 | + $v[ 1 ] = $v[ 1 ]->rotateLeft( 17 ); |
|
57 | 57 | |
58 | 58 | # v1 ^= v2; |
59 | - $v[1] = $v[1]->xorInt64($v[2]); |
|
59 | + $v[ 1 ] = $v[ 1 ]->xorInt64( $v[ 2 ] ); |
|
60 | 60 | |
61 | 61 | # v2=ROTL(v2,32) |
62 | - $v[2] = $v[2]->rotateLeft(32); |
|
62 | + $v[ 2 ] = $v[ 2 ]->rotateLeft( 32 ); |
|
63 | 63 | |
64 | 64 | return $v; |
65 | 65 | } |
@@ -73,9 +73,9 @@ discard block |
||
73 | 73 | * @throws SodiumException |
74 | 74 | * @throws TypeError |
75 | 75 | */ |
76 | - public static function sipHash24($in, $key) |
|
76 | + public static function sipHash24( $in, $key ) |
|
77 | 77 | { |
78 | - $inlen = self::strlen($in); |
|
78 | + $inlen = self::strlen( $in ); |
|
79 | 79 | |
80 | 80 | # /* "somepseudorandomlygeneratedbytes" */ |
81 | 81 | # u64 v0 = 0x736f6d6570736575ULL; |
@@ -84,16 +84,16 @@ discard block |
||
84 | 84 | # u64 v3 = 0x7465646279746573ULL; |
85 | 85 | $v = array( |
86 | 86 | new ParagonIE_Sodium_Core32_Int64( |
87 | - array(0x736f, 0x6d65, 0x7073, 0x6575) |
|
87 | + array( 0x736f, 0x6d65, 0x7073, 0x6575 ) |
|
88 | 88 | ), |
89 | 89 | new ParagonIE_Sodium_Core32_Int64( |
90 | - array(0x646f, 0x7261, 0x6e64, 0x6f6d) |
|
90 | + array( 0x646f, 0x7261, 0x6e64, 0x6f6d ) |
|
91 | 91 | ), |
92 | 92 | new ParagonIE_Sodium_Core32_Int64( |
93 | - array(0x6c79, 0x6765, 0x6e65, 0x7261) |
|
93 | + array( 0x6c79, 0x6765, 0x6e65, 0x7261 ) |
|
94 | 94 | ), |
95 | 95 | new ParagonIE_Sodium_Core32_Int64( |
96 | - array(0x7465, 0x6462, 0x7974, 0x6573) |
|
96 | + array( 0x7465, 0x6462, 0x7974, 0x6573 ) |
|
97 | 97 | ) |
98 | 98 | ); |
99 | 99 | |
@@ -101,47 +101,47 @@ discard block |
||
101 | 101 | # u64 k1 = LOAD64_LE( k + 8 ); |
102 | 102 | $k = array( |
103 | 103 | ParagonIE_Sodium_Core32_Int64::fromReverseString( |
104 | - self::substr($key, 0, 8) |
|
104 | + self::substr( $key, 0, 8 ) |
|
105 | 105 | ), |
106 | 106 | ParagonIE_Sodium_Core32_Int64::fromReverseString( |
107 | - self::substr($key, 8, 8) |
|
107 | + self::substr( $key, 8, 8 ) |
|
108 | 108 | ) |
109 | 109 | ); |
110 | 110 | |
111 | 111 | # b = ( ( u64 )inlen ) << 56; |
112 | 112 | $b = new ParagonIE_Sodium_Core32_Int64( |
113 | - array(($inlen << 8) & 0xffff, 0, 0, 0) |
|
113 | + array( ( $inlen << 8 ) & 0xffff, 0, 0, 0 ) |
|
114 | 114 | ); |
115 | 115 | |
116 | 116 | # v3 ^= k1; |
117 | - $v[3] = $v[3]->xorInt64($k[1]); |
|
117 | + $v[ 3 ] = $v[ 3 ]->xorInt64( $k[ 1 ] ); |
|
118 | 118 | # v2 ^= k0; |
119 | - $v[2] = $v[2]->xorInt64($k[0]); |
|
119 | + $v[ 2 ] = $v[ 2 ]->xorInt64( $k[ 0 ] ); |
|
120 | 120 | # v1 ^= k1; |
121 | - $v[1] = $v[1]->xorInt64($k[1]); |
|
121 | + $v[ 1 ] = $v[ 1 ]->xorInt64( $k[ 1 ] ); |
|
122 | 122 | # v0 ^= k0; |
123 | - $v[0] = $v[0]->xorInt64($k[0]); |
|
123 | + $v[ 0 ] = $v[ 0 ]->xorInt64( $k[ 0 ] ); |
|
124 | 124 | |
125 | 125 | $left = $inlen; |
126 | 126 | # for ( ; in != end; in += 8 ) |
127 | - while ($left >= 8) { |
|
127 | + while ( $left >= 8 ) { |
|
128 | 128 | # m = LOAD64_LE( in ); |
129 | 129 | $m = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
130 | - self::substr($in, 0, 8) |
|
130 | + self::substr( $in, 0, 8 ) |
|
131 | 131 | ); |
132 | 132 | |
133 | 133 | # v3 ^= m; |
134 | - $v[3] = $v[3]->xorInt64($m); |
|
134 | + $v[ 3 ] = $v[ 3 ]->xorInt64( $m ); |
|
135 | 135 | |
136 | 136 | # SIPROUND; |
137 | 137 | # SIPROUND; |
138 | - $v = self::sipRound($v); |
|
139 | - $v = self::sipRound($v); |
|
138 | + $v = self::sipRound( $v ); |
|
139 | + $v = self::sipRound( $v ); |
|
140 | 140 | |
141 | 141 | # v0 ^= m; |
142 | - $v[0] = $v[0]->xorInt64($m); |
|
142 | + $v[ 0 ] = $v[ 0 ]->xorInt64( $m ); |
|
143 | 143 | |
144 | - $in = self::substr($in, 8); |
|
144 | + $in = self::substr( $in, 8 ); |
|
145 | 145 | $left -= 8; |
146 | 146 | } |
147 | 147 | |
@@ -156,47 +156,47 @@ discard block |
||
156 | 156 | # case 1: b |= ( ( u64 )in[ 0] ); break; |
157 | 157 | # case 0: break; |
158 | 158 | # } |
159 | - switch ($left) { |
|
159 | + switch ( $left ) { |
|
160 | 160 | case 7: |
161 | 161 | $b = $b->orInt64( |
162 | 162 | ParagonIE_Sodium_Core32_Int64::fromInts( |
163 | - 0, self::chrToInt($in[6]) << 16 |
|
163 | + 0, self::chrToInt( $in[ 6 ] ) << 16 |
|
164 | 164 | ) |
165 | 165 | ); |
166 | 166 | case 6: |
167 | 167 | $b = $b->orInt64( |
168 | 168 | ParagonIE_Sodium_Core32_Int64::fromInts( |
169 | - 0, self::chrToInt($in[5]) << 8 |
|
169 | + 0, self::chrToInt( $in[ 5 ] ) << 8 |
|
170 | 170 | ) |
171 | 171 | ); |
172 | 172 | case 5: |
173 | 173 | $b = $b->orInt64( |
174 | 174 | ParagonIE_Sodium_Core32_Int64::fromInts( |
175 | - 0, self::chrToInt($in[4]) |
|
175 | + 0, self::chrToInt( $in[ 4 ] ) |
|
176 | 176 | ) |
177 | 177 | ); |
178 | 178 | case 4: |
179 | 179 | $b = $b->orInt64( |
180 | 180 | ParagonIE_Sodium_Core32_Int64::fromInts( |
181 | - self::chrToInt($in[3]) << 24, 0 |
|
181 | + self::chrToInt( $in[ 3 ] ) << 24, 0 |
|
182 | 182 | ) |
183 | 183 | ); |
184 | 184 | case 3: |
185 | 185 | $b = $b->orInt64( |
186 | 186 | ParagonIE_Sodium_Core32_Int64::fromInts( |
187 | - self::chrToInt($in[2]) << 16, 0 |
|
187 | + self::chrToInt( $in[ 2 ] ) << 16, 0 |
|
188 | 188 | ) |
189 | 189 | ); |
190 | 190 | case 2: |
191 | 191 | $b = $b->orInt64( |
192 | 192 | ParagonIE_Sodium_Core32_Int64::fromInts( |
193 | - self::chrToInt($in[1]) << 8, 0 |
|
193 | + self::chrToInt( $in[ 1 ] ) << 8, 0 |
|
194 | 194 | ) |
195 | 195 | ); |
196 | 196 | case 1: |
197 | 197 | $b = $b->orInt64( |
198 | 198 | ParagonIE_Sodium_Core32_Int64::fromInts( |
199 | - self::chrToInt($in[0]), 0 |
|
199 | + self::chrToInt( $in[ 0 ] ), 0 |
|
200 | 200 | ) |
201 | 201 | ); |
202 | 202 | case 0: |
@@ -204,35 +204,35 @@ discard block |
||
204 | 204 | } |
205 | 205 | |
206 | 206 | # v3 ^= b; |
207 | - $v[3] = $v[3]->xorInt64($b); |
|
207 | + $v[ 3 ] = $v[ 3 ]->xorInt64( $b ); |
|
208 | 208 | |
209 | 209 | # SIPROUND; |
210 | 210 | # SIPROUND; |
211 | - $v = self::sipRound($v); |
|
212 | - $v = self::sipRound($v); |
|
211 | + $v = self::sipRound( $v ); |
|
212 | + $v = self::sipRound( $v ); |
|
213 | 213 | |
214 | 214 | # v0 ^= b; |
215 | - $v[0] = $v[0]->xorInt64($b); |
|
215 | + $v[ 0 ] = $v[ 0 ]->xorInt64( $b ); |
|
216 | 216 | |
217 | 217 | // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
218 | 218 | # v2 ^= 0xff; |
219 | - $v[2]->limbs[3] ^= 0xff; |
|
219 | + $v[ 2 ]->limbs[ 3 ] ^= 0xff; |
|
220 | 220 | |
221 | 221 | # SIPROUND; |
222 | 222 | # SIPROUND; |
223 | 223 | # SIPROUND; |
224 | 224 | # SIPROUND; |
225 | - $v = self::sipRound($v); |
|
226 | - $v = self::sipRound($v); |
|
227 | - $v = self::sipRound($v); |
|
228 | - $v = self::sipRound($v); |
|
225 | + $v = self::sipRound( $v ); |
|
226 | + $v = self::sipRound( $v ); |
|
227 | + $v = self::sipRound( $v ); |
|
228 | + $v = self::sipRound( $v ); |
|
229 | 229 | |
230 | 230 | # b = v0 ^ v1 ^ v2 ^ v3; |
231 | 231 | # STORE64_LE( out, b ); |
232 | - return $v[0] |
|
233 | - ->xorInt64($v[1]) |
|
234 | - ->xorInt64($v[2]) |
|
235 | - ->xorInt64($v[3]) |
|
232 | + return $v[ 0 ] |
|
233 | + ->xorInt64( $v[ 1 ] ) |
|
234 | + ->xorInt64( $v[ 2 ] ) |
|
235 | + ->xorInt64( $v[ 3 ] ) |
|
236 | 236 | ->toReverseString(); |
237 | 237 | } |
238 | 238 | } |
@@ -9,16 +9,14 @@ discard block |
||
9 | 9 | * |
10 | 10 | * Only uses 32-bit arithmetic, while the original SipHash used 64-bit integers |
11 | 11 | */ |
12 | -class ParagonIE_Sodium_Core32_SipHash extends ParagonIE_Sodium_Core32_Util |
|
13 | -{ |
|
12 | +class ParagonIE_Sodium_Core32_SipHash extends ParagonIE_Sodium_Core32_Util { |
|
14 | 13 | /** |
15 | 14 | * @internal You should not use this directly from another application |
16 | 15 | * |
17 | 16 | * @param array<int, ParagonIE_Sodium_Core32_Int64> $v |
18 | 17 | * @return array<int, ParagonIE_Sodium_Core32_Int64> |
19 | 18 | */ |
20 | - public static function sipRound(array $v) |
|
21 | - { |
|
19 | + public static function sipRound(array $v) { |
|
22 | 20 | # v0 += v1; |
23 | 21 | $v[0] = $v[0]->addInt64($v[1]); |
24 | 22 | |
@@ -73,8 +71,7 @@ discard block |
||
73 | 71 | * @throws SodiumException |
74 | 72 | * @throws TypeError |
75 | 73 | */ |
76 | - public static function sipHash24($in, $key) |
|
77 | - { |
|
74 | + public static function sipHash24($in, $key) { |
|
78 | 75 | $inlen = self::strlen($in); |
79 | 76 | |
80 | 77 | # /* "somepseudorandomlygeneratedbytes" */ |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_XChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,56 +9,56 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_XChaCha20 extends ParagonIE_Sodium_Core32_HChaCha20 |
11 | 11 | { |
12 | - /** |
|
13 | - * @internal You should not use this directly from another application |
|
14 | - * |
|
15 | - * @param int $len |
|
16 | - * @param string $nonce |
|
17 | - * @param string $key |
|
18 | - * @return string |
|
19 | - * @throws SodiumException |
|
20 | - * @throws TypeError |
|
21 | - */ |
|
22 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
23 | - { |
|
24 | - if (self::strlen($nonce) !== 24) { |
|
25 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
26 | - } |
|
27 | - return self::encryptBytes( |
|
28 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx( |
|
29 | - self::hChaCha20( |
|
30 | - self::substr($nonce, 0, 16), |
|
31 | - $key |
|
32 | - ), |
|
33 | - self::substr($nonce, 16, 8) |
|
34 | - ), |
|
35 | - str_repeat("\x00", $len) |
|
36 | - ); |
|
37 | - } |
|
12 | + /** |
|
13 | + * @internal You should not use this directly from another application |
|
14 | + * |
|
15 | + * @param int $len |
|
16 | + * @param string $nonce |
|
17 | + * @param string $key |
|
18 | + * @return string |
|
19 | + * @throws SodiumException |
|
20 | + * @throws TypeError |
|
21 | + */ |
|
22 | + public static function stream($len = 64, $nonce = '', $key = '') |
|
23 | + { |
|
24 | + if (self::strlen($nonce) !== 24) { |
|
25 | + throw new SodiumException('Nonce must be 24 bytes long'); |
|
26 | + } |
|
27 | + return self::encryptBytes( |
|
28 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx( |
|
29 | + self::hChaCha20( |
|
30 | + self::substr($nonce, 0, 16), |
|
31 | + $key |
|
32 | + ), |
|
33 | + self::substr($nonce, 16, 8) |
|
34 | + ), |
|
35 | + str_repeat("\x00", $len) |
|
36 | + ); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @internal You should not use this directly from another application |
|
41 | - * |
|
42 | - * @param string $message |
|
43 | - * @param string $nonce |
|
44 | - * @param string $key |
|
45 | - * @param string $ic |
|
46 | - * @return string |
|
47 | - * @throws SodiumException |
|
48 | - * @throws TypeError |
|
49 | - */ |
|
50 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
51 | - { |
|
52 | - if (self::strlen($nonce) !== 24) { |
|
53 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
54 | - } |
|
55 | - return self::encryptBytes( |
|
56 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx( |
|
57 | - self::hChaCha20(self::substr($nonce, 0, 16), $key), |
|
58 | - self::substr($nonce, 16, 8), |
|
59 | - $ic |
|
60 | - ), |
|
61 | - $message |
|
62 | - ); |
|
63 | - } |
|
39 | + /** |
|
40 | + * @internal You should not use this directly from another application |
|
41 | + * |
|
42 | + * @param string $message |
|
43 | + * @param string $nonce |
|
44 | + * @param string $key |
|
45 | + * @param string $ic |
|
46 | + * @return string |
|
47 | + * @throws SodiumException |
|
48 | + * @throws TypeError |
|
49 | + */ |
|
50 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
51 | + { |
|
52 | + if (self::strlen($nonce) !== 24) { |
|
53 | + throw new SodiumException('Nonce must be 24 bytes long'); |
|
54 | + } |
|
55 | + return self::encryptBytes( |
|
56 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx( |
|
57 | + self::hChaCha20(self::substr($nonce, 0, 16), $key), |
|
58 | + self::substr($nonce, 16, 8), |
|
59 | + $ic |
|
60 | + ), |
|
61 | + $message |
|
62 | + ); |
|
63 | + } |
|
64 | 64 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_XChaCha20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_XChaCha20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -19,20 +19,20 @@ discard block |
||
19 | 19 | * @throws SodiumException |
20 | 20 | * @throws TypeError |
21 | 21 | */ |
22 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
22 | + public static function stream( $len = 64, $nonce = '', $key = '' ) |
|
23 | 23 | { |
24 | - if (self::strlen($nonce) !== 24) { |
|
25 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
24 | + if ( self::strlen( $nonce ) !== 24 ) { |
|
25 | + throw new SodiumException( 'Nonce must be 24 bytes long' ); |
|
26 | 26 | } |
27 | 27 | return self::encryptBytes( |
28 | 28 | new ParagonIE_Sodium_Core32_ChaCha20_Ctx( |
29 | 29 | self::hChaCha20( |
30 | - self::substr($nonce, 0, 16), |
|
30 | + self::substr( $nonce, 0, 16 ), |
|
31 | 31 | $key |
32 | 32 | ), |
33 | - self::substr($nonce, 16, 8) |
|
33 | + self::substr( $nonce, 16, 8 ) |
|
34 | 34 | ), |
35 | - str_repeat("\x00", $len) |
|
35 | + str_repeat( "\x00", $len ) |
|
36 | 36 | ); |
37 | 37 | } |
38 | 38 | |
@@ -47,15 +47,15 @@ discard block |
||
47 | 47 | * @throws SodiumException |
48 | 48 | * @throws TypeError |
49 | 49 | */ |
50 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
50 | + public static function streamXorIc( $message, $nonce = '', $key = '', $ic = '' ) |
|
51 | 51 | { |
52 | - if (self::strlen($nonce) !== 24) { |
|
53 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
52 | + if ( self::strlen( $nonce ) !== 24 ) { |
|
53 | + throw new SodiumException( 'Nonce must be 24 bytes long' ); |
|
54 | 54 | } |
55 | 55 | return self::encryptBytes( |
56 | 56 | new ParagonIE_Sodium_Core32_ChaCha20_Ctx( |
57 | - self::hChaCha20(self::substr($nonce, 0, 16), $key), |
|
58 | - self::substr($nonce, 16, 8), |
|
57 | + self::hChaCha20( self::substr( $nonce, 0, 16 ), $key ), |
|
58 | + self::substr( $nonce, 16, 8 ), |
|
59 | 59 | $ic |
60 | 60 | ), |
61 | 61 | $message |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_XChaCha20 |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core32_XChaCha20 extends ParagonIE_Sodium_Core32_HChaCha20 |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core32_XChaCha20 extends ParagonIE_Sodium_Core32_HChaCha20 { |
|
12 | 11 | /** |
13 | 12 | * @internal You should not use this directly from another application |
14 | 13 | * |
@@ -19,8 +18,7 @@ discard block |
||
19 | 18 | * @throws SodiumException |
20 | 19 | * @throws TypeError |
21 | 20 | */ |
22 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
23 | - { |
|
21 | + public static function stream($len = 64, $nonce = '', $key = '') { |
|
24 | 22 | if (self::strlen($nonce) !== 24) { |
25 | 23 | throw new SodiumException('Nonce must be 24 bytes long'); |
26 | 24 | } |
@@ -47,8 +45,7 @@ discard block |
||
47 | 45 | * @throws SodiumException |
48 | 46 | * @throws TypeError |
49 | 47 | */ |
50 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
51 | - { |
|
48 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') { |
|
52 | 49 | if (self::strlen($nonce) !== 24) { |
53 | 50 | throw new SodiumException('Nonce must be 24 bytes long'); |
54 | 51 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_X25519', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,337 +9,337 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519 |
11 | 11 | { |
12 | - /** |
|
13 | - * Alters the objects passed to this method in place. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
18 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g |
|
19 | - * @param int $b |
|
20 | - * @return void |
|
21 | - * @throws SodiumException |
|
22 | - * @throws TypeError |
|
23 | - * @psalm-suppress MixedMethodCall |
|
24 | - */ |
|
25 | - public static function fe_cswap( |
|
26 | - ParagonIE_Sodium_Core32_Curve25519_Fe $f, |
|
27 | - ParagonIE_Sodium_Core32_Curve25519_Fe $g, |
|
28 | - $b = 0 |
|
29 | - ) { |
|
30 | - $f0 = (int) $f[0]->toInt(); |
|
31 | - $f1 = (int) $f[1]->toInt(); |
|
32 | - $f2 = (int) $f[2]->toInt(); |
|
33 | - $f3 = (int) $f[3]->toInt(); |
|
34 | - $f4 = (int) $f[4]->toInt(); |
|
35 | - $f5 = (int) $f[5]->toInt(); |
|
36 | - $f6 = (int) $f[6]->toInt(); |
|
37 | - $f7 = (int) $f[7]->toInt(); |
|
38 | - $f8 = (int) $f[8]->toInt(); |
|
39 | - $f9 = (int) $f[9]->toInt(); |
|
40 | - $g0 = (int) $g[0]->toInt(); |
|
41 | - $g1 = (int) $g[1]->toInt(); |
|
42 | - $g2 = (int) $g[2]->toInt(); |
|
43 | - $g3 = (int) $g[3]->toInt(); |
|
44 | - $g4 = (int) $g[4]->toInt(); |
|
45 | - $g5 = (int) $g[5]->toInt(); |
|
46 | - $g6 = (int) $g[6]->toInt(); |
|
47 | - $g7 = (int) $g[7]->toInt(); |
|
48 | - $g8 = (int) $g[8]->toInt(); |
|
49 | - $g9 = (int) $g[9]->toInt(); |
|
50 | - $b = -$b; |
|
51 | - /** @var int $x0 */ |
|
52 | - $x0 = ($f0 ^ $g0) & $b; |
|
53 | - /** @var int $x1 */ |
|
54 | - $x1 = ($f1 ^ $g1) & $b; |
|
55 | - /** @var int $x2 */ |
|
56 | - $x2 = ($f2 ^ $g2) & $b; |
|
57 | - /** @var int $x3 */ |
|
58 | - $x3 = ($f3 ^ $g3) & $b; |
|
59 | - /** @var int $x4 */ |
|
60 | - $x4 = ($f4 ^ $g4) & $b; |
|
61 | - /** @var int $x5 */ |
|
62 | - $x5 = ($f5 ^ $g5) & $b; |
|
63 | - /** @var int $x6 */ |
|
64 | - $x6 = ($f6 ^ $g6) & $b; |
|
65 | - /** @var int $x7 */ |
|
66 | - $x7 = ($f7 ^ $g7) & $b; |
|
67 | - /** @var int $x8 */ |
|
68 | - $x8 = ($f8 ^ $g8) & $b; |
|
69 | - /** @var int $x9 */ |
|
70 | - $x9 = ($f9 ^ $g9) & $b; |
|
71 | - $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
72 | - $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
73 | - $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
74 | - $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
75 | - $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
76 | - $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
77 | - $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
78 | - $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
79 | - $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
80 | - $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
81 | - $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
82 | - $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
83 | - $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
84 | - $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
85 | - $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
86 | - $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
87 | - $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
88 | - $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
89 | - $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
90 | - $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @internal You should not use this directly from another application |
|
95 | - * |
|
96 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
97 | - * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
98 | - * @throws SodiumException |
|
99 | - * @throws TypeError |
|
100 | - * @psalm-suppress MixedAssignment |
|
101 | - * @psalm-suppress MixedMethodCall |
|
102 | - */ |
|
103 | - public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
104 | - { |
|
105 | - /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */ |
|
106 | - $h = array(); |
|
107 | - for ($i = 0; $i < 10; ++$i) { |
|
108 | - $h[$i] = $f[$i]->toInt64()->mulInt(121666, 17); |
|
109 | - } |
|
110 | - |
|
111 | - $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
112 | - $h[0] = $h[0]->addInt64($carry9->mulInt(19, 5)); |
|
113 | - $h[9] = $h[9]->subInt64($carry9->shiftLeft(25)); |
|
114 | - |
|
115 | - $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
116 | - $h[2] = $h[2]->addInt64($carry1); |
|
117 | - $h[1] = $h[1]->subInt64($carry1->shiftLeft(25)); |
|
118 | - |
|
119 | - $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
120 | - $h[4] = $h[4]->addInt64($carry3); |
|
121 | - $h[3] = $h[3]->subInt64($carry3->shiftLeft(25)); |
|
122 | - |
|
123 | - $carry5 = $h[5]->addInt(1 << 24)->shiftRight(25); |
|
124 | - $h[6] = $h[6]->addInt64($carry5); |
|
125 | - $h[5] = $h[5]->subInt64($carry5->shiftLeft(25)); |
|
126 | - |
|
127 | - $carry7 = $h[7]->addInt(1 << 24)->shiftRight(25); |
|
128 | - $h[8] = $h[8]->addInt64($carry7); |
|
129 | - $h[7] = $h[7]->subInt64($carry7->shiftLeft(25)); |
|
130 | - |
|
131 | - $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
132 | - $h[1] = $h[1]->addInt64($carry0); |
|
133 | - $h[0] = $h[0]->subInt64($carry0->shiftLeft(26)); |
|
134 | - |
|
135 | - $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
136 | - $h[3] = $h[3]->addInt64($carry2); |
|
137 | - $h[2] = $h[2]->subInt64($carry2->shiftLeft(26)); |
|
138 | - |
|
139 | - $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
140 | - $h[5] = $h[5]->addInt64($carry4); |
|
141 | - $h[4] = $h[4]->subInt64($carry4->shiftLeft(26)); |
|
142 | - |
|
143 | - $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
144 | - $h[7] = $h[7]->addInt64($carry6); |
|
145 | - $h[6] = $h[6]->subInt64($carry6->shiftLeft(26)); |
|
146 | - |
|
147 | - $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
148 | - $h[9] = $h[9]->addInt64($carry8); |
|
149 | - $h[8] = $h[8]->subInt64($carry8->shiftLeft(26)); |
|
150 | - |
|
151 | - for ($i = 0; $i < 10; ++$i) { |
|
152 | - $h[$i] = $h[$i]->toInt32(); |
|
153 | - } |
|
154 | - /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */ |
|
155 | - $h2 = $h; |
|
156 | - return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @internal You should not use this directly from another application |
|
161 | - * |
|
162 | - * Inline comments preceded by # are from libsodium's ref10 code. |
|
163 | - * |
|
164 | - * @param string $n |
|
165 | - * @param string $p |
|
166 | - * @return string |
|
167 | - * @throws SodiumException |
|
168 | - * @throws TypeError |
|
169 | - */ |
|
170 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
171 | - { |
|
172 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
173 | - $e = '' . $n; |
|
174 | - # e[0] &= 248; |
|
175 | - $e[0] = self::intToChr( |
|
176 | - self::chrToInt($e[0]) & 248 |
|
177 | - ); |
|
178 | - # e[31] &= 127; |
|
179 | - # e[31] |= 64; |
|
180 | - $e[31] = self::intToChr( |
|
181 | - (self::chrToInt($e[31]) & 127) | 64 |
|
182 | - ); |
|
183 | - # fe_frombytes(x1,p); |
|
184 | - $x1 = self::fe_frombytes($p); |
|
185 | - # fe_1(x2); |
|
186 | - $x2 = self::fe_1(); |
|
187 | - # fe_0(z2); |
|
188 | - $z2 = self::fe_0(); |
|
189 | - # fe_copy(x3,x1); |
|
190 | - $x3 = self::fe_copy($x1); |
|
191 | - # fe_1(z3); |
|
192 | - $z3 = self::fe_1(); |
|
193 | - |
|
194 | - # swap = 0; |
|
195 | - /** @var int $swap */ |
|
196 | - $swap = 0; |
|
197 | - |
|
198 | - # for (pos = 254;pos >= 0;--pos) { |
|
199 | - for ($pos = 254; $pos >= 0; --$pos) { |
|
200 | - # b = e[pos / 8] >> (pos & 7); |
|
201 | - /** @var int $b */ |
|
202 | - $b = self::chrToInt( |
|
203 | - $e[(int) floor($pos / 8)] |
|
204 | - ) >> ($pos & 7); |
|
205 | - # b &= 1; |
|
206 | - $b &= 1; |
|
207 | - |
|
208 | - # swap ^= b; |
|
209 | - $swap ^= $b; |
|
210 | - |
|
211 | - # fe_cswap(x2,x3,swap); |
|
212 | - self::fe_cswap($x2, $x3, $swap); |
|
213 | - |
|
214 | - # fe_cswap(z2,z3,swap); |
|
215 | - self::fe_cswap($z2, $z3, $swap); |
|
216 | - |
|
217 | - # swap = b; |
|
218 | - /** @var int $swap */ |
|
219 | - $swap = $b; |
|
220 | - |
|
221 | - # fe_sub(tmp0,x3,z3); |
|
222 | - $tmp0 = self::fe_sub($x3, $z3); |
|
223 | - |
|
224 | - # fe_sub(tmp1,x2,z2); |
|
225 | - $tmp1 = self::fe_sub($x2, $z2); |
|
226 | - |
|
227 | - # fe_add(x2,x2,z2); |
|
228 | - $x2 = self::fe_add($x2, $z2); |
|
229 | - |
|
230 | - # fe_add(z2,x3,z3); |
|
231 | - $z2 = self::fe_add($x3, $z3); |
|
232 | - |
|
233 | - # fe_mul(z3,tmp0,x2); |
|
234 | - $z3 = self::fe_mul($tmp0, $x2); |
|
235 | - |
|
236 | - # fe_mul(z2,z2,tmp1); |
|
237 | - $z2 = self::fe_mul($z2, $tmp1); |
|
238 | - |
|
239 | - # fe_sq(tmp0,tmp1); |
|
240 | - $tmp0 = self::fe_sq($tmp1); |
|
241 | - |
|
242 | - # fe_sq(tmp1,x2); |
|
243 | - $tmp1 = self::fe_sq($x2); |
|
244 | - |
|
245 | - # fe_add(x3,z3,z2); |
|
246 | - $x3 = self::fe_add($z3, $z2); |
|
247 | - |
|
248 | - # fe_sub(z2,z3,z2); |
|
249 | - $z2 = self::fe_sub($z3, $z2); |
|
250 | - |
|
251 | - # fe_mul(x2,tmp1,tmp0); |
|
252 | - $x2 = self::fe_mul($tmp1, $tmp0); |
|
253 | - |
|
254 | - # fe_sub(tmp1,tmp1,tmp0); |
|
255 | - $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
256 | - |
|
257 | - # fe_sq(z2,z2); |
|
258 | - $z2 = self::fe_sq($z2); |
|
259 | - |
|
260 | - # fe_mul121666(z3,tmp1); |
|
261 | - $z3 = self::fe_mul121666($tmp1); |
|
262 | - |
|
263 | - # fe_sq(x3,x3); |
|
264 | - $x3 = self::fe_sq($x3); |
|
265 | - |
|
266 | - # fe_add(tmp0,tmp0,z3); |
|
267 | - $tmp0 = self::fe_add($tmp0, $z3); |
|
268 | - |
|
269 | - # fe_mul(z3,x1,z2); |
|
270 | - $z3 = self::fe_mul($x1, $z2); |
|
271 | - |
|
272 | - # fe_mul(z2,tmp1,tmp0); |
|
273 | - $z2 = self::fe_mul($tmp1, $tmp0); |
|
274 | - } |
|
275 | - |
|
276 | - # fe_cswap(x2,x3,swap); |
|
277 | - self::fe_cswap($x2, $x3, $swap); |
|
278 | - |
|
279 | - # fe_cswap(z2,z3,swap); |
|
280 | - self::fe_cswap($z2, $z3, $swap); |
|
281 | - |
|
282 | - # fe_invert(z2,z2); |
|
283 | - $z2 = self::fe_invert($z2); |
|
284 | - |
|
285 | - # fe_mul(x2,x2,z2); |
|
286 | - $x2 = self::fe_mul($x2, $z2); |
|
287 | - # fe_tobytes(q,x2); |
|
288 | - return (string) self::fe_tobytes($x2); |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * @internal You should not use this directly from another application |
|
293 | - * |
|
294 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY |
|
295 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
296 | - * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
297 | - * @throws SodiumException |
|
298 | - * @throws TypeError |
|
299 | - */ |
|
300 | - public static function edwards_to_montgomery( |
|
301 | - ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
|
302 | - ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
303 | - ) { |
|
304 | - $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
305 | - $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
306 | - $tempZ = self::fe_invert($tempZ); |
|
307 | - return self::fe_mul($tempX, $tempZ); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * @internal You should not use this directly from another application |
|
312 | - * |
|
313 | - * @param string $n |
|
314 | - * @return string |
|
315 | - * @throws SodiumException |
|
316 | - * @throws TypeError |
|
317 | - */ |
|
318 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
319 | - { |
|
320 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
321 | - $e = '' . $n; |
|
322 | - |
|
323 | - # e[0] &= 248; |
|
324 | - $e[0] = self::intToChr( |
|
325 | - self::chrToInt($e[0]) & 248 |
|
326 | - ); |
|
327 | - |
|
328 | - # e[31] &= 127; |
|
329 | - # e[31] |= 64; |
|
330 | - $e[31] = self::intToChr( |
|
331 | - (self::chrToInt($e[31]) & 127) | 64 |
|
332 | - ); |
|
333 | - |
|
334 | - $A = self::ge_scalarmult_base($e); |
|
335 | - if ( |
|
336 | - !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
337 | - || |
|
338 | - !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
339 | - ) { |
|
340 | - throw new TypeError('Null points encountered'); |
|
341 | - } |
|
342 | - $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
343 | - return self::fe_tobytes($pk); |
|
344 | - } |
|
12 | + /** |
|
13 | + * Alters the objects passed to this method in place. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
18 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g |
|
19 | + * @param int $b |
|
20 | + * @return void |
|
21 | + * @throws SodiumException |
|
22 | + * @throws TypeError |
|
23 | + * @psalm-suppress MixedMethodCall |
|
24 | + */ |
|
25 | + public static function fe_cswap( |
|
26 | + ParagonIE_Sodium_Core32_Curve25519_Fe $f, |
|
27 | + ParagonIE_Sodium_Core32_Curve25519_Fe $g, |
|
28 | + $b = 0 |
|
29 | + ) { |
|
30 | + $f0 = (int) $f[0]->toInt(); |
|
31 | + $f1 = (int) $f[1]->toInt(); |
|
32 | + $f2 = (int) $f[2]->toInt(); |
|
33 | + $f3 = (int) $f[3]->toInt(); |
|
34 | + $f4 = (int) $f[4]->toInt(); |
|
35 | + $f5 = (int) $f[5]->toInt(); |
|
36 | + $f6 = (int) $f[6]->toInt(); |
|
37 | + $f7 = (int) $f[7]->toInt(); |
|
38 | + $f8 = (int) $f[8]->toInt(); |
|
39 | + $f9 = (int) $f[9]->toInt(); |
|
40 | + $g0 = (int) $g[0]->toInt(); |
|
41 | + $g1 = (int) $g[1]->toInt(); |
|
42 | + $g2 = (int) $g[2]->toInt(); |
|
43 | + $g3 = (int) $g[3]->toInt(); |
|
44 | + $g4 = (int) $g[4]->toInt(); |
|
45 | + $g5 = (int) $g[5]->toInt(); |
|
46 | + $g6 = (int) $g[6]->toInt(); |
|
47 | + $g7 = (int) $g[7]->toInt(); |
|
48 | + $g8 = (int) $g[8]->toInt(); |
|
49 | + $g9 = (int) $g[9]->toInt(); |
|
50 | + $b = -$b; |
|
51 | + /** @var int $x0 */ |
|
52 | + $x0 = ($f0 ^ $g0) & $b; |
|
53 | + /** @var int $x1 */ |
|
54 | + $x1 = ($f1 ^ $g1) & $b; |
|
55 | + /** @var int $x2 */ |
|
56 | + $x2 = ($f2 ^ $g2) & $b; |
|
57 | + /** @var int $x3 */ |
|
58 | + $x3 = ($f3 ^ $g3) & $b; |
|
59 | + /** @var int $x4 */ |
|
60 | + $x4 = ($f4 ^ $g4) & $b; |
|
61 | + /** @var int $x5 */ |
|
62 | + $x5 = ($f5 ^ $g5) & $b; |
|
63 | + /** @var int $x6 */ |
|
64 | + $x6 = ($f6 ^ $g6) & $b; |
|
65 | + /** @var int $x7 */ |
|
66 | + $x7 = ($f7 ^ $g7) & $b; |
|
67 | + /** @var int $x8 */ |
|
68 | + $x8 = ($f8 ^ $g8) & $b; |
|
69 | + /** @var int $x9 */ |
|
70 | + $x9 = ($f9 ^ $g9) & $b; |
|
71 | + $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
72 | + $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
73 | + $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
74 | + $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
75 | + $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
76 | + $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
77 | + $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
78 | + $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
79 | + $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
80 | + $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
81 | + $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
82 | + $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
83 | + $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
84 | + $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
85 | + $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
86 | + $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
87 | + $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
88 | + $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
89 | + $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
90 | + $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @internal You should not use this directly from another application |
|
95 | + * |
|
96 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
97 | + * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
98 | + * @throws SodiumException |
|
99 | + * @throws TypeError |
|
100 | + * @psalm-suppress MixedAssignment |
|
101 | + * @psalm-suppress MixedMethodCall |
|
102 | + */ |
|
103 | + public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
104 | + { |
|
105 | + /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */ |
|
106 | + $h = array(); |
|
107 | + for ($i = 0; $i < 10; ++$i) { |
|
108 | + $h[$i] = $f[$i]->toInt64()->mulInt(121666, 17); |
|
109 | + } |
|
110 | + |
|
111 | + $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
112 | + $h[0] = $h[0]->addInt64($carry9->mulInt(19, 5)); |
|
113 | + $h[9] = $h[9]->subInt64($carry9->shiftLeft(25)); |
|
114 | + |
|
115 | + $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
116 | + $h[2] = $h[2]->addInt64($carry1); |
|
117 | + $h[1] = $h[1]->subInt64($carry1->shiftLeft(25)); |
|
118 | + |
|
119 | + $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
120 | + $h[4] = $h[4]->addInt64($carry3); |
|
121 | + $h[3] = $h[3]->subInt64($carry3->shiftLeft(25)); |
|
122 | + |
|
123 | + $carry5 = $h[5]->addInt(1 << 24)->shiftRight(25); |
|
124 | + $h[6] = $h[6]->addInt64($carry5); |
|
125 | + $h[5] = $h[5]->subInt64($carry5->shiftLeft(25)); |
|
126 | + |
|
127 | + $carry7 = $h[7]->addInt(1 << 24)->shiftRight(25); |
|
128 | + $h[8] = $h[8]->addInt64($carry7); |
|
129 | + $h[7] = $h[7]->subInt64($carry7->shiftLeft(25)); |
|
130 | + |
|
131 | + $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
132 | + $h[1] = $h[1]->addInt64($carry0); |
|
133 | + $h[0] = $h[0]->subInt64($carry0->shiftLeft(26)); |
|
134 | + |
|
135 | + $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
136 | + $h[3] = $h[3]->addInt64($carry2); |
|
137 | + $h[2] = $h[2]->subInt64($carry2->shiftLeft(26)); |
|
138 | + |
|
139 | + $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
140 | + $h[5] = $h[5]->addInt64($carry4); |
|
141 | + $h[4] = $h[4]->subInt64($carry4->shiftLeft(26)); |
|
142 | + |
|
143 | + $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
144 | + $h[7] = $h[7]->addInt64($carry6); |
|
145 | + $h[6] = $h[6]->subInt64($carry6->shiftLeft(26)); |
|
146 | + |
|
147 | + $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
148 | + $h[9] = $h[9]->addInt64($carry8); |
|
149 | + $h[8] = $h[8]->subInt64($carry8->shiftLeft(26)); |
|
150 | + |
|
151 | + for ($i = 0; $i < 10; ++$i) { |
|
152 | + $h[$i] = $h[$i]->toInt32(); |
|
153 | + } |
|
154 | + /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */ |
|
155 | + $h2 = $h; |
|
156 | + return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @internal You should not use this directly from another application |
|
161 | + * |
|
162 | + * Inline comments preceded by # are from libsodium's ref10 code. |
|
163 | + * |
|
164 | + * @param string $n |
|
165 | + * @param string $p |
|
166 | + * @return string |
|
167 | + * @throws SodiumException |
|
168 | + * @throws TypeError |
|
169 | + */ |
|
170 | + public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
171 | + { |
|
172 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
173 | + $e = '' . $n; |
|
174 | + # e[0] &= 248; |
|
175 | + $e[0] = self::intToChr( |
|
176 | + self::chrToInt($e[0]) & 248 |
|
177 | + ); |
|
178 | + # e[31] &= 127; |
|
179 | + # e[31] |= 64; |
|
180 | + $e[31] = self::intToChr( |
|
181 | + (self::chrToInt($e[31]) & 127) | 64 |
|
182 | + ); |
|
183 | + # fe_frombytes(x1,p); |
|
184 | + $x1 = self::fe_frombytes($p); |
|
185 | + # fe_1(x2); |
|
186 | + $x2 = self::fe_1(); |
|
187 | + # fe_0(z2); |
|
188 | + $z2 = self::fe_0(); |
|
189 | + # fe_copy(x3,x1); |
|
190 | + $x3 = self::fe_copy($x1); |
|
191 | + # fe_1(z3); |
|
192 | + $z3 = self::fe_1(); |
|
193 | + |
|
194 | + # swap = 0; |
|
195 | + /** @var int $swap */ |
|
196 | + $swap = 0; |
|
197 | + |
|
198 | + # for (pos = 254;pos >= 0;--pos) { |
|
199 | + for ($pos = 254; $pos >= 0; --$pos) { |
|
200 | + # b = e[pos / 8] >> (pos & 7); |
|
201 | + /** @var int $b */ |
|
202 | + $b = self::chrToInt( |
|
203 | + $e[(int) floor($pos / 8)] |
|
204 | + ) >> ($pos & 7); |
|
205 | + # b &= 1; |
|
206 | + $b &= 1; |
|
207 | + |
|
208 | + # swap ^= b; |
|
209 | + $swap ^= $b; |
|
210 | + |
|
211 | + # fe_cswap(x2,x3,swap); |
|
212 | + self::fe_cswap($x2, $x3, $swap); |
|
213 | + |
|
214 | + # fe_cswap(z2,z3,swap); |
|
215 | + self::fe_cswap($z2, $z3, $swap); |
|
216 | + |
|
217 | + # swap = b; |
|
218 | + /** @var int $swap */ |
|
219 | + $swap = $b; |
|
220 | + |
|
221 | + # fe_sub(tmp0,x3,z3); |
|
222 | + $tmp0 = self::fe_sub($x3, $z3); |
|
223 | + |
|
224 | + # fe_sub(tmp1,x2,z2); |
|
225 | + $tmp1 = self::fe_sub($x2, $z2); |
|
226 | + |
|
227 | + # fe_add(x2,x2,z2); |
|
228 | + $x2 = self::fe_add($x2, $z2); |
|
229 | + |
|
230 | + # fe_add(z2,x3,z3); |
|
231 | + $z2 = self::fe_add($x3, $z3); |
|
232 | + |
|
233 | + # fe_mul(z3,tmp0,x2); |
|
234 | + $z3 = self::fe_mul($tmp0, $x2); |
|
235 | + |
|
236 | + # fe_mul(z2,z2,tmp1); |
|
237 | + $z2 = self::fe_mul($z2, $tmp1); |
|
238 | + |
|
239 | + # fe_sq(tmp0,tmp1); |
|
240 | + $tmp0 = self::fe_sq($tmp1); |
|
241 | + |
|
242 | + # fe_sq(tmp1,x2); |
|
243 | + $tmp1 = self::fe_sq($x2); |
|
244 | + |
|
245 | + # fe_add(x3,z3,z2); |
|
246 | + $x3 = self::fe_add($z3, $z2); |
|
247 | + |
|
248 | + # fe_sub(z2,z3,z2); |
|
249 | + $z2 = self::fe_sub($z3, $z2); |
|
250 | + |
|
251 | + # fe_mul(x2,tmp1,tmp0); |
|
252 | + $x2 = self::fe_mul($tmp1, $tmp0); |
|
253 | + |
|
254 | + # fe_sub(tmp1,tmp1,tmp0); |
|
255 | + $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
256 | + |
|
257 | + # fe_sq(z2,z2); |
|
258 | + $z2 = self::fe_sq($z2); |
|
259 | + |
|
260 | + # fe_mul121666(z3,tmp1); |
|
261 | + $z3 = self::fe_mul121666($tmp1); |
|
262 | + |
|
263 | + # fe_sq(x3,x3); |
|
264 | + $x3 = self::fe_sq($x3); |
|
265 | + |
|
266 | + # fe_add(tmp0,tmp0,z3); |
|
267 | + $tmp0 = self::fe_add($tmp0, $z3); |
|
268 | + |
|
269 | + # fe_mul(z3,x1,z2); |
|
270 | + $z3 = self::fe_mul($x1, $z2); |
|
271 | + |
|
272 | + # fe_mul(z2,tmp1,tmp0); |
|
273 | + $z2 = self::fe_mul($tmp1, $tmp0); |
|
274 | + } |
|
275 | + |
|
276 | + # fe_cswap(x2,x3,swap); |
|
277 | + self::fe_cswap($x2, $x3, $swap); |
|
278 | + |
|
279 | + # fe_cswap(z2,z3,swap); |
|
280 | + self::fe_cswap($z2, $z3, $swap); |
|
281 | + |
|
282 | + # fe_invert(z2,z2); |
|
283 | + $z2 = self::fe_invert($z2); |
|
284 | + |
|
285 | + # fe_mul(x2,x2,z2); |
|
286 | + $x2 = self::fe_mul($x2, $z2); |
|
287 | + # fe_tobytes(q,x2); |
|
288 | + return (string) self::fe_tobytes($x2); |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * @internal You should not use this directly from another application |
|
293 | + * |
|
294 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY |
|
295 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
296 | + * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
297 | + * @throws SodiumException |
|
298 | + * @throws TypeError |
|
299 | + */ |
|
300 | + public static function edwards_to_montgomery( |
|
301 | + ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
|
302 | + ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
303 | + ) { |
|
304 | + $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
305 | + $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
306 | + $tempZ = self::fe_invert($tempZ); |
|
307 | + return self::fe_mul($tempX, $tempZ); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * @internal You should not use this directly from another application |
|
312 | + * |
|
313 | + * @param string $n |
|
314 | + * @return string |
|
315 | + * @throws SodiumException |
|
316 | + * @throws TypeError |
|
317 | + */ |
|
318 | + public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
319 | + { |
|
320 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
321 | + $e = '' . $n; |
|
322 | + |
|
323 | + # e[0] &= 248; |
|
324 | + $e[0] = self::intToChr( |
|
325 | + self::chrToInt($e[0]) & 248 |
|
326 | + ); |
|
327 | + |
|
328 | + # e[31] &= 127; |
|
329 | + # e[31] |= 64; |
|
330 | + $e[31] = self::intToChr( |
|
331 | + (self::chrToInt($e[31]) & 127) | 64 |
|
332 | + ); |
|
333 | + |
|
334 | + $A = self::ge_scalarmult_base($e); |
|
335 | + if ( |
|
336 | + !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
337 | + || |
|
338 | + !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
339 | + ) { |
|
340 | + throw new TypeError('Null points encountered'); |
|
341 | + } |
|
342 | + $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
343 | + return self::fe_tobytes($pk); |
|
344 | + } |
|
345 | 345 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_X25519', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_X25519', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -27,67 +27,67 @@ discard block |
||
27 | 27 | ParagonIE_Sodium_Core32_Curve25519_Fe $g, |
28 | 28 | $b = 0 |
29 | 29 | ) { |
30 | - $f0 = (int) $f[0]->toInt(); |
|
31 | - $f1 = (int) $f[1]->toInt(); |
|
32 | - $f2 = (int) $f[2]->toInt(); |
|
33 | - $f3 = (int) $f[3]->toInt(); |
|
34 | - $f4 = (int) $f[4]->toInt(); |
|
35 | - $f5 = (int) $f[5]->toInt(); |
|
36 | - $f6 = (int) $f[6]->toInt(); |
|
37 | - $f7 = (int) $f[7]->toInt(); |
|
38 | - $f8 = (int) $f[8]->toInt(); |
|
39 | - $f9 = (int) $f[9]->toInt(); |
|
40 | - $g0 = (int) $g[0]->toInt(); |
|
41 | - $g1 = (int) $g[1]->toInt(); |
|
42 | - $g2 = (int) $g[2]->toInt(); |
|
43 | - $g3 = (int) $g[3]->toInt(); |
|
44 | - $g4 = (int) $g[4]->toInt(); |
|
45 | - $g5 = (int) $g[5]->toInt(); |
|
46 | - $g6 = (int) $g[6]->toInt(); |
|
47 | - $g7 = (int) $g[7]->toInt(); |
|
48 | - $g8 = (int) $g[8]->toInt(); |
|
49 | - $g9 = (int) $g[9]->toInt(); |
|
30 | + $f0 = (int)$f[ 0 ]->toInt(); |
|
31 | + $f1 = (int)$f[ 1 ]->toInt(); |
|
32 | + $f2 = (int)$f[ 2 ]->toInt(); |
|
33 | + $f3 = (int)$f[ 3 ]->toInt(); |
|
34 | + $f4 = (int)$f[ 4 ]->toInt(); |
|
35 | + $f5 = (int)$f[ 5 ]->toInt(); |
|
36 | + $f6 = (int)$f[ 6 ]->toInt(); |
|
37 | + $f7 = (int)$f[ 7 ]->toInt(); |
|
38 | + $f8 = (int)$f[ 8 ]->toInt(); |
|
39 | + $f9 = (int)$f[ 9 ]->toInt(); |
|
40 | + $g0 = (int)$g[ 0 ]->toInt(); |
|
41 | + $g1 = (int)$g[ 1 ]->toInt(); |
|
42 | + $g2 = (int)$g[ 2 ]->toInt(); |
|
43 | + $g3 = (int)$g[ 3 ]->toInt(); |
|
44 | + $g4 = (int)$g[ 4 ]->toInt(); |
|
45 | + $g5 = (int)$g[ 5 ]->toInt(); |
|
46 | + $g6 = (int)$g[ 6 ]->toInt(); |
|
47 | + $g7 = (int)$g[ 7 ]->toInt(); |
|
48 | + $g8 = (int)$g[ 8 ]->toInt(); |
|
49 | + $g9 = (int)$g[ 9 ]->toInt(); |
|
50 | 50 | $b = -$b; |
51 | 51 | /** @var int $x0 */ |
52 | - $x0 = ($f0 ^ $g0) & $b; |
|
52 | + $x0 = ( $f0 ^ $g0 ) & $b; |
|
53 | 53 | /** @var int $x1 */ |
54 | - $x1 = ($f1 ^ $g1) & $b; |
|
54 | + $x1 = ( $f1 ^ $g1 ) & $b; |
|
55 | 55 | /** @var int $x2 */ |
56 | - $x2 = ($f2 ^ $g2) & $b; |
|
56 | + $x2 = ( $f2 ^ $g2 ) & $b; |
|
57 | 57 | /** @var int $x3 */ |
58 | - $x3 = ($f3 ^ $g3) & $b; |
|
58 | + $x3 = ( $f3 ^ $g3 ) & $b; |
|
59 | 59 | /** @var int $x4 */ |
60 | - $x4 = ($f4 ^ $g4) & $b; |
|
60 | + $x4 = ( $f4 ^ $g4 ) & $b; |
|
61 | 61 | /** @var int $x5 */ |
62 | - $x5 = ($f5 ^ $g5) & $b; |
|
62 | + $x5 = ( $f5 ^ $g5 ) & $b; |
|
63 | 63 | /** @var int $x6 */ |
64 | - $x6 = ($f6 ^ $g6) & $b; |
|
64 | + $x6 = ( $f6 ^ $g6 ) & $b; |
|
65 | 65 | /** @var int $x7 */ |
66 | - $x7 = ($f7 ^ $g7) & $b; |
|
66 | + $x7 = ( $f7 ^ $g7 ) & $b; |
|
67 | 67 | /** @var int $x8 */ |
68 | - $x8 = ($f8 ^ $g8) & $b; |
|
68 | + $x8 = ( $f8 ^ $g8 ) & $b; |
|
69 | 69 | /** @var int $x9 */ |
70 | - $x9 = ($f9 ^ $g9) & $b; |
|
71 | - $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
72 | - $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
73 | - $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
74 | - $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
75 | - $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
76 | - $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
77 | - $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
78 | - $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
79 | - $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
80 | - $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
81 | - $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
82 | - $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
83 | - $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
84 | - $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
85 | - $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
86 | - $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
87 | - $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
88 | - $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
89 | - $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
90 | - $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
70 | + $x9 = ( $f9 ^ $g9 ) & $b; |
|
71 | + $f[ 0 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f0 ^ $x0 ); |
|
72 | + $f[ 1 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f1 ^ $x1 ); |
|
73 | + $f[ 2 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f2 ^ $x2 ); |
|
74 | + $f[ 3 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f3 ^ $x3 ); |
|
75 | + $f[ 4 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f4 ^ $x4 ); |
|
76 | + $f[ 5 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f5 ^ $x5 ); |
|
77 | + $f[ 6 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f6 ^ $x6 ); |
|
78 | + $f[ 7 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f7 ^ $x7 ); |
|
79 | + $f[ 8 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f8 ^ $x8 ); |
|
80 | + $f[ 9 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $f9 ^ $x9 ); |
|
81 | + $g[ 0 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g0 ^ $x0 ); |
|
82 | + $g[ 1 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g1 ^ $x1 ); |
|
83 | + $g[ 2 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g2 ^ $x2 ); |
|
84 | + $g[ 3 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g3 ^ $x3 ); |
|
85 | + $g[ 4 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g4 ^ $x4 ); |
|
86 | + $g[ 5 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g5 ^ $x5 ); |
|
87 | + $g[ 6 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g6 ^ $x6 ); |
|
88 | + $g[ 7 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g7 ^ $x7 ); |
|
89 | + $g[ 8 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g8 ^ $x8 ); |
|
90 | + $g[ 9 ] = ParagonIE_Sodium_Core32_Int32::fromInt( $g9 ^ $x9 ); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
@@ -100,60 +100,60 @@ discard block |
||
100 | 100 | * @psalm-suppress MixedAssignment |
101 | 101 | * @psalm-suppress MixedMethodCall |
102 | 102 | */ |
103 | - public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
103 | + public static function fe_mul121666( ParagonIE_Sodium_Core32_Curve25519_Fe $f ) |
|
104 | 104 | { |
105 | 105 | /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */ |
106 | 106 | $h = array(); |
107 | - for ($i = 0; $i < 10; ++$i) { |
|
108 | - $h[$i] = $f[$i]->toInt64()->mulInt(121666, 17); |
|
107 | + for ( $i = 0; $i < 10; ++$i ) { |
|
108 | + $h[ $i ] = $f[ $i ]->toInt64()->mulInt( 121666, 17 ); |
|
109 | 109 | } |
110 | 110 | |
111 | - $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
112 | - $h[0] = $h[0]->addInt64($carry9->mulInt(19, 5)); |
|
113 | - $h[9] = $h[9]->subInt64($carry9->shiftLeft(25)); |
|
111 | + $carry9 = $h[ 9 ]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
112 | + $h[ 0 ] = $h[ 0 ]->addInt64( $carry9->mulInt( 19, 5 ) ); |
|
113 | + $h[ 9 ] = $h[ 9 ]->subInt64( $carry9->shiftLeft( 25 ) ); |
|
114 | 114 | |
115 | - $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
116 | - $h[2] = $h[2]->addInt64($carry1); |
|
117 | - $h[1] = $h[1]->subInt64($carry1->shiftLeft(25)); |
|
115 | + $carry1 = $h[ 1 ]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
116 | + $h[ 2 ] = $h[ 2 ]->addInt64( $carry1 ); |
|
117 | + $h[ 1 ] = $h[ 1 ]->subInt64( $carry1->shiftLeft( 25 ) ); |
|
118 | 118 | |
119 | - $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
120 | - $h[4] = $h[4]->addInt64($carry3); |
|
121 | - $h[3] = $h[3]->subInt64($carry3->shiftLeft(25)); |
|
119 | + $carry3 = $h[ 3 ]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
120 | + $h[ 4 ] = $h[ 4 ]->addInt64( $carry3 ); |
|
121 | + $h[ 3 ] = $h[ 3 ]->subInt64( $carry3->shiftLeft( 25 ) ); |
|
122 | 122 | |
123 | - $carry5 = $h[5]->addInt(1 << 24)->shiftRight(25); |
|
124 | - $h[6] = $h[6]->addInt64($carry5); |
|
125 | - $h[5] = $h[5]->subInt64($carry5->shiftLeft(25)); |
|
123 | + $carry5 = $h[ 5 ]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
124 | + $h[ 6 ] = $h[ 6 ]->addInt64( $carry5 ); |
|
125 | + $h[ 5 ] = $h[ 5 ]->subInt64( $carry5->shiftLeft( 25 ) ); |
|
126 | 126 | |
127 | - $carry7 = $h[7]->addInt(1 << 24)->shiftRight(25); |
|
128 | - $h[8] = $h[8]->addInt64($carry7); |
|
129 | - $h[7] = $h[7]->subInt64($carry7->shiftLeft(25)); |
|
127 | + $carry7 = $h[ 7 ]->addInt( 1 << 24 )->shiftRight( 25 ); |
|
128 | + $h[ 8 ] = $h[ 8 ]->addInt64( $carry7 ); |
|
129 | + $h[ 7 ] = $h[ 7 ]->subInt64( $carry7->shiftLeft( 25 ) ); |
|
130 | 130 | |
131 | - $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
132 | - $h[1] = $h[1]->addInt64($carry0); |
|
133 | - $h[0] = $h[0]->subInt64($carry0->shiftLeft(26)); |
|
131 | + $carry0 = $h[ 0 ]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
132 | + $h[ 1 ] = $h[ 1 ]->addInt64( $carry0 ); |
|
133 | + $h[ 0 ] = $h[ 0 ]->subInt64( $carry0->shiftLeft( 26 ) ); |
|
134 | 134 | |
135 | - $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
136 | - $h[3] = $h[3]->addInt64($carry2); |
|
137 | - $h[2] = $h[2]->subInt64($carry2->shiftLeft(26)); |
|
135 | + $carry2 = $h[ 2 ]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
136 | + $h[ 3 ] = $h[ 3 ]->addInt64( $carry2 ); |
|
137 | + $h[ 2 ] = $h[ 2 ]->subInt64( $carry2->shiftLeft( 26 ) ); |
|
138 | 138 | |
139 | - $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
140 | - $h[5] = $h[5]->addInt64($carry4); |
|
141 | - $h[4] = $h[4]->subInt64($carry4->shiftLeft(26)); |
|
139 | + $carry4 = $h[ 4 ]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
140 | + $h[ 5 ] = $h[ 5 ]->addInt64( $carry4 ); |
|
141 | + $h[ 4 ] = $h[ 4 ]->subInt64( $carry4->shiftLeft( 26 ) ); |
|
142 | 142 | |
143 | - $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
144 | - $h[7] = $h[7]->addInt64($carry6); |
|
145 | - $h[6] = $h[6]->subInt64($carry6->shiftLeft(26)); |
|
143 | + $carry6 = $h[ 6 ]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
144 | + $h[ 7 ] = $h[ 7 ]->addInt64( $carry6 ); |
|
145 | + $h[ 6 ] = $h[ 6 ]->subInt64( $carry6->shiftLeft( 26 ) ); |
|
146 | 146 | |
147 | - $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
148 | - $h[9] = $h[9]->addInt64($carry8); |
|
149 | - $h[8] = $h[8]->subInt64($carry8->shiftLeft(26)); |
|
147 | + $carry8 = $h[ 8 ]->addInt( 1 << 25 )->shiftRight( 26 ); |
|
148 | + $h[ 9 ] = $h[ 9 ]->addInt64( $carry8 ); |
|
149 | + $h[ 8 ] = $h[ 8 ]->subInt64( $carry8->shiftLeft( 26 ) ); |
|
150 | 150 | |
151 | - for ($i = 0; $i < 10; ++$i) { |
|
152 | - $h[$i] = $h[$i]->toInt32(); |
|
151 | + for ( $i = 0; $i < 10; ++$i ) { |
|
152 | + $h[ $i ] = $h[ $i ]->toInt32(); |
|
153 | 153 | } |
154 | 154 | /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */ |
155 | 155 | $h2 = $h; |
156 | - return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2); |
|
156 | + return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( $h2 ); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | /** |
@@ -167,27 +167,27 @@ discard block |
||
167 | 167 | * @throws SodiumException |
168 | 168 | * @throws TypeError |
169 | 169 | */ |
170 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
170 | + public static function crypto_scalarmult_curve25519_ref10( $n, $p ) |
|
171 | 171 | { |
172 | 172 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
173 | 173 | $e = '' . $n; |
174 | 174 | # e[0] &= 248; |
175 | - $e[0] = self::intToChr( |
|
176 | - self::chrToInt($e[0]) & 248 |
|
175 | + $e[ 0 ] = self::intToChr( |
|
176 | + self::chrToInt( $e[ 0 ] ) & 248 |
|
177 | 177 | ); |
178 | 178 | # e[31] &= 127; |
179 | 179 | # e[31] |= 64; |
180 | - $e[31] = self::intToChr( |
|
181 | - (self::chrToInt($e[31]) & 127) | 64 |
|
180 | + $e[ 31 ] = self::intToChr( |
|
181 | + ( self::chrToInt( $e[ 31 ] ) & 127 ) | 64 |
|
182 | 182 | ); |
183 | 183 | # fe_frombytes(x1,p); |
184 | - $x1 = self::fe_frombytes($p); |
|
184 | + $x1 = self::fe_frombytes( $p ); |
|
185 | 185 | # fe_1(x2); |
186 | 186 | $x2 = self::fe_1(); |
187 | 187 | # fe_0(z2); |
188 | 188 | $z2 = self::fe_0(); |
189 | 189 | # fe_copy(x3,x1); |
190 | - $x3 = self::fe_copy($x1); |
|
190 | + $x3 = self::fe_copy( $x1 ); |
|
191 | 191 | # fe_1(z3); |
192 | 192 | $z3 = self::fe_1(); |
193 | 193 | |
@@ -196,12 +196,12 @@ discard block |
||
196 | 196 | $swap = 0; |
197 | 197 | |
198 | 198 | # for (pos = 254;pos >= 0;--pos) { |
199 | - for ($pos = 254; $pos >= 0; --$pos) { |
|
199 | + for ( $pos = 254; $pos >= 0; --$pos ) { |
|
200 | 200 | # b = e[pos / 8] >> (pos & 7); |
201 | 201 | /** @var int $b */ |
202 | 202 | $b = self::chrToInt( |
203 | - $e[(int) floor($pos / 8)] |
|
204 | - ) >> ($pos & 7); |
|
203 | + $e[ (int)floor( $pos / 8 ) ] |
|
204 | + ) >> ( $pos & 7 ); |
|
205 | 205 | # b &= 1; |
206 | 206 | $b &= 1; |
207 | 207 | |
@@ -209,83 +209,83 @@ discard block |
||
209 | 209 | $swap ^= $b; |
210 | 210 | |
211 | 211 | # fe_cswap(x2,x3,swap); |
212 | - self::fe_cswap($x2, $x3, $swap); |
|
212 | + self::fe_cswap( $x2, $x3, $swap ); |
|
213 | 213 | |
214 | 214 | # fe_cswap(z2,z3,swap); |
215 | - self::fe_cswap($z2, $z3, $swap); |
|
215 | + self::fe_cswap( $z2, $z3, $swap ); |
|
216 | 216 | |
217 | 217 | # swap = b; |
218 | 218 | /** @var int $swap */ |
219 | 219 | $swap = $b; |
220 | 220 | |
221 | 221 | # fe_sub(tmp0,x3,z3); |
222 | - $tmp0 = self::fe_sub($x3, $z3); |
|
222 | + $tmp0 = self::fe_sub( $x3, $z3 ); |
|
223 | 223 | |
224 | 224 | # fe_sub(tmp1,x2,z2); |
225 | - $tmp1 = self::fe_sub($x2, $z2); |
|
225 | + $tmp1 = self::fe_sub( $x2, $z2 ); |
|
226 | 226 | |
227 | 227 | # fe_add(x2,x2,z2); |
228 | - $x2 = self::fe_add($x2, $z2); |
|
228 | + $x2 = self::fe_add( $x2, $z2 ); |
|
229 | 229 | |
230 | 230 | # fe_add(z2,x3,z3); |
231 | - $z2 = self::fe_add($x3, $z3); |
|
231 | + $z2 = self::fe_add( $x3, $z3 ); |
|
232 | 232 | |
233 | 233 | # fe_mul(z3,tmp0,x2); |
234 | - $z3 = self::fe_mul($tmp0, $x2); |
|
234 | + $z3 = self::fe_mul( $tmp0, $x2 ); |
|
235 | 235 | |
236 | 236 | # fe_mul(z2,z2,tmp1); |
237 | - $z2 = self::fe_mul($z2, $tmp1); |
|
237 | + $z2 = self::fe_mul( $z2, $tmp1 ); |
|
238 | 238 | |
239 | 239 | # fe_sq(tmp0,tmp1); |
240 | - $tmp0 = self::fe_sq($tmp1); |
|
240 | + $tmp0 = self::fe_sq( $tmp1 ); |
|
241 | 241 | |
242 | 242 | # fe_sq(tmp1,x2); |
243 | - $tmp1 = self::fe_sq($x2); |
|
243 | + $tmp1 = self::fe_sq( $x2 ); |
|
244 | 244 | |
245 | 245 | # fe_add(x3,z3,z2); |
246 | - $x3 = self::fe_add($z3, $z2); |
|
246 | + $x3 = self::fe_add( $z3, $z2 ); |
|
247 | 247 | |
248 | 248 | # fe_sub(z2,z3,z2); |
249 | - $z2 = self::fe_sub($z3, $z2); |
|
249 | + $z2 = self::fe_sub( $z3, $z2 ); |
|
250 | 250 | |
251 | 251 | # fe_mul(x2,tmp1,tmp0); |
252 | - $x2 = self::fe_mul($tmp1, $tmp0); |
|
252 | + $x2 = self::fe_mul( $tmp1, $tmp0 ); |
|
253 | 253 | |
254 | 254 | # fe_sub(tmp1,tmp1,tmp0); |
255 | - $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
255 | + $tmp1 = self::fe_sub( $tmp1, $tmp0 ); |
|
256 | 256 | |
257 | 257 | # fe_sq(z2,z2); |
258 | - $z2 = self::fe_sq($z2); |
|
258 | + $z2 = self::fe_sq( $z2 ); |
|
259 | 259 | |
260 | 260 | # fe_mul121666(z3,tmp1); |
261 | - $z3 = self::fe_mul121666($tmp1); |
|
261 | + $z3 = self::fe_mul121666( $tmp1 ); |
|
262 | 262 | |
263 | 263 | # fe_sq(x3,x3); |
264 | - $x3 = self::fe_sq($x3); |
|
264 | + $x3 = self::fe_sq( $x3 ); |
|
265 | 265 | |
266 | 266 | # fe_add(tmp0,tmp0,z3); |
267 | - $tmp0 = self::fe_add($tmp0, $z3); |
|
267 | + $tmp0 = self::fe_add( $tmp0, $z3 ); |
|
268 | 268 | |
269 | 269 | # fe_mul(z3,x1,z2); |
270 | - $z3 = self::fe_mul($x1, $z2); |
|
270 | + $z3 = self::fe_mul( $x1, $z2 ); |
|
271 | 271 | |
272 | 272 | # fe_mul(z2,tmp1,tmp0); |
273 | - $z2 = self::fe_mul($tmp1, $tmp0); |
|
273 | + $z2 = self::fe_mul( $tmp1, $tmp0 ); |
|
274 | 274 | } |
275 | 275 | |
276 | 276 | # fe_cswap(x2,x3,swap); |
277 | - self::fe_cswap($x2, $x3, $swap); |
|
277 | + self::fe_cswap( $x2, $x3, $swap ); |
|
278 | 278 | |
279 | 279 | # fe_cswap(z2,z3,swap); |
280 | - self::fe_cswap($z2, $z3, $swap); |
|
280 | + self::fe_cswap( $z2, $z3, $swap ); |
|
281 | 281 | |
282 | 282 | # fe_invert(z2,z2); |
283 | - $z2 = self::fe_invert($z2); |
|
283 | + $z2 = self::fe_invert( $z2 ); |
|
284 | 284 | |
285 | 285 | # fe_mul(x2,x2,z2); |
286 | - $x2 = self::fe_mul($x2, $z2); |
|
286 | + $x2 = self::fe_mul( $x2, $z2 ); |
|
287 | 287 | # fe_tobytes(q,x2); |
288 | - return (string) self::fe_tobytes($x2); |
|
288 | + return (string)self::fe_tobytes( $x2 ); |
|
289 | 289 | } |
290 | 290 | |
291 | 291 | /** |
@@ -301,10 +301,10 @@ discard block |
||
301 | 301 | ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
302 | 302 | ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
303 | 303 | ) { |
304 | - $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
305 | - $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
306 | - $tempZ = self::fe_invert($tempZ); |
|
307 | - return self::fe_mul($tempX, $tempZ); |
|
304 | + $tempX = self::fe_add( $edwardsZ, $edwardsY ); |
|
305 | + $tempZ = self::fe_sub( $edwardsZ, $edwardsY ); |
|
306 | + $tempZ = self::fe_invert( $tempZ ); |
|
307 | + return self::fe_mul( $tempX, $tempZ ); |
|
308 | 308 | } |
309 | 309 | |
310 | 310 | /** |
@@ -315,31 +315,31 @@ discard block |
||
315 | 315 | * @throws SodiumException |
316 | 316 | * @throws TypeError |
317 | 317 | */ |
318 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
318 | + public static function crypto_scalarmult_curve25519_ref10_base( $n ) |
|
319 | 319 | { |
320 | 320 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
321 | 321 | $e = '' . $n; |
322 | 322 | |
323 | 323 | # e[0] &= 248; |
324 | - $e[0] = self::intToChr( |
|
325 | - self::chrToInt($e[0]) & 248 |
|
324 | + $e[ 0 ] = self::intToChr( |
|
325 | + self::chrToInt( $e[ 0 ] ) & 248 |
|
326 | 326 | ); |
327 | 327 | |
328 | 328 | # e[31] &= 127; |
329 | 329 | # e[31] |= 64; |
330 | - $e[31] = self::intToChr( |
|
331 | - (self::chrToInt($e[31]) & 127) | 64 |
|
330 | + $e[ 31 ] = self::intToChr( |
|
331 | + ( self::chrToInt( $e[ 31 ] ) & 127 ) | 64 |
|
332 | 332 | ); |
333 | 333 | |
334 | - $A = self::ge_scalarmult_base($e); |
|
334 | + $A = self::ge_scalarmult_base( $e ); |
|
335 | 335 | if ( |
336 | - !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
336 | + ! ( $A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe ) |
|
337 | 337 | || |
338 | - !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
338 | + ! ( $A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe ) |
|
339 | 339 | ) { |
340 | - throw new TypeError('Null points encountered'); |
|
340 | + throw new TypeError( 'Null points encountered' ); |
|
341 | 341 | } |
342 | - $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
343 | - return self::fe_tobytes($pk); |
|
342 | + $pk = self::edwards_to_montgomery( $A->Y, $A->Z ); |
|
343 | + return self::fe_tobytes( $pk ); |
|
344 | 344 | } |
345 | 345 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_X25519 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519 |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519 { |
|
12 | 11 | /** |
13 | 12 | * Alters the objects passed to this method in place. |
14 | 13 | * |
@@ -100,8 +99,7 @@ discard block |
||
100 | 99 | * @psalm-suppress MixedAssignment |
101 | 100 | * @psalm-suppress MixedMethodCall |
102 | 101 | */ |
103 | - public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
104 | - { |
|
102 | + public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) { |
|
105 | 103 | /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */ |
106 | 104 | $h = array(); |
107 | 105 | for ($i = 0; $i < 10; ++$i) { |
@@ -167,8 +165,7 @@ discard block |
||
167 | 165 | * @throws SodiumException |
168 | 166 | * @throws TypeError |
169 | 167 | */ |
170 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
171 | - { |
|
168 | + public static function crypto_scalarmult_curve25519_ref10($n, $p) { |
|
172 | 169 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
173 | 170 | $e = '' . $n; |
174 | 171 | # e[0] &= 248; |
@@ -315,8 +312,7 @@ discard block |
||
315 | 312 | * @throws SodiumException |
316 | 313 | * @throws TypeError |
317 | 314 | */ |
318 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
319 | - { |
|
315 | + public static function crypto_scalarmult_curve25519_ref10_base($n) { |
|
320 | 316 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
321 | 317 | $e = '' . $n; |
322 | 318 |
@@ -9,863 +9,863 @@ |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_Int32 |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array<int, int> - two 16-bit integers |
|
14 | - * |
|
15 | - * 0 is the higher 16 bits |
|
16 | - * 1 is the lower 16 bits |
|
17 | - */ |
|
18 | - public $limbs = array(0, 0); |
|
19 | - |
|
20 | - /** |
|
21 | - * @var int |
|
22 | - */ |
|
23 | - public $overflow = 0; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - public $unsignedInt = false; |
|
29 | - |
|
30 | - /** |
|
31 | - * ParagonIE_Sodium_Core32_Int32 constructor. |
|
32 | - * @param array $array |
|
33 | - * @param bool $unsignedInt |
|
34 | - */ |
|
35 | - public function __construct($array = array(0, 0), $unsignedInt = false) |
|
36 | - { |
|
37 | - $this->limbs = array( |
|
38 | - (int) $array[0], |
|
39 | - (int) $array[1] |
|
40 | - ); |
|
41 | - $this->overflow = 0; |
|
42 | - $this->unsignedInt = $unsignedInt; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Adds two int32 objects |
|
47 | - * |
|
48 | - * @param ParagonIE_Sodium_Core32_Int32 $addend |
|
49 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
50 | - */ |
|
51 | - public function addInt32(ParagonIE_Sodium_Core32_Int32 $addend) |
|
52 | - { |
|
53 | - $i0 = $this->limbs[0]; |
|
54 | - $i1 = $this->limbs[1]; |
|
55 | - $j0 = $addend->limbs[0]; |
|
56 | - $j1 = $addend->limbs[1]; |
|
57 | - |
|
58 | - $r1 = $i1 + ($j1 & 0xffff); |
|
59 | - $carry = $r1 >> 16; |
|
60 | - |
|
61 | - $r0 = $i0 + ($j0 & 0xffff) + $carry; |
|
62 | - $carry = $r0 >> 16; |
|
63 | - |
|
64 | - $r0 &= 0xffff; |
|
65 | - $r1 &= 0xffff; |
|
66 | - |
|
67 | - $return = new ParagonIE_Sodium_Core32_Int32( |
|
68 | - array($r0, $r1) |
|
69 | - ); |
|
70 | - $return->overflow = $carry; |
|
71 | - $return->unsignedInt = $this->unsignedInt; |
|
72 | - return $return; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Adds a normal integer to an int32 object |
|
77 | - * |
|
78 | - * @param int $int |
|
79 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
80 | - * @throws SodiumException |
|
81 | - * @throws TypeError |
|
82 | - */ |
|
83 | - public function addInt($int) |
|
84 | - { |
|
85 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
86 | - /** @var int $int */ |
|
87 | - $int = (int) $int; |
|
88 | - |
|
89 | - $int = (int) $int; |
|
90 | - |
|
91 | - $i0 = $this->limbs[0]; |
|
92 | - $i1 = $this->limbs[1]; |
|
93 | - |
|
94 | - $r1 = $i1 + ($int & 0xffff); |
|
95 | - $carry = $r1 >> 16; |
|
96 | - |
|
97 | - $r0 = $i0 + (($int >> 16) & 0xffff) + $carry; |
|
98 | - $carry = $r0 >> 16; |
|
99 | - $r0 &= 0xffff; |
|
100 | - $r1 &= 0xffff; |
|
101 | - $return = new ParagonIE_Sodium_Core32_Int32( |
|
102 | - array($r0, $r1) |
|
103 | - ); |
|
104 | - $return->overflow = $carry; |
|
105 | - $return->unsignedInt = $this->unsignedInt; |
|
106 | - return $return; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @param int $b |
|
111 | - * @return int |
|
112 | - */ |
|
113 | - public function compareInt($b = 0) |
|
114 | - { |
|
115 | - $gt = 0; |
|
116 | - $eq = 1; |
|
117 | - |
|
118 | - $i = 2; |
|
119 | - $j = 0; |
|
120 | - while ($i > 0) { |
|
121 | - --$i; |
|
122 | - /** @var int $x1 */ |
|
123 | - $x1 = $this->limbs[$i]; |
|
124 | - /** @var int $x2 */ |
|
125 | - $x2 = ($b >> ($j << 4)) & 0xffff; |
|
126 | - /** @var int $gt */ |
|
127 | - $gt |= (($x2 - $x1) >> 8) & $eq; |
|
128 | - /** @var int $eq */ |
|
129 | - $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
130 | - } |
|
131 | - return ($gt + $gt - $eq) + 1; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @param int $m |
|
136 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
137 | - */ |
|
138 | - public function mask($m = 0) |
|
139 | - { |
|
140 | - /** @var int $hi */ |
|
141 | - $hi = ($m >> 16) & 0xffff; |
|
142 | - /** @var int $lo */ |
|
143 | - $lo = ($m & 0xffff); |
|
144 | - return new ParagonIE_Sodium_Core32_Int32( |
|
145 | - array( |
|
146 | - (int) ($this->limbs[0] & $hi), |
|
147 | - (int) ($this->limbs[1] & $lo) |
|
148 | - ), |
|
149 | - $this->unsignedInt |
|
150 | - ); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @param array<int, int> $a |
|
155 | - * @param array<int, int> $b |
|
156 | - * @param int $baseLog2 |
|
157 | - * @return array<int, int> |
|
158 | - */ |
|
159 | - public function multiplyLong(array $a, array $b, $baseLog2 = 16) |
|
160 | - { |
|
161 | - $a_l = count($a); |
|
162 | - $b_l = count($b); |
|
163 | - /** @var array<int, int> $r */ |
|
164 | - $r = array_fill(0, $a_l + $b_l + 1, 0); |
|
165 | - $base = 1 << $baseLog2; |
|
166 | - for ($i = 0; $i < $a_l; ++$i) { |
|
167 | - $a_i = $a[$i]; |
|
168 | - for ($j = 0; $j < $a_l; ++$j) { |
|
169 | - $b_j = $b[$j]; |
|
170 | - $product = ($a_i * $b_j) + $r[$i + $j]; |
|
171 | - $carry = ($product >> $baseLog2 & 0xffff); |
|
172 | - $r[$i + $j] = ($product - (int) ($carry * $base)) & 0xffff; |
|
173 | - $r[$i + $j + 1] += $carry; |
|
174 | - } |
|
175 | - } |
|
176 | - return array_slice($r, 0, 5); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @param int $int |
|
181 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
182 | - */ |
|
183 | - public function mulIntFast($int) |
|
184 | - { |
|
185 | - // Handle negative numbers |
|
186 | - $aNeg = ($this->limbs[0] >> 15) & 1; |
|
187 | - $bNeg = ($int >> 31) & 1; |
|
188 | - $a = array_reverse($this->limbs); |
|
189 | - $b = array( |
|
190 | - $int & 0xffff, |
|
191 | - ($int >> 16) & 0xffff |
|
192 | - ); |
|
193 | - if ($aNeg) { |
|
194 | - for ($i = 0; $i < 2; ++$i) { |
|
195 | - $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
196 | - } |
|
197 | - ++$a[0]; |
|
198 | - } |
|
199 | - if ($bNeg) { |
|
200 | - for ($i = 0; $i < 2; ++$i) { |
|
201 | - $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
202 | - } |
|
203 | - ++$b[0]; |
|
204 | - } |
|
205 | - // Multiply |
|
206 | - $res = $this->multiplyLong($a, $b); |
|
207 | - |
|
208 | - // Re-apply negation to results |
|
209 | - if ($aNeg !== $bNeg) { |
|
210 | - for ($i = 0; $i < 2; ++$i) { |
|
211 | - $res[$i] = (0xffff ^ $res[$i]) & 0xffff; |
|
212 | - } |
|
213 | - // Handle integer overflow |
|
214 | - $c = 1; |
|
215 | - for ($i = 0; $i < 2; ++$i) { |
|
216 | - $res[$i] += $c; |
|
217 | - $c = $res[$i] >> 16; |
|
218 | - $res[$i] &= 0xffff; |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - // Return our values |
|
223 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
224 | - $return->limbs = array( |
|
225 | - $res[1] & 0xffff, |
|
226 | - $res[0] & 0xffff |
|
227 | - ); |
|
228 | - if (count($res) > 2) { |
|
229 | - $return->overflow = $res[2] & 0xffff; |
|
230 | - } |
|
231 | - $return->unsignedInt = $this->unsignedInt; |
|
232 | - return $return; |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * @param ParagonIE_Sodium_Core32_Int32 $right |
|
237 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
238 | - */ |
|
239 | - public function mulInt32Fast(ParagonIE_Sodium_Core32_Int32 $right) |
|
240 | - { |
|
241 | - $aNeg = ($this->limbs[0] >> 15) & 1; |
|
242 | - $bNeg = ($right->limbs[0] >> 15) & 1; |
|
243 | - |
|
244 | - $a = array_reverse($this->limbs); |
|
245 | - $b = array_reverse($right->limbs); |
|
246 | - if ($aNeg) { |
|
247 | - for ($i = 0; $i < 2; ++$i) { |
|
248 | - $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
249 | - } |
|
250 | - ++$a[0]; |
|
251 | - } |
|
252 | - if ($bNeg) { |
|
253 | - for ($i = 0; $i < 2; ++$i) { |
|
254 | - $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
255 | - } |
|
256 | - ++$b[0]; |
|
257 | - } |
|
258 | - $res = $this->multiplyLong($a, $b); |
|
259 | - if ($aNeg !== $bNeg) { |
|
260 | - if ($aNeg !== $bNeg) { |
|
261 | - for ($i = 0; $i < 2; ++$i) { |
|
262 | - $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; |
|
263 | - } |
|
264 | - $c = 1; |
|
265 | - for ($i = 0; $i < 2; ++$i) { |
|
266 | - $res[$i] += $c; |
|
267 | - $c = $res[$i] >> 16; |
|
268 | - $res[$i] &= 0xffff; |
|
269 | - } |
|
270 | - } |
|
271 | - } |
|
272 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
273 | - $return->limbs = array( |
|
274 | - $res[1] & 0xffff, |
|
275 | - $res[0] & 0xffff |
|
276 | - ); |
|
277 | - if (count($res) > 2) { |
|
278 | - $return->overflow = $res[2]; |
|
279 | - } |
|
280 | - return $return; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @param int $int |
|
285 | - * @param int $size |
|
286 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
287 | - * @throws SodiumException |
|
288 | - * @throws TypeError |
|
289 | - */ |
|
290 | - public function mulInt($int = 0, $size = 0) |
|
291 | - { |
|
292 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
293 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
294 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
295 | - return $this->mulIntFast((int) $int); |
|
296 | - } |
|
297 | - /** @var int $int */ |
|
298 | - $int = (int) $int; |
|
299 | - /** @var int $size */ |
|
300 | - $size = (int) $size; |
|
301 | - |
|
302 | - if (!$size) { |
|
303 | - $size = 31; |
|
304 | - } |
|
305 | - /** @var int $size */ |
|
306 | - |
|
307 | - $a = clone $this; |
|
308 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
309 | - $return->unsignedInt = $this->unsignedInt; |
|
310 | - |
|
311 | - // Initialize: |
|
312 | - $ret0 = 0; |
|
313 | - $ret1 = 0; |
|
314 | - $a0 = $a->limbs[0]; |
|
315 | - $a1 = $a->limbs[1]; |
|
316 | - |
|
317 | - /** @var int $size */ |
|
318 | - /** @var int $i */ |
|
319 | - for ($i = $size; $i >= 0; --$i) { |
|
320 | - $m = (int) (-($int & 1)); |
|
321 | - $x0 = $a0 & $m; |
|
322 | - $x1 = $a1 & $m; |
|
323 | - |
|
324 | - $ret1 += $x1; |
|
325 | - $c = $ret1 >> 16; |
|
326 | - |
|
327 | - $ret0 += $x0 + $c; |
|
328 | - |
|
329 | - $ret0 &= 0xffff; |
|
330 | - $ret1 &= 0xffff; |
|
331 | - |
|
332 | - $a1 = ($a1 << 1); |
|
333 | - $x1 = $a1 >> 16; |
|
334 | - $a0 = ($a0 << 1) | $x1; |
|
335 | - $a0 &= 0xffff; |
|
336 | - $a1 &= 0xffff; |
|
337 | - $int >>= 1; |
|
338 | - } |
|
339 | - $return->limbs[0] = $ret0; |
|
340 | - $return->limbs[1] = $ret1; |
|
341 | - return $return; |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * @param ParagonIE_Sodium_Core32_Int32 $int |
|
346 | - * @param int $size |
|
347 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
348 | - * @throws SodiumException |
|
349 | - * @throws TypeError |
|
350 | - */ |
|
351 | - public function mulInt32(ParagonIE_Sodium_Core32_Int32 $int, $size = 0) |
|
352 | - { |
|
353 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
354 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
355 | - return $this->mulInt32Fast($int); |
|
356 | - } |
|
357 | - if (!$size) { |
|
358 | - $size = 31; |
|
359 | - } |
|
360 | - /** @var int $size */ |
|
361 | - |
|
362 | - $a = clone $this; |
|
363 | - $b = clone $int; |
|
364 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
365 | - $return->unsignedInt = $this->unsignedInt; |
|
366 | - |
|
367 | - // Initialize: |
|
368 | - $ret0 = 0; |
|
369 | - $ret1 = 0; |
|
370 | - $a0 = $a->limbs[0]; |
|
371 | - $a1 = $a->limbs[1]; |
|
372 | - $b0 = $b->limbs[0]; |
|
373 | - $b1 = $b->limbs[1]; |
|
374 | - |
|
375 | - /** @var int $size */ |
|
376 | - /** @var int $i */ |
|
377 | - for ($i = $size; $i >= 0; --$i) { |
|
378 | - $m = (int) (-($b1 & 1)); |
|
379 | - $x0 = $a0 & $m; |
|
380 | - $x1 = $a1 & $m; |
|
381 | - |
|
382 | - $ret1 += $x1; |
|
383 | - $c = $ret1 >> 16; |
|
384 | - |
|
385 | - $ret0 += $x0 + $c; |
|
386 | - |
|
387 | - $ret0 &= 0xffff; |
|
388 | - $ret1 &= 0xffff; |
|
389 | - |
|
390 | - $a1 = ($a1 << 1); |
|
391 | - $x1 = $a1 >> 16; |
|
392 | - $a0 = ($a0 << 1) | $x1; |
|
393 | - $a0 &= 0xffff; |
|
394 | - $a1 &= 0xffff; |
|
395 | - |
|
396 | - $x0 = ($b0 & 1) << 16; |
|
397 | - $b0 = ($b0 >> 1); |
|
398 | - $b1 = (($b1 | $x0) >> 1); |
|
399 | - |
|
400 | - $b0 &= 0xffff; |
|
401 | - $b1 &= 0xffff; |
|
402 | - |
|
403 | - } |
|
404 | - $return->limbs[0] = $ret0; |
|
405 | - $return->limbs[1] = $ret1; |
|
406 | - |
|
407 | - return $return; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * OR this 32-bit integer with another. |
|
412 | - * |
|
413 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
414 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
415 | - */ |
|
416 | - public function orInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
417 | - { |
|
418 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
419 | - $return->unsignedInt = $this->unsignedInt; |
|
420 | - $return->limbs = array( |
|
421 | - (int) ($this->limbs[0] | $b->limbs[0]), |
|
422 | - (int) ($this->limbs[1] | $b->limbs[1]) |
|
423 | - ); |
|
424 | - /** @var int overflow */ |
|
425 | - $return->overflow = $this->overflow | $b->overflow; |
|
426 | - return $return; |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * @param int $b |
|
431 | - * @return bool |
|
432 | - */ |
|
433 | - public function isGreaterThan($b = 0) |
|
434 | - { |
|
435 | - return $this->compareInt($b) > 0; |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * @param int $b |
|
440 | - * @return bool |
|
441 | - */ |
|
442 | - public function isLessThanInt($b = 0) |
|
443 | - { |
|
444 | - return $this->compareInt($b) < 0; |
|
445 | - } |
|
446 | - |
|
447 | - /** |
|
448 | - * @param int $c |
|
449 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
450 | - * @throws SodiumException |
|
451 | - * @throws TypeError |
|
452 | - * @psalm-suppress MixedArrayAccess |
|
453 | - */ |
|
454 | - public function rotateLeft($c = 0) |
|
455 | - { |
|
456 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
457 | - /** @var int $c */ |
|
458 | - $c = (int) $c; |
|
459 | - |
|
460 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
461 | - $return->unsignedInt = $this->unsignedInt; |
|
462 | - $c &= 31; |
|
463 | - if ($c === 0) { |
|
464 | - // NOP, but we want a copy. |
|
465 | - $return->limbs = $this->limbs; |
|
466 | - } else { |
|
467 | - /** @var int $c */ |
|
468 | - |
|
469 | - /** @var int $idx_shift */ |
|
470 | - $idx_shift = ($c >> 4) & 1; |
|
471 | - |
|
472 | - /** @var int $sub_shift */ |
|
473 | - $sub_shift = $c & 15; |
|
474 | - |
|
475 | - /** @var array<int, int> $limbs */ |
|
476 | - $limbs =& $return->limbs; |
|
477 | - |
|
478 | - /** @var array<int, int> $myLimbs */ |
|
479 | - $myLimbs =& $this->limbs; |
|
480 | - |
|
481 | - for ($i = 1; $i >= 0; --$i) { |
|
482 | - /** @var int $j */ |
|
483 | - $j = ($i + $idx_shift) & 1; |
|
484 | - /** @var int $k */ |
|
485 | - $k = ($i + $idx_shift + 1) & 1; |
|
486 | - $limbs[$i] = (int) ( |
|
487 | - ( |
|
488 | - ((int) ($myLimbs[$j]) << $sub_shift) |
|
489 | - | |
|
490 | - ((int) ($myLimbs[$k]) >> (16 - $sub_shift)) |
|
491 | - ) & 0xffff |
|
492 | - ); |
|
493 | - } |
|
494 | - } |
|
495 | - return $return; |
|
496 | - } |
|
497 | - |
|
498 | - /** |
|
499 | - * Rotate to the right |
|
500 | - * |
|
501 | - * @param int $c |
|
502 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
503 | - * @throws SodiumException |
|
504 | - * @throws TypeError |
|
505 | - * @psalm-suppress MixedArrayAccess |
|
506 | - */ |
|
507 | - public function rotateRight($c = 0) |
|
508 | - { |
|
509 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
510 | - /** @var int $c */ |
|
511 | - $c = (int) $c; |
|
512 | - |
|
513 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
514 | - $return->unsignedInt = $this->unsignedInt; |
|
515 | - $c &= 31; |
|
516 | - /** @var int $c */ |
|
517 | - if ($c === 0) { |
|
518 | - // NOP, but we want a copy. |
|
519 | - $return->limbs = $this->limbs; |
|
520 | - } else { |
|
521 | - /** @var int $c */ |
|
522 | - |
|
523 | - /** @var int $idx_shift */ |
|
524 | - $idx_shift = ($c >> 4) & 1; |
|
525 | - |
|
526 | - /** @var int $sub_shift */ |
|
527 | - $sub_shift = $c & 15; |
|
528 | - |
|
529 | - /** @var array<int, int> $limbs */ |
|
530 | - $limbs =& $return->limbs; |
|
531 | - |
|
532 | - /** @var array<int, int> $myLimbs */ |
|
533 | - $myLimbs =& $this->limbs; |
|
534 | - |
|
535 | - for ($i = 1; $i >= 0; --$i) { |
|
536 | - /** @var int $j */ |
|
537 | - $j = ($i - $idx_shift) & 1; |
|
538 | - /** @var int $k */ |
|
539 | - $k = ($i - $idx_shift - 1) & 1; |
|
540 | - $limbs[$i] = (int) ( |
|
541 | - ( |
|
542 | - ((int) ($myLimbs[$j]) >> (int) ($sub_shift)) |
|
543 | - | |
|
544 | - ((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift))) |
|
545 | - ) & 0xffff |
|
546 | - ); |
|
547 | - } |
|
548 | - } |
|
549 | - return $return; |
|
550 | - } |
|
551 | - |
|
552 | - /** |
|
553 | - * @param bool $bool |
|
554 | - * @return self |
|
555 | - */ |
|
556 | - public function setUnsignedInt($bool = false) |
|
557 | - { |
|
558 | - $this->unsignedInt = !empty($bool); |
|
559 | - return $this; |
|
560 | - } |
|
561 | - |
|
562 | - /** |
|
563 | - * @param int $c |
|
564 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
565 | - * @throws SodiumException |
|
566 | - * @throws TypeError |
|
567 | - */ |
|
568 | - public function shiftLeft($c = 0) |
|
569 | - { |
|
570 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
571 | - /** @var int $c */ |
|
572 | - $c = (int) $c; |
|
573 | - |
|
574 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
575 | - $return->unsignedInt = $this->unsignedInt; |
|
576 | - $c &= 63; |
|
577 | - /** @var int $c */ |
|
578 | - if ($c === 0) { |
|
579 | - $return->limbs = $this->limbs; |
|
580 | - } elseif ($c < 0) { |
|
581 | - /** @var int $c */ |
|
582 | - return $this->shiftRight(-$c); |
|
583 | - } else { |
|
584 | - /** @var int $c */ |
|
585 | - /** @var int $tmp */ |
|
586 | - $tmp = $this->limbs[1] << $c; |
|
587 | - $return->limbs[1] = (int)($tmp & 0xffff); |
|
588 | - /** @var int $carry */ |
|
589 | - $carry = $tmp >> 16; |
|
590 | - |
|
591 | - /** @var int $tmp */ |
|
592 | - $tmp = ($this->limbs[0] << $c) | ($carry & 0xffff); |
|
593 | - $return->limbs[0] = (int) ($tmp & 0xffff); |
|
594 | - } |
|
595 | - return $return; |
|
596 | - } |
|
597 | - |
|
598 | - /** |
|
599 | - * @param int $c |
|
600 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
601 | - * @throws SodiumException |
|
602 | - * @throws TypeError |
|
603 | - * @psalm-suppress MixedAssignment |
|
604 | - * @psalm-suppress MixedOperand |
|
605 | - */ |
|
606 | - public function shiftRight($c = 0) |
|
607 | - { |
|
608 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
609 | - /** @var int $c */ |
|
610 | - $c = (int) $c; |
|
611 | - |
|
612 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
613 | - $return->unsignedInt = $this->unsignedInt; |
|
614 | - $c &= 63; |
|
615 | - /** @var int $c */ |
|
616 | - if ($c >= 16) { |
|
617 | - $return->limbs = array( |
|
618 | - (int) ($this->overflow & 0xffff), |
|
619 | - (int) ($this->limbs[0]) |
|
620 | - ); |
|
621 | - $return->overflow = $this->overflow >> 16; |
|
622 | - return $return->shiftRight($c & 15); |
|
623 | - } |
|
624 | - if ($c === 0) { |
|
625 | - $return->limbs = $this->limbs; |
|
626 | - } elseif ($c < 0) { |
|
627 | - /** @var int $c */ |
|
628 | - return $this->shiftLeft(-$c); |
|
629 | - } else { |
|
630 | - if (!is_int($c)) { |
|
631 | - throw new TypeError(); |
|
632 | - } |
|
633 | - /** @var int $c */ |
|
634 | - // $return->limbs[0] = (int) (($this->limbs[0] >> $c) & 0xffff); |
|
635 | - $carryLeft = (int) ($this->overflow & ((1 << ($c + 1)) - 1)); |
|
636 | - $return->limbs[0] = (int) ((($this->limbs[0] >> $c) | ($carryLeft << (16 - $c))) & 0xffff); |
|
637 | - $carryRight = (int) ($this->limbs[0] & ((1 << ($c + 1)) - 1)); |
|
638 | - $return->limbs[1] = (int) ((($this->limbs[1] >> $c) | ($carryRight << (16 - $c))) & 0xffff); |
|
639 | - $return->overflow >>= $c; |
|
640 | - } |
|
641 | - return $return; |
|
642 | - } |
|
643 | - |
|
644 | - /** |
|
645 | - * Subtract a normal integer from an int32 object. |
|
646 | - * |
|
647 | - * @param int $int |
|
648 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
649 | - * @throws SodiumException |
|
650 | - * @throws TypeError |
|
651 | - */ |
|
652 | - public function subInt($int) |
|
653 | - { |
|
654 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
655 | - /** @var int $int */ |
|
656 | - $int = (int) $int; |
|
657 | - |
|
658 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
659 | - $return->unsignedInt = $this->unsignedInt; |
|
660 | - |
|
661 | - /** @var int $tmp */ |
|
662 | - $tmp = $this->limbs[1] - ($int & 0xffff); |
|
663 | - /** @var int $carry */ |
|
664 | - $carry = $tmp >> 16; |
|
665 | - $return->limbs[1] = (int) ($tmp & 0xffff); |
|
666 | - |
|
667 | - /** @var int $tmp */ |
|
668 | - $tmp = $this->limbs[0] - (($int >> 16) & 0xffff) + $carry; |
|
669 | - $return->limbs[0] = (int) ($tmp & 0xffff); |
|
670 | - return $return; |
|
671 | - } |
|
672 | - |
|
673 | - /** |
|
674 | - * Subtract two int32 objects from each other |
|
675 | - * |
|
676 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
677 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
678 | - */ |
|
679 | - public function subInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
680 | - { |
|
681 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
682 | - $return->unsignedInt = $this->unsignedInt; |
|
683 | - |
|
684 | - /** @var int $tmp */ |
|
685 | - $tmp = $this->limbs[1] - ($b->limbs[1] & 0xffff); |
|
686 | - /** @var int $carry */ |
|
687 | - $carry = $tmp >> 16; |
|
688 | - $return->limbs[1] = (int) ($tmp & 0xffff); |
|
689 | - |
|
690 | - /** @var int $tmp */ |
|
691 | - $tmp = $this->limbs[0] - ($b->limbs[0] & 0xffff) + $carry; |
|
692 | - $return->limbs[0] = (int) ($tmp & 0xffff); |
|
693 | - return $return; |
|
694 | - } |
|
695 | - |
|
696 | - /** |
|
697 | - * XOR this 32-bit integer with another. |
|
698 | - * |
|
699 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
700 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
701 | - */ |
|
702 | - public function xorInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
703 | - { |
|
704 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
705 | - $return->unsignedInt = $this->unsignedInt; |
|
706 | - $return->limbs = array( |
|
707 | - (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
708 | - (int) ($this->limbs[1] ^ $b->limbs[1]) |
|
709 | - ); |
|
710 | - return $return; |
|
711 | - } |
|
712 | - |
|
713 | - /** |
|
714 | - * @param int $signed |
|
715 | - * @return self |
|
716 | - * @throws SodiumException |
|
717 | - * @throws TypeError |
|
718 | - */ |
|
719 | - public static function fromInt($signed) |
|
720 | - { |
|
721 | - ParagonIE_Sodium_Core32_Util::declareScalarType($signed, 'int', 1);; |
|
722 | - /** @var int $signed */ |
|
723 | - $signed = (int) $signed; |
|
724 | - |
|
725 | - return new ParagonIE_Sodium_Core32_Int32( |
|
726 | - array( |
|
727 | - (int) (($signed >> 16) & 0xffff), |
|
728 | - (int) ($signed & 0xffff) |
|
729 | - ) |
|
730 | - ); |
|
731 | - } |
|
732 | - |
|
733 | - /** |
|
734 | - * @param string $string |
|
735 | - * @return self |
|
736 | - * @throws SodiumException |
|
737 | - * @throws TypeError |
|
738 | - */ |
|
739 | - public static function fromString($string) |
|
740 | - { |
|
741 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
742 | - $string = (string) $string; |
|
743 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
744 | - throw new RangeException( |
|
745 | - 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
746 | - ); |
|
747 | - } |
|
748 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
749 | - |
|
750 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
751 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
752 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
753 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
754 | - return $return; |
|
755 | - } |
|
756 | - |
|
757 | - /** |
|
758 | - * @param string $string |
|
759 | - * @return self |
|
760 | - * @throws SodiumException |
|
761 | - * @throws TypeError |
|
762 | - */ |
|
763 | - public static function fromReverseString($string) |
|
764 | - { |
|
765 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
766 | - $string = (string) $string; |
|
767 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
768 | - throw new RangeException( |
|
769 | - 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
770 | - ); |
|
771 | - } |
|
772 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
773 | - |
|
774 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
775 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
776 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
777 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
778 | - return $return; |
|
779 | - } |
|
780 | - |
|
781 | - /** |
|
782 | - * @return array<int, int> |
|
783 | - */ |
|
784 | - public function toArray() |
|
785 | - { |
|
786 | - return array((int) ($this->limbs[0] << 16 | $this->limbs[1])); |
|
787 | - } |
|
788 | - |
|
789 | - /** |
|
790 | - * @return string |
|
791 | - * @throws TypeError |
|
792 | - */ |
|
793 | - public function toString() |
|
794 | - { |
|
795 | - return |
|
796 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
797 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
798 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
799 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff); |
|
800 | - } |
|
801 | - |
|
802 | - /** |
|
803 | - * @return int |
|
804 | - */ |
|
805 | - public function toInt() |
|
806 | - { |
|
807 | - return (int) ( |
|
808 | - (($this->limbs[0] & 0xffff) << 16) |
|
809 | - | |
|
810 | - ($this->limbs[1] & 0xffff) |
|
811 | - ); |
|
812 | - } |
|
813 | - |
|
814 | - /** |
|
815 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
816 | - */ |
|
817 | - public function toInt32() |
|
818 | - { |
|
819 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
820 | - $return->limbs[0] = (int) ($this->limbs[0] & 0xffff); |
|
821 | - $return->limbs[1] = (int) ($this->limbs[1] & 0xffff); |
|
822 | - $return->unsignedInt = $this->unsignedInt; |
|
823 | - $return->overflow = (int) ($this->overflow & 0x7fffffff); |
|
824 | - return $return; |
|
825 | - } |
|
826 | - |
|
827 | - /** |
|
828 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
829 | - */ |
|
830 | - public function toInt64() |
|
831 | - { |
|
832 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
833 | - $return->unsignedInt = $this->unsignedInt; |
|
834 | - if ($this->unsignedInt) { |
|
835 | - $return->limbs[0] += (($this->overflow >> 16) & 0xffff); |
|
836 | - $return->limbs[1] += (($this->overflow) & 0xffff); |
|
837 | - } else { |
|
838 | - $neg = -(($this->limbs[0] >> 15) & 1); |
|
839 | - $return->limbs[0] = (int)($neg & 0xffff); |
|
840 | - $return->limbs[1] = (int)($neg & 0xffff); |
|
841 | - } |
|
842 | - $return->limbs[2] = (int) ($this->limbs[0] & 0xffff); |
|
843 | - $return->limbs[3] = (int) ($this->limbs[1] & 0xffff); |
|
844 | - return $return; |
|
845 | - } |
|
846 | - |
|
847 | - /** |
|
848 | - * @return string |
|
849 | - * @throws TypeError |
|
850 | - */ |
|
851 | - public function toReverseString() |
|
852 | - { |
|
853 | - return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
854 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
855 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
856 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
857 | - } |
|
858 | - |
|
859 | - /** |
|
860 | - * @return string |
|
861 | - */ |
|
862 | - public function __toString() |
|
863 | - { |
|
864 | - try { |
|
865 | - return $this->toString(); |
|
866 | - } catch (TypeError $ex) { |
|
867 | - // PHP engine can't handle exceptions from __toString() |
|
868 | - return ''; |
|
869 | - } |
|
870 | - } |
|
12 | + /** |
|
13 | + * @var array<int, int> - two 16-bit integers |
|
14 | + * |
|
15 | + * 0 is the higher 16 bits |
|
16 | + * 1 is the lower 16 bits |
|
17 | + */ |
|
18 | + public $limbs = array(0, 0); |
|
19 | + |
|
20 | + /** |
|
21 | + * @var int |
|
22 | + */ |
|
23 | + public $overflow = 0; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + public $unsignedInt = false; |
|
29 | + |
|
30 | + /** |
|
31 | + * ParagonIE_Sodium_Core32_Int32 constructor. |
|
32 | + * @param array $array |
|
33 | + * @param bool $unsignedInt |
|
34 | + */ |
|
35 | + public function __construct($array = array(0, 0), $unsignedInt = false) |
|
36 | + { |
|
37 | + $this->limbs = array( |
|
38 | + (int) $array[0], |
|
39 | + (int) $array[1] |
|
40 | + ); |
|
41 | + $this->overflow = 0; |
|
42 | + $this->unsignedInt = $unsignedInt; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Adds two int32 objects |
|
47 | + * |
|
48 | + * @param ParagonIE_Sodium_Core32_Int32 $addend |
|
49 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
50 | + */ |
|
51 | + public function addInt32(ParagonIE_Sodium_Core32_Int32 $addend) |
|
52 | + { |
|
53 | + $i0 = $this->limbs[0]; |
|
54 | + $i1 = $this->limbs[1]; |
|
55 | + $j0 = $addend->limbs[0]; |
|
56 | + $j1 = $addend->limbs[1]; |
|
57 | + |
|
58 | + $r1 = $i1 + ($j1 & 0xffff); |
|
59 | + $carry = $r1 >> 16; |
|
60 | + |
|
61 | + $r0 = $i0 + ($j0 & 0xffff) + $carry; |
|
62 | + $carry = $r0 >> 16; |
|
63 | + |
|
64 | + $r0 &= 0xffff; |
|
65 | + $r1 &= 0xffff; |
|
66 | + |
|
67 | + $return = new ParagonIE_Sodium_Core32_Int32( |
|
68 | + array($r0, $r1) |
|
69 | + ); |
|
70 | + $return->overflow = $carry; |
|
71 | + $return->unsignedInt = $this->unsignedInt; |
|
72 | + return $return; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Adds a normal integer to an int32 object |
|
77 | + * |
|
78 | + * @param int $int |
|
79 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
80 | + * @throws SodiumException |
|
81 | + * @throws TypeError |
|
82 | + */ |
|
83 | + public function addInt($int) |
|
84 | + { |
|
85 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
86 | + /** @var int $int */ |
|
87 | + $int = (int) $int; |
|
88 | + |
|
89 | + $int = (int) $int; |
|
90 | + |
|
91 | + $i0 = $this->limbs[0]; |
|
92 | + $i1 = $this->limbs[1]; |
|
93 | + |
|
94 | + $r1 = $i1 + ($int & 0xffff); |
|
95 | + $carry = $r1 >> 16; |
|
96 | + |
|
97 | + $r0 = $i0 + (($int >> 16) & 0xffff) + $carry; |
|
98 | + $carry = $r0 >> 16; |
|
99 | + $r0 &= 0xffff; |
|
100 | + $r1 &= 0xffff; |
|
101 | + $return = new ParagonIE_Sodium_Core32_Int32( |
|
102 | + array($r0, $r1) |
|
103 | + ); |
|
104 | + $return->overflow = $carry; |
|
105 | + $return->unsignedInt = $this->unsignedInt; |
|
106 | + return $return; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @param int $b |
|
111 | + * @return int |
|
112 | + */ |
|
113 | + public function compareInt($b = 0) |
|
114 | + { |
|
115 | + $gt = 0; |
|
116 | + $eq = 1; |
|
117 | + |
|
118 | + $i = 2; |
|
119 | + $j = 0; |
|
120 | + while ($i > 0) { |
|
121 | + --$i; |
|
122 | + /** @var int $x1 */ |
|
123 | + $x1 = $this->limbs[$i]; |
|
124 | + /** @var int $x2 */ |
|
125 | + $x2 = ($b >> ($j << 4)) & 0xffff; |
|
126 | + /** @var int $gt */ |
|
127 | + $gt |= (($x2 - $x1) >> 8) & $eq; |
|
128 | + /** @var int $eq */ |
|
129 | + $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
130 | + } |
|
131 | + return ($gt + $gt - $eq) + 1; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @param int $m |
|
136 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
137 | + */ |
|
138 | + public function mask($m = 0) |
|
139 | + { |
|
140 | + /** @var int $hi */ |
|
141 | + $hi = ($m >> 16) & 0xffff; |
|
142 | + /** @var int $lo */ |
|
143 | + $lo = ($m & 0xffff); |
|
144 | + return new ParagonIE_Sodium_Core32_Int32( |
|
145 | + array( |
|
146 | + (int) ($this->limbs[0] & $hi), |
|
147 | + (int) ($this->limbs[1] & $lo) |
|
148 | + ), |
|
149 | + $this->unsignedInt |
|
150 | + ); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @param array<int, int> $a |
|
155 | + * @param array<int, int> $b |
|
156 | + * @param int $baseLog2 |
|
157 | + * @return array<int, int> |
|
158 | + */ |
|
159 | + public function multiplyLong(array $a, array $b, $baseLog2 = 16) |
|
160 | + { |
|
161 | + $a_l = count($a); |
|
162 | + $b_l = count($b); |
|
163 | + /** @var array<int, int> $r */ |
|
164 | + $r = array_fill(0, $a_l + $b_l + 1, 0); |
|
165 | + $base = 1 << $baseLog2; |
|
166 | + for ($i = 0; $i < $a_l; ++$i) { |
|
167 | + $a_i = $a[$i]; |
|
168 | + for ($j = 0; $j < $a_l; ++$j) { |
|
169 | + $b_j = $b[$j]; |
|
170 | + $product = ($a_i * $b_j) + $r[$i + $j]; |
|
171 | + $carry = ($product >> $baseLog2 & 0xffff); |
|
172 | + $r[$i + $j] = ($product - (int) ($carry * $base)) & 0xffff; |
|
173 | + $r[$i + $j + 1] += $carry; |
|
174 | + } |
|
175 | + } |
|
176 | + return array_slice($r, 0, 5); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @param int $int |
|
181 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
182 | + */ |
|
183 | + public function mulIntFast($int) |
|
184 | + { |
|
185 | + // Handle negative numbers |
|
186 | + $aNeg = ($this->limbs[0] >> 15) & 1; |
|
187 | + $bNeg = ($int >> 31) & 1; |
|
188 | + $a = array_reverse($this->limbs); |
|
189 | + $b = array( |
|
190 | + $int & 0xffff, |
|
191 | + ($int >> 16) & 0xffff |
|
192 | + ); |
|
193 | + if ($aNeg) { |
|
194 | + for ($i = 0; $i < 2; ++$i) { |
|
195 | + $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
196 | + } |
|
197 | + ++$a[0]; |
|
198 | + } |
|
199 | + if ($bNeg) { |
|
200 | + for ($i = 0; $i < 2; ++$i) { |
|
201 | + $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
202 | + } |
|
203 | + ++$b[0]; |
|
204 | + } |
|
205 | + // Multiply |
|
206 | + $res = $this->multiplyLong($a, $b); |
|
207 | + |
|
208 | + // Re-apply negation to results |
|
209 | + if ($aNeg !== $bNeg) { |
|
210 | + for ($i = 0; $i < 2; ++$i) { |
|
211 | + $res[$i] = (0xffff ^ $res[$i]) & 0xffff; |
|
212 | + } |
|
213 | + // Handle integer overflow |
|
214 | + $c = 1; |
|
215 | + for ($i = 0; $i < 2; ++$i) { |
|
216 | + $res[$i] += $c; |
|
217 | + $c = $res[$i] >> 16; |
|
218 | + $res[$i] &= 0xffff; |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + // Return our values |
|
223 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
224 | + $return->limbs = array( |
|
225 | + $res[1] & 0xffff, |
|
226 | + $res[0] & 0xffff |
|
227 | + ); |
|
228 | + if (count($res) > 2) { |
|
229 | + $return->overflow = $res[2] & 0xffff; |
|
230 | + } |
|
231 | + $return->unsignedInt = $this->unsignedInt; |
|
232 | + return $return; |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * @param ParagonIE_Sodium_Core32_Int32 $right |
|
237 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
238 | + */ |
|
239 | + public function mulInt32Fast(ParagonIE_Sodium_Core32_Int32 $right) |
|
240 | + { |
|
241 | + $aNeg = ($this->limbs[0] >> 15) & 1; |
|
242 | + $bNeg = ($right->limbs[0] >> 15) & 1; |
|
243 | + |
|
244 | + $a = array_reverse($this->limbs); |
|
245 | + $b = array_reverse($right->limbs); |
|
246 | + if ($aNeg) { |
|
247 | + for ($i = 0; $i < 2; ++$i) { |
|
248 | + $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
249 | + } |
|
250 | + ++$a[0]; |
|
251 | + } |
|
252 | + if ($bNeg) { |
|
253 | + for ($i = 0; $i < 2; ++$i) { |
|
254 | + $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
255 | + } |
|
256 | + ++$b[0]; |
|
257 | + } |
|
258 | + $res = $this->multiplyLong($a, $b); |
|
259 | + if ($aNeg !== $bNeg) { |
|
260 | + if ($aNeg !== $bNeg) { |
|
261 | + for ($i = 0; $i < 2; ++$i) { |
|
262 | + $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; |
|
263 | + } |
|
264 | + $c = 1; |
|
265 | + for ($i = 0; $i < 2; ++$i) { |
|
266 | + $res[$i] += $c; |
|
267 | + $c = $res[$i] >> 16; |
|
268 | + $res[$i] &= 0xffff; |
|
269 | + } |
|
270 | + } |
|
271 | + } |
|
272 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
273 | + $return->limbs = array( |
|
274 | + $res[1] & 0xffff, |
|
275 | + $res[0] & 0xffff |
|
276 | + ); |
|
277 | + if (count($res) > 2) { |
|
278 | + $return->overflow = $res[2]; |
|
279 | + } |
|
280 | + return $return; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @param int $int |
|
285 | + * @param int $size |
|
286 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
287 | + * @throws SodiumException |
|
288 | + * @throws TypeError |
|
289 | + */ |
|
290 | + public function mulInt($int = 0, $size = 0) |
|
291 | + { |
|
292 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
293 | + ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
294 | + if (ParagonIE_Sodium_Compat::$fastMult) { |
|
295 | + return $this->mulIntFast((int) $int); |
|
296 | + } |
|
297 | + /** @var int $int */ |
|
298 | + $int = (int) $int; |
|
299 | + /** @var int $size */ |
|
300 | + $size = (int) $size; |
|
301 | + |
|
302 | + if (!$size) { |
|
303 | + $size = 31; |
|
304 | + } |
|
305 | + /** @var int $size */ |
|
306 | + |
|
307 | + $a = clone $this; |
|
308 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
309 | + $return->unsignedInt = $this->unsignedInt; |
|
310 | + |
|
311 | + // Initialize: |
|
312 | + $ret0 = 0; |
|
313 | + $ret1 = 0; |
|
314 | + $a0 = $a->limbs[0]; |
|
315 | + $a1 = $a->limbs[1]; |
|
316 | + |
|
317 | + /** @var int $size */ |
|
318 | + /** @var int $i */ |
|
319 | + for ($i = $size; $i >= 0; --$i) { |
|
320 | + $m = (int) (-($int & 1)); |
|
321 | + $x0 = $a0 & $m; |
|
322 | + $x1 = $a1 & $m; |
|
323 | + |
|
324 | + $ret1 += $x1; |
|
325 | + $c = $ret1 >> 16; |
|
326 | + |
|
327 | + $ret0 += $x0 + $c; |
|
328 | + |
|
329 | + $ret0 &= 0xffff; |
|
330 | + $ret1 &= 0xffff; |
|
331 | + |
|
332 | + $a1 = ($a1 << 1); |
|
333 | + $x1 = $a1 >> 16; |
|
334 | + $a0 = ($a0 << 1) | $x1; |
|
335 | + $a0 &= 0xffff; |
|
336 | + $a1 &= 0xffff; |
|
337 | + $int >>= 1; |
|
338 | + } |
|
339 | + $return->limbs[0] = $ret0; |
|
340 | + $return->limbs[1] = $ret1; |
|
341 | + return $return; |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * @param ParagonIE_Sodium_Core32_Int32 $int |
|
346 | + * @param int $size |
|
347 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
348 | + * @throws SodiumException |
|
349 | + * @throws TypeError |
|
350 | + */ |
|
351 | + public function mulInt32(ParagonIE_Sodium_Core32_Int32 $int, $size = 0) |
|
352 | + { |
|
353 | + ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
354 | + if (ParagonIE_Sodium_Compat::$fastMult) { |
|
355 | + return $this->mulInt32Fast($int); |
|
356 | + } |
|
357 | + if (!$size) { |
|
358 | + $size = 31; |
|
359 | + } |
|
360 | + /** @var int $size */ |
|
361 | + |
|
362 | + $a = clone $this; |
|
363 | + $b = clone $int; |
|
364 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
365 | + $return->unsignedInt = $this->unsignedInt; |
|
366 | + |
|
367 | + // Initialize: |
|
368 | + $ret0 = 0; |
|
369 | + $ret1 = 0; |
|
370 | + $a0 = $a->limbs[0]; |
|
371 | + $a1 = $a->limbs[1]; |
|
372 | + $b0 = $b->limbs[0]; |
|
373 | + $b1 = $b->limbs[1]; |
|
374 | + |
|
375 | + /** @var int $size */ |
|
376 | + /** @var int $i */ |
|
377 | + for ($i = $size; $i >= 0; --$i) { |
|
378 | + $m = (int) (-($b1 & 1)); |
|
379 | + $x0 = $a0 & $m; |
|
380 | + $x1 = $a1 & $m; |
|
381 | + |
|
382 | + $ret1 += $x1; |
|
383 | + $c = $ret1 >> 16; |
|
384 | + |
|
385 | + $ret0 += $x0 + $c; |
|
386 | + |
|
387 | + $ret0 &= 0xffff; |
|
388 | + $ret1 &= 0xffff; |
|
389 | + |
|
390 | + $a1 = ($a1 << 1); |
|
391 | + $x1 = $a1 >> 16; |
|
392 | + $a0 = ($a0 << 1) | $x1; |
|
393 | + $a0 &= 0xffff; |
|
394 | + $a1 &= 0xffff; |
|
395 | + |
|
396 | + $x0 = ($b0 & 1) << 16; |
|
397 | + $b0 = ($b0 >> 1); |
|
398 | + $b1 = (($b1 | $x0) >> 1); |
|
399 | + |
|
400 | + $b0 &= 0xffff; |
|
401 | + $b1 &= 0xffff; |
|
402 | + |
|
403 | + } |
|
404 | + $return->limbs[0] = $ret0; |
|
405 | + $return->limbs[1] = $ret1; |
|
406 | + |
|
407 | + return $return; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * OR this 32-bit integer with another. |
|
412 | + * |
|
413 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
414 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
415 | + */ |
|
416 | + public function orInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
417 | + { |
|
418 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
419 | + $return->unsignedInt = $this->unsignedInt; |
|
420 | + $return->limbs = array( |
|
421 | + (int) ($this->limbs[0] | $b->limbs[0]), |
|
422 | + (int) ($this->limbs[1] | $b->limbs[1]) |
|
423 | + ); |
|
424 | + /** @var int overflow */ |
|
425 | + $return->overflow = $this->overflow | $b->overflow; |
|
426 | + return $return; |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * @param int $b |
|
431 | + * @return bool |
|
432 | + */ |
|
433 | + public function isGreaterThan($b = 0) |
|
434 | + { |
|
435 | + return $this->compareInt($b) > 0; |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * @param int $b |
|
440 | + * @return bool |
|
441 | + */ |
|
442 | + public function isLessThanInt($b = 0) |
|
443 | + { |
|
444 | + return $this->compareInt($b) < 0; |
|
445 | + } |
|
446 | + |
|
447 | + /** |
|
448 | + * @param int $c |
|
449 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
450 | + * @throws SodiumException |
|
451 | + * @throws TypeError |
|
452 | + * @psalm-suppress MixedArrayAccess |
|
453 | + */ |
|
454 | + public function rotateLeft($c = 0) |
|
455 | + { |
|
456 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
457 | + /** @var int $c */ |
|
458 | + $c = (int) $c; |
|
459 | + |
|
460 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
461 | + $return->unsignedInt = $this->unsignedInt; |
|
462 | + $c &= 31; |
|
463 | + if ($c === 0) { |
|
464 | + // NOP, but we want a copy. |
|
465 | + $return->limbs = $this->limbs; |
|
466 | + } else { |
|
467 | + /** @var int $c */ |
|
468 | + |
|
469 | + /** @var int $idx_shift */ |
|
470 | + $idx_shift = ($c >> 4) & 1; |
|
471 | + |
|
472 | + /** @var int $sub_shift */ |
|
473 | + $sub_shift = $c & 15; |
|
474 | + |
|
475 | + /** @var array<int, int> $limbs */ |
|
476 | + $limbs =& $return->limbs; |
|
477 | + |
|
478 | + /** @var array<int, int> $myLimbs */ |
|
479 | + $myLimbs =& $this->limbs; |
|
480 | + |
|
481 | + for ($i = 1; $i >= 0; --$i) { |
|
482 | + /** @var int $j */ |
|
483 | + $j = ($i + $idx_shift) & 1; |
|
484 | + /** @var int $k */ |
|
485 | + $k = ($i + $idx_shift + 1) & 1; |
|
486 | + $limbs[$i] = (int) ( |
|
487 | + ( |
|
488 | + ((int) ($myLimbs[$j]) << $sub_shift) |
|
489 | + | |
|
490 | + ((int) ($myLimbs[$k]) >> (16 - $sub_shift)) |
|
491 | + ) & 0xffff |
|
492 | + ); |
|
493 | + } |
|
494 | + } |
|
495 | + return $return; |
|
496 | + } |
|
497 | + |
|
498 | + /** |
|
499 | + * Rotate to the right |
|
500 | + * |
|
501 | + * @param int $c |
|
502 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
503 | + * @throws SodiumException |
|
504 | + * @throws TypeError |
|
505 | + * @psalm-suppress MixedArrayAccess |
|
506 | + */ |
|
507 | + public function rotateRight($c = 0) |
|
508 | + { |
|
509 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
510 | + /** @var int $c */ |
|
511 | + $c = (int) $c; |
|
512 | + |
|
513 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
514 | + $return->unsignedInt = $this->unsignedInt; |
|
515 | + $c &= 31; |
|
516 | + /** @var int $c */ |
|
517 | + if ($c === 0) { |
|
518 | + // NOP, but we want a copy. |
|
519 | + $return->limbs = $this->limbs; |
|
520 | + } else { |
|
521 | + /** @var int $c */ |
|
522 | + |
|
523 | + /** @var int $idx_shift */ |
|
524 | + $idx_shift = ($c >> 4) & 1; |
|
525 | + |
|
526 | + /** @var int $sub_shift */ |
|
527 | + $sub_shift = $c & 15; |
|
528 | + |
|
529 | + /** @var array<int, int> $limbs */ |
|
530 | + $limbs =& $return->limbs; |
|
531 | + |
|
532 | + /** @var array<int, int> $myLimbs */ |
|
533 | + $myLimbs =& $this->limbs; |
|
534 | + |
|
535 | + for ($i = 1; $i >= 0; --$i) { |
|
536 | + /** @var int $j */ |
|
537 | + $j = ($i - $idx_shift) & 1; |
|
538 | + /** @var int $k */ |
|
539 | + $k = ($i - $idx_shift - 1) & 1; |
|
540 | + $limbs[$i] = (int) ( |
|
541 | + ( |
|
542 | + ((int) ($myLimbs[$j]) >> (int) ($sub_shift)) |
|
543 | + | |
|
544 | + ((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift))) |
|
545 | + ) & 0xffff |
|
546 | + ); |
|
547 | + } |
|
548 | + } |
|
549 | + return $return; |
|
550 | + } |
|
551 | + |
|
552 | + /** |
|
553 | + * @param bool $bool |
|
554 | + * @return self |
|
555 | + */ |
|
556 | + public function setUnsignedInt($bool = false) |
|
557 | + { |
|
558 | + $this->unsignedInt = !empty($bool); |
|
559 | + return $this; |
|
560 | + } |
|
561 | + |
|
562 | + /** |
|
563 | + * @param int $c |
|
564 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
565 | + * @throws SodiumException |
|
566 | + * @throws TypeError |
|
567 | + */ |
|
568 | + public function shiftLeft($c = 0) |
|
569 | + { |
|
570 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
571 | + /** @var int $c */ |
|
572 | + $c = (int) $c; |
|
573 | + |
|
574 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
575 | + $return->unsignedInt = $this->unsignedInt; |
|
576 | + $c &= 63; |
|
577 | + /** @var int $c */ |
|
578 | + if ($c === 0) { |
|
579 | + $return->limbs = $this->limbs; |
|
580 | + } elseif ($c < 0) { |
|
581 | + /** @var int $c */ |
|
582 | + return $this->shiftRight(-$c); |
|
583 | + } else { |
|
584 | + /** @var int $c */ |
|
585 | + /** @var int $tmp */ |
|
586 | + $tmp = $this->limbs[1] << $c; |
|
587 | + $return->limbs[1] = (int)($tmp & 0xffff); |
|
588 | + /** @var int $carry */ |
|
589 | + $carry = $tmp >> 16; |
|
590 | + |
|
591 | + /** @var int $tmp */ |
|
592 | + $tmp = ($this->limbs[0] << $c) | ($carry & 0xffff); |
|
593 | + $return->limbs[0] = (int) ($tmp & 0xffff); |
|
594 | + } |
|
595 | + return $return; |
|
596 | + } |
|
597 | + |
|
598 | + /** |
|
599 | + * @param int $c |
|
600 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
601 | + * @throws SodiumException |
|
602 | + * @throws TypeError |
|
603 | + * @psalm-suppress MixedAssignment |
|
604 | + * @psalm-suppress MixedOperand |
|
605 | + */ |
|
606 | + public function shiftRight($c = 0) |
|
607 | + { |
|
608 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
609 | + /** @var int $c */ |
|
610 | + $c = (int) $c; |
|
611 | + |
|
612 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
613 | + $return->unsignedInt = $this->unsignedInt; |
|
614 | + $c &= 63; |
|
615 | + /** @var int $c */ |
|
616 | + if ($c >= 16) { |
|
617 | + $return->limbs = array( |
|
618 | + (int) ($this->overflow & 0xffff), |
|
619 | + (int) ($this->limbs[0]) |
|
620 | + ); |
|
621 | + $return->overflow = $this->overflow >> 16; |
|
622 | + return $return->shiftRight($c & 15); |
|
623 | + } |
|
624 | + if ($c === 0) { |
|
625 | + $return->limbs = $this->limbs; |
|
626 | + } elseif ($c < 0) { |
|
627 | + /** @var int $c */ |
|
628 | + return $this->shiftLeft(-$c); |
|
629 | + } else { |
|
630 | + if (!is_int($c)) { |
|
631 | + throw new TypeError(); |
|
632 | + } |
|
633 | + /** @var int $c */ |
|
634 | + // $return->limbs[0] = (int) (($this->limbs[0] >> $c) & 0xffff); |
|
635 | + $carryLeft = (int) ($this->overflow & ((1 << ($c + 1)) - 1)); |
|
636 | + $return->limbs[0] = (int) ((($this->limbs[0] >> $c) | ($carryLeft << (16 - $c))) & 0xffff); |
|
637 | + $carryRight = (int) ($this->limbs[0] & ((1 << ($c + 1)) - 1)); |
|
638 | + $return->limbs[1] = (int) ((($this->limbs[1] >> $c) | ($carryRight << (16 - $c))) & 0xffff); |
|
639 | + $return->overflow >>= $c; |
|
640 | + } |
|
641 | + return $return; |
|
642 | + } |
|
643 | + |
|
644 | + /** |
|
645 | + * Subtract a normal integer from an int32 object. |
|
646 | + * |
|
647 | + * @param int $int |
|
648 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
649 | + * @throws SodiumException |
|
650 | + * @throws TypeError |
|
651 | + */ |
|
652 | + public function subInt($int) |
|
653 | + { |
|
654 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
655 | + /** @var int $int */ |
|
656 | + $int = (int) $int; |
|
657 | + |
|
658 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
659 | + $return->unsignedInt = $this->unsignedInt; |
|
660 | + |
|
661 | + /** @var int $tmp */ |
|
662 | + $tmp = $this->limbs[1] - ($int & 0xffff); |
|
663 | + /** @var int $carry */ |
|
664 | + $carry = $tmp >> 16; |
|
665 | + $return->limbs[1] = (int) ($tmp & 0xffff); |
|
666 | + |
|
667 | + /** @var int $tmp */ |
|
668 | + $tmp = $this->limbs[0] - (($int >> 16) & 0xffff) + $carry; |
|
669 | + $return->limbs[0] = (int) ($tmp & 0xffff); |
|
670 | + return $return; |
|
671 | + } |
|
672 | + |
|
673 | + /** |
|
674 | + * Subtract two int32 objects from each other |
|
675 | + * |
|
676 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
677 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
678 | + */ |
|
679 | + public function subInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
680 | + { |
|
681 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
682 | + $return->unsignedInt = $this->unsignedInt; |
|
683 | + |
|
684 | + /** @var int $tmp */ |
|
685 | + $tmp = $this->limbs[1] - ($b->limbs[1] & 0xffff); |
|
686 | + /** @var int $carry */ |
|
687 | + $carry = $tmp >> 16; |
|
688 | + $return->limbs[1] = (int) ($tmp & 0xffff); |
|
689 | + |
|
690 | + /** @var int $tmp */ |
|
691 | + $tmp = $this->limbs[0] - ($b->limbs[0] & 0xffff) + $carry; |
|
692 | + $return->limbs[0] = (int) ($tmp & 0xffff); |
|
693 | + return $return; |
|
694 | + } |
|
695 | + |
|
696 | + /** |
|
697 | + * XOR this 32-bit integer with another. |
|
698 | + * |
|
699 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
700 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
701 | + */ |
|
702 | + public function xorInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
703 | + { |
|
704 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
705 | + $return->unsignedInt = $this->unsignedInt; |
|
706 | + $return->limbs = array( |
|
707 | + (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
708 | + (int) ($this->limbs[1] ^ $b->limbs[1]) |
|
709 | + ); |
|
710 | + return $return; |
|
711 | + } |
|
712 | + |
|
713 | + /** |
|
714 | + * @param int $signed |
|
715 | + * @return self |
|
716 | + * @throws SodiumException |
|
717 | + * @throws TypeError |
|
718 | + */ |
|
719 | + public static function fromInt($signed) |
|
720 | + { |
|
721 | + ParagonIE_Sodium_Core32_Util::declareScalarType($signed, 'int', 1);; |
|
722 | + /** @var int $signed */ |
|
723 | + $signed = (int) $signed; |
|
724 | + |
|
725 | + return new ParagonIE_Sodium_Core32_Int32( |
|
726 | + array( |
|
727 | + (int) (($signed >> 16) & 0xffff), |
|
728 | + (int) ($signed & 0xffff) |
|
729 | + ) |
|
730 | + ); |
|
731 | + } |
|
732 | + |
|
733 | + /** |
|
734 | + * @param string $string |
|
735 | + * @return self |
|
736 | + * @throws SodiumException |
|
737 | + * @throws TypeError |
|
738 | + */ |
|
739 | + public static function fromString($string) |
|
740 | + { |
|
741 | + ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
742 | + $string = (string) $string; |
|
743 | + if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
744 | + throw new RangeException( |
|
745 | + 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
746 | + ); |
|
747 | + } |
|
748 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
749 | + |
|
750 | + $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
751 | + $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
752 | + $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
753 | + $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
754 | + return $return; |
|
755 | + } |
|
756 | + |
|
757 | + /** |
|
758 | + * @param string $string |
|
759 | + * @return self |
|
760 | + * @throws SodiumException |
|
761 | + * @throws TypeError |
|
762 | + */ |
|
763 | + public static function fromReverseString($string) |
|
764 | + { |
|
765 | + ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
766 | + $string = (string) $string; |
|
767 | + if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
768 | + throw new RangeException( |
|
769 | + 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
770 | + ); |
|
771 | + } |
|
772 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
773 | + |
|
774 | + $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
775 | + $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
776 | + $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
777 | + $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
778 | + return $return; |
|
779 | + } |
|
780 | + |
|
781 | + /** |
|
782 | + * @return array<int, int> |
|
783 | + */ |
|
784 | + public function toArray() |
|
785 | + { |
|
786 | + return array((int) ($this->limbs[0] << 16 | $this->limbs[1])); |
|
787 | + } |
|
788 | + |
|
789 | + /** |
|
790 | + * @return string |
|
791 | + * @throws TypeError |
|
792 | + */ |
|
793 | + public function toString() |
|
794 | + { |
|
795 | + return |
|
796 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
797 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
798 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
799 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff); |
|
800 | + } |
|
801 | + |
|
802 | + /** |
|
803 | + * @return int |
|
804 | + */ |
|
805 | + public function toInt() |
|
806 | + { |
|
807 | + return (int) ( |
|
808 | + (($this->limbs[0] & 0xffff) << 16) |
|
809 | + | |
|
810 | + ($this->limbs[1] & 0xffff) |
|
811 | + ); |
|
812 | + } |
|
813 | + |
|
814 | + /** |
|
815 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
816 | + */ |
|
817 | + public function toInt32() |
|
818 | + { |
|
819 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
820 | + $return->limbs[0] = (int) ($this->limbs[0] & 0xffff); |
|
821 | + $return->limbs[1] = (int) ($this->limbs[1] & 0xffff); |
|
822 | + $return->unsignedInt = $this->unsignedInt; |
|
823 | + $return->overflow = (int) ($this->overflow & 0x7fffffff); |
|
824 | + return $return; |
|
825 | + } |
|
826 | + |
|
827 | + /** |
|
828 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
829 | + */ |
|
830 | + public function toInt64() |
|
831 | + { |
|
832 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
833 | + $return->unsignedInt = $this->unsignedInt; |
|
834 | + if ($this->unsignedInt) { |
|
835 | + $return->limbs[0] += (($this->overflow >> 16) & 0xffff); |
|
836 | + $return->limbs[1] += (($this->overflow) & 0xffff); |
|
837 | + } else { |
|
838 | + $neg = -(($this->limbs[0] >> 15) & 1); |
|
839 | + $return->limbs[0] = (int)($neg & 0xffff); |
|
840 | + $return->limbs[1] = (int)($neg & 0xffff); |
|
841 | + } |
|
842 | + $return->limbs[2] = (int) ($this->limbs[0] & 0xffff); |
|
843 | + $return->limbs[3] = (int) ($this->limbs[1] & 0xffff); |
|
844 | + return $return; |
|
845 | + } |
|
846 | + |
|
847 | + /** |
|
848 | + * @return string |
|
849 | + * @throws TypeError |
|
850 | + */ |
|
851 | + public function toReverseString() |
|
852 | + { |
|
853 | + return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
854 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
855 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
856 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
857 | + } |
|
858 | + |
|
859 | + /** |
|
860 | + * @return string |
|
861 | + */ |
|
862 | + public function __toString() |
|
863 | + { |
|
864 | + try { |
|
865 | + return $this->toString(); |
|
866 | + } catch (TypeError $ex) { |
|
867 | + // PHP engine can't handle exceptions from __toString() |
|
868 | + return ''; |
|
869 | + } |
|
870 | + } |
|
871 | 871 | } |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * 0 is the higher 16 bits |
16 | 16 | * 1 is the lower 16 bits |
17 | 17 | */ |
18 | - public $limbs = array(0, 0); |
|
18 | + public $limbs = array( 0, 0 ); |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * @var int |
@@ -32,11 +32,11 @@ discard block |
||
32 | 32 | * @param array $array |
33 | 33 | * @param bool $unsignedInt |
34 | 34 | */ |
35 | - public function __construct($array = array(0, 0), $unsignedInt = false) |
|
35 | + public function __construct( $array = array( 0, 0 ), $unsignedInt = false ) |
|
36 | 36 | { |
37 | 37 | $this->limbs = array( |
38 | - (int) $array[0], |
|
39 | - (int) $array[1] |
|
38 | + (int)$array[ 0 ], |
|
39 | + (int)$array[ 1 ] |
|
40 | 40 | ); |
41 | 41 | $this->overflow = 0; |
42 | 42 | $this->unsignedInt = $unsignedInt; |
@@ -48,24 +48,24 @@ discard block |
||
48 | 48 | * @param ParagonIE_Sodium_Core32_Int32 $addend |
49 | 49 | * @return ParagonIE_Sodium_Core32_Int32 |
50 | 50 | */ |
51 | - public function addInt32(ParagonIE_Sodium_Core32_Int32 $addend) |
|
51 | + public function addInt32( ParagonIE_Sodium_Core32_Int32 $addend ) |
|
52 | 52 | { |
53 | - $i0 = $this->limbs[0]; |
|
54 | - $i1 = $this->limbs[1]; |
|
55 | - $j0 = $addend->limbs[0]; |
|
56 | - $j1 = $addend->limbs[1]; |
|
53 | + $i0 = $this->limbs[ 0 ]; |
|
54 | + $i1 = $this->limbs[ 1 ]; |
|
55 | + $j0 = $addend->limbs[ 0 ]; |
|
56 | + $j1 = $addend->limbs[ 1 ]; |
|
57 | 57 | |
58 | - $r1 = $i1 + ($j1 & 0xffff); |
|
58 | + $r1 = $i1 + ( $j1 & 0xffff ); |
|
59 | 59 | $carry = $r1 >> 16; |
60 | 60 | |
61 | - $r0 = $i0 + ($j0 & 0xffff) + $carry; |
|
61 | + $r0 = $i0 + ( $j0 & 0xffff ) + $carry; |
|
62 | 62 | $carry = $r0 >> 16; |
63 | 63 | |
64 | 64 | $r0 &= 0xffff; |
65 | 65 | $r1 &= 0xffff; |
66 | 66 | |
67 | 67 | $return = new ParagonIE_Sodium_Core32_Int32( |
68 | - array($r0, $r1) |
|
68 | + array( $r0, $r1 ) |
|
69 | 69 | ); |
70 | 70 | $return->overflow = $carry; |
71 | 71 | $return->unsignedInt = $this->unsignedInt; |
@@ -80,26 +80,26 @@ discard block |
||
80 | 80 | * @throws SodiumException |
81 | 81 | * @throws TypeError |
82 | 82 | */ |
83 | - public function addInt($int) |
|
83 | + public function addInt( $int ) |
|
84 | 84 | { |
85 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
85 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $int, 'int', 1 ); |
|
86 | 86 | /** @var int $int */ |
87 | - $int = (int) $int; |
|
87 | + $int = (int)$int; |
|
88 | 88 | |
89 | - $int = (int) $int; |
|
89 | + $int = (int)$int; |
|
90 | 90 | |
91 | - $i0 = $this->limbs[0]; |
|
92 | - $i1 = $this->limbs[1]; |
|
91 | + $i0 = $this->limbs[ 0 ]; |
|
92 | + $i1 = $this->limbs[ 1 ]; |
|
93 | 93 | |
94 | - $r1 = $i1 + ($int & 0xffff); |
|
94 | + $r1 = $i1 + ( $int & 0xffff ); |
|
95 | 95 | $carry = $r1 >> 16; |
96 | 96 | |
97 | - $r0 = $i0 + (($int >> 16) & 0xffff) + $carry; |
|
97 | + $r0 = $i0 + ( ( $int >> 16 ) & 0xffff ) + $carry; |
|
98 | 98 | $carry = $r0 >> 16; |
99 | 99 | $r0 &= 0xffff; |
100 | 100 | $r1 &= 0xffff; |
101 | 101 | $return = new ParagonIE_Sodium_Core32_Int32( |
102 | - array($r0, $r1) |
|
102 | + array( $r0, $r1 ) |
|
103 | 103 | ); |
104 | 104 | $return->overflow = $carry; |
105 | 105 | $return->unsignedInt = $this->unsignedInt; |
@@ -110,41 +110,41 @@ discard block |
||
110 | 110 | * @param int $b |
111 | 111 | * @return int |
112 | 112 | */ |
113 | - public function compareInt($b = 0) |
|
113 | + public function compareInt( $b = 0 ) |
|
114 | 114 | { |
115 | 115 | $gt = 0; |
116 | 116 | $eq = 1; |
117 | 117 | |
118 | 118 | $i = 2; |
119 | 119 | $j = 0; |
120 | - while ($i > 0) { |
|
120 | + while ( $i > 0 ) { |
|
121 | 121 | --$i; |
122 | 122 | /** @var int $x1 */ |
123 | - $x1 = $this->limbs[$i]; |
|
123 | + $x1 = $this->limbs[ $i ]; |
|
124 | 124 | /** @var int $x2 */ |
125 | - $x2 = ($b >> ($j << 4)) & 0xffff; |
|
125 | + $x2 = ( $b >> ( $j << 4 ) ) & 0xffff; |
|
126 | 126 | /** @var int $gt */ |
127 | - $gt |= (($x2 - $x1) >> 8) & $eq; |
|
127 | + $gt |= ( ( $x2 - $x1 ) >> 8 ) & $eq; |
|
128 | 128 | /** @var int $eq */ |
129 | - $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
129 | + $eq &= ( ( $x2 ^ $x1 ) - 1 ) >> 8; |
|
130 | 130 | } |
131 | - return ($gt + $gt - $eq) + 1; |
|
131 | + return ( $gt + $gt - $eq ) + 1; |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | /** |
135 | 135 | * @param int $m |
136 | 136 | * @return ParagonIE_Sodium_Core32_Int32 |
137 | 137 | */ |
138 | - public function mask($m = 0) |
|
138 | + public function mask( $m = 0 ) |
|
139 | 139 | { |
140 | 140 | /** @var int $hi */ |
141 | - $hi = ($m >> 16) & 0xffff; |
|
141 | + $hi = ( $m >> 16 ) & 0xffff; |
|
142 | 142 | /** @var int $lo */ |
143 | - $lo = ($m & 0xffff); |
|
143 | + $lo = ( $m & 0xffff ); |
|
144 | 144 | return new ParagonIE_Sodium_Core32_Int32( |
145 | 145 | array( |
146 | - (int) ($this->limbs[0] & $hi), |
|
147 | - (int) ($this->limbs[1] & $lo) |
|
146 | + (int)( $this->limbs[ 0 ] & $hi ), |
|
147 | + (int)( $this->limbs[ 1 ] & $lo ) |
|
148 | 148 | ), |
149 | 149 | $this->unsignedInt |
150 | 150 | ); |
@@ -156,77 +156,77 @@ discard block |
||
156 | 156 | * @param int $baseLog2 |
157 | 157 | * @return array<int, int> |
158 | 158 | */ |
159 | - public function multiplyLong(array $a, array $b, $baseLog2 = 16) |
|
159 | + public function multiplyLong( array $a, array $b, $baseLog2 = 16 ) |
|
160 | 160 | { |
161 | - $a_l = count($a); |
|
162 | - $b_l = count($b); |
|
161 | + $a_l = count( $a ); |
|
162 | + $b_l = count( $b ); |
|
163 | 163 | /** @var array<int, int> $r */ |
164 | - $r = array_fill(0, $a_l + $b_l + 1, 0); |
|
164 | + $r = array_fill( 0, $a_l + $b_l + 1, 0 ); |
|
165 | 165 | $base = 1 << $baseLog2; |
166 | - for ($i = 0; $i < $a_l; ++$i) { |
|
167 | - $a_i = $a[$i]; |
|
168 | - for ($j = 0; $j < $a_l; ++$j) { |
|
169 | - $b_j = $b[$j]; |
|
170 | - $product = ($a_i * $b_j) + $r[$i + $j]; |
|
171 | - $carry = ($product >> $baseLog2 & 0xffff); |
|
172 | - $r[$i + $j] = ($product - (int) ($carry * $base)) & 0xffff; |
|
173 | - $r[$i + $j + 1] += $carry; |
|
166 | + for ( $i = 0; $i < $a_l; ++$i ) { |
|
167 | + $a_i = $a[ $i ]; |
|
168 | + for ( $j = 0; $j < $a_l; ++$j ) { |
|
169 | + $b_j = $b[ $j ]; |
|
170 | + $product = ( $a_i * $b_j ) + $r[ $i + $j ]; |
|
171 | + $carry = ( $product >> $baseLog2 & 0xffff ); |
|
172 | + $r[ $i + $j ] = ( $product - (int)( $carry * $base ) ) & 0xffff; |
|
173 | + $r[ $i + $j + 1 ] += $carry; |
|
174 | 174 | } |
175 | 175 | } |
176 | - return array_slice($r, 0, 5); |
|
176 | + return array_slice( $r, 0, 5 ); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | /** |
180 | 180 | * @param int $int |
181 | 181 | * @return ParagonIE_Sodium_Core32_Int32 |
182 | 182 | */ |
183 | - public function mulIntFast($int) |
|
183 | + public function mulIntFast( $int ) |
|
184 | 184 | { |
185 | 185 | // Handle negative numbers |
186 | - $aNeg = ($this->limbs[0] >> 15) & 1; |
|
187 | - $bNeg = ($int >> 31) & 1; |
|
188 | - $a = array_reverse($this->limbs); |
|
186 | + $aNeg = ( $this->limbs[ 0 ] >> 15 ) & 1; |
|
187 | + $bNeg = ( $int >> 31 ) & 1; |
|
188 | + $a = array_reverse( $this->limbs ); |
|
189 | 189 | $b = array( |
190 | 190 | $int & 0xffff, |
191 | - ($int >> 16) & 0xffff |
|
191 | + ( $int >> 16 ) & 0xffff |
|
192 | 192 | ); |
193 | - if ($aNeg) { |
|
194 | - for ($i = 0; $i < 2; ++$i) { |
|
195 | - $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
193 | + if ( $aNeg ) { |
|
194 | + for ( $i = 0; $i < 2; ++$i ) { |
|
195 | + $a[ $i ] = ( $a[ $i ] ^ 0xffff ) & 0xffff; |
|
196 | 196 | } |
197 | - ++$a[0]; |
|
197 | + ++$a[ 0 ]; |
|
198 | 198 | } |
199 | - if ($bNeg) { |
|
200 | - for ($i = 0; $i < 2; ++$i) { |
|
201 | - $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
199 | + if ( $bNeg ) { |
|
200 | + for ( $i = 0; $i < 2; ++$i ) { |
|
201 | + $b[ $i ] = ( $b[ $i ] ^ 0xffff ) & 0xffff; |
|
202 | 202 | } |
203 | - ++$b[0]; |
|
203 | + ++$b[ 0 ]; |
|
204 | 204 | } |
205 | 205 | // Multiply |
206 | - $res = $this->multiplyLong($a, $b); |
|
206 | + $res = $this->multiplyLong( $a, $b ); |
|
207 | 207 | |
208 | 208 | // Re-apply negation to results |
209 | - if ($aNeg !== $bNeg) { |
|
210 | - for ($i = 0; $i < 2; ++$i) { |
|
211 | - $res[$i] = (0xffff ^ $res[$i]) & 0xffff; |
|
209 | + if ( $aNeg !== $bNeg ) { |
|
210 | + for ( $i = 0; $i < 2; ++$i ) { |
|
211 | + $res[ $i ] = ( 0xffff ^ $res[ $i ] ) & 0xffff; |
|
212 | 212 | } |
213 | 213 | // Handle integer overflow |
214 | 214 | $c = 1; |
215 | - for ($i = 0; $i < 2; ++$i) { |
|
216 | - $res[$i] += $c; |
|
217 | - $c = $res[$i] >> 16; |
|
218 | - $res[$i] &= 0xffff; |
|
215 | + for ( $i = 0; $i < 2; ++$i ) { |
|
216 | + $res[ $i ] += $c; |
|
217 | + $c = $res[ $i ] >> 16; |
|
218 | + $res[ $i ] &= 0xffff; |
|
219 | 219 | } |
220 | 220 | } |
221 | 221 | |
222 | 222 | // Return our values |
223 | 223 | $return = new ParagonIE_Sodium_Core32_Int32(); |
224 | 224 | $return->limbs = array( |
225 | - $res[1] & 0xffff, |
|
226 | - $res[0] & 0xffff |
|
225 | + $res[ 1 ] & 0xffff, |
|
226 | + $res[ 0 ] & 0xffff |
|
227 | 227 | ); |
228 | - if (count($res) > 2) { |
|
229 | - $return->overflow = $res[2] & 0xffff; |
|
228 | + if ( count( $res ) > 2 ) { |
|
229 | + $return->overflow = $res[ 2 ] & 0xffff; |
|
230 | 230 | } |
231 | 231 | $return->unsignedInt = $this->unsignedInt; |
232 | 232 | return $return; |
@@ -236,46 +236,46 @@ discard block |
||
236 | 236 | * @param ParagonIE_Sodium_Core32_Int32 $right |
237 | 237 | * @return ParagonIE_Sodium_Core32_Int32 |
238 | 238 | */ |
239 | - public function mulInt32Fast(ParagonIE_Sodium_Core32_Int32 $right) |
|
239 | + public function mulInt32Fast( ParagonIE_Sodium_Core32_Int32 $right ) |
|
240 | 240 | { |
241 | - $aNeg = ($this->limbs[0] >> 15) & 1; |
|
242 | - $bNeg = ($right->limbs[0] >> 15) & 1; |
|
243 | - |
|
244 | - $a = array_reverse($this->limbs); |
|
245 | - $b = array_reverse($right->limbs); |
|
246 | - if ($aNeg) { |
|
247 | - for ($i = 0; $i < 2; ++$i) { |
|
248 | - $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
241 | + $aNeg = ( $this->limbs[ 0 ] >> 15 ) & 1; |
|
242 | + $bNeg = ( $right->limbs[ 0 ] >> 15 ) & 1; |
|
243 | + |
|
244 | + $a = array_reverse( $this->limbs ); |
|
245 | + $b = array_reverse( $right->limbs ); |
|
246 | + if ( $aNeg ) { |
|
247 | + for ( $i = 0; $i < 2; ++$i ) { |
|
248 | + $a[ $i ] = ( $a[ $i ] ^ 0xffff ) & 0xffff; |
|
249 | 249 | } |
250 | - ++$a[0]; |
|
250 | + ++$a[ 0 ]; |
|
251 | 251 | } |
252 | - if ($bNeg) { |
|
253 | - for ($i = 0; $i < 2; ++$i) { |
|
254 | - $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
252 | + if ( $bNeg ) { |
|
253 | + for ( $i = 0; $i < 2; ++$i ) { |
|
254 | + $b[ $i ] = ( $b[ $i ] ^ 0xffff ) & 0xffff; |
|
255 | 255 | } |
256 | - ++$b[0]; |
|
256 | + ++$b[ 0 ]; |
|
257 | 257 | } |
258 | - $res = $this->multiplyLong($a, $b); |
|
259 | - if ($aNeg !== $bNeg) { |
|
260 | - if ($aNeg !== $bNeg) { |
|
261 | - for ($i = 0; $i < 2; ++$i) { |
|
262 | - $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; |
|
258 | + $res = $this->multiplyLong( $a, $b ); |
|
259 | + if ( $aNeg !== $bNeg ) { |
|
260 | + if ( $aNeg !== $bNeg ) { |
|
261 | + for ( $i = 0; $i < 2; ++$i ) { |
|
262 | + $res[ $i ] = ( $res[ $i ] ^ 0xffff ) & 0xffff; |
|
263 | 263 | } |
264 | 264 | $c = 1; |
265 | - for ($i = 0; $i < 2; ++$i) { |
|
266 | - $res[$i] += $c; |
|
267 | - $c = $res[$i] >> 16; |
|
268 | - $res[$i] &= 0xffff; |
|
265 | + for ( $i = 0; $i < 2; ++$i ) { |
|
266 | + $res[ $i ] += $c; |
|
267 | + $c = $res[ $i ] >> 16; |
|
268 | + $res[ $i ] &= 0xffff; |
|
269 | 269 | } |
270 | 270 | } |
271 | 271 | } |
272 | 272 | $return = new ParagonIE_Sodium_Core32_Int32(); |
273 | 273 | $return->limbs = array( |
274 | - $res[1] & 0xffff, |
|
275 | - $res[0] & 0xffff |
|
274 | + $res[ 1 ] & 0xffff, |
|
275 | + $res[ 0 ] & 0xffff |
|
276 | 276 | ); |
277 | - if (count($res) > 2) { |
|
278 | - $return->overflow = $res[2]; |
|
277 | + if ( count( $res ) > 2 ) { |
|
278 | + $return->overflow = $res[ 2 ]; |
|
279 | 279 | } |
280 | 280 | return $return; |
281 | 281 | } |
@@ -287,19 +287,19 @@ discard block |
||
287 | 287 | * @throws SodiumException |
288 | 288 | * @throws TypeError |
289 | 289 | */ |
290 | - public function mulInt($int = 0, $size = 0) |
|
290 | + public function mulInt( $int = 0, $size = 0 ) |
|
291 | 291 | { |
292 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
293 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
294 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
295 | - return $this->mulIntFast((int) $int); |
|
292 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $int, 'int', 1 ); |
|
293 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $size, 'int', 2 ); |
|
294 | + if ( ParagonIE_Sodium_Compat::$fastMult ) { |
|
295 | + return $this->mulIntFast( (int)$int ); |
|
296 | 296 | } |
297 | 297 | /** @var int $int */ |
298 | - $int = (int) $int; |
|
298 | + $int = (int)$int; |
|
299 | 299 | /** @var int $size */ |
300 | - $size = (int) $size; |
|
300 | + $size = (int)$size; |
|
301 | 301 | |
302 | - if (!$size) { |
|
302 | + if ( ! $size ) { |
|
303 | 303 | $size = 31; |
304 | 304 | } |
305 | 305 | /** @var int $size */ |
@@ -311,13 +311,13 @@ discard block |
||
311 | 311 | // Initialize: |
312 | 312 | $ret0 = 0; |
313 | 313 | $ret1 = 0; |
314 | - $a0 = $a->limbs[0]; |
|
315 | - $a1 = $a->limbs[1]; |
|
314 | + $a0 = $a->limbs[ 0 ]; |
|
315 | + $a1 = $a->limbs[ 1 ]; |
|
316 | 316 | |
317 | 317 | /** @var int $size */ |
318 | 318 | /** @var int $i */ |
319 | - for ($i = $size; $i >= 0; --$i) { |
|
320 | - $m = (int) (-($int & 1)); |
|
319 | + for ( $i = $size; $i >= 0; --$i ) { |
|
320 | + $m = (int)(-( $int & 1 )); |
|
321 | 321 | $x0 = $a0 & $m; |
322 | 322 | $x1 = $a1 & $m; |
323 | 323 | |
@@ -329,15 +329,15 @@ discard block |
||
329 | 329 | $ret0 &= 0xffff; |
330 | 330 | $ret1 &= 0xffff; |
331 | 331 | |
332 | - $a1 = ($a1 << 1); |
|
332 | + $a1 = ( $a1 << 1 ); |
|
333 | 333 | $x1 = $a1 >> 16; |
334 | - $a0 = ($a0 << 1) | $x1; |
|
334 | + $a0 = ( $a0 << 1 ) | $x1; |
|
335 | 335 | $a0 &= 0xffff; |
336 | 336 | $a1 &= 0xffff; |
337 | 337 | $int >>= 1; |
338 | 338 | } |
339 | - $return->limbs[0] = $ret0; |
|
340 | - $return->limbs[1] = $ret1; |
|
339 | + $return->limbs[ 0 ] = $ret0; |
|
340 | + $return->limbs[ 1 ] = $ret1; |
|
341 | 341 | return $return; |
342 | 342 | } |
343 | 343 | |
@@ -348,13 +348,13 @@ discard block |
||
348 | 348 | * @throws SodiumException |
349 | 349 | * @throws TypeError |
350 | 350 | */ |
351 | - public function mulInt32(ParagonIE_Sodium_Core32_Int32 $int, $size = 0) |
|
351 | + public function mulInt32( ParagonIE_Sodium_Core32_Int32 $int, $size = 0 ) |
|
352 | 352 | { |
353 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
354 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
355 | - return $this->mulInt32Fast($int); |
|
353 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $size, 'int', 2 ); |
|
354 | + if ( ParagonIE_Sodium_Compat::$fastMult ) { |
|
355 | + return $this->mulInt32Fast( $int ); |
|
356 | 356 | } |
357 | - if (!$size) { |
|
357 | + if ( ! $size ) { |
|
358 | 358 | $size = 31; |
359 | 359 | } |
360 | 360 | /** @var int $size */ |
@@ -367,15 +367,15 @@ discard block |
||
367 | 367 | // Initialize: |
368 | 368 | $ret0 = 0; |
369 | 369 | $ret1 = 0; |
370 | - $a0 = $a->limbs[0]; |
|
371 | - $a1 = $a->limbs[1]; |
|
372 | - $b0 = $b->limbs[0]; |
|
373 | - $b1 = $b->limbs[1]; |
|
370 | + $a0 = $a->limbs[ 0 ]; |
|
371 | + $a1 = $a->limbs[ 1 ]; |
|
372 | + $b0 = $b->limbs[ 0 ]; |
|
373 | + $b1 = $b->limbs[ 1 ]; |
|
374 | 374 | |
375 | 375 | /** @var int $size */ |
376 | 376 | /** @var int $i */ |
377 | - for ($i = $size; $i >= 0; --$i) { |
|
378 | - $m = (int) (-($b1 & 1)); |
|
377 | + for ( $i = $size; $i >= 0; --$i ) { |
|
378 | + $m = (int)(-( $b1 & 1 )); |
|
379 | 379 | $x0 = $a0 & $m; |
380 | 380 | $x1 = $a1 & $m; |
381 | 381 | |
@@ -387,22 +387,22 @@ discard block |
||
387 | 387 | $ret0 &= 0xffff; |
388 | 388 | $ret1 &= 0xffff; |
389 | 389 | |
390 | - $a1 = ($a1 << 1); |
|
390 | + $a1 = ( $a1 << 1 ); |
|
391 | 391 | $x1 = $a1 >> 16; |
392 | - $a0 = ($a0 << 1) | $x1; |
|
392 | + $a0 = ( $a0 << 1 ) | $x1; |
|
393 | 393 | $a0 &= 0xffff; |
394 | 394 | $a1 &= 0xffff; |
395 | 395 | |
396 | - $x0 = ($b0 & 1) << 16; |
|
397 | - $b0 = ($b0 >> 1); |
|
398 | - $b1 = (($b1 | $x0) >> 1); |
|
396 | + $x0 = ( $b0 & 1 ) << 16; |
|
397 | + $b0 = ( $b0 >> 1 ); |
|
398 | + $b1 = ( ( $b1 | $x0 ) >> 1 ); |
|
399 | 399 | |
400 | 400 | $b0 &= 0xffff; |
401 | 401 | $b1 &= 0xffff; |
402 | 402 | |
403 | 403 | } |
404 | - $return->limbs[0] = $ret0; |
|
405 | - $return->limbs[1] = $ret1; |
|
404 | + $return->limbs[ 0 ] = $ret0; |
|
405 | + $return->limbs[ 1 ] = $ret1; |
|
406 | 406 | |
407 | 407 | return $return; |
408 | 408 | } |
@@ -413,13 +413,13 @@ discard block |
||
413 | 413 | * @param ParagonIE_Sodium_Core32_Int32 $b |
414 | 414 | * @return ParagonIE_Sodium_Core32_Int32 |
415 | 415 | */ |
416 | - public function orInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
416 | + public function orInt32( ParagonIE_Sodium_Core32_Int32 $b ) |
|
417 | 417 | { |
418 | 418 | $return = new ParagonIE_Sodium_Core32_Int32(); |
419 | 419 | $return->unsignedInt = $this->unsignedInt; |
420 | 420 | $return->limbs = array( |
421 | - (int) ($this->limbs[0] | $b->limbs[0]), |
|
422 | - (int) ($this->limbs[1] | $b->limbs[1]) |
|
421 | + (int)( $this->limbs[ 0 ] | $b->limbs[ 0 ] ), |
|
422 | + (int)( $this->limbs[ 1 ] | $b->limbs[ 1 ] ) |
|
423 | 423 | ); |
424 | 424 | /** @var int overflow */ |
425 | 425 | $return->overflow = $this->overflow | $b->overflow; |
@@ -430,18 +430,18 @@ discard block |
||
430 | 430 | * @param int $b |
431 | 431 | * @return bool |
432 | 432 | */ |
433 | - public function isGreaterThan($b = 0) |
|
433 | + public function isGreaterThan( $b = 0 ) |
|
434 | 434 | { |
435 | - return $this->compareInt($b) > 0; |
|
435 | + return $this->compareInt( $b ) > 0; |
|
436 | 436 | } |
437 | 437 | |
438 | 438 | /** |
439 | 439 | * @param int $b |
440 | 440 | * @return bool |
441 | 441 | */ |
442 | - public function isLessThanInt($b = 0) |
|
442 | + public function isLessThanInt( $b = 0 ) |
|
443 | 443 | { |
444 | - return $this->compareInt($b) < 0; |
|
444 | + return $this->compareInt( $b ) < 0; |
|
445 | 445 | } |
446 | 446 | |
447 | 447 | /** |
@@ -451,43 +451,43 @@ discard block |
||
451 | 451 | * @throws TypeError |
452 | 452 | * @psalm-suppress MixedArrayAccess |
453 | 453 | */ |
454 | - public function rotateLeft($c = 0) |
|
454 | + public function rotateLeft( $c = 0 ) |
|
455 | 455 | { |
456 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
456 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $c, 'int', 1 ); |
|
457 | 457 | /** @var int $c */ |
458 | - $c = (int) $c; |
|
458 | + $c = (int)$c; |
|
459 | 459 | |
460 | 460 | $return = new ParagonIE_Sodium_Core32_Int32(); |
461 | 461 | $return->unsignedInt = $this->unsignedInt; |
462 | 462 | $c &= 31; |
463 | - if ($c === 0) { |
|
463 | + if ( $c === 0 ) { |
|
464 | 464 | // NOP, but we want a copy. |
465 | 465 | $return->limbs = $this->limbs; |
466 | 466 | } else { |
467 | 467 | /** @var int $c */ |
468 | 468 | |
469 | 469 | /** @var int $idx_shift */ |
470 | - $idx_shift = ($c >> 4) & 1; |
|
470 | + $idx_shift = ( $c >> 4 ) & 1; |
|
471 | 471 | |
472 | 472 | /** @var int $sub_shift */ |
473 | 473 | $sub_shift = $c & 15; |
474 | 474 | |
475 | 475 | /** @var array<int, int> $limbs */ |
476 | - $limbs =& $return->limbs; |
|
476 | + $limbs = & $return->limbs; |
|
477 | 477 | |
478 | 478 | /** @var array<int, int> $myLimbs */ |
479 | - $myLimbs =& $this->limbs; |
|
479 | + $myLimbs = & $this->limbs; |
|
480 | 480 | |
481 | - for ($i = 1; $i >= 0; --$i) { |
|
481 | + for ( $i = 1; $i >= 0; --$i ) { |
|
482 | 482 | /** @var int $j */ |
483 | - $j = ($i + $idx_shift) & 1; |
|
483 | + $j = ( $i + $idx_shift ) & 1; |
|
484 | 484 | /** @var int $k */ |
485 | - $k = ($i + $idx_shift + 1) & 1; |
|
486 | - $limbs[$i] = (int) ( |
|
485 | + $k = ( $i + $idx_shift + 1 ) & 1; |
|
486 | + $limbs[ $i ] = (int)( |
|
487 | 487 | ( |
488 | - ((int) ($myLimbs[$j]) << $sub_shift) |
|
488 | + ( (int)( $myLimbs[ $j ] ) << $sub_shift ) |
|
489 | 489 | | |
490 | - ((int) ($myLimbs[$k]) >> (16 - $sub_shift)) |
|
490 | + ( (int)( $myLimbs[ $k ] ) >> ( 16 - $sub_shift ) ) |
|
491 | 491 | ) & 0xffff |
492 | 492 | ); |
493 | 493 | } |
@@ -504,44 +504,44 @@ discard block |
||
504 | 504 | * @throws TypeError |
505 | 505 | * @psalm-suppress MixedArrayAccess |
506 | 506 | */ |
507 | - public function rotateRight($c = 0) |
|
507 | + public function rotateRight( $c = 0 ) |
|
508 | 508 | { |
509 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
509 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $c, 'int', 1 ); |
|
510 | 510 | /** @var int $c */ |
511 | - $c = (int) $c; |
|
511 | + $c = (int)$c; |
|
512 | 512 | |
513 | 513 | $return = new ParagonIE_Sodium_Core32_Int32(); |
514 | 514 | $return->unsignedInt = $this->unsignedInt; |
515 | 515 | $c &= 31; |
516 | 516 | /** @var int $c */ |
517 | - if ($c === 0) { |
|
517 | + if ( $c === 0 ) { |
|
518 | 518 | // NOP, but we want a copy. |
519 | 519 | $return->limbs = $this->limbs; |
520 | 520 | } else { |
521 | 521 | /** @var int $c */ |
522 | 522 | |
523 | 523 | /** @var int $idx_shift */ |
524 | - $idx_shift = ($c >> 4) & 1; |
|
524 | + $idx_shift = ( $c >> 4 ) & 1; |
|
525 | 525 | |
526 | 526 | /** @var int $sub_shift */ |
527 | 527 | $sub_shift = $c & 15; |
528 | 528 | |
529 | 529 | /** @var array<int, int> $limbs */ |
530 | - $limbs =& $return->limbs; |
|
530 | + $limbs = & $return->limbs; |
|
531 | 531 | |
532 | 532 | /** @var array<int, int> $myLimbs */ |
533 | - $myLimbs =& $this->limbs; |
|
533 | + $myLimbs = & $this->limbs; |
|
534 | 534 | |
535 | - for ($i = 1; $i >= 0; --$i) { |
|
535 | + for ( $i = 1; $i >= 0; --$i ) { |
|
536 | 536 | /** @var int $j */ |
537 | - $j = ($i - $idx_shift) & 1; |
|
537 | + $j = ( $i - $idx_shift ) & 1; |
|
538 | 538 | /** @var int $k */ |
539 | - $k = ($i - $idx_shift - 1) & 1; |
|
540 | - $limbs[$i] = (int) ( |
|
539 | + $k = ( $i - $idx_shift - 1 ) & 1; |
|
540 | + $limbs[ $i ] = (int)( |
|
541 | 541 | ( |
542 | - ((int) ($myLimbs[$j]) >> (int) ($sub_shift)) |
|
542 | + ( (int)( $myLimbs[ $j ] ) >> (int)( $sub_shift ) ) |
|
543 | 543 | | |
544 | - ((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift))) |
|
544 | + ( (int)( $myLimbs[ $k ] ) << ( 16 - (int)( $sub_shift ) ) ) |
|
545 | 545 | ) & 0xffff |
546 | 546 | ); |
547 | 547 | } |
@@ -553,9 +553,9 @@ discard block |
||
553 | 553 | * @param bool $bool |
554 | 554 | * @return self |
555 | 555 | */ |
556 | - public function setUnsignedInt($bool = false) |
|
556 | + public function setUnsignedInt( $bool = false ) |
|
557 | 557 | { |
558 | - $this->unsignedInt = !empty($bool); |
|
558 | + $this->unsignedInt = ! empty( $bool ); |
|
559 | 559 | return $this; |
560 | 560 | } |
561 | 561 | |
@@ -565,32 +565,32 @@ discard block |
||
565 | 565 | * @throws SodiumException |
566 | 566 | * @throws TypeError |
567 | 567 | */ |
568 | - public function shiftLeft($c = 0) |
|
568 | + public function shiftLeft( $c = 0 ) |
|
569 | 569 | { |
570 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
570 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $c, 'int', 1 ); |
|
571 | 571 | /** @var int $c */ |
572 | - $c = (int) $c; |
|
572 | + $c = (int)$c; |
|
573 | 573 | |
574 | 574 | $return = new ParagonIE_Sodium_Core32_Int32(); |
575 | 575 | $return->unsignedInt = $this->unsignedInt; |
576 | 576 | $c &= 63; |
577 | 577 | /** @var int $c */ |
578 | - if ($c === 0) { |
|
578 | + if ( $c === 0 ) { |
|
579 | 579 | $return->limbs = $this->limbs; |
580 | - } elseif ($c < 0) { |
|
580 | + } elseif ( $c < 0 ) { |
|
581 | 581 | /** @var int $c */ |
582 | 582 | return $this->shiftRight(-$c); |
583 | 583 | } else { |
584 | 584 | /** @var int $c */ |
585 | 585 | /** @var int $tmp */ |
586 | - $tmp = $this->limbs[1] << $c; |
|
587 | - $return->limbs[1] = (int)($tmp & 0xffff); |
|
586 | + $tmp = $this->limbs[ 1 ] << $c; |
|
587 | + $return->limbs[ 1 ] = (int)( $tmp & 0xffff ); |
|
588 | 588 | /** @var int $carry */ |
589 | 589 | $carry = $tmp >> 16; |
590 | 590 | |
591 | 591 | /** @var int $tmp */ |
592 | - $tmp = ($this->limbs[0] << $c) | ($carry & 0xffff); |
|
593 | - $return->limbs[0] = (int) ($tmp & 0xffff); |
|
592 | + $tmp = ( $this->limbs[ 0 ] << $c ) | ( $carry & 0xffff ); |
|
593 | + $return->limbs[ 0 ] = (int)( $tmp & 0xffff ); |
|
594 | 594 | } |
595 | 595 | return $return; |
596 | 596 | } |
@@ -603,39 +603,39 @@ discard block |
||
603 | 603 | * @psalm-suppress MixedAssignment |
604 | 604 | * @psalm-suppress MixedOperand |
605 | 605 | */ |
606 | - public function shiftRight($c = 0) |
|
606 | + public function shiftRight( $c = 0 ) |
|
607 | 607 | { |
608 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
608 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $c, 'int', 1 ); |
|
609 | 609 | /** @var int $c */ |
610 | - $c = (int) $c; |
|
610 | + $c = (int)$c; |
|
611 | 611 | |
612 | 612 | $return = new ParagonIE_Sodium_Core32_Int32(); |
613 | 613 | $return->unsignedInt = $this->unsignedInt; |
614 | 614 | $c &= 63; |
615 | 615 | /** @var int $c */ |
616 | - if ($c >= 16) { |
|
616 | + if ( $c >= 16 ) { |
|
617 | 617 | $return->limbs = array( |
618 | - (int) ($this->overflow & 0xffff), |
|
619 | - (int) ($this->limbs[0]) |
|
618 | + (int)( $this->overflow & 0xffff ), |
|
619 | + (int)( $this->limbs[ 0 ] ) |
|
620 | 620 | ); |
621 | 621 | $return->overflow = $this->overflow >> 16; |
622 | - return $return->shiftRight($c & 15); |
|
622 | + return $return->shiftRight( $c & 15 ); |
|
623 | 623 | } |
624 | - if ($c === 0) { |
|
624 | + if ( $c === 0 ) { |
|
625 | 625 | $return->limbs = $this->limbs; |
626 | - } elseif ($c < 0) { |
|
626 | + } elseif ( $c < 0 ) { |
|
627 | 627 | /** @var int $c */ |
628 | 628 | return $this->shiftLeft(-$c); |
629 | 629 | } else { |
630 | - if (!is_int($c)) { |
|
630 | + if ( ! is_int( $c ) ) { |
|
631 | 631 | throw new TypeError(); |
632 | 632 | } |
633 | 633 | /** @var int $c */ |
634 | 634 | // $return->limbs[0] = (int) (($this->limbs[0] >> $c) & 0xffff); |
635 | - $carryLeft = (int) ($this->overflow & ((1 << ($c + 1)) - 1)); |
|
636 | - $return->limbs[0] = (int) ((($this->limbs[0] >> $c) | ($carryLeft << (16 - $c))) & 0xffff); |
|
637 | - $carryRight = (int) ($this->limbs[0] & ((1 << ($c + 1)) - 1)); |
|
638 | - $return->limbs[1] = (int) ((($this->limbs[1] >> $c) | ($carryRight << (16 - $c))) & 0xffff); |
|
635 | + $carryLeft = (int)( $this->overflow & ( ( 1 << ( $c + 1 ) ) - 1 ) ); |
|
636 | + $return->limbs[ 0 ] = (int)( ( ( $this->limbs[ 0 ] >> $c ) | ( $carryLeft << ( 16 - $c ) ) ) & 0xffff ); |
|
637 | + $carryRight = (int)( $this->limbs[ 0 ] & ( ( 1 << ( $c + 1 ) ) - 1 ) ); |
|
638 | + $return->limbs[ 1 ] = (int)( ( ( $this->limbs[ 1 ] >> $c ) | ( $carryRight << ( 16 - $c ) ) ) & 0xffff ); |
|
639 | 639 | $return->overflow >>= $c; |
640 | 640 | } |
641 | 641 | return $return; |
@@ -649,24 +649,24 @@ discard block |
||
649 | 649 | * @throws SodiumException |
650 | 650 | * @throws TypeError |
651 | 651 | */ |
652 | - public function subInt($int) |
|
652 | + public function subInt( $int ) |
|
653 | 653 | { |
654 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
654 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $int, 'int', 1 ); |
|
655 | 655 | /** @var int $int */ |
656 | - $int = (int) $int; |
|
656 | + $int = (int)$int; |
|
657 | 657 | |
658 | 658 | $return = new ParagonIE_Sodium_Core32_Int32(); |
659 | 659 | $return->unsignedInt = $this->unsignedInt; |
660 | 660 | |
661 | 661 | /** @var int $tmp */ |
662 | - $tmp = $this->limbs[1] - ($int & 0xffff); |
|
662 | + $tmp = $this->limbs[ 1 ] - ( $int & 0xffff ); |
|
663 | 663 | /** @var int $carry */ |
664 | 664 | $carry = $tmp >> 16; |
665 | - $return->limbs[1] = (int) ($tmp & 0xffff); |
|
665 | + $return->limbs[ 1 ] = (int)( $tmp & 0xffff ); |
|
666 | 666 | |
667 | 667 | /** @var int $tmp */ |
668 | - $tmp = $this->limbs[0] - (($int >> 16) & 0xffff) + $carry; |
|
669 | - $return->limbs[0] = (int) ($tmp & 0xffff); |
|
668 | + $tmp = $this->limbs[ 0 ] - ( ( $int >> 16 ) & 0xffff ) + $carry; |
|
669 | + $return->limbs[ 0 ] = (int)( $tmp & 0xffff ); |
|
670 | 670 | return $return; |
671 | 671 | } |
672 | 672 | |
@@ -676,20 +676,20 @@ discard block |
||
676 | 676 | * @param ParagonIE_Sodium_Core32_Int32 $b |
677 | 677 | * @return ParagonIE_Sodium_Core32_Int32 |
678 | 678 | */ |
679 | - public function subInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
679 | + public function subInt32( ParagonIE_Sodium_Core32_Int32 $b ) |
|
680 | 680 | { |
681 | 681 | $return = new ParagonIE_Sodium_Core32_Int32(); |
682 | 682 | $return->unsignedInt = $this->unsignedInt; |
683 | 683 | |
684 | 684 | /** @var int $tmp */ |
685 | - $tmp = $this->limbs[1] - ($b->limbs[1] & 0xffff); |
|
685 | + $tmp = $this->limbs[ 1 ] - ( $b->limbs[ 1 ] & 0xffff ); |
|
686 | 686 | /** @var int $carry */ |
687 | 687 | $carry = $tmp >> 16; |
688 | - $return->limbs[1] = (int) ($tmp & 0xffff); |
|
688 | + $return->limbs[ 1 ] = (int)( $tmp & 0xffff ); |
|
689 | 689 | |
690 | 690 | /** @var int $tmp */ |
691 | - $tmp = $this->limbs[0] - ($b->limbs[0] & 0xffff) + $carry; |
|
692 | - $return->limbs[0] = (int) ($tmp & 0xffff); |
|
691 | + $tmp = $this->limbs[ 0 ] - ( $b->limbs[ 0 ] & 0xffff ) + $carry; |
|
692 | + $return->limbs[ 0 ] = (int)( $tmp & 0xffff ); |
|
693 | 693 | return $return; |
694 | 694 | } |
695 | 695 | |
@@ -699,13 +699,13 @@ discard block |
||
699 | 699 | * @param ParagonIE_Sodium_Core32_Int32 $b |
700 | 700 | * @return ParagonIE_Sodium_Core32_Int32 |
701 | 701 | */ |
702 | - public function xorInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
702 | + public function xorInt32( ParagonIE_Sodium_Core32_Int32 $b ) |
|
703 | 703 | { |
704 | 704 | $return = new ParagonIE_Sodium_Core32_Int32(); |
705 | 705 | $return->unsignedInt = $this->unsignedInt; |
706 | 706 | $return->limbs = array( |
707 | - (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
708 | - (int) ($this->limbs[1] ^ $b->limbs[1]) |
|
707 | + (int)( $this->limbs[ 0 ] ^ $b->limbs[ 0 ] ), |
|
708 | + (int)( $this->limbs[ 1 ] ^ $b->limbs[ 1 ] ) |
|
709 | 709 | ); |
710 | 710 | return $return; |
711 | 711 | } |
@@ -716,16 +716,16 @@ discard block |
||
716 | 716 | * @throws SodiumException |
717 | 717 | * @throws TypeError |
718 | 718 | */ |
719 | - public static function fromInt($signed) |
|
719 | + public static function fromInt( $signed ) |
|
720 | 720 | { |
721 | - ParagonIE_Sodium_Core32_Util::declareScalarType($signed, 'int', 1);; |
|
721 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $signed, 'int', 1 ); ; |
|
722 | 722 | /** @var int $signed */ |
723 | - $signed = (int) $signed; |
|
723 | + $signed = (int)$signed; |
|
724 | 724 | |
725 | 725 | return new ParagonIE_Sodium_Core32_Int32( |
726 | 726 | array( |
727 | - (int) (($signed >> 16) & 0xffff), |
|
728 | - (int) ($signed & 0xffff) |
|
727 | + (int)( ( $signed >> 16 ) & 0xffff ), |
|
728 | + (int)( $signed & 0xffff ) |
|
729 | 729 | ) |
730 | 730 | ); |
731 | 731 | } |
@@ -736,21 +736,21 @@ discard block |
||
736 | 736 | * @throws SodiumException |
737 | 737 | * @throws TypeError |
738 | 738 | */ |
739 | - public static function fromString($string) |
|
739 | + public static function fromString( $string ) |
|
740 | 740 | { |
741 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
742 | - $string = (string) $string; |
|
743 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
741 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $string, 'string', 1 ); |
|
742 | + $string = (string)$string; |
|
743 | + if ( ParagonIE_Sodium_Core32_Util::strlen( $string ) !== 4 ) { |
|
744 | 744 | throw new RangeException( |
745 | - 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
745 | + 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen( $string ) . ' given.' |
|
746 | 746 | ); |
747 | 747 | } |
748 | 748 | $return = new ParagonIE_Sodium_Core32_Int32(); |
749 | 749 | |
750 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
751 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
752 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
753 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
750 | + $return->limbs[ 0 ] = (int)( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[ 0 ] ) & 0xff ) << 8 ); |
|
751 | + $return->limbs[ 0 ] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[ 1 ] ) & 0xff ); |
|
752 | + $return->limbs[ 1 ] = (int)( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[ 2 ] ) & 0xff ) << 8 ); |
|
753 | + $return->limbs[ 1 ] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[ 3 ] ) & 0xff ); |
|
754 | 754 | return $return; |
755 | 755 | } |
756 | 756 | |
@@ -760,21 +760,21 @@ discard block |
||
760 | 760 | * @throws SodiumException |
761 | 761 | * @throws TypeError |
762 | 762 | */ |
763 | - public static function fromReverseString($string) |
|
763 | + public static function fromReverseString( $string ) |
|
764 | 764 | { |
765 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
766 | - $string = (string) $string; |
|
767 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
765 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $string, 'string', 1 ); |
|
766 | + $string = (string)$string; |
|
767 | + if ( ParagonIE_Sodium_Core32_Util::strlen( $string ) !== 4 ) { |
|
768 | 768 | throw new RangeException( |
769 | - 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
769 | + 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen( $string ) . ' given.' |
|
770 | 770 | ); |
771 | 771 | } |
772 | 772 | $return = new ParagonIE_Sodium_Core32_Int32(); |
773 | 773 | |
774 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
775 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
776 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
777 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
774 | + $return->limbs[ 0 ] = (int)( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[ 3 ] ) & 0xff ) << 8 ); |
|
775 | + $return->limbs[ 0 ] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[ 2 ] ) & 0xff ); |
|
776 | + $return->limbs[ 1 ] = (int)( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[ 1 ] ) & 0xff ) << 8 ); |
|
777 | + $return->limbs[ 1 ] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[ 0 ] ) & 0xff ); |
|
778 | 778 | return $return; |
779 | 779 | } |
780 | 780 | |
@@ -783,7 +783,7 @@ discard block |
||
783 | 783 | */ |
784 | 784 | public function toArray() |
785 | 785 | { |
786 | - return array((int) ($this->limbs[0] << 16 | $this->limbs[1])); |
|
786 | + return array( (int)( $this->limbs[ 0 ] << 16 | $this->limbs[ 1 ] ) ); |
|
787 | 787 | } |
788 | 788 | |
789 | 789 | /** |
@@ -793,10 +793,10 @@ discard block |
||
793 | 793 | public function toString() |
794 | 794 | { |
795 | 795 | return |
796 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
797 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
798 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
799 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff); |
|
796 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[ 0 ] >> 8 ) & 0xff ) . |
|
797 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[ 0 ] & 0xff ) . |
|
798 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[ 1 ] >> 8 ) & 0xff ) . |
|
799 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[ 1 ] & 0xff ); |
|
800 | 800 | } |
801 | 801 | |
802 | 802 | /** |
@@ -804,10 +804,10 @@ discard block |
||
804 | 804 | */ |
805 | 805 | public function toInt() |
806 | 806 | { |
807 | - return (int) ( |
|
808 | - (($this->limbs[0] & 0xffff) << 16) |
|
807 | + return (int)( |
|
808 | + ( ( $this->limbs[ 0 ] & 0xffff ) << 16 ) |
|
809 | 809 | | |
810 | - ($this->limbs[1] & 0xffff) |
|
810 | + ( $this->limbs[ 1 ] & 0xffff ) |
|
811 | 811 | ); |
812 | 812 | } |
813 | 813 | |
@@ -817,10 +817,10 @@ discard block |
||
817 | 817 | public function toInt32() |
818 | 818 | { |
819 | 819 | $return = new ParagonIE_Sodium_Core32_Int32(); |
820 | - $return->limbs[0] = (int) ($this->limbs[0] & 0xffff); |
|
821 | - $return->limbs[1] = (int) ($this->limbs[1] & 0xffff); |
|
820 | + $return->limbs[ 0 ] = (int)( $this->limbs[ 0 ] & 0xffff ); |
|
821 | + $return->limbs[ 1 ] = (int)( $this->limbs[ 1 ] & 0xffff ); |
|
822 | 822 | $return->unsignedInt = $this->unsignedInt; |
823 | - $return->overflow = (int) ($this->overflow & 0x7fffffff); |
|
823 | + $return->overflow = (int)( $this->overflow & 0x7fffffff ); |
|
824 | 824 | return $return; |
825 | 825 | } |
826 | 826 | |
@@ -831,16 +831,16 @@ discard block |
||
831 | 831 | { |
832 | 832 | $return = new ParagonIE_Sodium_Core32_Int64(); |
833 | 833 | $return->unsignedInt = $this->unsignedInt; |
834 | - if ($this->unsignedInt) { |
|
835 | - $return->limbs[0] += (($this->overflow >> 16) & 0xffff); |
|
836 | - $return->limbs[1] += (($this->overflow) & 0xffff); |
|
834 | + if ( $this->unsignedInt ) { |
|
835 | + $return->limbs[ 0 ] += ( ( $this->overflow >> 16 ) & 0xffff ); |
|
836 | + $return->limbs[ 1 ] += ( ( $this->overflow ) & 0xffff ); |
|
837 | 837 | } else { |
838 | - $neg = -(($this->limbs[0] >> 15) & 1); |
|
839 | - $return->limbs[0] = (int)($neg & 0xffff); |
|
840 | - $return->limbs[1] = (int)($neg & 0xffff); |
|
838 | + $neg = -( ( $this->limbs[ 0 ] >> 15 ) & 1 ); |
|
839 | + $return->limbs[ 0 ] = (int)( $neg & 0xffff ); |
|
840 | + $return->limbs[ 1 ] = (int)( $neg & 0xffff ); |
|
841 | 841 | } |
842 | - $return->limbs[2] = (int) ($this->limbs[0] & 0xffff); |
|
843 | - $return->limbs[3] = (int) ($this->limbs[1] & 0xffff); |
|
842 | + $return->limbs[ 2 ] = (int)( $this->limbs[ 0 ] & 0xffff ); |
|
843 | + $return->limbs[ 3 ] = (int)( $this->limbs[ 1 ] & 0xffff ); |
|
844 | 844 | return $return; |
845 | 845 | } |
846 | 846 | |
@@ -850,10 +850,10 @@ discard block |
||
850 | 850 | */ |
851 | 851 | public function toReverseString() |
852 | 852 | { |
853 | - return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
854 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
855 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
856 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
853 | + return ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[ 1 ] & 0xff ) . |
|
854 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[ 1 ] >> 8 ) & 0xff ) . |
|
855 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[ 0 ] & 0xff ) . |
|
856 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[ 0 ] >> 8 ) & 0xff ); |
|
857 | 857 | } |
858 | 858 | |
859 | 859 | /** |
@@ -863,7 +863,7 @@ discard block |
||
863 | 863 | { |
864 | 864 | try { |
865 | 865 | return $this->toString(); |
866 | - } catch (TypeError $ex) { |
|
866 | + } catch ( TypeError $ex ) { |
|
867 | 867 | // PHP engine can't handle exceptions from __toString() |
868 | 868 | return ''; |
869 | 869 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | * |
8 | 8 | * These are immutable. It always returns a new instance. |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core32_Int32 |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core32_Int32 { |
|
12 | 11 | /** |
13 | 12 | * @var array<int, int> - two 16-bit integers |
14 | 13 | * |
@@ -32,8 +31,7 @@ discard block |
||
32 | 31 | * @param array $array |
33 | 32 | * @param bool $unsignedInt |
34 | 33 | */ |
35 | - public function __construct($array = array(0, 0), $unsignedInt = false) |
|
36 | - { |
|
34 | + public function __construct($array = array(0, 0), $unsignedInt = false) { |
|
37 | 35 | $this->limbs = array( |
38 | 36 | (int) $array[0], |
39 | 37 | (int) $array[1] |
@@ -48,8 +46,7 @@ discard block |
||
48 | 46 | * @param ParagonIE_Sodium_Core32_Int32 $addend |
49 | 47 | * @return ParagonIE_Sodium_Core32_Int32 |
50 | 48 | */ |
51 | - public function addInt32(ParagonIE_Sodium_Core32_Int32 $addend) |
|
52 | - { |
|
49 | + public function addInt32(ParagonIE_Sodium_Core32_Int32 $addend) { |
|
53 | 50 | $i0 = $this->limbs[0]; |
54 | 51 | $i1 = $this->limbs[1]; |
55 | 52 | $j0 = $addend->limbs[0]; |
@@ -80,8 +77,7 @@ discard block |
||
80 | 77 | * @throws SodiumException |
81 | 78 | * @throws TypeError |
82 | 79 | */ |
83 | - public function addInt($int) |
|
84 | - { |
|
80 | + public function addInt($int) { |
|
85 | 81 | ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
86 | 82 | /** @var int $int */ |
87 | 83 | $int = (int) $int; |
@@ -110,8 +106,7 @@ discard block |
||
110 | 106 | * @param int $b |
111 | 107 | * @return int |
112 | 108 | */ |
113 | - public function compareInt($b = 0) |
|
114 | - { |
|
109 | + public function compareInt($b = 0) { |
|
115 | 110 | $gt = 0; |
116 | 111 | $eq = 1; |
117 | 112 | |
@@ -135,8 +130,7 @@ discard block |
||
135 | 130 | * @param int $m |
136 | 131 | * @return ParagonIE_Sodium_Core32_Int32 |
137 | 132 | */ |
138 | - public function mask($m = 0) |
|
139 | - { |
|
133 | + public function mask($m = 0) { |
|
140 | 134 | /** @var int $hi */ |
141 | 135 | $hi = ($m >> 16) & 0xffff; |
142 | 136 | /** @var int $lo */ |
@@ -156,8 +150,7 @@ discard block |
||
156 | 150 | * @param int $baseLog2 |
157 | 151 | * @return array<int, int> |
158 | 152 | */ |
159 | - public function multiplyLong(array $a, array $b, $baseLog2 = 16) |
|
160 | - { |
|
153 | + public function multiplyLong(array $a, array $b, $baseLog2 = 16) { |
|
161 | 154 | $a_l = count($a); |
162 | 155 | $b_l = count($b); |
163 | 156 | /** @var array<int, int> $r */ |
@@ -180,8 +173,7 @@ discard block |
||
180 | 173 | * @param int $int |
181 | 174 | * @return ParagonIE_Sodium_Core32_Int32 |
182 | 175 | */ |
183 | - public function mulIntFast($int) |
|
184 | - { |
|
176 | + public function mulIntFast($int) { |
|
185 | 177 | // Handle negative numbers |
186 | 178 | $aNeg = ($this->limbs[0] >> 15) & 1; |
187 | 179 | $bNeg = ($int >> 31) & 1; |
@@ -236,8 +228,7 @@ discard block |
||
236 | 228 | * @param ParagonIE_Sodium_Core32_Int32 $right |
237 | 229 | * @return ParagonIE_Sodium_Core32_Int32 |
238 | 230 | */ |
239 | - public function mulInt32Fast(ParagonIE_Sodium_Core32_Int32 $right) |
|
240 | - { |
|
231 | + public function mulInt32Fast(ParagonIE_Sodium_Core32_Int32 $right) { |
|
241 | 232 | $aNeg = ($this->limbs[0] >> 15) & 1; |
242 | 233 | $bNeg = ($right->limbs[0] >> 15) & 1; |
243 | 234 | |
@@ -287,8 +278,7 @@ discard block |
||
287 | 278 | * @throws SodiumException |
288 | 279 | * @throws TypeError |
289 | 280 | */ |
290 | - public function mulInt($int = 0, $size = 0) |
|
291 | - { |
|
281 | + public function mulInt($int = 0, $size = 0) { |
|
292 | 282 | ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
293 | 283 | ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
294 | 284 | if (ParagonIE_Sodium_Compat::$fastMult) { |
@@ -348,8 +338,7 @@ discard block |
||
348 | 338 | * @throws SodiumException |
349 | 339 | * @throws TypeError |
350 | 340 | */ |
351 | - public function mulInt32(ParagonIE_Sodium_Core32_Int32 $int, $size = 0) |
|
352 | - { |
|
341 | + public function mulInt32(ParagonIE_Sodium_Core32_Int32 $int, $size = 0) { |
|
353 | 342 | ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
354 | 343 | if (ParagonIE_Sodium_Compat::$fastMult) { |
355 | 344 | return $this->mulInt32Fast($int); |
@@ -413,8 +402,7 @@ discard block |
||
413 | 402 | * @param ParagonIE_Sodium_Core32_Int32 $b |
414 | 403 | * @return ParagonIE_Sodium_Core32_Int32 |
415 | 404 | */ |
416 | - public function orInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
417 | - { |
|
405 | + public function orInt32(ParagonIE_Sodium_Core32_Int32 $b) { |
|
418 | 406 | $return = new ParagonIE_Sodium_Core32_Int32(); |
419 | 407 | $return->unsignedInt = $this->unsignedInt; |
420 | 408 | $return->limbs = array( |
@@ -430,8 +418,7 @@ discard block |
||
430 | 418 | * @param int $b |
431 | 419 | * @return bool |
432 | 420 | */ |
433 | - public function isGreaterThan($b = 0) |
|
434 | - { |
|
421 | + public function isGreaterThan($b = 0) { |
|
435 | 422 | return $this->compareInt($b) > 0; |
436 | 423 | } |
437 | 424 | |
@@ -439,8 +426,7 @@ discard block |
||
439 | 426 | * @param int $b |
440 | 427 | * @return bool |
441 | 428 | */ |
442 | - public function isLessThanInt($b = 0) |
|
443 | - { |
|
429 | + public function isLessThanInt($b = 0) { |
|
444 | 430 | return $this->compareInt($b) < 0; |
445 | 431 | } |
446 | 432 | |
@@ -451,8 +437,7 @@ discard block |
||
451 | 437 | * @throws TypeError |
452 | 438 | * @psalm-suppress MixedArrayAccess |
453 | 439 | */ |
454 | - public function rotateLeft($c = 0) |
|
455 | - { |
|
440 | + public function rotateLeft($c = 0) { |
|
456 | 441 | ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
457 | 442 | /** @var int $c */ |
458 | 443 | $c = (int) $c; |
@@ -504,8 +489,7 @@ discard block |
||
504 | 489 | * @throws TypeError |
505 | 490 | * @psalm-suppress MixedArrayAccess |
506 | 491 | */ |
507 | - public function rotateRight($c = 0) |
|
508 | - { |
|
492 | + public function rotateRight($c = 0) { |
|
509 | 493 | ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
510 | 494 | /** @var int $c */ |
511 | 495 | $c = (int) $c; |
@@ -553,8 +537,7 @@ discard block |
||
553 | 537 | * @param bool $bool |
554 | 538 | * @return self |
555 | 539 | */ |
556 | - public function setUnsignedInt($bool = false) |
|
557 | - { |
|
540 | + public function setUnsignedInt($bool = false) { |
|
558 | 541 | $this->unsignedInt = !empty($bool); |
559 | 542 | return $this; |
560 | 543 | } |
@@ -565,8 +548,7 @@ discard block |
||
565 | 548 | * @throws SodiumException |
566 | 549 | * @throws TypeError |
567 | 550 | */ |
568 | - public function shiftLeft($c = 0) |
|
569 | - { |
|
551 | + public function shiftLeft($c = 0) { |
|
570 | 552 | ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
571 | 553 | /** @var int $c */ |
572 | 554 | $c = (int) $c; |
@@ -603,8 +585,7 @@ discard block |
||
603 | 585 | * @psalm-suppress MixedAssignment |
604 | 586 | * @psalm-suppress MixedOperand |
605 | 587 | */ |
606 | - public function shiftRight($c = 0) |
|
607 | - { |
|
588 | + public function shiftRight($c = 0) { |
|
608 | 589 | ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
609 | 590 | /** @var int $c */ |
610 | 591 | $c = (int) $c; |
@@ -649,8 +630,7 @@ discard block |
||
649 | 630 | * @throws SodiumException |
650 | 631 | * @throws TypeError |
651 | 632 | */ |
652 | - public function subInt($int) |
|
653 | - { |
|
633 | + public function subInt($int) { |
|
654 | 634 | ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
655 | 635 | /** @var int $int */ |
656 | 636 | $int = (int) $int; |
@@ -676,8 +656,7 @@ discard block |
||
676 | 656 | * @param ParagonIE_Sodium_Core32_Int32 $b |
677 | 657 | * @return ParagonIE_Sodium_Core32_Int32 |
678 | 658 | */ |
679 | - public function subInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
680 | - { |
|
659 | + public function subInt32(ParagonIE_Sodium_Core32_Int32 $b) { |
|
681 | 660 | $return = new ParagonIE_Sodium_Core32_Int32(); |
682 | 661 | $return->unsignedInt = $this->unsignedInt; |
683 | 662 | |
@@ -699,8 +678,7 @@ discard block |
||
699 | 678 | * @param ParagonIE_Sodium_Core32_Int32 $b |
700 | 679 | * @return ParagonIE_Sodium_Core32_Int32 |
701 | 680 | */ |
702 | - public function xorInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
703 | - { |
|
681 | + public function xorInt32(ParagonIE_Sodium_Core32_Int32 $b) { |
|
704 | 682 | $return = new ParagonIE_Sodium_Core32_Int32(); |
705 | 683 | $return->unsignedInt = $this->unsignedInt; |
706 | 684 | $return->limbs = array( |
@@ -716,8 +694,7 @@ discard block |
||
716 | 694 | * @throws SodiumException |
717 | 695 | * @throws TypeError |
718 | 696 | */ |
719 | - public static function fromInt($signed) |
|
720 | - { |
|
697 | + public static function fromInt($signed) { |
|
721 | 698 | ParagonIE_Sodium_Core32_Util::declareScalarType($signed, 'int', 1);; |
722 | 699 | /** @var int $signed */ |
723 | 700 | $signed = (int) $signed; |
@@ -736,8 +713,7 @@ discard block |
||
736 | 713 | * @throws SodiumException |
737 | 714 | * @throws TypeError |
738 | 715 | */ |
739 | - public static function fromString($string) |
|
740 | - { |
|
716 | + public static function fromString($string) { |
|
741 | 717 | ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
742 | 718 | $string = (string) $string; |
743 | 719 | if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
@@ -760,8 +736,7 @@ discard block |
||
760 | 736 | * @throws SodiumException |
761 | 737 | * @throws TypeError |
762 | 738 | */ |
763 | - public static function fromReverseString($string) |
|
764 | - { |
|
739 | + public static function fromReverseString($string) { |
|
765 | 740 | ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
766 | 741 | $string = (string) $string; |
767 | 742 | if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
@@ -781,8 +756,7 @@ discard block |
||
781 | 756 | /** |
782 | 757 | * @return array<int, int> |
783 | 758 | */ |
784 | - public function toArray() |
|
785 | - { |
|
759 | + public function toArray() { |
|
786 | 760 | return array((int) ($this->limbs[0] << 16 | $this->limbs[1])); |
787 | 761 | } |
788 | 762 | |
@@ -790,8 +764,7 @@ discard block |
||
790 | 764 | * @return string |
791 | 765 | * @throws TypeError |
792 | 766 | */ |
793 | - public function toString() |
|
794 | - { |
|
767 | + public function toString() { |
|
795 | 768 | return |
796 | 769 | ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
797 | 770 | ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
@@ -802,8 +775,7 @@ discard block |
||
802 | 775 | /** |
803 | 776 | * @return int |
804 | 777 | */ |
805 | - public function toInt() |
|
806 | - { |
|
778 | + public function toInt() { |
|
807 | 779 | return (int) ( |
808 | 780 | (($this->limbs[0] & 0xffff) << 16) |
809 | 781 | | |
@@ -814,8 +786,7 @@ discard block |
||
814 | 786 | /** |
815 | 787 | * @return ParagonIE_Sodium_Core32_Int32 |
816 | 788 | */ |
817 | - public function toInt32() |
|
818 | - { |
|
789 | + public function toInt32() { |
|
819 | 790 | $return = new ParagonIE_Sodium_Core32_Int32(); |
820 | 791 | $return->limbs[0] = (int) ($this->limbs[0] & 0xffff); |
821 | 792 | $return->limbs[1] = (int) ($this->limbs[1] & 0xffff); |
@@ -827,8 +798,7 @@ discard block |
||
827 | 798 | /** |
828 | 799 | * @return ParagonIE_Sodium_Core32_Int64 |
829 | 800 | */ |
830 | - public function toInt64() |
|
831 | - { |
|
801 | + public function toInt64() { |
|
832 | 802 | $return = new ParagonIE_Sodium_Core32_Int64(); |
833 | 803 | $return->unsignedInt = $this->unsignedInt; |
834 | 804 | if ($this->unsignedInt) { |
@@ -848,8 +818,7 @@ discard block |
||
848 | 818 | * @return string |
849 | 819 | * @throws TypeError |
850 | 820 | */ |
851 | - public function toReverseString() |
|
852 | - { |
|
821 | + public function toReverseString() { |
|
853 | 822 | return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
854 | 823 | ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
855 | 824 | ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
@@ -859,8 +828,7 @@ discard block |
||
859 | 828 | /** |
860 | 829 | * @return string |
861 | 830 | */ |
862 | - public function __toString() |
|
863 | - { |
|
831 | + public function __toString() { |
|
864 | 832 | try { |
865 | 833 | return $this->toString(); |
866 | 834 | } catch (TypeError $ex) { |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Poly1305', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,55 +9,55 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util |
11 | 11 | { |
12 | - const BLOCK_SIZE = 16; |
|
12 | + const BLOCK_SIZE = 16; |
|
13 | 13 | |
14 | - /** |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param string $m |
|
18 | - * @param string $key |
|
19 | - * @return string |
|
20 | - * @throws SodiumException |
|
21 | - * @throws TypeError |
|
22 | - */ |
|
23 | - public static function onetimeauth($m, $key) |
|
24 | - { |
|
25 | - if (self::strlen($key) < 32) { |
|
26 | - throw new InvalidArgumentException( |
|
27 | - 'Key must be 32 bytes long.' |
|
28 | - ); |
|
29 | - } |
|
30 | - $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
31 | - self::substr($key, 0, 32) |
|
32 | - ); |
|
33 | - return $state |
|
34 | - ->update($m) |
|
35 | - ->finish(); |
|
36 | - } |
|
14 | + /** |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param string $m |
|
18 | + * @param string $key |
|
19 | + * @return string |
|
20 | + * @throws SodiumException |
|
21 | + * @throws TypeError |
|
22 | + */ |
|
23 | + public static function onetimeauth($m, $key) |
|
24 | + { |
|
25 | + if (self::strlen($key) < 32) { |
|
26 | + throw new InvalidArgumentException( |
|
27 | + 'Key must be 32 bytes long.' |
|
28 | + ); |
|
29 | + } |
|
30 | + $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
31 | + self::substr($key, 0, 32) |
|
32 | + ); |
|
33 | + return $state |
|
34 | + ->update($m) |
|
35 | + ->finish(); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @internal You should not use this directly from another application |
|
40 | - * |
|
41 | - * @param string $mac |
|
42 | - * @param string $m |
|
43 | - * @param string $key |
|
44 | - * @return bool |
|
45 | - * @throws SodiumException |
|
46 | - * @throws TypeError |
|
47 | - */ |
|
48 | - public static function onetimeauth_verify($mac, $m, $key) |
|
49 | - { |
|
50 | - if (self::strlen($key) < 32) { |
|
51 | - throw new InvalidArgumentException( |
|
52 | - 'Key must be 32 bytes long.' |
|
53 | - ); |
|
54 | - } |
|
55 | - $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
56 | - self::substr($key, 0, 32) |
|
57 | - ); |
|
58 | - $calc = $state |
|
59 | - ->update($m) |
|
60 | - ->finish(); |
|
61 | - return self::verify_16($calc, $mac); |
|
62 | - } |
|
38 | + /** |
|
39 | + * @internal You should not use this directly from another application |
|
40 | + * |
|
41 | + * @param string $mac |
|
42 | + * @param string $m |
|
43 | + * @param string $key |
|
44 | + * @return bool |
|
45 | + * @throws SodiumException |
|
46 | + * @throws TypeError |
|
47 | + */ |
|
48 | + public static function onetimeauth_verify($mac, $m, $key) |
|
49 | + { |
|
50 | + if (self::strlen($key) < 32) { |
|
51 | + throw new InvalidArgumentException( |
|
52 | + 'Key must be 32 bytes long.' |
|
53 | + ); |
|
54 | + } |
|
55 | + $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
56 | + self::substr($key, 0, 32) |
|
57 | + ); |
|
58 | + $calc = $state |
|
59 | + ->update($m) |
|
60 | + ->finish(); |
|
61 | + return self::verify_16($calc, $mac); |
|
62 | + } |
|
63 | 63 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_Poly1305', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_Poly1305', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -20,18 +20,18 @@ discard block |
||
20 | 20 | * @throws SodiumException |
21 | 21 | * @throws TypeError |
22 | 22 | */ |
23 | - public static function onetimeauth($m, $key) |
|
23 | + public static function onetimeauth( $m, $key ) |
|
24 | 24 | { |
25 | - if (self::strlen($key) < 32) { |
|
25 | + if ( self::strlen( $key ) < 32 ) { |
|
26 | 26 | throw new InvalidArgumentException( |
27 | 27 | 'Key must be 32 bytes long.' |
28 | 28 | ); |
29 | 29 | } |
30 | 30 | $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
31 | - self::substr($key, 0, 32) |
|
31 | + self::substr( $key, 0, 32 ) |
|
32 | 32 | ); |
33 | 33 | return $state |
34 | - ->update($m) |
|
34 | + ->update( $m ) |
|
35 | 35 | ->finish(); |
36 | 36 | } |
37 | 37 | |
@@ -45,19 +45,19 @@ discard block |
||
45 | 45 | * @throws SodiumException |
46 | 46 | * @throws TypeError |
47 | 47 | */ |
48 | - public static function onetimeauth_verify($mac, $m, $key) |
|
48 | + public static function onetimeauth_verify( $mac, $m, $key ) |
|
49 | 49 | { |
50 | - if (self::strlen($key) < 32) { |
|
50 | + if ( self::strlen( $key ) < 32 ) { |
|
51 | 51 | throw new InvalidArgumentException( |
52 | 52 | 'Key must be 32 bytes long.' |
53 | 53 | ); |
54 | 54 | } |
55 | 55 | $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
56 | - self::substr($key, 0, 32) |
|
56 | + self::substr( $key, 0, 32 ) |
|
57 | 57 | ); |
58 | 58 | $calc = $state |
59 | - ->update($m) |
|
59 | + ->update( $m ) |
|
60 | 60 | ->finish(); |
61 | - return self::verify_16($calc, $mac); |
|
61 | + return self::verify_16( $calc, $mac ); |
|
62 | 62 | } |
63 | 63 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_Poly1305 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util { |
|
12 | 11 | const BLOCK_SIZE = 16; |
13 | 12 | |
14 | 13 | /** |
@@ -20,8 +19,7 @@ discard block |
||
20 | 19 | * @throws SodiumException |
21 | 20 | * @throws TypeError |
22 | 21 | */ |
23 | - public static function onetimeauth($m, $key) |
|
24 | - { |
|
22 | + public static function onetimeauth($m, $key) { |
|
25 | 23 | if (self::strlen($key) < 32) { |
26 | 24 | throw new InvalidArgumentException( |
27 | 25 | 'Key must be 32 bytes long.' |
@@ -45,8 +43,7 @@ discard block |
||
45 | 43 | * @throws SodiumException |
46 | 44 | * @throws TypeError |
47 | 45 | */ |
48 | - public static function onetimeauth_verify($mac, $m, $key) |
|
49 | - { |
|
46 | + public static function onetimeauth_verify($mac, $m, $key) { |
|
50 | 47 | if (self::strlen($key) < 32) { |
51 | 48 | throw new InvalidArgumentException( |
52 | 49 | 'Key must be 32 bytes long.' |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_XSalsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,49 +9,49 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20 |
11 | 11 | { |
12 | - /** |
|
13 | - * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param int $len |
|
18 | - * @param string $nonce |
|
19 | - * @param string $key |
|
20 | - * @return string |
|
21 | - * @throws SodiumException |
|
22 | - * @throws TypeError |
|
23 | - */ |
|
24 | - public static function xsalsa20($len, $nonce, $key) |
|
25 | - { |
|
26 | - $ret = self::salsa20( |
|
27 | - $len, |
|
28 | - self::substr($nonce, 16, 8), |
|
29 | - self::hsalsa20($nonce, $key) |
|
30 | - ); |
|
31 | - return $ret; |
|
32 | - } |
|
12 | + /** |
|
13 | + * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param int $len |
|
18 | + * @param string $nonce |
|
19 | + * @param string $key |
|
20 | + * @return string |
|
21 | + * @throws SodiumException |
|
22 | + * @throws TypeError |
|
23 | + */ |
|
24 | + public static function xsalsa20($len, $nonce, $key) |
|
25 | + { |
|
26 | + $ret = self::salsa20( |
|
27 | + $len, |
|
28 | + self::substr($nonce, 16, 8), |
|
29 | + self::hsalsa20($nonce, $key) |
|
30 | + ); |
|
31 | + return $ret; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
36 | - * |
|
37 | - * @internal You should not use this directly from another application |
|
38 | - * |
|
39 | - * @param string $message |
|
40 | - * @param string $nonce |
|
41 | - * @param string $key |
|
42 | - * @return string |
|
43 | - * @throws SodiumException |
|
44 | - * @throws TypeError |
|
45 | - */ |
|
46 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | - { |
|
48 | - return self::xorStrings( |
|
49 | - $message, |
|
50 | - self::xsalsa20( |
|
51 | - self::strlen($message), |
|
52 | - $nonce, |
|
53 | - $key |
|
54 | - ) |
|
55 | - ); |
|
56 | - } |
|
34 | + /** |
|
35 | + * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
36 | + * |
|
37 | + * @internal You should not use this directly from another application |
|
38 | + * |
|
39 | + * @param string $message |
|
40 | + * @param string $nonce |
|
41 | + * @param string $key |
|
42 | + * @return string |
|
43 | + * @throws SodiumException |
|
44 | + * @throws TypeError |
|
45 | + */ |
|
46 | + public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | + { |
|
48 | + return self::xorStrings( |
|
49 | + $message, |
|
50 | + self::xsalsa20( |
|
51 | + self::strlen($message), |
|
52 | + $nonce, |
|
53 | + $key |
|
54 | + ) |
|
55 | + ); |
|
56 | + } |
|
57 | 57 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_XSalsa20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_XSalsa20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -21,12 +21,12 @@ discard block |
||
21 | 21 | * @throws SodiumException |
22 | 22 | * @throws TypeError |
23 | 23 | */ |
24 | - public static function xsalsa20($len, $nonce, $key) |
|
24 | + public static function xsalsa20( $len, $nonce, $key ) |
|
25 | 25 | { |
26 | 26 | $ret = self::salsa20( |
27 | 27 | $len, |
28 | - self::substr($nonce, 16, 8), |
|
29 | - self::hsalsa20($nonce, $key) |
|
28 | + self::substr( $nonce, 16, 8 ), |
|
29 | + self::hsalsa20( $nonce, $key ) |
|
30 | 30 | ); |
31 | 31 | return $ret; |
32 | 32 | } |
@@ -43,12 +43,12 @@ discard block |
||
43 | 43 | * @throws SodiumException |
44 | 44 | * @throws TypeError |
45 | 45 | */ |
46 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
46 | + public static function xsalsa20_xor( $message, $nonce, $key ) |
|
47 | 47 | { |
48 | 48 | return self::xorStrings( |
49 | 49 | $message, |
50 | 50 | self::xsalsa20( |
51 | - self::strlen($message), |
|
51 | + self::strlen( $message ), |
|
52 | 52 | $nonce, |
53 | 53 | $key |
54 | 54 | ) |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_XSalsa20 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20 |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20 { |
|
12 | 11 | /** |
13 | 12 | * Expand a key and nonce into an xsalsa20 keystream. |
14 | 13 | * |
@@ -21,8 +20,7 @@ discard block |
||
21 | 20 | * @throws SodiumException |
22 | 21 | * @throws TypeError |
23 | 22 | */ |
24 | - public static function xsalsa20($len, $nonce, $key) |
|
25 | - { |
|
23 | + public static function xsalsa20($len, $nonce, $key) { |
|
26 | 24 | $ret = self::salsa20( |
27 | 25 | $len, |
28 | 26 | self::substr($nonce, 16, 8), |
@@ -43,8 +41,7 @@ discard block |
||
43 | 41 | * @throws SodiumException |
44 | 42 | * @throws TypeError |
45 | 43 | */ |
46 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | - { |
|
44 | + public static function xsalsa20_xor($message, $nonce, $key) { |
|
48 | 45 | return self::xorStrings( |
49 | 46 | $message, |
50 | 47 | self::xsalsa20( |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_ChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,83 +9,83 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util |
11 | 11 | { |
12 | - /** |
|
13 | - * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param ParagonIE_Sodium_Core32_Int32 $a |
|
18 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
19 | - * @param ParagonIE_Sodium_Core32_Int32 $c |
|
20 | - * @param ParagonIE_Sodium_Core32_Int32 $d |
|
21 | - * @return array<int, ParagonIE_Sodium_Core32_Int32> |
|
22 | - * @throws SodiumException |
|
23 | - * @throws TypeError |
|
24 | - */ |
|
25 | - protected static function quarterRound( |
|
26 | - ParagonIE_Sodium_Core32_Int32 $a, |
|
27 | - ParagonIE_Sodium_Core32_Int32 $b, |
|
28 | - ParagonIE_Sodium_Core32_Int32 $c, |
|
29 | - ParagonIE_Sodium_Core32_Int32 $d |
|
30 | - ) { |
|
31 | - /** @var ParagonIE_Sodium_Core32_Int32 $a */ |
|
32 | - /** @var ParagonIE_Sodium_Core32_Int32 $b */ |
|
33 | - /** @var ParagonIE_Sodium_Core32_Int32 $c */ |
|
34 | - /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
|
12 | + /** |
|
13 | + * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param ParagonIE_Sodium_Core32_Int32 $a |
|
18 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
19 | + * @param ParagonIE_Sodium_Core32_Int32 $c |
|
20 | + * @param ParagonIE_Sodium_Core32_Int32 $d |
|
21 | + * @return array<int, ParagonIE_Sodium_Core32_Int32> |
|
22 | + * @throws SodiumException |
|
23 | + * @throws TypeError |
|
24 | + */ |
|
25 | + protected static function quarterRound( |
|
26 | + ParagonIE_Sodium_Core32_Int32 $a, |
|
27 | + ParagonIE_Sodium_Core32_Int32 $b, |
|
28 | + ParagonIE_Sodium_Core32_Int32 $c, |
|
29 | + ParagonIE_Sodium_Core32_Int32 $d |
|
30 | + ) { |
|
31 | + /** @var ParagonIE_Sodium_Core32_Int32 $a */ |
|
32 | + /** @var ParagonIE_Sodium_Core32_Int32 $b */ |
|
33 | + /** @var ParagonIE_Sodium_Core32_Int32 $c */ |
|
34 | + /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
|
35 | 35 | |
36 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
37 | - $a = $a->addInt32($b); |
|
38 | - $d = $d->xorInt32($a)->rotateLeft(16); |
|
36 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
37 | + $a = $a->addInt32($b); |
|
38 | + $d = $d->xorInt32($a)->rotateLeft(16); |
|
39 | 39 | |
40 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
41 | - $c = $c->addInt32($d); |
|
42 | - $b = $b->xorInt32($c)->rotateLeft(12); |
|
40 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
41 | + $c = $c->addInt32($d); |
|
42 | + $b = $b->xorInt32($c)->rotateLeft(12); |
|
43 | 43 | |
44 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
45 | - $a = $a->addInt32($b); |
|
46 | - $d = $d->xorInt32($a)->rotateLeft(8); |
|
44 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
45 | + $a = $a->addInt32($b); |
|
46 | + $d = $d->xorInt32($a)->rotateLeft(8); |
|
47 | 47 | |
48 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
49 | - $c = $c->addInt32($d); |
|
50 | - $b = $b->xorInt32($c)->rotateLeft(7); |
|
48 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
49 | + $c = $c->addInt32($d); |
|
50 | + $b = $b->xorInt32($c)->rotateLeft(7); |
|
51 | 51 | |
52 | - return array($a, $b, $c, $d); |
|
53 | - } |
|
52 | + return array($a, $b, $c, $d); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @internal You should not use this directly from another application |
|
57 | - * |
|
58 | - * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx |
|
59 | - * @param string $message |
|
60 | - * |
|
61 | - * @return string |
|
62 | - * @throws SodiumException |
|
63 | - * @throws TypeError |
|
64 | - */ |
|
65 | - public static function encryptBytes( |
|
66 | - ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
|
67 | - $message = '' |
|
68 | - ) { |
|
69 | - $bytes = self::strlen($message); |
|
55 | + /** |
|
56 | + * @internal You should not use this directly from another application |
|
57 | + * |
|
58 | + * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx |
|
59 | + * @param string $message |
|
60 | + * |
|
61 | + * @return string |
|
62 | + * @throws SodiumException |
|
63 | + * @throws TypeError |
|
64 | + */ |
|
65 | + public static function encryptBytes( |
|
66 | + ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
|
67 | + $message = '' |
|
68 | + ) { |
|
69 | + $bytes = self::strlen($message); |
|
70 | 70 | |
71 | - /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
72 | - /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
73 | - /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
74 | - /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
75 | - /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
76 | - /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
77 | - /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
78 | - /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
79 | - /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
80 | - /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
81 | - /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
82 | - /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
83 | - /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | - /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
85 | - /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
86 | - /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
71 | + /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
72 | + /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
73 | + /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
74 | + /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
75 | + /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
76 | + /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
77 | + /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
78 | + /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
79 | + /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
80 | + /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
81 | + /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
82 | + /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
83 | + /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | + /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
85 | + /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
86 | + /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
87 | 87 | |
88 | - /* |
|
88 | + /* |
|
89 | 89 | j0 = ctx->input[0]; |
90 | 90 | j1 = ctx->input[1]; |
91 | 91 | j2 = ctx->input[2]; |
@@ -103,89 +103,89 @@ discard block |
||
103 | 103 | j14 = ctx->input[14]; |
104 | 104 | j15 = ctx->input[15]; |
105 | 105 | */ |
106 | - /** @var ParagonIE_Sodium_Core32_Int32 $j0 */ |
|
107 | - $j0 = $ctx[0]; |
|
108 | - /** @var ParagonIE_Sodium_Core32_Int32 $j1 */ |
|
109 | - $j1 = $ctx[1]; |
|
110 | - /** @var ParagonIE_Sodium_Core32_Int32 $j2 */ |
|
111 | - $j2 = $ctx[2]; |
|
112 | - /** @var ParagonIE_Sodium_Core32_Int32 $j3 */ |
|
113 | - $j3 = $ctx[3]; |
|
114 | - /** @var ParagonIE_Sodium_Core32_Int32 $j4 */ |
|
115 | - $j4 = $ctx[4]; |
|
116 | - /** @var ParagonIE_Sodium_Core32_Int32 $j5 */ |
|
117 | - $j5 = $ctx[5]; |
|
118 | - /** @var ParagonIE_Sodium_Core32_Int32 $j6 */ |
|
119 | - $j6 = $ctx[6]; |
|
120 | - /** @var ParagonIE_Sodium_Core32_Int32 $j7 */ |
|
121 | - $j7 = $ctx[7]; |
|
122 | - /** @var ParagonIE_Sodium_Core32_Int32 $j8 */ |
|
123 | - $j8 = $ctx[8]; |
|
124 | - /** @var ParagonIE_Sodium_Core32_Int32 $j9 */ |
|
125 | - $j9 = $ctx[9]; |
|
126 | - /** @var ParagonIE_Sodium_Core32_Int32 $j10 */ |
|
127 | - $j10 = $ctx[10]; |
|
128 | - /** @var ParagonIE_Sodium_Core32_Int32 $j11 */ |
|
129 | - $j11 = $ctx[11]; |
|
130 | - /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
131 | - $j12 = $ctx[12]; |
|
132 | - /** @var ParagonIE_Sodium_Core32_Int32 $j13 */ |
|
133 | - $j13 = $ctx[13]; |
|
134 | - /** @var ParagonIE_Sodium_Core32_Int32 $j14 */ |
|
135 | - $j14 = $ctx[14]; |
|
136 | - /** @var ParagonIE_Sodium_Core32_Int32 $j15 */ |
|
137 | - $j15 = $ctx[15]; |
|
106 | + /** @var ParagonIE_Sodium_Core32_Int32 $j0 */ |
|
107 | + $j0 = $ctx[0]; |
|
108 | + /** @var ParagonIE_Sodium_Core32_Int32 $j1 */ |
|
109 | + $j1 = $ctx[1]; |
|
110 | + /** @var ParagonIE_Sodium_Core32_Int32 $j2 */ |
|
111 | + $j2 = $ctx[2]; |
|
112 | + /** @var ParagonIE_Sodium_Core32_Int32 $j3 */ |
|
113 | + $j3 = $ctx[3]; |
|
114 | + /** @var ParagonIE_Sodium_Core32_Int32 $j4 */ |
|
115 | + $j4 = $ctx[4]; |
|
116 | + /** @var ParagonIE_Sodium_Core32_Int32 $j5 */ |
|
117 | + $j5 = $ctx[5]; |
|
118 | + /** @var ParagonIE_Sodium_Core32_Int32 $j6 */ |
|
119 | + $j6 = $ctx[6]; |
|
120 | + /** @var ParagonIE_Sodium_Core32_Int32 $j7 */ |
|
121 | + $j7 = $ctx[7]; |
|
122 | + /** @var ParagonIE_Sodium_Core32_Int32 $j8 */ |
|
123 | + $j8 = $ctx[8]; |
|
124 | + /** @var ParagonIE_Sodium_Core32_Int32 $j9 */ |
|
125 | + $j9 = $ctx[9]; |
|
126 | + /** @var ParagonIE_Sodium_Core32_Int32 $j10 */ |
|
127 | + $j10 = $ctx[10]; |
|
128 | + /** @var ParagonIE_Sodium_Core32_Int32 $j11 */ |
|
129 | + $j11 = $ctx[11]; |
|
130 | + /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
131 | + $j12 = $ctx[12]; |
|
132 | + /** @var ParagonIE_Sodium_Core32_Int32 $j13 */ |
|
133 | + $j13 = $ctx[13]; |
|
134 | + /** @var ParagonIE_Sodium_Core32_Int32 $j14 */ |
|
135 | + $j14 = $ctx[14]; |
|
136 | + /** @var ParagonIE_Sodium_Core32_Int32 $j15 */ |
|
137 | + $j15 = $ctx[15]; |
|
138 | 138 | |
139 | - $c = ''; |
|
140 | - for (;;) { |
|
141 | - if ($bytes < 64) { |
|
142 | - $message .= str_repeat("\x00", 64 - $bytes); |
|
143 | - } |
|
139 | + $c = ''; |
|
140 | + for (;;) { |
|
141 | + if ($bytes < 64) { |
|
142 | + $message .= str_repeat("\x00", 64 - $bytes); |
|
143 | + } |
|
144 | 144 | |
145 | - $x0 = clone $j0; |
|
146 | - $x1 = clone $j1; |
|
147 | - $x2 = clone $j2; |
|
148 | - $x3 = clone $j3; |
|
149 | - $x4 = clone $j4; |
|
150 | - $x5 = clone $j5; |
|
151 | - $x6 = clone $j6; |
|
152 | - $x7 = clone $j7; |
|
153 | - $x8 = clone $j8; |
|
154 | - $x9 = clone $j9; |
|
155 | - $x10 = clone $j10; |
|
156 | - $x11 = clone $j11; |
|
157 | - $x12 = clone $j12; |
|
158 | - $x13 = clone $j13; |
|
159 | - $x14 = clone $j14; |
|
160 | - $x15 = clone $j15; |
|
145 | + $x0 = clone $j0; |
|
146 | + $x1 = clone $j1; |
|
147 | + $x2 = clone $j2; |
|
148 | + $x3 = clone $j3; |
|
149 | + $x4 = clone $j4; |
|
150 | + $x5 = clone $j5; |
|
151 | + $x6 = clone $j6; |
|
152 | + $x7 = clone $j7; |
|
153 | + $x8 = clone $j8; |
|
154 | + $x9 = clone $j9; |
|
155 | + $x10 = clone $j10; |
|
156 | + $x11 = clone $j11; |
|
157 | + $x12 = clone $j12; |
|
158 | + $x13 = clone $j13; |
|
159 | + $x14 = clone $j14; |
|
160 | + $x15 = clone $j15; |
|
161 | 161 | |
162 | - # for (i = 20; i > 0; i -= 2) { |
|
163 | - for ($i = 20; $i > 0; $i -= 2) { |
|
164 | - # QUARTERROUND( x0, x4, x8, x12) |
|
165 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
162 | + # for (i = 20; i > 0; i -= 2) { |
|
163 | + for ($i = 20; $i > 0; $i -= 2) { |
|
164 | + # QUARTERROUND( x0, x4, x8, x12) |
|
165 | + list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
166 | 166 | |
167 | - # QUARTERROUND( x1, x5, x9, x13) |
|
168 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
167 | + # QUARTERROUND( x1, x5, x9, x13) |
|
168 | + list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
169 | 169 | |
170 | - # QUARTERROUND( x2, x6, x10, x14) |
|
171 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
170 | + # QUARTERROUND( x2, x6, x10, x14) |
|
171 | + list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
172 | 172 | |
173 | - # QUARTERROUND( x3, x7, x11, x15) |
|
174 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
173 | + # QUARTERROUND( x3, x7, x11, x15) |
|
174 | + list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
175 | 175 | |
176 | - # QUARTERROUND( x0, x5, x10, x15) |
|
177 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
176 | + # QUARTERROUND( x0, x5, x10, x15) |
|
177 | + list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
178 | 178 | |
179 | - # QUARTERROUND( x1, x6, x11, x12) |
|
180 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
179 | + # QUARTERROUND( x1, x6, x11, x12) |
|
180 | + list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
181 | 181 | |
182 | - # QUARTERROUND( x2, x7, x8, x13) |
|
183 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
182 | + # QUARTERROUND( x2, x7, x8, x13) |
|
183 | + list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
184 | 184 | |
185 | - # QUARTERROUND( x3, x4, x9, x14) |
|
186 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
187 | - } |
|
188 | - /* |
|
185 | + # QUARTERROUND( x3, x4, x9, x14) |
|
186 | + list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
187 | + } |
|
188 | + /* |
|
189 | 189 | x0 = PLUS(x0, j0); |
190 | 190 | x1 = PLUS(x1, j1); |
191 | 191 | x2 = PLUS(x2, j2); |
@@ -203,24 +203,24 @@ discard block |
||
203 | 203 | x14 = PLUS(x14, j14); |
204 | 204 | x15 = PLUS(x15, j15); |
205 | 205 | */ |
206 | - $x0 = $x0->addInt32($j0); |
|
207 | - $x1 = $x1->addInt32($j1); |
|
208 | - $x2 = $x2->addInt32($j2); |
|
209 | - $x3 = $x3->addInt32($j3); |
|
210 | - $x4 = $x4->addInt32($j4); |
|
211 | - $x5 = $x5->addInt32($j5); |
|
212 | - $x6 = $x6->addInt32($j6); |
|
213 | - $x7 = $x7->addInt32($j7); |
|
214 | - $x8 = $x8->addInt32($j8); |
|
215 | - $x9 = $x9->addInt32($j9); |
|
216 | - $x10 = $x10->addInt32($j10); |
|
217 | - $x11 = $x11->addInt32($j11); |
|
218 | - $x12 = $x12->addInt32($j12); |
|
219 | - $x13 = $x13->addInt32($j13); |
|
220 | - $x14 = $x14->addInt32($j14); |
|
221 | - $x15 = $x15->addInt32($j15); |
|
206 | + $x0 = $x0->addInt32($j0); |
|
207 | + $x1 = $x1->addInt32($j1); |
|
208 | + $x2 = $x2->addInt32($j2); |
|
209 | + $x3 = $x3->addInt32($j3); |
|
210 | + $x4 = $x4->addInt32($j4); |
|
211 | + $x5 = $x5->addInt32($j5); |
|
212 | + $x6 = $x6->addInt32($j6); |
|
213 | + $x7 = $x7->addInt32($j7); |
|
214 | + $x8 = $x8->addInt32($j8); |
|
215 | + $x9 = $x9->addInt32($j9); |
|
216 | + $x10 = $x10->addInt32($j10); |
|
217 | + $x11 = $x11->addInt32($j11); |
|
218 | + $x12 = $x12->addInt32($j12); |
|
219 | + $x13 = $x13->addInt32($j13); |
|
220 | + $x14 = $x14->addInt32($j14); |
|
221 | + $x15 = $x15->addInt32($j15); |
|
222 | 222 | |
223 | - /* |
|
223 | + /* |
|
224 | 224 | x0 = XOR(x0, LOAD32_LE(m + 0)); |
225 | 225 | x1 = XOR(x1, LOAD32_LE(m + 4)); |
226 | 226 | x2 = XOR(x2, LOAD32_LE(m + 8)); |
@@ -238,36 +238,36 @@ discard block |
||
238 | 238 | x14 = XOR(x14, LOAD32_LE(m + 56)); |
239 | 239 | x15 = XOR(x15, LOAD32_LE(m + 60)); |
240 | 240 | */ |
241 | - $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
242 | - $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
243 | - $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
244 | - $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
245 | - $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
246 | - $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
247 | - $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
248 | - $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
249 | - $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
250 | - $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
251 | - $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
252 | - $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
253 | - $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
254 | - $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
255 | - $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
256 | - $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
241 | + $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
242 | + $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
243 | + $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
244 | + $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
245 | + $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
246 | + $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
247 | + $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
248 | + $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
249 | + $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
250 | + $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
251 | + $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
252 | + $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
253 | + $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
254 | + $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
255 | + $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
256 | + $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
257 | 257 | |
258 | - /* |
|
258 | + /* |
|
259 | 259 | j12 = PLUSONE(j12); |
260 | 260 | if (!j12) { |
261 | 261 | j13 = PLUSONE(j13); |
262 | 262 | } |
263 | 263 | */ |
264 | - /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
265 | - $j12 = $j12->addInt(1); |
|
266 | - if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
267 | - $j13 = $j13->addInt(1); |
|
268 | - } |
|
264 | + /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
265 | + $j12 = $j12->addInt(1); |
|
266 | + if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
267 | + $j13 = $j13->addInt(1); |
|
268 | + } |
|
269 | 269 | |
270 | - /* |
|
270 | + /* |
|
271 | 271 | STORE32_LE(c + 0, x0); |
272 | 272 | STORE32_LE(c + 4, x1); |
273 | 273 | STORE32_LE(c + 8, x2); |
@@ -286,115 +286,115 @@ discard block |
||
286 | 286 | STORE32_LE(c + 60, x15); |
287 | 287 | */ |
288 | 288 | |
289 | - $block = $x0->toReverseString() . |
|
290 | - $x1->toReverseString() . |
|
291 | - $x2->toReverseString() . |
|
292 | - $x3->toReverseString() . |
|
293 | - $x4->toReverseString() . |
|
294 | - $x5->toReverseString() . |
|
295 | - $x6->toReverseString() . |
|
296 | - $x7->toReverseString() . |
|
297 | - $x8->toReverseString() . |
|
298 | - $x9->toReverseString() . |
|
299 | - $x10->toReverseString() . |
|
300 | - $x11->toReverseString() . |
|
301 | - $x12->toReverseString() . |
|
302 | - $x13->toReverseString() . |
|
303 | - $x14->toReverseString() . |
|
304 | - $x15->toReverseString(); |
|
289 | + $block = $x0->toReverseString() . |
|
290 | + $x1->toReverseString() . |
|
291 | + $x2->toReverseString() . |
|
292 | + $x3->toReverseString() . |
|
293 | + $x4->toReverseString() . |
|
294 | + $x5->toReverseString() . |
|
295 | + $x6->toReverseString() . |
|
296 | + $x7->toReverseString() . |
|
297 | + $x8->toReverseString() . |
|
298 | + $x9->toReverseString() . |
|
299 | + $x10->toReverseString() . |
|
300 | + $x11->toReverseString() . |
|
301 | + $x12->toReverseString() . |
|
302 | + $x13->toReverseString() . |
|
303 | + $x14->toReverseString() . |
|
304 | + $x15->toReverseString(); |
|
305 | 305 | |
306 | - /* Partial block */ |
|
307 | - if ($bytes < 64) { |
|
308 | - $c .= self::substr($block, 0, $bytes); |
|
309 | - break; |
|
310 | - } |
|
306 | + /* Partial block */ |
|
307 | + if ($bytes < 64) { |
|
308 | + $c .= self::substr($block, 0, $bytes); |
|
309 | + break; |
|
310 | + } |
|
311 | 311 | |
312 | - /* Full block */ |
|
313 | - $c .= $block; |
|
314 | - $bytes -= 64; |
|
315 | - if ($bytes <= 0) { |
|
316 | - break; |
|
317 | - } |
|
318 | - $message = self::substr($message, 64); |
|
319 | - } |
|
320 | - /* end for(;;) loop */ |
|
312 | + /* Full block */ |
|
313 | + $c .= $block; |
|
314 | + $bytes -= 64; |
|
315 | + if ($bytes <= 0) { |
|
316 | + break; |
|
317 | + } |
|
318 | + $message = self::substr($message, 64); |
|
319 | + } |
|
320 | + /* end for(;;) loop */ |
|
321 | 321 | |
322 | - $ctx[12] = $j12; |
|
323 | - $ctx[13] = $j13; |
|
324 | - return $c; |
|
325 | - } |
|
322 | + $ctx[12] = $j12; |
|
323 | + $ctx[13] = $j13; |
|
324 | + return $c; |
|
325 | + } |
|
326 | 326 | |
327 | - /** |
|
328 | - * @internal You should not use this directly from another application |
|
329 | - * |
|
330 | - * @param int $len |
|
331 | - * @param string $nonce |
|
332 | - * @param string $key |
|
333 | - * @return string |
|
334 | - * @throws SodiumException |
|
335 | - * @throws TypeError |
|
336 | - */ |
|
337 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
338 | - { |
|
339 | - return self::encryptBytes( |
|
340 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
341 | - str_repeat("\x00", $len) |
|
342 | - ); |
|
343 | - } |
|
327 | + /** |
|
328 | + * @internal You should not use this directly from another application |
|
329 | + * |
|
330 | + * @param int $len |
|
331 | + * @param string $nonce |
|
332 | + * @param string $key |
|
333 | + * @return string |
|
334 | + * @throws SodiumException |
|
335 | + * @throws TypeError |
|
336 | + */ |
|
337 | + public static function stream($len = 64, $nonce = '', $key = '') |
|
338 | + { |
|
339 | + return self::encryptBytes( |
|
340 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
341 | + str_repeat("\x00", $len) |
|
342 | + ); |
|
343 | + } |
|
344 | 344 | |
345 | - /** |
|
346 | - * @internal You should not use this directly from another application |
|
347 | - * |
|
348 | - * @param int $len |
|
349 | - * @param string $nonce |
|
350 | - * @param string $key |
|
351 | - * @return string |
|
352 | - * @throws SodiumException |
|
353 | - * @throws TypeError |
|
354 | - */ |
|
355 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
356 | - { |
|
357 | - return self::encryptBytes( |
|
358 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
359 | - str_repeat("\x00", $len) |
|
360 | - ); |
|
361 | - } |
|
345 | + /** |
|
346 | + * @internal You should not use this directly from another application |
|
347 | + * |
|
348 | + * @param int $len |
|
349 | + * @param string $nonce |
|
350 | + * @param string $key |
|
351 | + * @return string |
|
352 | + * @throws SodiumException |
|
353 | + * @throws TypeError |
|
354 | + */ |
|
355 | + public static function ietfStream($len, $nonce = '', $key = '') |
|
356 | + { |
|
357 | + return self::encryptBytes( |
|
358 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
359 | + str_repeat("\x00", $len) |
|
360 | + ); |
|
361 | + } |
|
362 | 362 | |
363 | - /** |
|
364 | - * @internal You should not use this directly from another application |
|
365 | - * |
|
366 | - * @param string $message |
|
367 | - * @param string $nonce |
|
368 | - * @param string $key |
|
369 | - * @param string $ic |
|
370 | - * @return string |
|
371 | - * @throws SodiumException |
|
372 | - * @throws TypeError |
|
373 | - */ |
|
374 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
375 | - { |
|
376 | - return self::encryptBytes( |
|
377 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
378 | - $message |
|
379 | - ); |
|
380 | - } |
|
363 | + /** |
|
364 | + * @internal You should not use this directly from another application |
|
365 | + * |
|
366 | + * @param string $message |
|
367 | + * @param string $nonce |
|
368 | + * @param string $key |
|
369 | + * @param string $ic |
|
370 | + * @return string |
|
371 | + * @throws SodiumException |
|
372 | + * @throws TypeError |
|
373 | + */ |
|
374 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
375 | + { |
|
376 | + return self::encryptBytes( |
|
377 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
378 | + $message |
|
379 | + ); |
|
380 | + } |
|
381 | 381 | |
382 | - /** |
|
383 | - * @internal You should not use this directly from another application |
|
384 | - * |
|
385 | - * @param string $message |
|
386 | - * @param string $nonce |
|
387 | - * @param string $key |
|
388 | - * @param string $ic |
|
389 | - * @return string |
|
390 | - * @throws SodiumException |
|
391 | - * @throws TypeError |
|
392 | - */ |
|
393 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
394 | - { |
|
395 | - return self::encryptBytes( |
|
396 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
397 | - $message |
|
398 | - ); |
|
399 | - } |
|
382 | + /** |
|
383 | + * @internal You should not use this directly from another application |
|
384 | + * |
|
385 | + * @param string $message |
|
386 | + * @param string $nonce |
|
387 | + * @param string $key |
|
388 | + * @param string $ic |
|
389 | + * @return string |
|
390 | + * @throws SodiumException |
|
391 | + * @throws TypeError |
|
392 | + */ |
|
393 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
394 | + { |
|
395 | + return self::encryptBytes( |
|
396 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
397 | + $message |
|
398 | + ); |
|
399 | + } |
|
400 | 400 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core32_ChaCha20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core32_ChaCha20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -34,22 +34,22 @@ discard block |
||
34 | 34 | /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
35 | 35 | |
36 | 36 | # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
37 | - $a = $a->addInt32($b); |
|
38 | - $d = $d->xorInt32($a)->rotateLeft(16); |
|
37 | + $a = $a->addInt32( $b ); |
|
38 | + $d = $d->xorInt32( $a )->rotateLeft( 16 ); |
|
39 | 39 | |
40 | 40 | # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
41 | - $c = $c->addInt32($d); |
|
42 | - $b = $b->xorInt32($c)->rotateLeft(12); |
|
41 | + $c = $c->addInt32( $d ); |
|
42 | + $b = $b->xorInt32( $c )->rotateLeft( 12 ); |
|
43 | 43 | |
44 | 44 | # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
45 | - $a = $a->addInt32($b); |
|
46 | - $d = $d->xorInt32($a)->rotateLeft(8); |
|
45 | + $a = $a->addInt32( $b ); |
|
46 | + $d = $d->xorInt32( $a )->rotateLeft( 8 ); |
|
47 | 47 | |
48 | 48 | # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
49 | - $c = $c->addInt32($d); |
|
50 | - $b = $b->xorInt32($c)->rotateLeft(7); |
|
49 | + $c = $c->addInt32( $d ); |
|
50 | + $b = $b->xorInt32( $c )->rotateLeft( 7 ); |
|
51 | 51 | |
52 | - return array($a, $b, $c, $d); |
|
52 | + return array( $a, $b, $c, $d ); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
67 | 67 | $message = '' |
68 | 68 | ) { |
69 | - $bytes = self::strlen($message); |
|
69 | + $bytes = self::strlen( $message ); |
|
70 | 70 | |
71 | 71 | /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
72 | 72 | /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
@@ -104,54 +104,54 @@ discard block |
||
104 | 104 | j15 = ctx->input[15]; |
105 | 105 | */ |
106 | 106 | /** @var ParagonIE_Sodium_Core32_Int32 $j0 */ |
107 | - $j0 = $ctx[0]; |
|
107 | + $j0 = $ctx[ 0 ]; |
|
108 | 108 | /** @var ParagonIE_Sodium_Core32_Int32 $j1 */ |
109 | - $j1 = $ctx[1]; |
|
109 | + $j1 = $ctx[ 1 ]; |
|
110 | 110 | /** @var ParagonIE_Sodium_Core32_Int32 $j2 */ |
111 | - $j2 = $ctx[2]; |
|
111 | + $j2 = $ctx[ 2 ]; |
|
112 | 112 | /** @var ParagonIE_Sodium_Core32_Int32 $j3 */ |
113 | - $j3 = $ctx[3]; |
|
113 | + $j3 = $ctx[ 3 ]; |
|
114 | 114 | /** @var ParagonIE_Sodium_Core32_Int32 $j4 */ |
115 | - $j4 = $ctx[4]; |
|
115 | + $j4 = $ctx[ 4 ]; |
|
116 | 116 | /** @var ParagonIE_Sodium_Core32_Int32 $j5 */ |
117 | - $j5 = $ctx[5]; |
|
117 | + $j5 = $ctx[ 5 ]; |
|
118 | 118 | /** @var ParagonIE_Sodium_Core32_Int32 $j6 */ |
119 | - $j6 = $ctx[6]; |
|
119 | + $j6 = $ctx[ 6 ]; |
|
120 | 120 | /** @var ParagonIE_Sodium_Core32_Int32 $j7 */ |
121 | - $j7 = $ctx[7]; |
|
121 | + $j7 = $ctx[ 7 ]; |
|
122 | 122 | /** @var ParagonIE_Sodium_Core32_Int32 $j8 */ |
123 | - $j8 = $ctx[8]; |
|
123 | + $j8 = $ctx[ 8 ]; |
|
124 | 124 | /** @var ParagonIE_Sodium_Core32_Int32 $j9 */ |
125 | - $j9 = $ctx[9]; |
|
125 | + $j9 = $ctx[ 9 ]; |
|
126 | 126 | /** @var ParagonIE_Sodium_Core32_Int32 $j10 */ |
127 | - $j10 = $ctx[10]; |
|
127 | + $j10 = $ctx[ 10 ]; |
|
128 | 128 | /** @var ParagonIE_Sodium_Core32_Int32 $j11 */ |
129 | - $j11 = $ctx[11]; |
|
129 | + $j11 = $ctx[ 11 ]; |
|
130 | 130 | /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
131 | - $j12 = $ctx[12]; |
|
131 | + $j12 = $ctx[ 12 ]; |
|
132 | 132 | /** @var ParagonIE_Sodium_Core32_Int32 $j13 */ |
133 | - $j13 = $ctx[13]; |
|
133 | + $j13 = $ctx[ 13 ]; |
|
134 | 134 | /** @var ParagonIE_Sodium_Core32_Int32 $j14 */ |
135 | - $j14 = $ctx[14]; |
|
135 | + $j14 = $ctx[ 14 ]; |
|
136 | 136 | /** @var ParagonIE_Sodium_Core32_Int32 $j15 */ |
137 | - $j15 = $ctx[15]; |
|
137 | + $j15 = $ctx[ 15 ]; |
|
138 | 138 | |
139 | 139 | $c = ''; |
140 | - for (;;) { |
|
141 | - if ($bytes < 64) { |
|
142 | - $message .= str_repeat("\x00", 64 - $bytes); |
|
140 | + for ( ;; ) { |
|
141 | + if ( $bytes < 64 ) { |
|
142 | + $message .= str_repeat( "\x00", 64 - $bytes ); |
|
143 | 143 | } |
144 | 144 | |
145 | - $x0 = clone $j0; |
|
146 | - $x1 = clone $j1; |
|
147 | - $x2 = clone $j2; |
|
148 | - $x3 = clone $j3; |
|
149 | - $x4 = clone $j4; |
|
150 | - $x5 = clone $j5; |
|
151 | - $x6 = clone $j6; |
|
152 | - $x7 = clone $j7; |
|
153 | - $x8 = clone $j8; |
|
154 | - $x9 = clone $j9; |
|
145 | + $x0 = clone $j0; |
|
146 | + $x1 = clone $j1; |
|
147 | + $x2 = clone $j2; |
|
148 | + $x3 = clone $j3; |
|
149 | + $x4 = clone $j4; |
|
150 | + $x5 = clone $j5; |
|
151 | + $x6 = clone $j6; |
|
152 | + $x7 = clone $j7; |
|
153 | + $x8 = clone $j8; |
|
154 | + $x9 = clone $j9; |
|
155 | 155 | $x10 = clone $j10; |
156 | 156 | $x11 = clone $j11; |
157 | 157 | $x12 = clone $j12; |
@@ -160,30 +160,30 @@ discard block |
||
160 | 160 | $x15 = clone $j15; |
161 | 161 | |
162 | 162 | # for (i = 20; i > 0; i -= 2) { |
163 | - for ($i = 20; $i > 0; $i -= 2) { |
|
163 | + for ( $i = 20; $i > 0; $i -= 2 ) { |
|
164 | 164 | # QUARTERROUND( x0, x4, x8, x12) |
165 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
165 | + list( $x0, $x4, $x8, $x12 ) = self::quarterRound( $x0, $x4, $x8, $x12 ); |
|
166 | 166 | |
167 | 167 | # QUARTERROUND( x1, x5, x9, x13) |
168 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
168 | + list( $x1, $x5, $x9, $x13 ) = self::quarterRound( $x1, $x5, $x9, $x13 ); |
|
169 | 169 | |
170 | 170 | # QUARTERROUND( x2, x6, x10, x14) |
171 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
171 | + list( $x2, $x6, $x10, $x14 ) = self::quarterRound( $x2, $x6, $x10, $x14 ); |
|
172 | 172 | |
173 | 173 | # QUARTERROUND( x3, x7, x11, x15) |
174 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
174 | + list( $x3, $x7, $x11, $x15 ) = self::quarterRound( $x3, $x7, $x11, $x15 ); |
|
175 | 175 | |
176 | 176 | # QUARTERROUND( x0, x5, x10, x15) |
177 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
177 | + list( $x0, $x5, $x10, $x15 ) = self::quarterRound( $x0, $x5, $x10, $x15 ); |
|
178 | 178 | |
179 | 179 | # QUARTERROUND( x1, x6, x11, x12) |
180 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
180 | + list( $x1, $x6, $x11, $x12 ) = self::quarterRound( $x1, $x6, $x11, $x12 ); |
|
181 | 181 | |
182 | 182 | # QUARTERROUND( x2, x7, x8, x13) |
183 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
183 | + list( $x2, $x7, $x8, $x13 ) = self::quarterRound( $x2, $x7, $x8, $x13 ); |
|
184 | 184 | |
185 | 185 | # QUARTERROUND( x3, x4, x9, x14) |
186 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
186 | + list( $x3, $x4, $x9, $x14 ) = self::quarterRound( $x3, $x4, $x9, $x14 ); |
|
187 | 187 | } |
188 | 188 | /* |
189 | 189 | x0 = PLUS(x0, j0); |
@@ -203,22 +203,22 @@ discard block |
||
203 | 203 | x14 = PLUS(x14, j14); |
204 | 204 | x15 = PLUS(x15, j15); |
205 | 205 | */ |
206 | - $x0 = $x0->addInt32($j0); |
|
207 | - $x1 = $x1->addInt32($j1); |
|
208 | - $x2 = $x2->addInt32($j2); |
|
209 | - $x3 = $x3->addInt32($j3); |
|
210 | - $x4 = $x4->addInt32($j4); |
|
211 | - $x5 = $x5->addInt32($j5); |
|
212 | - $x6 = $x6->addInt32($j6); |
|
213 | - $x7 = $x7->addInt32($j7); |
|
214 | - $x8 = $x8->addInt32($j8); |
|
215 | - $x9 = $x9->addInt32($j9); |
|
216 | - $x10 = $x10->addInt32($j10); |
|
217 | - $x11 = $x11->addInt32($j11); |
|
218 | - $x12 = $x12->addInt32($j12); |
|
219 | - $x13 = $x13->addInt32($j13); |
|
220 | - $x14 = $x14->addInt32($j14); |
|
221 | - $x15 = $x15->addInt32($j15); |
|
206 | + $x0 = $x0->addInt32( $j0 ); |
|
207 | + $x1 = $x1->addInt32( $j1 ); |
|
208 | + $x2 = $x2->addInt32( $j2 ); |
|
209 | + $x3 = $x3->addInt32( $j3 ); |
|
210 | + $x4 = $x4->addInt32( $j4 ); |
|
211 | + $x5 = $x5->addInt32( $j5 ); |
|
212 | + $x6 = $x6->addInt32( $j6 ); |
|
213 | + $x7 = $x7->addInt32( $j7 ); |
|
214 | + $x8 = $x8->addInt32( $j8 ); |
|
215 | + $x9 = $x9->addInt32( $j9 ); |
|
216 | + $x10 = $x10->addInt32( $j10 ); |
|
217 | + $x11 = $x11->addInt32( $j11 ); |
|
218 | + $x12 = $x12->addInt32( $j12 ); |
|
219 | + $x13 = $x13->addInt32( $j13 ); |
|
220 | + $x14 = $x14->addInt32( $j14 ); |
|
221 | + $x15 = $x15->addInt32( $j15 ); |
|
222 | 222 | |
223 | 223 | /* |
224 | 224 | x0 = XOR(x0, LOAD32_LE(m + 0)); |
@@ -238,22 +238,22 @@ discard block |
||
238 | 238 | x14 = XOR(x14, LOAD32_LE(m + 56)); |
239 | 239 | x15 = XOR(x15, LOAD32_LE(m + 60)); |
240 | 240 | */ |
241 | - $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
242 | - $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
243 | - $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
244 | - $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
245 | - $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
246 | - $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
247 | - $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
248 | - $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
249 | - $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
250 | - $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
251 | - $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
252 | - $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
253 | - $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
254 | - $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
255 | - $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
256 | - $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
241 | + $x0 = $x0->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 0, 4 ) ) ); |
|
242 | + $x1 = $x1->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 4, 4 ) ) ); |
|
243 | + $x2 = $x2->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 8, 4 ) ) ); |
|
244 | + $x3 = $x3->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 12, 4 ) ) ); |
|
245 | + $x4 = $x4->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 16, 4 ) ) ); |
|
246 | + $x5 = $x5->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 20, 4 ) ) ); |
|
247 | + $x6 = $x6->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 24, 4 ) ) ); |
|
248 | + $x7 = $x7->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 28, 4 ) ) ); |
|
249 | + $x8 = $x8->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 32, 4 ) ) ); |
|
250 | + $x9 = $x9->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 36, 4 ) ) ); |
|
251 | + $x10 = $x10->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 40, 4 ) ) ); |
|
252 | + $x11 = $x11->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 44, 4 ) ) ); |
|
253 | + $x12 = $x12->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 48, 4 ) ) ); |
|
254 | + $x13 = $x13->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 52, 4 ) ) ); |
|
255 | + $x14 = $x14->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 56, 4 ) ) ); |
|
256 | + $x15 = $x15->xorInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString( self::substr( $message, 60, 4 ) ) ); |
|
257 | 257 | |
258 | 258 | /* |
259 | 259 | j12 = PLUSONE(j12); |
@@ -262,9 +262,9 @@ discard block |
||
262 | 262 | } |
263 | 263 | */ |
264 | 264 | /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
265 | - $j12 = $j12->addInt(1); |
|
266 | - if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
267 | - $j13 = $j13->addInt(1); |
|
265 | + $j12 = $j12->addInt( 1 ); |
|
266 | + if ( $j12->limbs[ 0 ] === 0 && $j12->limbs[ 1 ] === 0 ) { |
|
267 | + $j13 = $j13->addInt( 1 ); |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | /* |
@@ -304,23 +304,23 @@ discard block |
||
304 | 304 | $x15->toReverseString(); |
305 | 305 | |
306 | 306 | /* Partial block */ |
307 | - if ($bytes < 64) { |
|
308 | - $c .= self::substr($block, 0, $bytes); |
|
307 | + if ( $bytes < 64 ) { |
|
308 | + $c .= self::substr( $block, 0, $bytes ); |
|
309 | 309 | break; |
310 | 310 | } |
311 | 311 | |
312 | 312 | /* Full block */ |
313 | 313 | $c .= $block; |
314 | 314 | $bytes -= 64; |
315 | - if ($bytes <= 0) { |
|
315 | + if ( $bytes <= 0 ) { |
|
316 | 316 | break; |
317 | 317 | } |
318 | - $message = self::substr($message, 64); |
|
318 | + $message = self::substr( $message, 64 ); |
|
319 | 319 | } |
320 | 320 | /* end for(;;) loop */ |
321 | 321 | |
322 | - $ctx[12] = $j12; |
|
323 | - $ctx[13] = $j13; |
|
322 | + $ctx[ 12 ] = $j12; |
|
323 | + $ctx[ 13 ] = $j13; |
|
324 | 324 | return $c; |
325 | 325 | } |
326 | 326 | |
@@ -334,11 +334,11 @@ discard block |
||
334 | 334 | * @throws SodiumException |
335 | 335 | * @throws TypeError |
336 | 336 | */ |
337 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
337 | + public static function stream( $len = 64, $nonce = '', $key = '' ) |
|
338 | 338 | { |
339 | 339 | return self::encryptBytes( |
340 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
341 | - str_repeat("\x00", $len) |
|
340 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx( $key, $nonce ), |
|
341 | + str_repeat( "\x00", $len ) |
|
342 | 342 | ); |
343 | 343 | } |
344 | 344 | |
@@ -352,11 +352,11 @@ discard block |
||
352 | 352 | * @throws SodiumException |
353 | 353 | * @throws TypeError |
354 | 354 | */ |
355 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
355 | + public static function ietfStream( $len, $nonce = '', $key = '' ) |
|
356 | 356 | { |
357 | 357 | return self::encryptBytes( |
358 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
359 | - str_repeat("\x00", $len) |
|
358 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx( $key, $nonce ), |
|
359 | + str_repeat( "\x00", $len ) |
|
360 | 360 | ); |
361 | 361 | } |
362 | 362 | |
@@ -371,10 +371,10 @@ discard block |
||
371 | 371 | * @throws SodiumException |
372 | 372 | * @throws TypeError |
373 | 373 | */ |
374 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
374 | + public static function ietfStreamXorIc( $message, $nonce = '', $key = '', $ic = '' ) |
|
375 | 375 | { |
376 | 376 | return self::encryptBytes( |
377 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
377 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx( $key, $nonce, $ic ), |
|
378 | 378 | $message |
379 | 379 | ); |
380 | 380 | } |
@@ -390,10 +390,10 @@ discard block |
||
390 | 390 | * @throws SodiumException |
391 | 391 | * @throws TypeError |
392 | 392 | */ |
393 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
393 | + public static function streamXorIc( $message, $nonce = '', $key = '', $ic = '' ) |
|
394 | 394 | { |
395 | 395 | return self::encryptBytes( |
396 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
396 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx( $key, $nonce, $ic ), |
|
397 | 397 | $message |
398 | 398 | ); |
399 | 399 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core32_ChaCha20 |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util { |
|
12 | 11 | /** |
13 | 12 | * The ChaCha20 quarter round function. Works on four 32-bit integers. |
14 | 13 | * |
@@ -334,8 +333,7 @@ discard block |
||
334 | 333 | * @throws SodiumException |
335 | 334 | * @throws TypeError |
336 | 335 | */ |
337 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
338 | - { |
|
336 | + public static function stream($len = 64, $nonce = '', $key = '') { |
|
339 | 337 | return self::encryptBytes( |
340 | 338 | new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
341 | 339 | str_repeat("\x00", $len) |
@@ -352,8 +350,7 @@ discard block |
||
352 | 350 | * @throws SodiumException |
353 | 351 | * @throws TypeError |
354 | 352 | */ |
355 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
356 | - { |
|
353 | + public static function ietfStream($len, $nonce = '', $key = '') { |
|
357 | 354 | return self::encryptBytes( |
358 | 355 | new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
359 | 356 | str_repeat("\x00", $len) |
@@ -371,8 +368,7 @@ discard block |
||
371 | 368 | * @throws SodiumException |
372 | 369 | * @throws TypeError |
373 | 370 | */ |
374 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
375 | - { |
|
371 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') { |
|
376 | 372 | return self::encryptBytes( |
377 | 373 | new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
378 | 374 | $message |
@@ -390,8 +386,7 @@ discard block |
||
390 | 386 | * @throws SodiumException |
391 | 387 | * @throws TypeError |
392 | 388 | */ |
393 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
394 | - { |
|
389 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') { |
|
395 | 390 | return self::encryptBytes( |
396 | 391 | new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
397 | 392 | $message |