Skip to content

Instantly share code, notes, and snippets.

@stefanooldeman
Created December 14, 2011 08:44
Show Gist options
  • Save stefanooldeman/1475772 to your computer and use it in GitHub Desktop.
Save stefanooldeman/1475772 to your computer and use it in GitHub Desktop.
Diffie-Hellman with real world numbers in Haskell..

Here we start in the terminal with ghci (interactive Haskell interperter / ghc) First we create P a nice prime number, and 3 a prime root and two input secrets (8bit) Then start doing the Diffie-Hellman key exchange

Because the lazy evaluation in Haskell defining a variable p is only calculated when needed. And as shown when defining keyA everything is OK. but when called the calculations take too long and are terminated..

Prelude> let p = 2^56 - 46
Prelude> let v = 3
Prelude> let secretA = 19881104
Prelude> let secretB = 54296177

Prelude> let cryptA = v^secretA `mod` p
Prelude> cryptA
6313095572897871
Prelude> let cryptB = v^secretB `mod` p
Prelude> cryptB
70623318720639969

Prelude> let keyA = cryptB^secretA `mod` p
Prelude> keyA
Segmentation fault
$
$ ghci
Prelude> let keyB = cryptA^secretB `mod` p
Prelude> keyB
Segmentation fault
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment