8000 Fix parseCompressedPublicKey method by TiMESPLiNTER · Pull Request #10 · ionux/phactor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix parseCompressedPublicKey method #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
8000 Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,18 @@ public function parseUncompressedPublicKey($pubkey)
*/
public function parseCompressedPublicKey($pubkey, $returnHex = false)
{
$parsedVal = (substr($pubkey, 0, 2) == '02' || substr($pubkey, 0, 2) == '03') ? $this->prepAndClean($this->calcYfromX(substr($pubkey, 2), substr($pubkey, 0, 2))) : $this->prepAndClean($pubkey);

return ($returnHex === false) ? $parsedVal : $this->encodeHex($parsedVal);
$prefix = substr($pubkey, 0, 2);

if ($prefix !== '02' && $prefix !== '03') {
return $this->prepAndClean($pubkey);
}

$pointX = substr($pubkey, 2);
$pointY = substr($this->calcYfromX($pointX, $prefix), 2);

$parsedValue = $this->prepAndClean($pointX . $pointY);

return ($returnHex === false) ? $parsedValue : $this->encodeHex($parsedValue);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ public function calcYfromX($x_coord, $compressed_bit)
$c = $this->Subtract($this->decodeHex($this->addHexPrefix($compressed_bit)), '2');
$a = $this->Modulo($this->Add($this->PowMod($x, '3', $this->p), '7'), $this->p);
$y = $this->PowMod($a, $this->Divide($this->Add($this->p, '1'), '4'), $this->p);
$y = ($this->Modulo($y, '2') != $c) ? $this->decodeHex($this->Modulo($this->Multiply('-1', $y), $this->p)) : $this->decodeHex($y);

return ($this->Modulo($y, '2') != $c) ? $this->decodeHex($this->Modulo($this->Multiply('-1', $y), $this->p)) : $this->decodeHex($y);
return $this->encodeHex($y);
} catch (\Exception $e) {
throw $e;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/PointTest.php
< 6562 deferred-diff-lines class="awaiting-highlight" data-url="/ionux/phactor/diffs/d91a2cc442ba566393157a21a2ae1877e7e278cc..0a74aa7f479a79fff2a9f50f29ad7769ef98885c?base_sha=d91a2cc442ba566393157a21a2ae1877e7e278cc&whitespace_ignored=false">
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,11 @@ public function testCalcYfromX()

$x = '0x9970eb90fc5fb04d5c63201e6124b4c77957ea79ebc129f07dca7b30da54230c';
$y = '0x6ee4fc6f37788608b16a0cb81968176c1b2042c65515d4d4053a1d390e62433c';
$y_dec = '50158996713404002186761311168531259625706655107568015898414438196527281357628';
$compressed_bit = '0x02';

$calc_y = $this->mock->calcYfromX($x, $compressed_bit);

$this->assertNotNull($calc_y);
$this->assertEquals($y_dec, $calc_y);
$this->assertEquals($y, $calc_y);
}
}
0