





'''simple docstring'''

import os
import unicodedata
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import SPIECE_UNDERLINE, logging


__lowerCAmelCase							:     List[str]		     =logging.get_logger(__name__)

__lowerCAmelCase							:     List[str]		     ={"vocab_file": "spiece.model"}

__lowerCAmelCase							:     Optional[int]		     ={
    "vocab_file": {
        "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model",
        "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model",
    }
}

__lowerCAmelCase							:     List[Any]		     ={
    "xlnet-base-cased": None,
    "xlnet-large-cased": None,
}

# Segments (not really needed)
__lowerCAmelCase							:     List[str]		     =0
__lowerCAmelCase							:     int		     =1
__lowerCAmelCase							:     Optional[int]		     =2
__lowerCAmelCase							:     Optional[int]		     =3
__lowerCAmelCase							:     List[Any]		     =4






class        UpperCAmelCase     (	UpperCamelCase__    ):
 __lowercase       		=    VOCAB_FILES_NAMES
 __lowercase       		=    PRETRAINED_VOCAB_FILES_MAP
 __lowercase       		=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __lowercase       		=    '''left'''
 def __init__(    self				:Tuple						,	lowercase_				:Optional[Any]						,	lowercase_				:Optional[int]=False						,	lowercase_				:int=True						,	lowercase_				:List[Any]=False						,	lowercase_				:int="<s>"						,	lowercase_				:Union[str, Any]="</s>"						,	lowercase_				:List[str]="<unk>"						,	lowercase_				:str="<sep>"						,	lowercase_				:Optional[Any]="<pad>"						,	lowercase_				:Tuple="<cls>"						,	lowercase_				:int="<mask>"						,	lowercase_				:Union[str, Any]=["<eop>", "<eod>"]						,	lowercase_				:Optional[Dict[str, Any]] = None						,	**lowercase_				:int						,	)->							Optional[int]:
     A__    =      AddedToken(_snake_case						,	lstrip=_snake_case						,	rstrip=_snake_case							) if isinstance(_snake_case						,	_snake_case							) else mask_token

     A__    =      {} if sp_model_kwargs is None else sp_model_kwargs

     super().__init__(
         do_lower_case=_snake_case						,	remove_space=_snake_case						,	keep_accents=_snake_case						,	bos_token=_snake_case						,	eos_token=_snake_case						,	unk_token=_snake_case						,	sep_token=_snake_case						,	pad_token=_snake_case						,	cls_token=_snake_case						,	mask_token=_snake_case						,	additional_special_tokens=_snake_case						,	sp_model_kwargs=self.sp_model_kwargs						,	**_snake_case						,	)

     A__    =      3

     A__    =      do_lower_case
     A__    =      remove_space
     A__    =      keep_accents
     A__    =      vocab_file

     A__    =      spm.SentencePieceProcessor(**self.sp_model_kwargs							)
     self.sp_model.Load(_snake_case							)
 @property
 def 					UpperCAmelCase_							(    self				:Tuple							)->							Any:
     return len(self.sp_model							)
 def 					UpperCAmelCase_							(    self				:Union[str, Any]							)->							List[Any]:
     A__    =      {self.convert_ids_to_tokens(_snake_case							): i for i in range(self.vocab_size							)}
     vocab.update(self.added_tokens_encoder							)
     return vocab
 def __getstate__(    self				:Any							)->							Union[str, Any]:
     A__    =      self.__dict__.copy()
     A__    =      None
     return state
 def __setstate__(    self				:str						,	lowercase_				:Union[str, Any]							)->							List[str]:
     A__    =      d

     # for backward compatibility
     if not hasattr(self						,	"sp_model_kwargs"							):
         A__    =      {}

     A__    =      spm.SentencePieceProcessor(**self.sp_model_kwargs							)
     self.sp_model.Load(self.vocab_file							)
 def 					UpperCAmelCase_							(    self				:int						,	lowercase_				:Any							)->							Any:


     if self.remove_space:
         A__    =      " ".join(inputs.strip().split()							)
     else:
         A__    =      inputs
     A__    =      outputs.replace("``"						,	"\""							).replace("\'\'"						,	"\""							)

     if not self.keep_accents:
         A__    =      unicodedata.normalize("NFKD"						,	_snake_case							)
         A__    =      "".join([c for c in outputs if not unicodedata.combining(_snake_case							)]							)
     if self.do_lower_case:
         A__    =      outputs.lower()

     return outputs
 def 					UpperCAmelCase_							(    self				:Any						,	lowercase_				:str							)->							Tuple:
     A__    =      self.preprocess_text(_snake_case							)
     A__    =      self.sp_model.encode(_snake_case						,	out_type=_snake_case							)
     A__    =      []
     for piece in pieces:
         if len(_snake_case							) > 1 and piece[-1] == str(","							) and piece[-2].isdigit():
             A__    =      self.sp_model.EncodeAsPieces(piece[:-1].replace(_snake_case						,	""							)							)
             if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
                 if len(cur_pieces[0]							) == 1:
                     A__    =      cur_pieces[1:]
                 else:
                     A__    =      cur_pieces[0][1:]
             cur_pieces.append(piece[-1]							)
             new_pieces.extend(_snake_case							)
         else:
             new_pieces.append(_snake_case							)

     return new_pieces
 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:Dict							)->							Dict:
     return self.sp_model.PieceToId(_snake_case							)
 def 					UpperCAmelCase_							(    self				:str						,	lowercase_				:Union[str, Any]							)->							List[Any]:
     return self.sp_model.IdToPiece(_snake_case							)
 def 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:Optional[Any]							)->							Dict:
     A__    =      "".join(_snake_case							).replace(_snake_case						,	" "							).strip()
     return out_string
 def 					UpperCAmelCase_							(    self				:Any						,	lowercase_				:List[int]						,	lowercase_				:bool = False						,	lowercase_				:bool = None						,	lowercase_				:bool = True						,	**lowercase_				:Dict						,	)->							Optional[Any]:
     A__    =      kwargs.pop("use_source_tokenizer"						,	_snake_case							)

     A__    =      self.convert_ids_to_tokens(_snake_case						,	skip_special_tokens=_snake_case							)

     # To avoid mixing byte-level and unicode for byte-level BPT
     # we need to build string separately for added tokens and byte-level tokens
     # cf. https://github.com/huggingface/transformers/issues/1133
     A__    =      []
     A__    =      []
     for token in filtered_tokens:
         if skip_special_tokens and token in self.all_special_ids:
             continue
         if token in self.added_tokens_encoder:
             if current_sub_text:
                 sub_texts.append(self.convert_tokens_to_string(_snake_case							)							)
                 A__    =      []
             sub_texts.append(_snake_case							)
         else:
             current_sub_text.append(_snake_case							)
     if current_sub_text:
         sub_texts.append(self.convert_tokens_to_string(_snake_case							)							)

     # Mimic the behavior of the Rust tokenizer:
     # By default, there are no spaces between special tokens
     A__    =      "".join(_snake_case							)

     A__    =      (
         clean_up_tokenization_spaces
         if clean_up_tokenization_spaces is not None
         else self.clean_up_tokenization_spaces
     )
     if clean_up_tokenization_spaces:
         A__    =      self.clean_up_tokenization(_snake_case							)
         return clean_text
     else:
         return text
 def 					UpperCAmelCase_							(    self				:Dict						,	lowercase_				:List[int]						,	lowercase_				:Optional[List[int]] = None							)->							Optional[int]:
     A__    =      [self.sep_token_id]
     A__    =      [self.cls_token_id]
     if token_ids_a is None:
         return token_ids_a + sep + cls
     return token_ids_a + sep + token_ids_a + sep + cls
 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:List[int]						,	lowercase_				:Optional[List[int]] = None						,	lowercase_				:bool = False							)->							int:


     if already_has_special_tokens:
         return super().get_special_tokens_mask(
             token_ids_a=_snake_case						,	token_ids_a=_snake_case						,	already_has_special_tokens=_snake_case							)

     if token_ids_a is not None:
         return ([0] * len(_snake_case							)) + [1] + ([0] * len(_snake_case							)) + [1, 1]
     return ([0] * len(_snake_case							)) + [1, 1]
 def 					UpperCAmelCase_							(    self				:Any						,	lowercase_				:List[int]						,	lowercase_				:Optional[List[int]] = None							)->							int:
     A__    =      [self.sep_token_id]
     A__    =      [2]

     if token_ids_a is None:
         return len(token_ids_a + sep							) * [0] + cls_segment_id
     return len(token_ids_a + sep							) * [0] + len(token_ids_a + sep							) * [1] + cls_segment_id



 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:str						,	lowercase_				:Optional[str] = None							)->							Optional[int]:


     if not os.path.isdir(_snake_case							):
         logger.error(F"Vocabulary path ({save_directory}) should be a directory"							)
         return
     A__    =      os.path.join(
         _snake_case						,	(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]							)

     if os.path.abspath(self.vocab_file							) != os.path.abspath(_snake_case							) and os.path.isfile(self.vocab_file							):
         copyfile(self.vocab_file						,	_snake_case							)
     elif not os.path.isfile(self.vocab_file							):
         with open(_snake_case						,	"wb"							) as fi:
             A__    =      self.sp_model.serialized_model_proto()
             fi.write(_snake_case							)

     return (out_vocab_file,)










'''simple docstring'''

import os
import random
import sys

from . import cryptomath_module as cryptoMath  # noqa: N812
from . import rabin_miller as rabinMiller  # noqa: N812





def 						UpperCamelCase    (							):
    print("Making key files..."	)
    make_key_files("rsa"						,	10_24	)
    print("Key files generation successful."	)





def 						UpperCamelCase    (							_lowerCamelCase : int	):
    print("Generating prime p..."	)
    A__    =      rabinMiller.generate_large_prime(_lowerCamelCase	)
    print("Generating prime q..."	)
    A__    =      rabinMiller.generate_large_prime(_lowerCamelCase	)
    A__    =      p * q

    print("Generating e that is relatively prime to (p - 1) * (q - 1)..."	)
    while True:
        A__    =      random.randrange(2 ** (key_size - 1)						,	2 ** (key_size)	)
        if cryptoMath.gcd(_lowerCamelCase						,	(p - 1) * (q - 1)	) == 1:
            break

    print("Calculating d that is mod inverse of e..."	)
    A__    =      cryptoMath.find_mod_inverse(_lowerCamelCase						,	(p - 1) * (q - 1)	)

    A__    =      (n, e)
    A__    =      (n, 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__    =      generate_key(_lowerCamelCase	)
    print(F"\nWriting public key to file {name}_pubkey.txt..."	)
    with open(F"{name}_pubkey.txt"						,	"w"	) as out_file:
        out_file.write(F"{key_size},{public_key[0]},{public_key[1]}"	)

    print(F"Writing private key to file {name}_privkey.txt..."	)
    with open(F"{name}_privkey.txt"						,	"w"	) as out_file:
        out_file.write(F"{key_size},{private_key[0]},{private_key[1]}"	)


if __name__ == "__main__":
  main()



