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

Serde serialization rework #94

Merged
merged 5 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

Under construction.
### Changed

- When serialized to a human-readable format using `serde`, hex-encoded objects now have a `0x` prefix. ([#94])


### Added

- `serde` utility functions to serialize bytestrings as bytes or hex/base64 encoded strings, depending on the target format. Exposed as `serde_bytes` module. ([#94])


[#94]: https://github.com/nucypher/rust-umbral/pull/94


## [0.5.2] - 2022-03-15
Expand Down
18 changes: 9 additions & 9 deletions umbral-pre/src/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rand_core::{CryptoRng, RngCore};
use typenum::op;

#[cfg(feature = "serde-support")]
use crate::serde::{serde_deserialize, serde_serialize, Representation};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::capsule_frag::CapsuleFrag;
use crate::curve::{CurvePoint, CurveScalar, NonZeroCurveScalar};
Expand All @@ -21,7 +21,7 @@ use crate::traits::{
};

#[cfg(feature = "serde-support")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::serde_bytes::{deserialize_with_encoding, serialize_as_array, Encoding};

/// Errors that can happen when opening a `Capsule` using reencrypted `CapsuleFrag` objects.
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Serialize for Capsule {
where
S: Serializer,
{
serde_serialize(self, serializer, Representation::Base64)
serialize_as_array(self, serializer, Encoding::Base64)
}
}

Expand All @@ -103,7 +103,7 @@ impl<'de> Deserialize<'de> for Capsule {
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer, Representation::Base64)
deserialize_with_encoding(deserializer, Encoding::Base64)
}
}

Expand Down Expand Up @@ -268,10 +268,10 @@ mod tests {
};

#[cfg(feature = "serde-support")]
use crate::serde::tests::{check_deserialization, check_serialization};

#[cfg(feature = "serde-support")]
use crate::serde::Representation;
use crate::serde_bytes::{
tests::{check_deserialization, check_serialization},
Encoding,
};

#[test]
fn test_serialize() {
Expand Down Expand Up @@ -354,7 +354,7 @@ mod tests {
let delegating_pk = delegating_sk.public_key();
let (capsule, _key_seed) = Capsule::from_public_key(&mut OsRng, &delegating_pk);

check_serialization(&capsule, Representation::Base64);
check_serialization(&capsule, Encoding::Base64);
check_deserialization(&capsule);
}
}
16 changes: 8 additions & 8 deletions umbral-pre/src/capsule_frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::traits::{
};

#[cfg(feature = "serde-support")]
use crate::serde::{serde_deserialize, serde_serialize, Representation};
use crate::serde_bytes::{deserialize_with_encoding, serialize_as_array, Encoding};

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CapsuleFragProof {
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Serialize for CapsuleFrag {
where
S: Serializer,
{
serde_serialize(self, serializer, Representation::Base64)
serialize_as_array(self, serializer, Encoding::Base64)
}
}

Expand All @@ -179,7 +179,7 @@ impl<'de> Deserialize<'de> for CapsuleFrag {
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer, Representation::Base64)
deserialize_with_encoding(deserializer, Encoding::Base64)
}
}

Expand Down Expand Up @@ -385,10 +385,10 @@ mod tests {
};

#[cfg(feature = "serde-support")]
use crate::serde::tests::{check_deserialization, check_serialization};

#[cfg(feature = "serde-support")]
use crate::serde::Representation;
use crate::serde_bytes::{
tests::{check_deserialization, check_serialization},
Encoding,
};

fn prepare_cfrags() -> (
PublicKey,
Expand Down Expand Up @@ -453,7 +453,7 @@ mod tests {
let vcfrag = verified_cfrags[0].clone();
let cfrag = CapsuleFrag::from_array(&vcfrag.to_array()).unwrap();

check_serialization(&cfrag, Representation::Base64);
check_serialization(&cfrag, Encoding::Base64);
check_deserialization(&cfrag);
}
}
16 changes: 8 additions & 8 deletions umbral-pre/src/key_frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::traits::{
};

#[cfg(feature = "serde-support")]
use crate::serde::{serde_deserialize, serde_serialize, Representation};
use crate::serde_bytes::{deserialize_with_encoding, serialize_as_array, Encoding};

#[allow(clippy::upper_case_acronyms)]
type KeyFragIDSize = U32;
Expand Down Expand Up @@ -211,7 +211,7 @@ impl Serialize for KeyFrag {
where
S: Serializer,
{
serde_serialize(self, serializer, Representation::Base64)
serialize_as_array(self, serializer, Encoding::Base64)
}
}

Expand All @@ -222,7 +222,7 @@ impl<'de> Deserialize<'de> for KeyFrag {
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer, Representation::Base64)
deserialize_with_encoding(deserializer, Encoding::Base64)
}
}

Expand Down Expand Up @@ -511,10 +511,10 @@ mod tests {
use crate::{DeserializableFromArray, PublicKey, SecretKey, SerializableToArray, Signer};

#[cfg(feature = "serde-support")]
use crate::serde::tests::{check_deserialization, check_serialization};

#[cfg(feature = "serde-support")]
use crate::serde::Representation;
use crate::serde_bytes::{
tests::{check_deserialization, check_serialization},
Encoding,
};

fn prepare_kfrags(
sign_delegating_key: bool,
Expand Down Expand Up @@ -600,7 +600,7 @@ mod tests {
let vkfrag = verified_kfrags[0].clone();
let kfrag = KeyFrag::from_array(&vkfrag.to_array()).unwrap();

check_serialization(&kfrag, Representation::Base64);
check_serialization(&kfrag, Encoding::Base64);
check_deserialization(&kfrag);
}
}
21 changes: 11 additions & 10 deletions umbral-pre/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::traits::{
};

#[cfg(feature = "serde-support")]
use crate::serde::{serde_deserialize, serde_serialize, Representation};
use crate::serde_bytes::{deserialize_with_encoding, serialize_as_array, Encoding};

/// ECDSA signature object.
#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Serialize for Signature {
where
S: Serializer,
{
serde_serialize(self, serializer, Representation::Base64)
serialize_as_array(self, serializer, Encoding::Base64)
}
}

Expand All @@ -70,7 +70,7 @@ impl<'de> Deserialize<'de> for Signature {
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer, Representation::Base64)
deserialize_with_encoding(deserializer, Encoding::Base64)
}
}

Expand Down Expand Up @@ -263,7 +263,7 @@ impl Serialize for PublicKey {
where
S: Serializer,
{
serde_serialize(self, serializer, Representation::Hex)
serialize_as_array(self, serializer, Encoding::Hex)
}
}

Expand All @@ -274,7 +274,7 @@ impl<'de> Deserialize<'de> for PublicKey {
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer, Representation::Hex)
deserialize_with_encoding(deserializer, Encoding::Hex)
}
}

Expand Down Expand Up @@ -404,9 +404,10 @@ mod tests {
use crate::{DeserializableFromArray, SerializableToArray, SerializableToSecretArray};

#[cfg(feature = "serde-support")]
use crate::serde::tests::{check_deserialization, check_serialization};
#[cfg(feature = "serde-support")]
use crate::serde::Representation;
use crate::serde_bytes::{
tests::{check_deserialization, check_serialization},
Encoding,
};

#[test]
fn test_serialize_secret_key() {
Expand Down Expand Up @@ -466,10 +467,10 @@ mod tests {
let pk = signer.verifying_key();
let signature = signer.sign(message);

check_serialization(&pk, Representation::Hex);
check_serialization(&pk, Encoding::Hex);
check_deserialization(&pk);

check_serialization(&signature, Representation::Base64);
check_serialization(&signature, Encoding::Base64);
check_deserialization(&signature);
}
}
6 changes: 5 additions & 1 deletion umbral-pre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,14 @@ extern crate std;
extern crate alloc;

#[cfg(feature = "bench-internals")]
#[cfg_attr(docsrs, doc(cfg(feature = "bench-internals")))]
pub mod bench; // Re-export some internals for benchmarks.

#[cfg(feature = "bindings-python")]
#[cfg_attr(docsrs, doc(cfg(feature = "bindings-python")))]
pub mod bindings_python;
#[cfg(feature = "bindings-wasm")]
#[cfg_attr(docsrs, doc(cfg(feature = "bindings-wasm")))]
pub mod bindings_wasm;

mod capsule;
Expand All @@ -137,7 +140,8 @@ mod secret_box;
mod traits;

#[cfg(any(feature = "serde-support", feature = "bindings-wasm"))]
mod serde;
#[cfg_attr(docsrs, doc(cfg(feature = "serde-support")))]
pub mod serde_bytes;

pub use capsule::{Capsule, OpenReencryptedError};
pub use capsule_frag::{CapsuleFrag, CapsuleFragVerificationError, VerifiedCapsuleFrag};
Expand Down
Loading