From 8b6874e1663ef56615e19d3d4ba5cb95e4105900 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 17 May 2023 11:13:11 +0200 Subject: [PATCH] cplex module integration --- src/cplex/cli.rs | 59 ++++++++++------- src/cplex/mod.rs | 1 + src/cplex/printing.rs | 2 + src/main.rs | 145 ++---------------------------------------- src/math/modred.rs | 4 +- 5 files changed, 44 insertions(+), 167 deletions(-) diff --git a/src/cplex/cli.rs b/src/cplex/cli.rs index dd36ed5..88c640e 100644 --- a/src/cplex/cli.rs +++ b/src/cplex/cli.rs @@ -1,18 +1,29 @@ +/// command line options +/// +/// this module contains structs and enums that are used to parse command line arguments. +/// +/// Author: Christoph J. Scherr +/// License: MIT +/// Source: +/// +use clap::{Args, Parser, Subcommand}; +use clap_num::maybe_hex; + /// 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, + pub command: Commands, /// Machine output #[arg(short, long, default_value_t = false, global = true)] - machine: bool, + pub machine: bool, /// Verbose output #[arg(short, long, default_value_t = false, global = true)] - verbose: bool, + pub verbose: bool, } #[derive(Subcommand, Debug, Clone)] @@ -28,19 +39,19 @@ pub enum Commands { #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct MathCommand { #[command(subcommand)] - action: MathActions + pub action: MathActions } #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct BinaryCommand { #[command(subcommand)] - action: BinaryActions + pub action: BinaryActions } #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct AlgoCommand { #[command(subcommand)] - action: AlgoActions + pub action: AlgoActions } #[derive(Subcommand, Clone, Debug, PartialEq, Eq)] @@ -53,25 +64,25 @@ pub enum MathActions { #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct ModexpArgs { - base: String, - exp: String, - field: String + pub base: String, + pub exp: String, + pub field: String } #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct ModredArgs { #[clap(value_parser=maybe_hex::)] - polynomial: u64, + pub polynomial: u64, #[clap(value_parser=maybe_hex::)] - relation: u64, + pub relation: u64, } #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct PM1Args { #[clap(value_parser=maybe_hex::)] - n: u128, + pub n: u128, #[clap(value_parser=maybe_hex::)] - max_prime: u128, + pub max_prime: u128, } #[derive(Subcommand, Clone, Debug, PartialEq, Eq)] @@ -85,19 +96,19 @@ pub enum BinaryActions { #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct RotateArgs { #[arg(short, long, default_value_t = false)] - left: bool, + pub left: bool, #[clap(value_parser=maybe_hex::)] - base: u32, + pub base: u32, #[clap(value_parser=maybe_hex::)] - shift_width: u32, + pub shift_width: u32, } #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct XorArgs { #[clap(value_parser=maybe_hex::)] - a: u128, + pub a: u128, #[clap(value_parser=maybe_hex::)] - b: u128, + pub b: u128, } #[derive(Subcommand, Clone, Debug, PartialEq, Eq)] @@ -113,23 +124,23 @@ pub enum AlgoActions { #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct Feistel0InnerArgs { #[clap(value_parser=maybe_hex::)] - input: u16, + pub input: u16, #[clap(value_parser=maybe_hex::)] - key: u16, + pub key: u16, } #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct Feistel0SBOXArgs { #[clap(value_parser=maybe_hex::)] - index: u8, + pub index: u8, } #[derive(Args, Clone, Debug, PartialEq, Eq)] pub struct Feistel0Args{ #[clap(value_parser=maybe_hex::)] - input: u32, + pub input: u32, #[clap(value_parser=maybe_hex::)] - key: u32, + pub key: u32, #[arg(short, long, default_value_t = false)] - decrypt: bool, + pub decrypt: bool, } diff --git a/src/cplex/mod.rs b/src/cplex/mod.rs index d5b6239..9e54a5a 100644 --- a/src/cplex/mod.rs +++ b/src/cplex/mod.rs @@ -7,3 +7,4 @@ /// License: MIT /// Source: pub mod printing; +pub mod cli; diff --git a/src/cplex/printing.rs b/src/cplex/printing.rs index 478ded2..04b9da4 100644 --- a/src/cplex/printing.rs +++ b/src/cplex/printing.rs @@ -8,6 +8,8 @@ /// License: MIT /// Source: +use crate::cplex::cli::Cli; + use std::fmt::{Debug, LowerHex}; use num::Integer; diff --git a/src/main.rs b/src/main.rs index 4bbcdcb..588b8d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,156 +14,19 @@ //! Note that this library offers Python bindings using [PyO3](pyo3.rs) //! ___ //! Author: Christoph J. Scherr -//! //! License: MIT -//! //! Source: mod binary; mod math; mod algo; mod cplex; -use std::{str::FromStr, fmt::Debug}; +use cplex::cli::*; -use clap::{Args, Parser, Subcommand}; -use clap_num::maybe_hex; +use std::str::FromStr; + +use clap::Parser; use num_bigint; -/*************************************************************************************************/ -/// 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::)] - polynomial: u64, - #[clap(value_parser=maybe_hex::)] - relation: u64, -} - -#[derive(Args, Clone, Debug, PartialEq, Eq)] -pub struct PM1Args { - #[clap(value_parser=maybe_hex::)] - n: u128, - #[clap(value_parser=maybe_hex::)] - 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::)] - base: u32, - #[clap(value_parser=maybe_hex::)] - shift_width: u32, -} - -#[derive(Args, Clone, Debug, PartialEq, Eq)] -pub struct XorArgs { - #[clap(value_parser=maybe_hex::)] - a: u128, - #[clap(value_parser=maybe_hex::)] - 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::)] - input: u16, - #[clap(value_parser=maybe_hex::)] - key: u16, -} - -#[derive(Args, Clone, Debug, PartialEq, Eq)] -pub struct Feistel0SBOXArgs { - #[clap(value_parser=maybe_hex::)] - index: u8, -} - -#[derive(Args, Clone, Debug, PartialEq, Eq)] -pub struct Feistel0Args{ - #[clap(value_parser=maybe_hex::)] - input: u32, - #[clap(value_parser=maybe_hex::)] - key: u32, - #[arg(short, long, default_value_t = false)] - decrypt: bool, -} /*************************************************************************************************/ /// main function of plexcryptool. diff --git a/src/math/modred.rs b/src/math/modred.rs index 2a74ecb..fcd1cf5 100644 --- a/src/math/modred.rs +++ b/src/math/modred.rs @@ -10,7 +10,7 @@ /// License: MIT /// Source: -use crate::cplex; +use crate::cplex::printing::seperator; #[test] fn test_modred() { @@ -26,7 +26,7 @@ pub fn modred(mut poly: u64, relation: u64, verbose: bool) -> Result