Impl From<StaticSecret> for [u8; 32]

Plus derive(Copy), proptest bytes roundtrip
This commit is contained in:
Deirdre Connolly 2020-05-11 23:36:51 -04:00 committed by Deirdre Connolly
parent 06fddf3fb1
commit 18e12e7406
1 changed files with 16 additions and 1 deletions

View File

@ -128,10 +128,16 @@ impl From<SharedSecret> for [u8; 32] {
/// A Diffie-Hellman secret key used to derive a shared secret when
/// combined with a public key, that can be stored and loaded.
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Copy, Eq, PartialEq, Deserialize, Serialize)]
#[cfg_attr(test, derive(Debug))]
pub struct StaticSecret(pub(crate) Scalar);
impl From<StaticSecret> for [u8; 32] {
fn from(static_secret: StaticSecret) -> [u8; 32] {
static_secret.0.to_bytes()
}
}
impl From<[u8; 32]> for StaticSecret {
fn from(bytes: [u8; 32]) -> StaticSecret {
match Scalar::from_canonical_bytes(bytes) {
@ -264,6 +270,15 @@ mod tests {
);
}
#[test]
fn from_into_static_secret_bytes(static_secret in any::<StaticSecret>()) {
let bytes: [u8; 32] = static_secret.into();
prop_assert_eq!(
static_secret, StaticSecret::from(bytes)
);
}
#[test]
fn scalar_mul_different_paths(
secret in any::<EphemeralSecret>(),