Skip to content

Commit

Permalink
update serialization tests where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jul 5, 2023
1 parent 2137e24 commit 3bc28d7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
3 changes: 3 additions & 0 deletions ferveo-python/ferveo/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class FerveoPublicKey:
def __hash__(self) -> int:
...

def __richcmp__(self, other: FerveoPublicKey, op: int) -> bool:
...


class Validator:

Expand Down
21 changes: 15 additions & 6 deletions ferveo-python/test/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Dkg,
DkgPublicKey,
FerveoPublicKey,
SharedSecret,
)


Expand Down Expand Up @@ -35,7 +36,8 @@ def make_dkg_public_key():


def make_shared_secret():
# TODO: implement this
# TODO: Implement this
# SharedSecret.from_bytes(os.urandom(584))
pass


Expand All @@ -44,27 +46,34 @@ def make_pk():


# def test_shared_secret_serialization():
# shared_secret = create_shared_secret_instance()
# shared_secret = make_shared_secret()
# serialized = bytes(shared_secret)
# deserialized = SharedSecret.from_bytes(serialized)
# TODO: Implement comparison
# assert shared_secret == deserialized
# # TODO: Implement __richcmp__
# # assert shared_secret == deserialized
# assert serialized == bytes(deserialized)

def test_keypair_serialization():
keypair = Keypair.random()
serialized = bytes(keypair)
deserialized = Keypair.from_bytes(serialized)
# TODO: Implement comparison
# assert keypair == deserialized
# TODO: Implement __richcmp__
# assert serialized == deserialized
assert serialized == bytes(deserialized)


def test_dkg_public_key_serialization():
dkg_pk = make_dkg_public_key()
serialized = bytes(dkg_pk)
deserialized = DkgPublicKey.from_bytes(serialized)
# TODO: Implement __richcmp__
assert serialized == bytes(deserialized)
assert len(serialized) == DkgPublicKey.serialized_size()


def test_public_key_serialization():
pk = make_pk()
serialized = bytes(pk)
deserialized = FerveoPublicKey.from_bytes(serialized)
assert pk == deserialized
assert len(serialized) == FerveoPublicKey.serialized_size()
8 changes: 8 additions & 0 deletions ferveo/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ mod test_ferveo_api {
(messages, validators, validator_keypairs)
}

#[test]
fn test_dkg_pk_serialization() {
let dkg_pk = DkgPublicKey::random();
let serialized = dkg_pk.to_bytes().unwrap();
let deserialized = DkgPublicKey::from_bytes(&serialized).unwrap();
assert_eq!(dkg_pk, deserialized);
}

#[test]
fn test_server_api_tdec_precomputed() {
let rng = &mut StdRng::seed_from_u64(0);
Expand Down
13 changes: 1 addition & 12 deletions ferveo/src/bindings_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,6 @@ where
}
}

// TODO: Consider implementing macros to generate following methods

// fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
// richcmp(self, other, op)
// }

// fn __hash__(&self) -> PyResult<isize> {
// let bytes = self.0.to_bytes()?;
// hash(stringify!($struct_name), &bytes)
// }

macro_rules! generate_bytes_serialization {
($struct_name:ident) => {
#[pymethods]
Expand Down Expand Up @@ -326,6 +315,7 @@ generate_boxed_bytes_serialization!(FerveoPublicKey, InnerPublicKey);

#[pymethods]
impl FerveoPublicKey {
// We implement `__richcmp__` because FerveoPublicKeys must be sortable in some cases
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
richcmp(self, other, op)
}
Expand Down Expand Up @@ -857,7 +847,6 @@ mod test_ferveo_python {

let shared_secret = combine_decryption_shares_simple(decryption_shares);

// TODO: Fails because of a bad shared secret
let plaintext =
decrypt_with_shared_secret(&ciphertext, aad, &shared_secret)
.unwrap();
Expand Down

0 comments on commit 3bc28d7

Please sign in to comment.