key size
what is the total size of saving the public and private key ?
In the current version of the code, it's exactly 32 kilobytes. However, that's much larger than it needs to be.
The size of the keypair file is currently over-approximated:
size_t get_keypair_size(int nbits)
{
size_t aes_blklen = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128);
return nbits * aes_blklen;
}
For some crazy reason, this code assumes that an entire AES128 block is needed to encrypt each bit in the private key. It ignores the space requirement of the public key, but that's a hidden bug since it over-allocated things to begin with.
This is a hack, but here's something that works a bit better:
size_t get_keypair_size(int nbits)
{
size_t aes_blklen = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128);
// format overhead * {pub,priv}key (2 * bits)
size_t keypair_nbits = 4 * (2 * nbits);
size_t rem = keypair_nbits % aes_blklen;
return (keypair_nbits + rem) / 8;
}
This lets us save the keypair using just 2 kilobytes.
With a better understanding of the format we could reduce the space requirement even more, but I'm going to mark this as closed with commit cab14b.