Cryptography

RSA Encryption and Quantum Computing

Published on September 11, 2026

RSA Encryption and Quantum Computing

We are going to crack the RSA by finding the period of the ciphertext. Let m be the message, c the ciphertext, e the encoding number and N=pq such that

c=memodNc=m^e \mod N

From Period Finding and the RSA post, the period of a ciphertext is defined as the smallest number r such that

cr1modNc^r \equiv 1 \mod N

First we prepare n=2n0n=2n_0 QBits at the state 0\left\vert 0\right\rangle where n0n_0 is the number of bits of N=pqN=pq . The number of bits can be computed using the formula:

log2(N)\lceil \log_2(N) \rceil

We also prepare n0n_0 QBits for the output register.

So initially we have the following setup:

000n times000n0 times\underbrace{\left|00\ldots 0\right\rangle}_{n\text{ times}} \otimes \underbrace{\left|00\ldots0\right\rangle}_{n_0\text{ times}}

The output register will contain the output of the operator:

Ufx0=xf(x)\mathbf{U}_f \left|x\right\rangle\left|0\right\rangle = \left|x\right\rangle\left|f(x)\right\rangle

where

f(x)=cxmodNf(x) = c^x \mod N

We now apply the Hadarmard gate to the input register and apply the operator Uf\mathbf{U}_f:

12n/2x=02n1xf(x)\displaystyle \frac{1}{2^{n/2}} \sum_{x=0}^{2^n-1} \left|x\right\rangle\left|f(x)\right\rangle

The Hadamard gates transform the state into a superposition of all possible values of the period while the operator Uf\mathbf{U}_f transforms the output QBits as superposition of all possible values of cxmodNc^x\mod N.

Next we make a measurement of the output QBits. This will give us a number f0f_0 which corresponds to all states whose value of x\left\vert x\right\rangle is of the form x0+krx_0+kr since

cx0+krcx0ckrmodNcx0(cr)kmodNcx0modN\begin{array}{rl} c^{x_0+kr} & \equiv c^{x_0}c^{kr} \mod N \\ & \equiv c^{x_0}(c^{r})^k \mod N \\ & \equiv c^{x_0} \mod N \end{array}

since (cr)k1modN(c^{r})^k \equiv 1 \mod N by definition of a period.

Therefore, after the measurement, the input state is now

Ψ=1mk=0m1x0+kr\displaystyle \left|\Psi\right\rangle = \frac{1}{\sqrt{m}} \sum_{k=0}^{m-1} \left|x_0 + kr\right\rangle

where m is the smallest integer such that x0+mr2nx_0 + mr \ge 2^{n}.

We will then apply the quantum Fourier transform to state Ψ\left\vert \Psi\right\rangle . The Quantum Fourier Transform is defined by its action on an arbitrary x\left\vert x\right\rangle

UFTx=12n/2y=02n1e2πixy/2ny\displaystyle \mathbf{U}_{\mathbf{FT}}\left|x\right\rangle = \frac{1}{2^{n/2}} \sum_{y=0}^{2^n-1} e^{2\pi ixy/2^n}\left|y\right\rangle

Applying the Fourier Transform to the state Ψ\left\vert \Psi\right\rangle ,

UFT1mk=0m1x0+kr=1mk=0m1UFTx0+kr=1mk=0m112n/2y=02n1e2πi(x0+kr)y/2ny=1mk=0m112n/2y=02n1e2πix0y/2ne2πikry/2ny=y=02n1e2πix0y/2n12nmk=0m1e2πikry/2nprobability amplitudey\begin{array}{rl} \displaystyle \mathbf{U}_{\mathbf{FT}} \frac{1}{\sqrt{m}} \sum_{k=0}^{m-1}\left|x_0 + kr\right\rangle &= \displaystyle \frac{1}{\sqrt{m}} \sum_{k=0}^{m-1}\mathbf{U}_{\mathbf{FT}} \left|x_0 + kr\right\rangle\\ &= \displaystyle \frac{1}{\sqrt{m}} \sum_{k=0}^{m-1} \frac{1}{2^{n/2}} \sum_{y=0}^{2^n-1} e^{2\pi i(x_0+kr)y/2^n}\left|y\right\rangle\\ &= \displaystyle \frac{1}{\sqrt{m}} \sum_{k=0}^{m-1} \frac{1}{2^{n/2}} \sum_{y=0}^{2^n-1} e^{2\pi ix_0y/2^n}e^{2\pi ikry/2^n}\left|y\right\rangle\\ &= \displaystyle \sum_{y=0}^{2^n-1} \underbrace{e^{2\pi ix_0y/2^n} \frac{1}{\sqrt{2^n m}} \sum_{k=0}^{m-1} e^{2\pi ikry/2^n}}_{\text{probability amplitude}}\left|y\right\rangle \end{array}

Now if we make a measurement of this new state, the probability of getting a value of y is the probability amplitude multiplied by it's complex conjugate:

[e2πix0y/2n12nmk=0m1e2πikry/2n][e2πix0y/2n12nmk=0m1e2πikry/2n]\displaystyle \Big[\underbrace{e^{2\pi ix_0y/2^n} } \frac{1}{\sqrt{2^n m}} \sum_{k=0}^{m-1} e^{2\pi ikry/2^n}\Big]\Big[\underbrace{e^{-2\pi ix_0y/2^n} } \frac{1}{\sqrt{2^n m}} \sum_{k=0}^{m-1} e^{-2\pi ikry/2^n}\Big]

The factors in underbrace evaluates to 1 when multiplied and we're left with

12nmk=0m1e2πikry/2n2\displaystyle \frac{1}{2^n m}\Big| \underbrace{\sum_{k=0}^{m-1} e^{2\pi ikry/2^n}}\Big|^2

Using the formula

eiθ=cos(θ)+isin(θ)e^{i\theta} = \cos(\theta) + i\sin(\theta)

we can write the summation in underbrace as

(k=0m1cos(2πkry/2n))2+(k=0m1sin(2πkry/2n))2\displaystyle \Big(\sum_{k=0}^{m-1} \cos(2\pi kry/2^n)\Big)^2 + \Big(\sum_{k=0}^{m-1} \sin(2\pi kry/2^n)\Big)^2

We'd like to know at what value of y the expression above is maximum. We can use the following formula to simplify above summations:

k=0m1cos(2πfk)=cos(πf(m1))sin(πfm)sin(πf)k=0m1sin(2πfk)=sin(πf(m1))sin(πfm)sin(πf)\displaystyle \sum_{k=0}^{m-1} \cos(2\pi fk) = \frac{\cos(\pi f(m-1)) \sin(\pi fm)}{\sin(\pi f)}\\ \displaystyle \sum_{k=0}^{m-1} \sin(2\pi fk) = \frac{\sin(\pi f(m-1)) \sin(\pi fm)}{\sin(\pi f)}

If we let

f=ry/2nf = ry/2^n

this reduces the summations to:

(k=0m1cos(2πkry/2n))2+(k=0m1sin(2πkry/2n))2\displaystyle \Big(\sum_{k=0}^{m-1} \cos(2\pi kry/2^n)\Big)^2 + \Big(\sum_{k=0}^{m-1} \sin(2\pi kry/2^n)\Big)^2 =cos2(πf(m1))sin2(πfm)sin2(πf)+sin2(πf(m1))sin2(πfm))sin2(πf)=sin2(πfm)sin2(πf)[cos2(πf(m1))+sin2(πf(m1))= 1]=sin2(πfm)sin2(πf)\begin{array}{rl} &=\displaystyle \frac{\cos^2(\pi f(m-1)) \sin^2(\pi fm)}{\sin^2(\pi f)} + \frac{\sin^2(\pi f(m-1))\sin^2(\pi fm))}{\sin^2(\pi f)}\\ &= \displaystyle \frac{\sin^2(\pi fm)}{\sin^2(\pi f)}\Big[\underbrace{\cos^2(\pi f(m-1)) + \sin^2(\pi f(m-1))}_{\text{= 1}}\Big]\\ &= \displaystyle \frac{\sin^2(\pi fm)}{\sin^2(\pi f)} \end{array}

Therefore, the probability of getting a specific y is

p(y)=12nmsin2(πfm)sin2(πf)p(y) = \displaystyle \frac{1}{2^nm} \displaystyle \frac{\sin^2(\pi fm)}{\sin^2(\pi f)}

Here is a plot of the function sin2(πfm)/sin2(πf)\sin^2(\pi fm)/\sin^2(\pi f) for m=4:

Notice that the graph attains its maximums when the values of f are integer values, that is when

f=j=ry2n where j=1,2,3,4,,r\displaystyle f = j= \frac{ry}{2^n} \text{ where } j= 1, 2, 3, 4, \ldots, r

Since y is an integer value, this means that the value of y must be close to j2n/rj2^n/r. We can use a theorem in Number theory which states that if p/qp/q is a rational number that approximates a real number x and

pqx12q\displaystyle \Big|\frac{p}{q} - x\Big| \le \frac{1}{2q}

we can approximate x using continued fraction expansion of p/q. In fact, if we find a y that is within 1/2 of one of the j2n/rj2^n/r values, we have

yj2nr12\displaystyle \left| y-j\frac{2^n}{r}\right| \le \displaystyle \frac{1}{2}\\

and dividing both sides by 2n2^n, we get

y2njr12n+1\displaystyle \left| \frac{y}{2^n}-\frac{j}{r}\right| \le \displaystyle \frac{1}{2^{n+1}}

which satisfies the theorem. We can therefore expand y/2ny/2^n via continued fraction expansion and continue until we find a denominator rr that is less that 2n02^{n_0}. We then test whether r could be our period if it satisfies

cr1\displaystyle c^r \equiv 1

If it satisfies the above, then r is the period. It's just a matter of computing dd^\prime, the inverse of ee modulo r satisfying

ed1modr\displaystyle ed^\prime \equiv 1 \mod r

Having computed dd^\prime, we can then decrypt the ciphertext using

m=cdmodNm = c^{d^\prime} \mod N

In our example in period finding, let's suppose that the quantum computer gave us the following number

y=1446311y=1446311

We can expand the fraction y/2n=1446311/16777216y/2^n = 1446311/16777216 as a continued fractions as follows:

y2n=144631116777216=111+11+11+11=558\displaystyle \frac{y}{2^n} = \frac{1446311}{16777216} = \frac{1}{11 + \displaystyle \frac{1}{1+\displaystyle\frac{1}{1+\displaystyle\frac{1}{1}}}} = \frac{5}{58}

or

y2n=144631116777216=111+11+11+116886=34433399423\displaystyle \frac{y}{2^n} = \frac{1446311}{16777216} = \frac{1}{11 + \displaystyle \frac{1}{1+\displaystyle\frac{1}{1+\displaystyle\frac{1}{\displaystyle\frac{1}{6886}}}}} = \frac{34433}{399423}

We can stop until q=58q=58 since q=58 < 2^{n_0} = 2^{12} = 4096 . So we use the first expansion where q=58q=58 and testing it

c58mod3127=79458mod3127=1mod3127\begin{array}{rl} \displaystyle c^{58} \mod 3127 &= \displaystyle 794^{58} \mod 3127\\ &= \displaystyle 1 \mod 3127 \end{array}

which confirms that q=58q=58 is a period. Using r=58r=58 in the equation

ed=m×r+1\displaystyle ed^\prime = m\times r + 1

and solving for dd^\prime (which is the inverse of e modulo 58) we get

d=25\displaystyle d^\prime = 25

We can now get the original message using

079425mod3127=1907\displaystyle 0794^{25} \mod 3127 = 1907

which agrees with our original message.

How lucky do we need to get in order for the state to collapse to one of these y values close to j2n/rj2^n/r? Let's evaluate the probability for

yj=j2nr±δjy_j=\displaystyle j\frac{2^n}{r} \pm \delta_j

where δj\delta_j is the distance of the closest y to j2n/rj2^n/r. Since f=ry/2nf = ry/2^n, we have

p(yj)=12nmsin2(πfm)sin2(πf)=12nmsin2(πmry2n)sin2(πry2n)=12nmsin2[πmr2n(j2nr±δj)]sin2[πr2n(j2nr±δj)]=12nmsin2[jπm±πmrδj2n)]sin2[jπ±πrδj2n]=12nmsin2[πmrδj2n)]sin2[πrδj2n]\begin{array}{rl} p(y_j) = \displaystyle \frac{1}{2^nm}\frac{\sin^2(\pi fm)}{\sin^2(\pi f)} &= \displaystyle \frac{1}{2^nm} \frac{\sin^2(\pi mr\displaystyle\frac{y}{2^n})}{\sin^2(\pi r\displaystyle\frac{y}{2^n}) }\\ &= \displaystyle \frac{1}{2^nm} \frac{\sin^2\Big[\displaystyle\frac{\pi mr}{2^n}(j\frac{2^n}{r} \pm \delta_j)\Big]}{\sin^2\Big[\displaystyle\frac{\pi r}{2^n}(j\frac{2^n}{r} \pm \delta_j)\Big] }\\ &= \displaystyle \frac{1}{2^nm} \frac{\sin^2\Big[\displaystyle j\pi m \pm \frac{\pi m r\delta_j}{2^n})\Big]}{\sin^2\Big[\displaystyle j\pi \pm \frac{\pi r\delta_j}{2^n}\Big] }\\ &= \displaystyle \frac{1}{2^nm} \frac{\sin^2\Big[\displaystyle \frac{\pi m r\delta_j}{2^n})\Big]}{\sin^2\Big[\displaystyle \frac{\pi r\delta_j}{2^n}\Big] } \end{array}

We can approximate the denominator using the small angle approximation of sine:

sin(x)x\sin(x) \approx x

to get

p(yj)=12nmsin2[πmrδj2n)][πrδj2n]2\displaystyle p(y_j) = \displaystyle \frac{1}{2^nm} \frac{\sin^2\Big[\displaystyle \frac{\pi m r\delta_j}{2^n})\Big]}{\left[\displaystyle \frac{\pi r\delta_j}{2^n}\right]^2}

Since m is the smallest integer such that x0+mr2nx_0 + mr \ge 2^{n}, then

m=2nrm = \displaystyle \left\lceil\frac{2^n}{r}\right\rceil

We can use this to approximate

mr2n1\displaystyle m\frac{r}{2^n} \approx 1

to get

p(yj)=12nmsin2(πδj)(πδjm)2p(y_j) = \displaystyle \frac{1}{2^nm} \frac{\sin^2(\displaystyle \pi\delta_j)}{\left(\displaystyle \frac{\pi\delta_j}{m}\right)^2}

Since 0δj1/20 \le \delta_j \le 1/2, we can see from the graph below that the line joining (0,0) and (π/2,1)(\pi/2,1), whose equation is 2x/π2x/\pi is less that the value of the sine function at that interval, that is,

2xπsin(x)\displaystyle \frac{2x}{\pi} \le \sin(x)

Using this inequality, we can get a lower bound of the probability of getting a specific y value to be

p(yj)=12nmsin2(πδj)(πδjm)212nm(2πδjππδjm)2=12nm(2mπ)=m2n4π2=1r4π2\begin{array}{rl} p(y_j) = \displaystyle \frac{1}{2^nm} \frac{\sin^2(\displaystyle \pi\delta_j)}{\left(\displaystyle \frac{\pi \delta_j}{m}\right)^2} &\ge \displaystyle \frac{1}{2^nm} \left(\frac{\displaystyle \frac{2\pi\delta_j}{\pi}}{\displaystyle \frac{\pi \delta_j}{m}}\right)^2\\ &= \displaystyle \frac{1}{2^nm} \left( \frac{2m}{\pi}\right)\\ &= \displaystyle \frac{m}{2^n}\frac{4}{\pi^2}\\ &= \displaystyle \frac{1}{r}\frac{4}{\pi^2} \end{array}

Since there are r of these yjy_j's, the probability of getting any one of these yjy_j's is therefore:

p(y)=4π20.405p(y) = \displaystyle \frac{4}{\pi^2} \approx 0.405

which means that there is 40% probability of the state to collapse to a value of y that will give us the correct period.

We have just seen how a quantum computer can be used to crack the RSA. We have exploited the fact that the measurement will give us a value of y that is near j2n/rj2^n/r with high probability. We then computed for the period using the continued fraction expansion of y/2ny/2^n. With the period computed, it's becomes straightforward to decrypt the ciphertext.

Image Credit: "Prime Numbers" by chrisinplymouth is marked with CC BY-NC-SA 2.0. To view the terms, visit https://creativecommons.org/licenses/by-nc-sa/2.0/?ref=openverse