From 4943a7d47c41717af2edfc326fd2da18b379b8b7 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 23 May 2023 16:30:16 +0200 Subject: [PATCH] gallois python compat --- src-py/plexcryptool/math/gallois.pyi | 23 +++++++++++++++++++++++ src/math/gallois.rs | 22 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src-py/plexcryptool/math/gallois.pyi diff --git a/src-py/plexcryptool/math/gallois.pyi b/src-py/plexcryptool/math/gallois.pyi new file mode 100644 index 0000000..f508353 --- /dev/null +++ b/src-py/plexcryptool/math/gallois.pyi @@ -0,0 +1,23 @@ +""" +gallois field calculations + +Implements a number of calculations in gallois fields. Use the GalloisFiled struct/class and it's methods. + + +___ +@Author: Christoph J. Scherr +@License: MIT +@Source: +""" + +class GalloisFiled: + def __init__(self, base: int, verbose: bool) -> None: + """ + Create a new Gallois field + """ + ... + + def reduce(self, n: int) -> int: + """ + reduce the given number to fit into the field + """ diff --git a/src/math/gallois.rs b/src/math/gallois.rs index 5b691f3..4efc15f 100644 --- a/src/math/gallois.rs +++ b/src/math/gallois.rs @@ -13,6 +13,8 @@ use core::fmt; use num::Integer; +use pyo3::prelude::*; + /////////////////////////////////////////////////////////////////////////////////////////////////// #[derive(Debug)] /// used when trying to find a root for a number which does not have a root. @@ -45,6 +47,7 @@ impl fmt::Display for NoRootError { } /////////////////////////////////////////////////////////////////////////////////////////////////// #[derive(Debug, Copy, Clone)] +#[pyclass] /// represent a gallois field pub struct GalloisFiled { base: u128, @@ -297,6 +300,25 @@ impl GalloisFiled { } } +#[pymethods] +/// python wrappers for the gallois field +impl GalloisFiled { + #[new] + pub fn py_new(base: u128, verbose: bool) -> Self { + return GalloisFiled::new(base, verbose); + } + + #[pyo3(name="reduce")] + /// reduce any int + pub fn py_reduce(&self, n: i128) -> u128 { + if n.is_negative() { + return self.reduce_neg(n); + } + return self.reduce(n as u128); + } + // TODO implement wrappers for all other gallois methods +} + /////////////////////////////////////////////////////////////////////////////////////////////////// #[test] fn test_gallois_sqrt() {