How Is A Private Key Generated Bitcoin

  1. What Is Private Key In Bitcoin
  2. Advantages Of Private Key Encryption
  1. The key, quite literally, is a private key Bitcoin is best known as a peer-to-peer electronic cash system – one that is decentralised and eliminates the need for a middle-man. As a result, users trading or mining Bitcoin must secure their funds themselves, typically with the use of a cryptocurrency wallet.
  2. Feb 26, 2018 This article will explain at a high-level Private and Public Key Cryptography used in Bitcoin and it’s unique security feature. We will be looking at how Public Keys are generated, why this is.

A private key is just a number picked at random.

The private key is used to generate the public key through an irreversible process.

In the case of Bitcoin the Private key is turned into the Public key through Elliptic Curve Cryptography or ECC for short.

A Bitcoin address is a 160-bit hash of the public portion of a public/private ECDSA keypair. Using public-key cryptography, you can 'sign' data with your private key and anyone who knows your public key can verify that the signature is valid. A new keypair is generated for each receiving address (with newer HD wallets, this is done. Jan 29, 2020 Private Key Format Bitcoin. Here are some of the most popular private key formats of Bitcoin that are used in different types of wallets nowadays: #1. Raw Private Key. A private key (in bitcoin, i.e. ECDSA SECP256K1) is a 32 byte number between 0x1 and 0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4140.

If you lose your private key you can't access your Bitcoins, and if someone else finds it out they can steal them.

A Private key is just a long number and in the case of a Bitcoin it is a number between 1 & 1.158x 10^77. In practice this number is generated using a secure random number generator that is then fed into the SHA-256 hashing algorithm. The SHA-256 hashing algorithm takes a string of numbers and outputs a 256 bit number which then has to be checked to see if it is less than 1.158x 10^77.

How Is A Private Key Generated Bitcoin


When creating your private key most software will do this under the hood, but if you choose certain methods on your own such as 1x10^77 or other certain obvious numbers and then hash via the SHA-256 – hackers can create rainbow tables and match to your private key, public key and address. If you do this and choose a particular phrase you are reducing the entropy or chaos of your choice! Always opt for randomness just to be safe even if the chances are very small.


The point of using Elliptic curve cryptography is to find a method whereby you can create a Bitcoin Public Key easily Bitcoin Private Key but not the reverse – i.e. find the Bitcoin Private Key from its Public Key.

This is due to the discrete logarithm problem for elliptic curves, where the best mathematical solutions to break elliptic curve cryptography have to take step proportional to 2n/2 , where n is the length of the number which the Bitcoin key has to below (1.158x 10^77) (also known as the field size the curve is based on for modular arithmetic).

In practice this means that a hacker would need 21^28 calculations to break elliptic curve cryptography based on Bitcoins specifications. Using a million CPU’s this would take about 260 billion times the age of the universe – a heck of a long time.


The public bitcoin key that is produced by elliptic curve cryptography is actually a point with x and y coordinates. To see why have a look at our in depth guide to Elliptic curve cryptography.

Lately I’ve been working my way through Mastering Bitcoin, implementing as many of the examples in the book in Elixir as I can. Office 2007 pro product key generator.

I’ve been amazed at how well Elixir has fared with implementing the algorithms involved in working with Bitcoin keys and addresses. Elixir ships with all the tools required to generate a cryptographically secure private key and transform it into a public address string.

Let’s walk through the process step by step and build our our own Elixir module to generate private keys and public addresses.

What are Private Keys and Public Addresses?

A Bitcoin private key is really just a random two hundred fifty six bit number. As the name implies, this number is intended to be kept private.

From each private key, a public-facing Bitcoin address can be generated. Bitcoin can be sent to this public address by anyone in the world. However, only the keeper of the private key can produce a signature that allows them to access the Bitcoin stored there.

Let’s use Elixir to generate a cryptographically secure private key and then generate its most basic corresponding public address so we can receive some Bitcoin!

Pulling a Private Key Out of Thin Air

As I mentioned earlier, a Bitcoin private key is really just a random two hundred and fifty six bit number. In other words, a private key can be any number between 0 and 2^256.

However, not all random numbers are created equally. We need to be sure that we’re generating our random number from a cryptographically secure source of entropy. Thankfully, Elixir exposes Erlang’s :crypto.strong_rand_bytes/1 function which lets us easily generate a list of truly random bytes.

Let’s use :crypto.strong_rand_bytes/1 as the basis for our private key generator. We’ll start by creating a new PrivateKey module and a generate/0 function that takes no arguments:

Inside our generate/0 function, we’ll request 32 random bytes (or 256 bits) from :crypto.strong_rand_bytes/1:

This gives us a random set of 32 bytes that, when viewed as an unsigned integer, ranges between 0 and 2^256-1.

Unfortunately, we’re not quite done.

Validating our Private Key

To ensure that our private key is difficult to guess, the Standards for Efficient Cryptography Group recommends that we pick a private key between the number 1 and a number slightly smaller than 1.158e77:

We can add this validation check fairly easily by adding the SECG-provided upper bound as an attribute to our PrivateKey module:

Next, we’ll add a valid?/1 function to our module that returns true if the provided secret key falls within this range, and false if it does not:

Before we pass our private key into our valid?/1 function, we’ll need to convert it from a thirty two byte binary into an unsigned integer. Let’s add a third valid?/1 function head that does just that:

We’ll finish off our validation by passing our generated private key into our new valid?/1 function. If the key is valid, we’ll return it. Otherwise, we’ll generate a new private key and try again:

Now we can call PrivateKey.generate to generate a new Bitcoin private key!

From Private Key to Public Key …

The most basic process for turning a Bitcoin private key into a sharable public address involves three basic steps. The first step is to transform our private key into a public key with the help of elliptic curve cryptography.

We’ll start by adding a new to_public_key/1 function to our PrivateKey module:

In our to_public_key/1 function, we’ll use Erlang’s :crypto.generate_key function to sign our private_key using an elliptic curve. We’ll specifically use the :secp256k1 curve:

We’re using the elliptic curve key generation as a trapdoor function to ensure our private key’s secrecy. It’s easy for us to generate our public key from our private key, but reversing the computation and generating our private key from our public key is nearly impossible.

The :crypto.generate_key function returns a two-element tuple. The first element in this tuple is our Bitcoin public key. We’ll pull it out using Elixir’s elem/1 function:

The returned value is a sixty five byte binary representing our public key!

… Public Key to Public Hash …

Once we have our public key in memory, our next step in transforming it into a public address is to hash it. This gives us what’s called the “public hash” of our public key.

Let’s make a new function, to_public_hash/1 that takes our private_key as an argument:

We’ll start the hashing process by turning our private_key into a public key with a call to to_public_key:

Next, we pipe our public key through two hashing functions: SHA-256, followed by RIPEMD-160:

Bitcoin uses the RIPEMD-160 hashing algorithm because it produces a short hash. The intermediate SHA-256 hashing is used to prevent insecurities through unexpected interactions between our elliptic curve signing algorithm and the RIPEMD algorithm.

What Is Private Key In Bitcoin

In this example, hash/1 is a helper function that wraps Erlang’s :crypto.hash.

Flipping the arguments to :crypto.hash in this way lets us easily pipe our data through the hash/1 helper.

… And Public Hash to Public Address

Lastly, we can convert our public hash into a full-fledged Bitcoin address by Base58Check encoding the hash with a version byte corresponding to the network where we’re using the address.

Let’s add a to_public_address/2 function to our PrivateKey module:

The to_public_address/2 function takes a private_key and a version byte as its arguments. The version defaults to <<0x00>>, indicating that this address will be used on the live Bitcoin network.

To create a Bitcoin address, we start by converting our private_key into a public hash with a call to to_public_hash/1:

All that’s left to do is Base58Check encode the resulting hash with the provided version byte:

After laying the groundwork, the final pieces of the puzzle effortlessly fall into place.

Putting Our Creation to Use

Online

Now that we can generate cryptographically secure private keys and transform them into publishable public addresses, we’re in business.

Literally!

Let’s generate a new private key, transform it into its corresponding public address, and try out on the Bitcoin testnet. We’ll start by generating our private key:

This gives us a thirty two byte binary. If we wanted, we could Base58Check encode this with a testnet version byte of 0xEF. This is known as the “Wallet Import Format”, or WIF, of our Bitcoin private key:

Advantages Of Private Key Encryption

As its name suggests, converting our private key into a WIF allows us to easily import it into most Bitcoin wallet software:

Next, let’s convert our private key into a testnet public address using a version byte of 0x6F:

Now that we have our public address, let’s find a testnet faucet and send a few tBTC to our newly generated address! After initiating the transaction with our faucet, we should see our Bitcoin arrive at our address on either a blockchain explorer, or within our wallet software.

Victory!

Final Thoughts

Elixir, thanks to its Erlang heritage, ships with a wealth of tools that make this kind of hashing, signing, and byte mashing a walk in the park.

I encourage you to check our the PrivateKey module on Github to get a better feel for the simplicity of the code we wrote today. Overall, I’m very happy with the result.

If you enjoyed this article, I highly recommend you check out the Mastering Bitcoin book. If you really enjoyed this article, feel free to send a few Bitcoin to this address I generated using our new PrivateKey module:

Stay tuned for more Bitcoin-related content as I work my way through Mastering Bitcoin!

Comments are closed.