Skip to content

Commit

Permalink
Merge pull request #29 from rgex/master
Browse files Browse the repository at this point in the history
code modernization
  • Loading branch information
rgex committed Dec 7, 2015
2 parents ca8959c + d2dea37 commit 77e6c0c
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public function __construct()
$this->p = gmp_init('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16);
$this->n = gmp_init('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16);

$this->G = array('x' => gmp_init('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
'y' => gmp_init('32670510020758816978083085130507043184471273380659243275938904335757337482424'));
$this->G = [
'x' => gmp_init('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
'y' => gmp_init('32670510020758816978083085130507043184471273380659243275938904335757337482424')
];

$this->networkPrefix = '00';
}
Expand Down Expand Up @@ -84,15 +86,16 @@ public function getNetworkPrefix()
*/
public function base58_permutation($char, $reverse = false)
{
$table = array('1','2','3','4','5','6','7','8','9','A','B','C','D',
'E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W',
'X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','m','n','o',
'p','q','r','s','t','u','v','w','x','y','z'
);
$table = [
'1','2','3','4','5','6','7','8','9','A','B','C','D',
'E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W',
'X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','m','n','o',
'p','q','r','s','t','u','v','w','x','y','z'
];

if($reverse)
{
$reversedTable = array();
$reversedTable = [];
foreach($table as $key => $element)
{
$reversedTable[$element] = $key;
Expand Down Expand Up @@ -145,7 +148,7 @@ public function generateRandom256BitsHexaString($extra = 'FkejkzqesrfeifH3ioio9h
$hex = bin2hex($bytes);
$random = $hex . microtime(true) . rand(100000000000, 1000000000000) . $extra;

if (!$cStrong) {
if ($cStrong === false) {
throw new \Exception('Your system is not able to generate strong enough random numbers');
}
$res = $this->hash256($random);
Expand Down Expand Up @@ -305,7 +308,7 @@ public function doublePoint(Array $pt)

// nPtX = slope^2 - 2 * ptX
// Equals slope^2 - ptX - ptX
$nPt = array();
$nPt = [];
$nPt['x'] = gmp_mod(
gmp_sub(
gmp_sub(
Expand Down Expand Up @@ -377,7 +380,7 @@ public function addPoints(Array $pt1, Array $pt2)
);

// nPtX = slope^2 - ptX1 - ptX2
$nPt = array();
$nPt = [];
$nPt['x'] = gmp_mod(
gmp_sub(
gmp_sub(
Expand Down Expand Up @@ -474,7 +477,7 @@ public function sqrt($a)
// In an infinite number field you have -2^2 = 2^2 = 4
// In a finite number field you have a^2 = (p-a)^2
$sqrt2 = gmp_mod(gmp_sub($p, $sqrt1), $p);
return array($sqrt1, $sqrt2);
return [$sqrt1, $sqrt2];
}
else
{
Expand Down Expand Up @@ -569,14 +572,14 @@ public function getPubKeyPointsWithDerPubKey($derPubKey)
//uncompressed der encoded public key
$x = substr($derPubKey, 2, 64);
$y = substr($derPubKey, 66, 64);
return array('x' => $x, 'y' => $y);
return ['x' => $x, 'y' => $y];
}
else if((substr($derPubKey, 0, 2) === '02' || substr($derPubKey, 0, 2) === '03') && strlen($derPubKey) === 66)
{
//compressed der encoded public key
$x = substr($derPubKey, 2, 64);
$y = $this->calculateYWithX($x, substr($derPubKey, 0, 2));
return array('x' => $x, 'y' => $y);
return ['x' => $x, 'y' => $y];
}
else
{
Expand Down Expand Up @@ -655,8 +658,9 @@ public function getPubKeyPoints()
throw new \Exception('No Private Key was defined');
}

$pubKey = $this->mulPoint($k,
array('x' => $G['x'], 'y' => $G['y'])
$pubKey = $this->mulPoint(
$k,
['x' => $G['x'], 'y' => $G['y']]
);

$pubKey['x'] = gmp_strval($pubKey['x'], 16);
Expand All @@ -682,7 +686,7 @@ public function getPubKeyPoints()
* @return string (hexa)
* @throws \Exception
*/
public function getUncompressedPubKey(array $pubKeyPts = array())
public function getUncompressedPubKey(array $pubKeyPts = [])
{
if(empty($pubKeyPts))
$pubKeyPts = $this->getPubKeyPoints();
Expand All @@ -698,7 +702,7 @@ public function getUncompressedPubKey(array $pubKeyPts = array())
* @return array|string
* @throws \Exception
*/
public function getPubKey(array $pubKeyPts = array())
public function getPubKey(array $pubKeyPts = [])
{
if(empty($pubKeyPts))
$pubKeyPts = $this->getPubKeyPoints();
Expand All @@ -724,7 +728,7 @@ public function getUncompressedAddress($compressed = false, $derPubKey = null)
{
if($derPubKey !== null)
{
if($compressed) {
if($compressed === true) {
$address = $this->getPubKey($this->getPubKeyPointsWithDerPubKey($derPubKey));
}
else {
Expand All @@ -733,7 +737,7 @@ public function getUncompressedAddress($compressed = false, $derPubKey = null)
}
else
{
if($compressed) {
if($compressed === true) {
$address = $this->getPubKey();
}
else {
Expand Down Expand Up @@ -911,7 +915,7 @@ public function getSignatureHashPoints($hash, $nonce = null)
$R = '0' . $R;
}

return array('R' => $R, 'S' => $S);
return ['R' => $R, 'S' => $S];
}

/***
Expand Down Expand Up @@ -1062,7 +1066,7 @@ public function getPubKeyWithRS($flag, $R, $S, $hash)
if($y === null)
return null;

$Rpt = array('x' => $x, 'y' => $y);
$Rpt = ['x' => $x, 'y' => $y];

//step 1.6.1
//calculate r^-1 (S*Rpt - eG)
Expand Down Expand Up @@ -1096,7 +1100,7 @@ public function getPubKeyWithRS($flag, $R, $S, $hash)
if($this->checkSignaturePoints($derPubKey, $R, $S, $hash))
return $derPubKey;
else
return false;
return null;

}

Expand Down

0 comments on commit 77e6c0c

Please sign in to comment.