BigInteger: EvalBarrett / Barrett could sometimes slow to a crawl

This commit is contained in:
terrafrost 2024-05-01 15:12:04 -05:00
parent f7f9831bec
commit 2689c727e7
4 changed files with 24 additions and 3 deletions

View File

@ -66,7 +66,7 @@ abstract class Barrett extends Base
$m_length = strlen($m);
if (strlen($n) > 2 * $m_length) {
if (strlen($n) >= 2 * $m_length) {
return bcmod($n, $m);
}

View File

@ -56,7 +56,7 @@ abstract class Barrett extends Base
$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;

View File

@ -100,7 +100,7 @@ abstract class EvalBarrett extends Base
$cutoff = count($m) + (count($m) >> 1);
$code = '
if (count($n) > ' . (2 * count($m)) . ') {
if (count($n) >= ' . (2 * count($m)) . ') {
$lhs = new ' . $class . '();
$rhs = new ' . $class . '();
$lhs->value = $n;

View File

@ -1421,4 +1421,25 @@ vtpiPBM=
$this->pkcs8tester($key, $pass);
}
/**
* @group github1994
*/
public function testCloseNumbers()
{
$rsa = PublicKeyLoader::load([
// Modulus
'n' => new BigInteger('5BDD6AFB1E1AFB50D1B2989F70B549B8D44AE3712B444F2C5D862C46C99526E998B79BF0B4F1461524E39D263F3130B9E08F3B17C2070785EFB0EDEC1E75C6C2B8185FA9596886D5DAF8B68E92FCF5F1B33E7CD772845555B086D2A2466B6398A04DFE1C727BB020g1ED2BF3F03D2826F89616D0846C18B1D87064616FAD394462', 16),
// Exponent
'e' => new BigInteger('6FE4F5D0AFCC16E8A5CC68955D4EF28255A546D06F34DD103540B9A7D202AEC96353072DB65D9C360E9030F413971142EE6A28974767CCF3ABFA4E7ADDAEAD81D3F8AE5FF1B8241CA9EF51C10941FFFA74482A636CBD909D29CF7A0346653D3C286EA1F392F4968AEF1489EC4B4BCEA4F248F3931B1C9BE2808DBD33B049731A', 16)
])
->withPadding(RSA::SIGNATURE_PKCS1)
->withHash('md5')
->asPrivateKey();
$sig = bin2hex($rsa->sign('toto'));
$expected = '4370b3fd5dd318c0c3be8989574fbf4ededc805c6f225ada84f8d882d327b7b300f899878204ff99efdf03b17c26518b8941d602abd16dbdac637c5ae61814cb689da266fe07bc978d417fe6742f650bc35ee79dd2431912fc19e36012e61fcb7cdfd506ca3c5b80';
$this->assertSame($expected, $sig);
}
}