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.
Many implementations do not work for the entire domain of inputs. There are additional optional tests defined which help ensure that all valid inputs produce valid results.
To run the bonus tests, remove the #[ignore] flag and execute the tests with
the big-primes feature, like this:
$ cargo test --features big-primesRefer to the exercism help page for Rust installation and learning resources.
Execute the tests with:
$ cargo testAll 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 -- --ignoredTo run a specific test, for example some_test, you can use:
$ cargo test some_testIf the specific test is ignored use:
$ cargo test some_test -- --ignoredTo 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 fmtTo see, if your solution contains some common ineffective use cases, inside the solution directory use
cargo clippy --all-targetsGenerally 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.