From a9ae170db064226d18eb3e795c78bd7100f30a41 Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Wed, 24 Apr 2024 11:38:44 -0400 Subject: [PATCH] Address Scrutinizer complaints --- src/Curves/NistCurve.php | 8 ++++++-- src/Math/GmpMath.php | 7 ++++++- src/Primitives/Point.php | 4 ++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Curves/NistCurve.php b/src/Curves/NistCurve.php index e14b6d0..f99274c 100644 --- a/src/Curves/NistCurve.php +++ b/src/Curves/NistCurve.php @@ -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); } @@ -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); } diff --git a/src/Math/GmpMath.php b/src/Math/GmpMath.php index c551fa2..0037db6 100644 --- a/src/Math/GmpMath.php +++ b/src/Math/GmpMath.php @@ -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"); @@ -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)); } @@ -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; diff --git a/src/Primitives/Point.php b/src/Primitives/Point.php index 2010aa8..510509a 100644 --- a/src/Primitives/Point.php +++ b/src/Primitives/Point.php @@ -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);