For extended private key it is 512 bits but for extended public key how it is 512? It will be 264+256 right? And why in the book mastering bitcoin they said extended key is either 512 bits or 513 bits
Recommend reading BIP-0032, and specifically serialization format.
The private key is a scalar value of 32 bytes (256 bits).
The public key is a point on the elliptic curve, a point is an XY value. This is called an uncompressed public key. It is prefixed with
0x04. An uncompressed public key is 65 bytes.
For efficiency we simply represent the public key as an X value since the Y value can be derived easily. This is called a compressed public key. However for any X value there could be two Y values, as such we need to store a value to represent the sign of Y (or evenness). For even Y value we prefix
0x02, for odd Y value we prefix
0x03. A compressed public key is 33 bytes (see Chapter 4 in Mastering Bitcoin).
For HD, we have a private HD key and a public HD key. The serialization format from BIP-32 is as follows.
- 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, 0x04358394 private)
- 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 derived keys, ....
- 4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
- 4 bytes: child number. This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (0x00000000 if master key)
- 32 bytes: the chain code
- 33 bytes: the public key or private key data (serP(K) for public keys, 0x00 || ser256(k) for private keys)
Typically the important part (for recovery purposes and generating other keys) is the chain code (32 bytes = 256 bits) and the public / private key (33 bytes).
The book says 512 or 513 bits because the extended private key is
256 chain code bits || 256 private key bits (512), and the extended public key is
256 chain code bits || 1 evenness bit || 256 public key bits (513).
Since computers storage works in bytes, the 1 bit for evenness is represented as a whole byte.