Generating Bitcoin Public and Private Keys

0

I managed to write the below code which will create public and private keys

Private Key

$input = "satoshinakamoto";
$sha256 = hash("sha256", ($input));
echo "<strong>Private Key: </strong>".$sha256."<br/>";

Public Key

    $step1 = hexStringToByteString("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6");
    echo "<p><strong>Step 1:</strong> ".$step1."</p>";
    $step2 = hash("sha256", $step1);
    echo "<p><strong>Step 2: </strong>".$step2."</p>";
    $step3 = hash('ripemd160', hexStringToByteString($step2));
    echo "<p><strong>Step 3: </strong>".$step3."</p>";
    $step4 = '00'.$step3;
    echo "<p><strong>Step 4: </strong>".$step4."</p>";
    $step5 = hash("sha256", hexStringToByteString($step4));
    echo "<p><strong>Step 5: </strong>".$step5."</p>";
    $step6 = hash("sha256",hexStringToByteString($step5));
    echo "<p><strong>Step 6: </strong>".$step6."</p>";
    $step7 = substr($step6,0,8);
    echo "<p><strong>Step 7: </strong>".$step7."</p>";
    $step8 = $step4.$step7;
    echo "<p><strong>Step 8: </strong>".$step8."</p>";
    $step9 = bc_hexdec($step8);
    echo "<p><strong>Step 9: </strong>".$step9."</p>";
    $step10 = bc_base58_encode($step9);
    echo "<p><strong>Step 10: </strong>1".$step10."</p>";
    return $step10;

To generate public key I took reference from https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses

In that link they provided Step by Step process and I followed the same and if I input from Step 1 in the link then I am able to convert the code to public key but I am not sure how to convert Step 0 to Step 1

i.e.

From:

18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725

To

0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6

Document says

Take the corresponding public key generated with it (65 bytes, 1 byte 0x04, 32 bytes corresponding to X coordinate, 32 bytes corresponding to Y coordinate)

What does that mean? How can I generate this value from Private Key.

Edit 1

This question says https://stackoverflow.com/questions/17672696/generating-bitcoin-address-from-ecdsa-public-key

Joining the Elliptic Public Key and I still don't understand what are they.

lock

Posted 2016-02-24T01:21:52.853

Reputation: 215

5DO NOT GENERATE PRIVATE KEYS BY HASHING STRINGS. This type of system is known as a brainwallet and it is broken. Any Bitcoins deposited in a wallet generated this way WILL be stolen. – None – 2016-02-24T04:57:27.450

@duskwuff - I know that, I am just using a string as example but I am looking for algorithm how the wallets are generated. I know about the vulnerabilities of brainwallet. I just need to know the formula for how that X and Y Coordinates are generated.lock 2016-02-24T06:43:47.620

Answers

1

Bitcoin uses point multiplication on the Elliptic Curve secp256k1 to generate a public key from a private key. Basically, this curve has a defined Generator point G, and a method for 'adding' two points together in a way to get a new point (EC Point Addition).

Your private key is just a number, aka a Scalar, so to get your public key you just add the generator point to itself privkey number of times - basically scalar multiplication on the Elliptic curve. At the end, you'll end up with a point on the curve, with an X and Y coordinate, and this point is your public key. Those two coordinates of your point are what is referred to by the document you quoted, using 32 bytes for each coordinate plus a prefix.

The 0x04 prefix used is to denote the fact this is an uncompressed private key. You can also form a compressed version, by just using the X coordinate with either 0x02 or 0x03 prefix, because the Elliptic curve has only 2 values for Y coordinate for each X coordinate (one positive and one negative), so it's trivial to recover the Y coordinate if you know the X coordinate and the sign (based on your choice of prefix).

MeshCollider

Posted 2016-02-24T01:21:52.853

Reputation: 8 735

-1

your priv = 18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725
X = priv x 79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798
Y = priv x 483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8

https://en.bitcoin.it/wiki/Secp256k1

amaclin

Posted 2016-02-24T01:21:52.853

Reputation: 5 763

3Downvoted. You make it sound like you need to multiply the individual X and Y coordates of the generator with the private key as numbers. This is not correct; you need to perform an elliptic curve multiplication, which requires a repeated geometric construction.Pieter Wuille 2016-06-13T11:23:43.343