Skip to content

Instantly share code, notes, and snippets.

@xqus
Created December 19, 2012 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xqus/4336999 to your computer and use it in GitHub Desktop.
Save xqus/4336999 to your computer and use it in GitHub Desktop.
Signing of hash using ECDSA in PHP
<?php
// configure the ECC lib
if (!defined('USE_EXT')) {
if (extension_loaded('gmp')) {
define('USE_EXT', 'GMP');
} else if(extension_loaded('bcmath')) {
define('USE_EXT', 'BCMATH');
} else {
die('GMP or bcmath required. (GMP is faster).');
}
}
define('MAX_BASE', 256); // so we can use bcmath_Utils::bin2bc with "base256"
$secp256k1 = new CurveFp(
'115792089237316195423570985008687907853269984665640564039457584007908834671663',
'0', '7');
$secp256k1_G = new Point($secp256k1,
'55066263022277343669578718895168534326250603453777594175500187360389116729240',
'32670510020758816978083085130507043184471273380659243275938904335757337482424',
'115792089237316195423570985008687907852837564279074904382605163141518161494337');
/* The hash I want to sign. */
$txHash = '9302bda273a887cb40c13e02a50b4071a31fd3aae3ae04021b0b843dd61ad18e';
/* Private key to sign the hash with. */
$privKey = '18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725';
/* This is my first problem. I assume using $secp256k1_G is correct, and I assume I have
to convert the private key to something that the point class constructer will understand.
But what? */
$pubKey = new PublicKey($secp256k1_G, new Point('do something with $privKey I assume'));
/* Next problem, what is this secret multiplier supposed to be? */
$prvKey = new PrivateKey($pubKey, $someSecretMultiplier);
/* Last problem (I hope), $k is supposed to be between 0 and the order of the finite field the curve
is defined on. I'm not a mathematician, so I'm not sure what to do there. */
$k = rand(0, 8);
$signature = $prvKey->sign($txHash, $k);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment