Skip to content

Add DocTests to diffie.py #10156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions ciphers/diffie.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@


def find_primitive(n: int) -> int | None:
"""
Find a primitive root modulo n, if one exists.

Args:
n (int): The modulus for which to find a primitive root.

Returns:
int | None: The primitive root if one exists, or None if there is none.

Examples:
>>> find_primitive(7) # Modulo 7 has primitive root 3
3

>>> find_primitive(11) # Modulo 11 has primitive root 2
2

>>> find_primitive(8) # Modulo 8 has no primitive root

"""
for r in range(1, n):
li = []
for x in range(n - 1):
Expand All @@ -15,18 +34,6 @@ def find_primitive(n: int) -> int | None:


if __name__ == "__main__":
q = int(input("Enter a prime number q: "))
a = find_primitive(q)
if a is None:
print(f"Cannot find the primitive for the value: {a!r}")
else:
a_private = int(input("Enter private key of A: "))
a_public = pow(a, a_private, q)
b_private = int(input("Enter private key of B: "))
b_public = pow(a, b_private, q)

a_secret = pow(b_public, a_private, q)
b_secret = pow(a_public, b_private, q)

print("The key value generated by A is: ", a_secret)
print("The key value generated by B is: ", b_secret)
import doctest

doctest.testmod()