> For the complete documentation index, see [llms.txt](https://docs.bsvblockchain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bsvblockchain.org/guides/sdks/ts/low-level/ecdh.md).

# ECDH

The process of ECDH enables two parties to agree on a common shared secret. Then, messages can be exchanged using this common secret. This guide provides a conceptual example, then delves into a practical example.

## The Setup

Each party randomly generates a private key, then sends the associated public key to the other party. In order to compute the public key from the private key, they multiply the private key by a generator point on an elliptic curve, using point multiplication.

```
Alice = random()
AlicePub = Alice * GeneratorPoint
Bob = random()
BobPub = Bob * GeneratorPoint
```

Then, the parties use Elliptic Curve Diffie-Hallman (ECDH) to derive a shared secret. The way this works is that the first party, let's call her Alice, uses her private key combined with the second user's (call him Bob) public key to compute a secret. Meanwhile, Bob can use his private key and Alice's public key to compute the same secret:

```
AliceSecret = Alice * BobPub
BobSecret = Bob * AlicePub
```

These secrets are equal because:

```
AlicePub (Alice * GeneratorPoint) * Bob = BobPub (Bob * GeneratorPoint) * Alice
```

Put more simply:

```
Alice * Bob * GeneratorPoint
Bob * Alice * GeneratorPoint
```

Because multiplication is easy but division is practically impossible in elliptic curve math, the system is secure even though both parties generate the same shared secret. Also, no third party can generate the shared secret. Alice can't learn Bob's private key and Bob can't learn Alice's private key.

## Practical Demonstration

The BSV SDK enables the computation of a shared secret as follows:

```ts
import { PrivateKey } from '@bsv/sdk'

const alice = PrivateKey.fromRandom()
const alicePub = alice.toPublicKey()

const bob = PrivateKey.fromRandom()
const bobPub = bob.toPublicKey()

// Alice derives the secret with Bob's public key
// She does not need his private key.
const aliceSecret = alice.deriveSharedSecret(bobPub).toString()

// Bob derives the secret with Alice's public key
// He does not need her private key.
const bobSecret = bob.deriveSharedSecret(alicePub).toString()

// We've converted these secrets into hex and now we can see if they are equal
console.log(aliceSecret === bobSecret)
// true
```

You can then use the secret to encrypt data or otherwise communicate securely between the two parties.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.bsvblockchain.org/guides/sdks/ts/low-level/ecdh.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
