Skip to content

Commit

Permalink
Add RSA Phase Estimate Bloq and Move ModExp to rsa/ subdirectory (#1428)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
fpapa250 and mpharrigan authored Oct 18, 2024
1 parent 3117a84 commit c227032
Show file tree
Hide file tree
Showing 21 changed files with 611 additions and 289 deletions.
12 changes: 7 additions & 5 deletions dev_tools/qualtran_dev_tools/notebook_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
import qualtran.bloqs.data_loading.qrom_base
import qualtran.bloqs.data_loading.select_swap_qrom
import qualtran.bloqs.factoring.ecc
import qualtran.bloqs.factoring.mod_exp
import qualtran.bloqs.factoring.rsa
import qualtran.bloqs.gf_arithmetic.gf2_add_k
import qualtran.bloqs.gf_arithmetic.gf2_addition
import qualtran.bloqs.gf_arithmetic.gf2_inverse
Expand Down Expand Up @@ -517,10 +517,12 @@
],
),
NotebookSpecV2(
title='Modular Exponentiation',
module=qualtran.bloqs.factoring.mod_exp,
bloq_specs=[qualtran.bloqs.factoring.mod_exp._MODEXP_DOC],
directory=f'{SOURCE_DIR}/bloqs/factoring',
title='Factoring RSA',
module=qualtran.bloqs.factoring.rsa,
bloq_specs=[
qualtran.bloqs.factoring.rsa.rsa_phase_estimate._RSA_PE_BLOQ_DOC,
qualtran.bloqs.factoring.rsa.rsa_mod_exp._RSA_MODEXP_DOC,
],
),
NotebookSpecV2(
title='Elliptic Curve Addition',
Expand Down
2 changes: 1 addition & 1 deletion docs/bloqs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Bloqs Library
mod_arithmetic/mod_addition.ipynb
mod_arithmetic/mod_subtraction.ipynb
mod_arithmetic/mod_multiplication.ipynb
factoring/mod_exp.ipynb
factoring/rsa/rsa.ipynb
factoring/ecc/ec_add.ipynb
factoring/ecc/ecc.ipynb

Expand Down
2 changes: 1 addition & 1 deletion qualtran/_infra/Bloqs-Tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qualtran.bloqs.factoring import ModExp\n",
"from qualtran.bloqs.factoring.rsa import ModExp\n",
"\n",
"mod_exp = ModExp(base=8, mod=13*17, exp_bitsize=3, x_bitsize=1024)\n",
"show_bloq(mod_exp)"
Expand Down
4 changes: 1 addition & 3 deletions qualtran/bloqs/factoring/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Google LLC
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -11,5 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .mod_exp import ModExp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

from qualtran import Bloq, CompositeBloq, DecomposeTypeError, QBit, Register, Side, Signature
from qualtran.drawing import RarrowTextBox, Text, WireSymbol
from qualtran.symbolics import SymbolicInt


@frozen
class MeasureQFT(Bloq):
n: int
n: 'SymbolicInt'

@cached_property
def signature(self) -> 'Signature':
Expand Down
2 changes: 1 addition & 1 deletion qualtran/bloqs/factoring/ecc/ec_phase_estimate_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from qualtran.bloqs.basic_gates import PlusState
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator

from ._ecc_shims import MeasureQFT
from .._factoring_shims import MeasureQFT
from .ec_add_r import ECAddR
from .ec_point import ECPoint

Expand Down
Empty file.
23 changes: 0 additions & 23 deletions qualtran/bloqs/factoring/mod_exp.html

This file was deleted.

209 changes: 0 additions & 209 deletions qualtran/bloqs/factoring/mod_exp.ipynb

This file was deleted.

37 changes: 37 additions & 0 deletions qualtran/bloqs/factoring/rsa/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# isort:skip_file

r"""Bloqs for breaking RSA cryptography systems via integer factorization.
RSA cryptography is a form of public key cryptography based on the difficulty of
factoring the product of two large prime numbers.
Using RSA, the cryptographic scheme chooses two large prime numbers p, q, their product n,
λ(n) = lcm(p - 1, q - 1) where λ is Carmichael's totient function, an integer e such that
1 < e < λ(n), and finally d as d ≡ e^-1 (mod λ(n)). The public key consists of the modulus n and
the public (or encryption) exponent e. The private key consists of the private (or decryption)
exponent d, which must be kept secret. p, q, and λ(n) must also be kept secret because they can be
used to calculate d.
Using Shor's algorithm for factoring, we can find p and q (the factors of n) in polynomial time
with a quantum algorithm.
References:
[RSA (cryptosystem)](https://en.wikipedia.org/wiki/RSA_(cryptosystem)).
"""

from .rsa_phase_estimate import RSAPhaseEstimate
from .rsa_mod_exp import ModExp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qualtran.bloqs.factoring.mod_exp import ModExp\n",
"from qualtran.bloqs.factoring.rsa.rsa_mod_exp import ModExp\n",
"from qualtran.drawing import show_bloq\n",
"\n",
"mod_exp = ModExp(base=g, mod=N, exp_bitsize=32, x_bitsize=32)\n",
Expand All @@ -205,6 +205,7 @@
"metadata": {},
"outputs": [],
"source": [
"from qualtran import QUInt\n",
"for e in range(20):\n",
" ref = (g ** e) % N\n",
" _, bloq_eval = mod_exp.call_classically(exponent=e)\n",
Expand All @@ -231,7 +232,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit c227032

Please sign in to comment.