{"record":{"id":"29e39a5d5c96bf5e","repo":"golang/go","slug":"ecdsa-curve-not-supported-by-publickey-bytes","errorCode":null,"errorMessage":"ecdsa: curve not supported by PublicKey.Bytes","messagePattern":"ecdsa: curve not supported by PublicKey\\.Bytes","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/ecdsa/ecdsa.go","lineNumber":155,"sourceCode":"// PublicKey.Curve must be one of [elliptic.P224], [elliptic.P256],\n// [elliptic.P384], or [elliptic.P521], or Bytes returns an error.\n//\n// Bytes returns the same format as [ecdh.PublicKey.Bytes] does for NIST curves.\n//\n// Note that public keys are more commonly encoded in DER (or PEM) format, which\n// can be generated with [crypto/x509.MarshalPKIXPublicKey] (and [encoding/pem]).\nfunc (pub *PublicKey) Bytes() ([]byte, error) {\n\tswitch pub.Curve {\n\tcase elliptic.P224():\n\t\treturn publicKeyBytes(ecdsa.P224(), pub)\n\tcase elliptic.P256():\n\t\treturn publicKeyBytes(ecdsa.P256(), pub)\n\tcase elliptic.P384():\n\t\treturn publicKeyBytes(ecdsa.P384(), pub)\n\tcase elliptic.P521():\n\t\treturn publicKeyBytes(ecdsa.P521(), pub)\n\tdefault:\n\t\treturn nil, errors.New(\"ecdsa: curve not supported by PublicKey.Bytes\")\n\t}\n}\n\nfunc publicKeyBytes[P ecdsa.Point[P]](c *ecdsa.Curve[P], pub *PublicKey) ([]byte, error) {\n\tk, err := publicKeyToFIPS(c, pub)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn k.Bytes(), nil\n}\n\n// PrivateKey represents an ECDSA private key.\ntype PrivateKey struct {\n\tPublicKey\n\n\t// D is the private scalar value.\n\t//\n\t// Deprecated: modifying the raw value can produce invalid keys, and may","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/ecdsa/ecdsa.go#L137-L173","documentation":"Thrown by PublicKey.Bytes() when pub.Curve does not match any of P224, P256, P384, or P521 in the switch statement. The serialization to uncompressed SEC 1 format is only implemented for standard NIST curves via the internal FIPS ecdsa package.","triggerScenarios":"Calling Bytes() on a PublicKey where Curve is a custom or non-standard elliptic.Curve. This can also occur if the curve was set to the Params() value or a third-party curve type that doesn't match the singletons.","commonSituations":"Loading keys from external sources that specify non-standard curves; programmatically constructing a PublicKey with a custom curve; deserialization code that doesn't validate the curve before calling Bytes().","solutions":["Ensure the PublicKey was created with a standard NIST curve (P224/P256/P384/P521) — validate at construction time.","If using a custom curve, implement your own serialization using the X and Y big.Int fields directly.","Check pub.Curve against the standard singletons before calling Bytes() and handle the unsupported case explicitly."],"exampleFix":"// before\nb, err := pub.Bytes()\n\n// after\nswitch pub.Curve {\ncase elliptic.P224(), elliptic.P256(), elliptic.P384(), elliptic.P521():\n    b, err = pub.Bytes()\ndefault:\n    // manual serialization for non-NIST curves\n    b = append([]byte{0x04}, append(pub.X.FillBytes(make([]byte, pub.Curve.Params().BitSize/8)), pub.Y.FillBytes(make([]byte, pub.Curve.Params().BitSize/8))...)\n}","handlingStrategy":"validation","validationCode":"func canSerializePublicKey(pub *ecdsa.PublicKey) bool {\n    switch pub.Curve {\n    case elliptic.P224(), elliptic.P256(), elliptic.P384(), elliptic.P521():\n        return true\n    }\n    return false\n}","typeGuard":null,"tryCatchPattern":"b, err := pub.Bytes()\nif err != nil {\n    // fall back to manual serialization using pub.X, pub.Y\n    fieldSize := (pub.Curve.Params().BitLen + 7) / 8\n    b = append([]byte{0x04}, append(pub.X.FillBytes(make([]byte, fieldSize)), pub.Y.FillBytes(make([]byte, fieldSize))...)...)\n}","preventionTips":["Always construct PublicKeys using GenerateKey or parsing functions that enforce standard curves.","For non-NIST curves, serialize X and Y coordinates manually."],"tags":["crypto","ecdsa","public-key","serialization","curve-mismatch"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}