Speed

Named Curves and Sizes

An elliptic-curve key names its curve, holds a private number and a public point, and the size of that point follows from the size of the curve's field; one command prints a P-256 test key, and a program counts the point bytes for any field size.

The question

An RSA key is a modulus and two exponents. What is an elliptic-curve key? It does not hold a modulus of its own: it names a curve, a set of public parameters that many keys share, and it holds a private number d and a public point Q, the curve's base point multiplied by d. How big is the public point, and how does that depend on the curve?

The key in this lesson, p256-signer, is test material made for these lessons on the curve P-256. Its private number is as secret as any private key; here it is public on purpose.

Work it by hand

A curve over a prime field works with numbers of a fixed size. P-256 has a 256-bit field, 32 bytes. A public point is two such numbers, x and y, and a first byte that says how the point is written: RFC 5480 section 2.2 says the first octet is 04 for the uncompressed form and 02 or 03 for the compressed form.

  • Private number d: one number of up to 32 bytes, shown as 32 bytes.
  • Public point, uncompressed: 04, then x of 32 bytes, then y of 32 bytes. That is 1 plus 2 times 32, which is 65 bytes.
  • In general the point has 1 plus 2 times the field size in bytes, and a field of bits bits needs bits divided by 8, rounded up, bytes. A 521-bit field needs 66 bytes.

The dump below prints 16 bytes to a line. So the 32 bytes of d fill 2 lines and the 65 bytes of the point fill 5 lines, four of 16 bytes and one of a single byte, the 04 comes first.

Run it

The command ec -in in/key.txt -noout -text reads the key and prints its parts: a first line with the size of the field, the private number after priv:, the point after pub:, and two lines that name the curve.

For p256-signer the output has 12 lines: the first starts Private-Key: (256 bit field, priv: has 2 lines, pub: has 5, and the last two lines read ASN1 OID: prime256v1 and NIST CURVE: P-256. The first byte of the public point is 04.

The 12 lines of the dump: a first line that starts Private-Key: (256 bit field, then priv: with two lines of bytes, pub: with five lines of bytes starting 04, and the lines ASN1 OID: prime256v1 and NIST CURVE: P-256.

Public test material, generated for this lesson. Never use it for anything real.

# Reads the private key in in/key.txt.
# Prints the numbers of the key.
ec -in in/key.txt -noout -text
{
  "answer": "Private-Key: (256 bit field, 128 bit security level)\npriv:\n    c8:ef:a8:2e:b0:56:b7:ae:bc:5d:37:36:c9:f1:7d:23:\n    4c:db:ae:4f:3b:bc:44:a9:7f:cc:a4:ff:92:a1:89:61\npub:\n    04:0a:ac:e3:89:56:60:fc:7c:1b:8f:c7:10:90:1d:23:\n    d1:53:76:83:eb:b0:3a:3b:e8:c9:a3:43:ac:43:67:ae:\n    05:dd:6a:d4:38:d8:ae:bc:e1:ad:67:53:06:e6:39:bc:\n    dc:86:34:c1:d8:06:c4:cd:47:57:66:ac:1f:aa:f8:42:\n    9a\nASN1 OID: prime256v1\nNIST CURVE: P-256"
}

You can change the key. The key of the 112-bit curve secp112r1, also test material of this book, prints priv: with 14 bytes and pub: with 29 bytes and the OID secp112r1. A text that is not a key stops the command with "unable to load Key", and a public key with "unable to load Key: unsupported". An RSA key does not stop it: it prints the numbers of that key.

Your turn

The program below should return the length in bytes of the uncompressed public point of a key on a curve with a prime field of bits bits: the prefix byte and two coordinates, each rounded up to whole bytes. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on five others.

The field size 163 bits and the whole number of bytes that the shown program returns.
let prefix = 1;
let half = bits / 8;
let answer = prefix + 2 * half;
{
  "answer": 41
}

A curve has a prime field of bits bits (the size of its numbers). Return the length in bytes of an uncompressed public point on it: one prefix byte and the two coordinates x and y, each written in as many whole bytes as the field size needs.

Read the result carefully

The size of a point says nothing about how strong a key is, and this page gives no such figure. OpenSSL prints a security level in its own output; that is its output and not a claim of this page. The curve secp112r1 has a 112-bit field and is far too small to use; it appears because its numbers are short.

The page shows keys made once for these lessons and committed with them. It does not show how a curve is chosen, how a received point is checked, or how keys are stored. The compressed form of a point, which keeps only x and one bit of y, is the subject of the last lesson.

Recap

  • An elliptic-curve key names a curve and holds a private number and a public point.
  • The uncompressed public point is 04, x and y: 1 plus 2 times the field size in bytes.
  • 29 bytes for a 112-bit field and 65 for a 256-bit field.
  • ec -text prints a key's parts, 16 bytes to a line.