






'''simple docstring'''




from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin







@dataclass
class       __lowercase					(_snake_case    ):
	_lowerCamelCase              =  42







class       __lowercase					(_snake_case   ,				_snake_case    ):

	@register_to_config
	def __init__(      self   :						Optional[int]  ,					UpperCAmelCase_   :						int = 16  ,					UpperCAmelCase_   :						int = 88  ,					UpperCAmelCase_   :						Optional[int] = None  ,					UpperCAmelCase_   :						Optional[int] = None  ,					UpperCAmelCase_   :						int = 1  ,					UpperCAmelCase_   :						float = 0.0  ,					UpperCAmelCase_   :						int = 32  ,					UpperCAmelCase_   :						Optional[int] = None  ,					UpperCAmelCase_   :						bool = False  ,					UpperCAmelCase_   :						Optional[int] = None  ,					UpperCAmelCase_   :						str = "geglu"  ,					UpperCAmelCase_   :						bool = True  ,					UpperCAmelCase_   :						bool = True  ,					):
			super().__init__()
			UpperCamelCase__				:					int					=					num_attention_heads
			UpperCamelCase__				:					Union[str, Any]					=					attention_head_dim
			UpperCamelCase__				:					Any					=					num_attention_heads * attention_head_dim

			UpperCamelCase__				:					Tuple					=					in_channels

			UpperCamelCase__				:					Dict					=					torch.nn.GroupNorm(num_groups=UpperCAmelCase_  ,					num_channels=UpperCAmelCase_  ,					eps=1e-6  ,					affine=UpperCAmelCase_)
			UpperCamelCase__				:					Optional[Any]					=					nn.Linear(UpperCAmelCase_  ,					UpperCAmelCase_)

			# 3. Define transformers blocks
			UpperCamelCase__				:					Tuple					=					nn.ModuleList(
			    [
			        BasicTransformerBlock(
			            UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					dropout=UpperCAmelCase_  ,					cross_attention_dim=UpperCAmelCase_  ,					activation_fn=UpperCAmelCase_  ,					attention_bias=UpperCAmelCase_  ,					double_self_attention=UpperCAmelCase_  ,					norm_elementwise_affine=UpperCAmelCase_  ,					)
			        for d in range(UpperCAmelCase_)
			    ])

			UpperCamelCase__				:					List[str]					=					nn.Linear(UpperCAmelCase_  ,					UpperCAmelCase_)





	def      __UpperCamelCase							(      self   :						Dict  ,					UpperCAmelCase_   :						Tuple  ,					UpperCAmelCase_   :						Optional[int]=None  ,					UpperCAmelCase_   :						int=None  ,					UpperCAmelCase_   :						Dict=None  ,					UpperCAmelCase_   :						str=1  ,					UpperCAmelCase_   :						int=None  ,					UpperCAmelCase_   :						bool = True  ,					):
			UpperCamelCase__,	UpperCamelCase__,	UpperCamelCase__,	UpperCamelCase__				:					List[Any]					=					hidden_states.shape
			UpperCamelCase__				:					List[Any]					=					batch_frames // num_frames

			UpperCamelCase__				:					Any					=					hidden_states

			UpperCamelCase__				:					Dict					=					hidden_states[None, :].reshape(UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_)
			UpperCamelCase__				:					str					=					hidden_states.permute(0  ,					2  ,					1  ,					3  ,					4)

			UpperCamelCase__				:					List[Any]					=					self.norm(UpperCAmelCase_)
			UpperCamelCase__				:					Optional[Any]					=					hidden_states.permute(0  ,					3  ,					4  ,					2  ,					1).reshape(batch_size * height * width  ,					UpperCAmelCase_  ,					UpperCAmelCase_)

			UpperCamelCase__				:					Optional[Any]					=					self.proj_in(UpperCAmelCase_)

			# 2. Blocks
			for block in self.transformer_blocks:
					UpperCamelCase__				:					Optional[Any]					=					block(
					    UpperCAmelCase_  ,					encoder_hidden_states=UpperCAmelCase_  ,					timestep=UpperCAmelCase_  ,					cross_attention_kwargs=UpperCAmelCase_  ,					class_labels=UpperCAmelCase_  ,					)

			# 3. Output
			UpperCamelCase__				:					Optional[Any]					=					self.proj_out(UpperCAmelCase_)
			UpperCamelCase__				:					Union[str, Any]					=					(
			    hidden_states[None, None, :]
			    .reshape(UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_)
			    .permute(0  ,					3  ,					4  ,					1  ,					2)
			    .contiguous()
			)
			UpperCamelCase__				:					Tuple					=					hidden_states.reshape(UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_)

			UpperCamelCase__				:					Union[str, Any]					=					hidden_states + residual

			if not return_dict:
					return (output,)

			return TransformerTemporalModelOutput(sample=UpperCAmelCase_)












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




