-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqcLec19-RSA.R
182 lines (154 loc) · 4.21 KB
/
qcLec19-RSA.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# ----------------------
# ---------- AD S. Aaronson, Introduction to Quantum Information Science Lecture Notes
# ---------- https://www.scottaaronson.com/qclec.pdf
# ----------------------
#
# This script is designed to run line by line --> RStudio and Ctrl + Enter
#
# Author: Mariusz Krej
#
# ----------------------
# ad RSA encryption
#
# https://www.scottaaronson.com/democritus/lec8.html
# https://www.scottaaronson.com/qclec.pdf
#
#
# Author: Mariusz Krej
#
library(pracma)
if("rstudioapi" %in% rownames(installed.packages()) && rstudioapi::isAvailable())
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
getwd()
if (FALSE)
rm(list=ls())
source("modpow.R")
epsilon = 1e-12
c = 11
c = 7
c = 5
c = 3
mx = 50
mx = 100
mx = 1000
while(1)
{
p = trunc(runif(1, c, mx))
q = trunc(runif(1, c, mx))
if (p==q)
next
if (!all(abs(q / 2:(q-1) - trunc(q / 2:(q-1))) > epsilon))
next
if (!all(abs(p / 2:(p-1) - trunc(p / 2:(p-1))) > epsilon))
next
Npq = (p-1) * (q-1)
if (Npq/c - trunc(Npq/c) != 0)
{
x = trunc(runif(1, p*q/2, p*q))
cat(sprintf("p=%g q=%g x=%g\n", p, q, x))
break
}
}
N = p * q
N
Npq = (p-1)*(q-1)
Npq
Npq / c
stopifnot(x < N)
stopifnot(Npq / c - trunc(Npq / c) != 0)
if (FALSE)
{
modpow(x, 0:20, N)
modpow(x, 0:20 + Npq, N)
modpow(x, 1, N)
modpow(x, c, N)
}
retval = modpow(x, c, N)
retval
if(FALSE)
{
modpow(x, c, N)
modpow(retval, 1, N)
}
# by raising the retval to the next powers, it jumps over c positions in a row
# but k is such that (c * k) stands next to the end of the period (p-1)(q-1), right where x sits
# (after the end of the period there is 1, because it is a copy of x^0, the next is x^1)
k = which((c*(1:Npq)) %% Npq == 1)[1]
stopifnot(!is.na(k))
if (FALSE)
{
k
(c*k) %% Npq
(c*(1:Npq)) %% Npq
}
# this is x !!
res = modpow(retval, k, N)
stopifnot(res == x)
cat("OK!\n")
# -------
# + it looks like the period is a few shorter than Npq - that's interesting
# ok, here it is confirmed:
# https://www.scottaaronson.com/qclec.pdf, lecture 19
# "the period of f might not equal φ(N), the most we can say is that the period divides φ(N)"
#
if (FALSE)
{
which(modpow(retval, 1:Npq, N) == x)
which(modpow(retval, 1:Npq, N) == 1)
modpow(retval, k, N)
plot(1:(Npq),modpow(x, 1:(Npq), N), type = 'l', col = 'blue')
diff(which(modpow(x, 1:(Npq), N) == 1))
diff(which(modpow(retval, 1:Npq, N) == x))
f = diff(which(modpow(retval, 1:Npq, N) == x))[1]
print(Npq/f)
modpow(x, 0:k, N)
modpow(retval, 0:(k+10), N)
}
# ------- period attack (classical)
# (you can pick your own x until it meets "we get lucky" conditions)
if (
abs(x/p - trunc(x/p)) > epsilon &&
abs(x/q - trunc(x/q)) > epsilon
) # we get lucky
{
cat("period search...")
period = diff(which(modpow(x, 1:N, N) == 1))
stopifnot(abs(max(period) - min(period)) < epsilon)
period = period[1]
cat("ok\n")
rec = NA
if (period %% 2 == 0) # we get lucky (again)
{
xs2 = modpow(x, period/2, N)
if (xs2 > 1 && xs2 < N - 1) # we get lucky (again)
{
if (FALSE)
{
((xs2 + 1) * (xs2 - 1)) %% N
gcd(xs2 - 1, N)
gcd(xs2 + 1, N)
c((xs2 - 1) / p, (xs2 - 1) / q, (xs2 + 1) / p, (xs2 + 1) / q)
pmul = (xs2 - 1) / p
qmul = (xs2 - 1) / q
gcd(p*pmul, p*q)
gcd(q*qmul, p*q)
}
cat("we get lucky\n")
rec = max(gcd(xs2 - 1, N), gcd(xs2 + 1, N))
}
}
# less lucky
if (is.na(rec))
{
# here is a little cheat because Npq should not be known,
# but maybe this can be circumvented e.g. by checking all pre-matching multipliers?
mul = Npq / period
paq = N - period * mul + 1
# p^2 - paq*p + N = 0 --> quadratic equation
d = paq^2 - 4*N # ---> b^2 - 4*a*c
stopifnot(d >= 0)
rec = max( (paq + sqrt(d)) / 2, (paq - sqrt(d)) / 2) # ---> (-b +/- sqrt(d))/(2*a)
}
cat(sprintf("recovered (p or q) = %d\n", rec))
stopifnot(any(abs(c(q, p) - rec) < epsilon))
}