Published at Mar 31 2020
·
0 comments

Instructions

Test suite

Solution

Diffie-Hellman key exchange.

Alice and Bob use Diffie-Hellman key exchange to share secrets. They start with prime numbers, pick private keys, generate and share public keys, and then generate a shared secret key.

The test program supplies prime numbers p and g.

Alice picks a private key, a, greater than 1 and less than p. Bob does the same to pick a private key b.

Alice calculates a public key A.

```
A = g**a mod p
```

Using the same p and g, Bob similarly calculates a public key B from his private key b.

Alice and Bob exchange public keys. Alice calculates secret key s.

```
s = B**a mod p
```

Bob calculates

```
s = A**b mod p
```

The calculations produce the same result! Alice and Bob now share secret s.

One possible solution for this exercise is to implement your own modular exponentiation function. To learn more about it refer to the following page.

Refer to the exercism help page for Rust installation and learning resources.

Execute the tests with:

```
$ cargo test
```

All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests`

directory
and remove the `#[ignore]`

flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]`

flag above it.
Continue, until you pass every test.

If you wish to run all ignored tests without editing the tests source file, use:

```
$ cargo test -- --ignored
```

To run a specific test, for example `some_test`

, you can use:

```
$ cargo test some_test
```

If the specific test is ignored use:

```
$ cargo test some_test -- --ignored
```

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

```
cargo fmt
```

To see, if your solution contains some common ineffective use cases, inside the solution directory use

```
cargo clippy --all-targets
```

Generally you should submit all files in which you implemented your solution (`src/lib.rs`

in most cases). If you are using any external crates, please consider submitting the `Cargo.toml`

file. This will make the review process faster and clearer.

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.

Wikipedia, 1024 bit key from www.cryptopp.com/wiki. http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
use diffie_hellman::*;
#[test]
fn test_private_key_in_range_key() {
let primes: Vec<u64> = vec![
5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 773, 967, 3461, 6131,
];
let private_keys: Vec<u64> = primes.iter().map(|x| private_key(*x)).collect();
for i in 0..primes.len() {
assert!(1 < private_keys[i] && private_keys[i] < primes[i]);
}
}
#[test]
#[ignore]
fn test_public_key_correct() {
let p: u64 = 23;
let g: u64 = 5;
let private_key: u64 = 6;
let expected: u64 = 8;
assert_eq!(public_key(p, g, private_key), expected);
}
#[test]
#[ignore]
fn test_secret_key_correct() {
let p: u64 = 11;
let private_key_a = 7;
let public_key_b = 8;
let secret = secret(p, public_key_b, private_key_a);
let expected = 2;
assert_eq!(secret, expected);
}
#[test]
#[ignore]
fn test_public_key_correct_big_numbers() {
let p: u64 = 4_294_967_299;
let g: u64 = 8;
let private_key: u64 = 4_294_967_296;
let expected: u64 = 4096;
assert_eq!(public_key(p, g, private_key), expected);
}
#[test]
#[ignore]
fn test_secret_key_correct_big_numbers() {
let p: u64 = 4_294_967_927;
let private_key_a = 4_294_967_300;
let public_key_b = 843;
let secret = secret(p, public_key_b, private_key_a);
let expected = 1_389_354_282;
assert_eq!(secret, expected);
}
#[test]
#[ignore]
fn test_changed_secret_key() {
let p: u64 = 13;
let g: u64 = 11;
let private_key_a = private_key(p);
let private_key_b = private_key(p);
let public_key_a = public_key(p, g, private_key_a);
let public_key_b = public_key(p, g, private_key_b);
// Key exchange
let secret_a = secret(p, public_key_b, private_key_a);
let secret_b = secret(p, public_key_a, private_key_b);
assert_eq!(secret_a, secret_b);
}
```

```
use rand::Rng;
pub fn private_key(p: u64) -> u64 {
rand::thread_rng().gen_range(2, p)
}
pub fn public_key(p: u64, g: u64, a: u64) -> u64 {
mod_exp(g, a, p)
}
pub fn secret(p: u64, b_pub: u64, a: u64) -> u64 {
mod_exp(b_pub, a, p)
}
fn mod_exp(mut base: u64, mut exp: u64, modulus: u64) -> u64 {
if modulus == 1 {
return 0;
}
let mut result = 1;
base = base % modulus;
while exp > 0 {
if exp % 2 == 1 {
result = result * base % modulus;
}
exp = exp >> 1;
base = base * base % modulus
}
result
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.