Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #103 from sambohler/master
Browse files Browse the repository at this point in the history
#65 Removed dead code, updated arrays to Points, finished writing functi...
  • Loading branch information
JoshuaEstes committed Nov 10, 2014
2 parents 1ee2f2e + f9fc476 commit d35e35c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 88 deletions.
97 changes: 10 additions & 87 deletions src/Bitpay/Bitauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ public function getPublicKeyFromPrivateKey(\Bitpay\PrivateKey $privateKey)
}

/**
* @return Sin
* @return SinKey
*/
public function getSinFromPublicKey(\Bitpay\PublicKey $publicKey)
{
// TODO
//return Sin::fromPubKey($publicKey);
return $publicKey->getSin();
}

/**
Expand All @@ -78,80 +77,6 @@ public function getSinFromPublicKey(\Bitpay\PublicKey $publicKey)
public function sign($data, \Bitpay\PrivateKey $privateKey)
{
return $privateKey->sign($data);

//if (!ctype_xdigit($privateKey->getHex())) {
// throw new \Exception('The private key must be in hex format.');
//}

//if (empty($data)) {
// throw new \Exception('You did not provide any data to sign.');
//}

//$e = Util::decodeHex(hash('sha256', $data));

//do {
// if (substr(strtolower($privateKey->getHex()), 0, 2) != '0x') {
// $d = '0x'.$privateKey->getHex();
// } else {
// $d = $privateKey->getHex();
// }

// $k = SecureRandom::generateRandom(32);

// $k_hex = '0x'.strtolower(bin2hex($k));
// $n_hex = '0x'.Secp256k1::N;
// $a_hex = '0x'.Secp256k1::A;
// $p_hex = '0x'.Secp256k1::P;

// $Gx = '0x'.substr(Secp256k1::G, 0, 62);
// $Gy = '0x'.substr(Secp256k1::G, 64, 62);

// $P = new Point($Gx, $Gy);

// // Calculate a new curve point from Q=k*G (x1,y1)
// $R = Gmp::doubleAndAdd($k_hex, $P);

// $Rx_hex = Util::encodeHex($R->getX());
// $Ry_hex = Util::encodeHex($R->getY());

// while (strlen($Rx_hex) < 64) {
// $Rx_hex = '0'.$Rx_hex;
// }

// while (strlen($Ry_hex) < 64) {
// $Ry_hex = '0'.$Ry_hex;
// }

// // r = x1 mod n
// $r = gmp_strval(gmp_mod('0x'.$Rx_hex, $n_hex));

// // s = k^-1 * (e+d*r) mod n
// $edr = gmp_add($e, gmp_mul($d, $r));
// $invk = gmp_invert($k_hex, $n_hex);
// $kedr = gmp_mul($invk, $edr);
// $s = gmp_strval(gmp_mod($kedr, $n_hex));

// // The signature is the pair (r,s)
// $signature = array(
// 'r' => Util::encodeHex($r),
// 's' => Util::encodeHex($s),
// );

// while (strlen($signature['r']) < 64) {
// $signature['r'] = '0'.$signature['r'];
// }

// while (strlen($signature['s']) < 64) {
// $signature['s'] = '0'.$signature['s'];
// }
//} while (gmp_cmp($r, '0') <= 0 || gmp_cmp($s, '0') <= 0);

//$sig = array(
// 'sig_rs' => $signature,
// 'sig_hex' => self::serializeSig($signature['r'], $signature['s']),
//);

//return $sig['sig_hex']['seq'];
}

/**
Expand Down Expand Up @@ -222,14 +147,12 @@ public static function serializeSig($r, $s)
/**
* Verifies a previously generated ECDSA signature
*
* @param string
* @param string
* @param array
* @param array
* @param string
* @return boolean
*/
public function verifySignature($contract, $publicKey, $signature, $Q, $data)
public function verifySignature($signature, $Q, $data)
{
$Gx = '0x'.substr(Secp256k1::G, 0, 62);
$Gy = '0x'.substr(Secp256k1::G, 64, 62);
Expand Down Expand Up @@ -262,20 +185,20 @@ public function verifySignature($contract, $publicKey, $signature, $Q, $data)
// calculate u2 = r*w (mod n)
$u2 = gmp_mod(gmp_mul($r, $w), $n_hex);

$P = array('x' => $Gx, 'y' => $Gy);
$P = new Point($Gx, $Gy);

$Qx = '0x'.substr($Q, 2, 64);
$Qy = '0x'.substr($Q, 66, 64);

$Q = array('x' => $Qx, 'y' => $Qy);
$Q = new Point($Qx, $Qy);

// Get new point Z(x1,y1) = (u1 * G) + (u2 * Q)
$Za = Gmp::doubleAndAdd($u1, $P, $p_hex, $a_hex);
$Zb = Gmp::doubleAndAdd($u2, $Q, $p_hex, $a_hex);
$Z = Gmp::gmpPointAdd($Za, $Zb, $p_hex, $a_hex);
$Za = Gmp::doubleAndAdd($u1, $P);
$Zb = Gmp::doubleAndAdd($u2, $Q);
$Z = Gmp::gmpPointAdd($Za, $Zb);

$Zx_hex = Util::encodeHex($Z['x']);
$Zy_hex = Util::encodeHex($Z['y']);
$Zx_hex = Util::encodeHex($Z->getX());
$Zy_hex = Util::encodeHex($Z->getY());

while (strlen($Zx_hex) < 64) {
$Zx_hex = '0'.$Zx_hex;
Expand Down
6 changes: 5 additions & 1 deletion src/Bitpay/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ public function setPrivateKey(PrivateKey $privateKey)
*
* @return Bitpay\PublicKey
*/
public function generate()
public function generate(PrivateKey $privateKey = null)
{
if ($privateKey instanceof PrivateKey) {
$this->setPrivateKey($privateKey);
}

if (!empty($this->hex)) {
return $this;
}
Expand Down

0 comments on commit d35e35c

Please sign in to comment.