


"""simple docstring"""







import os
import sys

from . import rsa_key_generator as rkg

__a      			= 1_28
__a      			= 2_56






def        a__			(							_UpperCamelCase						,      _UpperCamelCase = DEFAULT_BLOCK_SIZE			):





						"""simple docstring"""

						UpperCAmelCase__												=     message.encode('''ascii'''			)

						UpperCAmelCase__												=     []
						for block_start in range(0						,      len(_UpperCamelCase			)						,      _UpperCamelCase			):
												UpperCAmelCase__												=     0
												for i in range(_UpperCamelCase						,      min(block_start + block_size						,      len(_UpperCamelCase			)			)			):
																		block_int += message_bytes[i] * (BYTE_SIZE ** (i % block_size))
												block_ints.append(_UpperCamelCase			)
						return block_ints






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase = DEFAULT_BLOCK_SIZE			):





						"""simple docstring"""

						UpperCAmelCase__												=     []
						for block_int in block_ints:
												UpperCAmelCase__												=     []
												for i in range(block_size - 1						,      -1						,      -1			):
																		if len(_UpperCamelCase			) + i < message_length:
																								UpperCAmelCase__												=     block_int // (BYTE_SIZE**i)
																								UpperCAmelCase__												=     block_int % (BYTE_SIZE**i)
																								block_message.insert(0						,      chr(_UpperCamelCase			)			)
												message.extend(_UpperCamelCase			)
						return "".join(_UpperCamelCase			)






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase = DEFAULT_BLOCK_SIZE			):





						"""simple docstring"""

						UpperCAmelCase__												=     []
						UpperCAmelCase__						,					UpperCAmelCase__												=     key

						for block in get_blocks_from_text(_UpperCamelCase						,      _UpperCamelCase			):
												encrypted_blocks.append(pow(_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase			)			)
						return encrypted_blocks






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase = DEFAULT_BLOCK_SIZE						,      ):





						"""simple docstring"""

						UpperCAmelCase__												=     []
						UpperCAmelCase__						,					UpperCAmelCase__												=     key
						for block in encrypted_blocks:
												decrypted_blocks.append(pow(_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase			)			)
						return get_text_from_blocks(_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase			)






def        a__			(							_UpperCamelCase			):





						"""simple docstring"""

						with open(_UpperCamelCase			) as fo:
												UpperCAmelCase__												=     fo.read()
						UpperCAmelCase__						,					UpperCAmelCase__						,					UpperCAmelCase__												=     content.split(''','''			)
						return (int(_UpperCamelCase			), int(_UpperCamelCase			), int(_UpperCamelCase			))






def        a__			(							_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase = DEFAULT_BLOCK_SIZE						,      ):





						"""simple docstring"""

						UpperCAmelCase__						,					UpperCAmelCase__						,					UpperCAmelCase__												=     read_key_file(_UpperCamelCase			)
						if key_size < block_size * 8:
												sys.exit(
												    '''ERROR: Block size is {} bits and key size is {} bits. The RSA cipher '''
												    '''requires the block size to be equal to or greater than the key size. '''
												    '''Either decrease the block size or use different keys.'''.format(
												        block_size * 8						,      _UpperCamelCase			)			)

						UpperCAmelCase__												=     [str(_UpperCamelCase			) for i in encrypt_message(_UpperCamelCase						,      (n, e)						,      _UpperCamelCase			)]

						UpperCAmelCase__												=     ''','''.join(_UpperCamelCase			)
						UpperCAmelCase__												=     F"""{len(_UpperCamelCase			)}_{block_size}_{encrypted_content}"""
						with open(_UpperCamelCase						,      '''w'''			) as fo:
												fo.write(_UpperCamelCase			)
						return encrypted_content






def        a__			(							_UpperCamelCase						,      _UpperCamelCase			):





						"""simple docstring"""

						UpperCAmelCase__						,					UpperCAmelCase__						,					UpperCAmelCase__												=     read_key_file(_UpperCamelCase			)
						with open(_UpperCamelCase			) as fo:
												UpperCAmelCase__												=     fo.read()
						UpperCAmelCase__						,					UpperCAmelCase__						,					UpperCAmelCase__												=     content.split('''_'''			)
						UpperCAmelCase__												=     int(_UpperCamelCase			)
						UpperCAmelCase__												=     int(_UpperCamelCase			)

						if key_size < block_size * 8:
												sys.exit(
												    '''ERROR: Block size is {} bits and key size is {} bits. The RSA cipher '''
												    '''requires the block size to be equal to or greater than the key size. '''
												    '''Did you specify the correct key file and encrypted file?'''.format(
												        block_size * 8						,      _UpperCamelCase			)			)

						UpperCAmelCase__												=     []
						for block in encrypted_message.split(''','''			):
												encrypted_blocks.append(int(_UpperCamelCase			)			)

						return decrypt_message(_UpperCamelCase						,      _UpperCamelCase						,      (n, d)						,      _UpperCamelCase			)






def        a__			(							):





						"""simple docstring"""

						UpperCAmelCase__												=     '''encrypted_file.txt'''
						UpperCAmelCase__												=     input(R'''Encrypt\Decrypt [e\d]: '''			)

						if response.lower().startswith('''e'''			):
												UpperCAmelCase__												=     '''encrypt'''
						elif response.lower().startswith('''d'''			):
												UpperCAmelCase__												=     '''decrypt'''

						if mode == "encrypt":
												if not os.path.exists('''rsa_pubkey.txt'''			):
																		rkg.make_key_files('''rsa'''						,      1024			)

												UpperCAmelCase__												=     input('''\nEnter message: '''			)
												UpperCAmelCase__												=     '''rsa_pubkey.txt'''
												print(F"""Encrypting and writing to {filename}..."""			)
												UpperCAmelCase__												=     encrypt_and_write_to_file(_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase			)

												print('''\nEncrypted text:'''			)
												print(_UpperCamelCase			)

						elif mode == "decrypt":
												UpperCAmelCase__												=     '''rsa_privkey.txt'''
												print(F"""Reading from {filename} and decrypting..."""			)
												UpperCAmelCase__												=     read_from_file_and_decrypt(_UpperCamelCase						,      _UpperCamelCase			)
												print('''writing decryption to rsa_decryption.txt...'''			)
												with open('''rsa_decryption.txt'''						,      '''w'''			) as dec:
																		dec.write(_UpperCamelCase			)

												print('''\nDecryption:'''			)
												print(_UpperCamelCase			)


if __name__ == "__main__":
						main()



"""simple docstring"""







import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp






def        a__			(							):





						"""simple docstring"""

						UpperCAmelCase__												=     ArgumentParser(
						    description=(
						        '''PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes'''
						    )			)

						# Optional arguments for the launch helper
						parser.add_argument('''--num_cores'''						,      type=_UpperCamelCase						,      default=1						,      help='''Number of TPU cores to use (1 or 8).'''			)

						# positional
						parser.add_argument(
						    '''training_script'''						,      type=_UpperCamelCase						,      help=(
						        '''The full path to the single TPU training '''
						        '''program/script to be launched in parallel, '''
						        '''followed by all the arguments for the '''
						        '''training script'''
						    )						,      )

						# rest from the training program
						parser.add_argument('''training_script_args'''						,      nargs=_UpperCamelCase			)

						return parser.parse_args()






def        a__			(							):





						"""simple docstring"""

						UpperCAmelCase__												=     parse_args()

						# Import training_script as a module.
						UpperCAmelCase__												=     Path(args.training_script			)
						sys.path.append(str(script_fpath.parent.resolve()			)			)
						UpperCAmelCase__												=     script_fpath.stem
						UpperCAmelCase__												=     importlib.import_module(_UpperCamelCase			)

						# Patch sys.argv
						UpperCAmelCase__												=     [args.training_script] + args.training_script_args + ['''--tpu_num_cores''', str(args.num_cores			)]

						xmp.spawn(mod._mp_fn						,      args=()						,      nprocs=args.num_cores			)


if __name__ == "__main__":
						main()