Skip to content

Commit

Permalink
Add diffie-hellman exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Dec 11, 2024
1 parent 822a531 commit 03f194b
Show file tree
Hide file tree
Showing 7 changed files with 144 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"
}
3 changes: 3 additions & 0 deletions exercises/practice/diffie-hellman/.meta/example.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PrivateKey ← ⍜(-2|⌊×)⊙⚂
PublicKey ← |3 ⊙⋅⋅(⍤ "Please implement PublicKey" 0)
Secret ← |3 ⊙⋅⋅(⍤ "Please implement PublicKey" 0)
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 = failwith "You need to implement this function."
Secret ← |3 ⊙⋅⋅(⍤ "Please implement PublicKey" 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 03f194b

Please sign in to comment.