gyllois python interface

This commit is contained in:
Christoph J. Scherr 2023-05-28 00:09:46 +02:00
parent e7e393293a
commit cb0bc911a4
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
3 changed files with 32 additions and 0 deletions

View File

@ -54,6 +54,7 @@ fn register_math_module(py: Python, parent_module: &PyModule) -> PyResult<()> {
math_module.add_function(wrap_pyfunction!(math::pm1::py_p_minus_one, math_module)?)?; math_module.add_function(wrap_pyfunction!(math::pm1::py_p_minus_one, math_module)?)?;
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::gcd::alt_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>()?; math_module.add_class::<math::gallois::GalloisFiled>()?;
parent_module.add_submodule(math_module)?; parent_module.add_submodule(math_module)?;

View File

@ -375,5 +375,10 @@ fn test_gallois_inverse() {
assert_eq!(field.inverse(54).unwrap(), 20); assert_eq!(field.inverse(54).unwrap(), 20);
assert!(field.inverse(0).is_err()); 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 add a test for a field that has a non prime base // TODO add a test for a field that has a non prime base
} }

View File

@ -23,6 +23,32 @@ pub fn egcd(mut a: u128, mut b: u128) -> Vec<i128> {
return vec![egcd.gcd, egcd.x, egcd.y] return vec![egcd.gcd, egcd.x, egcd.y]
} }
#[pyfunction]
/// own implementation of egcd
pub fn alt_egcd(mut a: i128, mut b: i128, recursion: bool) -> Vec<i128> {
if recursion && a > b {
let tmp = a;
a = b;
b = tmp;
}
if a == 0 {
return vec![b, 0, 1]
}
let v = alt_egcd(b % a, a, true);
let mut result = vec![
v[0],
v[2] - (b.checked_div(a).unwrap()) * v[1],
v[1],
];
return result;
}
#[test]
fn test_alt_gcd() {
assert_eq!(egcd(12193, 123213), alt_egcd(12193, 123213, false));
assert_eq!(egcd(52193, 123212), alt_egcd(52193, 123212, false));
}
#[pyfunction] #[pyfunction]
/// euclidian algorithm /// euclidian algorithm
pub fn gcd(a: u128, b: u128) -> u128 { pub fn gcd(a: u128, b: u128) -> u128 {