-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implement exercise diffie-hellman #756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c256a33
Implement exercise diffie-hellman
bd97b39
Add diffie-hellman exercise
ce3d511
Fix flake8 errors
d115165
Add to config.json
8a0c198
Add comma
86eb852
Remove number whitespace
ea45584
Add pseudo-random note
64f526d
Fix note and typos
26161f7
Merge branch 'master' into diffie-hellman
ilya-khadykin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Diffie Hellman | ||
|
||
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. | ||
|
||
## Step 0 | ||
|
||
The test program supplies prime numbers p and g. | ||
|
||
## Step 1 | ||
|
||
Alice picks a private key, a, greater than 1 and less than p. Bob does | ||
the same to pick a private key b. | ||
|
||
## Step 2 | ||
|
||
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. | ||
|
||
## Step 3 | ||
|
||
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. | ||
|
||
## Notes | ||
|
||
Python, as of version 3.6, includes two different random modules. The module called `random` is pseudo-random, meaning it does not generate true randomness, but follows and algorithm that simulates randomness. Since random numbers are generated through a known algorithm, they are not truly random. The `random` module is not correctly suited for cryptography and should not be used, because it is pseudo-random. In version 3.6, Python introduced the `secrets` module, which generates cryptographically strong random numbers that provide the greater security required for cryptography. Since this is only an exercise, `random` is fine to use, but note that it would be very insecure if actually used for cryptography. | ||
|
||
### Submitting Exercises | ||
|
||
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory. | ||
|
||
For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/python/bob/bob.py`. | ||
|
||
|
||
For more detailed information about running tests, code style and linting, | ||
please see the [help page](http://exercism.io/languages/python). | ||
## Source | ||
|
||
Wikipedia, 1024 bit key from www.cryptopp.com/wiki. [http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange](http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange) | ||
|
||
## Submitting Incomplete Solutions | ||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def private_key(p): | ||
pass | ||
|
||
|
||
def public_key(p, g, private): | ||
pass | ||
|
||
|
||
def secret(p, public, private): | ||
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import unittest | ||
|
||
import diffie_hellman | ||
|
||
|
||
class DiffieHellmanTest(unittest.TestCase): | ||
|
||
def test_private_in_range(self): | ||
primes = [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] | ||
for i in primes: | ||
self.assertTrue(1 < diffie_hellman.private_key(i) < i) | ||
|
||
# Can fail due to randomness, but most likely will not, | ||
# due to pseudo-randomness and the large number chosen | ||
def test_private_key_randomness(self): | ||
p = 2147483647 | ||
private_keys = [] | ||
for i in range(5): | ||
private_keys.append(diffie_hellman.private_key(p)) | ||
self.assertEqual(len(list(set(private_keys))), len(private_keys)) | ||
|
||
def test_public_key_correct(self): | ||
p = 23 | ||
g = 5 | ||
private = 6 | ||
expected = 8 | ||
|
||
actual = diffie_hellman.public_key(p, g, private) | ||
self.assertEqual(actual, expected) | ||
|
||
def test_secret_key_correct(self): | ||
p = 23 | ||
public = 19 | ||
private = 6 | ||
expected = 2 | ||
|
||
actual = diffie_hellman.secret(p, public, private) | ||
self.assertEqual(actual, expected) | ||
|
||
def test_secret_key_correct_large_nums(self): | ||
p = int("""120227323036150778550155526710966921740030662\ | ||
69457894729842354923526575959371158734103742634711454153\ | ||
30066288563005527069961435922404533456428692335628867529\ | ||
30249953227657883929905072620233073626594386072962776144\ | ||
69143365881426187411323246174903542571280506720291038940\ | ||
7991986070558964461330091797026762932543""".replace( | ||
"\n", "").replace(" ", "")) | ||
public = int("""7520544115435791944292554616920871123548\ | ||
58559049691782063133092992058683123990461493675163366079\ | ||
66149689640419216591714331722664409474612463910928128055\ | ||
99415792293044373353565984826436410603792531597409532111\ | ||
27577117569121441377056137760635413505489115127155125391\ | ||
86192176020596861210448363099541947258202188""".replace( | ||
"\n", "").replace(" ", "")) | ||
private = int("""248347939362593293991108130435688850515\ | ||
37971354473275017926961991904690152151776307586179022004\ | ||
17377685436170904594686456961202706692908603181062371925\ | ||
882""".replace("\n", "").replace(" ", "")) | ||
expected = int("""70900735223964890815905879227737819348\ | ||
80851869892044649134650898046120174656773533145582564442\ | ||
98779465564310958207858354973848497783442169812282262526\ | ||
39932672153547963980483673419756271345828771971984887453\ | ||
01448857224581986445413661898091472983952358126388674082\ | ||
1363010486083940557620831348661126601106717071""".replace( | ||
"\n", "").replace(" ", "")) | ||
|
||
actual = diffie_hellman.secret(p, public, private) | ||
self.assertEqual(actual, expected) | ||
|
||
def test_exchange(self): | ||
p = 23 | ||
g = 5 | ||
|
||
privateA = diffie_hellman.private_key(p) | ||
privateB = diffie_hellman.private_key(p) | ||
|
||
publicA = diffie_hellman.public_key(p, g, privateA) | ||
publicB = diffie_hellman.public_key(p, g, privateB) | ||
|
||
secretA = diffie_hellman.secret(p, publicB, privateA) | ||
secretB = diffie_hellman.secret(p, publicA, privateB) | ||
|
||
self.assertEqual(secretA, secretB) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import random | ||
|
||
|
||
def private_key(p): | ||
return random.randint(2, p-1) | ||
|
||
|
||
def public_key(p, g, a): | ||
return pow(g, a, p) | ||
|
||
|
||
def secret(p, B, a): | ||
return pow(B, a, p) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this should use
secrets
rather thanrandom
, since the exercise is a cryptographic one. Perhaps we should also have a note in README.md that solutions should avoid usingrandom
since it's not cryptographically secure?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did look at that, but according to the PEP it was introduced in version 3.6, so it isn't compatible with Python 2 or most Python 3 versions on stable distros. It seems that exercism wants cross compatibility which is why I chose to use the default random instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good points!
These are just training exercises, so
random
is good enough I think.But it would be a really good idea to mention
secrets
inHINTS.md
for such exercises.@N-Parsons, could you please create an issue for it to not forget?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a note to the readme about it and pseudo randomness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, @kusti8, I hadn't realised that
secrets
was only in Python 3.6.@m-a-ge, I'll create an issue for it now :)