This commit is contained in:
Christoph J. Scherr 2023-05-28 00:52:44 +02:00
parent cb0bc911a4
commit c29b08487e
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
5 changed files with 99 additions and 16 deletions

View File

@ -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::gallois::GalloisFiled>()?;
math_module.add_class::<math::gallois::GalloisField>()?;
parent_module.add_submodule(math_module)?;
Ok(())
}

View File

@ -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);

86
src/math/ecc.rs Normal file
View File

@ -0,0 +1,86 @@
#![allow(dead_code)]
/// eliptic curve cryptography
///
/// This module implements structs and functionalities used for ECC.
///
/// Author: Christoph J. Scherr <software@cscherr.de>
/// License: MIT
/// Source: <https://git.cscherr.de/PlexSheep/plexcryptool/>
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<GalloisField>,
a: i128,
b: i128,
points: Vec<ElipticCurvePoint>,
verbose: bool,
INFINITY_POINT: ElipticCurvePoint
}
impl ElipticCurve {
pub fn new(f: Option<GalloisField>, 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");
}
}

View File

@ -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
}

View File

@ -12,3 +12,4 @@ pub mod modred;
pub mod gallois;
pub mod gcd;
pub mod factorise;
pub mod ecc;