Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 /**
5 * Class ParagonIE_Sodium_Core_Ristretto255
6 */
7 class ParagonIE_Sodium_Core_Ristretto255 extends ParagonIE_Sodium_Core_Ed25519
8 {
9 const crypto_core_ristretto255_HASHBYTES = 64;
10 const HASH_SC_L = 48;
11 const CORE_H2C_SHA256 = 1;
12 const CORE_H2C_SHA512 = 2;
13
14 /**
15 * @param ParagonIE_Sodium_Core_Curve25519_Fe $f
16 * @param int $b
17 * @return ParagonIE_Sodium_Core_Curve25519_Fe
18 */
19 public static function fe_cneg(
20 ParagonIE_Sodium_Core_Curve25519_Fe $f,
21 int $b
22 ): ParagonIE_Sodium_Core_Curve25519_Fe {
23 $negf = self::fe_neg($f);
24 return self::fe_cmov($f, $negf, $b);
25 }
26
27 /**
28 * @param ParagonIE_Sodium_Core_Curve25519_Fe $f
29 * @return ParagonIE_Sodium_Core_Curve25519_Fe
30 * @throws SodiumException
31 */
32 public static function fe_abs(
33 ParagonIE_Sodium_Core_Curve25519_Fe $f
34 ): ParagonIE_Sodium_Core_Curve25519_Fe {
35 return self::fe_cneg($f, self::fe_isnegative($f));
36 }
37
38 /**
39 * Returns 0 if this field element results in all NUL bytes.
40 *
41 * @internal You should not use this directly from another application
42 *
43 * @param ParagonIE_Sodium_Core_Curve25519_Fe $f
44 * @return int
45 * @throws SodiumException
46 */
47 public static function fe_iszero(ParagonIE_Sodium_Core_Curve25519_Fe $f): int
48 {
49 static $zero;
50 if ($zero === null) {
51 $zero = str_repeat("\x00", 32);
52 }
53 /** @var string $zero */
54 $str = self::fe_tobytes($f);
55
56 $d = 0;
57 for ($i = 0; $i < 32; ++$i) {
58 $d |= self::chrToInt($str[$i]);
59 }
60 return (($d - 1) >> 31) & 1;
61 }
62
63
64 /**
65 * @param ParagonIE_Sodium_Core_Curve25519_Fe $u
66 * @param ParagonIE_Sodium_Core_Curve25519_Fe $v
67 * @return array{x: ParagonIE_Sodium_Core_Curve25519_Fe, nonsquare: int}
68 *
69 * @throws SodiumException
70 */
71 public static function ristretto255_sqrt_ratio_m1(
72 ParagonIE_Sodium_Core_Curve25519_Fe $u,
73 ParagonIE_Sodium_Core_Curve25519_Fe $v
74 ): array {
75 $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::SQRTM1);
76
77 $v3 = self::fe_mul(
78 self::fe_sq($v),
79 $v
80 ); /* v3 = v^3 */
81 $x = self::fe_mul(
82 self::fe_mul(
83 self::fe_sq($v3),
84 $u
85 ),
86 $v
87 ); /* x = uv^7 */
88
89 $x = self::fe_mul(
90 self::fe_mul(
91 self::fe_pow22523($x), /* x = (uv^7)^((q-5)/8) */
92 $v3
93 ),
94 $u
95 ); /* x = uv^3(uv^7)^((q-5)/8) */
96
97 $vxx = self::fe_mul(
98 self::fe_sq($x),
99 $v
100 ); /* vx^2 */
101
102 $m_root_check = self::fe_sub($vxx, $u); /* vx^2-u */
103 $p_root_check = self::fe_add($vxx, $u); /* vx^2+u */
104 $f_root_check = self::fe_mul($u, $sqrtm1); /* u*sqrt(-1) */
105 $f_root_check = self::fe_add($vxx, $f_root_check); /* vx^2+u*sqrt(-1) */
106
107 $has_m_root = self::fe_iszero($m_root_check);
108 $has_p_root = self::fe_iszero($p_root_check);
109 $has_f_root = self::fe_iszero($f_root_check);
110
111 $x_sqrtm1 = self::fe_mul($x, $sqrtm1); /* x*sqrt(-1) */
112
113 $x = self::fe_abs(
114 self::fe_cmov($x, $x_sqrtm1, $has_p_root | $has_f_root)
115 );
116 return array(
117 'x' => $x,
118 'nonsquare' => $has_m_root | $has_p_root
119 );
120 }
121
122 /**
123 * @param string $s
124 * @return int
125 * @throws SodiumException
126 */
127 public static function ristretto255_point_is_canonical(
128 #[SensitiveParameter]
129 string $s
130 ): int {
131 $c = (self::chrToInt($s[31]) & 0x7f) ^ 0x7f;
132 for ($i = 30; $i > 0; --$i) {
133 $c |= self::chrToInt($s[$i]) ^ 0xff;
134 }
135 $c = ($c - 1) >> 8;
136 $d = (0xed - 1 - self::chrToInt($s[0])) >> 8;
137 $e = self::chrToInt($s[31]) >> 7;
138
139 return 1 - ((($c & $d) | $e | self::chrToInt($s[0])) & 1);
140 }
141
142 /**
143 * @param string $s
144 * @param bool $skipCanonicalCheck
145 * @return array{h: ParagonIE_Sodium_Core_Curve25519_Ge_P3, res: int}
146 * @throws SodiumException
147 */
148 public static function ristretto255_frombytes(
149 #[SensitiveParameter]
150 string $s,
151 bool $skipCanonicalCheck = false
152 ): array {
153 if (!$skipCanonicalCheck) {
154 if (!self::ristretto255_point_is_canonical($s)) {
155 throw new SodiumException('S is not canonical');
156 }
157 }
158
159 $s_ = self::fe_frombytes($s);
160 $ss = self::fe_sq($s_); /* ss = s^2 */
161
162 $u1 = self::fe_sub(self::fe_1(), $ss); /* u1 = 1-ss */
163 $u1u1 = self::fe_sq($u1); /* u1u1 = u1^2 */
164
165 $u2 = self::fe_add(self::fe_1(), $ss); /* u2 = 1+ss */
166 $u2u2 = self::fe_sq($u2); /* u2u2 = u2^2 */
167
168 $v = self::fe_mul(
169 ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::D),
170 $u1u1
171 ); /* v = d*u1^2 */
172 $v = self::fe_neg($v); /* v = -d*u1^2 */
173 $v = self::fe_sub($v, $u2u2); /* v = -(d*u1^2)-u2^2 */
174 $v_u2u2 = self::fe_mul($v, $u2u2); /* v_u2u2 = v*u2^2 */
175
176 $one = self::fe_1();
177 $result = self::ristretto255_sqrt_ratio_m1($one, $v_u2u2);
178 $inv_sqrt = $result['x'];
179 $notsquare = $result['nonsquare'];
180
181 $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3();
182
183 $h->X = self::fe_mul($inv_sqrt, $u2);
184 $h->Y = self::fe_mul(self::fe_mul($inv_sqrt, $h->X), $v);
185
186 $h->X = self::fe_mul($h->X, $s_);
187 $h->X = self::fe_abs(
188 self::fe_add($h->X, $h->X)
189 );
190 $h->Y = self::fe_mul($u1, $h->Y);
191 $h->Z = self::fe_1();
192 $h->T = self::fe_mul($h->X, $h->Y);
193
194 $res = - ((1 - $notsquare) | self::fe_isnegative($h->T) | self::fe_iszero($h->Y));
195 return array('h' => $h, 'res' => $res);
196 }
197
198 /**
199 * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h
200 * @return string
201 * @throws SodiumException
202 */
203 public static function ristretto255_p3_tobytes(
204 ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h
205 ): string {
206 $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::SQRTM1);
207 $invsqrtamd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::INVSQRTAMD);
208
209 $u1 = self::fe_add($h->Z, $h->Y); /* u1 = Z+Y */
210 $zmy = self::fe_sub($h->Z, $h->Y); /* zmy = Z-Y */
211 $u1 = self::fe_mul($u1, $zmy); /* u1 = (Z+Y)*(Z-Y) */
212 $u2 = self::fe_mul($h->X, $h->Y); /* u2 = X*Y */
213
214 $u1_u2u2 = self::fe_mul(self::fe_sq($u2), $u1); /* u1_u2u2 = u1*u2^2 */
215 $one = self::fe_1();
216
217 $result = self::ristretto255_sqrt_ratio_m1($one, $u1_u2u2);
218 $inv_sqrt = $result['x'];
219
220 $den1 = self::fe_mul($inv_sqrt, $u1); /* den1 = inv_sqrt*u1 */
221 $den2 = self::fe_mul($inv_sqrt, $u2); /* den2 = inv_sqrt*u2 */
222 $z_inv = self::fe_mul($h->T, self::fe_mul($den1, $den2)); /* z_inv = den1*den2*T */
223
224 $ix = self::fe_mul($h->X, $sqrtm1); /* ix = X*sqrt(-1) */
225 $iy = self::fe_mul($h->Y, $sqrtm1); /* iy = Y*sqrt(-1) */
226 $eden = self::fe_mul($den1, $invsqrtamd);
227
228 $t_z_inv = self::fe_mul($h->T, $z_inv); /* t_z_inv = T*z_inv */
229 $rotate = self::fe_isnegative($t_z_inv);
230
231 $x_ = clone $h->X;
232 $y_ = clone $h->Y;
233 $den_inv = clone $den2;
234
235 $x_ = self::fe_cmov($x_, $iy, $rotate);
236 $y_ = self::fe_cmov($y_, $ix, $rotate);
237 $den_inv = self::fe_cmov($den_inv, $eden, $rotate);
238
239 $x_z_inv = self::fe_mul($x_, $z_inv);
240 $y_ = self::fe_cneg($y_, self::fe_isnegative($x_z_inv));
241
242
243 return self::fe_tobytes(
244 self::fe_abs(
245 self::fe_mul(
246 $den_inv,
247 self::fe_sub($h->Z, $y_)
248 )
249 )
250 );
251 }
252
253 /**
254 * @param ParagonIE_Sodium_Core_Curve25519_Fe $t
255 * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3
256 *
257 * @throws SodiumException
258 */
259 public static function ristretto255_elligator(
260 ParagonIE_Sodium_Core_Curve25519_Fe $t
261 ): ParagonIE_Sodium_Core_Curve25519_Ge_P3 {
262 $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::SQRTM1);
263 $onemsqd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::ONEMSQD);
264 $d = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::D);
265 $sqdmone = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::SQDMONE);
266 $sqrtadm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::SQRTADM1);
267
268 $one = self::fe_1();
269 $r = self::fe_mul($sqrtm1, self::fe_sq($t)); /* r = sqrt(-1)*t^2 */
270 $u = self::fe_mul(self::fe_add($r, $one), $onemsqd); /* u = (r+1)*(1-d^2) */
271 $c = self::fe_neg(self::fe_1()); /* c = -1 */
272 $rpd = self::fe_add($r, $d); /* rpd = r+d */
273
274 $v = self::fe_mul(
275 self::fe_sub(
276 $c,
277 self::fe_mul($r, $d)
278 ),
279 $rpd
280 ); /* v = (c-r*d)*(r+d) */
281
282 $result = self::ristretto255_sqrt_ratio_m1($u, $v);
283 $s = $result['x'];
284 $wasnt_square = 1 - $result['nonsquare'];
285
286 $s_prime = self::fe_neg(
287 self::fe_abs(
288 self::fe_mul($s, $t)
289 )
290 ); /* s_prime = -|s*t| */
291 $s = self::fe_cmov($s, $s_prime, $wasnt_square);
292 $c = self::fe_cmov($c, $r, $wasnt_square);
293
294 $n = self::fe_sub(
295 self::fe_mul(
296 self::fe_mul(
297 self::fe_sub($r, $one),
298 $c
299 ),
300 $sqdmone
301 ),
302 $v
303 ); /* n = c*(r-1)*(d-1)^2-v */
304
305 $w0 = self::fe_mul(
306 self::fe_add($s, $s),
307 $v
308 ); /* w0 = 2s*v */
309
310 $w1 = self::fe_mul($n, $sqrtadm1); /* w1 = n*sqrt(ad-1) */
311 $ss = self::fe_sq($s); /* ss = s^2 */
312 $w2 = self::fe_sub($one, $ss); /* w2 = 1-s^2 */
313 $w3 = self::fe_add($one, $ss); /* w3 = 1+s^2 */
314
315 return new ParagonIE_Sodium_Core_Curve25519_Ge_P3(
316 self::fe_mul($w0, $w3),
317 self::fe_mul($w2, $w1),
318 self::fe_mul($w1, $w3),
319 self::fe_mul($w0, $w2)
320 );
321 }
322
323 /**
324 * @param string $h
325 * @return string
326 * @throws SodiumException
327 */
328 public static function ristretto255_from_hash(
329 #[SensitiveParameter]
330 string $h
331 ): string {
332 if (self::strlen($h) !== 64) {
333 throw new SodiumException('Hash must be 64 bytes');
334 }
335 $r0 = self::fe_frombytes(self::substr($h, 0, 32));
336 $r1 = self::fe_frombytes(self::substr($h, 32, 32));
337
338 $p0 = self::ristretto255_elligator($r0);
339 $p1 = self::ristretto255_elligator($r1);
340
341 $p_p1p1 = self::ge_add(
342 $p0,
343 self::ge_p3_to_cached($p1)
344 );
345 return self::ristretto255_p3_tobytes(
346 self::ge_p1p1_to_p3($p_p1p1)
347 );
348 }
349
350 /**
351 * @param string $p
352 * @return int
353 * @throws SodiumException
354 */
355 public static function is_valid_point(
356 #[SensitiveParameter]
357 string $p
358 ): int {
359 $result = self::ristretto255_frombytes($p);
360 if ($result['res'] !== 0) {
361 return 0;
362 }
363 return 1;
364 }
365
366 /**
367 * @param string $p
368 * @param string $q
369 * @return string
370 * @throws SodiumException
371 */
372 public static function ristretto255_add(
373 string $p,
374 string $q
375 ): string {
376 $p_res = self::ristretto255_frombytes($p);
377 $q_res = self::ristretto255_frombytes($q);
378 if ($p_res['res'] !== 0 || $q_res['res'] !== 0) {
379 throw new SodiumException('Could not add points');
380 }
381 $p_p3 = $p_res['h'];
382 $q_p3 = $q_res['h'];
383 $q_cached = self::ge_p3_to_cached($q_p3);
384 $r_p1p1 = self::ge_add($p_p3, $q_cached);
385 $r_p3 = self::ge_p1p1_to_p3($r_p1p1);
386 return self::ristretto255_p3_tobytes($r_p3);
387 }
388
389 /**
390 * @param string $p
391 * @param string $q
392 * @return string
393 * @throws SodiumException
394 */
395 public static function ristretto255_sub(
396 #[SensitiveParameter]
397 string $p,
398 #[SensitiveParameter]
399 string $q
400 ): string {
401 $p_res = self::ristretto255_frombytes($p);
402 $q_res = self::ristretto255_frombytes($q);
403 if ($p_res['res'] !== 0 || $q_res['res'] !== 0) {
404 throw new SodiumException('Could not add points');
405 }
406 $p_p3 = $p_res['h'];
407 $q_p3 = $q_res['h'];
408 $q_cached = self::ge_p3_to_cached($q_p3);
409 $r_p1p1 = self::ge_sub($p_p3, $q_cached);
410 $r_p3 = self::ge_p1p1_to_p3($r_p1p1);
411 return self::ristretto255_p3_tobytes($r_p3);
412 }
413
414
415 /**
416 * @param int $hLen
417 * @param ?string $ctx
418 * @param string $msg
419 * @return string
420 * @throws SodiumException
421 */
422 protected static function h2c_string_to_hash_sha256(
423 int $hLen,
424 #[SensitiveParameter]
425 ?string $ctx,
426 #[SensitiveParameter]
427 string $msg
428 ): string {
429 $h = array_fill(0, $hLen, 0);
430 if (is_null($ctx)) {
431 $ctx = '';
432 }
433 $ctx_len = self::strlen($ctx);
434 if ($hLen > 0xff) {
435 throw new SodiumException('Hash must be less than 256 bytes');
436 }
437
438 if ($ctx_len > 0xff) {
439 $st = hash_init('sha256');
440 hash_update($st, "H2C-OVERSIZE-DST-");
441 hash_update($st, $ctx);
442 $ctx = hash_final($st, true);
443 $ctx_len = 32;
444 }
445 $t = array(0, $hLen, 0);
446 $ux = str_repeat("\0", 64);
447 $st = hash_init('sha256');
448 hash_update($st, $ux);
449 hash_update($st, $msg);
450 hash_update($st, self::intArrayToString($t));
451 hash_update($st, $ctx);
452 hash_update($st, self::intToChr($ctx_len));
453 $u0 = hash_final($st, true);
454
455 for ($i = 0; $i < $hLen; $i += 64) {
456 $ux = self::xorStrings($ux, $u0);
457 ++$t[2];
458 $st = hash_init('sha256');
459 hash_update($st, $ux);
460 hash_update($st, self::intToChr($t[2]));
461 hash_update($st, $ctx);
462 hash_update($st, self::intToChr($ctx_len));
463 $ux = hash_final($st, true);
464 $amount = min($hLen - $i, 64);
465 for ($j = 0; $j < $amount; ++$j) {
466 $h[$i + $j] = self::chrToInt($ux[$i]);
467 }
468 }
469 return self::intArrayToString(array_slice($h, 0, $hLen));
470 }
471
472 /**
473 * @param int $hLen
474 * @param ?string $ctx
475 * @param string $msg
476 * @return string
477 * @throws SodiumException
478 */
479 protected static function h2c_string_to_hash_sha512(
480 int $hLen,
481 #[SensitiveParameter]
482 ?string $ctx,
483 #[SensitiveParameter]
484 string $msg
485 ): string {
486 $h = array_fill(0, $hLen, 0);
487 if (is_null($ctx)) {
488 $ctx = '';
489 }
490 $ctx_len = self::strlen($ctx);
491 if ($hLen > 0xff) {
492 throw new SodiumException('Hash must be less than 256 bytes');
493 }
494
495 if ($ctx_len > 0xff) {
496 $st = hash_init('sha256');
497 hash_update($st, "H2C-OVERSIZE-DST-");
498 hash_update($st, $ctx);
499 $ctx = hash_final($st, true);
500 $ctx_len = 32;
501 }
502 $t = array(0, $hLen, 0);
503 $ux = str_repeat("\0", 128);
504 $st = hash_init('sha512');
505 hash_update($st, $ux);
506 hash_update($st, $msg);
507 hash_update($st, self::intArrayToString($t));
508 hash_update($st, $ctx);
509 hash_update($st, self::intToChr($ctx_len));
510 $u0 = hash_final($st, true);
511
512 for ($i = 0; $i < $hLen; $i += 128) {
513 $ux = self::xorStrings($ux, $u0);
514 ++$t[2];
515 $st = hash_init('sha512');
516 hash_update($st, $ux);
517 hash_update($st, self::intToChr($t[2]));
518 hash_update($st, $ctx);
519 hash_update($st, self::intToChr($ctx_len));
520 $ux = hash_final($st, true);
521 $amount = min($hLen - $i, 128);
522 for ($j = 0; $j < $amount; ++$j) {
523 $h[$i + $j] = self::chrToInt($ux[$i]);
524 }
525 }
526 return self::intArrayToString(array_slice($h, 0, $hLen));
527 }
528
529 /**
530 * @param int $hLen
531 * @param ?string $ctx
532 * @param string $msg
533 * @param int $hash_alg
534 * @return string
535 * @throws SodiumException
536 */
537 public static function h2c_string_to_hash(
538 int $hLen,
539 #[SensitiveParameter]
540 ?string $ctx,
541 #[SensitiveParameter]
542 string $msg,
543 int $hash_alg
544 ): string {
545 return match ($hash_alg) {
546 self::CORE_H2C_SHA256 => self::h2c_string_to_hash_sha256($hLen, $ctx, $msg),
547 self::CORE_H2C_SHA512 => self::h2c_string_to_hash_sha512($hLen, $ctx, $msg),
548 default => throw new SodiumException('Invalid H2C hash algorithm'),
549 };
550 }
551
552 /**
553 * @param ?string $ctx
554 * @param string $msg
555 * @param int $hash_alg
556 * @return string
557 * @throws SodiumException
558 */
559 protected static function _string_to_element(
560 #[SensitiveParameter]
561 ?string $ctx,
562 #[SensitiveParameter]
563 string $msg,
564 int $hash_alg
565 ): string {
566 return self::ristretto255_from_hash(
567 self::h2c_string_to_hash(self::crypto_core_ristretto255_HASHBYTES, $ctx, $msg, $hash_alg)
568 );
569 }
570
571 /**
572 * @return string
573 * @throws SodiumException
574 * @throws Exception
575 */
576 public static function ristretto255_random(): string
577 {
578 return self::ristretto255_from_hash(
579 ParagonIE_Sodium_Compat::randombytes_buf(self::crypto_core_ristretto255_HASHBYTES)
580 );
581 }
582
583 /**
584 * @return string
585 * @throws SodiumException
586 */
587 public static function ristretto255_scalar_random(): string
588 {
589 return self::scalar_random();
590 }
591
592 /**
593 * @param string $s
594 * @return string
595 * @throws SodiumException
596 */
597 public static function ristretto255_scalar_complement(
598 #[SensitiveParameter]
599 string $s
600 ): string {
601 return self::scalar_complement($s);
602 }
603
604
605 /**
606 * @param string $s
607 * @return string
608 */
609 public static function ristretto255_scalar_invert(
610 #[SensitiveParameter]
611 string $s
612 ): string {
613 return self::sc25519_invert($s);
614 }
615
616 /**
617 * @param string $s
618 * @return string
619 * @throws SodiumException
620 */
621 public static function ristretto255_scalar_negate(
622 #[SensitiveParameter]
623 string $s
624 ): string {
625 return self::scalar_negate($s);
626 }
627
628 /**
629 * @param string $x
630 * @param string $y
631 * @return string
632 *
633 * @throws SodiumException
634 */
635 public static function ristretto255_scalar_add(
636 #[SensitiveParameter]
637 string $x,
638 #[SensitiveParameter]
639 string $y
640 ): string {
641 return self::scalar_add($x, $y);
642 }
643
644 /**
645 * @param string $x
646 * @param string $y
647 * @return string
648 *
649 * @throws SodiumException
650 */
651 public static function ristretto255_scalar_sub(
652 #[SensitiveParameter]
653 string $x,
654 #[SensitiveParameter]
655 string $y
656 ): string {
657 return self::scalar_sub($x, $y);
658 }
659
660 /**
661 * @param string $x
662 * @param string $y
663 * @return string
664 */
665 public static function ristretto255_scalar_mul(
666 #[SensitiveParameter]
667 string $x,
668 #[SensitiveParameter]
669 string $y
670 ): string {
671 return self::sc25519_mul($x, $y);
672 }
673
674 /**
675 * @param string $ctx
676 * @param string $msg
677 * @param int $hash_alg
678 * @return string
679 * @throws SodiumException
680 */
681 public static function ristretto255_scalar_from_string(
682 #[SensitiveParameter]
683 string $ctx,
684 #[SensitiveParameter]
685 string $msg,
686 int $hash_alg
687 ): string {
688 $h = array_fill(0, 64, 0);
689 $h_be = self::stringToIntArray(
690 self::h2c_string_to_hash(
691 self::HASH_SC_L, $ctx, $msg, $hash_alg
692 )
693 );
694
695 for ($i = 0; $i < self::HASH_SC_L; ++$i) {
696 $h[$i] = $h_be[self::HASH_SC_L - 1 - $i];
697 }
698 return self::ristretto255_scalar_reduce(self::intArrayToString($h));
699 }
700
701 /**
702 * @param string $s
703 * @return string
704 */
705 public static function ristretto255_scalar_reduce(
706 #[SensitiveParameter]
707 string $s
708 ): string {
709 return self::sc_reduce($s);
710 }
711
712 /**
713 * @param string $n
714 * @param string $p
715 * @return string
716 * @throws SodiumException
717 */
718 public static function scalarmult_ristretto255(
719 #[SensitiveParameter]
720 string $n,
721 #[SensitiveParameter]
722 string $p
723 ): string {
724 if (self::strlen($n) !== 32) {
725 throw new SodiumException('Scalar must be 32 bytes, ' . self::strlen($p) . ' given.');
726 }
727 if (self::strlen($p) !== 32) {
728 throw new SodiumException('Point must be 32 bytes, ' . self::strlen($p) . ' given.');
729 }
730 $result = self::ristretto255_frombytes($p);
731 if ($result['res'] !== 0) {
732 throw new SodiumException('Could not multiply points');
733 }
734 $P = $result['h'];
735
736 $t = self::stringToIntArray($n);
737 $t[31] &= 0x7f;
738 $Q = self::ge_scalarmult(self::intArrayToString($t), $P);
739 $q = self::ristretto255_p3_tobytes($Q);
740 if (ParagonIE_Sodium_Compat::is_zero($q)) {
741 throw new SodiumException('An unknown error has occurred');
742 }
743 return $q;
744 }
745
746 /**
747 * @param string $n
748 * @return string
749 * @throws SodiumException
750 */
751 public static function scalarmult_ristretto255_base(
752 #[SensitiveParameter]
753 string $n
754 ): string {
755 $t = self::stringToIntArray($n);
756 $t[31] &= 0x7f;
757 $Q = self::ge_scalarmult_base(self::intArrayToString($t));
758 $q = self::ristretto255_p3_tobytes($Q);
759 if (ParagonIE_Sodium_Compat::is_zero($q)) {
760 throw new SodiumException('An unknown error has occurred');
761 }
762 return $q;
763 }
764 }