def run_e7v1(x: int) -> int: i = n = 0 while (i < x): if is_prime(n): i += 1 if x == i: return n n += 1
def ecc_generate(): if request.method == 'GET': return render_template('ecc/generate/index.html') coef = int(request.form['x-coef']) const = int(request.form['const']) modulo = int(request.form['modulo']) if not is_prime(modulo): return "Error: Modulo must be prime" curve = EllipticCurve(coef, const, modulo) base_x = int(request.form['bpoint-x']) base_y = int(request.form['bpoint-y']) bpoint = Point(base_x, base_y, modulo) if not curve.contains(bpoint): return "Error: The base point is not a point in the curve" private_key = randrange(1, MAX_KEY) public_key = curve.multiply(private_key, bpoint) filename = request.form['filename'] public_file = FileStorage(stream=BytesIO(public_key.print().encode()), filename=filename + '.pub') public_file.save('static/' + public_file.filename) private_file = FileStorage(stream=BytesIO(str(private_key).encode()), filename=filename + '.pri') private_file.save('static/' + private_file.filename) return render_template('ecc/generate/result.html')
def ecc_encrypt(): if request.method == 'GET': return render_template('ecc/encrypt/index.html') message = request.files['message'].read() filename = 'encrypted_' + request.files['message'].filename kob = int(request.form['koblitz']) coef = int(request.form['x-coef']) const = int(request.form['const']) modulo = int(request.form['modulo']) if not is_prime(modulo): return "Error: Modulo must be prime" curve = EllipticCurve(coef, const, modulo) base_x = int(request.form['bpoint-x']) base_y = int(request.form['bpoint-y']) bpoint = Point(base_x, base_y, modulo) if not curve.contains(bpoint): return "Error: The base point is not a point in the curve" pubkey = request.files.get('pubkey-file', None) if pubkey == None: pubkey_x = int(request.form['pubkey-x']) pubkey_y = int(request.form['pubkey-y']) pubkey = Point(pubkey_x, pubkey_y, modulo) else: pubkey = pubkey.read().decode() pubkey = Point(0, 0, modulo, pubkey) if not curve.contains(pubkey): return "Error: The public key is not a point in the curve" plaintext = encode(curve, message, kob) start = default_timer() k = randrange(1, modulo) ciphertext = [ curve.add(point, curve.multiply(k, pubkey)) for point in plaintext ] ciphertext.append(curve.multiply(k, bpoint)) end = default_timer() string = ' '.join([point.print() for point in ciphertext]) file = FileStorage(stream=BytesIO(string.encode()), filename=filename) file.seek(0, SEEK_END) size = file.tell() file.seek(0) file.save('static/' + filename) return render_template('ecc/encrypt/result.html', plaintext=message, ciphertext=string, time=end - start, filename=filename, size=size)
def test_turing_machine(suite): """ Checks that the given Turing Machine accepts the given word :param suite: Dictionary with test suite Dict['path'] - path to the file with the Turing Machine Dict['word'] - accepted by the Turing Machine :return: None """ turing_machine = TuringMachine.from_txt(suite['path']) word = suite['word'] assert turing_machine.accepts(word) == is_prime(len(word) - 2)
def test_csg_manual(suite): """ Checks that the given Unrestricted Gramamr generates the given word :param suite: Dictionary with test suite Dict['path'] - path to the file with the grammar Dict['word'] - word generated by the grammar :return: None """ grammar = UnrestrictedGrammar.from_txt(suite['path']) word = suite['word'] assert (grammar.accepts(word) != tuple()) == is_prime(len(word))
def ecc_decrypt(): if request.method == 'GET': return render_template('ecc/decrypt/index.html') message = request.files['message'].read() kob = int(request.form['koblitz']) coef = int(request.form['x-coef']) const = int(request.form['const']) modulo = int(request.form['modulo']) if not is_prime(modulo): return "Error: Modulo must be prime" curve = EllipticCurve(coef, const, modulo) prikey = request.files.get('prikey-file', None) if prikey == None: prikey = int(request.form['prikey']) else: prikey = int(prikey.read()) raw_ciphertext = message.decode().split(' ') while raw_ciphertext[-1] == '': del raw_ciphertext[-1] ciphertext = [Point(0, 0, modulo, point) for point in raw_ciphertext] start = default_timer() point_neg = curve.multiply(prikey, ciphertext[-1]) del ciphertext[-1] point_neg.negate() plaintext = [curve.add(point, point_neg) for point in ciphertext] end = default_timer() string = decode(plaintext, kob) filename = request.form['filename'] file = FileStorage(stream=BytesIO(string), filename=filename) file.seek(0, SEEK_END) size = file.tell() file.seek(0) file.save('static/' + filename) return render_template('ecc/decrypt/result.html', plaintext=string, ciphertext=message.decode(), time=end - start, filename=filename, size=size)
import pytest from src.utils import is_prime params = [{ 'path': 'resources/primality_check_csg.txt', 'word': 'a' * p } for p in range(24) if is_prime(p)] @pytest.fixture(scope='session', params=params) def suite(request): return request.param
def test_is_prime_false(): assert is_prime(35) is False
def test_is_prime_true(): assert is_prime(37) is True
def run_e10v1(x: int) -> int: rec = 0 for n in range(x): if is_prime(n): rec += n return rec
import pytest from src.utils import is_prime params = [{ 'path': 'resources/primality_check_lba.txt', 'word': list(f'${"a" * n}#') } for n in range(24) if is_prime(n)] @pytest.fixture(scope='session', params=params) def suite(request): return request.param
def run_e3v1(x: int) -> int: lim = int(sqrt(x)) for n in range(lim, 1, -1): if x % n == 0: if is_prime(n): return n
import pytest from src.utils import is_prime params = [{ 'path': 'resources/primality_check_ug.txt', 'word': 'a' * p } for p in range(16) if is_prime(p)] @pytest.fixture(scope='session', params=params) def suite(request): return request.param