


import json
import os
import unittest

from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import (
    VOCAB_FILES_NAMES,
    GPTSanJapaneseTokenizer,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class    UpperCAmelCase					(       __snake_case      , unittest.TestCase    ):
			lowercase						= GPTSanJapaneseTokenizer
			lowercase						= False
			lowercase						= {"""do_clean_text""": False, """add_prefix_space""": False}




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						super().setUp()

						# fmt: off
						UpperCamelCase         =					["""こん""", """こんに""", """にちは""", """ばんは""", """世界,㔺界""", """、""", """。""", """<BR>""", """<SP>""", """<TAB>""", """<URL>""", """<EMAIL>""", """<TEL>""", """<DATE>""", """<PRICE>""", """<BLOCK>""", """<KIGOU>""", """<U2000U2BFF>""", """<|emoji1|>""", """<unk>""", """<|bagoftoken|>""", """<|endoftext|>"""]
						# fmt: on
						UpperCamelCase         =					{"""emoji""": {"""\ud83d\ude00""": """<|emoji1|>"""}, """emoji_inv""": {"""<|emoji1|>""": """\ud83d\ude00"""}}  # 😀
						UpperCamelCase         =					{"""unk_token""": """<unk>"""}

						UpperCamelCase         =					os.path.join(self.tmpdirname       ,       VOCAB_FILES_NAMES["""vocab_file"""]						)
						UpperCamelCase         =					os.path.join(self.tmpdirname       ,       VOCAB_FILES_NAMES["""emoji_file"""]						)
						with open(self.vocab_file       ,       """w"""       ,       encoding="""utf-8"""						) as vocab_writer:
									vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]						)						)
						with open(self.emoji_file       ,       """w"""						) as emoji_writer:
									emoji_writer.write(json.dumps(__magic_name__						)						)




			def   lowerCamelCase_					(						self							:		List[Any]       ,       **__magic_name__							:		Optional[int]						):







						"""simple docstring"""







						kwargs.update(self.special_tokens_map						)
						return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname       ,       **__magic_name__						)




			def   lowerCamelCase_					(						self							:		List[str]       ,       __magic_name__							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					"""こんにちは、世界。 \nこんばんは、㔺界。😀"""
						UpperCamelCase         =					"""こんにちは、世界。 \nこんばんは、世界。😀"""
						return input_text, output_text




			def   lowerCamelCase_					(						self							:		List[str]       ,       __magic_name__							:		Union[str, Any]						):







						"""simple docstring"""







						UpperCamelCase  ,  UpperCamelCase         =					self.get_input_output_texts(__magic_name__						)
						UpperCamelCase         =					tokenizer.encode(__magic_name__       ,       add_special_tokens=__magic_name__						)
						UpperCamelCase         =					tokenizer.decode(__magic_name__       ,       clean_up_tokenization_spaces=__magic_name__						)
						return text, ids




			def   lowerCamelCase_					(						self							:		Any						):







						"""simple docstring"""







						pass  # TODO add if relevant




			def   lowerCamelCase_					(						self							:		Tuple						):







						"""simple docstring"""







						pass  # TODO add if relevant




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						pass  # TODO add if relevant




			def   lowerCamelCase_					(						self							:		Optional[int]						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_tokenizer()

						# Testing tokenization
						UpperCamelCase         =					"""こんにちは、世界。　こんばんは、㔺界。"""
						UpperCamelCase         =					["""こん""", """にちは""", """、""", """世界""", """。""", """<SP>""", """こん""", """ばんは""", """、""", """㔺界""", """。"""]
						UpperCamelCase         =					tokenizer.tokenize(__magic_name__						)
						self.assertListEqual(__magic_name__       ,       __magic_name__						)

						# Testing conversion to ids without special tokens
						UpperCamelCase         =					[0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
						UpperCamelCase         =					tokenizer.convert_tokens_to_ids(__magic_name__						)
						self.assertListEqual(__magic_name__       ,       __magic_name__						)

						# Testing conversion to ids with special tokens
						UpperCamelCase         =					tokens + [tokenizer.unk_token]
						UpperCamelCase         =					[0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 1_9]
						UpperCamelCase         =					tokenizer.convert_tokens_to_ids(__magic_name__						)
						self.assertListEqual(__magic_name__       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		List[Any]						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_tokenizer()

						# Testing tokenization
						UpperCamelCase         =					"""こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。"""
						UpperCamelCase         =					"""こんにちは、、、、世界。こんばんは、、、、世界。"""
						UpperCamelCase         =					tokenizer.encode(__magic_name__						)
						UpperCamelCase         =					tokenizer.decode(__magic_name__						)
						self.assertEqual(__magic_name__       ,       __magic_name__						)




			@slow
			def   lowerCamelCase_					(						self							:		str						):







						"""simple docstring"""







						UpperCamelCase         =					self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""						)

						# Testing tokenization
						UpperCamelCase         =					"""こんにちは、世界。"""
						UpperCamelCase         =					"""こんばんは、㔺界。😀"""
						UpperCamelCase         =					"""こんにちは、世界。こんばんは、世界。😀"""
						UpperCamelCase         =					tokenizer.encode(prefix_text + input_text						)
						UpperCamelCase         =					tokenizer.encode(""""""       ,       prefix_text=prefix_text + input_text						)
						UpperCamelCase         =					tokenizer.encode(__magic_name__       ,       prefix_text=__magic_name__						)
						UpperCamelCase         =					tokenizer.decode(__magic_name__						)
						UpperCamelCase         =					tokenizer.decode(__magic_name__						)
						UpperCamelCase         =					tokenizer.decode(__magic_name__						)
						self.assertEqual(__magic_name__       ,       __magic_name__						)
						self.assertEqual(__magic_name__       ,       __magic_name__						)
						self.assertEqual(__magic_name__       ,       __magic_name__						)




			@slow
			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""						)

						# Testing tokenization
						UpperCamelCase         =					"""こんにちは、世界。"""
						UpperCamelCase         =					"""こんばんは、㔺界。😀"""

						UpperCamelCase         =					len(tokenizer.encode(__magic_name__						)						) - 2
						UpperCamelCase         =					len(tokenizer.encode(__magic_name__						)						) - 2

						UpperCamelCase         =					[1] + [0] * (len_prefix + len_text + 1)
						UpperCamelCase         =					[1] * (len_prefix + len_text + 1) + [0]
						UpperCamelCase         =					[1] + [1] * (len_prefix) + [0] * (len_text + 1)

						UpperCamelCase         =					tokenizer(prefix_text + input_text						).token_type_ids
						UpperCamelCase         =					tokenizer(""""""       ,       prefix_text=prefix_text + input_text						).token_type_ids
						UpperCamelCase         =					tokenizer(__magic_name__       ,       prefix_text=__magic_name__						).token_type_ids
						self.assertListEqual(__magic_name__       ,       __magic_name__						)
						self.assertListEqual(__magic_name__       ,       __magic_name__						)
						self.assertListEqual(__magic_name__       ,       __magic_name__						)




			@slow
			def   lowerCamelCase_					(						self							:		Tuple						):







						"""simple docstring"""







						UpperCamelCase         =					self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""						)

						UpperCamelCase         =					tokenizer.encode("""あンいワ"""						)
						UpperCamelCase         =					tokenizer.encode(""""""       ,       prefix_text="""あンいワ"""						)
						UpperCamelCase         =					tokenizer.encode("""いワ"""       ,       prefix_text="""あン"""						)

						self.assertEqual(tokenizer.decode(__magic_name__						)       ,       tokenizer.decode(__magic_name__						)						)
						self.assertEqual(tokenizer.decode(__magic_name__						)       ,       tokenizer.decode(__magic_name__						)						)
						self.assertNotEqual(__magic_name__       ,       __magic_name__						)
						self.assertNotEqual(__magic_name__       ,       __magic_name__						)
						self.assertEqual(x_token_a[1]       ,       x_token_a[-1]						)  # SEG token
						self.assertEqual(x_token_a[1]       ,       x_token_a[3]						)  # SEG token




			@slow
			def   lowerCamelCase_					(						self							:		List[str]						):







						"""simple docstring"""







						UpperCamelCase         =					self.tokenizer_class.from_pretrained("""Tanrei/GPTSAN-japanese"""						)

						UpperCamelCase         =					[["""武田信玄""", """は、"""], ["""織田信長""", """の配下の、"""]]
						UpperCamelCase         =					tokenizer(__magic_name__       ,       padding=__magic_name__						)
						UpperCamelCase         =					tokenizer.batch_encode_plus(__magic_name__       ,       padding=__magic_name__						)

						# fmt: off
						UpperCamelCase         =					[[3_5_9_9_3, 8_6_4_0, 2_5_9_4_8, 3_5_9_9_8, 3_0_6_4_7, 3_5_6_7_5, 3_5_9_9_9, 3_5_9_9_9], [3_5_9_9_3, 1_0_3_8_2, 9_8_6_8, 3_5_9_9_8, 3_0_6_4_6, 9_4_5_9, 3_0_6_4_6, 3_5_6_7_5]]
						UpperCamelCase         =					[[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]]
						UpperCamelCase         =					[[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]]
						# fmt: on
						self.assertListEqual(x_token.input_ids       ,       __magic_name__						)
						self.assertListEqual(x_token.token_type_ids       ,       __magic_name__						)
						self.assertListEqual(x_token.attention_mask       ,       __magic_name__						)
						self.assertListEqual(x_token_a.input_ids       ,       __magic_name__						)
						self.assertListEqual(x_token_a.token_type_ids       ,       __magic_name__						)
						self.assertListEqual(x_token_a.attention_mask       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		List[Any]						):







						"""simple docstring"""







						pass





			def   lowerCamelCase_					(						self							:		Optional[int]						):







						"""simple docstring"""







						pass








import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

__snake_case            =   3






def 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		int	):





			"""simple docstring"""




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






def 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		int	):





			"""simple docstring"""




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

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

			return public_key, private_key






def 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		str       ,  SCREAMING_SNAKE_CASE_   :		int	):





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

			UpperCamelCase  ,  UpperCamelCase         =					generate_key(SCREAMING_SNAKE_CASE_	)
			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 							_lowercase    (		):





			"""simple docstring"""




			print("""Making key files..."""	)
			make_key_files("""elgamal"""       ,  2_048	)
			print("""Key files generation successful"""	)


if __name__ == "__main__":
			main()




