wp/wp-includes/sodium_compat/src/Core/Ed25519.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 21 48c4eec2b7e6
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
     9  */
     9  */
    10 abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519
    10 abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519
    11 {
    11 {
    12     const KEYPAIR_BYTES = 96;
    12     const KEYPAIR_BYTES = 96;
    13     const SEED_BYTES = 32;
    13     const SEED_BYTES = 32;
       
    14     const SCALAR_BYTES = 32;
    14 
    15 
    15     /**
    16     /**
    16      * @internal You should not use this directly from another application
    17      * @internal You should not use this directly from another application
    17      *
    18      *
    18      * @return string (96 bytes)
    19      * @return string (96 bytes)
   475                 return true;
   476                 return true;
   476             }
   477             }
   477         }
   478         }
   478         return false;
   479         return false;
   479     }
   480     }
       
   481 
       
   482     /**
       
   483      * @param string $s
       
   484      * @return string
       
   485      * @throws SodiumException
       
   486      */
       
   487     public static function scalar_complement($s)
       
   488     {
       
   489         $t_ = self::L . str_repeat("\x00", 32);
       
   490         sodium_increment($t_);
       
   491         $s_ = $s . str_repeat("\x00", 32);
       
   492         ParagonIE_Sodium_Compat::sub($t_, $s_);
       
   493         return self::sc_reduce($t_);
       
   494     }
       
   495 
       
   496     /**
       
   497      * @return string
       
   498      * @throws SodiumException
       
   499      */
       
   500     public static function scalar_random()
       
   501     {
       
   502         do {
       
   503             $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES);
       
   504             $r[self::SCALAR_BYTES - 1] = self::intToChr(
       
   505                 self::chrToInt($r[self::SCALAR_BYTES - 1]) & 0x1f
       
   506             );
       
   507         } while (
       
   508             !self::check_S_lt_L($r) || ParagonIE_Sodium_Compat::is_zero($r)
       
   509         );
       
   510         return $r;
       
   511     }
       
   512 
       
   513     /**
       
   514      * @param string $s
       
   515      * @return string
       
   516      * @throws SodiumException
       
   517      */
       
   518     public static function scalar_negate($s)
       
   519     {
       
   520         $t_ = self::L . str_repeat("\x00", 32) ;
       
   521         $s_ = $s . str_repeat("\x00", 32) ;
       
   522         ParagonIE_Sodium_Compat::sub($t_, $s_);
       
   523         return self::sc_reduce($t_);
       
   524     }
       
   525 
       
   526     /**
       
   527      * @param string $a
       
   528      * @param string $b
       
   529      * @return string
       
   530      * @throws SodiumException
       
   531      */
       
   532     public static function scalar_add($a, $b)
       
   533     {
       
   534         $a_ = $a . str_repeat("\x00", 32);
       
   535         $b_ = $b . str_repeat("\x00", 32);
       
   536         ParagonIE_Sodium_Compat::add($a_, $b_);
       
   537         return self::sc_reduce($a_);
       
   538     }
       
   539 
       
   540     /**
       
   541      * @param string $x
       
   542      * @param string $y
       
   543      * @return string
       
   544      * @throws SodiumException
       
   545      */
       
   546     public static function scalar_sub($x, $y)
       
   547     {
       
   548         $yn = self::scalar_negate($y);
       
   549         return self::scalar_add($x, $yn);
       
   550     }
   480 }
   551 }