# ParagonIE_Sodium_Core_Util::mul() public WP 1.0

Multiply two integers in constant-time

Micro-architecture timing side-channels caused by how your CPU implements multiplication are best prevented by never using the multiplication operators and ensuring that our code always takes the same number of operations to complete, regardless of the values of `$a` and `$b`.

{} It's a method of the class: ParagonIE_Sodium_Core_Util{}

No Hooks.

##### Return

`Int`

. Null. Nothing.

### Usage

$result = ParagonIE_Sodium_Core_Util::mul( $a, $b, $size );

- $a
*(int) (required)* - -
- $b
*(int) (required)* - -
- $size
*(int)* - Limits the number of operations (useful for small, constant operands)

###
Code of `ParagonIE_Sodium_Core_Util::mul() ParagonIE Sodium Core Util::mul `
^{WP 5.7}

public static function mul($a, $b, $size = 0)
{
if (ParagonIE_Sodium_Compat::$fastMult) {
return (int) ($a * $b);
}
static $defaultSize = null;
/** @var int $defaultSize */
if (!$defaultSize) {
/** @var int $defaultSize */
$defaultSize = (PHP_INT_SIZE << 3) - 1;
}
if ($size < 1) {
/** @var int $size */
$size = $defaultSize;
}
/** @var int $size */
$c = 0;
/**
* Mask is either -1 or 0.
*
* -1 in binary looks like 0x1111 ... 1111
* 0 in binary looks like 0x0000 ... 0000
*
* @var int
*/
$mask = -(($b >> ((int) $defaultSize)) & 1);
/**
* Ensure $b is a positive integer, without creating
* a branching side-channel
*
* @var int $b
*/
$b = ($b & ~$mask) | ($mask & -$b);
/**
* Unless $size is provided:
*
* This loop always runs 32 times when PHP_INT_SIZE is 4.
* This loop always runs 64 times when PHP_INT_SIZE is 8.
*/
for ($i = $size; $i >= 0; --$i) {
$c += (int) ($a & -($b & 1));
$a <<= 1;
$b >>= 1;
}
/**
* If $b was negative, we then apply the same value to $c here.
* It doesn't matter much if $a was negative; the $c += above would
* have produced a negative integer to begin with. But a negative $b
* makes $b >>= 1 never return 0, so we would end up with incorrect
* results.
*
* The end result is what we'd expect from integer multiplication.
*/
return (int) (($c & ~$mask) | ($mask & -$c));
}