2022-03-01 15:22:58 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""In ElGamal cyclic systems, there is at least one generator.
|
|
|
|
|
|
|
|
Find out if a given number is a generator.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
"""Ask for p and g and calculate"""
|
|
|
|
|
|
|
|
p = int(input("p: "))
|
|
|
|
g = int(input("g: "))
|
|
|
|
|
|
|
|
print(is_generator(p, g))
|
|
|
|
|
|
|
|
|
|
|
|
def is_generator(p: int, g: int) -> bool:
|
|
|
|
"""Algorithm"""
|
|
|
|
|
|
|
|
family_size = 0
|
|
|
|
|
|
|
|
for i in range(1, p):
|
2022-03-14 14:05:49 +01:00
|
|
|
total = pow(g, i, p)
|
2022-03-01 15:22:58 +01:00
|
|
|
print(total)
|
|
|
|
if total == 1:
|
|
|
|
family_size = i
|
|
|
|
break
|
|
|
|
|
|
|
|
if family_size == p - 1:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|