Why does the length of a bitcoin key vary? Why aren't they all the same length?
-
some interesting bitcoin trivia - the first time a compressed public key is used to spend on the blockchain is in tx 94af4607627535f9b2968bd1fbbf67be101971d682023d6a3b64d8caeb448870, which spends 0.01337 btc. clearly someone was showing off lol – mulllhausen Nov 11 '14 at 12:45
-
the compressed pubkey was 0271f1e54016d623897fa9b973c2f084376ff61651ee5b71eef0d98a8be56a75cc btw – mulllhausen Nov 11 '14 at 12:55
2 Answers
The length of a key doesn't vary. Private keys are always 32 bytes, and public keys are always 65 bytes (or 33 bytes for a compressed public key). Public key hashes are always 20 bytes.
The length of addresses vary because in Bitcoin's base58 encoding, leading zero bytes are expressed as a single "1". Other bytes require more than one character in order to be expressed in base58.

- 8,994
- 42
- 37
-
Do you have private keys and public keys reversed in the above answer? It seems to me that private keys are longer. – shoeless joe Nov 20 '11 at 19:34
-
2@shoelessjoe I don't think so. Public keys are definitely 64 bytes plus the leading 0x04 byte, and I assume that the 256 in "secp256k1" refers to the size of the private key in bits. – theymos Nov 21 '11 at 01:33
-
You said "Private keys are always 32 bytes, and public keys are always 65 bytes." That makes public keys longer than private keys? – shoeless joe Nov 29 '11 at 18:13
-
@shoelessjoe Yes. I think this is the case with all public-key crypto, actually. – theymos Nov 30 '11 at 22:12
-
In fact, we could move to compressed public keys, which are 257 bits long (33 bytes). Slightly longer than the private key indeed, but almost nothing. – Pieter Wuille Dec 04 '11 at 14:56
-
-
I have seen examples of private keys larger than 32 bytes. How does qt truncate them? As an example: https://bitcoin.stackexchange.com/a/22881/2075. I could not get any test vectors to pass. The op claims to have used QT in the tests. Can you explain how QT would work with larger than 32 bytes keys? – Jus12 Dec 26 '17 at 09:44
Private keys are 256 bit numbers
Public keys are a pair of X,Y coordinates. Each coordinate is a 256 bit number. BUT for every X coordinate there are only two possible Y coordinates (one positive, one negative) so you can store a public key as just the X coordinate (256 bits) and the sign of the Y coordinate (1 bit) and the proper Y coordinate can then be calculated from the X coordinate and the sign.
So private key = 256 bits, public key = 2*256=512 bits OR 256+1=257 bits
Then there is a small amount of formatting overhead of a few bits.
Then these are encoded.