TheAlgorithms/Python · error · ValueError

Unsupported Group

Error message

Unsupported Group

What it means

Raised by DiffieHellman.__init__ in ciphers/diffie_hellman.py when the requested group number is not a key of the module's `primes` table. The implementation only ships predefined MODP groups, so any unknown group id is rejected.

Source

Thrown at ciphers/diffie_hellman.py:216

    >>> bob_shared = bob.generate_shared_key(alice_public)

    >>> assert alice_shared == bob_shared

    >>> # generating shared key using static methods
    >>> alice_shared = DiffieHellman.generate_shared_key_static(
    ...     alice_private, bob_public
    ... )
    >>> bob_shared = DiffieHellman.generate_shared_key_static(
    ...     bob_private, alice_public
    ... )

    >>> assert alice_shared == bob_shared
    """

    # Current minimum recommendation is 2048 bit (group 14)
    def __init__(self, group: int = 14) -> None:
        if group not in primes:
            raise ValueError("Unsupported Group")
        self.prime = primes[group]["prime"]
        self.generator = primes[group]["generator"]

        self.__private_key = int(hexlify(urandom(32)), base=16)

    def get_private_key(self) -> str:
        return hex(self.__private_key)[2:]

    def generate_public_key(self) -> str:
        public_key = pow(self.generator, self.__private_key, self.prime)
        return hex(public_key)[2:]

    def is_valid_public_key(self, key: int) -> bool:
        # check if the other public key is valid based on NIST SP800-56
        return (
            2 <= key <= self.prime - 2
            and pow(key, (self.prime - 1) // 2, self.prime) == 1
        )

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use a group id present in the module's primes dict (inspect it first: from ciphers.diffie_hellman import primes; print(primes.keys()))
  2. Default to group=14 (2048-bit, the module's recommended minimum)
  3. Validate the group from config against primes before constructing

Example fix

# before
dh = DiffieHellman(group=19)

# after
from ciphers.diffie_hellman import primes
dh = DiffieHellman(group=19 if 19 in primes else 14)
Defensive patterns

Strategy: validation

Validate before calling

from ciphers.diffie_hellman import primes
if group not in primes:
    group = 14  # or raise your own configuration error

Type guard

def is_supported_group(group: int) -> bool:
    from ciphers.diffie_hellman import primes
    return group in primes

Try / catch

try:
    dh = DiffieHellman(group=group)
except ValueError as exc:
    raise ConfigError(f"DH group {group} not available: {exc}") from exc

Prevention

When it happens

Trigger: DiffieHellman(group=24) or group=5 if not present in the primes dict; passing a string like group="14"; iterating group ids and hitting an unlisted one.

Common situations: Assuming IETF MODP group numbers (e.g. 15-18, 21-24) are supported when the module ships fewer; config files carried over from another DH library; typos in group settings.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/8e1012fbbf2a2417. Report an issue: GitHub.