


'''simple docstring'''





class    UpperCAmelCase					:




			def __init__(						self							:		Optional[int]       ,       __magic_name__							:		int       ,       __magic_name__							:		int=None       ,       __magic_name__							:		int=None						):







						"""simple docstring"""







						UpperCamelCase         =					data
						UpperCamelCase         =					previous
						UpperCamelCase         =					next_node




			def __str__(						self							:		str						):







						"""simple docstring"""







						return F'{self.data}'




			def   lowerCamelCase_					(						self							:		List[Any]						):







						"""simple docstring"""







						return self.data




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						return self.next





			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						return self.previous




class    UpperCAmelCase					:




			def __init__(						self							:		List[str]       ,       __magic_name__							:		Union[str, Any]						):







						"""simple docstring"""







						UpperCamelCase         =					head




			def __iter__(						self							:		List[Any]						):







						"""simple docstring"""







						return self





			def   lowerCamelCase_					(						self							:		Union[str, Any]						):







						"""simple docstring"""







						if not self.current:
									raise StopIteration
						else:
									UpperCamelCase         =					self.current.get_data()
									UpperCamelCase         =					self.current.get_next()
									return value




class    UpperCAmelCase					:




			def __init__(						self							:		Union[str, Any]						):







						"""simple docstring"""







						UpperCamelCase         =					None  # First node in list
						UpperCamelCase         =					None  # Last node in list




			def __str__(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.head
						UpperCamelCase         =					[]
						while current is not None:
									nodes.append(current.get_data()						)
									UpperCamelCase         =					current.get_next()
						return " ".join(str(_UpperCAmelCase						) for node in nodes						)




			def __contains__(						self							:		str       ,       __magic_name__							:		int						):







						"""simple docstring"""







						UpperCamelCase         =					self.head
						while current:
									if current.get_data() == value:
												return True
									UpperCamelCase         =					current.get_next()
						return False




			def __iter__(						self							:		Union[str, Any]						):







						"""simple docstring"""







						return LinkedListIterator(self.head						)




			def   lowerCamelCase_					(						self							:		str						):







						"""simple docstring"""







						if self.head:
									return self.head.get_data()
						return None




			def   lowerCamelCase_					(						self							:		Optional[Any]						):







						"""simple docstring"""







						if self.tail:
									return self.tail.get_data()
						return None




			def   lowerCamelCase_					(						self							:		Union[str, Any]       ,       __magic_name__							:		Node						):







						"""simple docstring"""







						if self.head is None:
									UpperCamelCase         =					node
									UpperCamelCase         =					node
						else:
									self.insert_before_node(self.head       ,       _UpperCAmelCase						)




			def   lowerCamelCase_					(						self							:		List[Any]       ,       __magic_name__							:		Node						):







						"""simple docstring"""







						if self.head is None:
									self.set_head(_UpperCAmelCase						)
						else:
									self.insert_after_node(self.tail       ,       _UpperCAmelCase						)




			def   lowerCamelCase_					(						self							:		str       ,       __magic_name__							:		int						):







						"""simple docstring"""







						UpperCamelCase         =					Node(_UpperCAmelCase						)
						if self.head is None:
									self.set_head(_UpperCAmelCase						)
						else:
									self.set_tail(_UpperCAmelCase						)




			def   lowerCamelCase_					(						self							:		List[Any]       ,       __magic_name__							:		Node       ,       __magic_name__							:		Node						):







						"""simple docstring"""







						UpperCamelCase         =					node
						UpperCamelCase         =					node.previous

						if node.get_previous() is None:
									UpperCamelCase         =					node_to_insert
						else:
									UpperCamelCase         =					node_to_insert

						UpperCamelCase         =					node_to_insert




			def   lowerCamelCase_					(						self							:		Tuple       ,       __magic_name__							:		Node       ,       __magic_name__							:		Node						):







						"""simple docstring"""







						UpperCamelCase         =					node
						UpperCamelCase         =					node.next

						if node.get_next() is None:
									UpperCamelCase         =					node_to_insert
						else:
									UpperCamelCase         =					node_to_insert

						UpperCamelCase         =					node_to_insert




			def   lowerCamelCase_					(						self							:		Dict       ,       __magic_name__							:		int       ,       __magic_name__							:		int						):







						"""simple docstring"""







						UpperCamelCase         =					1
						UpperCamelCase         =					Node(_UpperCAmelCase						)
						UpperCamelCase         =					self.head
						while node:
									if current_position == position:
												self.insert_before_node(_UpperCAmelCase       ,       _UpperCAmelCase						)
												return
									current_position += 1
									UpperCamelCase         =					node.next
						self.insert_after_node(self.tail       ,       _UpperCAmelCase						)




			def   lowerCamelCase_					(						self							:		Tuple       ,       __magic_name__							:		int						):







						"""simple docstring"""







						UpperCamelCase         =					self.head
						while node:
									if node.get_data() == item:
												return node
									UpperCamelCase         =					node.get_next()
						raise Exception("""Node not found"""						)




			def   lowerCamelCase_					(						self							:		Optional[Any]       ,       __magic_name__							:		Optional[int]						):







						"""simple docstring"""







						if (node := self.get_node(_UpperCAmelCase						)) is not None:
									if node == self.head:
												UpperCamelCase         =					self.head.get_next()

									if node == self.tail:
												UpperCamelCase         =					self.tail.get_previous()

									self.remove_node_pointers(_UpperCAmelCase						)




			@staticmethod
			def   lowerCamelCase_					(						__magic_name__							:		Node						):







						"""simple docstring"""







						if node.get_next():
									UpperCamelCase         =					node.previous

						if node.get_previous():
									UpperCamelCase         =					node.next

						UpperCamelCase         =					None
						UpperCamelCase         =					None





			def   lowerCamelCase_					(						self							:		int						):







						"""simple docstring"""







						return self.head is None






def 							_lowercase    (		):





			"""simple docstring"""




			pass


if __name__ == "__main__":
			import doctest

			doctest.testmod()








import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

__snake_case            =   3






def 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		int	):





			"""simple docstring"""




			print("""Generating primitive root of p"""	)
			while True:
						UpperCamelCase         =					random.randrange(3       ,  SCREAMING_SNAKE_CASE_	)
						if pow(SCREAMING_SNAKE_CASE_       ,  2       ,  SCREAMING_SNAKE_CASE_	) == 1:
									continue
						if pow(SCREAMING_SNAKE_CASE_       ,  SCREAMING_SNAKE_CASE_       ,  SCREAMING_SNAKE_CASE_	) == 1:
									continue
						return g






def 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		int	):





			"""simple docstring"""




			print("""Generating prime p..."""	)
			UpperCamelCase         =					rabin_miller.generate_large_prime(SCREAMING_SNAKE_CASE_	)  # select large prime number.
			UpperCamelCase         =					primitive_root(SCREAMING_SNAKE_CASE_	)  # one primitive root on modulo p.
			UpperCamelCase         =					random.randrange(3       ,  SCREAMING_SNAKE_CASE_	)  # private_key -> have to be greater than 2 for safety.
			UpperCamelCase         =					cryptomath.find_mod_inverse(pow(SCREAMING_SNAKE_CASE_       ,  SCREAMING_SNAKE_CASE_       ,  SCREAMING_SNAKE_CASE_	)       ,  SCREAMING_SNAKE_CASE_	)

			UpperCamelCase         =					(key_size, e_a, e_a, p)
			UpperCamelCase         =					(key_size, d)

			return public_key, private_key






def 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		str       ,  SCREAMING_SNAKE_CASE_   :		int	):





			"""simple docstring"""




			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()

			UpperCamelCase  ,  UpperCamelCase         =					generate_key(SCREAMING_SNAKE_CASE_	)
			print(f'\nWriting public key to file {name}_pubkey.txt...'	)
			with open(f'{name}_pubkey.txt'       ,  """w"""	) as fo:
						fo.write(f'{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}'	)

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






def 							_lowercase    (		):





			"""simple docstring"""




			print("""Making key files..."""	)
			make_key_files("""elgamal"""       ,  2_048	)
			print("""Key files generation successful"""	)


if __name__ == "__main__":
			main()




