Speed

A Public Key Is a Point

The public key of a P-256 key is 65 bytes, the byte 04 and two 32-byte numbers x and y; the pair lies on the curve y squared equals x cubed minus 3x plus b, which a program with 256-bit numbers cannot check but an oracle with Python integers can.

The question

The last lesson said that a public key is a point and that its uncompressed form has 65 bytes on P-256. What are those bytes? A point on a curve has two coordinates, x and y, and the first byte says how they are written. If the bytes are split in the right places, what is left is two numbers, and the pair must satisfy the curve's equation.

The key in this lesson, p256-a, is test material, made for these lessons. Only its public point is used here.

Work it by hand

P-256 is the curve y squared equal to x cubed minus 3x plus b, taken modulo a 256-bit prime p. The numbers p and b are public parameters of the curve; OpenSSL prints them with ecparam -name prime256v1 -param_enc explicit -text -noout, with a equal to p minus 3 and b equal to 5ac635d8 and so on up to 604b. The 65 bytes of a public key split like this:

  • Byte 0 is 04, which marks the uncompressed form.
  • Bytes 1 to 32 are x, 32 bytes.
  • Bytes 33 to 64 are y, 32 bytes.

Check by squares: y squared and x cubed minus 3x plus b must leave the same remainder modulo p. Those numbers have 512 and 768 bits before the remainder is taken; the integers of this page's programs stop at 2 to the power 53, so the check cannot run in a program here. The oracle of the lesson runs it with Python integers for every P-256 point of the lesson, and the points pass.

Run it

The program takes the point as hexadecimal bytes. Line 1 counts the bytes, line 2 reads the first byte and lines 3 and 4 cut the two coordinates out with hex_slice(hex, at, count), a start byte and a number of bytes.

For p256-a the point has 65 bytes, the first byte is 4, and x starts 3c86a171, y starts 8014142a. The program returns the length, 65.

The point has 65 bytes and its first byte is 4. The 32 bytes of x start 3c 86 a1 71 and the 32 bytes of y start 80 14 14 2a.

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

let size = hex_len(point);
let prefix = hex_u8(point, 0);
let x = hex_slice(point, 1, 32);
let y = hex_slice(point, 33, 32);
lmcode_return_put("prefix", prefix);
lmcode_return_put("x", x);
lmcode_return_put("y", y);
let answer = size;
{
  "answer": 65,
  "prefix": 4,
  "x": "3c86a17186bb783374b70ae420c31571755eedf239c01a36862b1064d1bd0685",
  "y": "8014142a978a023c22861b0d63e2e47bdbf0740ffd8619cdf560221d416cf2e9"
}

You can change the point, but the program cuts at fixed places. A point of the 112-bit curve, 29 bytes, stops it with "bytes 1 to 32 are past the end of the data", and a compressed P-256 point of 33 bytes stops it with "bytes 33 to 64 are past the end of the data". An empty point stops it at the first read.

Your turn

The program below should return the two coordinates of an uncompressed public point given as hexadecimal text in point: a list of two texts, x and then y, each as the hexadecimal digits of that coordinate. The point may belong to any curve: the coordinates are as long as the field needs. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on four others.

The 65 bytes of the public point, starting with 04, shown 16 to a line.
let n = str_len(point);
let answer = str_list();
answer.add(str_substr(point, 0, n / 2));
answer.add(str_substr(point, n / 2, n / 2));
{
  "answer": [
    "04d6cc6a18661d451dedb6f608aa69e37b97adcc5061aa4165210697ba1603779",
    "1fd708c103a78cfd7c18891e7119ea790fb00113b58c6886a1bf0e5b25157c9a7"
  ]
}

point is an uncompressed elliptic-curve public point as hexadecimal text: the byte 04, then x, then y, with x and y of the same length. Return a list of two texts: the hexadecimal digits of x, then those of y, without the 04. The point may be 65 bytes (P-256) or 29 bytes (secp112r1).

Read the result carefully

The program only cuts bytes. It does not check that the numbers satisfy a curve equation, and a text of the right length with any digits passes. A real program that receives a point from someone else has to check it, and this page does not show how.

The page also does not show how the point is computed from the private number, the compressed form with the byte 02 or 03, or the choice of the curve. The keys are test material. The curve secp112r1, whose point is 29 bytes, is far too small to use.

Recap

  • An uncompressed public point is the byte 04, then x, then y.
  • On P-256 each coordinate has 32 bytes; in general the coordinate length is the point length minus 1, divided by 2.
  • The pair satisfies the curve equation; here the oracle checks it, not the program.
  • Cutting the bytes does not validate the point.