use pyo3::{exceptions::PyException, prelude::*};
use crate::core::{Float as FloatRust, KEY_LENGTH};
#[pyclass]
pub struct Float(FloatRust);
#[pymethods]
impl Float {
#[new]
fn new() -> PyResult<Self> {
match FloatRust::instantiate() {
Ok(itg) => Ok(Self(itg)),
Err(e) => Err(PyException::new_err(format!(
"FPE Float Instantiation failed: {e:?}"
))),
}
}
fn encrypt_decrypt(
&self,
key: Vec<u8>,
tweak: Vec<u8>,
input: f64,
encrypt_flag: bool,
) -> PyResult<f64> {
if key.len() != KEY_LENGTH {
return Err(PyException::new_err(format!(
"FPE Float error: key length incorrect: {}, expected {}",
key.len(),
KEY_LENGTH
)));
}
let mut k: [u8; 32] = [0; 32];
k.copy_from_slice(&key);
let output = if encrypt_flag {
self.0.encrypt(&k, &tweak, input)
} else {
self.0.decrypt(&k, &tweak, input)
};
match output {
Ok(ciphertext) => Ok(ciphertext),
Err(e) => Err(PyException::new_err(e.to_string())),
}
}
pub fn encrypt(&self, key: Vec<u8>, tweak: Vec<u8>, plaintext: f64) -> PyResult<f64> {
self.encrypt_decrypt(key, tweak, plaintext, true)
}
pub fn decrypt(&self, key: Vec<u8>, tweak: Vec<u8>, ciphertext: f64) -> PyResult<f64> {
self.encrypt_decrypt(key, tweak, ciphertext, false)
}
}