{"record":{"id":"0de5c47d41bcf590","repo":"golang/go","slug":"ecdsa-curve-not-supported-by-privatekey-bytes","errorCode":null,"errorMessage":"ecdsa: curve not supported by PrivateKey.Bytes","messagePattern":"ecdsa: curve not supported by PrivateKey\\.Bytes","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/ecdsa/ecdsa.go","lineNumber":296,"sourceCode":"// [elliptic.P384], or [elliptic.P521], or Bytes returns an error.\n//\n// Bytes returns the same format as [ecdh.PrivateKey.Bytes] does for NIST curves.\n//\n// Note that private keys are more commonly encoded in ASN.1 or PKCS#8 format,\n// which can be generated with [crypto/x509.MarshalECPrivateKey] or\n// [crypto/x509.MarshalPKCS8PrivateKey] (and [encoding/pem]).\nfunc (priv *PrivateKey) Bytes() ([]byte, error) {\n\tswitch priv.Curve {\n\tcase elliptic.P224():\n\t\treturn privateKeyBytes(ecdsa.P224(), priv)\n\tcase elliptic.P256():\n\t\treturn privateKeyBytes(ecdsa.P256(), priv)\n\tcase elliptic.P384():\n\t\treturn privateKeyBytes(ecdsa.P384(), priv)\n\tcase elliptic.P521():\n\t\treturn privateKeyBytes(ecdsa.P521(), priv)\n\tdefault:\n\t\treturn nil, errors.New(\"ecdsa: curve not supported by PrivateKey.Bytes\")\n\t}\n}\n\nfunc privateKeyBytes[P ecdsa.Point[P]](c *ecdsa.Curve[P], priv *PrivateKey) ([]byte, error) {\n\tk, err := privateKeyToFIPS(c, priv)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn k.Bytes(), nil\n}\n\n// Sign signs a hash (which should be the result of hashing a larger message\n// with opts.HashFunc()) using the private key, priv. If the hash is longer than\n// the bit-length of the private key's curve order, the hash will be truncated\n// to that length. It returns the ASN.1 encoded signature, like [SignASN1].\n//\n// If random is not nil, the signature is randomized. Most applications should use\n// [crypto/rand.Reader] as random, but unless GODEBUG=cryptocustomrand=1 is set, a","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/ecdsa/ecdsa.go#L278-L314","documentation":"Thrown by PrivateKey.Bytes() when priv.Curve does not match P224, P256, P384, or P521. Only standard NIST curves have serialization implemented through the internal FIPS ecdsa package. The raw private key scalar serialization is not available for custom or unsupported curves.","triggerScenarios":"Calling Bytes() on a PrivateKey whose Curve is a non-NIST curve or was set using .Params() instead of the singleton. The switch falls through to the default branch.","commonSituations":"Manually constructing a PrivateKey struct with a custom curve; loading keys from non-standard formats; using curve.Params() as the Curve field instead of the singleton.","solutions":["Ensure the PrivateKey was created with a standard NIST curve singleton before calling Bytes().","For non-NIST curves, serialize priv.D manually using FillBytes to the curve's byte size.","Validate priv.Curve at construction or deserialization time, rejecting unsupported curves early."],"exampleFix":"// before\nb, err := priv.Bytes()\n\n// after\nswitch priv.Curve {\ncase elliptic.P224(), elliptic.P256(), elliptic.P384(), elliptic.P521():\n    b, err = priv.Bytes()\ndefault:\n    size := (priv.Curve.Params().N.BitLen() + 7) / 8\n    b = priv.D.FillBytes(make([]byte, size))\n}","handlingStrategy":"validation","validationCode":"func canSerializePrivateKey(priv *ecdsa.PrivateKey) bool {\n    switch priv.Curve {\n    case elliptic.P224(), elliptic.P256(), elliptic.P384(), elliptic.P521():\n        return true\n    }\n    return false\n}","typeGuard":null,"tryCatchPattern":"b, err := priv.Bytes()\nif err != nil {\n    // manual serialization for non-NIST curves\n    size := (priv.Curve.Params().N.BitLen() + 7) / 8\n    b = priv.D.FillBytes(make([]byte, size))\n}","preventionTips":["Use standard NIST curves for all keys that need library serialization support.","Validate the curve at key construction time to fail early."],"tags":["crypto","ecdsa","private-key","serialization","curve-mismatch"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}