Merge branch '1.0' into 2.0

This commit is contained in:
terrafrost 2019-09-15 17:20:11 -05:00
commit bd6dbd0695
2 changed files with 26 additions and 1 deletions

View File

@ -545,7 +545,7 @@ class BigInteger
$bytes = chr(0);
}
if (ord($bytes[0]) & 0x80) {
if ($this->precision <= 0 && (ord($bytes[0]) & 0x80)) {
$bytes = chr(0) . $bytes;
}
@ -707,6 +707,7 @@ class BigInteger
}
$temp = $this->copy();
$temp->bitmask = false;
$temp->is_negative = false;
$divisor = new static();
@ -3569,7 +3570,14 @@ class BigInteger
switch (MATH_BIGINTEGER_MODE) {
case self::MODE_GMP:
if ($this->bitmask !== false) {
$flip = gmp_cmp($result->value, gmp_init(0)) < 0;
if ($flip) {
$result->value = gmp_neg($result->value);
}
$result->value = gmp_and($result->value, $result->bitmask->value);
if ($flip) {
$result->value = gmp_neg($result->value);
}
}
return $result;

View File

@ -431,4 +431,21 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
$temp = $this->getInstance('-0');
$this->assertSame($temp->toString(), '0');
}
public function testNegativePrecision()
{
$vals = [
'-9223372036854775808', // eg. 8000 0000 0000 0000
'-1'
];
foreach ($vals as $val) {
$x = $this->getInstance($val);
$x->setPrecision(64); // ie. 8 bytes
$this->assertSame($val, "$x");
$r = $x->toBytes(true);
$this->assertSame(8, strlen($r));
$x2 = $this->getInstance($r, -256);
$this->assertSame(0, $x->compare($x2));
}
}
}