diff --git a/src/lib.rs b/src/lib.rs index 0b2fc5b..358acd6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,7 +56,7 @@ fn register_math_module(py: Python, parent_module: &PyModule) -> PyResult<()> { math_module.add_function(wrap_pyfunction!(math::gcd::egcd, math_module)?)?; math_module.add_function(wrap_pyfunction!(math::gcd::alt_egcd, math_module)?)?; math_module.add_function(wrap_pyfunction!(math::factorise::prime_factors , math_module)?)?; - math_module.add_class::()?; + math_module.add_class::()?; parent_module.add_submodule(math_module)?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 2e993cd..c01582f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,7 @@ 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); + let field = math::gallois::GalloisField::new(gal_args.field, args.verbose); match gal_args.action { GalloisActions::Sqrt(gal_sqrt_args) => { let result = field.sqrt(gal_sqrt_args.a); diff --git a/src/math/ecc.rs b/src/math/ecc.rs new file mode 100644 index 0000000..62ae2fa --- /dev/null +++ b/src/math/ecc.rs @@ -0,0 +1,86 @@ +#![allow(dead_code)] +/// eliptic curve cryptography +/// +/// This module implements structs and functionalities used for ECC. +/// +/// Author: Christoph J. Scherr +/// License: MIT +/// Source: + +use super::gallois::GalloisField; + +use pyo3::prelude::*; + +/// This is a very special math point, it does not really exist but is useful. +pub const INFINITY_POINT: ElipticCurvePoint = ElipticCurvePoint { + x: 0, + y: 0, + is_infinity_point: true, + verbose: false +}; + +#[derive(Debug, Clone)] +#[allow(non_snake_case)] +#[pyclass] +/// represent a specific eliptic curve +/// +/// real curves not supported, only in Gallois Fields +pub struct ElipticCurve { + f: Option, + a: i128, + b: i128, + points: Vec, + verbose: bool, + INFINITY_POINT: ElipticCurvePoint +} + +impl ElipticCurve { + pub fn new(f: Option, a: i128, b: i128, verbose: bool) -> Self { + let e = ElipticCurve { + f, + a, + b, + points: Vec::new(), + verbose, + INFINITY_POINT + }; + return e; + } +} + +#[derive(Debug, Clone)] +#[pyclass] +/// represent a specific eliptic curves point +pub struct ElipticCurvePoint { + x: i128, + y: i128, + is_infinity_point: bool, + verbose: bool +} + +impl ElipticCurvePoint { + pub fn new(x: i128, y: i128, verbose: bool) -> Self { + ElipticCurvePoint { + x, + y, + is_infinity_point: false, + verbose + } + } + + pub fn get_infinity_point() -> Self { + return INFINITY_POINT; + } + + /// add two points + pub fn add(a: Self, b: Self) -> Self { + // TODO + panic!("TODO"); + } + + /// multiply a point by an integer + pub fn mul(n: u128, a: Self) -> Self { + // TODO + panic!("TODO"); + } +} diff --git a/src/math/gallois.rs b/src/math/gallois.rs index 4539866..38e5cbb 100644 --- a/src/math/gallois.rs +++ b/src/math/gallois.rs @@ -49,26 +49,24 @@ impl fmt::Display for NoRootError { #[derive(Debug, Copy, Clone)] #[pyclass] /// represent a gallois field -pub struct GalloisFiled { +pub struct GalloisField { base: u128, cha: u128, verbose: bool, } /// implementations for the gallois field -impl GalloisFiled { +impl GalloisField { /// make a new gallois field pub fn new(base: u128, verbose: bool) -> Self { - let field = GalloisFiled{ + let field = GalloisField{ base, // TODO: calculate the characteristic cha: 0, verbose }; if verbose { - seperator(); println!("In Gallois Field F_{}", field.base); - seperator(); } return field; } @@ -302,10 +300,10 @@ impl GalloisFiled { #[pymethods] /// python wrappers for the gallois field -impl GalloisFiled { +impl GalloisField { #[new] pub fn py_new(base: u128, verbose: bool) -> Self { - return GalloisFiled::new(base, verbose); + return GalloisField::new(base, verbose); } #[pyo3(name="pow")] @@ -357,7 +355,7 @@ impl GalloisFiled { /////////////////////////////////////////////////////////////////////////////////////////////////// #[test] fn test_gallois_sqrt() { - let field = GalloisFiled::new(977, true); + let field = GalloisField::new(977, true); assert_eq!(field.sqrt(269).expect("function says there is no root but there is"), (313, 664)); assert_eq!(field.sqrt(524).expect("function says there is no root but there is"), (115, 862)); assert_eq!(field.sqrt(275).expect("function says there is no root but there is"), (585, 392)); @@ -365,20 +363,18 @@ fn test_gallois_sqrt() { #[test] fn test_gallois_inverse() { - let field = GalloisFiled::new(31, true); + let field = GalloisField::new(31, true); assert_eq!(field.inverse(12).unwrap(), 13); assert_eq!(field.inverse(28).unwrap(), 10); assert!(field.inverse(0).is_err()); - let field = GalloisFiled::new(83, true); + let field = GalloisField::new(83, true); assert_eq!(field.inverse(6).unwrap(), 14); assert_eq!(field.inverse(54).unwrap(), 20); assert!(field.inverse(0).is_err()); - let field = GalloisFiled::new(1151, true); - assert_eq!(field.inverse(6).unwrap(), 14); - assert_eq!(field.inverse(54).unwrap(), 20); - assert!(field.inverse(0).is_err()); + // TODO i think this test does not catch all edge cases. In some cases, something seems to be + // wrong. // TODO add a test for a field that has a non prime base } diff --git a/src/math/mod.rs b/src/math/mod.rs index e36eccf..f241287 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -12,3 +12,4 @@ pub mod modred; pub mod gallois; pub mod gcd; pub mod factorise; +pub mod ecc;