Skip to content

Commit ec76106

Browse files
authored
Revert "ecdklp temp removal"
This reverts commit 5a82c43.
1 parent 903a2e8 commit ec76106

File tree

4 files changed

+2070
-0
lines changed

4 files changed

+2070
-0
lines changed

algorithms/number_theory_and_cryptography/ecdlp/elliptic_curve_discrete_log.ipynb

Lines changed: 1715 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"friendly_name": "Elliptic Curve Discrete Logarithm",
3+
"description": "Solving Elliptic Curve Discrete Logarithm Problem using Shor's Algorithm",
4+
"level": ["advanced"],
5+
"problem_domain_tags": [],
6+
"qmod_type": ["algorithms"],
7+
"vertical_tags": []
8+
}
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
qstruct EllipticCurvePoint {
2+
x: qnum<3>;
3+
y: qnum<3>;
4+
}
5+
6+
qfunc hadamard_transform_expanded___0(target: qbit[3]) {
7+
repeat (index: 3) {
8+
H(target[index]);
9+
}
10+
}
11+
12+
qperm modular_add_constant_inplace_expanded___0(modulus: int, a: int, x: qnum<3, False, 0>) {
13+
carry: qbit;
14+
allocate(1, carry);
15+
temp: qnum<4, True, 0>;
16+
within {
17+
{x, carry} -> temp;
18+
} apply {
19+
temp += a;
20+
temp += -modulus;
21+
}
22+
control (carry) {
23+
x += modulus;
24+
}
25+
carry ^= x >= a;
26+
free(carry);
27+
}
28+
29+
qperm mock_modular_inverse_expanded___0(const x: qnum<3, False, 0>, result: qnum<3, False, 0>) {
30+
result ^= [0, 1, 4, 5, 2, 3, 6, 0][x];
31+
}
32+
33+
qperm modular_add_inplace_expanded___0(modulus: int, const x: qnum<3, False, 0>, y: qnum<3, False, 0>) {
34+
carry: qbit;
35+
allocate(1, carry);
36+
temp: qnum<4, True, 0>;
37+
within {
38+
{y, carry} -> temp;
39+
} apply {
40+
temp += x;
41+
temp += -modulus;
42+
}
43+
control (carry) {
44+
y += modulus;
45+
}
46+
carry ^= y >= x;
47+
free(carry);
48+
}
49+
50+
qperm cyclic_shift_left_expanded___0(reg: qbit[4]) {
51+
repeat (i: 3) {
52+
SWAP(reg[(4 - i) - 1], reg[(4 - i) - 2]);
53+
}
54+
}
55+
56+
qperm modular_double_inplace_expanded___0(modulus: int, x: qnum<3, False, 0>) {
57+
carry: qbit;
58+
allocate(1, carry);
59+
res_and_carry: qnum<4, True, 0>;
60+
within {
61+
{x, carry} -> res_and_carry;
62+
} apply {
63+
cyclic_shift_left_expanded___0(res_and_carry);
64+
res_and_carry += -modulus;
65+
}
66+
control (carry) {
67+
x += modulus;
68+
}
69+
carry ^= (x % 2) == 0;
70+
free(carry);
71+
}
72+
73+
qperm modular_multiply_expanded___0(modulus: int, const x: qbit[3], const y: qbit[3], z: qbit[3]) {
74+
repeat (idx: 3) {
75+
control (x[(3 - idx) - 1]) {
76+
modular_add_inplace_expanded___0(modulus, y, z);
77+
}
78+
if (idx != 2) {
79+
modular_double_inplace_expanded___0(modulus, z);
80+
}
81+
}
82+
}
83+
84+
qperm modular_square_expanded___0(modulus: int, const x: qbit[3], z: qbit[3]) {
85+
repeat (i: 2) {
86+
control (x[(3 - i) - 1]) {
87+
modular_add_inplace_expanded___0(modulus, x, z);
88+
}
89+
modular_double_inplace_expanded___0(modulus, z);
90+
}
91+
control (x[0]) {
92+
modular_add_inplace_expanded___0(modulus, x, z);
93+
}
94+
}
95+
96+
qperm apply_to_all_expanded___0(target: qbit[3]) {
97+
repeat (index: 3) {
98+
X(target[index]);
99+
}
100+
}
101+
102+
qperm bitwise_negate_expanded___0(x: qbit[3]) {
103+
apply_to_all_expanded___0(x);
104+
}
105+
106+
qperm modular_negate_inplace_expanded___0(modulus: int, x: qnum<3, False, 0>) {
107+
is_all_zeros: qbit;
108+
allocate(1, is_all_zeros);
109+
is_all_zeros ^= x == 0;
110+
control (is_all_zeros) {
111+
x += modulus;
112+
}
113+
x += 7 - modulus;
114+
is_all_zeros ^= x == 7;
115+
bitwise_negate_expanded___0(x);
116+
free(is_all_zeros);
117+
}
118+
119+
qperm modular_subtract_inplace_expanded___0(modulus: int, const x: qnum<3, False, 0>, y: qnum<3, False, 0>) {
120+
modular_negate_inplace_expanded___0(modulus, y);
121+
modular_add_inplace_expanded___0(modulus, x, y);
122+
}
123+
124+
qperm mock_modular_inverse_expanded___1(const x: qnum<3, False, 0>, result: qnum<3, False, 0>) {
125+
result ^= [0, 1, 4, 5, 2, 3, 6, 0][x];
126+
}
127+
128+
qperm ec_point_add_expanded___0(ecp: EllipticCurvePoint) {
129+
slope: qnum<3, False, 0>;
130+
allocate(3, slope);
131+
t0: qnum<3, False, 0>;
132+
allocate(3, t0);
133+
modular_add_constant_inplace_expanded___0(7, 2, ecp.y);
134+
modular_add_constant_inplace_expanded___0(7, 0, ecp.x);
135+
within {
136+
mock_modular_inverse_expanded___0(ecp.x, t0);
137+
} apply {
138+
modular_multiply_expanded___0(7, t0, ecp.y, slope);
139+
}
140+
within {
141+
modular_multiply_expanded___0(7, slope, ecp.x, t0);
142+
} apply {
143+
ecp.y ^= t0;
144+
}
145+
within {
146+
modular_square_expanded___0(7, slope, t0);
147+
} apply {
148+
modular_subtract_inplace_expanded___0(7, t0, ecp.x);
149+
modular_negate_inplace_expanded___0(7, ecp.x);
150+
modular_add_constant_inplace_expanded___0(7, 0, ecp.x);
151+
}
152+
modular_multiply_expanded___0(7, slope, ecp.x, ecp.y);
153+
t1: qnum<3, False, 0>;
154+
within {
155+
mock_modular_inverse_expanded___1(ecp.x, t0);
156+
} apply {
157+
within {
158+
allocate(3, t1);
159+
modular_multiply_expanded___0(7, t0, ecp.y, t1);
160+
} apply {
161+
slope ^= t1;
162+
}
163+
}
164+
free(slope);
165+
modular_add_constant_inplace_expanded___0(7, 2, ecp.y);
166+
modular_negate_inplace_expanded___0(7, ecp.x);
167+
modular_add_constant_inplace_expanded___0(7, 0, ecp.x);
168+
}
169+
170+
qperm ec_point_add_expanded___1(ecp: EllipticCurvePoint) {
171+
slope: qnum<3, False, 0>;
172+
allocate(3, slope);
173+
t0: qnum<3, False, 0>;
174+
allocate(3, t0);
175+
modular_add_constant_inplace_expanded___0(7, 6, ecp.y);
176+
modular_add_constant_inplace_expanded___0(7, 5, ecp.x);
177+
within {
178+
mock_modular_inverse_expanded___0(ecp.x, t0);
179+
} apply {
180+
modular_multiply_expanded___0(7, t0, ecp.y, slope);
181+
}
182+
within {
183+
modular_multiply_expanded___0(7, slope, ecp.x, t0);
184+
} apply {
185+
ecp.y ^= t0;
186+
}
187+
within {
188+
modular_square_expanded___0(7, slope, t0);
189+
} apply {
190+
modular_subtract_inplace_expanded___0(7, t0, ecp.x);
191+
modular_negate_inplace_expanded___0(7, ecp.x);
192+
modular_add_constant_inplace_expanded___0(7, 6, ecp.x);
193+
}
194+
modular_multiply_expanded___0(7, slope, ecp.x, ecp.y);
195+
t1: qnum<3, False, 0>;
196+
within {
197+
mock_modular_inverse_expanded___1(ecp.x, t0);
198+
} apply {
199+
within {
200+
allocate(3, t1);
201+
modular_multiply_expanded___0(7, t0, ecp.y, t1);
202+
} apply {
203+
slope ^= t1;
204+
}
205+
}
206+
free(slope);
207+
modular_add_constant_inplace_expanded___0(7, 6, ecp.y);
208+
modular_negate_inplace_expanded___0(7, ecp.x);
209+
modular_add_constant_inplace_expanded___0(7, 2, ecp.x);
210+
}
211+
212+
qperm ec_point_add_expanded___2(ecp: EllipticCurvePoint) {
213+
slope: qnum<3, False, 0>;
214+
allocate(3, slope);
215+
t0: qnum<3, False, 0>;
216+
allocate(3, t0);
217+
modular_add_constant_inplace_expanded___0(7, 5, ecp.y);
218+
modular_add_constant_inplace_expanded___0(7, 0, ecp.x);
219+
within {
220+
mock_modular_inverse_expanded___0(ecp.x, t0);
221+
} apply {
222+
modular_multiply_expanded___0(7, t0, ecp.y, slope);
223+
}
224+
within {
225+
modular_multiply_expanded___0(7, slope, ecp.x, t0);
226+
} apply {
227+
ecp.y ^= t0;
228+
}
229+
within {
230+
modular_square_expanded___0(7, slope, t0);
231+
} apply {
232+
modular_subtract_inplace_expanded___0(7, t0, ecp.x);
233+
modular_negate_inplace_expanded___0(7, ecp.x);
234+
modular_add_constant_inplace_expanded___0(7, 0, ecp.x);
235+
}
236+
modular_multiply_expanded___0(7, slope, ecp.x, ecp.y);
237+
t1: qnum<3, False, 0>;
238+
within {
239+
mock_modular_inverse_expanded___1(ecp.x, t0);
240+
} apply {
241+
within {
242+
allocate(3, t1);
243+
modular_multiply_expanded___0(7, t0, ecp.y, t1);
244+
} apply {
245+
slope ^= t1;
246+
}
247+
}
248+
free(slope);
249+
modular_add_constant_inplace_expanded___0(7, 5, ecp.y);
250+
modular_negate_inplace_expanded___0(7, ecp.x);
251+
modular_add_constant_inplace_expanded___0(7, 0, ecp.x);
252+
}
253+
254+
qperm ec_scalar_mult_add_expanded___0(ecp: EllipticCurvePoint, k: qbit[3]) {
255+
control (k[0]) {
256+
ec_point_add_expanded___0(ecp);
257+
}
258+
control (k[1]) {
259+
ec_point_add_expanded___1(ecp);
260+
}
261+
control (k[2]) {
262+
ec_point_add_expanded___2(ecp);
263+
}
264+
}
265+
266+
qfunc qft_no_swap_expanded___0(qbv: qbit[3]) {
267+
repeat (i: 3) {
268+
H(qbv[i]);
269+
repeat (j: (3 - i) - 1) {
270+
CPHASE(pi / (2 ** (j + 1)), qbv[(i + j) + 1], qbv[i]);
271+
}
272+
}
273+
}
274+
275+
qfunc qft_expanded___0(target: qbit[3]) {
276+
repeat (index: 1.5) {
277+
SWAP(target[index], target[2 - index]);
278+
}
279+
qft_no_swap_expanded___0(target);
280+
}
281+
282+
qfunc shor_ecdlp_expanded___0(output x1: qnum<3, False, 3>, output x2: qnum<3, False, 3>, output ecp: EllipticCurvePoint) {
283+
allocate(3, False, 3, x1);
284+
allocate(3, False, 3, x2);
285+
allocate(6, ecp);
286+
ecp.x ^= 4;
287+
ecp.y ^= 2;
288+
hadamard_transform_expanded___0(x1);
289+
hadamard_transform_expanded___0(x2);
290+
ec_scalar_mult_add_expanded___0(ecp, x1);
291+
ec_scalar_mult_add_expanded___0(ecp, x2);
292+
invert {
293+
qft_expanded___0(x1);
294+
}
295+
invert {
296+
qft_expanded___0(x2);
297+
}
298+
}
299+
300+
qfunc main(output x1: qnum<3, False, 3>, output x2: qnum<3, False, 3>, output ecp: EllipticCurvePoint) {
301+
shor_ecdlp_expanded___0(x1, x2, ecp);
302+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"constraints": {
3+
"max_gate_count": {},
4+
"optimization_parameter": "width"
5+
},
6+
"preferences": {
7+
"custom_hardware_settings": {
8+
"basis_gates": [
9+
"rx",
10+
"h",
11+
"r",
12+
"tdg",
13+
"cz",
14+
"s",
15+
"sdg",
16+
"z",
17+
"u",
18+
"cx",
19+
"y",
20+
"sx",
21+
"x",
22+
"id",
23+
"sxdg",
24+
"ry",
25+
"cy",
26+
"t",
27+
"p",
28+
"rz",
29+
"u2",
30+
"u1"
31+
],
32+
"is_symmetric_connectivity": true
33+
},
34+
"debug_mode": true,
35+
"machine_precision": 8,
36+
"optimization_level": 1,
37+
"output_format": ["qasm"],
38+
"pretty_qasm": true,
39+
"qasm3": true,
40+
"random_seed": 3794060243,
41+
"synthesize_all_separately": false,
42+
"timeout_seconds": 3600,
43+
"transpilation_option": "auto optimize"
44+
}
45+
}

0 commit comments

Comments
 (0)