Skip to content

Commit

Permalink
Use ASN.1 DER instead of MessagePack
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Jan 15, 2023
1 parent 5080d6d commit 9a631ba
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- `DefaultSerialize`/`DefaultDeserialize` for Umbral objects (`Caspule`, `(Verified)CapsuleFrag`, `(Verified)KeyFrag`) which uses MessagePack via `serde` (gated by `default-serialization` feature). This is what the serialization methods in the bindings use. ([#110])
- Use ASN.1 DER instead of MessagePack in the traits above. ([#111])
- `to_compressed_bytes()`/`try_from_compressed_bytes()` for `PublicKey` to give access to non-serde representation (just 33 bytes). These are exposed in the bindings in lieu of the "default" methods (e.g. `__bytes__()` or `toBytes()`). ([#110])
- `to_der_bytes()`/`try_from_der_bytes()` for `Signature` to give access to non-serde representation. These are exposed in the bindings in lieu of the "default" methods (e.g. `__bytes__()` or `toBytes()`). ([#110])
- `SecretKeyFactory::make_secret()` to provide a more straightforward replacement of `SecretKeyFactory::make_secret_key()`->`SecretKey::to_secret_bytes()`. ([#110])
Expand All @@ -30,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[#105]: https://github.com/nucypher/rust-umbral/pull/105
[#110]: https://github.com/nucypher/rust-umbral/pull/110
[#111]: https://github.com/nucypher/rust-umbral/pull/111


## [0.7.0] - 2022-09-30
Expand Down
6 changes: 3 additions & 3 deletions umbral-pre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ typenum = "1.13" # typenum is a 2018-edition crate starting from 1.13
getrandom = { version = "0.2", optional = true, default-features = false, features = ["js"] }
subtle = { version = "2.4", default-features = false }
zeroize = { version = "1.5", default-features = false, features = ["derive"] }
rmp-serde = { version = "0.15", optional = true }
serde_asn1_der = { version = "0.7", optional = true }

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
serde_json = "1"
rmp-serde = "0.15"
serde_asn1_der = "0.7"

[features]
default = ["default-rng"]
bench-internals = ["default-rng"]
bindings-python = ["pyo3", "std", "derive_more", "default-serialization"]
bindings-wasm = ["js-sys", "default-serialization", "wasm-bindgen", "derive_more", "wasm-bindgen-derive"]
default-rng = ["getrandom", "rand_core/getrandom"]
default-serialization = ["serde-support", "rmp-serde"]
default-serialization = ["serde-support", "serde_asn1_der"]
serde-support = ["serde"]
std = []

Expand Down
4 changes: 2 additions & 2 deletions umbral-pre/src/capsule_frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ mod tests {
let cfrag = verified_cfrags[0].clone().unverify();

// Check that the cfrag serializes to the same thing as the verified cfrag
let cfrag_bytes = rmp_serde::to_vec(&cfrag).unwrap();
let vcfrag_bytes = rmp_serde::to_vec(&verified_cfrags[0]).unwrap();
let cfrag_bytes = serde_asn1_der::to_vec(&cfrag).unwrap();
let vcfrag_bytes = serde_asn1_der::to_vec(&verified_cfrags[0]).unwrap();
assert_eq!(vcfrag_bytes, cfrag_bytes);

check_serialization_roundtrip(&cfrag);
Expand Down
4 changes: 2 additions & 2 deletions umbral-pre/src/key_frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ mod tests {
let kfrag = verified_kfrags[0].clone().unverify();

// Check that the kfrag serializes to the same thing as the verified kfrag
let kfrag_bytes = rmp_serde::to_vec(&kfrag).unwrap();
let vkfrag_bytes = rmp_serde::to_vec(&verified_kfrags[0]).unwrap();
let kfrag_bytes = serde_asn1_der::to_vec(&kfrag).unwrap();
let vkfrag_bytes = serde_asn1_der::to_vec(&verified_kfrags[0]).unwrap();
assert_eq!(vkfrag_bytes, kfrag_bytes);

check_serialization_roundtrip(&kfrag);
Expand Down
6 changes: 3 additions & 3 deletions umbral-pre/src/serde_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ pub(crate) mod tests {
let deserialized: T = serde_json::from_str(&serialized).unwrap();
assert_eq!(obj, &deserialized);

// Check serialization to MessagePack (binary)
// Check serialization to ASN.1 DER (binary)

let serialized = rmp_serde::to_vec(obj).unwrap();
let deserialized: T = rmp_serde::from_slice(&serialized).unwrap();
let serialized = serde_asn1_der::to_vec(obj).unwrap();
let deserialized: T = serde_asn1_der::from_bytes(&serialized).unwrap();
assert_eq!(obj, &deserialized);
}
}
12 changes: 6 additions & 6 deletions umbral-pre/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ pub(crate) fn fmt_public(
}

/// Default serialization of an object that is used in all the bindings.
/// Uses MessagePack format.
/// Uses ASN.1 DER format.
#[cfg(feature = "default-serialization")]
pub trait DefaultSerialize: Serialize {
/// Serializes this object.
fn to_bytes(&self) -> Result<Box<[u8]>, rmp_serde::encode::Error> {
rmp_serde::to_vec(self).map(|v| v.into_boxed_slice())
fn to_bytes(&self) -> Result<Box<[u8]>, serde_asn1_der::SerdeAsn1DerError> {
serde_asn1_der::to_vec(self).map(|v| v.into_boxed_slice())
}
}

/// Default deserialization of an object that is used in all the bindings.
/// Uses MessagePack format.
/// Uses ASN.1 DER format.
#[cfg(feature = "default-serialization")]
pub trait DefaultDeserialize<'de>: Deserialize<'de> {
/// Deserializes a bytestring into this object.
fn from_bytes(bytes: &'de [u8]) -> Result<Self, rmp_serde::decode::Error> {
rmp_serde::from_slice(bytes)
fn from_bytes(bytes: &'de [u8]) -> Result<Self, serde_asn1_der::SerdeAsn1DerError> {
serde_asn1_der::from_bytes(bytes)
}
}

0 comments on commit 9a631ba

Please sign in to comment.