golang/go · error

mldsa: nil public key

Error message

mldsa: nil public key

What it means

Returned by the package-level Verify function when the *PublicKey argument is nil. Verify immediately dereferences pk.p, so a nil pointer would otherwise panic; this check converts that into an explicit error. It is the first guard before checking the inner key value and before delegating to mldsa.Verify.

Source

Thrown at src/crypto/mldsa/mldsa_fips140v1.26.go:217

// Parameters returns the parameters associated with this public key.
func (pk *PublicKey) Parameters() Parameters {
	switch pk.p.Parameters() {
	case "ML-DSA-44":
		return MLDSA44()
	case "ML-DSA-65":
		return MLDSA65()
	case "ML-DSA-87":
		return MLDSA87()
	default:
		panic("mldsa: invalid parameters in public key")
	}
}

// Verify reports whether signature is a valid signature of message by pk.
// If opts is nil, it's equivalent to the zero value of Options.
func Verify(pk *PublicKey, message []byte, signature []byte, opts *Options) error {
	if pk == nil {
		return errors.New("mldsa: nil public key")
	}
	if pk.p == (mldsa.PublicKey{}) {
		return errors.New("mldsa: zero public key")
	}
	if opts == nil {
		opts = &Options{}
	}
	return mldsa.Verify(&pk.p, message, signature, opts.Context)
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Nil-check the public key before calling Verify: if pk == nil { return errors.New("...") }.
  2. Ensure key-decode helpers return a non-nil *PublicKey and that you propagate their error.
  3. Initialize the key via GenerateKey().PublicKey() or Unmarshal/Decode and verify non-nil.

Example fix

// before
var pk *mldsa.PublicKey // nil after failed decode
err := mldsa.Verify(pk, msg, sig, nil)

// after
pk, err := mldsa.NewPublicKeyFromBytes(raw)
if err != nil { return err }
if err := mldsa.Verify(pk, msg, sig, nil); err != nil { return err }
Defensive patterns

Strategy: validation

Validate before calling

if pk == nil {
    return errors.New("public key is nil")
}
return mldsa.Verify(pk, msg, sig, opts)

Type guard

func isNonNilPublicKey(pk *PublicKey) bool { return pk != nil }

Try / catch

if err := mldsa.Verify(pk, msg, sig, opts); err != nil {
    if strings.Contains(err.Error(), "nil public key") {
        // re-load key, ensure non-nil pointer
    }
    return err
}

Prevention

When it happens

Trigger: Calling mldsa.Verify(nil, message, signature, opts) directly. Passing a *PublicKey field from a struct that was never populated and defaulted to nil.

Common situations: Decoding a public key where the decode path returns a nil pointer on failure but the error is ignored. Returning a nil *PublicKey from a lookup/map function and forwarding it to Verify. Test fixtures that declare var pk *mldsa.PublicKey without assignment.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/93fce741e637fa22. Report an issue: GitHub.