Bitcoin Forum

Bitcoin => Bitcoin Discussion => Topic started by: mndrix on April 12, 2013, 06:27:25 PM



Title: Elliptic curve Diffie-Hellman example
Post by: mndrix on April 12, 2013, 06:27:25 PM
Diffie-Hellman key agreement is really easy using elliptic curves.  Maybe because of that, I was unable to find a good, working example with which to experiment, so I wrote one: 32 lines of Go (https://gist.github.com/mndrix/5373519).  It should be equally short for any other language that has a decent EC math library.

Here's a sample run:

$ go run ecc.go
Alice: c84fd9a9c8538375d86d4fb03f6651ed62431d236584d07d45048432
       18424947940967940791172868542123538214411876209787668504243298468721
Bob: a2101edbea799523a0ab0cf0281e32cc39fdca05a7b6cb8c9e534f6c
     25522202360313088829965757364820961191890334919586259842035197968775

Alice: 21477883440431114094502151543289380813227450324823096547884771069269
Bob:   21477883440431114094502151543289380813227450324823096547884771069269


The first 4 lines show Alice and Bob's private and public keys, respectively.  The final 2 lines show that they each calculate the same shared secret.  The example uses curve P-224 instead of secp256k1 since the former is part of Go's library.