wp/wp-includes/sodium_compat/src/Core32/Curve25519.php
changeset 19 3d72ae0968f4
parent 9 177826044cd9
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   323         $f[8] = $f[8]->subInt64($carry8->shiftLeft(26));
   323         $f[8] = $f[8]->subInt64($carry8->shiftLeft(26));
   324 
   324 
   325         $carry9 = $f[9]->shiftRight(25);
   325         $carry9 = $f[9]->shiftRight(25);
   326         $f[9] = $f[9]->subInt64($carry9->shiftLeft(25));
   326         $f[9] = $f[9]->subInt64($carry9->shiftLeft(25));
   327 
   327 
   328         /** @var int $h0 */
       
   329         $h0 = $f[0]->toInt32()->toInt();
   328         $h0 = $f[0]->toInt32()->toInt();
   330         /** @var int $h1 */
       
   331         $h1 = $f[1]->toInt32()->toInt();
   329         $h1 = $f[1]->toInt32()->toInt();
   332         /** @var int $h2 */
       
   333         $h2 = $f[2]->toInt32()->toInt();
   330         $h2 = $f[2]->toInt32()->toInt();
   334         /** @var int $h3 */
       
   335         $h3 = $f[3]->toInt32()->toInt();
   331         $h3 = $f[3]->toInt32()->toInt();
   336         /** @var int $h4 */
       
   337         $h4 = $f[4]->toInt32()->toInt();
   332         $h4 = $f[4]->toInt32()->toInt();
   338         /** @var int $h5 */
       
   339         $h5 = $f[5]->toInt32()->toInt();
   333         $h5 = $f[5]->toInt32()->toInt();
   340         /** @var int $h6 */
       
   341         $h6 = $f[6]->toInt32()->toInt();
   334         $h6 = $f[6]->toInt32()->toInt();
   342         /** @var int $h7 */
       
   343         $h7 = $f[7]->toInt32()->toInt();
   335         $h7 = $f[7]->toInt32()->toInt();
   344         /** @var int $h8 */
       
   345         $h8 = $f[8]->toInt32()->toInt();
   336         $h8 = $f[8]->toInt32()->toInt();
   346         /** @var int $h9 */
       
   347         $h9 = $f[9]->toInt32()->toInt();
   337         $h9 = $f[9]->toInt32()->toInt();
   348 
   338 
   349         /**
   339         /**
   350          * @var array<int, int>
   340          * @var array<int, int>
   351          */
   341          */
   416     {
   406     {
   417         static $zero;
   407         static $zero;
   418         if ($zero === null) {
   408         if ($zero === null) {
   419             $zero = str_repeat("\x00", 32);
   409             $zero = str_repeat("\x00", 32);
   420         }
   410         }
   421         /** @var string $str */
       
   422         $str = self::fe_tobytes($f);
   411         $str = self::fe_tobytes($f);
   423         /** @var string $zero */
   412         /** @var string $zero */
   424         return !self::verify_32($str, $zero);
   413         return !self::verify_32($str, $zero);
   425     }
   414     }
   426 
   415 
   495         $g5_19 = $g5->mulInt(19, 5);
   484         $g5_19 = $g5->mulInt(19, 5);
   496         $g6_19 = $g6->mulInt(19, 5);
   485         $g6_19 = $g6->mulInt(19, 5);
   497         $g7_19 = $g7->mulInt(19, 5);
   486         $g7_19 = $g7->mulInt(19, 5);
   498         $g8_19 = $g8->mulInt(19, 5);
   487         $g8_19 = $g8->mulInt(19, 5);
   499         $g9_19 = $g9->mulInt(19, 5);
   488         $g9_19 = $g9->mulInt(19, 5);
   500         /** @var ParagonIE_Sodium_Core32_Int64 $f1_2 */
       
   501         $f1_2 = $f1->shiftLeft(1);
   489         $f1_2 = $f1->shiftLeft(1);
   502         /** @var ParagonIE_Sodium_Core32_Int64 $f3_2 */
       
   503         $f3_2 = $f3->shiftLeft(1);
   490         $f3_2 = $f3->shiftLeft(1);
   504         /** @var ParagonIE_Sodium_Core32_Int64 $f5_2 */
       
   505         $f5_2 = $f5->shiftLeft(1);
   491         $f5_2 = $f5->shiftLeft(1);
   506         /** @var ParagonIE_Sodium_Core32_Int64 $f7_2 */
       
   507         $f7_2 = $f7->shiftLeft(1);
   492         $f7_2 = $f7->shiftLeft(1);
   508         /** @var ParagonIE_Sodium_Core32_Int64 $f9_2 */
       
   509         $f9_2 = $f9->shiftLeft(1);
   493         $f9_2 = $f9->shiftLeft(1);
   510         $f0g0    = $f0->mulInt64($g0, 27);
   494         $f0g0    = $f0->mulInt64($g0, 27);
   511         $f0g1    = $f0->mulInt64($g1, 27);
   495         $f0g1    = $f0->mulInt64($g1, 27);
   512         $f0g2    = $f0->mulInt64($g2, 27);
   496         $f0g2    = $f0->mulInt64($g2, 27);
   513         $f0g3    = $f0->mulInt64($g3, 27);
   497         $f0g3    = $f0->mulInt64($g3, 27);
   773      * @throws TypeError
   757      * @throws TypeError
   774      * @psalm-suppress MixedMethodCall
   758      * @psalm-suppress MixedMethodCall
   775      */
   759      */
   776     public static function fe_sq(ParagonIE_Sodium_Core32_Curve25519_Fe $f)
   760     public static function fe_sq(ParagonIE_Sodium_Core32_Curve25519_Fe $f)
   777     {
   761     {
   778         /** @var ParagonIE_Sodium_Core32_Int64 $f0 */
       
   779         $f0 = $f[0]->toInt64();
   762         $f0 = $f[0]->toInt64();
   780         /** @var ParagonIE_Sodium_Core32_Int64 $f1 */
       
   781         $f1 = $f[1]->toInt64();
   763         $f1 = $f[1]->toInt64();
   782         /** @var ParagonIE_Sodium_Core32_Int64 $f2 */
       
   783         $f2 = $f[2]->toInt64();
   764         $f2 = $f[2]->toInt64();
   784         /** @var ParagonIE_Sodium_Core32_Int64 $f3 */
       
   785         $f3 = $f[3]->toInt64();
   765         $f3 = $f[3]->toInt64();
   786         /** @var ParagonIE_Sodium_Core32_Int64 $f4 */
       
   787         $f4 = $f[4]->toInt64();
   766         $f4 = $f[4]->toInt64();
   788         /** @var ParagonIE_Sodium_Core32_Int64 $f5 */
       
   789         $f5 = $f[5]->toInt64();
   767         $f5 = $f[5]->toInt64();
   790         /** @var ParagonIE_Sodium_Core32_Int64 $f6 */
       
   791         $f6 = $f[6]->toInt64();
   768         $f6 = $f[6]->toInt64();
   792         /** @var ParagonIE_Sodium_Core32_Int64 $f7 */
       
   793         $f7 = $f[7]->toInt64();
   769         $f7 = $f[7]->toInt64();
   794         /** @var ParagonIE_Sodium_Core32_Int64 $f8 */
       
   795         $f8 = $f[8]->toInt64();
   770         $f8 = $f[8]->toInt64();
   796         /** @var ParagonIE_Sodium_Core32_Int64 $f9 */
       
   797         $f9 = $f[9]->toInt64();
   771         $f9 = $f[9]->toInt64();
   798 
   772 
   799         /** @var ParagonIE_Sodium_Core32_Int64 $f0_2 */
       
   800         $f0_2 = $f0->shiftLeft(1);
   773         $f0_2 = $f0->shiftLeft(1);
   801         $f1_2 = $f1->shiftLeft(1);
   774         $f1_2 = $f1->shiftLeft(1);
   802         $f2_2 = $f2->shiftLeft(1);
   775         $f2_2 = $f2->shiftLeft(1);
   803         $f3_2 = $f3->shiftLeft(1);
   776         $f3_2 = $f3->shiftLeft(1);
   804         $f4_2 = $f4->shiftLeft(1);
   777         $f4_2 = $f4->shiftLeft(1);
   808         $f5_38 = $f5->mulInt(38, 6);
   781         $f5_38 = $f5->mulInt(38, 6);
   809         $f6_19 = $f6->mulInt(19, 5);
   782         $f6_19 = $f6->mulInt(19, 5);
   810         $f7_38 = $f7->mulInt(38, 6);
   783         $f7_38 = $f7->mulInt(38, 6);
   811         $f8_19 = $f8->mulInt(19, 5);
   784         $f8_19 = $f8->mulInt(19, 5);
   812         $f9_38 = $f9->mulInt(38, 6);
   785         $f9_38 = $f9->mulInt(38, 6);
   813         /** @var ParagonIE_Sodium_Core32_Int64 $f0f0*/
   786 
   814         $f0f0    = $f0->mulInt64($f0, 28);
   787         $f0f0    = $f0->mulInt64($f0, 28);
   815         $f0f1_2  = $f0_2->mulInt64($f1, 28);
   788         $f0f1_2  = $f0_2->mulInt64($f1, 28);
   816         $f0f2_2 =  $f0_2->mulInt64($f2, 28);
   789         $f0f2_2 =  $f0_2->mulInt64($f2, 28);
   817         $f0f3_2 =  $f0_2->mulInt64($f3, 28);
   790         $f0f3_2 =  $f0_2->mulInt64($f3, 28);
   818         $f0f4_2 =  $f0_2->mulInt64($f4, 28);
   791         $f0f4_2 =  $f0_2->mulInt64($f4, 28);
   977      * @throws TypeError
   950      * @throws TypeError
   978      * @psalm-suppress MixedMethodCall
   951      * @psalm-suppress MixedMethodCall
   979      */
   952      */
   980     public static function fe_sq2(ParagonIE_Sodium_Core32_Curve25519_Fe $f)
   953     public static function fe_sq2(ParagonIE_Sodium_Core32_Curve25519_Fe $f)
   981     {
   954     {
   982         /** @var ParagonIE_Sodium_Core32_Int64 $f0 */
       
   983         $f0 = $f[0]->toInt64();
   955         $f0 = $f[0]->toInt64();
   984         /** @var ParagonIE_Sodium_Core32_Int64 $f1 */
       
   985         $f1 = $f[1]->toInt64();
   956         $f1 = $f[1]->toInt64();
   986         /** @var ParagonIE_Sodium_Core32_Int64 $f2 */
       
   987         $f2 = $f[2]->toInt64();
   957         $f2 = $f[2]->toInt64();
   988         /** @var ParagonIE_Sodium_Core32_Int64 $f3 */
       
   989         $f3 = $f[3]->toInt64();
   958         $f3 = $f[3]->toInt64();
   990         /** @var ParagonIE_Sodium_Core32_Int64 $f4 */
       
   991         $f4 = $f[4]->toInt64();
   959         $f4 = $f[4]->toInt64();
   992         /** @var ParagonIE_Sodium_Core32_Int64 $f5 */
       
   993         $f5 = $f[5]->toInt64();
   960         $f5 = $f[5]->toInt64();
   994         /** @var ParagonIE_Sodium_Core32_Int64 $f6 */
       
   995         $f6 = $f[6]->toInt64();
   961         $f6 = $f[6]->toInt64();
   996         /** @var ParagonIE_Sodium_Core32_Int64 $f7 */
       
   997         $f7 = $f[7]->toInt64();
   962         $f7 = $f[7]->toInt64();
   998         /** @var ParagonIE_Sodium_Core32_Int64 $f8 */
       
   999         $f8 = $f[8]->toInt64();
   963         $f8 = $f[8]->toInt64();
  1000         /** @var ParagonIE_Sodium_Core32_Int64 $f9 */
       
  1001         $f9 = $f[9]->toInt64();
   964         $f9 = $f[9]->toInt64();
  1002 
   965 
  1003         $f0_2 = $f0->shiftLeft(1);
   966         $f0_2 = $f0->shiftLeft(1);
  1004         $f1_2 = $f1->shiftLeft(1);
   967         $f1_2 = $f1->shiftLeft(1);
  1005         $f2_2 = $f2->shiftLeft(1);
   968         $f2_2 = $f2->shiftLeft(1);
  1477      */
  1440      */
  1478     public static function ge_frombytes_negate_vartime($s)
  1441     public static function ge_frombytes_negate_vartime($s)
  1479     {
  1442     {
  1480         static $d = null;
  1443         static $d = null;
  1481         if (!$d) {
  1444         if (!$d) {
  1482             /** @var ParagonIE_Sodium_Core32_Curve25519_Fe $d */
       
  1483             $d = ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray(
  1445             $d = ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray(
  1484                 array(
  1446                 array(
  1485                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[0]),
  1447                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[0]),
  1486                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[1]),
  1448                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[1]),
  1487                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[2]),
  1449                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[2]),
  1493                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[8]),
  1455                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[8]),
  1494                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[9])
  1456                     ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[9])
  1495                 )
  1457                 )
  1496             );
  1458             );
  1497         }
  1459         }
       
  1460         /** @var ParagonIE_Sodium_Core32_Curve25519_Fe $d */
  1498 
  1461 
  1499         # fe_frombytes(h->Y,s);
  1462         # fe_frombytes(h->Y,s);
  1500         # fe_1(h->Z);
  1463         # fe_1(h->Z);
  1501         $h = new ParagonIE_Sodium_Core32_Curve25519_Ge_P3(
  1464         $h = new ParagonIE_Sodium_Core32_Curve25519_Ge_P3(
  1502             self::fe_0(),
  1465             self::fe_0(),
  1831      * @return int
  1794      * @return int
  1832      * @psalm-suppress MixedReturnStatement
  1795      * @psalm-suppress MixedReturnStatement
  1833      */
  1796      */
  1834     public static function equal($b, $c)
  1797     public static function equal($b, $c)
  1835     {
  1798     {
  1836         return (int) ((($b ^ $c) - 1 & 0xffffffff) >> 31);
  1799         $b0 = $b & 0xffff;
       
  1800         $b1 = ($b >> 16) & 0xffff;
       
  1801         $c0 = $c & 0xffff;
       
  1802         $c1 = ($c >> 16) & 0xffff;
       
  1803 
       
  1804         $d0 = (($b0 ^ $c0) - 1) >> 31;
       
  1805         $d1 = (($b1 ^ $c1) - 1) >> 31;
       
  1806         return ($d0 & $d1) & 1;
  1837     }
  1807     }
  1838 
  1808 
  1839     /**
  1809     /**
  1840      * @internal You should not use this directly from another application
  1810      * @internal You should not use this directly from another application
  1841      *
  1811      *
  1848     {
  1818     {
  1849         if (is_int($char)) {
  1819         if (is_int($char)) {
  1850             return $char < 0 ? 1 : 0;
  1820             return $char < 0 ? 1 : 0;
  1851         }
  1821         }
  1852         /** @var string $char */
  1822         /** @var string $char */
  1853         /** @var int $x */
       
  1854         $x = self::chrToInt(self::substr($char, 0, 1));
  1823         $x = self::chrToInt(self::substr($char, 0, 1));
  1855         return (int) ($x >> 31);
  1824         return (int) ($x >> 31);
  1856     }
  1825     }
  1857 
  1826 
  1858     /**
  1827     /**
  1954         if ($pos < 0 || $pos > 31) {
  1923         if ($pos < 0 || $pos > 31) {
  1955             throw new RangeException('Position is out of range [0, 31]');
  1924             throw new RangeException('Position is out of range [0, 31]');
  1956         }
  1925         }
  1957 
  1926 
  1958         $bnegative = self::negative($b);
  1927         $bnegative = self::negative($b);
  1959         /** @var int $babs */
       
  1960         $babs = $b - (((-$bnegative) & $b) << 1);
  1928         $babs = $b - (((-$bnegative) & $b) << 1);
  1961 
  1929 
  1962         $t = self::ge_precomp_0();
  1930         $t = self::ge_precomp_0();
  1963         for ($i = 0; $i < 8; ++$i) {
  1931         for ($i = 0; $i < 8; ++$i) {
  1964             $t = self::cmov(
  1932             $t = self::cmov(
  1965                 $t,
  1933                 $t,
  1966                 $base[$pos][$i],
  1934                 $base[$pos][$i],
  1967                 self::equal($babs, $i + 1)
  1935                 -self::equal($babs, $i + 1)
  1968             );
  1936             );
  1969         }
  1937         }
  1970         $minusT = new ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp(
  1938         $minusT = new ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp(
  1971             self::fe_copy($t->yminusx),
  1939             self::fe_copy($t->yminusx),
  1972             self::fe_copy($t->yplusx),
  1940             self::fe_copy($t->yplusx),
  2228 
  2196 
  2229         /** @var int $carry */
  2197         /** @var int $carry */
  2230         $carry = 0;
  2198         $carry = 0;
  2231         for ($i = 0; $i < 63; ++$i) {
  2199         for ($i = 0; $i < 63; ++$i) {
  2232             $e[$i] += $carry;
  2200             $e[$i] += $carry;
  2233             /** @var int $carry */
       
  2234             $carry = $e[$i] + 8;
  2201             $carry = $e[$i] + 8;
  2235             /** @var int $carry */
       
  2236             $carry >>= 4;
  2202             $carry >>= 4;
  2237             $e[$i] -= $carry << 4;
  2203             $e[$i] -= $carry << 4;
  2238         }
  2204         }
  2239 
  2205 
  2240         /** @var array<int, int> $e */
  2206         /** @var array<int, int> $e */
  3138      * @throws SodiumException
  3104      * @throws SodiumException
  3139      * @throws TypeError
  3105      * @throws TypeError
  3140      */
  3106      */
  3141     public static function ge_mul_l(ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A)
  3107     public static function ge_mul_l(ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A)
  3142     {
  3108     {
  3143         /** @var array<int, int> $aslide */
       
  3144         $aslide = array(
  3109         $aslide = array(
  3145             13, 0, 0, 0, 0, -1, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0,
  3110             13, 0, 0, 0, 0, -1, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0,
  3146             0, 0, 0, -3, 0, 0, 0, 0, -13, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 3, 0,
  3111             0, 0, 0, -3, 0, 0, 0, 0, -13, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 3, 0,
  3147             0, 0, 0, -13, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0,
  3112             0, 0, 0, -13, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0,
  3148             0, 0, 11, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, -1,
  3113             0, 0, 11, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, -1,