{"record":{"id":"8e1012fbbf2a2417","repo":"TheAlgorithms/Python","slug":"unsupported-group","errorCode":null,"errorMessage":"Unsupported Group","messagePattern":"Unsupported Group","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ciphers/diffie_hellman.py","lineNumber":216,"sourceCode":"    >>> bob_shared = bob.generate_shared_key(alice_public)\n\n    >>> assert alice_shared == bob_shared\n\n    >>> # generating shared key using static methods\n    >>> alice_shared = DiffieHellman.generate_shared_key_static(\n    ...     alice_private, bob_public\n    ... )\n    >>> bob_shared = DiffieHellman.generate_shared_key_static(\n    ...     bob_private, alice_public\n    ... )\n\n    >>> assert alice_shared == bob_shared\n    \"\"\"\n\n    # Current minimum recommendation is 2048 bit (group 14)\n    def __init__(self, group: int = 14) -> None:\n        if group not in primes:\n            raise ValueError(\"Unsupported Group\")\n        self.prime = primes[group][\"prime\"]\n        self.generator = primes[group][\"generator\"]\n\n        self.__private_key = int(hexlify(urandom(32)), base=16)\n\n    def get_private_key(self) -> str:\n        return hex(self.__private_key)[2:]\n\n    def generate_public_key(self) -> str:\n        public_key = pow(self.generator, self.__private_key, self.prime)\n        return hex(public_key)[2:]\n\n    def is_valid_public_key(self, key: int) -> bool:\n        # check if the other public key is valid based on NIST SP800-56\n        return (\n            2 <= key <= self.prime - 2\n            and pow(key, (self.prime - 1) // 2, self.prime) == 1\n        )","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/diffie_hellman.py#L198-L234","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use a group id present in the module's primes dict (inspect it first: from ciphers.diffie_hellman import primes; print(primes.keys()))","Default to group=14 (2048-bit, the module's recommended minimum)","Validate the group from config against primes before constructing"],"exampleFix":"# before\ndh = DiffieHellman(group=19)\n\n# after\nfrom ciphers.diffie_hellman import primes\ndh = DiffieHellman(group=19 if 19 in primes else 14)","handlingStrategy":"validation","validationCode":"from ciphers.diffie_hellman import primes\nif group not in primes:\n    group = 14  # or raise your own configuration error","typeGuard":"def is_supported_group(group: int) -> bool:\n    from ciphers.diffie_hellman import primes\n    return group in primes","tryCatchPattern":"try:\n    dh = DiffieHellman(group=group)\nexcept ValueError as exc:\n    raise ConfigError(f\"DH group {group} not available: {exc}\") from exc","preventionTips":["Inspect primes.keys() once and pin your config to a supported group","Default to group 14 (2048-bit) unless you have a reason otherwise","Validate group ids from config files before constructing the object"],"tags":["diffie-hellman","key-exchange","configuration","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}