Finite Field Diffie-Hellman

Toy implementation of Diffie-Hellman key exchange over finite fields \(\Zmod{p}\).

Warning

This is a toy implementation for educational use only! Do not use this implementation, or any cryptographic features of Sage, in any setting where security is needed!

AUTHORS:

  • Vincent Macri (2024-07-30): initial version

  • Brian Heckel (2025-12-06): converted to inherit from KeyExchangeBase class

class sage.crypto.public_key.key_exchange.finite_field_diffie_hellman.FiniteFieldDiffieHellman(p: Integer | int, generator: Integer | IntegerMod_abstract | int, proof: bool | None = None)[source]

Bases: CommutativeKeyExchangeBase

Create an instance of the Diffie-Hellman key exchange scheme using the given prime p and base g.

INPUT:

  • p – prime integer defining the field \(\GF{p}\) that the key exchanges will be performed over, must be at least 5

  • generator – base for the key exchange, (coerceable to) an element of \(\GF{p}\) from \(2\) to \(p - 2\)

  • proof – whether to require a proof that p is prime. If False, a probabilistic test is used to check that p is prime. This should be set to False when using large (cryptographic size) primes, otherwise checking primality will take too long. If this is not specified, then the default behaviour is to use the current value of \(proof.arithmetic()\).

REFERENCES:

For more information, see Section 8.1 of [PP2010].

EXAMPLES:

sage: DH = key_exchange.FiniteFieldDiffieHellman(13, 2)
doctest:...: FutureWarning: SageMath's key exchange functionality is experimental and might change in the future.
             See https://github.com/sagemath/sage/issues/41218 for details.

This is an example of a full key exchange using a cryptographically large prime. This is the prime from the 8192-bit MODP group in RFC 3526 (see [KK2003]):

sage: p = 2^8192 - 2^8128 - 1 + 2^64 * (round(2^8062 * pi) + 4743158)
sage: DH = key_exchange.FiniteFieldDiffieHellman(p, 2, proof=False)
sage: alice_sk = DH.secret_key()
sage: alice_pk = DH.public_key(alice_sk)
sage: bob_sk = DH.secret_key()
sage: bob_pk = DH.public_key(bob_sk)
sage: alice_shared_secret = DH.compute_shared_secret(alice_sk, bob_pk)
sage: bob_shared_secret = DH.compute_shared_secret(bob_sk, alice_pk)
sage: alice_shared_secret == bob_shared_secret
True
compute_shared_secret(secret_key, public_key)[source]

Compute the shared secret using the given public key and secret keys.

INPUT:

  • pk – public key

  • sk – secret key

EXAMPLES:

sage: DH = key_exchange.FiniteFieldDiffieHellman(17, 3)
sage: DH.compute_shared_secret(11, 13)
4
parameters()[source]

Returns a list of the prime p used, the base generator g and the field used.

EXAMPLES:

sage: DH = key_exchange.FiniteFieldDiffieHellman(17, 3)
sage: DH.parameters()
(Finite Field of size 17, 3)
public_key(secret_key)[source]

Generate a Diffie-Hellman public key using the given secret key.

INPUT:

  • secret_key – the secret key to generate the public key with

EXAMPLES:

sage: DH = key_exchange.FiniteFieldDiffieHellman(13, 2)
sage: DH.public_key(4)
3
secret_key()[source]

Generate a random Diffie-Hellman secret key.