You are here

public static function ParagonIE_Sodium_Compat::runtime_speed_test in Automatic Updates 8

Same name and namespace in other branches
  1. 7 vendor/paragonie/sodium_compat/src/Compat.php \ParagonIE_Sodium_Compat::runtime_speed_test()

Runtime testing method for 32-bit platforms.

Usage: If runtime_speed_test() returns FALSE, then our 32-bit implementation is to slow to use safely without risking timeouts. If this happens, install sodium from PECL to get acceptable performance.

Parameters

int $iterations Number of multiplications to attempt:

int $maxTimeout Milliseconds:

Return value

bool TRUE if we're fast enough, FALSE is not

Throws

SodiumException

File

vendor/paragonie/sodium_compat/src/Compat.php, line 3467

Class

ParagonIE_Sodium_Compat

Code

public static function runtime_speed_test($iterations, $maxTimeout) {
  if (self::polyfill_is_fast()) {
    return true;
  }

  /** @var float $end */
  $end = 0.0;

  /** @var float $start */
  $start = microtime(true);

  /** @var ParagonIE_Sodium_Core32_Int64 $a */
  $a = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16));
  for ($i = 0; $i < $iterations; ++$i) {

    /** @var ParagonIE_Sodium_Core32_Int64 $b */
    $b = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16));
    $a
      ->mulInt64($b);
  }

  /** @var float $end */
  $end = microtime(true);

  /** @var int $diff */
  $diff = (int) ceil(($end - $start) * 1000);
  return $diff < $maxTimeout;
}