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

elliptic-curve: add alloc feature + EncodedPoint::to_bytes() #265

Merged
merged 1 commit into from
Aug 21, 2020
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
3 changes: 2 additions & 1 deletion elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ hex-literal = "0.2"

[features]
default = ["rand"]
alloc = []
ecdh = ["rand", "weierstrass", "zeroize"]
rand = ["rand_core"]
weierstrass = []
std = []
std = ["alloc"]

[package.metadata.docs.rs]
all-features = true
Expand Down
4 changes: 2 additions & 2 deletions elliptic-curve/src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//! # Usage
//!
//! Have each participant generate an [`EphemeralSecret`] value, compute the
//! [`EncodedPoint`] for that value, exchange public keys, then each participant
//! uses their [`EphemeralSecret`] and the other participant's [`EncodedPoint`]
//! [`PublicKey'] for that value, exchange public keys, then each participant
//! uses their [`EphemeralSecret`] and the other participant's [`PublicKey`]
//! to compute a [`SharedSecret`] value.
//!
//! # ⚠️ SECURITY WARNING ⚠️
Expand Down
3 changes: 3 additions & 0 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
html_root_url = "https://docs.rs/elliptic-curve/0.5.0"
)]

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

Expand Down
18 changes: 17 additions & 1 deletion elliptic-curve/src/sec1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use generic_array::{
};
use subtle::CtOption;

#[cfg(feature = "alloc")]
use alloc::boxed::Box;

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

Expand Down Expand Up @@ -120,11 +123,17 @@ where
}

/// Get byte slice containing the serialized [`EncodedPoint`].
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..self.len()]
}

/// Get boxed byte slice containing the serialized [`EncodedPoint`]
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn to_bytes(&self) -> Box<[u8]> {
self.as_bytes().to_vec().into_boxed_slice()
}

/// Compress this [`EncodedPoint`], returning a new [`EncodedPoint`].
pub fn compress(&self) -> Self {
if self.tag().is_compressed() {
Expand Down Expand Up @@ -448,4 +457,11 @@ mod tests {
let compressed_point = uncompressed_point.compress();
assert_eq!(compressed_point.as_bytes(), &COMPRESSED_BYTES[..]);
}

#[cfg(feature = "alloc")]
#[test]
fn to_bytes() {
let uncompressed_point = EncodedPoint::from_bytes(&UNCOMPRESSED_BYTES[..]).unwrap();
assert_eq!(&*uncompressed_point.to_bytes(), &UNCOMPRESSED_BYTES[..]);
}
}