initial la moitié du TP est fait mais commit

This commit is contained in:
boyreau 2026-11-09 18:30:30 +01:00
commit c58e985561
4 changed files with 145 additions and 0 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use nix

104
ElGamal.py Normal file
View file

@ -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

13
shell.nix Normal file
View file

@ -0,0 +1,13 @@
let
pkgs = import <nixpkgs> { };
in
pkgs.mkShell {
packages = [
pkgs.python314
pkgs.openssl
pkgs.ruff
pkgs.python314Packages.python-lsp-server
pkgs.python314Packages.pycryptodome
pkgs.python314Packages.sympy
];
}

27
test_ElGamal.py Normal file
View file

@ -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