Skip to content

Commit

Permalink
Address Scrutinizer complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Apr 24, 2024
1 parent e0e641a commit a9ae170
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Curves/NistCurve.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public function curve192(): NamedCurveFp
/** @var GMP $b */
$b = gmp_init('64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1', 16);

$parameters = new CurveParameters(192, $p, gmp_init('-3', 10), $b);
/** @var GMP $minusThree */
$minusThree = gmp_init('-3', 10);
$parameters = new CurveParameters(192, $p, $minusThree, $b);

return new NamedCurveFp(self::NAME_P192, $parameters, $this->adapter);
}
Expand Down Expand Up @@ -108,7 +110,9 @@ public function curve224(): NamedCurveFp
/** @var GMP $b */
$b = gmp_init('b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4', 16);

$parameters = new CurveParameters(224, $p, gmp_init(-3, 10), $b);
/** @var GMP $minusThree */
$minusThree = gmp_init('-3', 10);
$parameters = new CurveParameters(224, $p, $minusThree, $b);

return new NamedCurveFp(self::NAME_P224, $parameters, $this->adapter);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Math/GmpMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ public function intToFixedSizeString(GMP $x, int $byteSize): string
throw new \RuntimeException("x was negative - not yet supported");
}

/** @var GMP $two */
$two = gmp_init(2);
/** @var GMP $range */
$range = gmp_pow($two, $byteSize * 8);
if (NumberSize::bnNumBits($this, $x) >= NumberSize::bnNumBits($this, $range)) {
throw new \RuntimeException("Number overflows byte size");
Expand Down Expand Up @@ -313,6 +315,7 @@ public function stringToInt(string $s): GMP
$sLen = BinaryString::length($s);

for ($c = 0; $c < $sLen; $c ++) {
/** @var GMP $result */
$result = gmp_add(gmp_mul(256, $result), gmp_init(ord($s[$c]), 10));
}

Expand All @@ -334,7 +337,9 @@ public function digestInteger(GMP $m): GMP
*/
public function gcd2(GMP $a, GMP $m): GMP
{
while ($this->cmp($a, gmp_init(0)) > 0) {
/** @var GMP $zero */
$zero = gmp_init(0);
while ($this->cmp($a, $zero) > 0) {
$temp = $a;
$a = $this->mod($m, $a);
$m = $temp;
Expand Down
4 changes: 4 additions & 0 deletions src/Primitives/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,16 @@ public function cswapValue(& $a, & $b, int $cond, int $maskBitSize)

$mask = str_pad('', $maskBitSize, (string) (1 - intval($cond)), STR_PAD_LEFT);
$mask = gmp_init($mask, 2);
/** @var GMP $mask */

$taA = $this->adapter->bitwiseAnd($sa, $mask);
$taB = $this->adapter->bitwiseAnd($sb, $mask);

/** @var GMP $sa */
$sa = $this->adapter->bitwiseXor($this->adapter->bitwiseXor($sa, $sb), $taB);
/** @var GMP $sb */
$sb = $this->adapter->bitwiseXor($this->adapter->bitwiseXor($sa, $sb), $taA);
/** @var GMP $sa */
$sa = $this->adapter->bitwiseXor($this->adapter->bitwiseXor($sa, $sb), $taB);

$a = $isGMP ? $sa : (bool) gmp_strval($sa, 10);
Expand Down

0 comments on commit a9ae170

Please # to comment.