From 6c1d4becd89005d6698734caa9d681dde727bff6 Mon Sep 17 00:00:00 2001
From: Piotr Roslaniec
Date: Wed, 12 Jul 2023 12:01:14 +0200
Subject: [PATCH] apply pr suggestions
---
ferveo-python/ferveo/__init__.pyi | 8 ++---
ferveo-python/test/test_serialization.py | 4 +--
ferveo/src/api.rs | 9 ++++++
ferveo/src/bindings_python.rs | 41 ++++++++++++++++--------
4 files changed, 42 insertions(+), 20 deletions(-)
diff --git a/ferveo-python/ferveo/__init__.pyi b/ferveo-python/ferveo/__init__.pyi
index 9fc82342..6c32cdc4 100644
--- a/ferveo-python/ferveo/__init__.pyi
+++ b/ferveo-python/ferveo/__init__.pyi
@@ -179,8 +179,8 @@ class SharedSecret:
@final
class FerveoVariant:
- simple: str
- precomputed: str
+ simple: FerveoVariant
+ precomputed: FerveoVariant
def encrypt(message: bytes, aad: bytes, dkg_public_key: DkgPublicKey) -> Ciphertext:
@@ -188,13 +188,13 @@ def encrypt(message: bytes, aad: bytes, dkg_public_key: DkgPublicKey) -> Ciphert
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:
...
diff --git a/ferveo-python/test/test_serialization.py b/ferveo-python/test/test_serialization.py
index f608f5b9..9eaccdbd 100644
--- a/ferveo-python/test/test_serialization.py
+++ b/ferveo-python/test/test_serialization.py
@@ -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"
diff --git a/ferveo/src/api.rs b/ferveo/src/api.rs
index 10b283a7..c909cfed 100644
--- a/ferveo/src/api.rs
+++ b/ferveo/src/api.rs
@@ -21,6 +21,8 @@ pub type Validator = crate::Validator;
pub type Transcript = PubliclyVerifiableSS;
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,
@@ -101,6 +103,13 @@ impl FerveoVariant {
}
}
+#[cfg(feature = "bindings-python")]
+impl From for FerveoVariant {
+ fn from(variant: bindings_python::FerveoVariant) -> Self {
+ variant.0
+ }
+}
+
#[serde_as]
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DkgPublicKey(
diff --git a/ferveo/src/bindings_python.rs b/ferveo/src/bindings_python.rs
index 0c5db521..51088e23 100644
--- a/ferveo/src/bindings_python.rs
+++ b/ferveo/src/bindings_python.rs
@@ -1,4 +1,7 @@
-use std::fmt::{Debug, Formatter};
+use std::{
+ fmt,
+ fmt::{Debug, Formatter},
+};
use ferveo_common::serialization::{FromBytes, ToBytes};
use pyo3::{
@@ -235,9 +238,9 @@ pub fn encrypt(
#[pyfunction]
pub fn combine_decryption_shares_simple(
- shares: Vec,
+ decryption_shares: Vec,
) -> SharedSecret {
- let shares = shares
+ let shares = decryption_shares
.iter()
.map(|share| share.0.clone())
.collect::>();
@@ -247,9 +250,9 @@ pub fn combine_decryption_shares_simple(
#[pyfunction]
pub fn combine_decryption_shares_precomputed(
- shares: Vec,
+ decryption_shares: Vec,
) -> SharedSecret {
- let shares = shares
+ let shares = decryption_shares
.iter()
.map(|share| share.0.clone())
.collect::>();
@@ -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::from_string(variant)
- .map_err(|err| FerveoPythonError::FerveoError(err).into())
+impl From 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)
}
}