






'''simple docstring'''




import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  ''''''
	_lowerCamelCase              =  (
	    None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
	)
	_lowerCamelCase              =  None  # compression type in fsspec. ex: "gzip"
	_lowerCamelCase              =  None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz

	def __init__(      self   :						Optional[Any]  ,					UpperCAmelCase_   :						str = ""  ,					UpperCAmelCase_   :						Optional[str] = None  ,					UpperCAmelCase_   :						Optional[dict] = None  ,					**UpperCAmelCase_   :						Dict):
			super().__init__(self  ,					**UpperCAmelCase_)
			# always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
			UpperCamelCase__				:					int					=					fsspec.open(
			    UpperCAmelCase_  ,					mode='rb'  ,					protocol=UpperCAmelCase_  ,					compression=self.compression  ,					client_kwargs={
			        'requote_redirect_url': False,  # see https://github.com/huggingface/datasets/pull/5459
			        'trust_env': True,  # Enable reading proxy env variables.
			        **(target_options or {}).pop('client_kwargs'  ,					{}),  # To avoid issues if it was already passed.
			    }  ,					**(target_options or {})  ,					)
			UpperCamelCase__				:					Tuple					=					os.path.basename(self.file.path.split('::')[0])
			UpperCamelCase__				:					Union[str, Any]					=					(
			    self.compressed_name[: self.compressed_name.rindex('.')]
			    if '.' in self.compressed_name
			    else self.compressed_name
			)
			UpperCamelCase__				:					str					=					None

	@classmethod
	def      __UpperCamelCase							(      cls   :						Optional[int]  ,					UpperCAmelCase_   :						Dict):
			# compressed file paths are always relative to the archive root
			return super()._strip_protocol(UpperCAmelCase_).lstrip('/')

	def      __UpperCamelCase							(      self   :						Any):
			if self.dir_cache is None:
					UpperCamelCase__				:					Tuple					=					{**self.file.fs.info(self.file.path), 'name': self.uncompressed_name}
					UpperCamelCase__				:					Dict					=					{f['name']: f}

	def      __UpperCamelCase							(      self   :						str  ,					UpperCAmelCase_   :						str):
			return self.file.open().read()





	def      __UpperCamelCase							(      self   :						Dict  ,					UpperCAmelCase_   :						str  ,					UpperCAmelCase_   :						str = "rb"  ,					UpperCAmelCase_   :						Any=None  ,					UpperCAmelCase_   :						Optional[int]=True  ,					UpperCAmelCase_   :						Any=None  ,					**UpperCAmelCase_   :						str  ,					):
			UpperCamelCase__				:					str					=					self._strip_protocol(UpperCAmelCase_)
			if mode != "rb":
					raise ValueError(F'Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'')
			return self.file.open()







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''bz2'''
	_lowerCamelCase              =  '''bz2'''
	_lowerCamelCase              =  '''.bz2'''







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''gzip'''
	_lowerCamelCase              =  '''gzip'''
	_lowerCamelCase              =  '''.gz'''







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''lz4'''
	_lowerCamelCase              =  '''lz4'''
	_lowerCamelCase              =  '''.lz4'''







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''xz'''
	_lowerCamelCase              =  '''xz'''
	_lowerCamelCase              =  '''.xz'''







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''zstd'''
	_lowerCamelCase              =  '''zstd'''
	_lowerCamelCase              =  '''.zst'''

	def __init__(      self   :						Optional[int]  ,					UpperCAmelCase_   :						str  ,					UpperCAmelCase_   :						str = "rb"  ,					UpperCAmelCase_   :						Optional[str] = None  ,					UpperCAmelCase_   :						Optional[dict] = None  ,					UpperCAmelCase_   :						int = DEFAULT_BLOCK_SIZE  ,					**UpperCAmelCase_   :						Dict  ,					):
			super().__init__(
			    fo=UpperCAmelCase_  ,					mode=UpperCAmelCase_  ,					target_protocol=UpperCAmelCase_  ,					target_options=UpperCAmelCase_  ,					block_size=UpperCAmelCase_  ,					**UpperCAmelCase_  ,					)
			# We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
			#
			# File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
			#     out.close = close
			# AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
			#
			# see https://github.com/intake/filesystem_spec/issues/725
			UpperCamelCase__				:					str					=					self.file.__enter__







			class       __lowercase					:

				def __init__(      self   :						Optional[int]  ,					UpperCAmelCase_   :						Dict):
						UpperCamelCase__				:					str					=					file_

				def __enter__(      self   :						str):
						self._file.__enter__()
						return self

				def __exit__(      self   :						Optional[int]  ,					*UpperCAmelCase_   :						Dict  ,					**UpperCAmelCase_   :						Any):
						self._file.__exit__(*UpperCAmelCase_  ,					**UpperCAmelCase_)

				def __iter__(      self   :						Tuple):
						return iter(self._file)

				def      __UpperCamelCase							(      self   :						Any):
						return next(self._file)





				def __getattr__(      self   :						str  ,					UpperCAmelCase_   :						Tuple):
						return getattr(self._file  ,					UpperCAmelCase_)

			def fixed_enter(*UpperCAmelCase_   :						List[str]  ,					**UpperCAmelCase_   :						Dict):
					return WrappedFile(_enter(*UpperCAmelCase_  ,					**UpperCAmelCase_))

			UpperCamelCase__				:					str					=					fixed_enter












'''simple docstring'''




import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

lowerCAmelCase__          =						3







def       __UpperCAmelCase						(  lowerCamelCase_)							->     int:
		print('Generating primitive root of p')
		while True:
				UpperCamelCase__				:					Any					=					random.randrange(3     ,      lowerCamelCase_)
				if pow(lowerCamelCase_     ,      2     ,      lowerCamelCase_) == 1:
						continue
				if pow(lowerCamelCase_     ,      lowerCamelCase_     ,      lowerCamelCase_) == 1:
						continue
				return g







def       __UpperCAmelCase						(  lowerCamelCase_)							->     tuple[tuple[int, int, int, int], tuple[int, int]]:
		print('Generating prime p...')
		UpperCamelCase__				:					List[str]					=					rabin_miller.generate_large_prime(lowerCamelCase_)  # select large prime number.
		UpperCamelCase__				:					Any					=					primitive_root(lowerCamelCase_)  # one primitive root on modulo p.
		UpperCamelCase__				:					Union[str, Any]					=					random.randrange(3     ,      lowerCamelCase_)  # private_key -> have to be greater than 2 for safety.
		UpperCamelCase__				:					Dict					=					cryptomath.find_mod_inverse(pow(lowerCamelCase_     ,      lowerCamelCase_     ,      lowerCamelCase_)     ,      lowerCamelCase_)

		UpperCamelCase__				:					List[Any]					=					(key_size, e_a, e_a, p)
		UpperCamelCase__				:					Optional[Any]					=					(key_size, d)

		return public_key, private_key







def       __UpperCAmelCase						(  lowerCamelCase_     ,      lowerCamelCase_)							->     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()

		UpperCamelCase__,	UpperCamelCase__				:					Union[str, Any]					=					generate_key(lowerCamelCase_)
		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       __UpperCAmelCase						(  )							->     None:
		print('Making key files...')
		make_key_files('elgamal'     ,      2_048)
		print('Key files generation successful')


if __name__ == "__main__":
		main()




