Skip to content

Commit

Permalink
add equality to FerveoVariant python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jul 13, 2023
1 parent 6c1d4be commit cea467e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
19 changes: 19 additions & 0 deletions ferveo-python/ferveo/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Sequence, final


@final
class Keypair:
@staticmethod
Expand All @@ -24,6 +25,7 @@ class Keypair:
def public_key(self) -> FerveoPublicKey:
...


@final
class FerveoPublicKey:
@staticmethod
Expand All @@ -40,6 +42,10 @@ class FerveoPublicKey:
def serialized_size() -> int:
...

def __eq__(self, other: object) -> bool:
...


@final
class Validator:

Expand All @@ -50,6 +56,7 @@ class Validator:

public_key: FerveoPublicKey


@final
class Transcript:
@staticmethod
Expand All @@ -59,6 +66,7 @@ class Transcript:
def __bytes__(self) -> bytes:
...


@final
class DkgPublicKey:
@staticmethod
Expand All @@ -72,6 +80,7 @@ class DkgPublicKey:
def serialized_size() -> int:
...


@final
class ValidatorMessage:

Expand All @@ -85,6 +94,7 @@ class ValidatorMessage:
validator: Validator
transcript: Transcript


@final
class Dkg:

Expand All @@ -106,6 +116,7 @@ class Dkg:
def aggregate_transcripts(self, messages: Sequence[ValidatorMessage]) -> AggregatedTranscript:
...


@final
class Ciphertext:
@staticmethod
Expand All @@ -115,6 +126,7 @@ class Ciphertext:
def __bytes__(self) -> bytes:
...


@final
class DecryptionShareSimple:
@staticmethod
Expand All @@ -123,6 +135,8 @@ class DecryptionShareSimple:

def __bytes__(self) -> bytes:
...


@final
class DecryptionSharePrecomputed:
@staticmethod
Expand All @@ -132,6 +146,7 @@ class DecryptionSharePrecomputed:
def __bytes__(self) -> bytes:
...


@final
class AggregatedTranscript:

Expand Down Expand Up @@ -166,6 +181,7 @@ class AggregatedTranscript:
def __bytes__(self) -> bytes:
...


@final
class SharedSecret:

Expand All @@ -182,6 +198,9 @@ class FerveoVariant:
simple: FerveoVariant
precomputed: FerveoVariant

def __eq__(self, other: object) -> bool:
...


def encrypt(message: bytes, aad: bytes, dkg_public_key: DkgPublicKey) -> Ciphertext:
...
Expand Down
3 changes: 3 additions & 0 deletions ferveo-python/test/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ def test_public_key_serialization():
def test_ferveo_variant_serialization():
assert str(FerveoVariant.precomputed) == "FerveoVariant::Precomputed"
assert str(FerveoVariant.simple) == "FerveoVariant::Simple"
assert FerveoVariant.precomputed == FerveoVariant.precomputed
assert FerveoVariant.simple == FerveoVariant.simple
assert FerveoVariant.precomputed != FerveoVariant.simple
13 changes: 12 additions & 1 deletion ferveo/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub type ValidatorMessage = (Validator, Transcript);

#[cfg(feature = "bindings-python")]
use crate::bindings_python;
#[cfg(feature = "bindings-wasm")]
use crate::bindings_wasm;
pub use crate::EthereumAddress;
use crate::{
do_verify_aggregation, Error, PVSSMap, PubliclyVerifiableParams,
Expand Down Expand Up @@ -72,7 +74,9 @@ pub fn decrypt_with_shared_secret(
}

/// The ferveo variant to use for the decryption share derivation.
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Copy, Clone)]
#[derive(
PartialEq, Eq, Debug, Serialize, Deserialize, Copy, Clone, PartialOrd,
)]
pub enum FerveoVariant {
/// The simple variant requires m of n shares to decrypt
Simple,
Expand Down Expand Up @@ -110,6 +114,13 @@ impl From<bindings_python::FerveoVariant> for FerveoVariant {
}
}

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

#[serde_as]
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DkgPublicKey(
Expand Down
10 changes: 5 additions & 5 deletions ferveo/src/bindings_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ pub fn decrypt_with_shared_secret(
}

#[pyclass(module = "ferveo")]
#[derive(Clone)]
#[derive(
Clone, PartialEq, PartialOrd, Eq, derive_more::From, derive_more::AsRef,
)]
pub struct FerveoVariant(pub(crate) api::FerveoVariant);

#[pymethods]
Expand All @@ -289,11 +291,9 @@ impl FerveoVariant {
fn __str__(&self) -> String {
self.0.to_string()
}
}

impl From<api::FerveoVariant> for FerveoVariant {
fn from(variant: api::FerveoVariant) -> Self {
Self(variant)
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
richcmp(self, other, op)
}
}

Expand Down
25 changes: 20 additions & 5 deletions ferveo/src/bindings_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,33 @@ macro_rules! generate_common_methods {
}

#[wasm_bindgen]
pub struct FerveoVariant {}
#[derive(Clone, Debug, derive_more::AsRef, derive_more::From)]
pub struct FerveoVariant(pub(crate) api::FerveoVariant);

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

generate_common_methods!(FerveoVariant);

#[wasm_bindgen]
impl FerveoVariant {
#[wasm_bindgen(js_name = "precomputed", getter)]
pub fn precomputed() -> String {
api::FerveoVariant::Precomputed.as_str().to_string()
pub fn precomputed() -> FerveoVariant {
FerveoVariant(api::FerveoVariant::Precomputed)
}

#[wasm_bindgen(js_name = "simple", getter)]
pub fn simple() -> String {
api::FerveoVariant::Simple.as_str().to_string()
pub fn simple() -> FerveoVariant {
FerveoVariant(api::FerveoVariant::Simple)
}

#[allow(clippy::inherent_to_string_shadow_display)]
#[wasm_bindgen(js_name = "toString")]
pub fn to_string(&self) -> String {
self.0.to_string()
}
}

Expand Down

0 comments on commit cea467e

Please sign in to comment.