From 9d37023df4722f8889ca72b87b748edf482cc5ac Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Thu, 18 May 2023 01:51:20 +0200 Subject: [PATCH] scripts module and type hinting --- src-py/plexcryptool/__init__.py | 1 + src-py/plexcryptool/__init__.pyi | 10 ++++ src-py/plexcryptool/algo/__init__.pyi | 4 ++ src-py/plexcryptool/algo/feistel0.pyi | 59 +++++++++++++++++++ .../{binary.pyi => binary/__init__.pyi} | 0 src-py/plexcryptool/cplex/__init__.pyi | 4 ++ src-py/plexcryptool/cplex/printing.pyi | 28 +++++++++ src-py/plexcryptool/math.pyi | 10 ---- src-py/plexcryptool/math/__init__.pyi | 9 +++ src-py/plexcryptool/math/modexp.pyi | 27 +++++++++ src-py/plexcryptool/math/modred.pyi | 23 ++++++++ src-py/plexcryptool/math/pm1.pyi | 29 +++++++++ src-py/plexcryptool/plexcryptool.pyi | 1 + src-py/plexcryptool/scripts/__init__.py | 9 +++ src-py/plexcryptool/{ => scripts}/authur1.py | 0 .../{ => scripts}/basic-decrypt.py | 0 .../{ => scripts}/md5-analyzer.py | 0 .../plexcryptool/{ => scripts}/trash-hash.py | 0 src/cplex/printing.rs | 2 +- src/math/modred.rs | 20 +++++++ 20 files changed, 225 insertions(+), 11 deletions(-) create mode 100644 src-py/plexcryptool/__init__.pyi create mode 100644 src-py/plexcryptool/algo/__init__.pyi create mode 100644 src-py/plexcryptool/algo/feistel0.pyi rename src-py/plexcryptool/{binary.pyi => binary/__init__.pyi} (100%) create mode 100644 src-py/plexcryptool/cplex/__init__.pyi create mode 100644 src-py/plexcryptool/cplex/printing.pyi delete mode 100644 src-py/plexcryptool/math.pyi create mode 100644 src-py/plexcryptool/math/__init__.pyi create mode 100644 src-py/plexcryptool/math/modexp.pyi create mode 100644 src-py/plexcryptool/math/modred.pyi create mode 100644 src-py/plexcryptool/math/pm1.pyi create mode 100644 src-py/plexcryptool/scripts/__init__.py rename src-py/plexcryptool/{ => scripts}/authur1.py (100%) rename src-py/plexcryptool/{ => scripts}/basic-decrypt.py (100%) rename src-py/plexcryptool/{ => scripts}/md5-analyzer.py (100%) rename src-py/plexcryptool/{ => scripts}/trash-hash.py (100%) diff --git a/src-py/plexcryptool/__init__.py b/src-py/plexcryptool/__init__.py index 6433255..4961b1d 100644 --- a/src-py/plexcryptool/__init__.py +++ b/src-py/plexcryptool/__init__.py @@ -1 +1,2 @@ from .plexcryptool import * +from . import scripts as scripts diff --git a/src-py/plexcryptool/__init__.pyi b/src-py/plexcryptool/__init__.pyi new file mode 100644 index 0000000..c25d18f --- /dev/null +++ b/src-py/plexcryptool/__init__.pyi @@ -0,0 +1,10 @@ +""" +Bindings for the plexcryptool rust library + +plexcryptool.plexcryptool is direct access to the shared library, do not use it. +""" +from . import binary +from . import math +from . import algo +from . import cplex +from . import scripts diff --git a/src-py/plexcryptool/algo/__init__.pyi b/src-py/plexcryptool/algo/__init__.pyi new file mode 100644 index 0000000..1c9a422 --- /dev/null +++ b/src-py/plexcryptool/algo/__init__.pyi @@ -0,0 +1,4 @@ +""" +various algorithms implemented +""" +from . import feistel0 as feistel0 diff --git a/src-py/plexcryptool/algo/feistel0.pyi b/src-py/plexcryptool/algo/feistel0.pyi new file mode 100644 index 0000000..8b65a46 --- /dev/null +++ b/src-py/plexcryptool/algo/feistel0.pyi @@ -0,0 +1,59 @@ +""" +# basic implementation of a feistel network + +This module implements a feistel network according to an exercise at DHBW Mannheim. +For demonstration purposes only, do not use this in a secure environment. + +___ +@Author: Christoph J. Scherr +@License: MIT +@Source: +""" +def inner(input: int, key: int, verbose: bool) -> int: + """ + the inner function of the feistel network + + takes input, scrambles it by using two s and p boxes, + then xors with the key. + + :param input unsigned 16 bit int + :param key unsigned 16 bit int + :param verbose print steps + """ + ... + +def encrypt(plaintext: int, keys: list[int], verbose: bool) -> int: + """ + encrypt using the feistel0 network + + performes some rounds of the feistelnetwork to encrypt the input. + This will only encrypt a single block. + + DO NOT USE THIS FOR ACTUAL ENCRYPTION! + + :param plaintext unsigned 32 bit int + :param keys vec of the round keys, usually 3 diffrent ones. + :param verbose print steps + """ + ... + +def decrypt(ciphertext: int, keys: list[int], verbose: bool) -> int: + """ + decrypt using the feistel0 network + + performs encryption backwards more or less + + DO NOT USE THIS FOR ACTUAL ENCRYPTION! + + :param ciphertext unsigned 32 bit int + :param keys vec of the round keys, usually 3 diffrent ones. + :param verbose print steps + """ + ... + +def sbox(index: int) -> int: + """ + returns the value of the sbox at index. + + :param index range 0 - 15 + """ diff --git a/src-py/plexcryptool/binary.pyi b/src-py/plexcryptool/binary/__init__.pyi similarity index 100% rename from src-py/plexcryptool/binary.pyi rename to src-py/plexcryptool/binary/__init__.pyi diff --git a/src-py/plexcryptool/cplex/__init__.pyi b/src-py/plexcryptool/cplex/__init__.pyi new file mode 100644 index 0000000..4c75028 --- /dev/null +++ b/src-py/plexcryptool/cplex/__init__.pyi @@ -0,0 +1,4 @@ +""" +various common functionalities used by many modules +""" +from . import printing as printing diff --git a/src-py/plexcryptool/cplex/printing.pyi b/src-py/plexcryptool/cplex/printing.pyi new file mode 100644 index 0000000..3e4b4e1 --- /dev/null +++ b/src-py/plexcryptool/cplex/printing.pyi @@ -0,0 +1,28 @@ +""" +common functionality for printing + +Implements code that might be used by many other modules + +___ +@Author: Christoph J. Scherr +@License: MIT +@Source: +""" + +def version(): + """ + prints the plexcryptool version + """ + ... + +def about(): + """ + prints the about section + """ + ... + +def seperator(): + """ + prints a separator line + """ + ... diff --git a/src-py/plexcryptool/math.pyi b/src-py/plexcryptool/math.pyi deleted file mode 100644 index 3f98755..0000000 --- a/src-py/plexcryptool/math.pyi +++ /dev/null @@ -1,10 +0,0 @@ -""" -Some basic math functionalities -""" -def modular_exponentiation(base: int, exp: int, field: int) -> int: - """ - calculates base^exp in the gallois given gallois field - - Uses iterative squaring to be able to calculate large exponents aswell. - """ - ... diff --git a/src-py/plexcryptool/math/__init__.pyi b/src-py/plexcryptool/math/__init__.pyi new file mode 100644 index 0000000..8d73d99 --- /dev/null +++ b/src-py/plexcryptool/math/__init__.pyi @@ -0,0 +1,9 @@ +""" +# math module + +Funcionality for math things. Contains tedious algorithms like binary exponentiation. +""" +from . import modexp as modexp +from . import modred as modred +from . import pm1 as pm1 + diff --git a/src-py/plexcryptool/math/modexp.pyi b/src-py/plexcryptool/math/modexp.pyi new file mode 100644 index 0000000..6692cf4 --- /dev/null +++ b/src-py/plexcryptool/math/modexp.pyi @@ -0,0 +1,27 @@ +""" +modular exponentiaton + +Implements fast exponentiation with applied modulo. Usefull for calculations in a gallois +field. + +___ +@Author: Christoph J. Scherr +@License: MIT +@Source: +""" + +def modular_exponentiation( + base: int, + orig_exp: int, + field: int, + verbose: bool + ) -> int: + """ + perform modular exponentiation + + :param base the base of the exponentiation + :param orig_exp the exponent of the base + :param field the number that describes the gallois field (should be prime) + :param verbose print steps + """ + ... diff --git a/src-py/plexcryptool/math/modred.pyi b/src-py/plexcryptool/math/modred.pyi new file mode 100644 index 0000000..5ce71c0 --- /dev/null +++ b/src-py/plexcryptool/math/modred.pyi @@ -0,0 +1,23 @@ +""" +modular reduction + +Implements automatic modular reduction in a field specified by a given relation. + +Basically, any binary number can be written as a polynomial. This polynomial can be reduced by +the relation that defines a field. In that field. This is what we call modular reduction. + +___ +@Author: Christoph J. Scherr +@License: MIT +@Source: +""" + +def modred(poly: int, relation: int, verbose: bool) -> int: + """ + perform modular reduction + + :param poly the polynomial as int + :param relation the relation as int + :param verbose print steps + """ + ... diff --git a/src-py/plexcryptool/math/pm1.pyi b/src-py/plexcryptool/math/pm1.pyi new file mode 100644 index 0000000..1a37529 --- /dev/null +++ b/src-py/plexcryptool/math/pm1.pyi @@ -0,0 +1,29 @@ +""" +P minus 1 method + +Determine the prime factors of a number with the p minus 1 method. +Effecient for numbers with low ranged prime factors. + +___ +@Author: Christoph J. Scherr +@License: MIT +@Source: +""" + +def p_minus_one(n: int, max_prime: int, verbose: bool) -> list[int]: + """ + p minus 1 prime number test + + :param n u128 number to check + :param max_prime the highest prime power to use + :param verbose print steps + """ + ... + +def alt_gcd(a: int, b: int) -> int: + """ + find greatest common divisor + + this is a primitive extended euclidic algorithm. + """ + ... diff --git a/src-py/plexcryptool/plexcryptool.pyi b/src-py/plexcryptool/plexcryptool.pyi index 8174ceb..d1dc75b 100644 --- a/src-py/plexcryptool/plexcryptool.pyi +++ b/src-py/plexcryptool/plexcryptool.pyi @@ -2,4 +2,5 @@ Bindings for the plexcryptool rust library plexcryptool.plexcryptool is direct access to the shared library, do not use it. +(you should not see this specific version of this text) """ diff --git a/src-py/plexcryptool/scripts/__init__.py b/src-py/plexcryptool/scripts/__init__.py new file mode 100644 index 0000000..6ee6f1f --- /dev/null +++ b/src-py/plexcryptool/scripts/__init__.py @@ -0,0 +1,9 @@ +""" +various scripts + +these are coded in python and can currently not be called from the executable. +""" +from . import authur1 as authur1 +from . import basic_decrypt as basic_decrypt +from . import md5_analyzer as md5_analyzer +from . import trash_hash as trash_hash diff --git a/src-py/plexcryptool/authur1.py b/src-py/plexcryptool/scripts/authur1.py similarity index 100% rename from src-py/plexcryptool/authur1.py rename to src-py/plexcryptool/scripts/authur1.py diff --git a/src-py/plexcryptool/basic-decrypt.py b/src-py/plexcryptool/scripts/basic-decrypt.py similarity index 100% rename from src-py/plexcryptool/basic-decrypt.py rename to src-py/plexcryptool/scripts/basic-decrypt.py diff --git a/src-py/plexcryptool/md5-analyzer.py b/src-py/plexcryptool/scripts/md5-analyzer.py similarity index 100% rename from src-py/plexcryptool/md5-analyzer.py rename to src-py/plexcryptool/scripts/md5-analyzer.py diff --git a/src-py/plexcryptool/trash-hash.py b/src-py/plexcryptool/scripts/trash-hash.py similarity index 100% rename from src-py/plexcryptool/trash-hash.py rename to src-py/plexcryptool/scripts/trash-hash.py diff --git a/src/cplex/printing.rs b/src/cplex/printing.rs index 9035a00..2d2442a 100644 --- a/src/cplex/printing.rs +++ b/src/cplex/printing.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -/// common functionality +/// common functionality for printing /// /// Implements code that might be used by many other modules /// diff --git a/src/math/modred.rs b/src/math/modred.rs index fcd1cf5..ad22d0a 100644 --- a/src/math/modred.rs +++ b/src/math/modred.rs @@ -12,6 +12,8 @@ use crate::cplex::printing::seperator; +use pyo3::{prelude::*, exceptions::PyException}; + #[test] fn test_modred() { let rel: u64 = 0x1053; @@ -19,6 +21,9 @@ fn test_modred() { assert_eq!(modred(pol0, rel, false).unwrap(), 0x21e); } +/// modular reduction of a polynomial with a given relation +/// +/// (the function uses the integer representations) pub fn modred(mut poly: u64, relation: u64, verbose: bool) -> Result { let mut diffrence: u32; @@ -44,3 +49,18 @@ pub fn modred(mut poly: u64, relation: u64, verbose: bool) -> Result PyResult { + let res = modred(poly, relation, verbose); + match res { + Ok(n) => { + return Ok(n); + } + Err(e) => { + return Err(PyException::new_err(e)); + } + } +}