Skip to content

Commit

Permalink
BigInteger: speed up Barrett reductions
Browse files Browse the repository at this point in the history
the changes for phpseclib#1994 (commit 2689c72) slowed things down
unnecessarily.
  • Loading branch information
terrafrost committed Sep 24, 2024
1 parent db92f1b commit c3cd458
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
11 changes: 9 additions & 2 deletions phpseclib/Math/BigInteger/Engines/BCMath/Reductions/Barrett.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected static function reduce($n, $m)

$m_length = strlen($m);

if (strlen($n) >= 2 * $m_length) {
if (strlen($n) > 2 * $m_length) {
return bcmod($n, $m);
}

Expand All @@ -75,6 +75,13 @@ protected static function reduce($n, $m)
return self::regularBarrett($n, $m);
}
// n = 2 * m.length
$correctionNeeded = false;
if ($m_length & 1) {
$correctionNeeded = true;
$n .= '0';
$m .= '0';
$m_length++;
}

if (($key = array_search($m, $cache[self::VARIABLE])) === false) {
$key = count($cache[self::VARIABLE]);
Expand Down Expand Up @@ -131,7 +138,7 @@ protected static function reduce($n, $m)
$result = bcsub($result, $m);
}

return $result;
return $correctionNeeded ? substr($result, 0, -1) : $result;
}

/**
Expand Down
23 changes: 18 additions & 5 deletions phpseclib/Math/BigInteger/Engines/PHP/Reductions/Barrett.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected static function reduce(array $n, array $m, $class)
$m_length = count($m);

// if (self::compareHelper($n, $static::square($m)) >= 0) {
if (count($n) >= 2 * $m_length) {
if (count($n) > 2 * $m_length) {
$lhs = new $class();
$rhs = new $class();
$lhs->value = $n;
Expand All @@ -70,6 +70,13 @@ protected static function reduce(array $n, array $m, $class)
return self::regularBarrett($n, $m, $class);
}
// n = 2 * m.length
$correctionNeeded = false;
if ($m_length & 1) {
$correctionNeeded = true;
array_unshift($n, 0);
array_unshift($m, 0);
$m_length++;
}

if (($key = array_search($m, $cache[self::VARIABLE])) === false) {
$key = count($cache[self::VARIABLE]);
Expand Down Expand Up @@ -109,24 +116,30 @@ protected static function reduce(array $n, array $m, $class)
$temp = array_slice($n[self::VALUE], $m_length - 1);
// if even: ((m.length >> 1) + 2) + (m.length >> 1) == m.length + 2
// if odd: ((m.length >> 1) + 2) + (m.length >> 1) == (m.length - 1) + 2 == m.length + 1
// note that these are upper bounds. let's say m.length is 2. then you'd be multiplying a
// 3 digit number by a 1 digit number. if you're doing 999 * 9 (in base 10) the result will
// be a 4 digit number. but if you're multiplying 111 * 1 then the result will be a 3 digit
// number.
$temp = $class::multiplyHelper($temp, false, $u, false);
// if even: (m.length + 2) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) + 1
// if odd: (m.length + 1) - ((m.length >> 1) + 1) = m.length - (m.length >> 1)
$temp = array_slice($temp[self::VALUE], ($m_length >> 1) + 1);
// if even: (m.length - (m.length >> 1) + 1) + m.length = 2 * m.length - (m.length >> 1) + 1
// if odd: (m.length - (m.length >> 1)) + m.length = 2 * m.length - (m.length >> 1)
$temp = $class::multiplyHelper($temp, false, $m, false);

// at this point, if m had an odd number of digits, we'd be subtracting a 2 * m.length - (m.length >> 1) digit
// number from a m.length + (m.length >> 1) + 1 digit number. ie. there'd be an extra digit and the while loop
// at this point, if m had an odd number of digits, we'd (probably) be subtracting a 2 * m.length - (m.length >> 1)
// digit number from a m.length + (m.length >> 1) + 1 digit number. ie. there'd be an extra digit and the while loop
// following this comment would loop a lot (hence our calling _regularBarrett() in that situation).

$result = $class::subtractHelper($n[self::VALUE], false, $temp[self::VALUE], false);

while (self::compareHelper($result[self::VALUE], $result[self::SIGN], $m, false) >= 0) {
$result = $class::subtractHelper($result[self::VALUE], $result[self::SIGN], $m, false);
}

if ($correctionNeeded) {
array_shift($result[self::VALUE]);
}

return $result[self::VALUE];
}

Expand Down
20 changes: 18 additions & 2 deletions phpseclib/Math/BigInteger/Engines/PHP/Reductions/EvalBarrett.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ protected static function generateCustomReduction(PHP $m, $class)
return $func;
}

$correctionNeeded = false;
if ($m_length & 1) {
$correctionNeeded = true;
$m = clone $m;
array_unshift($m->value, 0);
$m_length++;
}

$lhs = new $class();
$lhs_value = &$lhs->value;

Expand All @@ -99,8 +107,12 @@ protected static function generateCustomReduction(PHP $m, $class)

$cutoff = count($m) + (count($m) >> 1);

$code = '
if (count($n) >= ' . (2 * count($m)) . ') {
$code = $correctionNeeded ?
'array_unshift($n, 0);' :
'';

$code .= '
if (count($n) > ' . (2 * count($m)) . ') {
$lhs = new ' . $class . '();
$rhs = new ' . $class . '();
$lhs->value = $n;
Expand Down Expand Up @@ -141,6 +153,10 @@ protected static function generateCustomReduction(PHP $m, $class)

$code .= self::generateInlineCompare($m, 'temp', $subcode);

if ($correctionNeeded) {
$code .= 'array_shift($temp);';
}

$code .= 'return $temp;';

eval('$func = function ($n) { ' . $code . '};');
Expand Down

0 comments on commit c3cd458

Please sign in to comment.