Skip to content

Commit db04f38

Browse files
authored
ed25519: optional zerocopy dependency (#1134)
Derives trait impls for `Signature` and `PublicKeyBytes`
1 parent 3138dd2 commit db04f38

4 files changed

Lines changed: 17 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ed25519/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pkcs8 = { version = "0.11.0-rc.8", optional = true }
2525
serde = { version = "1", optional = true, default-features = false }
2626
serde_bytes = { version = "0.11", optional = true, default-features = false }
2727
zeroize = { version = "1", optional = true, default-features = false }
28+
zerocopy = { version = "0.8", optional = true, features = ["derive"] }
2829

2930
[dev-dependencies]
3031
#ed25519-dalek = { version = "2", features = ["rand_core"] }

ed25519/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ use pkcs8::spki::{
296296
#[cfg(feature = "zeroize")]
297297
use zeroize::Zeroize;
298298

299+
#[cfg(feature = "zerocopy")]
300+
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};
301+
299302
/// Size of a single component of an Ed25519 signature.
300303
const COMPONENT_SIZE: usize = 32;
301304

@@ -314,6 +317,10 @@ pub type SignatureBytes = [u8; Signature::BYTE_SIZE];
314317
/// Signature verification libraries are expected to reject invalid field
315318
/// elements at the time a signature is verified.
316319
#[derive(Copy, Clone, Eq, PartialEq)]
320+
#[cfg_attr(
321+
feature = "zerocopy",
322+
derive(FromBytes, IntoBytes, Immutable, KnownLayout, Unaligned)
323+
)]
317324
#[repr(C)]
318325
pub struct Signature {
319326
R: ComponentBytes,

ed25519/src/pkcs8.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ use core::str;
3535
#[cfg(feature = "zeroize")]
3636
use zeroize::Zeroize;
3737

38+
#[cfg(feature = "zerocopy")]
39+
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};
40+
3841
/// Algorithm [`ObjectIdentifier`] for the Ed25519 digital signature algorithm
3942
/// (`id-Ed25519`).
4043
///
@@ -226,6 +229,11 @@ impl str::FromStr for KeypairBytes {
226229
/// Note that this type operates on raw bytes and performs no validation that
227230
/// public keys represent valid compressed Ed25519 y-coordinates.
228231
#[derive(Clone, Copy, Eq, PartialEq)]
232+
#[cfg_attr(
233+
feature = "zerocopy",
234+
derive(IntoBytes, FromBytes, Unaligned, KnownLayout, Immutable,)
235+
)]
236+
#[repr(transparent)]
229237
pub struct PublicKeyBytes(pub [u8; Self::BYTE_SIZE]);
230238

231239
impl PublicKeyBytes {

0 commit comments

Comments
 (0)