



import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MODEL_MAPPING,
							    SegformerForImageClassification,
							    SegformerForSemanticSegmentation,
							    SegformerModel,
							)
							from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import SegformerImageProcessor







class       __lowerCAmelCase   (      A						):



			def    _lowerCamelCase	(						self       :  int)       ->   List[str]:



						"""simple docstring"""






						_UpperCAmelCase			=       self.config_class(**self.inputs_dict)
						self.parent.assertTrue(hasattr(A			,		'hidden_sizes'))
						self.parent.assertTrue(hasattr(A			,		'num_attention_heads'))
						self.parent.assertTrue(hasattr(A			,		'num_encoder_blocks'))







class       __lowerCAmelCase   :



			def __init__(						self       :  Optional[Any]			,		A       :  List[str]			,		A       :  List[Any]=13			,		A       :  List[str]=64			,		A       :  List[Any]=3			,		A       :  Any=4			,		A       :  Optional[Any]=[2, 2, 2, 2]			,		A       :  List[Any]=[8, 4, 2, 1]			,		A       :  Union[str, Any]=[16, 32, 64, 1_28]			,		A       :  Dict=[1, 4, 8, 16]			,		A       :  Optional[Any]=[1, 2, 4, 8]			,		A       :  Union[str, Any]=True			,		A       :  Optional[int]=True			,		A       :  Optional[int]="gelu"			,		A       :  str=0.1			,		A       :  Union[str, Any]=0.1			,		A       :  List[Any]=0.0_2			,		A       :  Any=3			,		A       :  Union[str, Any]=None			,		)       ->   int:



						"""simple docstring"""






						_UpperCAmelCase			=       parent
						_UpperCAmelCase			=       batch_size
						_UpperCAmelCase			=       image_size
						_UpperCAmelCase			=       num_channels
						_UpperCAmelCase			=       num_encoder_blocks
						_UpperCAmelCase			=       sr_ratios
						_UpperCAmelCase			=       depths
						_UpperCAmelCase			=       hidden_sizes
						_UpperCAmelCase			=       downsampling_rates
						_UpperCAmelCase			=       num_attention_heads
						_UpperCAmelCase			=       is_training
						_UpperCAmelCase			=       use_labels
						_UpperCAmelCase			=       hidden_act
						_UpperCAmelCase			=       hidden_dropout_prob
						_UpperCAmelCase			=       attention_probs_dropout_prob
						_UpperCAmelCase			=       initializer_range
						_UpperCAmelCase			=       num_labels
						_UpperCAmelCase			=       scope



			def    _lowerCamelCase	(						self       :  Tuple)       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

						_UpperCAmelCase			=       None
						if self.use_labels:
									_UpperCAmelCase			=       ids_tensor([self.batch_size, self.image_size, self.image_size]			,		self.num_labels)

						_UpperCAmelCase			=       self.get_config()
						return config, pixel_values, labels



			def    _lowerCamelCase	(						self       :  Any)       ->   Any:



						"""simple docstring"""






						return SegformerConfig(
						    image_size=self.image_size			,		num_channels=self.num_channels			,		num_encoder_blocks=self.num_encoder_blocks			,		depths=self.depths			,		hidden_sizes=self.hidden_sizes			,		num_attention_heads=self.num_attention_heads			,		hidden_act=self.hidden_act			,		hidden_dropout_prob=self.hidden_dropout_prob			,		attention_probs_dropout_prob=self.attention_probs_dropout_prob			,		initializer_range=self.initializer_range			,		)



			def    _lowerCamelCase	(						self       :  str			,		A       :  List[Any]			,		A       :  int			,		A       :  Optional[int])       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       SegformerModel(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase			=       model(A)
						_UpperCAmelCase			=       _UpperCAmelCase			=       self.image_size // (self.downsampling_rates[-1] * 2)
						self.parent.assertEqual(
						    result.last_hidden_state.shape			,		(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width))



			def    _lowerCamelCase	(						self       :  Any			,		A       :  int			,		A       :  Any			,		A       :  Optional[int])       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       self.num_labels
						_UpperCAmelCase			=       SegformerForSemanticSegmentation(A)
						model.to(A)
						model.eval()
						_UpperCAmelCase			=       model(A)
						self.parent.assertEqual(
						    result.logits.shape			,		(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4))
						_UpperCAmelCase			=       model(A			,		labels=A)
						self.parent.assertEqual(
						    result.logits.shape			,		(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4))
						self.parent.assertGreater(result.loss			,		0.0)



			def    _lowerCamelCase	(						self       :  Dict			,		A       :  Union[str, Any]			,		A       :  Any			,		A       :  Union[str, Any])       ->   Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       1
						_UpperCAmelCase			=       SegformerForSemanticSegmentation(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase			=       torch.randint(0			,		1			,		(self.batch_size, self.image_size, self.image_size)).to(A)
						_UpperCAmelCase			=       model(A			,		labels=A)
						self.parent.assertGreater(result.loss			,		0.0)



			def    _lowerCamelCase	(						self       :  List[Any])       ->   List[str]:



						"""simple docstring"""






						_UpperCAmelCase			=       self.prepare_config_and_inputs()
						_UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase			=       config_and_inputs
						_UpperCAmelCase			=       {'pixel_values': pixel_values}
						return config, inputs_dict







@require_torch
class       __lowerCAmelCase   (      A    ,						A    ,						unittest.TestCase						):
			UpperCamelCase        =					(
			    (
			        SegformerModel,
			        SegformerForSemanticSegmentation,
			        SegformerForImageClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase        =					(
			    {
			        '''feature-extraction''': SegformerModel,
			        '''image-classification''': SegformerForImageClassification,
			        '''image-segmentation''': SegformerForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			UpperCamelCase        =					True
			UpperCamelCase        =					False
			UpperCamelCase        =					False
			UpperCamelCase        =					False



			def    _lowerCamelCase	(						self       :  Dict)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       SegformerModelTester(self)
						_UpperCAmelCase			=       SegformerConfigTester(self			,		config_class=A)



			def    _lowerCamelCase	(						self       :  Tuple)       ->   List[Any]:



						"""simple docstring"""






						self.config_tester.run_common_tests()



			def    _lowerCamelCase	(						self       :  Optional[Any])       ->   Dict:



						"""simple docstring"""






						_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A)



			def    _lowerCamelCase	(						self       :  Dict)       ->   List[str]:



						"""simple docstring"""






						_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_binary_image_segmentation(*A)



			def    _lowerCamelCase	(						self       :  List[Any])       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_segmentation(*A)



			@unittest.skip('SegFormer does not use inputs_embeds')
			def    _lowerCamelCase	(						self       :  Union[str, Any])       ->   Tuple:



						"""simple docstring"""






						pass



			@unittest.skip('SegFormer does not have get_input_embeddings method and get_output_embeddings methods')
			def    _lowerCamelCase	(						self       :  List[str])       ->   Optional[Any]:



						"""simple docstring"""






						pass



			def    _lowerCamelCase	(						self       :  List[Any])       ->   Tuple:



						"""simple docstring"""






						_UpperCAmelCase	,					_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									_UpperCAmelCase			=       model_class(A)
									_UpperCAmelCase			=       inspect.signature(model.forward)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									_UpperCAmelCase			=       [*signature.parameters.keys()]

									_UpperCAmelCase			=       ['pixel_values']
									self.assertListEqual(arg_names[:1]			,		A)



			def    _lowerCamelCase	(						self       :  Tuple)       ->   List[Any]:



						"""simple docstring"""






						_UpperCAmelCase	,					_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs_for_common()
						_UpperCAmelCase			=       True

						for model_class in self.all_model_classes:
									_UpperCAmelCase			=       True
									_UpperCAmelCase			=       False
									_UpperCAmelCase			=       True
									_UpperCAmelCase			=       model_class(A)
									model.to(A)
									model.eval()
									with torch.no_grad():
												_UpperCAmelCase			=       model(**self._prepare_for_class(A			,		A))
									_UpperCAmelCase			=       outputs.attentions

									_UpperCAmelCase			=       sum(self.model_tester.depths)
									self.assertEqual(len(A)			,		A)

									# check that output_attentions also work using config
									del inputs_dict["output_attentions"]
									_UpperCAmelCase			=       True
									_UpperCAmelCase			=       model_class(A)
									model.to(A)
									model.eval()
									with torch.no_grad():
												_UpperCAmelCase			=       model(**self._prepare_for_class(A			,		A))
									_UpperCAmelCase			=       outputs.attentions

									self.assertEqual(len(A)			,		A)

									# verify the first attentions (first block, first layer)
									_UpperCAmelCase			=       (self.model_tester.image_size // 4) ** 2
									_UpperCAmelCase			=       (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
									self.assertListEqual(
									    list(attentions[0].shape[-3:])			,		[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]			,		)

									# verify the last attentions (last block, last layer)
									_UpperCAmelCase			=       (self.model_tester.image_size // 32) ** 2
									_UpperCAmelCase			=       (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
									self.assertListEqual(
									    list(attentions[-1].shape[-3:])			,		[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]			,		)
									_UpperCAmelCase			=       len(A)

									# Check attention is always last and order is fine
									_UpperCAmelCase			=       True
									_UpperCAmelCase			=       True
									_UpperCAmelCase			=       model_class(A)
									model.to(A)
									model.eval()
									with torch.no_grad():
												_UpperCAmelCase			=       model(**self._prepare_for_class(A			,		A))

									self.assertEqual(out_len + 1			,		len(A))

									_UpperCAmelCase			=       outputs.attentions

									self.assertEqual(len(A)			,		A)
									# verify the first attentions (first block, first layer)
									_UpperCAmelCase			=       (self.model_tester.image_size // 4) ** 2
									_UpperCAmelCase			=       (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
									self.assertListEqual(
									    list(self_attentions[0].shape[-3:])			,		[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]			,		)



			def    _lowerCamelCase	(						self       :  str)       ->   Optional[int]:



						"""simple docstring"""






						def check_hidden_states_output(A       :  Union[str, Any]			,		A       :  List[Any]			,		A       :  List[str]):
									_UpperCAmelCase			=       model_class(A)
									model.to(A)
									model.eval()

									with torch.no_grad():
												_UpperCAmelCase			=       model(**self._prepare_for_class(A			,		A))

									_UpperCAmelCase			=       outputs.hidden_states

									_UpperCAmelCase			=       self.model_tester.num_encoder_blocks
									self.assertEqual(len(A)			,		A)

									# verify the first hidden states (first block)
									self.assertListEqual(
									    list(hidden_states[0].shape[-3:])			,		[
									        self.model_tester.hidden_sizes[0],
									        self.model_tester.image_size // 4,
									        self.model_tester.image_size // 4,
									    ]			,		)

						_UpperCAmelCase	,					_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									_UpperCAmelCase			=       True
									check_hidden_states_output(A			,		A			,		A)

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									_UpperCAmelCase			=       True

									check_hidden_states_output(A			,		A			,		A)



			def    _lowerCamelCase	(						self       :  List[str])       ->   Union[str, Any]:



						"""simple docstring"""






						if not self.model_tester.is_training:
									return

						_UpperCAmelCase	,					_UpperCAmelCase			=       self.model_tester.prepare_config_and_inputs_for_common()
						_UpperCAmelCase			=       True

						for model_class in self.all_model_classes:
									if model_class in get_values(A):
												continue

									_UpperCAmelCase			=       model_class(A)
									model.to(A)
									model.train()
									_UpperCAmelCase			=       self._prepare_for_class(A			,		A			,		return_labels=A)
									_UpperCAmelCase			=       model(**A).loss
									loss.backward()



			@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
			def    _lowerCamelCase	(						self       :  Tuple)       ->   List[str]:



						"""simple docstring"""






						pass



			@slow
			def    _lowerCamelCase	(						self       :  List[str])       ->   Optional[int]:



						"""simple docstring"""






						for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_UpperCAmelCase			=       SegformerModel.from_pretrained(A)
									self.assertIsNotNone(A)





def 							A     (					)    ->			List[Any]:







			'''simple docstring'''
			_UpperCAmelCase			=       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
			return image







@require_torch
class       __lowerCAmelCase   (      unittest.TestCase						):



			@slow
			def    _lowerCamelCase	(						self       :  int)       ->   int:



						"""simple docstring"""






						_UpperCAmelCase			=       SegformerImageProcessor(
						    image_scale=(5_12, 5_12)			,		keep_ratio=A			,		align=A			,		do_random_crop=A)
						_UpperCAmelCase			=       SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512').to(
						    A)

						_UpperCAmelCase			=       prepare_img()
						_UpperCAmelCase			=       image_processor(images=A			,		return_tensors='pt')
						_UpperCAmelCase			=       encoded_inputs.pixel_values.to(A)

						with torch.no_grad():
									_UpperCAmelCase			=       model(A)

						_UpperCAmelCase			=       torch.Size((1, model.config.num_labels, 1_28, 1_28))
						self.assertEqual(outputs.logits.shape			,		A)

						_UpperCAmelCase			=       torch.tensor(
						    [
						        [[-4.6_3_1_0, -5.5_2_3_2, -6.2_3_5_6], [-5.1_9_2_1, -6.1_4_4_4, -6.5_9_9_6], [-5.4_4_2_4, -6.2_7_9_0, -6.7_5_7_4]],
						        [[-1_2.1_3_9_1, -1_3.3_1_2_2, -1_3.9_5_5_4], [-1_2.8_7_3_2, -1_3.9_3_5_2, -1_4.3_5_6_3], [-1_2.9_4_3_8, -1_3.8_2_2_6, -1_4.2_5_1_3]],
						        [[-1_2.5_1_3_4, -1_3.4_6_8_6, -1_4.4_9_1_5], [-1_2.8_6_6_9, -1_4.4_3_4_3, -1_4.7_7_5_8], [-1_3.2_5_2_3, -1_4.5_8_1_9, -1_5.0_6_9_4]],
						    ]).to(A)
						self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]			,		A			,		atol=1E-4))



			@slow
			def    _lowerCamelCase	(						self       :  List[Any])       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       SegformerImageProcessor(
						    image_scale=(5_12, 5_12)			,		keep_ratio=A			,		align=A			,		do_random_crop=A)
						_UpperCAmelCase			=       SegformerForSemanticSegmentation.from_pretrained(
						    'nvidia/segformer-b1-finetuned-cityscapes-1024-1024').to(A)

						_UpperCAmelCase			=       prepare_img()
						_UpperCAmelCase			=       image_processor(images=A			,		return_tensors='pt')
						_UpperCAmelCase			=       encoded_inputs.pixel_values.to(A)

						with torch.no_grad():
									_UpperCAmelCase			=       model(A)

						_UpperCAmelCase			=       torch.Size((1, model.config.num_labels, 1_28, 1_28))
						self.assertEqual(outputs.logits.shape			,		A)

						_UpperCAmelCase			=       torch.tensor(
						    [
						        [[-1_3.5_7_4_8, -1_3.9_1_1_1, -1_2.6_5_0_0], [-1_4.3_5_0_0, -1_5.3_6_8_3, -1_4.2_3_2_8], [-1_4.7_5_3_2, -1_6.0_4_2_4, -1_5.6_0_8_7]],
						        [[-1_7.1_6_5_1, -1_5.8_7_2_5, -1_2.9_6_5_3], [-1_7.2_5_8_0, -1_7.3_7_1_8, -1_4.8_2_2_3], [-1_6.6_0_5_8, -1_6.8_7_8_3, -1_6.7_4_5_2]],
						        [[-3.6_4_5_6, -3.0_2_0_9, -1.4_2_0_3], [-3.0_7_9_7, -3.1_9_5_9, -2.0_0_0_0], [-1.8_7_5_7, -1.9_2_1_7, -1.6_9_9_7]],
						    ]).to(A)
						self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]			,		A			,		atol=1E-1))



			@slow
			def    _lowerCamelCase	(						self       :  Any)       ->   Dict:



						"""simple docstring"""






						_UpperCAmelCase			=       SegformerImageProcessor(
						    image_scale=(5_12, 5_12)			,		keep_ratio=A			,		align=A			,		do_random_crop=A)
						_UpperCAmelCase			=       SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b0-finetuned-ade-512-512').to(
						    A)

						_UpperCAmelCase			=       prepare_img()
						_UpperCAmelCase			=       image_processor(images=A			,		return_tensors='pt')
						_UpperCAmelCase			=       encoded_inputs.pixel_values.to(A)

						with torch.no_grad():
									_UpperCAmelCase			=       model(A)

						_UpperCAmelCase			=       outputs.logits.detach().cpu()

						_UpperCAmelCase			=       image_processor.post_process_semantic_segmentation(outputs=A			,		target_sizes=[(5_00, 3_00)])
						_UpperCAmelCase			=       torch.Size((5_00, 3_00))
						self.assertEqual(segmentation[0].shape			,		A)

						_UpperCAmelCase			=       image_processor.post_process_semantic_segmentation(outputs=A)
						_UpperCAmelCase			=       torch.Size((1_28, 1_28))
						self.assertEqual(segmentation[0].shape			,		A)





from binascii import hexlify
from hashlib import shaaaa
from os import urandom

# RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for
# Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526

UpperCAmelCase__       =	{
    # 1536-bit
    5: {
        "prime": int(
            "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
            + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
            + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
            + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
            + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
            + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
            + "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
            + "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF",
            base=16,
        ),
        "generator": 2,
    },
    # 2048-bit
    14: {
        "prime": int(
            "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
            + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
            + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
            + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
            + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
            + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
            + "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
            + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
            + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
            + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
            + "15728E5A8AACAA68FFFFFFFFFFFFFFFF",
            base=16,
        ),
        "generator": 2,
    },
    # 3072-bit
    15: {
        "prime": int(
            "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
            + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
            + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
            + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
            + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
            + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
            + "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
            + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
            + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
            + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
            + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
            + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
            + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
            + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
            + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
            + "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF",
            base=16,
        ),
        "generator": 2,
    },
    # 4096-bit
    16: {
        "prime": int(
            "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
            + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
            + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
            + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
            + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
            + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
            + "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
            + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
            + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
            + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
            + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
            + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
            + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
            + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
            + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
            + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
            + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
            + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
            + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
            + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
            + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
            + "FFFFFFFFFFFFFFFF",
            base=16,
        ),
        "generator": 2,
    },
    # 6144-bit
    17: {
        "prime": int(
            "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
            + "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
            + "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
            + "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
            + "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
            + "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
            + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
            + "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
            + "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
            + "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
            + "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
            + "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
            + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
            + "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
            + "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
            + "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
            + "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
            + "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
            + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406"
            + "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918"
            + "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151"
            + "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03"
            + "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F"
            + "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
            + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B"
            + "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632"
            + "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E"
            + "6DCC4024FFFFFFFFFFFFFFFF",
            base=16,
        ),
        "generator": 2,
    },
    # 8192-bit
    18: {
        "prime": int(
            "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
            + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
            + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
            + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
            + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
            + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
            + "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
            + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
            + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
            + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
            + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
            + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
            + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
            + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
            + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
            + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
            + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
            + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
            + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
            + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
            + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
            + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD"
            + "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831"
            + "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
            + "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF"
            + "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6"
            + "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3"
            + "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
            + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328"
            + "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C"
            + "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE"
            + "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4"
            + "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300"
            + "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568"
            + "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9"
            + "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B"
            + "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A"
            + "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36"
            + "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1"
            + "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92"
            + "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47"
            + "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
            + "60C980DD98EDD3DFFFFFFFFFFFFFFFFF",
            base=16,
        ),
        "generator": 2,
    },
}







class       __lowerCAmelCase   :



			def __init__(						self       :  List[str]			,		A       :  int = 14)       ->   None:



						"""simple docstring"""






						if group not in primes:
									raise ValueError('Unsupported Group')
						_UpperCAmelCase			=       primes[group]['prime']
						_UpperCAmelCase			=       primes[group]['generator']

						_UpperCAmelCase			=       int(hexlify(urandom(32))			,		base=16)



			def    _lowerCamelCase	(						self       :  int)       ->   str:



						"""simple docstring"""






						return hex(self.__private_key)[2:]



			def    _lowerCamelCase	(						self       :  Union[str, Any])       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       pow(self.generator			,		self.__private_key			,		self.prime)
						return hex(A)[2:]



			def    _lowerCamelCase	(						self       :  Tuple			,		A       :  int)       ->   bool:



						"""simple docstring"""






						return (
						    2 <= key <= self.prime - 2
						    and pow(A			,		(self.prime - 1) // 2			,		self.prime) == 1
						)



			def    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  str)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       int(A			,		base=16)
						if not self.is_valid_public_key(A):
									raise ValueError('Invalid public key')
						_UpperCAmelCase			=       pow(A			,		self.__private_key			,		self.prime)
						return shaaaa(str(A).encode()).hexdigest()



			@staticmethod
			def    _lowerCamelCase	(						A       :  int			,		A       :  int)       ->   bool:



						"""simple docstring"""






						return (
						    2 <= remote_public_key_str <= prime - 2
						    and pow(A			,		(prime - 1) // 2			,		A) == 1
						)



			@staticmethod
			def    _lowerCamelCase	(						A       :  str			,		A       :  str			,		A       :  int = 14)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       int(A			,		base=16)
						_UpperCAmelCase			=       int(A			,		base=16)
						_UpperCAmelCase			=       primes[group]['prime']
						if not DiffieHellman.is_valid_public_key_static(A			,		A):
									raise ValueError('Invalid public key')
						_UpperCAmelCase			=       pow(A			,		A			,		A)
						return shaaaa(str(A).encode()).hexdigest()


if __name__ == "__main__":
							import doctest

							doctest.testmod()
