dcrypt.ecc.curve25519

Members

Functions

clamp
void clamp(ubyte[] sk)

Transforms 32 random bytes into a valid secret key.

curve25519_scalarmult
ubyte[32] curve25519_scalarmult(in ref ubyte[32] secret, in ref ubyte[32] p = publicBasePoint)
curve25519_scalarmult
ubyte[32] curve25519_scalarmult(in ubyte[] secret, in ubyte[] p = cast(const ubyte[32])publicBasePoint)

Variables

publicBasePoint
enum ubyte[32] publicBasePoint;

The default public base point.

Examples

Implementation of Curve25519.

Generate a public key from a secret. Test vectors from http://cr.yp.to/highspeed/naclcrypto-20090310.pdf

1 t {
2 	alias ubyte[32] key_t;
3 	
4 	key_t secretKey = cast(key_t) x"77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a";
5 	
6 	key_t publicKey = curve25519_scalarmult(secretKey);
7 	
8 	auto expectedPublic = x"8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a";
9 	
10 	assert(publicKey == expectedPublic, "curve25519 public key generation failed!"

Generate a public key from a secret. Test vectors from http://cr.yp.to/highspeed/naclcrypto-20090310.pdf

1 t {
2 	alias ubyte[32] key_t;
3 	
4 	key_t secretKey = cast(key_t) x"5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb";
5 	
6 	key_t publicKey = curve25519_scalarmult(secretKey);
7 	
8 	auto expectedPublic = x"de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f";
9 	
10 	assert(publicKey == expectedPublic, "curve25519 public key generation failed!"

DH key exchange Test vectors from http://cr.yp.to/highspeed/naclcrypto-20090310.pdf

1 t {
2 	alias ubyte[32] key_t;
3 	
4 	key_t priv1, priv2, pub1, pub2;
5 	priv1[] = cast(key_t) x"77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a";
6 	priv2[] = cast(key_t) x"5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb";
7 	
8 	pub1 = curve25519_scalarmult(priv1);
9 	pub2 = curve25519_scalarmult(priv2);
10 	
11 	key_t shared1, shared2;
12 	
13 	// Generate the shared keys. Both should be equal.
14 	shared1 = curve25519_scalarmult(priv1, pub2);
15 	shared2 = curve25519_scalarmult(priv2, pub1);
16 	
17 	auto expectedSharedSecret = x"4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742";
18 	
19 	assert(shared1 == expectedSharedSecret, "curve25519 DH key agreement failed!");
20 	assert(shared1 == shared2, "curve25519 DH key agreement failed!"

Meta