This is an example of a bitcoin address: 14NWDXkQwcGN1Pd9fboL8npVynD5SfyJAE
CONCEPT:- Elliptic Curves
Each address corresponds to a point on the elliptic curve secp256k1 (y^3=x^2+7 mod p) where prime p = 115792089237316195423570985008687907853269984665640564039457584007908834671663
The point for the above address as a decimal point on a graph:
to check this is on the curve we can do:
So how do we get from the point to the address?
CONCEPT: HEX, ripemd150, sha256, base58
First lets convert it to hexadecimal:
>>> x = 91519190036866233587583752863966343541024156557754641198598352460350806215674
>>> x2 = x.to_bytes(32, ‘big’).hex().encode()
>>> y = 18110675123485594228049867696927871008716109164646265340106239137304742587157
>>> y2 = y.to_bytes(32, ‘big’).hex().encode()
>>> print(“(“,x2, “, “, y2, “)”)
( b’ca5606a1e820e7a2f6bb3ab090e8ade7b04a7e0b5909a68dda2744ae3b8ecbfa’ , b’280a47639c811134d648e8ee8096c33b41611be509ebca837fbda10baaa1eb15′ )
- Uncompressed SEC Format <0x04><x coordinate in 32 byte big endian hex><y coordinate in 32 byte big endian hex>
>>> r = “04ca5606a1e820e7a2f6bb3ab090e8ade7b04a7e0b5909a68dda2744ae3b8ecbfa280a47639c811134d648e8ee8096c33b41611be509ebca837fbda10baaa1eb15”
>>> rr = bytes.fromhex(r)
- Compressed SEC format <0x02 or 0x03><x coordinate in 32 byte big endian hex>
For every value (x, y) on the Elliptic curve the value (x, p-y) is also on the curve so we can encode the y value as even or odd
- WIF Format this is described in this diagram –
i) take the uncompressed sec format address
ii) sha256 the long address, followed by ripemd160 -> 20 bytes
>>> hashlib.new(‘ripemd160’, hashlib.sha256(rr).digest()).digest().hex()
iii) append 00 prefix byte to this -> 21 bytes
iv) sha256 these 21 bytes twice and output the first 4 bytes of the output -> 4 bytes
>>> rrr = “0024f98038e995ee03c4178bccaff1652223eba473”
v) append these 4 bytes to the output of step iii) above
vi) convert this to base58 encoding to get the address
>>> import base58
>>> rrrrr = “0024f98038e995ee03c4178bccaff1652223eba473669e533b”
How do we pick a point for our address?
CONCEPT: Public Key Cryptography
The point on the elliptic curve is the public key anyone can use to make payments to (for example p2pk transactions in early bitcoin). The above address works in the same way as the public key but it is easier to use and more secure* (for example p2pkh transactions that came a bit later in bitcoin)
To receive bitcoins you don’t select a the public key, instead you select the private key and generate a public key from the private key.
Once you have selected a private key (random integer between 1 and 2^256) you generate a public key through scalar multiplication of the private key with a specific point G (the generator point) on the elliptic curve as defined by secp256k1’s definition. This generates a new point (the public key) on the curve from which you create your address as above.
Given a public key, they only way to know the private key is to check every possible private key (number between 1 and 2^256), which takes an impossibly long time. This is what secures bitcoin (CONCEPT: Elliptic curve cryptography)
The most important requirement for a private key is that is is random over the whole set of possible keys. Then if someone does try to search the entire key space they will not be able to spend your bitcoins because it would just take too long.
CONCEPT: Random numbers
Random number generation is a bit tricky for computers and humans to do. The two main issues are that random number generators on computers are often deterministic (predictable given seed values), or contain hidden patterns.
Good of private key generators are:
1. bitaddress.org – this takes various sources of entropy such as time, screen size, and mouse position as you wiggle it around and generates a private key using that
2. openssl – uses /dev/urandom in unix or CryptGenRandom() on Windows. These use system events such as packet times, process id’s etc as entropy and are generally well tested as unpredictable
e.g. (there’s probably a better way to do this but it worked on windows so i transferred it to mac)
i) mac$ openssl ecparam -genkey -name secp256k1 -out /var/tmp/delete_me -outform PEM
ii) mac$ openssl ec -inform PEM -in /var/tmp/delete_me -text -out /var/tmp/delete_me2
read EC key
writing EC key
iii) Private-Key: (256 bit)
ASN1 OID: secp256k1
—–BEGIN EC PRIVATE KEY—–
—–END EC PRIVATE KEY—–
iv) the bit after priv: is the private key in hex, and the bit after pub: is the public key point on the graph. Notice that it starts with 04 as the prefix to show it is sec uncompressed format
Some bad examples to generate private key (including the above public key address):
1. Brain wallets – these take a string of text and hash it to create a private key.
For example the private key for the above address is the hash of “bitcoin is awesome” (sha256(“bitcoin is awesome”)=0x23d4a09295be678b21a5f1dceae1f634a69c1b41775f680ebf8165266471401b which is the private key)
2. Insecure random number generators – using the java class SecureRandom to generate random numbers for private keys https://www.theregister.co.uk/2013/08/12/android_bug_batters_bitcoin_wallets/
1. How many addresses are there?
2. How the blockchain works?
3. What are transactions?
4. How are bitcoins created?
5. Given a private key how do you generate public key/address?