

from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


a		="""CompVis/stable-diffusion-v1-1"""
a		="""CompVis/stable-diffusion-v1-2"""
a		="""CompVis/stable-diffusion-v1-3"""
a		="""CompVis/stable-diffusion-v1-4"""






class 	A_		(	SCREAMING_SNAKE_CASE    ):
							def __init__(		self		:      Any       ,SCREAMING_SNAKE_CASE__		:      AutoencoderKL       ,SCREAMING_SNAKE_CASE__		:      CLIPTextModel       ,SCREAMING_SNAKE_CASE__		:      CLIPTokenizer       ,SCREAMING_SNAKE_CASE__		:      UNetaDConditionModel       ,SCREAMING_SNAKE_CASE__		:      Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]       ,SCREAMING_SNAKE_CASE__		:      StableDiffusionSafetyChecker       ,SCREAMING_SNAKE_CASE__		:      CLIPImageProcessor       ,SCREAMING_SNAKE_CASE__		:      bool = True       ,):
								super()._init_()

								__lowerCamelCase    :    List[Any] 	=			StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    str 	=			StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    Union[str, Any] 	=			StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    Dict 	=			StableDiffusionPipeline(
								    vae=SCREAMING_SNAKE_CASE__       ,text_encoder=SCREAMING_SNAKE_CASE__       ,tokenizer=SCREAMING_SNAKE_CASE__       ,unet=SCREAMING_SNAKE_CASE__       ,scheduler=SCREAMING_SNAKE_CASE__       ,safety_checker=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__       ,requires_safety_checker=SCREAMING_SNAKE_CASE__       ,)

								self.register_modules(pipelinea=self.pipea       ,pipelinea=self.pipea       ,pipelinea=self.pipea       ,pipelinea=self.pipea)
							@property
							def    lowerCAmelCase					(		self		:      int):
								return {k: getattr(self       ,SCREAMING_SNAKE_CASE__) for k in self.config.keys() if not k.startswith('_')}
							def    lowerCAmelCase					(		self		:      int       ,SCREAMING_SNAKE_CASE__		:      Optional[Union[str, int]] = "auto"):
								if slice_size == "auto":
									# half the attention head size is usually a good trade-off between
									# speed and memory
									__lowerCamelCase    :    Any 	=			self.unet.config.attention_head_dim // 2
								self.unet.set_attention_slice(SCREAMING_SNAKE_CASE__)
							def    lowerCAmelCase					(		self		:      Optional[Any]):
								self.enable_attention_slicing(SCREAMING_SNAKE_CASE__)
							@torch.no_grad()
							def    lowerCAmelCase					(		self		:      List[Any]       ,SCREAMING_SNAKE_CASE__		:      Union[str, List[str]]       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_0       ,SCREAMING_SNAKE_CASE__		:      float = 7.5       ,SCREAMING_SNAKE_CASE__		:      Optional[Union[str, List[str]]] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[int] = 1       ,SCREAMING_SNAKE_CASE__		:      float = 0.0       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.Generator] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.FloatTensor] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[str] = "pil"       ,SCREAMING_SNAKE_CASE__		:      bool = True       ,SCREAMING_SNAKE_CASE__		:      Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,SCREAMING_SNAKE_CASE__		:      int = 1       ,**SCREAMING_SNAKE_CASE__		:      Any       ,):
								return self.pipea(
								    prompt=SCREAMING_SNAKE_CASE__       ,height=SCREAMING_SNAKE_CASE__       ,width=SCREAMING_SNAKE_CASE__       ,num_inference_steps=SCREAMING_SNAKE_CASE__       ,guidance_scale=SCREAMING_SNAKE_CASE__       ,negative_prompt=SCREAMING_SNAKE_CASE__       ,num_images_per_prompt=SCREAMING_SNAKE_CASE__       ,eta=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,latents=SCREAMING_SNAKE_CASE__       ,output_type=SCREAMING_SNAKE_CASE__       ,return_dict=SCREAMING_SNAKE_CASE__       ,callback=SCREAMING_SNAKE_CASE__       ,callback_steps=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)
							@torch.no_grad()
							def    lowerCAmelCase					(		self		:      List[Any]       ,SCREAMING_SNAKE_CASE__		:      Union[str, List[str]]       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_0       ,SCREAMING_SNAKE_CASE__		:      float = 7.5       ,SCREAMING_SNAKE_CASE__		:      Optional[Union[str, List[str]]] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[int] = 1       ,SCREAMING_SNAKE_CASE__		:      float = 0.0       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.Generator] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.FloatTensor] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[str] = "pil"       ,SCREAMING_SNAKE_CASE__		:      bool = True       ,SCREAMING_SNAKE_CASE__		:      Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,SCREAMING_SNAKE_CASE__		:      int = 1       ,**SCREAMING_SNAKE_CASE__		:      List[str]       ,):
								return self.pipea(
								    prompt=SCREAMING_SNAKE_CASE__       ,height=SCREAMING_SNAKE_CASE__       ,width=SCREAMING_SNAKE_CASE__       ,num_inference_steps=SCREAMING_SNAKE_CASE__       ,guidance_scale=SCREAMING_SNAKE_CASE__       ,negative_prompt=SCREAMING_SNAKE_CASE__       ,num_images_per_prompt=SCREAMING_SNAKE_CASE__       ,eta=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,latents=SCREAMING_SNAKE_CASE__       ,output_type=SCREAMING_SNAKE_CASE__       ,return_dict=SCREAMING_SNAKE_CASE__       ,callback=SCREAMING_SNAKE_CASE__       ,callback_steps=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)
							@torch.no_grad()
							def    lowerCAmelCase					(		self		:      Tuple       ,SCREAMING_SNAKE_CASE__		:      Union[str, List[str]]       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_0       ,SCREAMING_SNAKE_CASE__		:      float = 7.5       ,SCREAMING_SNAKE_CASE__		:      Optional[Union[str, List[str]]] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[int] = 1       ,SCREAMING_SNAKE_CASE__		:      float = 0.0       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.Generator] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.FloatTensor] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[str] = "pil"       ,SCREAMING_SNAKE_CASE__		:      bool = True       ,SCREAMING_SNAKE_CASE__		:      Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,SCREAMING_SNAKE_CASE__		:      int = 1       ,**SCREAMING_SNAKE_CASE__		:      int       ,):
								return self.pipea(
								    prompt=SCREAMING_SNAKE_CASE__       ,height=SCREAMING_SNAKE_CASE__       ,width=SCREAMING_SNAKE_CASE__       ,num_inference_steps=SCREAMING_SNAKE_CASE__       ,guidance_scale=SCREAMING_SNAKE_CASE__       ,negative_prompt=SCREAMING_SNAKE_CASE__       ,num_images_per_prompt=SCREAMING_SNAKE_CASE__       ,eta=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,latents=SCREAMING_SNAKE_CASE__       ,output_type=SCREAMING_SNAKE_CASE__       ,return_dict=SCREAMING_SNAKE_CASE__       ,callback=SCREAMING_SNAKE_CASE__       ,callback_steps=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)
							@torch.no_grad()
							def    lowerCAmelCase					(		self		:      Optional[int]       ,SCREAMING_SNAKE_CASE__		:      Union[str, List[str]]       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_0       ,SCREAMING_SNAKE_CASE__		:      float = 7.5       ,SCREAMING_SNAKE_CASE__		:      Optional[Union[str, List[str]]] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[int] = 1       ,SCREAMING_SNAKE_CASE__		:      float = 0.0       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.Generator] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.FloatTensor] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[str] = "pil"       ,SCREAMING_SNAKE_CASE__		:      bool = True       ,SCREAMING_SNAKE_CASE__		:      Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,SCREAMING_SNAKE_CASE__		:      int = 1       ,**SCREAMING_SNAKE_CASE__		:      List[str]       ,):
								return self.pipea(
								    prompt=SCREAMING_SNAKE_CASE__       ,height=SCREAMING_SNAKE_CASE__       ,width=SCREAMING_SNAKE_CASE__       ,num_inference_steps=SCREAMING_SNAKE_CASE__       ,guidance_scale=SCREAMING_SNAKE_CASE__       ,negative_prompt=SCREAMING_SNAKE_CASE__       ,num_images_per_prompt=SCREAMING_SNAKE_CASE__       ,eta=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,latents=SCREAMING_SNAKE_CASE__       ,output_type=SCREAMING_SNAKE_CASE__       ,return_dict=SCREAMING_SNAKE_CASE__       ,callback=SCREAMING_SNAKE_CASE__       ,callback_steps=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)


							@torch.no_grad()
							def    lowerCAmelCase					(		self		:      Dict       ,SCREAMING_SNAKE_CASE__		:      Union[str, List[str]]       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_1_2       ,SCREAMING_SNAKE_CASE__		:      int = 5_0       ,SCREAMING_SNAKE_CASE__		:      float = 7.5       ,SCREAMING_SNAKE_CASE__		:      Optional[Union[str, List[str]]] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[int] = 1       ,SCREAMING_SNAKE_CASE__		:      float = 0.0       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.Generator] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[torch.FloatTensor] = None       ,SCREAMING_SNAKE_CASE__		:      Optional[str] = "pil"       ,SCREAMING_SNAKE_CASE__		:      bool = True       ,SCREAMING_SNAKE_CASE__		:      Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,SCREAMING_SNAKE_CASE__		:      int = 1       ,**SCREAMING_SNAKE_CASE__		:      List[Any]       ,):
								__lowerCamelCase    :    Optional[Any] 	=			'cuda' if torch.cuda.is_available() else 'cpu'
								self.to(SCREAMING_SNAKE_CASE__)

								# Checks if the height and width are divisible by 8 or not
								if height % 8 != 0 or width % 8 != 0:
									raise ValueError(F"`height` and `width` must be divisible by 8 but are {height} and {width}.")

								# Get first result from Stable Diffusion Checkpoint v1.1
								__lowerCamelCase    :    str 	=			self.textaimg_sda_a(
								    prompt=SCREAMING_SNAKE_CASE__       ,height=SCREAMING_SNAKE_CASE__       ,width=SCREAMING_SNAKE_CASE__       ,num_inference_steps=SCREAMING_SNAKE_CASE__       ,guidance_scale=SCREAMING_SNAKE_CASE__       ,negative_prompt=SCREAMING_SNAKE_CASE__       ,num_images_per_prompt=SCREAMING_SNAKE_CASE__       ,eta=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,latents=SCREAMING_SNAKE_CASE__       ,output_type=SCREAMING_SNAKE_CASE__       ,return_dict=SCREAMING_SNAKE_CASE__       ,callback=SCREAMING_SNAKE_CASE__       ,callback_steps=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)

								# Get first result from Stable Diffusion Checkpoint v1.2
								__lowerCamelCase    :    str 	=			self.textaimg_sda_a(
								    prompt=SCREAMING_SNAKE_CASE__       ,height=SCREAMING_SNAKE_CASE__       ,width=SCREAMING_SNAKE_CASE__       ,num_inference_steps=SCREAMING_SNAKE_CASE__       ,guidance_scale=SCREAMING_SNAKE_CASE__       ,negative_prompt=SCREAMING_SNAKE_CASE__       ,num_images_per_prompt=SCREAMING_SNAKE_CASE__       ,eta=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,latents=SCREAMING_SNAKE_CASE__       ,output_type=SCREAMING_SNAKE_CASE__       ,return_dict=SCREAMING_SNAKE_CASE__       ,callback=SCREAMING_SNAKE_CASE__       ,callback_steps=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)

								# Get first result from Stable Diffusion Checkpoint v1.3
								__lowerCamelCase    :    List[str] 	=			self.textaimg_sda_a(
								    prompt=SCREAMING_SNAKE_CASE__       ,height=SCREAMING_SNAKE_CASE__       ,width=SCREAMING_SNAKE_CASE__       ,num_inference_steps=SCREAMING_SNAKE_CASE__       ,guidance_scale=SCREAMING_SNAKE_CASE__       ,negative_prompt=SCREAMING_SNAKE_CASE__       ,num_images_per_prompt=SCREAMING_SNAKE_CASE__       ,eta=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,latents=SCREAMING_SNAKE_CASE__       ,output_type=SCREAMING_SNAKE_CASE__       ,return_dict=SCREAMING_SNAKE_CASE__       ,callback=SCREAMING_SNAKE_CASE__       ,callback_steps=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)

								# Get first result from Stable Diffusion Checkpoint v1.4
								__lowerCamelCase    :    str 	=			self.textaimg_sda_a(
								    prompt=SCREAMING_SNAKE_CASE__       ,height=SCREAMING_SNAKE_CASE__       ,width=SCREAMING_SNAKE_CASE__       ,num_inference_steps=SCREAMING_SNAKE_CASE__       ,guidance_scale=SCREAMING_SNAKE_CASE__       ,negative_prompt=SCREAMING_SNAKE_CASE__       ,num_images_per_prompt=SCREAMING_SNAKE_CASE__       ,eta=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,latents=SCREAMING_SNAKE_CASE__       ,output_type=SCREAMING_SNAKE_CASE__       ,return_dict=SCREAMING_SNAKE_CASE__       ,callback=SCREAMING_SNAKE_CASE__       ,callback_steps=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)

								# Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
								return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]])







import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

a		=3






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					int:
	print('Generating primitive root of p'      )
	while True:
		__lowerCamelCase    :    Tuple 	=			random.randrange(3     ,			lowerCamelCase__      )
		if pow(lowerCamelCase__     ,			2     ,			lowerCamelCase__      ) == 1:
			continue
		if pow(lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__      ) == 1:
			continue
		return g






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

	__lowerCamelCase    :    int 	=			(key_size, e_a, e_a, p)
	__lowerCamelCase    :    str 	=			(key_size, d)

	return public_key, private_key






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

	__lowerCamelCase	,			__lowerCamelCase    :    List[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        SCREAMING_SNAKE_CASE__	(  )  ->					None:
	print('Making key files...'      )
	make_key_files('elgamal'     ,			2_0_4_8      )
	print('Key files generation successful'      )


if __name__ == "__main__":
					main()




