Standard library / crypto/ecdh
crypto/ecdh.Curve
Interface for GenerateKey, NewPrivateKey and NewPublicKey. NIST curves require valid nonzero scalar and uncompressed valid public point; X25519 validates length at construction and rejects all-zero agreement results. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
type Curve interface { GenerateKey(rand io.Reader) (*PrivateKey, error)
NewPrivateKey(key []byte) (*PrivateKey, error)
NewPublicKey(key []byte) (*PublicKey, error)
}Working Example
Example Snippetgo
package main
import ( "crypto/ecdh" "crypto/rand" "fmt")
func main() { k := must(ecdh.X25519().GenerateKey(rand.Reader)) v := must(ecdh.X25519().NewPrivateKey(k.Bytes())) p := must(ecdh.X25519().NewPublicKey(k.PublicKey().Bytes())) fmt.Println(v.Equal(k) && p.Equal(k.PublicKey()))}func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}