



import os
import random
import sys

from . import cryptomath_module as cryptoMath  # noqa: N812
from . import rabin_miller as rabinMiller  # noqa: N812

def 	SCREAMING_SNAKE_CASE_							(	)  ->     None:
   print('''Making key files...'''				)
   make_key_files('''rsa'''	,   1_024				)
   print('''Key files generation successful.'''				)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:int				)  ->     tuple[tuple[int, int], tuple[int, int]]:
   print('''Generating prime p...'''				)
   _A      =       rabinMiller.generate_large_prime(_snake_case				)
   print('''Generating prime q...'''				)
   _A      =       rabinMiller.generate_large_prime(_snake_case				)
   _A      =       p * q

   print('''Generating e that is relatively prime to (p - 1) * (q - 1)...'''				)
   while True:
      _A      =       random.randrange(2 ** (key_size - 1)	,   2 ** (key_size)				)
      if cryptoMath.gcd(_snake_case	,   (p - 1) * (q - 1)				) == 1:
         break

   print('''Calculating d that is mod inverse of e...'''				)
   _A      =       cryptoMath.find_mod_inverse(_snake_case	,   (p - 1) * (q - 1)				)

   _A      =       (n, e)
   _A      =       (n, d)
   return (public_key, private_key)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:str	,   _snake_case	:int				)  ->     None:
   if os.path.exists(F'''{name}_pubkey.txt'''				) or os.path.exists(F'''{name}_privkey.txt'''				):
      print('''\nWARNING:'''				)
      print(
          F'''"{name}_pubkey.txt" or "{name}_privkey.txt" already exists. \n'''
          '''Use a different name or delete these files and re-run this program.'''				)
      sys.exit()

   _A  ,    _A      =       generate_key(_snake_case				)
   print(F'''\nWriting public key to file {name}_pubkey.txt...'''				)
   with open(F'''{name}_pubkey.txt'''	,   '''w'''				) as out_file:
      out_file.write(F'''{key_size},{public_key[0]},{public_key[1]}'''				)

   print(F'''Writing private key to file {name}_privkey.txt...'''				)
   with open(F'''{name}_privkey.txt'''	,   '''w'''				) as out_file:
      out_file.write(F'''{key_size},{private_key[0]},{private_key[1]}'''				)


if __name__ == "__main__":
   main()








def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:list				)  ->     bool:
   if not isinstance(_snake_case	,   _snake_case				):
      raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''				)
   if len(_snake_case				) == 0:
      raise ValueError('''Input list must be a non empty list'''				)
   if len(_snake_case				) == 1:
      return True
   _A      =       series[1] - series[0]
   for index in range(len(_snake_case				) - 1				):
      if series[index + 1] - series[index] != common_diff:
         return False
   return True

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:list				)  ->     float:
   if not isinstance(_snake_case	,   _snake_case				):
      raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''				)
   if len(_snake_case				) == 0:
      raise ValueError('''Input list must be a non empty list'''				)
   _A      =       0
   for val in series:
      answer += val
   return answer / len(_snake_case				)


if __name__ == "__main__":
   import doctest

   doctest.testmod()



