Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Python typings #139

Merged
merged 9 commits into from
Aug 1, 2023
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
Copy link
Member

@derekpierre derekpierre Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor nitpick I didn't pick up on earlier (and I know it's annoying), but can these be SIMPLE/PRECOMPUTED or Simple/Precomputed. The lowercase looks a little weird in the Python code 😅

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are class attributes, so I assumed they should be lowercase. I can fix this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. Admittedly, it's a nitpick so if it is easy to fix/makes sense then great; if not, then you can ignore.

I didn't notice it until I saw it used in the 3171.

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"
9 changes: 9 additions & 0 deletions ferveo/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub type Validator = crate::Validator<E>;
pub type Transcript = PubliclyVerifiableSS<E>;
pub type ValidatorMessage = (Validator, Transcript);

#[cfg(feature = "bindings-python")]
use crate::bindings_python;
pub use crate::EthereumAddress;
use crate::{
do_verify_aggregation, Error, PVSSMap, PubliclyVerifiableParams,
Expand Down Expand Up @@ -101,6 +103,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()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 - this seems much better than using strings.


#[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
Loading