Speed

Modulus Size in Bits

The size of an RSA key is the number of bits of its modulus, counted without leading zeros; the lesson counts them from the hexadecimal text of a modulus.

The question

People say "a 2048-bit key" or "a 512-bit key". What is counted? The modulus n, written in binary without leading zeros. The same number written in hexadecimal has four bits per digit, but not every hexadecimal number of 128 digits has 512 bits: the first digit decides how many bits it adds.

A tool that prints a modulus may add leading zeros, and a text may be copied with or without them. The size of the key must not change with the way the number is written. How is the bit length of a hexadecimal number counted?

Work it by hand

The bit length of a number is the number of binary digits after the leading zeros are dropped. Each hexadecimal digit is four binary digits, 0 to 15 as 0000 to 1111. So drop the leading zero digits, and then the first digit that is left counts only as many bits as it needs.

  • A first digit of 1 needs 1 bit, 2 or 3 need 2 bits, 4 to 7 need 3 bits, and 8 to F need 4 bits.
  • Every digit after the first adds 4 bits.
  • Example 3A7: the first digit is 3, which is 11 in binary: 2 bits, plus 4 and 4, in all 10 bits.
  • Example 00B2C: drop the two zeros; the first digit is B, which is 1011: 4 bits, plus 4 and 4, in all 12 bits.

A modulus of 512 bits whose first digit is 8 to F has 128 hexadecimal digits, and a 2048-bit modulus has 512. A key file written with a first digit of 1 to 7 has the same number of digits but one to three bits fewer.

Run it

The program takes the modulus as hexadecimal bytes. Line 1 counts the bytes and line 2 reads the first byte. The loop on lines 4 to 6 finds how many bits that byte needs: it sets top_bits to k plus 1 for each k from 0 to 7 for which the byte is at least 2 to the power k. Line 7 adds 8 bits for each of the other bytes.

For the test key carol the program finds 64 bytes, a first byte of 177 that needs 8 bits, and so 512 bits. The program returns the byte count, 64; the bit length is in the extra value bits.

The modulus has 64 bytes. The first byte is 177 and needs 8 bits, so the modulus has 8 times 63 plus 8, which is 512 bits.

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

let size = hex_len(modulus);
let top = hex_u8(modulus, 0);
let top_bits = 0;
for (k in range(0, 8)) {
  if (top >= math_pow(2, k)) { top_bits = k + 1; }
}
let bits = 8 * (size - 1) + top_bits;
lmcode_return_put("top_byte", top);
lmcode_return_put("top_byte_bits", top_bits);
lmcode_return_put("bits", bits);
let answer = size;
{
  "answer": 64,
  "top_byte": 177,
  "top_byte_bits": 8,
  "bits": 512
}

You can change the modulus, but this program counts bytes, so it expects a first byte that is not zero. With leading zero bytes, as in 00000fff, line 7 reports 24 bits for a number that has 12. An empty modulus stops with "byte 0 is past the end of the data". An odd number of digits is cut to whole bytes: fff is read as the one byte ff, and a single digit stops the run like an empty text.

Your turn

The program below is meant to return the bit length of a number written as hexadecimal text: digits 0 to 9 and a to f in either case, leading zeros allowed, no 0x. The text may be any length up to 2048 characters. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on six others.

The hexadecimal number 0100 and the whole number that the shown program returns.
let text = str_lower(number);
let n = str_len(text);
let first = 0;
for (i in range(0, n)) {
  if (first == 0) {
    if (str_substr(text, i, 1) != "0") { first = i + 1; }
  }
}
let answer = 0;
if (first > 0) { answer = 4 * (n - first + 1); }
{
  "answer": 12
}

number is a hexadecimal number as text: digits 0 to 9 and a to f in either case, leading zeros allowed, no 0x, up to 2048 characters. Return its bit length: the number of binary digits it has after the leading zeros are dropped; return 0 for the number 0.

Read the result carefully

The number of bits of the modulus is a property of the number, and it is the usual name for the size of a key. It does not say how hard the key is to attack, and this page gives no such figure. A 512-bit key is the smallest OpenSSL makes; it is far too small to use, and 2048-bit keys are the size the committed test key of this book has for comparison.

The candidates trust their input: a character that is not a hexadecimal digit does not stop them, and the result for such a text means nothing. An empty text or a text of zeros has no first digit and returns 0, the bit length of the number 0. The page uses test material only; no key is made here.

Recap

  • The size of an RSA key is the bit length of its modulus n.
  • The bit length counts the binary digits that remain after the leading zeros are dropped.
  • A 512-bit modulus has 128 hexadecimal digits when its first digit is 8 to F, a 2048-bit one has 512.
  • The size does not change with leading zeros or with the case of the letters.