4 void ed25519_key_exchange(unsigned char *shared_secret, const unsigned char *public_key, const unsigned char *private_key) {
20 /* copy the private key and make sure it's valid */
21 for(i = 0; i < 32; ++i) {
22 e[i] = private_key[i];
29 /* unpack the public key and convert edwards to montgomery */
30 /* due to CodesInChaos: montgomeryX = (edwardsY + 1)*inverse(1 - edwardsY) mod p */
31 fe_frombytes(x1, public_key);
33 fe_add(tmp0, x1, tmp1);
34 fe_sub(tmp1, tmp1, x1);
35 fe_invert(tmp1, tmp1);
36 fe_mul(x1, tmp0, tmp1);
45 for(pos = 254; pos >= 0; --pos) {
46 b = e[pos / 8] >> (pos & 7);
49 fe_cswap(x2, x3, swap);
50 fe_cswap(z2, z3, swap);
53 /* from montgomery.h */
64 fe_mul(x2, tmp1, tmp0);
65 fe_sub(tmp1, tmp1, tmp0);
67 fe_mul121666(z3, tmp1);
69 fe_add(tmp0, tmp0, z3);
71 fe_mul(z2, tmp1, tmp0);
74 fe_cswap(x2, x3, swap);
75 fe_cswap(z2, z3, swap);
79 fe_tobytes(shared_secret, x2);