Search

Breaking the unbreakable? Shor’s Algorithm

illustration

Overview

Secondary School

Physics, Mathematics, Computer Science

Quantum Computing

English

Overview

Keywords: encryption, decryption, modulo arithmetic, plaintext, ciphertext, modular exponentiation, Toffoli gate, Hadamard gate, CNOT gate, NOT gate, Quantum Fourier Transform, cryptography
Age group: 16-18 years
Required knowledge/skills: understanding of superposition and the action of various quantum logic gates
Time frame: 120 minutes (2 x 60 minute lessons)

Author: Sam Robbins (UK)

Content

Lesson plan
Student exercises and teacher notes
Period finding
Exercise 1: Finding the period of an encrypted RSA number
How the period helps break RSA
Quantum computers to the rescue
Practical example
Exercise 2: Build a circuit for another modular exponential function
Breaking the code
Exercise 3: Verify that we have broken RSA in this case
Afterword

Summary

This is the second of two lessons which aim to show how RSA encryption works and how quantum computers may, in the future, be able to break it. This lesson focusses on how RSA may be broken by a quantum computer, showing students how there is a weakness in RSA involving the periodicity of the modular exponentiation function and how Shor’s Algorithm can crack the code by finding such a period.

Teaser Icon Quantum Algorithms

Lesson plan
 

Timing
in minutes
Lesson phasesActivityResources
0–20Exercise 1Exposition, followed by exercise to find the period of the powers of 62 (mod 77).Worksheet, Python Interpreter (an online editor should be sufficient)
20–40Exposition of how period can be used to break RSATeacher explains how period can be found from ciphertext and used to break RSA.Worksheet, Python Interpreter 
40–50Exposition of modular exponentiation function circuitTeacher models how modular exponentiation function is constructed.Worksheet, Quantum composer (e.g. IBM quantum composer)
50–90Exercise 2Students calculate prime factors of numbers shown and articulate the method used to do so.Worksheet, Quantum composer 
90–100Exposition of the full circuitry for Shor's AlgorithmTeacher shows the full construction of the circuitry and how the period is deduced from the results.Worksheet, Quantum composer 
100–120Exercise 3Students use the period to find d’ and demonstrate that they have broken RSA in the case of pq=15Worksheet, Calculator

Student exercises and teacher notes

In this lesson, we will look out how a quantum algorithm devised by Peter Shor in 1994 can be used to crack the RSA encryption method. Although quantum computers are not yet powerful enough to apply this in a large-scale implementation, Shor’s algorithm is nevertheless driving a revolution in cryptography to ensure that the encryption routines the internet depends on remain secure into the future.

Download the worksheet

The following tasks are available as worksheet for download as pdf and docx.

The tasks are also available as an interactive worksheet with embedded Python environment here.

Period finding

Shor’s algorithm exploits a weakness in the RSA system due to the fact that modular exponentiation is periodic, i.e. if we raise our plaintext (or ciphertext) by successive powers under modulo arithmetic, eventually we will return to our original number and the pattern will repeat itself.
For example, raising 2 to successive powers modulo 5 gives:

21 2 ( mod 5 )  
22 4 ( mod 5 )  
23 = 8 3 ( mod 5 )  
24 = 16 1 ( mod 5 )  
25 = 32 2 ( mod 5 )

So the pattern goes: 2, 4, 3, 1, 2, 4, 3, 1, and continues in this cycle. The first power where we get 1 ( mod 5 ) tells us the period of the cycle, in this case 4.

Exercise 1: finding the period of an encrypted RSA number

For this exercise we will use an RSA system with pq = 77 (p = 7, q = 11) and encryption key = 7. We will use the code for modular exponentiation from the unit RSA: the unbreakble code:

def powerMod(a, b, c):  # raises a to the power of b modulo c
    res = a % c
    for i in range(0, b - 1):
        res = a * res % c
    return res

pq = 77
e = 7
a = 13
print(powerMod(a,e,pg))

If we start with a plaintext number of 13, this should return the encrypted number 62. The strategy for breaking RSA will be to start from an encrypted number (which in real-life we would be able to intercept) and work out its period.
Use the modular exponentiation function to work out what the period of 62 is ( mod 77 ) , i.e. what is the first power of 62 that is equal to 1 ( mod 77 ) .

The student should write a simple loop to iterate through the powers of 62 (see below for example listing). Note that the cycle terminates at 1 because 13 and 77 have a highest common factor of 1. 

Answer:

The period is 10 and the powers are [62, 71, 13, 36, 76, 15, 6, 64, 41, 1].

Example code listing:

def powerMod(a, b, c):  # raises a to the power of b modulo c
    res = a % c
    for i in range(0, b - 1):
        res = a * res % c
    return res

pq = 77
a = 62

for i in range(1,11):
    print(powerMod(a,i,pg))

 

How the period helps break RSA

If we were to encrypt the number 16 using this key (e = 7), we would have a ciphertext of 58. 58 has a period of 15. The full list of powers of 58 is [58, 53, 71, 37, 67, 36, 9, 60, 15, 23, 25, 64, 16, 4, 1]. (Note: the period ends at 1 because 16 and 77 have a highest common factor of 1.) There are two important mathematical facts concerning the period that help us to break RSA:

  1. 16 (which was the original plaintext) is a member of the set of powers of 58.
  2. 16 also has a period of 15 and running the code for 16 shows that the set of numbers generated is exactly the same as 58, only in a different order: [16, 25, 15, 9, 67, 71, 58, 4, 64, 23, 60, 36, 37, 53, 1].

So if we can work out the period of a piece of ciphertext, we will also know the period of the piece of plaintext that generated it.
Let the period be r. We can now find a number d’, such that:

e d 1 ( mod r )

In our example, e = 7 and r = 15, so we could choose d’ to be 13, as 7 13 = 91 1 ( mod 15 ) .
If we now raise our ciphertext to the power of d’ it will return the original plaintext directly and we will have done this only with access to information in the public domain (e and pq), so we will have broken the code! This is because:

Ciphertext  y x e ( mod p q )  
So                y d x e d ( mod p q )
But   x e d x 1 + k r ( mod p q )
And   x k r 1 ( mod p q ) because any multiple power of the period will return 1
So    x e d x ( mod p q )
 

Using our code with the ciphertext 58 and d’ = 13 we can verify that:

58 13 16 ( mod 77 )
We have managed to find out the plaintext using a method that doesn’t rely on knowing what it was originally!

Unfortunately, finding the period is not easy using classical methods. As we have seen in these examples, although modular exponentiation is periodic, the powers follow no clear pattern (unlike a periodic function such as a sine curve). This means we have no alternative but to either guess which power first returns 1 or evaluate all successive powers until we find it. For numbers of the size used in real-life implementations of RSA, this cannot be done by a classical computer in a practical timeframe. 

The example shown on the worksheet can be modelled to the class using the code listing below. The key ideas to demonstrate are that a plaintext character and its corresponding ciphertext have the same period, and that the set of powers of both contain the same numbers.

def powerMod(a, b, c):  # raises a to the power of b modulo c
    res = a % c
    for i in range(0, b - 1):
        res = a * res % c
    return res

pq = 77
plainText = 16
e = 7 cipherText = powerMod(plainText,e,pq) print("PlainText: "+str(plainText))
print("")
print("CipherText: "+str(cipherText))
print("")
print("Powers of Cipher Text")
print("") for i in range(1,16): print (str(cipherText)+"^"+str(i)+" = "+str(powerMod(cipherText,i,pq))+" (mod "+str(pq)+")")

The mathematical proof of how the period can be used to break RSA is shown in the worksheet. Note that d’ may be found using the Extended Euclidean Algorithm in the same way that the holder of the private key constructs the decryption key in the normal implementation of RSA. The important difference here - and the key learning point for the students -  is that a member of the public who intercepted this message would be able to work out d’ from only publicly available information: the ciphertext which they have intercepted in transmission; the period, r, which they have calculated from the ciphertext; and the public key (e, pq) which is freely available to everyone. They would therefore not need to factorise pq if they had an efficient method for calculating r.

Quantum computers to the rescue!

For a classical computer, finding the period of modular exponentiation is still an intractable problem. However, there is an ingenious method that can be employed by a quantum computer that can extract information about periodic functions very quickly by use of a mathematical method known as the Quantum Fourier Transform. Shor’s algorithm works using the following steps:

  1. We encode the modular exponentiation function using quantum logic gates on a number of qubits sufficient to cover the size of the RSA key (pq);
  2. We use Hadamard gates on all qubits to create an equally-weighted superposition to which the function is applied (this is the same strategy that we have seen in all our quantum algorithms);
  3. We perform measurements on the outputs of this function, which collapses the input qubits to a superposition of states which are spaced out by the period of the function;
  4. We apply the Quantum Fourier Transform to these inputs and measure again to return information about the period itself. 

It should be noted that step 4. will only give us a probabilistic guess at what the period is, but this information would already be sufficient, in a real-world context, to enable us to break the code in a realistic timeframe (by checking the vastly reduced number of high-probability guesses on a classical computer).

Practical example

In the following example, we will build a modular exponentiation function for the key pq = 15, using successive powers of the number 7. The table on the left shows the calculation for each power of 7 in turn. The table on the right shows these numbers converted into binary, showing the mapping we will attempt to implement from input qubits to output qubits:

table of a modular exponentiation function

We now use four qubits, | x 0 , | x 1 , | x 2 , | x 3 , to represent the input binary number with digits x 3 x 2 x 1 x 0 and four qubits, | x 4 , | x 5 , | x 6 , | x 7 , to represent the output binary number with digits x 7 x 6 x 5 x 4 .
We then use combinations of NOT, CNOT and Toffoli (CCNOT) gates to map the inputs to the outputs. 

First, we note that the output is determined by the final two digits of the input in all cases. This means that qubits | x 2 and | x 3 do not need to be connected to the output qubits at all and can be ignored.

We can then construct our circuit by considering how | x 0 and | x 1 affect each of the output qubits in turn.

Let’s start by looking at | x 7 (the largest digit of the output number) and extracting the relevant digits from the table above to see how it is affected by | x 0 and | x 1 :

Tabelle
x1x0x7
010
100
111
000

We can see that it is only | x 1 when both | x 0 and | x 1 are | 1 , and | 0 in all other cases (in the classical world, this would be an AND operation). This is the same as the action of a Toffoli gate (which is a CNOT gate that switches the state of a target qubit when the state of two control gates is | 1 ). Using a quantum composer, this can be represented as follows:

representation using a quantum composer

For | x 6 Tabelle

x1x0x6
011
101
111
000

The output is | 1 when either | x 0 or | x 1 is | 1 , or both are | 1 (a classical OR operation). We can use a CNOT gate on each of the two input qubits to switch the output qubit to | 1 in the case where only one of the inputs is | 1 . However, if both inputs are | 1 , the effect of the two CNOTs will be to switch the output qubit back to | 0 . To deal with this case, we need to add an additional Toffoli gate, to give the following:

representation using a quantum composer

For | x 5 :

Tabelle

x1x0x5
011
100
110
000

The output is | 1 only when | x 0 is | 1 and | x 1 is | 0 . To achieve this, we first use a CNOT gate connecting | x 0 to | x 5 and then a Toffoli gate connecting | x 0 and | x 1 to | x 5 to correct the output back to | 0 in the case where both inputs are | 1 :

representation using a quantum composer

Finally, for | x 4 :

Tabelle

x1x0x4
011
100
111
001

We observe that the output | x 4 is | 1 in all cases except when only | x 1 is | 1 . This can be created by applying a CNOT gate connecting | x 1 to | x 4 and a Toffoli gate connecting | x 0 and | x 1 to | x 4 , followed a single-qubit NOT gate on | x 4 :

representation using a quantum composer

This circuit will now replicate the action of 7 x ( mod 15 ) for all input values of x. We will then be able to apply further quantum functions to extract information about the period of the function.

The worksheet provides an example of how to build the modular exponentiation function in a quantum computer. It is quite involved, so it may be best to model the steps using a quantum composer.

Note that the qubits are mapped from right to left in the table of digits, so q[0] represents the units column, q[1] the 2s column, q[2] the 4s column and q[3] the 8s column. The output qubits are similarly arranged.

Exercise 2: Build a circuit for another modular exponential function

Following the example provided above, build the circuit for 8 x ( mod 15 ) . Start by completing the following tables and use the mappings of the combinations of bits to derive the circuitry of the qubits (noting that | x 0 and | x 1 are again the only two qubits to determine the output):

The students should then use the example provided to fill in the table provided and construct the circuit for 8 x ( mod 15 ) . Note that the circuit they end up drawing is just a rearrangement of the one shown for 7 x ( mod 15 ) .

Answer:

solution table for modular exponential function 

Circuit:

circuit for modular exponential function

Breaking the code

Now we have a way of building the function using quantum logic gates, we can extract the information we need by applying four further steps:

  1. We apply Hadamards to all of the input qubits. This allows us to create a superposition state of all the outputs of the modular exponentiation function we have chosen.
  2. Apply a measurement to all of the output qubits. This will now leave our input qubits in a particular superposition of states which are spaced by the period of the modular exponentiation function.
  3. Apply the Quantum Fourier Transform to generate a state whose probability density function is determined by the period of the original modular exponentiation function.
  4. Apply a measurement to the input qubits and run the algorithm multiple times, then use the outcomes with highest frequency to deduce what the period is from knowledge of the probability density function.

The full circuit which does this is shown here for the case of 7 x ( mod 15 ) . For ease of viewing this is broken into two parts. First the Hadamards, modular exponentiation function and measurement of the output qubits:

full circuit for 7^x (mod 15)

Then this is followed by the Quantum Fourier Transform and the final measurement of the input qubits:

Quantum Fourier Transform

The Quantum Fourier Transform itself is performed by a combination of Hadamard gates and conditional rotation gates as set out above.

The probability density function for the final state of the input qubits will have peaks at multiples 2 n / r , where n is the number of qubits and r is the period of the modular exponentiation function. So by looking at the positions of the peaks from our running of the algorithm we can derive an estimate for the period r .
Running the circuit for 7 x ( mod 15 ) 1024 times yielded the following results:

graph for Shor's Algorithm results

We can clearly see from this experiment that the peaks occur at multiples of 4. Given that 2 n = 2 4 = 16 , we can deduce from our experiment that the period of our function is also 4. In this simple example, this was clear from the mapping exercise we did at an earlier stage. However, with larger keys than this we would be in a situation where we would not necessarily be able to observe the period of the function when we input it into our circuit. In these cases, Shor’s algorithm would provide us with the only way of deducing this information in a practically-computable period of time.

The theory behind the Quantum Fourier Transform and how it is constructed using logic gates is not included on the worksheet (as it goes well beyond the level of high school mathematics). N. David Mermin’s “Quantum Computer Science” provides a good overview, should students wish to read further.
It is not intended that the student implements the full circuit shown for Shor’s Algorithm. However, this may be done as an extension exercise. The student would need full access to a quantum computer to be able to run the experiment shown.

Exercise 3: Verify that we have broken RSA in this case

Using the key pq = 15, plaintext = 7, and encryption key = 3:

  1. Compute the ciphertext
  2. Use the period (= 4) to find d’. (Recall d’ is chosen such that e d 1 ( mod r ) )
  3. Check that raising the ciphertext to d’ returns the original plaintext

Congratulations! You have broken RSA encryption!

Answers:

  1. Ciphertext = 13; 
  2. d’ = 3; 
  3.  13 3 7 ( mod15 ) as required.
     

Afterword

Shor’s Algorithm has not yet been implemented in a way that might threaten any real-world implementation of RSA. Our example relied heavily on the fact that the modular exponentiation function was easy to construct (which was a consequence of it having a short period). In a real-world example, there would be no simple pattern for mapping the input qubits to the output qubits, and so far no-one has managed to invent a mechanism for building the circuitry for a general modular exponentiation function. However, the prospect that Shor’s Algorithm might one day be able to break RSA has already been sufficient to drive the development of a whole new field of “Post-Quantum Cryptography”, designed to insure against the eventuality that Shor’s Algorithm does one day become practicable.

Close search