





'''simple docstring'''



class 							__lowercase    :







	'''simple docstring'''

	def __init__(self	)							->							Optional[int]:







				'''simple docstring'''





				__lowercase			  =							0
				__lowercase			  =							0
				__lowercase			  =							{}


	def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							Optional[int]:







				'''simple docstring'''





				if vertex not in self.adjacency:
							__lowercase			  =							{}
							self.num_vertices += 1


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)							->							List[Any]:







				'''simple docstring'''





				self.add_vertex(_lowerCamelCase	)
				self.add_vertex(_lowerCamelCase	)

				if head == tail:
							return

				__lowercase			  =							weight
				__lowercase			  =							weight


	def   _UpperCAmelCase       (self	)							->							Dict:







				'''simple docstring'''





				__lowercase			  =							self.get_edges()
				for edge in edges:
							__lowercase  ,						__lowercase  ,						__lowercase			  =							edge
							edges.remove((tail, head, weight)	)
				for i in range(len(_lowerCamelCase	)	):
							__lowercase			  =							list(edges[i]	)

				edges.sort(key=lambda _lowerCamelCase	: e[2]	)
				for i in range(len(_lowerCamelCase	) - 1	):
							if edges[i][2] >= edges[i + 1][2]:
										__lowercase			  =							edges[i][2] + 1
				for edge in edges:
							__lowercase  ,						__lowercase  ,						__lowercase			  =							edge
							__lowercase			  =							weight
							__lowercase			  =							weight


	def __str__(self	)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase			  =							''''''
				for tail in self.adjacency:
							for head in self.adjacency[tail]:
										__lowercase			  =							self.adjacency[head][tail]
										string += f"{head} -> {tail} == {weight}\n"
				return string.rstrip('''\n'''	)


	def   _UpperCAmelCase       (self	)							->							Dict:







				'''simple docstring'''





				__lowercase			  =							[]
				for tail in self.adjacency:
							for head in self.adjacency[tail]:
										output.append((tail, head, self.adjacency[head][tail])	)
				return output


	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				return self.adjacency.keys()


	@staticmethod
	def   _UpperCAmelCase       (_lowerCamelCase=None       ,_lowerCamelCase=None	)							->							List[str]:







				'''simple docstring'''





				__lowercase			  =							Graph()
				if vertices is None:
							__lowercase			  =							[]
				if edges is None:
							__lowercase			  =							[]
				for vertex in vertices:
							g.add_vertex(_lowerCamelCase	)
				for edge in edges:
							g.add_edge(*_lowerCamelCase	)
				return g






	class 							__lowercase    :







		'''simple docstring'''

		def __init__(self	)							->							int:







					'''simple docstring'''





					__lowercase			  =							{}
					__lowercase			  =							{}


		def __len__(self	)							->							Optional[int]:







					'''simple docstring'''





					return len(self.parent	)


		def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							Tuple:







					'''simple docstring'''





					if item in self.parent:
								return self.find(_lowerCamelCase	)

					__lowercase			  =							item
					__lowercase			  =							0
					return item


		def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							Optional[int]:







					'''simple docstring'''





					if item not in self.parent:
								return self.make_set(_lowerCamelCase	)
					if item != self.parent[item]:
								__lowercase			  =							self.find(self.parent[item]	)
					return self.parent[item]


		def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase	)							->							Dict:







					'''simple docstring'''





					__lowercase			  =							self.find(_lowerCamelCase	)
					__lowercase			  =							self.find(_lowerCamelCase	)

					if roota == roota:
								return roota

					if self.rank[roota] > self.rank[roota]:
								__lowercase			  =							roota
								return roota

					if self.rank[roota] < self.rank[roota]:
								__lowercase			  =							roota
								return roota

					if self.rank[roota] == self.rank[roota]:
								self.rank[roota] += 1
								__lowercase			  =							roota
								return roota
					return None





	@staticmethod
	def   _UpperCAmelCase       (_lowerCamelCase	)							->							str:







				'''simple docstring'''





				__lowercase			  =							graph.num_vertices

				__lowercase			  =							Graph.UnionFind()
				__lowercase			  =							[]
				while num_components > 1:
							__lowercase			  =							{}
							for vertex in graph.get_vertices():
										__lowercase			  =							-1

							__lowercase			  =							graph.get_edges()
							for edge in edges:
										__lowercase  ,						__lowercase  ,						__lowercase			  =							edge
										edges.remove((tail, head, weight)	)
							for edge in edges:
										__lowercase  ,						__lowercase  ,						__lowercase			  =							edge
										__lowercase			  =							union_find.find(_lowerCamelCase	)
										__lowercase			  =							union_find.find(_lowerCamelCase	)
										if seta != seta:
													if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
																__lowercase			  =							[head, tail, weight]

													if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
																__lowercase			  =							[head, tail, weight]
							for vertex in cheap_edge:
										if cheap_edge[vertex] != -1:
													__lowercase  ,						__lowercase  ,						__lowercase			  =							cheap_edge[vertex]
													if union_find.find(_lowerCamelCase	) != union_find.find(_lowerCamelCase	):
																union_find.union(_lowerCamelCase       ,_lowerCamelCase	)
																mst_edges.append(cheap_edge[vertex]	)
																__lowercase			  =							num_components - 1
				__lowercase			  =							Graph.build(edges=_lowerCamelCase	)
				return mst












'''simple docstring'''



import os
import random
import sys

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




def 				_lowerCAmelCase  (			):
			print('''Making key files...'''			)
			make_key_files('''rsa'''						,    1_0_2_4			)
			print('''Key files generation successful.'''			)




def 				_lowerCAmelCase  (			lowerCamelCase_     :					int			):
			print('''Generating prime p...'''			)
			__lowercase			  =							rabinMiller.generate_large_prime(lowerCamelCase_			)
			print('''Generating prime q...'''			)
			__lowercase			  =							rabinMiller.generate_large_prime(lowerCamelCase_			)
			__lowercase			  =							p * q

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

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

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




def 				_lowerCAmelCase  (			lowerCamelCase_     :					str						,    lowerCamelCase_     :					int			):
			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()

			__lowercase  ,						__lowercase			  =							generate_key(lowerCamelCase_			)
			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()





