1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
<?php
/**
* BCMath Barrett Modular Exponentiation Engine
*
* PHP version 5 and 7
*
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2017 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://pear.php.net/package/Math_BigInteger
*/
namespace phpseclib3\Math\BigInteger\Engines\BCMath\Reductions;
use phpseclib3\Math\BigInteger\Engines\BCMath\Base;
/**
* PHP Barrett Modular Exponentiation Engine
*
* @author Jim Wigginton <terrafrost@php.net>
*/
abstract class Barrett extends Base
{
/**
* Cache constants
*
* $cache[self::VARIABLE] tells us whether or not the cached data is still valid.
*
*/
const VARIABLE = 0;
/**
* $cache[self::DATA] contains the cached data.
*
*/
const DATA = 1;
/**
* Barrett Modular Reduction
*
* See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=14 HAC 14.3.3} /
* {@link http://math.libtomcrypt.com/files/tommath.pdf#page=165 MPM 6.2.5} for more information. Modified slightly,
* so as not to require negative numbers (initially, this script didn't support negative numbers).
*
* Employs "folding", as described at
* {@link http://www.cosic.esat.kuleuven.be/publications/thesis-149.pdf#page=66 thesis-149.pdf#page=66}. To quote from
* it, "the idea [behind folding] is to find a value x' such that x (mod m) = x' (mod m), with x' being smaller than x."
*
* Unfortunately, the "Barrett Reduction with Folding" algorithm described in thesis-149.pdf is not, as written, all that
* usable on account of (1) its not using reasonable radix points as discussed in
* {@link http://math.libtomcrypt.com/files/tommath.pdf#page=162 MPM 6.2.2} and (2) the fact that, even with reasonable
* radix points, it only works when there are an even number of digits in the denominator. The reason for (2) is that
* (x >> 1) + (x >> 1) != x / 2 + x / 2. If x is even, they're the same, but if x is odd, they're not. See the in-line
* comments for details.
*
* @param string $n
* @param string $m
* @return string
*/
protected static function reduce($n, $m)
{
static $cache = [
self::VARIABLE => [],
self::DATA => []
];
$m_length = strlen($m);
if (strlen($n) > 2 * $m_length) {
return self::BCMOD_THREE_PARAMS ? bcmod($n, $m, 0) : bcmod($n, $m);
}
// if (m.length >> 1) + 2 <= m.length then m is too small and n can't be reduced
if ($m_length < 5) {
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]);
$cache[self::VARIABLE][] = $m;
$lhs = '1' . str_repeat('0', $m_length + ($m_length >> 1));
$u = bcdiv($lhs, $m, 0);
$m1 = bcsub($lhs, bcmul($u, $m, 0), 0);
$cache[self::DATA][] = [
'u' => $u, // m.length >> 1 (technically (m.length >> 1) + 1)
'm1' => $m1 // m.length
];
} else {
$cacheValues = $cache[self::DATA][$key];
$u = $cacheValues['u'];
$m1 = $cacheValues['m1'];
}
$cutoff = $m_length + ($m_length >> 1);
$lsd = substr($n, -$cutoff);
$msd = substr($n, 0, -$cutoff);
$temp = bcmul($msd, $m1, 0); // m.length + (m.length >> 1)
$n = bcadd($lsd, $temp, 0); // m.length + (m.length >> 1) + 1 (so basically we're adding two same length numbers)
//if ($m_length & 1) {
// return self::regularBarrett($n, $m);
//}
// (m.length + (m.length >> 1) + 1) - (m.length - 1) == (m.length >> 1) + 2
$temp = substr($n, 0, -$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
$temp = bcmul($temp, $u, 0);
// 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 = substr($temp, 0, -($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 = bcmul($temp, $m, 0);
// 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
// following this comment would loop a lot (hence our calling _regularBarrett() in that situation).
$result = bcsub($n, $temp, 0);
//if (bccomp($result, '0') < 0) {
if ($result[0] == '-') {
$temp = '1' . str_repeat('0', $m_length + 1);
$result = bcadd($result, $temp, 0);
}
while (bccomp($result, $m, 0) >= 0) {
$result = bcsub($result, $m, 0);
}
return $correctionNeeded && $result != '0' ? substr($result, 0, -1) : $result;
}
/**
* (Regular) Barrett Modular Reduction
*
* For numbers with more than four digits BigInteger::_barrett() is faster. The difference between that and this
* is that this function does not fold the denominator into a smaller form.
*
* @param string $x
* @param string $n
* @return string
*/
private static function regularBarrett($x, $n)
{
static $cache = [
self::VARIABLE => [],
self::DATA => []
];
$n_length = strlen($n);
if (strlen($x) > 2 * $n_length) {
return self::BCMOD_THREE_PARAMS ? bcmod($x, $n, 0) : bcmod($x, $n);
}
if (($key = array_search($n, $cache[self::VARIABLE])) === false) {
$key = count($cache[self::VARIABLE]);
$cache[self::VARIABLE][] = $n;
$lhs = '1' . str_repeat('0', 2 * $n_length);
$cache[self::DATA][] = bcdiv($lhs, $n, 0);
}
$temp = substr($x, 0, -$n_length + 1);
$temp = bcmul($temp, $cache[self::DATA][$key], 0);
$temp = substr($temp, 0, -$n_length - 1);
$r1 = substr($x, -$n_length - 1);
$r2 = substr(bcmul($temp, $n, 0), -$n_length - 1);
$result = bcsub($r1, $r2);
//if (bccomp($result, '0') < 0) {
if ($result[0] == '-') {
$q = '1' . str_repeat('0', $n_length + 1);
$result = bcadd($result, $q, 0);
}
while (bccomp($result, $n, 0) >= 0) {
$result = bcsub($result, $n, 0);
}
return $result;
}
}
|