diff --git a/phpseclib/Math/PrimeField/Integer.php b/phpseclib/Math/PrimeField/Integer.php index c24f4e18..7edfd6c6 100644 --- a/phpseclib/Math/PrimeField/Integer.php +++ b/phpseclib/Math/PrimeField/Integer.php @@ -294,8 +294,11 @@ class Integer extends Base */ public function toBytes(): string { - $length = static::$modulo[$this->instanceID]->getLengthInBytes(); - return str_pad($this->value->toBytes(), $length, "\0", STR_PAD_LEFT); + if (isset(static::$modulo[$this->instanceID])) { + $length = static::$modulo[$this->instanceID]->getLengthInBytes(); + return str_pad($this->value->toBytes(), $length, "\0", STR_PAD_LEFT); + } + return $this->value->toBytes(); } /** diff --git a/tests/Unit/Math/PrimeFieldTest.php b/tests/Unit/Math/PrimeFieldTest.php index 0567fdbd..470ce815 100644 --- a/tests/Unit/Math/PrimeFieldTest.php +++ b/tests/Unit/Math/PrimeFieldTest.php @@ -22,4 +22,21 @@ class PrimeFieldTest extends PhpseclibTestCase echo $num2->squareRoot(); } + + /** + * @group github1929 + */ + public function testGarbageCollectedToBytes() + { + $blob = base64_decode('BFgsTFQeqKr0toyURbtT43INMDS7FTHjz3yn3MR1/Yv/pb2b9ZCYNQ/Tafe5hQpEJ4TpZOKfikP/hWZvFL8QCPgqbIGqw/KTfA=='); + $public = "\0" . substr($blob, 0, 49); + $private = substr($blob, -24); + + $point = \phpseclib3\Crypt\EC\Formats\Keys\PKCS1::extractPoint( + $public, + new \phpseclib3\Crypt\EC\Curves\secp192r1() + ); + + $this->assertIsString($point[0]->toBytes()); + } }