Tidy and add tests

This commit is contained in:
Deirdre Connolly 2020-05-08 00:31:23 -04:00 committed by Deirdre Connolly
parent e331030fc7
commit e25d608b04
2 changed files with 44 additions and 25 deletions

View File

@ -12,6 +12,5 @@ zeroize = "1.1.0"
[dev-dependencies]
bincode = "1.2.1"
rand = "0.7"
proptest = "0.9"
proptest-derive = "0.1.0"

View File

@ -9,7 +9,7 @@
//! ## Example
//!
//! ```
//! use rand::rngs::OsRng;
//! use rand_core::OsRng;
//!
//! use ristretto255_dh::EphemeralSecret;
//! use ristretto255_dh::PublicKey;
@ -35,15 +35,6 @@
//! );
//! ```
//!
//! # Installation
//!
//! To install, add the following to your project's `Cargo.toml`:
//!
//! ```toml
//! [dependencies.ristretto255-dh]
//! version = "0.1.0"
//! ```
//!
//! ## About
//!
//! The high-level Diffie-Hellman API is inspired by [x25519-dalek].
@ -212,8 +203,28 @@ impl Arbitrary for StaticSecret {
#[cfg(test)]
mod tests {
use bincode;
use rand_core::OsRng;
use super::*;
#[test]
fn random_dh() {
let alice_secret = EphemeralSecret::new(&mut OsRng);
let alice_public = PublicKey::from(&alice_secret);
let bob_secret = EphemeralSecret::new(&mut OsRng);
let bob_public = PublicKey::from(&bob_secret);
let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);
assert_eq!(
<[u8; 32]>::from(alice_shared_secret),
<[u8; 32]>::from(bob_shared_secret)
);
}
proptest! {
#[test]
@ -221,19 +232,12 @@ mod tests {
alice_secret in any::<EphemeralSecret>(),
bob_secret in any::<EphemeralSecret>()
) {
// Alice's side
let alice_public = PublicKey::from(&alice_secret);
// Bob's side
let bob_public = PublicKey::from(&bob_secret);
// Alice again
let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
// Bob again
let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);
// Each peer's computed shared secret should be the same.
prop_assert_eq!(
<[u8; 32]>::from(alice_shared_secret),
<[u8; 32]>::from(bob_shared_secret)
@ -245,24 +249,40 @@ mod tests {
alice_secret in any::<StaticSecret>(),
bob_secret in any::<StaticSecret>()
) {
// Alice's side
let alice_public = PublicKey::from(&alice_secret);
// Bob's side
let bob_public = PublicKey::from(&bob_secret);
// Alice again
let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
// Bob again
let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);
// Each peer's computed shared secret should be the same.
prop_assert_eq!(
<[u8; 32]>::from(alice_shared_secret),
<[u8; 32]>::from(bob_shared_secret)
);
}
#[test]
fn serde_pubkey(alice_secret in any::<EphemeralSecret>()) {
let alice_public = PublicKey::from(&alice_secret);
let serialized = bincode::serialize(&alice_public).unwrap();
prop_assert_eq!(
alice_public, bincode::deserialize(&serialized[..]).unwrap()
);
}
#[test]
fn serde_static_key(alice_secret in any::<StaticSecret>()) {
let serialized = bincode::serialize(&alice_secret).unwrap();
prop_assert_eq!(
alice_secret, bincode::deserialize(&serialized[..]).unwrap()
);
}
}
}