From ccd7bd104ee9dbd7ae790a40e2c9b4bad62b0605 Mon Sep 17 00:00:00 2001 From: Harsha Kottapalli Date: Mon, 9 Oct 2023 13:08:26 +0530 Subject: [PATCH 1/3] diffie doctest --- ciphers/diffie.py | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/ciphers/diffie.py b/ciphers/diffie.py index 4ff90be009c1..caeae6c7de12 100644 --- a/ciphers/diffie.py +++ b/ciphers/diffie.py @@ -2,6 +2,28 @@ 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 + None + + >>> find_primitive(15) # Modulo 15 has no primitive root + None + """ for r in range(1, n): li = [] for x in range(n - 1): @@ -15,18 +37,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() From 0a789cd3e211aa2ba4c4a8ffcfbbb0c7002f39be Mon Sep 17 00:00:00 2001 From: Harsha Kottapalli Date: Mon, 9 Oct 2023 13:16:36 +0530 Subject: [PATCH 2/3] fix ut --- ciphers/diffie.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/ciphers/diffie.py b/ciphers/diffie.py index caeae6c7de12..85f0e79f5078 100644 --- a/ciphers/diffie.py +++ b/ciphers/diffie.py @@ -19,10 +19,7 @@ def find_primitive(n: int) -> int | None: 2 >>> find_primitive(8) # Modulo 8 has no primitive root - None - >>> find_primitive(15) # Modulo 15 has no primitive root - None """ for r in range(1, n): li = [] From aeb442e31210a0022e81ac9c45698971278bf3ad Mon Sep 17 00:00:00 2001 From: Harsha Kottapalli Date: Mon, 9 Oct 2023 19:58:59 +0530 Subject: [PATCH 3/3] update doctest --- ciphers/diffie.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/ciphers/diffie.py b/ciphers/diffie.py index 85f0e79f5078..1e1e868999b6 100644 --- a/ciphers/diffie.py +++ b/ciphers/diffie.py @@ -1,30 +1,28 @@ from __future__ import annotations -def find_primitive(n: int) -> int | None: +def find_primitive(modulus: int) -> int | None: """ - Find a primitive root modulo n, if one exists. + Find a primitive root modulo modulus, if one exists. Args: - n (int): The modulus for which to find a primitive root. + modulus : The modulus for which to find a primitive root. Returns: - int | None: The primitive root if one exists, or None if there is 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 - + >>> find_primitive(7) # Modulo 7 has primitive root 3 + 3 + >>> find_primitive(11) # Modulo 11 has primitive root 2 + 2 + >>> find_primitive(8) == None # Modulo 8 has no primitive root + True """ - for r in range(1, n): + for r in range(1, modulus): li = [] - for x in range(n - 1): - val = pow(r, x, n) + for x in range(modulus - 1): + val = pow(r, x, modulus) if val in li: break li.append(val) @@ -37,3 +35,19 @@ def find_primitive(n: int) -> int | None: import doctest doctest.testmod() + + prime = int(input("Enter a prime number q: ")) + primitive_root = find_primitive(prime) + if primitive_root is None: + print(f"Cannot find the primitive for the value: {primitive_root!r}") + else: + a_private = int(input("Enter private key of A: ")) + a_public = pow(primitive_root, a_private, prime) + b_private = int(input("Enter private key of B: ")) + b_public = pow(primitive_root, b_private, prime) + + a_secret = pow(b_public, a_private, prime) + b_secret = pow(a_public, b_private, prime) + + print("The key value generated by A is: ", a_secret) + print("The key value generated by B is: ", b_secret)