Skip to content

Commit

Permalink
Expose more types and serialization in Python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Mar 18, 2021
1 parent 1e3ad49 commit 3f978c6
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
82 changes: 81 additions & 1 deletion umbral-pre-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ impl SecretKey {
backend: umbral_pre::SecretKey::random(),
}
}

pub fn to_encrypted_bytes(&self, py: Python, password: &[u8]) -> Option<PyObject> {
let ciphertext = self.backend.to_encrypted_bytes(password)?;
Some(PyBytes::new(py, ciphertext.as_slice()).into())
}

#[staticmethod]
pub fn from_encrypted_bytes(password: &[u8], bytes: &[u8]) -> Option<Self> {
let sk = umbral_pre::SecretKey::from_encrypted_bytes(password, bytes)?;
Some(Self { backend: sk })
}
}

#[pyclass(module = "umbral")]
#[derive(PartialEq)]
pub struct PublicKey {
backend: umbral_pre::PublicKey,
}
Expand All @@ -35,14 +47,53 @@ impl PublicKey {
backend: umbral_pre::PublicKey::from_secret_key(&sk.backend),
}
}

pub fn __bytes__(&self, py: Python) -> PyObject {
let serialized = self.backend.to_array();
PyBytes::new(py, serialized.as_slice()).into()
}

#[staticmethod]
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
let backend_pubkey = umbral_pre::PublicKey::from_bytes(bytes)?;
Some(Self {
backend: backend_pubkey,
})
}
}

#[pyproto]
impl PyObjectProtocol for PublicKey {
fn __richcmp__(&self, other: PyRef<PublicKey>, op: CompareOp) -> PyResult<bool> {
match op {
CompareOp::Eq => Ok(self == &*other),
CompareOp::Ne => Ok(self != &*other),
_ => Err(PyTypeError::new_err("PublicKey objects are not ordered")),
}
}
}

#[pyclass(module = "umbral")]
#[derive(Clone)]
pub struct Capsule {
backend: umbral_pre::Capsule,
}

#[pymethods]
impl Capsule {
pub fn __bytes__(&self, py: Python) -> PyObject {
let serialized = self.backend.to_array();
PyBytes::new(py, serialized.as_slice()).into()
}

#[staticmethod]
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
let backend_capsule = umbral_pre::Capsule::from_bytes(bytes)?;
Some(Self {
backend: backend_capsule,
})
}
}

#[pyfunction]
pub fn encrypt(py: Python, pk: &PublicKey, plaintext: &[u8]) -> (Capsule, PyObject) {
let (capsule, ciphertext) = umbral_pre::encrypt(&pk.backend, plaintext).unwrap();
Expand Down Expand Up @@ -83,6 +134,19 @@ impl KeyFrag {
receiving_pk.map(|pk| &pk.backend),
)
}

pub fn __bytes__(&self, py: Python) -> PyObject {
let serialized = self.backend.to_array();
PyBytes::new(py, serialized.as_slice()).into()
}

#[staticmethod]
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
let backend_kfrag = umbral_pre::KeyFrag::from_bytes(bytes)?;
Some(Self {
backend: backend_kfrag,
})
}
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -135,6 +199,19 @@ impl CapsuleFrag {
&receiving_pk.backend,
)
}

pub fn __bytes__(&self, py: Python) -> PyObject {
let serialized = self.backend.to_array();
PyBytes::new(py, serialized.as_slice()).into()
}

#[staticmethod]
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
let backend_cfrag = umbral_pre::CapsuleFrag::from_bytes(bytes)?;
Some(Self {
backend: backend_cfrag,
})
}
}

#[pyfunction]
Expand Down Expand Up @@ -174,6 +251,9 @@ pub fn decrypt_reencrypted(
fn _umbral(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<SecretKey>()?;
m.add_class::<PublicKey>()?;
m.add_class::<Capsule>()?;
m.add_class::<KeyFrag>()?;
m.add_class::<CapsuleFrag>()?;
m.add_function(wrap_pyfunction!(encrypt, m)?).unwrap();
m.add_function(wrap_pyfunction!(decrypt_original, m)?)
.unwrap();
Expand Down
3 changes: 3 additions & 0 deletions umbral-pre-python/umbral_pre/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from ._umbral import (
SecretKey,
PublicKey,
Capsule,
KeyFrag,
CapsuleFrag,
encrypt,
decrypt_original,
decrypt_reencrypted,
Expand Down

0 comments on commit 3f978c6

Please sign in to comment.