Skip to content

Commit c227032

Browse files
fpapa250mpharrigan
andauthored
Add RSA Phase Estimate Bloq and Move ModExp to rsa/ subdirectory (#1428)
* Add rsa files - needs a lot of work just stashing it for now * Made some structure changes RSA * Rework rsa mod exp bloqs to work in a rsa phase estimation circuit * Fix mypy issues * Better symbolic messages * Refactor RSA to have a phase estimation circuit and a classical simulable modular exponentiation circuit * Fix notebook specs merge conflict * remove unecessary x values for classical simulation test * fix nits * Better documentation init * Fix broken link * Fix random issue and cirq interop import of modexp * Fix broken import msft interop * Fix another dependency of ModExp --------- Co-authored-by: Matthew Harrigan <mpharrigan@google.com>
1 parent 3117a84 commit c227032

21 files changed

+611
-289
lines changed

dev_tools/qualtran_dev_tools/notebook_specs.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
import qualtran.bloqs.data_loading.qrom_base
8484
import qualtran.bloqs.data_loading.select_swap_qrom
8585
import qualtran.bloqs.factoring.ecc
86-
import qualtran.bloqs.factoring.mod_exp
86+
import qualtran.bloqs.factoring.rsa
8787
import qualtran.bloqs.gf_arithmetic.gf2_add_k
8888
import qualtran.bloqs.gf_arithmetic.gf2_addition
8989
import qualtran.bloqs.gf_arithmetic.gf2_inverse
@@ -517,10 +517,12 @@
517517
],
518518
),
519519
NotebookSpecV2(
520-
title='Modular Exponentiation',
521-
module=qualtran.bloqs.factoring.mod_exp,
522-
bloq_specs=[qualtran.bloqs.factoring.mod_exp._MODEXP_DOC],
523-
directory=f'{SOURCE_DIR}/bloqs/factoring',
520+
title='Factoring RSA',
521+
module=qualtran.bloqs.factoring.rsa,
522+
bloq_specs=[
523+
qualtran.bloqs.factoring.rsa.rsa_phase_estimate._RSA_PE_BLOQ_DOC,
524+
qualtran.bloqs.factoring.rsa.rsa_mod_exp._RSA_MODEXP_DOC,
525+
],
524526
),
525527
NotebookSpecV2(
526528
title='Elliptic Curve Addition',

docs/bloqs/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Bloqs Library
8383
mod_arithmetic/mod_addition.ipynb
8484
mod_arithmetic/mod_subtraction.ipynb
8585
mod_arithmetic/mod_multiplication.ipynb
86-
factoring/mod_exp.ipynb
86+
factoring/rsa/rsa.ipynb
8787
factoring/ecc/ec_add.ipynb
8888
factoring/ecc/ecc.ipynb
8989

qualtran/_infra/Bloqs-Tutorial.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@
914914
"metadata": {},
915915
"outputs": [],
916916
"source": [
917-
"from qualtran.bloqs.factoring import ModExp\n",
917+
"from qualtran.bloqs.factoring.rsa import ModExp\n",
918918
"\n",
919919
"mod_exp = ModExp(base=8, mod=13*17, exp_bitsize=3, x_bitsize=1024)\n",
920920
"show_bloq(mod_exp)"

qualtran/bloqs/factoring/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023 Google LLC
1+
# Copyright 2024 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -11,5 +11,3 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
from .mod_exp import ModExp

qualtran/bloqs/factoring/ecc/_ecc_shims.py qualtran/bloqs/factoring/_factoring_shims.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919

2020
from qualtran import Bloq, CompositeBloq, DecomposeTypeError, QBit, Register, Side, Signature
2121
from qualtran.drawing import RarrowTextBox, Text, WireSymbol
22+
from qualtran.symbolics import SymbolicInt
2223

2324

2425
@frozen
2526
class MeasureQFT(Bloq):
26-
n: int
27+
n: 'SymbolicInt'
2728

2829
@cached_property
2930
def signature(self) -> 'Signature':

qualtran/bloqs/factoring/ecc/ec_phase_estimate_r.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from qualtran.bloqs.basic_gates import PlusState
3434
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator
3535

36-
from ._ecc_shims import MeasureQFT
36+
from .._factoring_shims import MeasureQFT
3737
from .ec_add_r import ECAddR
3838
from .ec_point import ECPoint
3939

qualtran/bloqs/factoring/mod_add_test.py

Whitespace-only changes.

qualtran/bloqs/factoring/mod_exp.html

-23
This file was deleted.

qualtran/bloqs/factoring/mod_exp.ipynb

-209
This file was deleted.
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# isort:skip_file
16+
17+
r"""Bloqs for breaking RSA cryptography systems via integer factorization.
18+
19+
RSA cryptography is a form of public key cryptography based on the difficulty of
20+
factoring the product of two large prime numbers.
21+
22+
Using RSA, the cryptographic scheme chooses two large prime numbers p, q, their product n,
23+
λ(n) = lcm(p - 1, q - 1) where λ is Carmichael's totient function, an integer e such that
24+
1 < e < λ(n), and finally d as d ≡ e^-1 (mod λ(n)). The public key consists of the modulus n and
25+
the public (or encryption) exponent e. The private key consists of the private (or decryption)
26+
exponent d, which must be kept secret. p, q, and λ(n) must also be kept secret because they can be
27+
used to calculate d.
28+
29+
Using Shor's algorithm for factoring, we can find p and q (the factors of n) in polynomial time
30+
with a quantum algorithm.
31+
32+
References:
33+
[RSA (cryptosystem)](https://en.wikipedia.org/wiki/RSA_(cryptosystem)).
34+
"""
35+
36+
from .rsa_phase_estimate import RSAPhaseEstimate
37+
from .rsa_mod_exp import ModExp

qualtran/bloqs/factoring/factoring-via-modexp.ipynb qualtran/bloqs/factoring/rsa/factoring-via-modexp.ipynb

+3-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
"metadata": {},
181181
"outputs": [],
182182
"source": [
183-
"from qualtran.bloqs.factoring.mod_exp import ModExp\n",
183+
"from qualtran.bloqs.factoring.rsa.rsa_mod_exp import ModExp\n",
184184
"from qualtran.drawing import show_bloq\n",
185185
"\n",
186186
"mod_exp = ModExp(base=g, mod=N, exp_bitsize=32, x_bitsize=32)\n",
@@ -205,6 +205,7 @@
205205
"metadata": {},
206206
"outputs": [],
207207
"source": [
208+
"from qualtran import QUInt\n",
208209
"for e in range(20):\n",
209210
" ref = (g ** e) % N\n",
210211
" _, bloq_eval = mod_exp.call_classically(exponent=e)\n",
@@ -231,7 +232,7 @@
231232
"name": "python",
232233
"nbconvert_exporter": "python",
233234
"pygments_lexer": "ipython3",
234-
"version": "3.10.9"
235+
"version": "3.10.13"
235236
}
236237
},
237238
"nbformat": 4,

0 commit comments

Comments
 (0)