src/crypto/public_key/elliptic/ecdh.js
async function genPublicEphemeralKey(curve, Q) {
  if (curve.name === 'curve25519') {
    const { secretKey: d } = nacl.box.keyPair();
    const { secretKey, sharedKey } = await genPrivateEphemeralKey(curve, Q, d);
    let { publicKey } = nacl.box.keyPair.fromSecretKey(secretKey);
    publicKey = util.concatUint8Array([new Uint8Array([0x40]), publicKey]);
    return { publicKey, sharedKey }; // Note: sharedKey is little-endian here, unlike below
  }
  const v = await curve.genKeyPair();
  Q = curve.keyFromPublic(Q);
  const publicKey = new Uint8Array(v.getPublic());
  const S = v.derive(Q);
  const len = curve.curve.curve.p.byteLength();
  const sharedKey = S.toArrayLike(Uint8Array, 'be', len);
  return { publicKey, sharedKey };
}
async function genPublicEphemeralKey(curve, Q) {
  if (curve.name === 'curve25519') {
    const { secretKey: d } = nacl.box.keyPair();
    const { secretKey, sharedKey } = await genPrivateEphemeralKey(curve, Q, d);
    let { publicKey } = nacl.box.keyPair.fromSecretKey(secretKey);
    publicKey = util.concatUint8Array([new Uint8Array([0x40]), publicKey]);
    return { publicKey, sharedKey }; // Note: sharedKey is little-endian here, unlike below
  }
  const v = await curve.genKeyPair();
  Q = curve.keyFromPublic(Q);
  const publicKey = new Uint8Array(v.getPublic());
  const S = v.derive(Q);
  const len = curve.curve.curve.p.byteLength();
  const sharedKey = S.toArrayLike(Uint8Array, 'be', len);
  return { publicKey, sharedKey };
}
async function genPublicEphemeralKey ( curve , Q ) { if ( curve . name === 'curve25519' ) { const { secretKey : d } = nacl . box . keyPair ( ) ; const { secretKey , sharedKey } = await genPrivateEphemeralKey ( curve , Q , d ) ; let { publicKey } = nacl . box . keyPair . fromSecretKey ( secretKey ) ; publicKey = util . concatUint8Array ( [ new Uint8Array ( [ 0x40 ] ) , publicKey ] ) ; return { publicKey , sharedKey } ; // Note: sharedKey is little-endian here, unlike below } const v = await curve . genKeyPair ( ) ; Q = curve . keyFromPublic ( Q ) ; const publicKey = new Uint8Array ( v . getPublic ( ) ) ; const S = v . derive ( Q ) ; const len = curve . curve . curve . p . byteLength ( ) ; const sharedKey = S . toArrayLike ( Uint8Array , 'be' , len ) ; return { publicKey , sharedKey } ; }
Generate ECDHE ephemeral key and secret from public key

@param  {Curve}                  curve        Elliptic curve object
@param  {Uint8Array}             Q                   Recipient public key
@returns {Promise<{V: Uint8Array, S: BN}>}   Returns public part of ephemeral key and generated ephemeral secret
@async
Generate ECDHE ephemeral key and secret from public key