gallois py interface

This commit is contained in:
Christoph J. Scherr 2023-05-27 18:29:39 +02:00
parent 8a69fa04d1
commit e7e393293a
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
2 changed files with 38 additions and 2 deletions

View File

@ -55,6 +55,7 @@ fn register_math_module(py: Python, parent_module: &PyModule) -> PyResult<()> {
math_module.add_function(wrap_pyfunction!(math::gcd::gcd, math_module)?)?; math_module.add_function(wrap_pyfunction!(math::gcd::gcd, math_module)?)?;
math_module.add_function(wrap_pyfunction!(math::gcd::egcd, math_module)?)?; math_module.add_function(wrap_pyfunction!(math::gcd::egcd, math_module)?)?;
math_module.add_function(wrap_pyfunction!(math::factorise::prime_factors , math_module)?)?; math_module.add_function(wrap_pyfunction!(math::factorise::prime_factors , math_module)?)?;
math_module.add_class::<math::gallois::GalloisFiled>()?;
parent_module.add_submodule(math_module)?; parent_module.add_submodule(math_module)?;
Ok(()) Ok(())
} }

View File

@ -13,7 +13,7 @@ use core::fmt;
use num::Integer; use num::Integer;
use pyo3::prelude::*; use pyo3::{prelude::*, exceptions::PyValueError};
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug)] #[derive(Debug)]
@ -307,6 +307,12 @@ impl GalloisFiled {
pub fn py_new(base: u128, verbose: bool) -> Self { pub fn py_new(base: u128, verbose: bool) -> Self {
return GalloisFiled::new(base, verbose); return GalloisFiled::new(base, verbose);
} }
#[pyo3(name="pow")]
/// calculate the exponent of a base in the field
pub fn py_pow(&self, base: u128, exp: u128) -> u128 {
return self.pow(base, exp);
}
#[pyo3(name="reduce")] #[pyo3(name="reduce")]
/// reduce any int /// reduce any int
@ -316,7 +322,36 @@ impl GalloisFiled {
} }
return self.reduce(n as u128); return self.reduce(n as u128);
} }
// TODO implement wrappers for all other gallois methods
#[pyo3(name="a_inverse")]
/// find the additive inverse of a number
pub fn py_a_inverse(&self, n: u128) -> u128 {
return self.a_inverse(n)
}
#[pyo3(name="sqrt")]
/// calculate the square root of a number in a field
pub fn py_sqrt(&self, a: u128) -> PyResult<(u128, u128)> {
match self.sqrt(a) {
Ok(v) => Ok(v),
Err(e) => {
let py_e = PyValueError::new_err(e.to_string());
return Err(py_e)
}
}
}
#[pyo3(name="inverse")]
/// get multiplicative inverse
pub fn py_inverse(&self, n: u128) -> PyResult<u128> {
match self.inverse(n) {
Ok(v) => Ok(v),
Err(e) => {
let py_e = PyValueError::new_err(e.to_string());
return Err(py_e)
}
}
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////