golang/go · error

mldsa: invalid parameters

Error message

mldsa: invalid parameters

What it means

Returned by mldsa.GenerateKey (in the real, v1.26 implementation) when params does not match MLDSA44(), MLDSA65(), or MLDSA87(). The switch only handles the three NIST FIPS 204 parameter sets; the default branch returns errInvalidParameters. The Parameters type is comparable via switch, so a zero-value or hand-constructed Parameters falls through to default.

Source

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

package mldsa

import (
	"crypto"
	"crypto/internal/fips140/mldsa"
	"errors"
	"io"
)

// PrivateKey is an in-memory ML-DSA private key. It implements [crypto.Signer]
// and the informal extended [crypto.PrivateKey] interface.
//
// A PrivateKey is safe for concurrent use.
type PrivateKey struct {
	k mldsa.PrivateKey
}

var errInvalidParameters = errors.New("mldsa: invalid parameters")

// GenerateKey generates a new random ML-DSA private key.
func GenerateKey(params Parameters) (*PrivateKey, error) {
	switch params {
	case MLDSA44():
		return &PrivateKey{k: *mldsa.GenerateKey44()}, nil
	case MLDSA65():
		return &PrivateKey{k: *mldsa.GenerateKey65()}, nil
	case MLDSA87():
		return &PrivateKey{k: *mldsa.GenerateKey87()}, nil
	default:
		return nil, errInvalidParameters
	}
}

// NewPrivateKey decodes an ML-DSA private key from the given seed.
//
// The seed must be exactly [PrivateKeySize] bytes long.

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Always use the package-provided constructors: mldsa.MLDSA44(), mldsa.MLDSA65(), or mldsa.MLDSA87().
  2. If reading the parameter set from config, map the string ('ML-DSA-44' etc.) to the constructor explicitly and reject unknown values.
  3. Add a unit test that asserts every code path passes one of the three constants into GenerateKey.
  4. Validate params before calling GenerateKey (see validationCode below).

Example fix

// before
var params mldsa.Parameters // zero value
_, err := mldsa.GenerateKey(params) // -> errInvalidParameters
// after
_, err := mldsa.GenerateKey(mldsa.MLDSA65())
Defensive patterns

Strategy: validation

Validate before calling

func validMLDSAParams(p mldsa.Parameters) bool {
    switch p {
    case mldsa.MLDSA44(), mldsa.MLDSA65(), mldsa.MLDSA87():
        return true
    }
    return false
}
if !validMLDSAParams(params) {
    return errors.New("params must be MLDSA44, MLDSA65, or MLDSA87")
}

Prevention

When it happens

Trigger: Calling GenerateKey with the zero value of Parameters (params := mldsa.Parameters{}), with a Parameters value hand-constructed by filling struct fields instead of using a constructor, or with a value returned from a future/extra parameter set not yet supported.

Common situations: Declaring a Parameters variable and forgetting to initialize it; deserializing a Parameters from a config file whose value does not exactly equal one of the three predefined sets; passing a parameter set constant from a different ML-DSA package version.

Related errors


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