Skip to content

Commit

Permalink
py-bindings: expose SecretKeyFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Mar 26, 2021
1 parent edc21b6 commit 696c5fc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
48 changes: 48 additions & 0 deletions umbral-pre-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use pyo3::PyObjectProtocol;
use umbral_pre::SerializableToArray;

#[pyclass(module = "umbral")]
#[derive(PartialEq)]
pub struct SecretKey {
backend: umbral_pre::SecretKey,
}
Expand Down Expand Up @@ -35,6 +36,52 @@ impl SecretKey {
}
}

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

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

#[pymethods]
impl SecretKeyFactory {
#[staticmethod]
pub fn random() -> Self {
Self {
backend: umbral_pre::SecretKeyFactory::random(),
}
}

pub fn secret_key_by_label(&self, label: &[u8]) -> Option<SecretKey> {
let backend_sk = self.backend.secret_key_by_label(label)?;
Some(SecretKey {
backend: backend_sk,
})
}

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_factory = umbral_pre::SecretKeyFactory::from_bytes(bytes)?;
Some(Self {
backend: backend_factory,
})
}
}

#[pyclass(module = "umbral")]
#[derive(PartialEq)]
pub struct PublicKey {
Expand Down Expand Up @@ -254,6 +301,7 @@ pub fn decrypt_reencrypted(
#[pymodule]
fn _umbral(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<SecretKey>()?;
m.add_class::<SecretKeyFactory>()?;
m.add_class::<PublicKey>()?;
m.add_class::<Capsule>()?;
m.add_class::<KeyFrag>()?;
Expand Down
1 change: 1 addition & 0 deletions umbral-pre-python/umbral_pre/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ._umbral import (
SecretKey,
SecretKeyFactory,
PublicKey,
Capsule,
KeyFrag,
Expand Down

0 comments on commit 696c5fc

Please sign in to comment.