

"""simple docstring"""


snake_case_    					=	256
# Modulus to hash a string
snake_case_    					=	100_0003

def 			_lowerCAmelCase				(       lowercase_		,     lowercase_						):
						UpperCAmelCase					=			len(lowerCamelCase_						)
						UpperCAmelCase					=			len(lowerCamelCase_						)
						if p_len > t_len:
												return False

						UpperCAmelCase					=			0
						UpperCAmelCase					=			0
						UpperCAmelCase					=			1

						# Calculating the hash of pattern and substring of text
						for i in range(lowerCamelCase_						):
												UpperCAmelCase					=			(ord(pattern[i]						) + p_hash * alphabet_size) % modulus
												UpperCAmelCase					=			(ord(text[i]						) + text_hash * alphabet_size) % modulus
												if i == p_len - 1:
																		continue
												UpperCAmelCase					=			(modulus_power * alphabet_size) % modulus

						for i in range(0		,     t_len - p_len + 1						):
												if text_hash == p_hash and text[i : i + p_len] == pattern:
																		return True
												if i == t_len - p_len:
																		continue
												# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
												UpperCAmelCase					=			(
												    (text_hash - ord(text[i]						) * modulus_power) * alphabet_size
												    + ord(text[i + p_len]						)
												) % modulus
						return False

def 			_lowerCAmelCase				(       ):
						UpperCAmelCase					=			"""abc1abc12"""
						UpperCAmelCase					=			"""alskfjaldsabc1abc1abc12k23adsfabcabc"""
						UpperCAmelCase					=			"""alskfjaldsk23adsfabcabc"""
						assert rabin_karp(lowerCamelCase_		,     lowerCamelCase_						) and not rabin_karp(lowerCamelCase_		,     lowerCamelCase_						)

						# Test 2)
						UpperCAmelCase					=			"""ABABX"""
						UpperCAmelCase					=			"""ABABZABABYABABX"""
						assert rabin_karp(lowerCamelCase_		,     lowerCamelCase_						)

						# Test 3)
						UpperCAmelCase					=			"""AAAB"""
						UpperCAmelCase					=			"""ABAAAAAB"""
						assert rabin_karp(lowerCamelCase_		,     lowerCamelCase_						)

						# Test 4)
						UpperCAmelCase					=			"""abcdabcy"""
						UpperCAmelCase					=			"""abcxabcdabxabcdabcdabcy"""
						assert rabin_karp(lowerCamelCase_		,     lowerCamelCase_						)

						# Test 5)
						UpperCAmelCase					=			"""Lü"""
						UpperCAmelCase					=			"""Lüsai"""
						assert rabin_karp(lowerCamelCase_		,     lowerCamelCase_						)
						UpperCAmelCase					=			"""Lue"""
						assert not rabin_karp(lowerCamelCase_		,     lowerCamelCase_						)
						print('Success.'						)


if __name__ == "__main__":
				test_rabin_karp()










"""simple docstring"""


import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

snake_case_    					=	3

def 			_lowerCAmelCase				(       lowercase_						):
						print('Generating primitive root of p'						)
						while True:
												UpperCAmelCase					=			random.randrange(3		,     lowercase_						)
												if pow(lowercase_		,     2		,     lowercase_						) == 1:
																		continue
												if pow(lowercase_		,     lowercase_		,     lowercase_						) == 1:
																		continue
												return g

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

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

						return public_key, private_key

def 			_lowerCAmelCase				(       lowercase_		,     lowercase_						):
						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(lowercase_						)
						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 			_lowerCAmelCase				(       ):
						print('Making key files...'						)
						make_key_files('elgamal'		,     2048						)
						print('Key files generation successful'						)


if __name__ == "__main__":
				main()





