diff --git a/config.json b/config.json index 64e6309..9da91f6 100644 --- a/config.json +++ b/config.json @@ -450,6 +450,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "diffie-hellman", + "name": "Diffie-Hellman", + "uuid": "b11e4b1a-09f7-4f10-83da-46cde3aed5b8", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "etl", "name": "ETL", diff --git a/exercises/practice/diffie-hellman/.docs/instructions.md b/exercises/practice/diffie-hellman/.docs/instructions.md new file mode 100644 index 0000000..9f1c85e --- /dev/null +++ b/exercises/practice/diffie-hellman/.docs/instructions.md @@ -0,0 +1,37 @@ +# Instructions + +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ᵃ 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ᵃ mod p + +Bob calculates + + s = Aᵇ mod p + +The calculations produce the same result! +Alice and Bob now share secret s. diff --git a/exercises/practice/diffie-hellman/.meta/config.json b/exercises/practice/diffie-hellman/.meta/config.json new file mode 100644 index 0000000..168ed3c --- /dev/null +++ b/exercises/practice/diffie-hellman/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "diffie-hellman.ua" + ], + "test": [ + "tests.ua" + ], + "example": [ + ".meta/example.ua" + ] + }, + "blurb": "Diffie-Hellman key exchange.", + "source": "Wikipedia, 1024 bit key from www.cryptopp.com/wiki.", + "source_url": "https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange" +} diff --git a/exercises/practice/diffie-hellman/.meta/example.ua b/exercises/practice/diffie-hellman/.meta/example.ua new file mode 100644 index 0000000..faad5f4 --- /dev/null +++ b/exercises/practice/diffie-hellman/.meta/example.ua @@ -0,0 +1,3 @@ +PrivateKey ← ⍜(-2|⌊×)⊙⚂ +PublicKey ← |3 ⊙⋅⋅(⍤ "Please implement PublicKey" 0) +Secret ← |3 ⊙⋅⋅(⍤ "Please implement PublicKey" 0) diff --git a/exercises/practice/diffie-hellman/.meta/tests.toml b/exercises/practice/diffie-hellman/.meta/tests.toml new file mode 100644 index 0000000..a56c97f --- /dev/null +++ b/exercises/practice/diffie-hellman/.meta/tests.toml @@ -0,0 +1,28 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[1b97bf38-4307-418e-bfd2-446ffc77588d] +description = "private key is greater than 1 and less than p" + +[68b2a5f7-7755-44c3-97b2-d28d21f014a9] +description = "private key is random" + +[b4161d8e-53a1-4241-ae8f-48cc86527f22] +description = "can calculate public key using private key" + +[0d25f8d7-4897-4338-a033-2d3d7a9af688] +description = "can calculate public key when given a different private key" + +[cd02ad45-3f52-4510-99cc-5161dad948a8] +description = "can calculate secret using other party's public key" + +[17f13c61-a111-4075-9a1f-c2d4636dfa60] +description = "key exchange" diff --git a/exercises/practice/diffie-hellman/diffie-hellman.ua b/exercises/practice/diffie-hellman/diffie-hellman.ua new file mode 100644 index 0000000..3c664b4 --- /dev/null +++ b/exercises/practice/diffie-hellman/diffie-hellman.ua @@ -0,0 +1,11 @@ +# Generate a private key +# Key ? PrimeP +PrivateKey ← |1 ⊙(⍤"Please implement PrivateKey" 0) + +# Calculate the public key +# PublicKey ? primeP primeG privateKey +PublicKey ← |3 ⊙⋅⋅(⍤ "Please implement PublicKey" 0) + +# Calculate the secret +# Secret ? PrimeP PublicKey PrivateKey = failwith "You need to implement this function." +Secret ← |3 ⊙⋅⋅(⍤ "Please implement PublicKey" 0) diff --git a/exercises/practice/diffie-hellman/tests.ua b/exercises/practice/diffie-hellman/tests.ua new file mode 100644 index 0000000..9e7bf3a --- /dev/null +++ b/exercises/practice/diffie-hellman/tests.ua @@ -0,0 +1,38 @@ +~ "diffie-hellman.ua" ~ PrivateKey PublicKey Secret + +# Private key is greater than 1 and less than p +P ← 7919 +⍤⤙≍ 1 /××⊃(≥2|100 ⧻◴ [⍥(PrivateKey P) 1000] + +# # Can calculate public key using private key +# p ← 23 +# g ← 5 +# privateKey ← 6 +# ⍤⤙≍ 8 publicKey p g privateKey + +# # Can calculate public key when given a different private key +# p ← 23 +# g ← 5 +# privateKey ← 15 +# ⍤⤙≍ 19 publicKey p g privateKey + +# # Can calculate secret using other party's public key +# p ← 23 +# theirPublicKey ← 19 +# myPrivateKey ← 6 +# ⍤⤙≍ 2 secret p theirPublicKey myPrivateKey + +# # Key exchange +# p ← 23 +# g ← 5 +# alicePrivateKey ← privateKey p +# alicePublicKey ← publicKey p g alicePrivateKey +# bobPrivateKey ← privateKey p +# bobPublicKey ← publicKey p g bobPrivateKey +# secretA ← secret p bobPublicKey alicePrivateKey +# secretB ← secret p alicePublicKey bobPrivateKey +# ⍤⤙≍ secretB secretA