Skip to content

Commit

Permalink
apply pr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jul 12, 2023
1 parent fbb97be commit 9bcce6a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
8 changes: 4 additions & 4 deletions ferveo-python/ferveo/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,22 @@ class SharedSecret:

@final
class FerveoVariant:
simple: str
precomputed: str
simple: FerveoVariant
precomputed: FerveoVariant


def encrypt(message: bytes, aad: bytes, dkg_public_key: DkgPublicKey) -> Ciphertext:
...


def combine_decryption_shares_simple(
shares: Sequence[DecryptionShareSimple],
decryption_shares: Sequence[DecryptionShareSimple],
) -> bytes:
...


def combine_decryption_shares_precomputed(
shares: Sequence[DecryptionSharePrecomputed],
decryption_shares: Sequence[DecryptionSharePrecomputed],
) -> SharedSecret:
...

Expand Down
4 changes: 2 additions & 2 deletions ferveo-python/test/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ def test_public_key_serialization():


def test_ferveo_variant_serialization():
assert FerveoVariant.precomputed == "FerveoVariant::Precomputed"
assert FerveoVariant.simple == "FerveoVariant::Simple"
assert str(FerveoVariant.precomputed) == "FerveoVariant::Precomputed"
assert str(FerveoVariant.simple) == "FerveoVariant::Simple"
11 changes: 9 additions & 2 deletions ferveo/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub type ValidatorMessage = (Validator, Transcript);

pub use crate::EthereumAddress;
use crate::{
do_verify_aggregation, Error, PVSSMap, PubliclyVerifiableParams,
PubliclyVerifiableSS, Result,
bindings_python, do_verify_aggregation, Error, PVSSMap,
PubliclyVerifiableParams, PubliclyVerifiableSS, Result,
};

pub type DecryptionSharePrecomputed = tpke::api::DecryptionSharePrecomputed;
Expand Down Expand Up @@ -101,6 +101,13 @@ impl FerveoVariant {
}
}

#[cfg(feature = "bindings-python")]
impl From<bindings_python::FerveoVariant> for FerveoVariant {
fn from(variant: bindings_python::FerveoVariant) -> Self {
variant.0
}
}

#[serde_as]
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DkgPublicKey(
Expand Down
41 changes: 27 additions & 14 deletions ferveo/src/bindings_python.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::fmt::{Debug, Formatter};
use std::{
fmt,
fmt::{Debug, Formatter},
};

use ferveo_common::serialization::{FromBytes, ToBytes};
use pyo3::{
Expand Down Expand Up @@ -235,9 +238,9 @@ pub fn encrypt(

#[pyfunction]
pub fn combine_decryption_shares_simple(
shares: Vec<DecryptionShareSimple>,
decryption_shares: Vec<DecryptionShareSimple>,
) -> SharedSecret {
let shares = shares
let shares = decryption_shares
.iter()
.map(|share| share.0.clone())
.collect::<Vec<_>>();
Expand All @@ -247,9 +250,9 @@ pub fn combine_decryption_shares_simple(

#[pyfunction]
pub fn combine_decryption_shares_precomputed(
shares: Vec<DecryptionSharePrecomputed>,
decryption_shares: Vec<DecryptionSharePrecomputed>,
) -> SharedSecret {
let shares = shares
let shares = decryption_shares
.iter()
.map(|share| share.0.clone())
.collect::<Vec<_>>();
Expand All @@ -268,25 +271,35 @@ pub fn decrypt_with_shared_secret(
}

#[pyclass(module = "ferveo")]
pub struct FerveoVariant {}
#[derive(Clone)]
pub struct FerveoVariant(pub(crate) api::FerveoVariant);

#[pymethods]
impl FerveoVariant {
#[classattr]
fn precomputed() -> &'static str {
api::FerveoVariant::Precomputed.as_str()
fn precomputed() -> FerveoVariant {
api::FerveoVariant::Precomputed.into()
}

#[classattr]
fn simple() -> &'static str {
api::FerveoVariant::Simple.as_str()
fn simple() -> FerveoVariant {
api::FerveoVariant::Simple.into()
}

fn __str__(&self) -> String {
self.0.to_string()
}
}

impl FerveoVariant {
pub fn inner_form_string(variant: &str) -> PyResult<api::FerveoVariant> {
api::FerveoVariant::from_string(variant)
.map_err(|err| FerveoPythonError::FerveoError(err).into())
impl From<api::FerveoVariant> for FerveoVariant {
fn from(variant: api::FerveoVariant) -> Self {
Self(variant)
}
}

impl fmt::Display for FerveoVariant {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

Expand Down

0 comments on commit 9bcce6a

Please sign in to comment.