1. Home
  2. Explore
  3. Computer Science
  4. Number Theory for Cryptography

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Number Theory for Cryptography

Speaker 1Speaker 2

Number Theory for Cryptography

9:37
0:00-9:37
  1. Maria

    Hello, everyone, and welcome. Today we're shifting our focus to number theory, but specifically to the parts that are useful in computer science and especially cryptography: modular arithmetic, properties of prime numbers, algorithms for greatest common divisors, and modular exponentiation.

  2. Hope

    Our main focus in this conversation will be modular arithmetic and efficient algorithms for carrying it out. Modular exponentiation is particularly important because it appears as a central operation in cryptographic algorithms.

  3. Maria

    Let's begin with the basic definition. Suppose a is an integer and N is a positive integer called the modulus. Then a modulo N is the remainder r obtained when a is divided by N, with the requirement that r lies between 0 and N minus 1. Formally, we can write a as qN plus r for some integer q.

  4. Hope

    That range requirement matters, especially for negative values. For example, 5 modulo 4 is 1. Also, negative 11 modulo 3 is 1, not negative 2, because the standard remainder must be in the range 0 through 2.

  5. Maria

    A useful way to visualize this is with a clock having N positions, representing the possible remainders. For a positive number, start at zero and move around the clock in the usual direction. For a negative number, move in the opposite, or anticlockwise, direction. With 5 modulo 4, the possible positions are 0, 1, 2, and 3, and five steps from zero land at 1.

  6. Hope

    Now let's introduce congruence. We say that a is congruent to b modulo N when a and b produce the same remainder upon division by N. The notation is a congruent to b modulo N, and it means that a and b are equivalent with respect to their remainders.

  7. Maria

    There is an equivalent and often very useful test: a is congruent to b modulo N if and only if a minus b is completely divisible by N. So congruence can be checked either by comparing remainders or by checking whether their difference is a multiple of the modulus.

  8. Hope

    Modular arithmetic also has convenient rules for addition, subtraction, and multiplication. If a modulo N is a-prime and b modulo N is b-prime, then you can compute a plus b modulo N by computing a-prime plus b-prime modulo N. The same idea works for subtraction and multiplication.

  9. Maria

    In other words, you may reduce the operands first, perform the operation on those smaller values, and reduce again if necessary. This gives the same result as performing the operation on the original values and reducing only at the end.

  10. Hope

    For addition, the reason is straightforward. Write a as a quotient times N plus a-prime, and b as another quotient times N plus b-prime. When you add them, all the terms containing N contribute remainder zero, so the remainder is determined entirely by a-prime plus b-prime.

  11. Maria

    The same reasoning applies to multiplication and subtraction. For a concrete example, suppose you want to compute the product of two large numbers modulo 100, and their remainders modulo 100 are 28 and 1. Instead of multiplying the original large numbers, multiply 28 by 1. The result is 28, already less than 100, so the desired remainder is 28.

  12. Hope

    This ability to reduce early is extremely useful computationally. It prevents intermediate values from becoming unnecessarily large, while preserving the final modular result.

  13. Maria

    Division is different, though. You cannot automatically say that a divided by b modulo N equals a-prime divided by b-prime modulo N. In fact, a divided by b might not even be an integer, and modular arithmetic as defined here deals with integer remainders.

  14. Hope

    For example, if a is 3, b is 5, and N is 4, then 3 divided by 1 modulo 4 is well defined and equals 3. But 3 divided by 5 is 0.6, and taking 0.6 modulo 4 is not defined in this setting.

  15. Maria

    This also explains why cancellation is not always valid. From ac congruent to bc modulo N, you cannot automatically cancel c and conclude that a is congruent to b modulo N. Cancellation can work under certain additional conditions, but it is not a general rule of modular arithmetic.

  16. Hope

    With those rules established, let's turn to algorithms. We care about addition, subtraction, multiplication, and modular exponentiation. The inputs a, b, and N are treated as n-bit integers, so efficiency is measured by the number of bit operations as a function of n.

  17. Maria

    The desired algorithms should use a number of operations that is polynomial in n. We do not want algorithms whose running time is exponential, or even subexponential, in the number of bits needed to represent the inputs.

  18. Hope

    Addition and subtraction can be performed bit by bit, while keeping track of carries or borrows, and then the result can be reduced modulo N. Multiplication can also be carried out using a polynomial number of bit operations, followed by reduction. Therefore, modular addition, subtraction, and multiplication all have polynomial-time algorithms.

  19. Maria

    Modular exponentiation is more challenging. Suppose we want to compute a raised to the power b modulo N. The naive approach repeatedly multiplies by a, reducing modulo N after each multiplication. That requires roughly b modular multiplications.

  20. Hope

    At first glance, that might sound polynomial, but b is itself an n-bit number. Its magnitude can be as large as 2 to the power n. So performing b operations can require exponentially many operations in n. For instance, when the inputs have around 1024 bits, the exponent can be enormously large, making the naive method completely impractical.

  21. Maria

    The efficient alternative is the square-and-multiply method. The key idea is to write the exponent in binary. If the exponent has a 1 in a particular binary position, we include the corresponding power of a; if it has a 0, we skip that power.

  22. Hope

    Take exponent 53 as an example. Its binary representation is 110101. Reading the positions of the 1 bits shows that 53 equals 32 plus 16 plus 4 plus 1. Therefore, a to the 53 can be formed by multiplying together a to the 32, a to the 16, a to the 4, and a to the 1.

  23. Maria

    We obtain those powers efficiently by repeated squaring. Start with a to the first power. Square it to get a to the second, square again to get a to the fourth, then continue to obtain a to the eighth, sixteenth, and thirty-second powers. Every squaring and every multiplication is performed modulo N.

  24. Hope

    At the same time, maintain an accumulator for the final result. Process the exponent bits from least significant to most significant. Whenever the current bit is 1, multiply the accumulator by the current power of a modulo N; whenever the bit is 0, skip that accumulation.

  25. Maria

    For 53, you would accumulate the first power, skip the second power because the corresponding bit is zero, accumulate the fourth power, skip the eighth, and accumulate the sixteenth and thirty-second powers. The accumulator eventually contains a to the 53 modulo N.

  26. Hope

    That is why the method is called square and multiply: every iteration includes a compulsory squaring to generate the next power, while multiplication into the accumulator is conditional on the current exponent bit being 1.

  27. Maria

    The improvement is dramatic. The naive method for a to the 53 would require 52 modular multiplications, whereas square and multiply needs only 9 in the example described. More generally, if the exponent has n bits, there are at most n compulsory squarings and, in the worst case, n accumulator multiplications, for a total of at most about 2n modular multiplications.

  28. Hope

    So an exponential number of repeated multiplications is replaced by a number of modular multiplications that is linear in the number of exponent bits. Since each modular multiplication itself takes a polynomial number of bit operations, modular exponentiation becomes polynomial-time.

  29. Maria

    The pseudocode follows directly from this idea. Initialize the accumulator y to 1 and the current power z to a. While the exponent x is not zero, check whether x is odd. If it is, update y by multiplying it by z modulo N. Then divide x by 2, using the quotient, square z modulo N, and continue.

  30. Hope

    Checking whether x is odd reveals the next binary bit, starting with the least significant bit. Dividing x by 2 removes that bit and exposes the next one. Meanwhile, squaring z moves from a to a squared, then to a to the fourth, a to the eighth, and so on.

  31. Maria

    So the algorithm simultaneously extracts the exponent's binary representation and builds the needed powers of the base. It avoids constructing the enormous value of a to the b directly, reducing modulo N after every multiplication.

  32. Hope

    To summarize, modular arithmetic represents an integer by its remainder in the range zero through N minus 1. Congruence means having the same remainder, and it is equivalent to saying that the difference is divisible by N. Addition, subtraction, and multiplication allow reduction before the operation, but division and cancellation require caution.

  33. Maria

    And for modular exponentiation, the naive repeated-multiplication algorithm is exponential in the bit length of the exponent. The square-and-multiply method uses binary representation, repeated squaring, and conditional accumulation to reduce the work to a polynomial number of operations, which is why it is so important in cryptography.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent