{"record":{"id":"6ecd262951ed14fd","repo":"golang/go","slug":"overflowing-coordinate","errorCode":null,"errorMessage":"overflowing coordinate","messagePattern":"overflowing coordinate","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/ecdsa/ecdsa.go","lineNumber":628,"sourceCode":"\tD := priv.D.FillBytes(make([]byte, size, maxScalarSize))\n\n\treturn privateKeyCache.Get(priv, func() (*ecdsa.PrivateKey, error) {\n\t\treturn ecdsa.NewPrivateKey(c, D, Q)\n\t}, func(k *ecdsa.PrivateKey) bool {\n\t\treturn subtle.ConstantTimeCompare(k.PublicKey().Bytes(), Q) == 1 &&\n\t\t\tsubtle.ConstantTimeCompare(k.Bytes(), D) == 1\n\t})\n}\n\n// pointFromAffine is used to convert the PublicKey to a nistec SetBytes input.\nfunc pointFromAffine(curve elliptic.Curve, x, y *big.Int) ([]byte, error) {\n\tbitSize := curve.Params().BitSize\n\t// Reject values that would not get correctly encoded.\n\tif x.Sign() < 0 || y.Sign() < 0 {\n\t\treturn nil, errors.New(\"negative coordinate\")\n\t}\n\tif x.BitLen() > bitSize || y.BitLen() > bitSize {\n\t\treturn nil, errors.New(\"overflowing coordinate\")\n\t}\n\t// Encode the coordinates and let [ecdsa.NewPublicKey] reject invalid points.\n\tbyteLen := (bitSize + 7) / 8\n\tbuf := make([]byte, 1+2*byteLen)\n\tbuf[0] = 4 // uncompressed point\n\tx.FillBytes(buf[1 : 1+byteLen])\n\ty.FillBytes(buf[1+byteLen : 1+2*byteLen])\n\treturn buf, nil\n}\n\n// pointToAffine is used to convert a nistec Bytes encoding to a PublicKey.\nfunc pointToAffine(curve elliptic.Curve, p []byte) (x, y *big.Int, err error) {\n\tif len(p) == 1 && p[0] == 0 {\n\t\t// This is the encoding of the point at infinity.\n\t\treturn nil, nil, errors.New(\"ecdsa: public key point is the infinity\")\n\t}\n\tbyteLen := (curve.Params().BitSize + 7) / 8\n\tx = new(big.Int).SetBytes(p[1 : 1+byteLen])","sourceCodeStart":610,"sourceCodeEnd":646,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/ecdsa/ecdsa.go#L610-L646","documentation":"Thrown by pointFromAffine (ecdsa.go:628) when a public key coordinate's bit-length exceeds the curve's BitSize. This guards the fixed-width FillBytes encoding: a coordinate larger than the field would silently overflow the buffer. It indicates a wrong curve assignment or a coordinate that was never reduced modulo the field prime.","triggerScenarios":"Same Sign/Verify conversion paths as 241. Triggered when x.BitLen() > curve.Params().BitSize || y.BitLen() > curve.Params().BitSize — e.g. assigning a P-384 point to a P-256 curve, or coordinates built from a hash without masking/reduction.","commonSituations":"Curve mismatch (key generated on P-384 but PublicKey.Curve set to P-256), importing a point from a system using a different field, or coordinates derived from raw hash output that exceed the field.","solutions":["Ensure PublicKey.Curve matches the curve the key was generated on; load via standard parsing which sets Curve correctly.","Reduce coordinates modulo the field prime and confirm x.BitLen() <= curve.Params().BitSize before use.","Re-derive or re-parse the key from its canonical encoding (SEC1 uncompressed point)."],"exampleFix":"// before\npub := ecdsa.PublicKey{Curve: elliptic.P256(), X: p384X, Y: p384Y} // wrong curve\n// -> error 242: overflowing coordinate\n\n// after\npub := ecdsa.PublicKey{Curve: elliptic.P384(), X: p384X, Y: p384Y}","handlingStrategy":"validation","validationCode":"bs := pub.Curve.Params().BitSize\nif pub.X.BitLen() > bs || pub.Y.BitLen() > bs {\n    return fmt.Errorf(\"coordinate overflows %d-bit curve\", bs)\n}","typeGuard":"func coordsFitCurve(pub *ecdsa.PublicKey) bool {\n    bs := pub.Curve.Params().BitSize\n    return pub.X.BitLen() <= bs && pub.Y.BitLen() <= bs\n}","tryCatchPattern":null,"preventionTips":["Ensure PublicKey.Curve matches the curve the coordinates belong to.","Reduce coordinates modulo the field prime before use.","Parse points from canonical SEC1/ASN.1 encodings rather than raw coordinates."],"tags":["go","crypto","ecdsa","validation","fips"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}