---
source: https://iinventedlight.com/library/notes/go-crypto-ecdh.md
title: "Go crypto/ecdh: Elliptic Curve Diffie-Hellman on NIST curves and X25519"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (The Go Authors); BSD-3-Clause (The Go Authors); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Go Authors, ecdh package - crypto/ecdh; The Go Authors, ecdh.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-27
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-27
as_of: 2026-11-27
confidence: medium
untested: "Not reproduced (2026-11-27). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go-crypto-ecdh"
tags: [go, ecdh, x25519, p256, key-exchange]
client: Librarian
sources:
  - id: S1
    title: "ecdh package - crypto/ecdh"
    publisher: The Go Authors
    url: https://pkg.go.dev/crypto/ecdh
    accessed: 2026-11-27
    license: "BSD-3-Clause"
  - id: S2
    title: "ecdh.go (source)"
    publisher: The Go Authors
    url: https://go.dev/src/crypto/ecdh/ecdh.go
    accessed: 2026-11-27
    license: "BSD-3-Clause"
---
# Go crypto/ecdh: Elliptic Curve Diffie-Hellman on NIST curves and X25519
**Answer:** The crypto/ecdh package implements Elliptic Curve Diffie-Hellman over NIST curves and Curve25519.

## Curves and keys

P256, P384, P521, and X25519 each return a Curve value. Multiple invocations return the same value, so it can be used as a map key.

GenerateKey creates a random PrivateKey. From Go 1.26 a secure random source is always used, and rand is ignored unless GODEBUG=cryptocustomrand=1 is set.

NewPrivateKey checks that a key encoding is valid. On NIST curves a zero key is rejected and the integer must be lower than the curve order. On X25519 only the scalar length is checked.

NewPublicKey checks a public encoding. NIST keys must be uncompressed points per SEC 1, Version 2.0, Section 2.3.4; compressed forms and the point at infinity are rejected. X25519 only checks u-coordinate length. Adversarially selected public keys can cause ECDH to return an error.

PrivateKey is usually kept secret. PublicKey is usually a peer's ECDH share sent over the wire. crypto/x509 can parse and marshal these keys; NIST curves still need a crypto/ecdsa conversion afterward.

## Exchange and comparison

PrivateKey.ECDH runs the exchange and returns the shared secret. Both keys must use the same curve.

On NIST curves the method follows SEC 1, Version 2.0, Section 3.3.1, and encodes the x-coordinate per Section 2.3.5. The result is never the point at infinity. That step is also the shared-secret computation in NIST SP 800-56A Rev. 3, Section 6.1.2.2.

On X25519 it follows RFC 7748, Section 6.1, and returns an error if the result is all zeros.

Bytes returns a copy of the key encoding. Equal reports whether two keys represent the same key. The Equal check is performed in constant time as long as the key types match.

## See also

- [go-crypto-ecdsa](https://iinventedlight.com/library/notes/go-crypto-ecdsa/)
- [go-ed25519](https://iinventedlight.com/library/notes/go-ed25519/)
- [go-crypto-sha256](https://iinventedlight.com/library/notes/go-crypto-sha256/)

## Claims

- id: C1
  claim: The package implements ECDH over NIST curves and Curve25519.
  source: S1
  evidence: "implements Elliptic Curve Diffie-Hellman over"

- id: C2
  claim: ECDH performs an exchange and returns the shared secret.
  source: S2
  evidence: "ECDH performs an ECDH exchange and returns the shared secret"

- id: C3
  claim: For X25519, NewPrivateKey only checks the scalar length.
  source: S2
  evidence: "For X25519, this only checks the scalar length"

## Sources

- [S1] ecdh package - crypto/ecdh. The Go Authors. https://pkg.go.dev/crypto/ecdh . License: BSD-3-Clause. Accessed 2026-11-27.

- [S2] ecdh.go (source). The Go Authors. https://go.dev/src/crypto/ecdh/ecdh.go . License: BSD-3-Clause. Accessed 2026-11-27.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, ecdh package - crypto/ecdh; The Go Authors, ecdh.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
