Fix README examples and doctests.

This commit is contained in:
Isis Lovecruft 2019-02-27 20:45:55 +00:00
parent 2ffd916dc7
commit 60f276db72
No known key found for this signature in database
GPG key ID: AB41313533E8E812
3 changed files with 23 additions and 55 deletions

View file

@ -33,33 +33,24 @@ First, Alice uses `EphemeralSecret::new()` and then
```rust
extern crate rand_os;
extern crate x25519_dalek;
use rand_os::OsRng;
extern crate x25519_dalek;
use x25519_dalek::EphemeralSecret;
use x25519_dalek::PublicKey;
# fn main() {
let mut alice_csprng = OsRng::new().unwrap();
let alice_secret = EphemeralSecret::new(&mut alice_csprng);
let alice_public = PublicKey::from(&alice_secret);
# }
```
Bob does the same:
```rust
# extern crate rand_os;
# use rand_os::OsRng;
#
# extern crate x25519_dalek;
# use x25519_dalek::EphemeralSecret;
# use x25519_dalek::PublicKey;
# fn main() {
let mut bob_csprng = OsRng::new().unwrap();
let bob_secret = EphemeralSecret::new(&mut bob_csprng);
let bob_public = PublicKey::from(&bob_secret);
# }
```
Alice meows across the room, telling `alice_public` to Bob, and Bob
@ -67,63 +58,19 @@ loudly meows `bob_public` back to Alice. Alice now computes her
shared secret with Bob by doing:
```rust
# extern crate rand_os;
# use rand_os::OsRng;
#
# extern crate x25519_dalek;
# use x25519_dalek::EphemeralSecret;
# use x25519_dalek::PublicKey;
#
# fn main() {
# let mut csprng = OsRng::new().unwrap();
# let alice_secret = EphemeralSecret::new(&mut csprng);
# let alice_public = PublicKey::from(&alice_secret);
# let bob_secret = EphemeralSecret::new(&mut csprng);
# let bob_public = PublicKey::from(&bob_secret);
let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
# }
```
Similarly, Bob computes a shared secret by doing:
```rust
# extern crate rand_os;
# use rand_os::OsRng;
#
# extern crate x25519_dalek;
# use x25519_dalek::EphemeralSecret;
# use x25519_dalek::PublicKey;
#
# fn main() {
# let mut csprng = OsRng::new().unwrap();
# let alice_secret = EphemeralSecret::new(&mut csprng);
# let alice_public = PublicKey::from(&alice_secret);
# let bob_secret = EphemeralSecret::new(&mut csprng);
# let bob_public = PublicKey::from(&bob_secret);
let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);
# }
```
These secrets are the same:
```rust
# extern crate rand_os;
# use rand_os::OsRng;
#
# extern crate x25519_dalek;
# use x25519_dalek::EphemeralSecret;
# use x25519_dalek::PublicKey;
#
# fn main() {
# let mut csprng = OsRng::new().unwrap();
# let alice_secret = EphemeralSecret::new(&mut csprng);
# let alice_public = PublicKey::from(&alice_secret);
# let bob_secret = EphemeralSecret::new(&mut csprng);
# 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!(alice_shared_secret.as_bytes(), bob_shared_secret.as_bytes());
# }
```
Voilá! Alice and Bob can now use their shared secret to encrypt their

View file

@ -30,6 +30,9 @@ extern crate curve25519_dalek;
extern crate rand_core;
#[cfg(test)]
extern crate rand_os;
mod x25519;
pub use x25519::*;

View file

@ -191,6 +191,24 @@ pub const X25519_BASEPOINT_BYTES: [u8; 32] = [
mod test {
use super::*;
use rand_os::OsRng;
// This was previously a doctest but it got moved to the README to
// avoid duplication where it then wasn't being run, so now it
// lives here.
#[test]
fn alice_and_bob() {
let mut csprng = OsRng::new().unwrap();
let alice_secret = EphemeralSecret::new(&mut csprng);
let alice_public = PublicKey::from(&alice_secret);
let bob_secret = EphemeralSecret::new(&mut csprng);
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!(alice_shared_secret.as_bytes(), bob_shared_secret.as_bytes());
}
#[test]
fn byte_basepoint_matches_edwards_scalar_mul() {
let mut scalar_bytes = [0x37; 32];