
import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

lowercase__          =3




def        __UpperCamelCase					(    lowerCAmelCase__							:    int  ):
 print('''Generating primitive root of p'''  )
 while True:
  __a			:  str		    =  random.randrange(3  ,       lowerCAmelCase__  )
  if pow(lowerCAmelCase__  ,       2  ,       lowerCAmelCase__  ) == 1:
   continue
  if pow(lowerCAmelCase__  ,       lowerCAmelCase__  ,       lowerCAmelCase__  ) == 1:
   continue
  return g




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

 __a			:  Tuple		    =  (key_size, e_a, e_a, p)
 __a			:  Tuple		    =  (key_size, d)

 return public_key, private_key




def        __UpperCamelCase					(    lowerCAmelCase__							:    str  ,       lowerCAmelCase__							:    int  ):
 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			:  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        __UpperCamelCase					(    ):
 print('''Making key files...'''  )
 make_key_files('''elgamal'''  ,       2_0_4_8  )
 print('''Key files generation successful'''  )


if __name__ == "__main__":
      main()


import os
import unittest

from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer

from ...test_tokenization_common import TokenizerTesterMixin



class 		UpperCamelCase__  (	__lowercase   ,unittest.TestCase   ):
   _SCREAMING_SNAKE_CASE				:							Union[str, Any]     	=				PhobertTokenizer
   _SCREAMING_SNAKE_CASE				:							int     	=				False
   def 				lowerCAmelCase      (self			: Optional[int]       ):
    super().setUp()

    # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
    __a			:  Optional[int]		    =  ['''T@@''', '''i''', '''I''', '''R@@''', '''r''', '''e@@''']
    __a			:  Tuple		    =  dict(zip(snake_case_ ,    range(len(snake_case_       )       )       )       )
    __a			:  int		    =  ['''#version: 0.2''', '''l à</w>''']
    __a			:  List[Any]		    =  {'''unk_token''': '''<unk>'''}

    __a			:  Any		    =  os.path.join(self.tmpdirname ,    VOCAB_FILES_NAMES['''vocab_file''']       )
    __a			:  List[Any]		    =  os.path.join(self.tmpdirname ,    VOCAB_FILES_NAMES['''merges_file''']       )

    with open(self.vocab_file ,    '''w''' ,    encoding='''utf-8'''       ) as fp:
     for token in vocab_tokens:
      fp.write(f"{token} {vocab_tokens[token]}\n"       )
    with open(self.merges_file ,    '''w''' ,    encoding='''utf-8'''       ) as fp:
     fp.write('''\n'''.join(snake_case_       )       )
   def 				lowerCAmelCase      (self			: Union[str, Any] ,    **snake_case_			: List[str]       ):
    kwargs.update(self.special_tokens_map       )
    return PhobertTokenizer.from_pretrained(self.tmpdirname ,    **snake_case_       )
   def 				lowerCAmelCase      (self			: Any ,    snake_case_			: Dict       ):
    __a			:  Union[str, Any]		    =  '''Tôi là VinAI Research'''
    __a			:  int		    =  '''T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>'''
    return input_text, output_text







   def 				lowerCAmelCase      (self			: Optional[Any]       ):
    __a			:  Optional[int]		    =  PhobertTokenizer(self.vocab_file ,    self.merges_file ,    **self.special_tokens_map       )
    __a			:  Any		    =  '''Tôi là VinAI Research'''
    __a			:  Union[str, Any]		    =  '''T@@ ô@@ i l@@ à V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h'''.split()
    __a			:  List[str]		    =  tokenizer.tokenize(snake_case_       )
    print(snake_case_       )
    self.assertListEqual(snake_case_ ,    snake_case_       )

    __a			:  str		    =  tokens + [tokenizer.unk_token]

    __a			:  str		    =  [4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3]
    self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case_       ) ,    snake_case_       )
