#!/usr/bin/env python3

from Cryptotools.Groups.cyclic import Cyclic
from Cryptotools.Numbers.primeNumber import isPrimeNumber
from random import choice


def operation(a, b, n):
    return (a ** b) % n

n = 19
g = list()
for i in range(1, n):
    g.append(i)
   
print(f"n = {n}")
print(f"G = {g}")

cyclic = Cyclic(g, n, operation)
order = len(g) # Length of the group
print(f"len: {order}")
print(f"prime: {isPrimeNumber(order)}")
    
cyclic.generator()
  
print(f"All generators: {cyclic.getGenerators()}")
print(f"Is cyclic: {cyclic.isCyclic()}")

# Check if the abelian group is respected
print(f"It is an abelian group: {cyclic.closure()}\n")


e = choice(cyclic.getGenerators())
z = list()
for a in range(1, n):
    res = operation(e, a, n)
    z.append(res)

print(z)
if z == g:
    print(f"The group generated with the generator {e} works")
