structioning

This commit is contained in:
Christoph J. Scherr 2023-05-16 15:54:24 +02:00
parent a7d552ecb2
commit 3f50c42a6e
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
6 changed files with 182 additions and 38 deletions

135
src/cplex/cli.rs Normal file
View File

@ -0,0 +1,135 @@
/// This is just structures for parsing Cli args
#[derive(Parser, Debug, Clone)]
#[clap(name="plexcryptool", author="Christoph J. Scherr", version, about="Various tools for use with math and cryptology, includes executable and a library.")]
pub struct Cli {
/// Which submodule to use
#[command(subcommand)]
command: Commands,
/// Machine output
#[arg(short, long, default_value_t = false, global = true)]
machine: bool,
/// Verbose output
#[arg(short, long, default_value_t = false, global = true)]
verbose: bool,
}
#[derive(Subcommand, Debug, Clone)]
pub enum Commands {
/// Use math functions
Math(MathCommand),
/// Use binary functions
Binary(BinaryCommand),
/// Use custom algorithms
Algo(AlgoCommand),
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct MathCommand {
#[command(subcommand)]
action: MathActions
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct BinaryCommand {
#[command(subcommand)]
action: BinaryActions
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct AlgoCommand {
#[command(subcommand)]
action: AlgoActions
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum MathActions {
#[command(name="modexp")]
Modexp(ModexpArgs),
Modred(ModredArgs),
Pm1(PM1Args),
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct ModexpArgs {
base: String,
exp: String,
field: String
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct ModredArgs {
#[clap(value_parser=maybe_hex::<u64>)]
polynomial: u64,
#[clap(value_parser=maybe_hex::<u64>)]
relation: u64,
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct PM1Args {
#[clap(value_parser=maybe_hex::<u128>)]
n: u128,
#[clap(value_parser=maybe_hex::<u128>)]
max_prime: u128,
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum BinaryActions {
/// bit rotation/circular shifting (only 32bit)
#[command(name="rotate")]
Rotate(RotateArgs),
Xor(XorArgs)
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct RotateArgs {
#[arg(short, long, default_value_t = false)]
left: bool,
#[clap(value_parser=maybe_hex::<u32>)]
base: u32,
#[clap(value_parser=maybe_hex::<u32>)]
shift_width: u32,
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct XorArgs {
#[clap(value_parser=maybe_hex::<u128>)]
a: u128,
#[clap(value_parser=maybe_hex::<u128>)]
b: u128,
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
pub enum AlgoActions {
#[command(name="feistel0-i")]
Feistel0Inner(Feistel0InnerArgs),
#[command(name="feistel0-sbox")]
Feistel0SBOX(Feistel0SBOXArgs),
#[command(name="feistel0")]
Feistel0(Feistel0Args)
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct Feistel0InnerArgs {
#[clap(value_parser=maybe_hex::<u16>)]
input: u16,
#[clap(value_parser=maybe_hex::<u16>)]
key: u16,
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct Feistel0SBOXArgs {
#[clap(value_parser=maybe_hex::<u8>)]
index: u8,
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
pub struct Feistel0Args{
#[clap(value_parser=maybe_hex::<u32>)]
input: u32,
#[clap(value_parser=maybe_hex::<u32>)]
key: u32,
#[arg(short, long, default_value_t = false)]
decrypt: bool,
}

View File

@ -1,4 +1,4 @@
/// # common module
/// # cplex module
///
/// This module implements functionality used by multiple other ones.
///

View File

@ -8,12 +8,15 @@
/// License: MIT
/// Source: <https://git.cscherr.de/PlexSheep/plexcryptool/>
use crate::Cli;
use std::fmt::{Debug, LowerHex};
use num::Integer;
/// print a seperator
pub fn seperator() {
println!("{:=<120}", '=');
}
/// process a result with some int
pub fn proc_result<T>(result: Result<T, String>, args: Cli)
where
@ -27,7 +30,7 @@ pub fn proc_result<T>(result: Result<T, String>, args: Cli)
println!("{:#x}", res);
}
else {
println!("=======================================================================");
seperator();
println!("result is {:#x}", res);
}
}
@ -36,7 +39,7 @@ pub fn proc_result<T>(result: Result<T, String>, args: Cli)
println!("{:#?}", e)
}
else {
println!("=======================================================================");
seperator();
println!("could not compute:\n{:#?}", e)
}
}
@ -54,7 +57,7 @@ pub fn proc_num<T>(num: T, args: Cli)
println!("{:#x}", num);
}
else {
println!("=======================================================================");
seperator();
println!("result is {:#x}", num);
}
}
@ -68,7 +71,7 @@ pub fn proc_vec<T>(vec: Vec<T>, args: Cli)
println!("{:#?}", vec);
}
else {
println!("=======================================================================");
seperator();
println!("result is\n{:#?}", vec);
}
}
@ -84,7 +87,7 @@ pub fn proc_result_vec<T>(res: Result<Vec<T>, String>, args: Cli)
println!("{:#?}", vec);
}
else {
println!("=======================================================================");
seperator();
println!("result is {:#?}", vec);
}
}
@ -93,7 +96,7 @@ pub fn proc_result_vec<T>(res: Result<Vec<T>, String>, args: Cli)
println!("{:#?}", e)
}
else {
println!("=======================================================================");
seperator();
println!("could not compute:\n{:#?}", e)
}
}

View File

@ -23,6 +23,7 @@ use pyo3::prelude::*;
mod binary;
mod math;
mod algo;
mod cplex;
#[pymodule]
fn register_binary_module(py: Python, parent_module: &PyModule) -> PyResult<()> {

View File

@ -21,7 +21,7 @@
mod binary;
mod math;
mod algo;
mod common_plex;
mod cplex;
use std::{str::FromStr, fmt::Debug};
@ -29,7 +29,7 @@ use clap::{Args, Parser, Subcommand};
use clap_num::maybe_hex;
use num_bigint;
/*************************************************************************************************/
// This is just structures for parsing Cli args
/// This is just structures for parsing Cli args
#[derive(Parser, Debug, Clone)]
#[clap(name="plexcryptool", author="Christoph J. Scherr", version, about="Various tools for use with math and cryptology, includes executable and a library.")]
pub struct Cli {
@ -47,7 +47,7 @@ pub struct Cli {
}
#[derive(Subcommand, Debug, Clone)]
enum Commands {
pub enum Commands {
/// Use math functions
Math(MathCommand),
/// Use binary functions
@ -57,25 +57,25 @@ enum Commands {
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct MathCommand {
pub struct MathCommand {
#[command(subcommand)]
action: MathActions
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct BinaryCommand {
pub struct BinaryCommand {
#[command(subcommand)]
action: BinaryActions
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct AlgoCommand {
pub struct AlgoCommand {
#[command(subcommand)]
action: AlgoActions
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
enum MathActions {
pub enum MathActions {
#[command(name="modexp")]
Modexp(ModexpArgs),
Modred(ModredArgs),
@ -83,14 +83,14 @@ enum MathActions {
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct ModexpArgs {
pub struct ModexpArgs {
base: String,
exp: String,
field: String
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct ModredArgs {
pub struct ModredArgs {
#[clap(value_parser=maybe_hex::<u64>)]
polynomial: u64,
#[clap(value_parser=maybe_hex::<u64>)]
@ -98,7 +98,7 @@ struct ModredArgs {
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct PM1Args {
pub struct PM1Args {
#[clap(value_parser=maybe_hex::<u128>)]
n: u128,
#[clap(value_parser=maybe_hex::<u128>)]
@ -106,7 +106,7 @@ struct PM1Args {
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
enum BinaryActions {
pub enum BinaryActions {
/// bit rotation/circular shifting (only 32bit)
#[command(name="rotate")]
Rotate(RotateArgs),
@ -114,7 +114,7 @@ enum BinaryActions {
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct RotateArgs {
pub struct RotateArgs {
#[arg(short, long, default_value_t = false)]
left: bool,
#[clap(value_parser=maybe_hex::<u32>)]
@ -124,7 +124,7 @@ struct RotateArgs {
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct XorArgs {
pub struct XorArgs {
#[clap(value_parser=maybe_hex::<u128>)]
a: u128,
#[clap(value_parser=maybe_hex::<u128>)]
@ -132,7 +132,7 @@ struct XorArgs {
}
#[derive(Subcommand, Clone, Debug, PartialEq, Eq)]
enum AlgoActions {
pub enum AlgoActions {
#[command(name="feistel0-i")]
Feistel0Inner(Feistel0InnerArgs),
#[command(name="feistel0-sbox")]
@ -142,7 +142,7 @@ enum AlgoActions {
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct Feistel0InnerArgs {
pub struct Feistel0InnerArgs {
#[clap(value_parser=maybe_hex::<u16>)]
input: u16,
#[clap(value_parser=maybe_hex::<u16>)]
@ -150,13 +150,13 @@ struct Feistel0InnerArgs {
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct Feistel0SBOXArgs {
pub struct Feistel0SBOXArgs {
#[clap(value_parser=maybe_hex::<u8>)]
index: u8,
}
#[derive(Args, Clone, Debug, PartialEq, Eq)]
struct Feistel0Args{
pub struct Feistel0Args{
#[clap(value_parser=maybe_hex::<u32>)]
input: u32,
#[clap(value_parser=maybe_hex::<u32>)]
@ -172,6 +172,9 @@ struct Feistel0Args{
/// internal functions with the corresponding values, then shows the results to the user.
pub fn main() {
let args = Cli::parse();
if args.verbose {
cplex::printing::seperator();
}
match args.clone().command {
Commands::Math(action) => {
match action.action {
@ -180,11 +183,11 @@ pub fn main() {
let e = num_bigint::BigInt::from_str(&mod_exp_args.exp.as_str()).expect("could not make bigint");
let f = num_bigint::BigInt::from_str(&mod_exp_args.field.as_str()).expect("could not make bigint");
let num = math::modexp::modular_exponentiation(b.clone(), e, f, args.verbose);
common_plex::printing::proc_num(num, args);
cplex::printing::proc_num(num, args);
}
MathActions::Modred(mod_red_args) => {
let result = math::modred::modred(mod_red_args.polynomial, mod_red_args.relation, args.verbose);
common_plex::printing::proc_result(result, args);
cplex::printing::proc_result(result, args);
}
MathActions::Pm1(pm1_args) => {
let vec: Result<Vec<u128>, String> = math::pm1::p_minus_one(
@ -192,7 +195,7 @@ pub fn main() {
pm1_args.max_prime,
args.verbose
);
common_plex::printing::proc_result_vec(vec, args);
cplex::printing::proc_result_vec(vec, args);
}
}
}
@ -206,11 +209,11 @@ pub fn main() {
else {
result = binary::rotr32(bin_rot_args.base, bin_rot_args.shift_width);
}
common_plex::printing::proc_num(result, args);
cplex::printing::proc_num(result, args);
},
BinaryActions::Xor(bin_xor_args) => {
let result: u128 = binary::xor(bin_xor_args.a, bin_xor_args.b);
common_plex::printing::proc_num(result, args);
cplex::printing::proc_num(result, args);
}
}
}
@ -218,11 +221,11 @@ pub fn main() {
match action.action {
AlgoActions::Feistel0Inner(alg_fei_args) => {
let result: u16 = algo::feistel0::inner(alg_fei_args.input, alg_fei_args.key, args.verbose);
common_plex::printing::proc_num(result, args);
cplex::printing::proc_num(result, args);
}
AlgoActions::Feistel0SBOX(alg_fsb_args) => {
let result: u8 = algo::feistel0::sbox(alg_fsb_args.index);
common_plex::printing::proc_num(result, args);
cplex::printing::proc_num(result, args);
}
AlgoActions::Feistel0(alg_fe0_args) => {
let keys = algo::feistel0::key_scheduler(alg_fe0_args.key);
@ -233,7 +236,7 @@ pub fn main() {
else {
result = algo::feistel0::encrypt(alg_fe0_args.input, keys, args.verbose);
}
common_plex::printing::proc_num(result, args);
cplex::printing::proc_num(result, args);
}
}
}

View File

@ -10,6 +10,8 @@
/// License: MIT
/// Source: <https://git.cscherr.de/PlexSheep/plexcryptool/>
use crate::cplex;
#[test]
fn test_modred() {
let rel: u64 = 0x1053;
@ -22,9 +24,9 @@ pub fn modred(mut poly: u64, relation: u64, verbose: bool) -> Result<u64, String
let mut diffrence: u32;
let mut index: usize = 0;
if verbose {
println!("relation:\t0x{:x}\t", relation);
println!("polynomial:\t0x{:x}\t", poly);
println!("=======================================================================");
println!("relation:\t{:#x}\t", relation);
println!("polynomial:\t{:#x}\t", poly);
println!("{:=<120}", '=');
}
if relation > poly {
if verbose {
@ -36,7 +38,7 @@ pub fn modred(mut poly: u64, relation: u64, verbose: bool) -> Result<u64, String
diffrence = relation.leading_zeros() - poly.leading_zeros();
poly = poly ^ (relation << diffrence);
if verbose {
println!("{index}:\tpoly: 0x{:x}\t 0b{:b}", poly, poly);
println!("{index}:\tpoly: {:#x}\t {:#064b}", poly, poly);
}
index += 1;
}