Skip to content

Commit

Permalink
Use catalog file
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentmacri committed Jul 30, 2024
1 parent 5726da8 commit fc1a4dc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/doc/en/reference/cryptography/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Cryptography
sage/crypto/lattice
sage/crypto/lwe

sage/crypto/key_exchange/catalog
sage/crypto/key_exchange/key_exchange_scheme
sage/crypto/key_exchange/diffie_hellman

Expand Down
2 changes: 1 addition & 1 deletion src/sage/crypto/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
])


from sage.crypto.key_exchange.all import *
import sage.crypto.key_exchange.catalog as key_exchange

del lazy_import
1 change: 1 addition & 0 deletions src/sage/crypto/key_exchange/all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sage.misc.lazy_import import lazy_import

lazy_import('sage.crypto.key_exchange.diffie_hellman', 'DiffieHellman')
lazy_import('sage.crypto.key_exchange.key_exchange_scheme', 'KeyExchangeScheme')

del lazy_import
23 changes: 23 additions & 0 deletions src/sage/crypto/key_exchange/catalog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Index of key exchange schemes
This catalogue includes implementations of key exchange schemes.
Let ``<tab>`` indicate pressing the :kbd:`Tab` key. So begin by typing
``key_exchange.<tab>`` to the see the currently implemented key exchange
schemes.
This catalogue includes the following key exchange schemes:
- :class:`sage.crypto.key_exchange.diffie_hellman.DiffieHellman`
To import these names into the global namespace, use::
sage: from sage.crypto.key_exchange.catalog import *
"""

from sage.misc.lazy_import import lazy_import

lazy_import('sage.crypto.key_exchange.diffie_hellman', 'DiffieHellman')

del lazy_import
44 changes: 22 additions & 22 deletions src/sage/crypto/key_exchange/diffie_hellman.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, p: Integer, g: Union[Integer, IntegerMod_abstract],
EXAMPLES::
sage: DH = DiffieHellman(13, 2)
sage: DH = key_exchange.DiffieHellman(13, 2)
doctest:...: FutureWarning: This class/method/function is marked as experimental. It, its functionality or its interface might change without a formal deprecation.
See https://github.com/sagemath/sage/issues/37305 for details.
Expand All @@ -77,7 +77,7 @@ def __init__(self, p: Integer, g: Union[Integer, IntegerMod_abstract],
(see [KK2003]_)::
sage: p = 2^8192 - 2^8128 - 1 + 2^64 * (round(2^8062 * pi) + 4743158)
sage: DH = DiffieHellman(p, 2, proof=False)
sage: DH = key_exchange.DiffieHellman(p, 2, proof=False)
sage: alice_sk = DH.generate_secret_key()
sage: alice_pk = DH.generate_public_key(alice_sk)
sage: bob_sk = DH.generate_secret_key()
Expand All @@ -89,22 +89,22 @@ def __init__(self, p: Integer, g: Union[Integer, IntegerMod_abstract],
TESTS::
sage: DH = DiffieHellman(3, 2)
sage: DH = key_exchange.DiffieHellman(3, 2)
Traceback (most recent call last):
...
ValueError: p must be at least 5
sage: DH = DiffieHellman(5, 0)
sage: DH = key_exchange.DiffieHellman(5, 0)
Traceback (most recent call last):
...
ValueError: g cannot be 0, 1, or p - 1 (mod p)
sage: DH = DiffieHellman(5, 1)
sage: DH = key_exchange.DiffieHellman(5, 1)
Traceback (most recent call last):
...
ValueError: g cannot be 0, 1, or p - 1 (mod p)
sage: DH = DiffieHellman(5, 4)
sage: DH = key_exchange.DiffieHellman(5, 4)
Traceback (most recent call last):
...
ValueError: g cannot be 0, 1, or p - 1 (mod p)
Expand Down Expand Up @@ -137,7 +137,7 @@ def field(self) -> FiniteField_prime_modn:
EXAMPLES::
sage: DH = DiffieHellman(5, 2)
sage: DH = key_exchange.DiffieHellman(5, 2)
sage: DH.field()
Finite Field of size 5
"""
Expand All @@ -149,7 +149,7 @@ def prime(self) -> Integer:
EXAMPLES::
sage: DH = DiffieHellman(7, 3)
sage: DH = key_exchange.DiffieHellman(7, 3)
sage: DH.prime()
7
"""
Expand All @@ -161,7 +161,7 @@ def generator(self) -> IntegerMod_abstract:
EXAMPLES::
sage: DH = DiffieHellman(7, 3)
sage: DH = key_exchange.DiffieHellman(7, 3)
sage: DH.generator()
3
"""
Expand All @@ -173,7 +173,7 @@ def parameters(self) -> tuple[Integer, IntegerMod_abstract]:
EXAMPLES::
sage: DH = DiffieHellman(7, 3)
sage: DH = key_exchange.DiffieHellman(7, 3)
sage: DH.parameters()
(7, 3)
"""
Expand All @@ -185,7 +185,7 @@ def generate_secret_key(self) -> Integer:
TESTS:
sage: DH = DiffieHellman(7, 2)
sage: DH = key_exchange.DiffieHellman(7, 2)
sage: keys = [DH.generate_secret_key() for i in range(10)]
sage: all(2 <= i <= 5 for i in keys)
True
Expand All @@ -202,7 +202,7 @@ def generate_public_key(self, secret_key: Integer) -> IntegerMod_abstract:
EXAMPLES::
sage: DH = DiffieHellman(13, 2)
sage: DH = key_exchange.DiffieHellman(13, 2)
sage: DH.generate_public_key(4)
3
"""
Expand All @@ -221,7 +221,7 @@ def compute_shared_secret(self, pk: IntegerMod_abstract,
EXAMPLES::
sage: DH = DiffieHellman(17, 3)
sage: DH = key_exchange.DiffieHellman(17, 3)
sage: DH.compute_shared_secret(13, 11)
4
"""
Expand All @@ -237,14 +237,14 @@ def subgroup_size(self) -> Integer:
This is an example of a ``DiffieHellman`` instance where the subgroup
size is `(p - 1) / 2`::
sage: DH = DiffieHellman(47, 2)
sage: DH = key_exchange.DiffieHellman(47, 2)
sage: DH.subgroup_size()
23
This is an example of a ``DiffieHellman`` instance where the subgroup
size is `p - 1`::
sage: DH = DiffieHellman(47, 5)
sage: DH = key_exchange.DiffieHellman(47, 5)
sage: DH.subgroup_size()
46
"""
Expand All @@ -257,7 +257,7 @@ def __len__(self) -> int:
TESTS::
sage: DH = DiffieHellman(53, 9)
sage: DH = key_exchange.DiffieHellman(53, 9)
sage: len(DH)
26
"""
Expand All @@ -269,8 +269,8 @@ def __eq__(self, other) -> bool:
TESTS::
sage: DH1 = DiffieHellman(5, 2)
sage: DH2 = DiffieHellman(5, 2)
sage: DH1 = key_exchange.DiffieHellman(5, 2)
sage: DH2 = key_exchange.DiffieHellman(5, 2)
sage: DH1 == DH2
True
sage: DH1 == 5
Expand All @@ -286,8 +286,8 @@ def __hash__(self) -> int:
TESTS::
sage: DH1 = DiffieHellman(7, 3)
sage: DH2 = DiffieHellman(7, 3)
sage: DH1 = key_exchange.DiffieHellman(7, 3)
sage: DH2 = key_exchange.DiffieHellman(7, 3)
sage: s = set([DH1, DH2])
sage: len(s)
1
Expand All @@ -300,7 +300,7 @@ def _repr_(self) -> str:
TESTS::
sage: DH = DiffieHellman(7, 3)
sage: DH = key_exchange.DiffieHellman(7, 3)
sage: DH
Diffie-Hellman key exchange over Finite Field of size 7 with generator 3
"""
Expand All @@ -315,7 +315,7 @@ def _latex_(self) -> str:
TESTS::
sage: DH = DiffieHellman(7, 3)
sage: DH = key_exchange.DiffieHellman(7, 3)
sage: latex(DH)
\text{Diffie-Hellman key exchange over }\Bold{F}_{7}\text{ with generator }3
"""
Expand Down

0 comments on commit fc1a4dc

Please sign in to comment.