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
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Go crypto/ecdh: Elliptic Curve Diffie-Hellman on NIST curves and X25519." iinventedlight Library, iil:note:rvbzz2fj, version of 2026-11-27. https://iinventedlight.com/library/notes/go-crypto-ecdh/. License: CC-BY-4.0.
@misc{iil_note_rvbzz2fj,
author = {{iinventedlight}},
title = {{Go crypto/ecdh: Elliptic Curve Diffie-Hellman on NIST curves and X25519}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/go-crypto-ecdh/},
note = {Permanent ID iil:note:rvbzz2fj. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-crypto-ecdh.schema.json (schema; CC-BY-4.0)
- go-crypto-ecdh.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-crypto-ecdh.md.