forked from AtitBimali/Crypto-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FermentsEulers-theroem-Eulers-totient-function.py
70 lines (58 loc) · 1.62 KB
/
FermentsEulers-theroem-Eulers-totient-function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import random
def gcd(a, b):
while b:
a, b = b, a % b
return a
def power_mod(base, exponent, modulus):
result = 1
while exponent > 0:
if exponent % 2 == 1:
result = (result * base) % modulus
exponent //= 2
base = (base * base) % modulus
return result
def fermat_test(n, k=5):
if n <= 1:
return False
if n == 2 or n == 3:
return True
for _ in range(k):
a = random.randint(2, n - 2)
if gcd(a, n) != 1:
return False
if power_mod(a, n - 1, n) != 1:
return False
return True
def euler_totient_function(n):
result = n
p = 2
while p * p <= n:
if n % p == 0:
while n % p == 0:
n //= p
result -= result // p
p += 1
if n > 1:
result -= result // n
return result
def euler_theorem(a, m):
if gcd(a, m) != 1:
return None # Euler's theorem doesn't apply
phi_m = euler_totient_function(m)
return power_mod(a, phi_m, m)
print("Fermat's Theorem Test ")
n = int(input("Enter a number : "))
if fermat_test(n):
print(f"{n} is probably prime.")
else:
print(f"{n} is composite.")
print("\nEuler’s totient Function")
print(f"Euler's Totient Function of {n} is {euler_totient_function(n)}")
print("\nEuler Theorem")
a = int(input("Enter a base 'a' : "))
m = int(input("Enter a modulus 'm' : "))
result = euler_theorem(a, m)
if result is None:
print("Euler's Theorem doesn't apply for the given values of 'a' and 'm'.")
else:
print(f"Euler's Theorem: {a}^{euler_totient_function(m)} % {m} = {result}")