Skip to content

Commit

Permalink
Some simplification using derived From traits
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Sep 19, 2022
1 parent afed7f6 commit d42ae5d
Showing 1 changed file with 18 additions and 39 deletions.
57 changes: 18 additions & 39 deletions umbral-pre/src/bindings_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ pub struct SecretKeyFactory {
impl SecretKeyFactory {
#[staticmethod]
pub fn random() -> Self {
Self {
backend: umbral_pre::SecretKeyFactory::random(),
}
umbral_pre::SecretKeyFactory::random().into()
}

#[staticmethod]
Expand All @@ -156,22 +154,16 @@ impl SecretKeyFactory {
#[staticmethod]
pub fn from_secure_randomness(seed: &[u8]) -> PyResult<SecretKeyFactory> {
umbral_pre::SecretKeyFactory::from_secure_randomness(seed)
.map(|backend_sk| SecretKeyFactory {
backend: backend_sk,
})
.map(SecretKeyFactory::from)
.map_err(|err| PyValueError::new_err(format!("{}", err)))
}

pub fn make_key(&self, label: &[u8]) -> SecretKey {
SecretKey {
backend: self.backend.make_key(label),
}
self.backend.make_key(label).into()
}

pub fn make_factory(&self, label: &[u8]) -> Self {
Self {
backend: self.backend.make_factory(label),
}
self.backend.make_factory(label).into()
}

pub fn to_secret_bytes(&self) -> PyResult<PyObject> {
Expand Down Expand Up @@ -229,6 +221,7 @@ impl PublicKey {
}

#[pyclass(module = "umbral")]
#[derive(derive_more::From)]
pub struct Signer {
pub backend: umbral_pre::Signer,
}
Expand All @@ -237,21 +230,15 @@ pub struct Signer {
impl Signer {
#[new]
pub fn new(sk: &SecretKey) -> Self {
Self {
backend: umbral_pre::Signer::new(sk.backend.clone()),
}
umbral_pre::Signer::new(sk.backend.clone()).into()
}

pub fn sign(&self, message: &[u8]) -> Signature {
Signature {
backend: self.backend.sign(message),
}
self.backend.sign(message).into()
}

pub fn verifying_key(&self) -> PublicKey {
PublicKey {
backend: self.backend.verifying_key(),
}
self.backend.verifying_key().into()
}

fn __str__(&self) -> PyResult<String> {
Expand Down Expand Up @@ -342,9 +329,7 @@ pub fn encrypt(
umbral_pre::encrypt(&delegating_pk.backend, plaintext)
.map(|(backend_capsule, ciphertext)| {
(
Capsule {
backend: backend_capsule,
},
backend_capsule.into(),
PyBytes::new(py, &ciphertext).into(),
)
})
Expand Down Expand Up @@ -385,9 +370,7 @@ impl KeyFrag {
receiving_pk.map(|pk| &pk.backend),
)
.map_err(|(err, _kfrag)| VerificationError::new_err(format!("{}", err)))
.map(|backend_vkfrag| VerifiedKeyFrag {
backend: backend_vkfrag,
})
.map(VerifiedKeyFrag::from)
}

pub fn skip_verification(&self) -> VerifiedKeyFrag {
Expand Down Expand Up @@ -424,7 +407,7 @@ impl KeyFrag {
}

#[pyclass(module = "umbral")]
#[derive(PartialEq, Clone, derive_more::AsRef)]
#[derive(PartialEq, Clone, derive_more::AsRef, derive_more::From)]
pub struct VerifiedKeyFrag {
pub backend: umbral_pre::VerifiedKeyFrag,
}
Expand All @@ -434,7 +417,7 @@ impl VerifiedKeyFrag {
#[staticmethod]
pub fn from_verified_bytes(data: &[u8]) -> PyResult<Self> {
umbral_pre::VerifiedKeyFrag::from_verified_bytes(data)
.map(|vkfrag| Self { backend: vkfrag })
.map(Self::from)
.map_err(|err| PyValueError::new_err(format!("{}", err)))
}

Expand Down Expand Up @@ -518,9 +501,7 @@ impl CapsuleFrag {
&receiving_pk.backend,
)
.map_err(|(err, _cfrag)| VerificationError::new_err(format!("{}", err)))
.map(|backend_vcfrag| VerifiedCapsuleFrag {
backend: backend_vcfrag,
})
.map(VerifiedCapsuleFrag::from)
}

pub fn skip_verification(&self) -> VerifiedCapsuleFrag {
Expand Down Expand Up @@ -557,7 +538,7 @@ impl CapsuleFrag {
}

#[pyclass(module = "umbral")]
#[derive(PartialEq, Clone, derive_more::AsRef)]
#[derive(PartialEq, Clone, derive_more::AsRef, derive_more::From)]
pub struct VerifiedCapsuleFrag {
pub backend: umbral_pre::VerifiedCapsuleFrag,
}
Expand All @@ -579,7 +560,7 @@ impl VerifiedCapsuleFrag {
#[staticmethod]
pub fn from_verified_bytes(data: &[u8]) -> PyResult<Self> {
umbral_pre::VerifiedCapsuleFrag::from_verified_bytes(data)
.map(|vcfrag| Self { backend: vcfrag })
.map(Self::from)
.map_err(|err| PyValueError::new_err(format!("{}", err)))
}

Expand All @@ -602,9 +583,7 @@ impl VerifiedCapsuleFrag {
#[pyfunction]
pub fn reencrypt(capsule: &Capsule, kfrag: &VerifiedKeyFrag) -> VerifiedCapsuleFrag {
let backend_vcfrag = umbral_pre::reencrypt(&capsule.backend, kfrag.backend.clone());
VerifiedCapsuleFrag {
backend: backend_vcfrag,
}
VerifiedCapsuleFrag::from(backend_vcfrag)
}

#[pyfunction]
Expand All @@ -616,11 +595,11 @@ pub fn decrypt_reencrypted(
verified_cfrags: Vec<VerifiedCapsuleFrag>,
ciphertext: &[u8],
) -> PyResult<PyObject> {
let backend_cfrags: Vec<umbral_pre::VerifiedCapsuleFrag> = verified_cfrags
let backend_cfrags = verified_cfrags
.iter()
.cloned()
.map(|vcfrag| vcfrag.backend)
.collect();
.collect::<Vec<_>>();
umbral_pre::decrypt_reencrypted(
&receiving_sk.backend,
&delegating_pk.backend,
Expand Down

0 comments on commit d42ae5d

Please sign in to comment.