commit c58e9855619bc1c6dd3edf4f5d14ab7660917e63 Author: boyreau Date: Mon Nov 9 18:30:30 2026 +0100 initial la moitiƩ du TP est fait mais commit diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..1d953f4 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use nix diff --git a/ElGamal.py b/ElGamal.py new file mode 100644 index 0000000..c00a6ad --- /dev/null +++ b/ElGamal.py @@ -0,0 +1,104 @@ +from random import randint +from sympy import discrete_log +from functools import reduce + + +def KeyGen(g: int, q: int) -> (int, int): + """Generate a secure key for ElGamal. + + Args: + g: A generator for the group. + q: The order of a group. + Returns: + return A public key and a secret key. + """ + k = randint(2, q - 1) + return pow(g, k, q), k + + +def Encrypt_cc(g: int, q: int, pk: int, m: int) -> (int, int): + """Encrypt data using a public key + + Args: + g: A generator for the group + q: The order of the group + pk: The public key used to cipher the message + m: A message to cipher. + Returns: + The ciphered message. + """ + r = randint(2, q - 1) + return (pow(g, r, q), (m * pow(pk, r, q))) + + +def Decrypt_cc(g: int, q: int, sk: int, c: (int, int)) -> int: + """Decrypt an ElGamal cipher using a public key + + Args: + g: A generator for the group + q: The order of the group + sk: The secret key to use to decipher + c: A ciphered message to clarify + Returns: + The clear message. + """ + return c[1] // pow(c[0], sk, q) + + +def Encrypt_ce(g: int, q: int, pk: int, m: int) -> (int, int): + """Encrypt an ElGamal cipher using a public key using exponential + + Args: + g: A generator for the group + q: The order of the group + pk: The public key to use to cipher + m: A message to cipher + Returns: + The ciphered message. + """ + r = randint(2, q - 1) + return (pow(g, r, q), (pow(g, m, q) * pow(pk, r, q)) % q) + + +def Decrypt_ce(g: int, q: int, sk: int, c: (int, int)) -> int: + """Encrypt an ElGamal cipher using a public key (exponential) + + Args: + g: A generator for the group + q: The order of the group + sk: The secret key to use to decipher + c: A ciphered message to decipher + Returns: + The clear message. + """ + s = c[1] * pow(c[0], -sk, q) + return discrete_log(q, s, g) + + +def Add_cipher(c: list[(int, int)]) -> int: + """Add multiple ciphers together into a new ElGamal cipher. + Args: + c: A list of ciphers. + Returns: + The accumulation of ciphers. + """ + return reduce(lambda x, y: x + y[1], c, 0) + + +def KeyGen_central(g: int, q: int, N: int) -> (int, int): + """Generate N public/private key pairs. + + Args: + g: A generator for the group. + q: The order of a group. + N: The number of keypairs to generate. + Returns: + N public keys and 1 private key. + """ + keys = [] + for i in range(N): + r = randint(1, q - 1) + keys.push(pow(g, r, q)) + K = reduce(lambda x, y: x * y, keys, 1) + keys.push(K) + return keys diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..ee1f631 --- /dev/null +++ b/shell.nix @@ -0,0 +1,13 @@ +let + pkgs = import { }; +in +pkgs.mkShell { + packages = [ + pkgs.python314 + pkgs.openssl + pkgs.ruff + pkgs.python314Packages.python-lsp-server + pkgs.python314Packages.pycryptodome + pkgs.python314Packages.sympy + ]; +} diff --git a/test_ElGamal.py b/test_ElGamal.py new file mode 100644 index 0000000..9cd78a6 --- /dev/null +++ b/test_ElGamal.py @@ -0,0 +1,27 @@ +from ElGamal import KeyGen, Encrypt_cc, Decrypt_cc, Encrypt_ce, Decrypt_ce, Add_cipher +from sympy import randprime, primitive_root + +if __name__ == "__main__": + # On prend le groupe de (randprime - 1) + ordre = randprime(2, 1 << 10) + gen = primitive_root(ordre) + + m = 22 + pk, sk = KeyGen(gen, ordre) + print(f"Private key: {pk}, public key: {sk}") + cipher = Encrypt_cc(gen, ordre, pk, m) + # print(f"Cipher of {m}: {cipher}") + clear = Decrypt_cc(gen, ordre, sk, cipher) + print(f"Cleartext message : {clear}") + assert clear == m + + cipher = 0 + clear = 0 + cipher = Encrypt_ce(gen, ordre, pk, m) + print(f"Cipher_ce: {cipher}") + clear = Decrypt_ce(gen, ordre, sk, cipher) + print(f"Cleartext_ce: {clear}") + assert clear == m + + ciphers = [(1, 1), (1, 2), (1, 3)] + assert Add_cipher(ciphers) == 1 + 2 + 3