


import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

_lowercase       :      List[Any]	=	3





def   _lowerCAmelCase				(   UpperCamelCase__:     int						) ->   int:





	"""simple docstring"""







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





def   _lowerCAmelCase				(   UpperCamelCase__:     int						) ->   tuple[tuple[int, int, int, int], tuple[int, int]]:





	"""simple docstring"""







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

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

	return public_key, private_key





def   _lowerCAmelCase				(   UpperCamelCase__:     str							,					UpperCamelCase__:     int						) ->   None:





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

	A					,			A								=				generate_key(UpperCamelCase__						)
	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   _lowerCAmelCase				(   ) ->   None:





	"""simple docstring"""







	print("""Making key files..."""						)
	make_key_files("""elgamal"""							,					20_48						)
	print("""Key files generation successful"""						)


if __name__ == "__main__":
	main()







import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch






@require_torch
class       _UpperCamelCase    :

				"""simple docstring"""







				@property
				def      _UpperCAmelCase					(						self   )	->				Any:
					return self.get_dummy_input()

				@property
				def      _UpperCAmelCase					(						self   )	->				Union[str, Any]:
					if self.block_type == "down":
						return (4, 32, 16, 16)
					elif self.block_type == "mid":
						return (4, 32, 32, 32)
					elif self.block_type == "up":
						return (4, 32, 64, 64)

					raise ValueError(f'\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.'   )

				def      _UpperCAmelCase					(						self				,   a__=True				,   a__=False				,   a__=False				,   a__=False				,   )	->				Optional[Any]:
					A								=				4
					A								=				32
					A								=				(32, 32)

					A								=				torch.manual_seed(0   )
					A								=				torch.device(a__   )
					A								=				(batch_size, num_channels) + sizes
					A								=				randn_tensor(a__				,   generator=a__				,   device=a__   )
					A								=				{"""hidden_states""": hidden_states}

					if include_temb:
						A								=				128
						A								=				randn_tensor((batch_size, temb_channels)				,   generator=a__				,   device=a__   )

					if include_res_hidden_states_tuple:
						A								=				torch.manual_seed(1   )
						A								=				(randn_tensor(a__				,   generator=a__				,   device=a__   ),)

					if include_encoder_hidden_states:
						A								=				floats_tensor((batch_size, 32, 32)   ).to(a__   )

					if include_skip_sample:
						A								=				randn_tensor(((batch_size, 3) + sizes)				,   generator=a__				,   device=a__   )

					return dummy_input

				def      _UpperCAmelCase					(						self   )	->				int:
					A								=				{
					    """in_channels""": 32,
					    """out_channels""": 32,
					    """temb_channels""": 128,
					}
					if self.block_type == "up":
						A								=				32

					if self.block_type == "mid":
						init_dict.pop("""out_channels"""   )

					A								=				self.dummy_input
					return init_dict, inputs_dict

				def      _UpperCAmelCase					(						self				,   a__   )	->				Optional[int]:
					A					,			A								=				self.prepare_init_args_and_inputs_for_common()
					A								=				self.block_class(**a__   )
					unet_block.to(a__   )
					unet_block.eval()

					with torch.no_grad():
						A								=				unet_block(**a__   )

					if isinstance(a__				,   a__   ):
						A								=				output[0]

					self.assertEqual(output.shape				,   self.output_shape   )

					A								=				output[0, -1, -3:, -3:]
					A								=				torch.tensor(a__   ).to(a__   )
					assert torch_all_close(output_slice.flatten()				,   a__				,   atol=5e-3   )

				@unittest.skipIf(torch_device == """mps"""				,   """Training is not supported in mps"""   )
				def      _UpperCAmelCase					(						self   )	->				str:
					A					,			A								=				self.prepare_init_args_and_inputs_for_common()
					A								=				self.block_class(**a__   )
					model.to(a__   )
					model.train()
					A								=				model(**a__   )

					if isinstance(a__				,   a__   ):
						A								=				output[0]

					A								=				torch.device(a__   )
					A								=				randn_tensor(output.shape				,   device=a__   )
					A								=				torch.nn.functional.mse_loss(a__				,   a__   )
					loss.backward()



