Skip to content

Commit

Permalink
Add Psalm to workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Apr 23, 2024
1 parent df6140b commit 1cb3e5c
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 88 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Psalm

on: [push]

jobs:
psalm:
name: Psalm on PHP ${{ matrix.php-versions }}
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: ['ubuntu-latest']
php-versions: ['8.3']
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: psalm:4
coverage: none

- name: Install Composer dependencies
uses: "ramsey/composer-install@v2"
with:
composer-options: --no-dev

- name: Static Analysis
run: psalm
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Pure PHP Elliptic Curve DSA and DH

[![Build Status](https://github.com/paragonie/phpecc/actions/workflows/test.yml/badge.svg)](https://github.com/paragonie/phpecc/actions)
[![Type Safety](https://github.com/paragonie/phpecc/actions/workflows/psalm.yml/badge.svg)](https://github.com/paragonie/phpecc/actions)

[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/paragonie/phpecc/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/paragonie/phpecc?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/paragonie/phpecc/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/phpecc/phpecc/?branch=master)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"phpunit/phpunit": "^6.0||^8.0||^9.0",
"squizlabs/php_codesniffer": "^2.0",
"symfony/yaml": "^2.6|^3.0",
"vimeo/psalm": "^4|^5",
"ext-json": "*"
},
"autoload": {
Expand Down
23 changes: 23 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<psalm
errorLevel="7"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="true"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<UnnecessaryVarAnnotation errorLevel="info" />
<UnusedClass errorLevel="info" />
<UnusedProperty errorLevel="info" />
<PossiblyUnusedMethod errorLevel="info" />
</issueHandlers>
</psalm>
2 changes: 2 additions & 0 deletions src/Math/DebugDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ private function write($message)
* @param string $func
* @param array $args
* @return mixed
*
* @psalm-suppress UnusedReturnValue
*/
private function call($func, $args)
{
Expand Down
83 changes: 42 additions & 41 deletions src/Math/GmpMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mdanter\Ecc\Math;

use GMP;
use Mdanter\Ecc\Util\BinaryString;
use Mdanter\Ecc\Util\NumberSize;

Expand All @@ -11,17 +12,17 @@ class GmpMath implements GmpMathInterface
* {@inheritDoc}
* @see GmpMathInterface::cmp()
*/
public function cmp(\GMP $first, \GMP $other): int
public function cmp(GMP $first, GMP $other): int
{
return gmp_cmp($first, $other);
}

/**
* @param \GMP $first
* @param \GMP $other
* @param GMP $first
* @param GMP $other
* @return bool
*/
public function equals(\GMP $first, \GMP $other): bool
public function equals(GMP $first, GMP $other): bool
{
return gmp_cmp($first, $other) === 0;
}
Expand All @@ -30,7 +31,7 @@ public function equals(\GMP $first, \GMP $other): bool
* {@inheritDoc}
* @see GmpMathInterface::mod()
*/
public function mod(\GMP $number, \GMP $modulus): \GMP
public function mod(GMP $number, GMP $modulus): GMP
{
return gmp_mod($number, $modulus);
}
Expand All @@ -39,7 +40,7 @@ public function mod(\GMP $number, \GMP $modulus): \GMP
* {@inheritDoc}
* @see GmpMathInterface::add()
*/
public function add(\GMP $augend, \GMP $addend): \GMP
public function add(GMP $augend, GMP $addend): GMP
{
return gmp_add($augend, $addend);
}
Expand All @@ -48,7 +49,7 @@ public function add(\GMP $augend, \GMP $addend): \GMP
* {@inheritDoc}
* @see GmpMathInterface::sub()
*/
public function sub(\GMP $minuend, \GMP $subtrahend): \GMP
public function sub(GMP $minuend, GMP $subtrahend): GMP
{
return gmp_sub($minuend, $subtrahend);
}
Expand All @@ -57,7 +58,7 @@ public function sub(\GMP $minuend, \GMP $subtrahend): \GMP
* {@inheritDoc}
* @see GmpMathInterface::mul()
*/
public function mul(\GMP $multiplier, \GMP $multiplicand): \GMP
public function mul(GMP $multiplier, GMP $multiplicand): GMP
{
return gmp_mul($multiplier, $multiplicand);
}
Expand All @@ -66,7 +67,7 @@ public function mul(\GMP $multiplier, \GMP $multiplicand): \GMP
* {@inheritDoc}
* @see GmpMathInterface::div()
*/
public function div(\GMP $dividend, \GMP $divisor): \GMP
public function div(GMP $dividend, GMP $divisor): GMP
{
return gmp_div($dividend, $divisor);
}
Expand All @@ -75,7 +76,7 @@ public function div(\GMP $dividend, \GMP $divisor): \GMP
* {@inheritDoc}
* @see GmpMathInterface::pow()
*/
public function pow(\GMP $base, int $exponent): \GMP
public function pow(GMP $base, int $exponent): GMP
{
return gmp_pow($base, $exponent);
}
Expand All @@ -84,7 +85,7 @@ public function pow(\GMP $base, int $exponent): \GMP
* {@inheritDoc}
* @see GmpMathInterface::bitwiseAnd()
*/
public function bitwiseAnd(\GMP $first, \GMP $other): \GMP
public function bitwiseAnd(GMP $first, GMP $other): GMP
{
return gmp_and($first, $other);
}
Expand All @@ -93,7 +94,7 @@ public function bitwiseAnd(\GMP $first, \GMP $other): \GMP
* {@inheritDoc}
* @see GmpMathInterface::rightShift()
*/
public function rightShift(\GMP $number, int $positions): \GMP
public function rightShift(GMP $number, int $positions): GMP
{
// Shift 1 right = div / 2
return gmp_div($number, gmp_pow(gmp_init(2, 10), $positions));
Expand All @@ -103,7 +104,7 @@ public function rightShift(\GMP $number, int $positions): \GMP
* {@inheritDoc}
* @see GmpMathInterface::bitwiseXor()
*/
public function bitwiseXor(\GMP $first, \GMP $other): \GMP
public function bitwiseXor(GMP $first, GMP $other): GMP
{
return gmp_xor($first, $other);
}
Expand All @@ -112,7 +113,7 @@ public function bitwiseXor(\GMP $first, \GMP $other): \GMP
* {@inheritDoc}
* @see GmpMathInterface::leftShift()
*/
public function leftShift(\GMP $number, int $positions): \GMP
public function leftShift(GMP $number, int $positions): GMP
{
// Shift 1 left = mul by 2
return gmp_mul($number, gmp_pow(2, $positions));
Expand All @@ -122,7 +123,7 @@ public function leftShift(\GMP $number, int $positions): \GMP
* {@inheritDoc}
* @see GmpMathInterface::toString()
*/
public function toString(\GMP $value): string
public function toString(GMP $value): string
{
return gmp_strval($value);
}
Expand All @@ -131,24 +132,24 @@ public function toString(\GMP $value): string
* {@inheritDoc}
* @see GmpMathInterface::hexDec()
*/
public function hexDec(string $hex): string
public function hexDec(string $hexString): string
{
return gmp_strval(gmp_init($hex, 16), 10);
return gmp_strval(gmp_init($hexString, 16), 10);
}

/**
* {@inheritDoc}
* @see GmpMathInterface::decHex()
*/
public function decHex(string $dec): string
public function decHex(string $decString): string
{
$dec = gmp_init($dec, 10);
$decString = gmp_init($decString, 10);

if (gmp_cmp($dec, 0) < 0) {
if (gmp_cmp($decString, 0) < 0) {
throw new \InvalidArgumentException('Unable to convert negative integer to string');
}

$hex = gmp_strval($dec, 16);
$hex = gmp_strval($decString, 16);

if (BinaryString::length($hex) % 2 != 0) {
$hex = '0'.$hex;
Expand All @@ -161,7 +162,7 @@ public function decHex(string $dec): string
* {@inheritDoc}
* @see GmpMathInterface::powmod()
*/
public function powmod(\GMP $base, \GMP $exponent, \GMP $modulus): \GMP
public function powmod(GMP $base, GMP $exponent, GMP $modulus): GMP
{
if ($this->cmp($exponent, gmp_init(0, 10)) < 0) {
throw new \InvalidArgumentException("Negative exponents (" . $this->toString($exponent) . ") not allowed.");
Expand All @@ -174,7 +175,7 @@ public function powmod(\GMP $base, \GMP $exponent, \GMP $modulus): \GMP
* {@inheritDoc}
* @see GmpMathInterface::isPrime()
*/
public function isPrime(\GMP $n): bool
public function isPrime(GMP $n): bool
{
$prob = gmp_prob_prime($n);

Expand All @@ -189,16 +190,16 @@ public function isPrime(\GMP $n): bool
* {@inheritDoc}
* @see GmpMathInterface::nextPrime()
*/
public function nextPrime(\GMP $starting_value): \GMP
public function nextPrime(GMP $currentPrime): GMP
{
return gmp_nextprime($starting_value);
return gmp_nextprime($currentPrime);
}

/**
* {@inheritDoc}
* @see GmpMathInterface::inverseMod()
*/
public function inverseMod(\GMP $a, \GMP $m): \GMP
public function inverseMod(GMP $a, GMP $m): GMP
{
return gmp_invert($a, $m);
}
Expand All @@ -207,17 +208,17 @@ public function inverseMod(\GMP $a, \GMP $m): \GMP
* {@inheritDoc}
* @see GmpMathInterface::jacobi()
*/
public function jacobi(\GMP $a, \GMP $n): int
public function jacobi(GMP $a, GMP $p): int
{
return gmp_jacobi($a, $n);
return gmp_jacobi($a, $p);
}

/**
* @param \GMP $x
* @param GMP $x
* @param int $byteSize
* @return string
*/
public function intToFixedSizeString(\GMP $x, int $byteSize): string
public function intToFixedSizeString(GMP $x, int $byteSize): string
{
if ($byteSize < 0) {
throw new \RuntimeException("Byte size cannot be negative");
Expand Down Expand Up @@ -249,7 +250,7 @@ public function intToFixedSizeString(\GMP $x, int $byteSize): string
* {@inheritDoc}
* @see GmpMathInterface::intToString()
*/
public function intToString(\GMP $x): string
public function intToString(GMP $x): string
{
if (gmp_cmp($x, 0) < 0) {
throw new \InvalidArgumentException('Unable to convert negative integer to string');
Expand All @@ -268,7 +269,7 @@ public function intToString(\GMP $x): string
* {@inheritDoc}
* @see GmpMathInterface::stringToInt()
*/
public function stringToInt(string $s): \GMP
public function stringToInt(string $s): GMP
{
$result = gmp_init(0, 10);
$sLen = BinaryString::length($s);
Expand All @@ -284,7 +285,7 @@ public function stringToInt(string $s): \GMP
* {@inheritDoc}
* @see GmpMathInterface::digestInteger()
*/
public function digestInteger(\GMP $m): \GMP
public function digestInteger(GMP $m): GMP
{
return $this->stringToInt(hash('sha1', $this->intToString($m), true));
}
Expand All @@ -293,24 +294,24 @@ public function digestInteger(\GMP $m): \GMP
* {@inheritDoc}
* @see GmpMathInterface::gcd2()
*/
public function gcd2(\GMP $a, \GMP $b): \GMP
public function gcd2(GMP $a, GMP $m): GMP
{
while ($this->cmp($a, gmp_init(0)) > 0) {
$temp = $a;
$a = $this->mod($b, $a);
$b = $temp;
$a = $this->mod($m, $a);
$m = $temp;
}

return $b;
return $m;
}

/**
* {@inheritDoc}
* @see GmpMathInterface::baseConvert()
*/
public function baseConvert(string $number, int $from, int $to): string
public function baseConvert(string $value, int $fromBase, int $toBase): string
{
return gmp_strval(gmp_init($number, $from), $to);
return gmp_strval(gmp_init($value, $fromBase), $toBase);
}

/**
Expand All @@ -323,10 +324,10 @@ public function getNumberTheory(): NumberTheory
}

/**
* @param \GMP $modulus
* @param GMP $modulus
* @return ModularArithmetic
*/
public function getModularArithmetic(\GMP $modulus): ModularArithmetic
public function getModularArithmetic(GMP $modulus): ModularArithmetic
{
return new ModularArithmetic($this, $modulus);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Primitives/CurveFp.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public function getGenerator(GMP $x, GMP $y, GMP $order, ?RandomNumberGeneratorI

/**
* @param bool $wasOdd
* @param GMP $xCoord
* @param GMP $x
* @return GMP
*/
public function recoverYfromX(bool $wasOdd, GMP $xCoord): GMP
public function recoverYfromX(bool $wasOdd, GMP $x): GMP
{
$math = $this->adapter;
$prime = $this->getPrime();
Expand All @@ -121,8 +121,8 @@ public function recoverYfromX(bool $wasOdd, GMP $xCoord): GMP
$root = $this->adapter->getNumberTheory()->squareRootModP(
$math->add(
$math->add(
$this->modAdapter->pow($xCoord, gmp_init(3, 10)),
$math->mul($this->getA(), $xCoord)
$this->modAdapter->pow($x, gmp_init(3, 10)),
$math->mul($this->getA(), $x)
),
$this->getB()
),
Expand Down
Loading

0 comments on commit 1cb3e5c

Please sign in to comment.