Skip to content

Commit

Permalink
Add diffie-hellman exercise (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Dec 11, 2024
1 parent 1ba27ba commit 75f2d7b
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 37 additions & 0 deletions exercises/practice/diffie-hellman/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions exercises/practice/diffie-hellman/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
4 changes: 4 additions & 0 deletions exercises/practice/diffie-hellman/.meta/example.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PrivateKey ← ⍜(-2|⌊×)⊙⚂
PowMod ← ◿⊙(ⁿ:)
PublicKey ← PowMod
Secret ← PowMod
28 changes: 28 additions & 0 deletions exercises/practice/diffie-hellman/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions exercises/practice/diffie-hellman/diffie-hellman.ua
Original file line number Diff line number Diff line change
@@ -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
Secret ← |3 ⊙⋅⋅(⍤ "Please implement Secret" 0)
38 changes: 38 additions & 0 deletions exercises/practice/diffie-hellman/tests.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
~ "diffie-hellman.ua" ~ PrivateKey PublicKey Secret

# Private key is greater than 1 and less than p
P ← 7919
⍤⤙≍ 1 /××⊃(≥2|<P) [⍥(PrivateKey P) 1000]

# Private key is random
P ← 7919
⍤⤙≍ 1 >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

0 comments on commit 75f2d7b

Please sign in to comment.