From 34385e8a6de950e911a760bc071c31ba3dde9417 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 19 May 2023 13:56:29 +0200 Subject: [PATCH] gallois cli tool --- src/cplex/cli.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/cplex/printing.rs | 30 +++++++++++++++++------------- src/main.rs | 17 +++++++++++++++++ src/math/gallois.rs | 14 +++++--------- 4 files changed, 79 insertions(+), 22 deletions(-) diff --git a/src/cplex/cli.rs b/src/cplex/cli.rs index a3284c8..ce242de 100644 --- a/src/cplex/cli.rs +++ b/src/cplex/cli.rs @@ -64,9 +64,14 @@ pub struct AlgoCommand { #[derive(Subcommand, Clone, Debug, PartialEq, Eq)] pub enum MathActions { #[command(name="modexp")] + /// perform exponentiation with a constant modulo applied Modexp(ModexpArgs), + /// perform modular reduction Modred(ModredArgs), + /// p minus 1 prime test Pm1(PM1Args), + //// calculate in a gallois field + Gallois(GalloisAction), } #[derive(Args, Clone, Debug, PartialEq, Eq)] @@ -92,6 +97,41 @@ pub struct PM1Args { pub max_prime: u128, } +#[derive(Args, Clone, Debug, PartialEq, Eq)] +pub struct GalloisAction { + #[clap(value_parser=maybe_hex::)] + pub field: u128, + #[command(subcommand)] + pub action: GalloisActions +} + +#[derive(Subcommand, Clone, Debug, PartialEq, Eq)] +pub enum GalloisActions { + /// draw the root of n + Sqrt(GalloisSqrtArgs), + /// reduce n to the range of the field + Reduce(GalloisReduceArgs), + /// calculate the (multiplicative) inverse of n + Invert(GalloisInvertArgs), +} + +#[derive(Args, Clone, Debug, PartialEq, Eq)] +pub struct GalloisSqrtArgs { + #[clap(value_parser=maybe_hex::)] + pub n: u128, +} + +#[derive(Args, Clone, Debug, PartialEq, Eq)] +pub struct GalloisReduceArgs { + pub n: i128, +} + +#[derive(Args, Clone, Debug, PartialEq, Eq)] +pub struct GalloisInvertArgs { + #[clap(value_parser=maybe_hex::)] + pub n: u128, +} + #[derive(Subcommand, Clone, Debug, PartialEq, Eq)] pub enum BinaryActions { /// bit rotation/circular shifting (only 32bit) diff --git a/src/cplex/printing.rs b/src/cplex/printing.rs index 2d2442a..cd38f8d 100644 --- a/src/cplex/printing.rs +++ b/src/cplex/printing.rs @@ -10,7 +10,7 @@ use crate::cplex::cli::Cli; -use std::fmt::{Debug, LowerHex}; +use std::fmt::{Debug, LowerHex, Display}; use pyo3::prelude::*; @@ -48,11 +48,13 @@ pub fn seperator() { /////////////////////////////////////////////////////////////////////////////////////////////////// /// process a result with some int -pub fn proc_result(result: Result, args: Cli) +pub fn proc_result(result: Result, args: Cli) where T: Debug, T: Integer, - T: LowerHex + T: LowerHex, + T: Display, + K: Debug { if args.verbose { seperator(); @@ -60,11 +62,11 @@ pub fn proc_result(result: Result, args: Cli) match result { Ok(res) => { if args.machine { - println!("{:#x}", res); + println!("{} ({:#x})", res, res); } else { seperator(); - println!("result is {:#x}", res); + println!("result is {} ({:#x})", res, res); } } Err(e) => { @@ -84,17 +86,18 @@ pub fn proc_num(num: T, args: Cli) where T: Debug, T: Integer, - T: LowerHex + T: LowerHex, + T: Display, { if args.verbose { seperator(); } - if args.machine { - println!("{:#x}", num); - } - else { - seperator(); - println!("result is {:#x}", num); + if args.machine { + println!("{} ({:#x})", num, num); + } + else { + seperator(); + println!("result is {} ({:#x})", num, num); } } @@ -116,9 +119,10 @@ pub fn proc_vec(vec: Vec, args: Cli) } /// process a result with some vec -pub fn proc_result_vec(res: Result, String>, args: Cli) +pub fn proc_result_vec(res: Result, K>, args: Cli) where T: Debug, + K: Debug { if args.verbose { seperator(); diff --git a/src/main.rs b/src/main.rs index 06aed42..16bb407 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,6 +60,23 @@ pub fn main() { ); cplex::printing::proc_result_vec(vec, args); } + MathActions::Gallois(gal_args) => { + let field = math::gallois::GalloisFiled::new(gal_args.field, args.verbose); + match gal_args.action { + GalloisActions::Sqrt(gal_sqrt_args) => { + let result = field.sqrt(gal_sqrt_args.n); + cplex::printing::proc_result(result, args); + } + GalloisActions::Reduce(gal_red_args) => { + let result = field.reduce_neg(gal_red_args.n); + cplex::printing::proc_num(result, args); + } + GalloisActions::Invert(gal_inv_args) => { + let result = field.inverse(gal_inv_args.n); + cplex::printing::proc_result(result, args); + } + } + } } } Commands::Binary(action) => { diff --git a/src/math/gallois.rs b/src/math/gallois.rs index b58c7f2..f3e592a 100644 --- a/src/math/gallois.rs +++ b/src/math/gallois.rs @@ -56,7 +56,7 @@ pub struct GalloisFiled { impl GalloisFiled { /// make a new gallois field pub fn new(base: u128, verbose: bool) -> Self { - let mut field = GalloisFiled{ + let field = GalloisFiled{ base, // TODO: calculate the characteristic cha: 0, @@ -70,14 +70,7 @@ impl GalloisFiled { /// reduce a number to fit into the gallois field pub fn reduce(self, n: u128) -> u128 { - let mut n = n; - if n < 0 { - while n < 0 { - n += self.base; - } - } - n %= self.base; - return n; + return n % self.base; } /// reduce a negative number to fit into the gallois field @@ -135,6 +128,7 @@ impl GalloisFiled { /////////////////////////////////////////////////////////////////////////////////////////////////// #[test] fn test_gallois_sqrt() { + let field = GalloisFiled::new(67, true); panic!("TODO") } @@ -149,4 +143,6 @@ fn test_gallois_inverse() { assert_eq!(field.inverse(6).unwrap(), 14); assert_eq!(field.inverse(54).unwrap(), 20); assert!(field.inverse(0).is_err()); + + // TODO add a test for a field that has a non prime base }