Skip to content

Commit

Permalink
module name added into loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
serengil committed Dec 12, 2023
1 parent b4e19df commit ca9b98d
Show file tree
Hide file tree
Showing 23 changed files with 57 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lightphe/cryptosystems/Benaloh.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lightphe.models.Homomorphic import Homomorphic
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="lightphe/cryptosystems/Benaloh.py")


class Benaloh(Homomorphic):
Expand Down
2 changes: 1 addition & 1 deletion lightphe/cryptosystems/DamgardJurik.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lightphe.models.Homomorphic import Homomorphic
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="lightphe/cryptosystems/DamgardJurik.py")


class DamgardJurik(Homomorphic):
Expand Down
5 changes: 3 additions & 2 deletions lightphe/cryptosystems/ElGamal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lightphe.models.Homomorphic import Homomorphic
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="lightphe/cryptosystems/ElGamal.py")


class ElGamal(Homomorphic):
Expand All @@ -26,7 +26,8 @@ def __init__(self, keys: Optional[dict] = None, exponential=False, key_size: int
"""
self.exponential = exponential
self.keys = keys or self.generate_keys(key_size)
self.modulo = self.keys["public_key"]["p"]
self.plaintext_modulo = self.keys["public_key"]["p"]
self.ciphertext_modulo = self.keys["public_key"]["p"]

def generate_keys(self, key_size: int):
"""
Expand Down
5 changes: 3 additions & 2 deletions lightphe/cryptosystems/EllipticCurveElGamal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from lightphe.elliptic.Weierstrass import Weierstrass
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="lightphe/cryptosystems/EllipticCurveElGamal.py")


class EllipticCurveElGamal(Homomorphic):
Expand All @@ -27,7 +27,8 @@ def __init__(self, keys: Optional[dict] = None, key_size: int = 160):
# TODO: add different forms and curves. e.g. Koblitz, Edwards (Ed25519)
self.curve = Weierstrass()
self.keys = keys or self.generate_keys(key_size)
self.modulo = self.curve.p
self.plaintext_modulo = self.curve.p
self.ciphertext_modulo = self.curve.p

def generate_keys(self, key_size: int):
"""
Expand Down
4 changes: 3 additions & 1 deletion lightphe/cryptosystems/GoldwasserMicali.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from lightphe.models.Homomorphic import Homomorphic
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="lightphe/cryptosystems/GoldwasserMicali.py")

# pylint:disable=consider-using-enumerate

Expand All @@ -26,6 +26,8 @@ def __init__(self, keys: Optional[dict] = None, key_size=100):
"""
self.keys = keys or self.generate_keys(key_size)
self.ciphertext_modulo = self.keys["public_key"]["n"]
# TODO: not sure about the plaintext modulo
self.plaintext_modulo = self.keys["public_key"]["n"]

def generate_keys(self, key_size: int) -> dict:
"""
Expand Down
2 changes: 1 addition & 1 deletion lightphe/cryptosystems/NaccacheStern.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from lightphe.models.Homomorphic import Homomorphic
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="lightphe/cryptosystems/NaccacheStern.py")

# pylint: disable=simplifiable-if-expression, consider-using-enumerate

Expand Down
2 changes: 1 addition & 1 deletion lightphe/cryptosystems/OkamotoUchiyama.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lightphe.models.Homomorphic import Homomorphic
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="lightphe/cryptosystems/OkamotoUchiyama.py")


class OkamotoUchiyama(Homomorphic):
Expand Down
2 changes: 1 addition & 1 deletion lightphe/cryptosystems/Paillier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lightphe.models.Homomorphic import Homomorphic
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="lightphe/cryptosystems/Paillier.py")


class Paillier(Homomorphic):
Expand Down
5 changes: 3 additions & 2 deletions lightphe/cryptosystems/RSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from lightphe.models.Homomorphic import Homomorphic
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="lightphe/cryptosystems/RSA.py")


class RSA(Homomorphic):
Expand All @@ -28,7 +28,8 @@ def __init__(self, keys: Optional[dict] = None, key_size: int = 1024, encrypt_wi
and do decryption with private key d.
"""
self.keys = keys or self.generate_keys(key_size)
self.modulo = self.keys["public_key"]["n"]
self.plaintext_modulo = self.keys["public_key"]["n"]
self.ciphertext_modulo = self.keys["public_key"]["n"]
self.encrypt_with_public = encrypt_with_public

def generate_keys(self, key_size: int) -> dict:
Expand Down
1 change: 1 addition & 0 deletions lightphe/elliptic/Weierstrass.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def __init__(self, curve="secp256k1"):
if curve == "secp256k1":
self.a = 0
self.b = 7
# modulo
self.p = (
pow(2, 256)
- pow(2, 32)
Expand Down
11 changes: 5 additions & 6 deletions lightphe/models/Homomorphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@


class Homomorphic(ABC):
keys: Optional[dict] = None
modulo: Optional[int] = None
plaintext_modulo: Optional[int] = None
ciphertext_modulo: Optional[int] = None
keys: dict
plaintext_modulo: int
ciphertext_modulo: int

@abstractmethod
def generate_keys(self, key_size: int, s: Optional[int] = None) -> dict:
Expand Down Expand Up @@ -36,7 +35,7 @@ def add(

@abstractmethod
def multiply(
self, ciphertext1: Union[int, tuple], ciphertext2: Union[int, tuple]
self, ciphertext1: Union[int, tuple, list], ciphertext2: Union[int, tuple, list]
) -> Union[int, tuple]:
pass

Expand All @@ -45,7 +44,7 @@ def xor(self, ciphertext1: list, ciphertext2: list) -> list:
pass

@abstractmethod
def multiply_by_contant(self, ciphertext: int, constant: int) -> int:
def multiply_by_contant(self, ciphertext: Union[int, tuple, list], constant: int) -> int:
pass

@abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# pylint: disable=eval-used

logger = Logger()
logger = Logger(module="tests/__init__.py")


class LightPHE:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_benaloh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from lightphe.cryptosystems.Benaloh import Benaloh
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="tests/test_benaloh.py")


def test_benaloh():
Expand Down
18 changes: 13 additions & 5 deletions tests/test_cloud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from lightphe.commons.logger import Logger
from lightphe import LightPHE

logger = Logger()
logger = Logger(module="tests/test_cloud.py")

# pre-generated 20-bit RSA keys
PRIVATE = {"private_key": {"d": 30365}, "public_key": {"n": 175501, "e": 101753}}
Expand All @@ -15,16 +16,14 @@ def test_encryption():
m1 = 10000
c1 = cs.encrypt(m1)

# m2 = 1.05 # TODO: add support
m2 = 5
m2 = 1.05
c2 = cs.encrypt(m2)

assert cs.decrypt(c1) == m1
assert cs.decrypt(c2) == m2

logger.info("✅ Cloud encryption tests done")

c3_val = homomorphic_operations(c1.value, c2.value)
c3_val = homomorphic_operations(c1=c1.value, c2=c2.value)
c3 = cs.create_ciphertext_obj(c3_val)

assert cs.decrypt(c3) == m1 * m2
Expand All @@ -39,6 +38,15 @@ def homomorphic_operations(c1: int, c2: int):
cs = LightPHE(algorithm_name="RSA", keys=PUBLIC)
c1_obj = cs.create_ciphertext_obj(c1)
c2_obj = cs.create_ciphertext_obj(c2)

# one cannot perform decryoption without private key
with pytest.raises(ValueError):
cs.decrypt(c1_obj)

with pytest.raises(ValueError):
cs.decrypt(c2_obj)

# no need private key!
c3 = c1_obj * c2_obj
logger.info("✅ Cloud homomorphic operation tests done")
return c3.value
2 changes: 1 addition & 1 deletion tests/test_damgard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from lightphe.cryptosystems.DamgardJurik import DamgardJurik
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="tests/test_damgard.py")


def test_damgardjurik():
Expand Down
16 changes: 8 additions & 8 deletions tests/test_elgamal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
from lightphe.cryptosystems.ElGamal import ElGamal
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="tests/test_elgamal.py")


def test_elgamal():
eg = ElGamal()

m1 = eg.modulo + 100
m2 = eg.modulo + 150
m1 = eg.plaintext_modulo + 100
m2 = eg.plaintext_modulo + 150

c1 = eg.encrypt(plaintext=m1)
c2 = eg.encrypt(plaintext=m2)
c3 = eg.multiply(c1, c2)

# homomorphic operations
assert eg.decrypt(c3) == (m1 * m2) % eg.modulo
assert eg.decrypt(c3) == (m1 * m2) % eg.plaintext_modulo

# unsupported homomorphic operations
with pytest.raises(ValueError):
Expand Down Expand Up @@ -44,18 +44,18 @@ def test_exponential_elgamal():

logger.debug(additive_eg.keys)

m1 = additive_eg.modulo + 222
m2 = additive_eg.modulo + 111
m1 = additive_eg.plaintext_modulo + 222
m2 = additive_eg.plaintext_modulo + 111

c1 = additive_eg.encrypt(plaintext=m1)
c2 = additive_eg.encrypt(plaintext=m2)
c3 = additive_eg.add(c1, c2)

# homomorphic operations
assert additive_eg.decrypt(c3) == (m1 + m2) % additive_eg.modulo
assert additive_eg.decrypt(c3) == (m1 + m2) % additive_eg.plaintext_modulo
assert (
additive_eg.decrypt(additive_eg.multiply_by_contant(c1, m2))
== (m1 * m2) % additive_eg.modulo
== (m1 * m2) % additive_eg.plaintext_modulo
)

# unsupported homomorphic operations
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ellipticcurveelgamal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from lightphe.cryptosystems.EllipticCurveElGamal import EllipticCurveElGamal
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="tests/test_ellipticcurveelgamal.py")


def test_elliptic_curve_elgamal():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_goldwasser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from lightphe.cryptosystems.GoldwasserMicali import GoldwasserMicali
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="tests/test_goldwasser.py")


def test_goldwasser():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_naccache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from lightphe.cryptosystems.NaccacheStern import NaccacheStern
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="tests/test_naccache.py")


def test_naccache_on_plaintexts():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_okamoto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from lightphe.cryptosystems.OkamotoUchiyama import OkamotoUchiyama
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="tests/test_okamoto.py")


def test_okamoto():
Expand Down
5 changes: 1 addition & 4 deletions tests/test_paillier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from lightphe.cryptosystems.Paillier import Paillier
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="tests/test_paillier.py")


def test_paillier():
Expand Down Expand Up @@ -92,6 +92,3 @@ def test_float_operations():
k3 = -20
assert cs.decrypt(c1 * k3) == (m1 * k3) % cs.cs.plaintext_modulo
assert cs.decrypt(k3 * c1) == (m1 * k3) % cs.cs.plaintext_modulo

# TODO: think and implement this later
_ = -1.05
8 changes: 4 additions & 4 deletions tests/test_rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
from lightphe.commons.logger import Logger
from lightphe import LightPHE

logger = Logger()
logger = Logger(module="tests/test_rsa.py")


def test_rsa():
rsa = RSA()

m1 = rsa.modulo + 9
m2 = rsa.modulo + 11
m1 = rsa.plaintext_modulo + 9
m2 = rsa.plaintext_modulo + 11

c1 = rsa.encrypt(m1)
c2 = rsa.encrypt(m2)
c3 = rsa.multiply(c1, c2)

# homomorphic operations
assert rsa.decrypt(c3) == (m1 * m2) % rsa.modulo
assert rsa.decrypt(c3) == (m1 * m2) % rsa.plaintext_modulo

# unsupported homomorphic operations
with pytest.raises(ValueError):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_salary.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from lightphe.cryptosystems.Paillier import Paillier
from lightphe.commons.logger import Logger

logger = Logger()
logger = Logger(module="tests/test_salary.py")


def test_salary():
Expand Down

0 comments on commit ca9b98d

Please sign in to comment.