Skip to content

Commit 1550029

Browse files
authored
Split encrypt/decrypt test (#408)
1 parent 8568fbb commit 1550029

File tree

10 files changed

+148
-132
lines changed

10 files changed

+148
-132
lines changed

poetry.lock

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ eth-typing = "^5.2.1"
5757

5858
[tool.poetry.group.test.dependencies]
5959
pytest = "^8.4.0"
60-
pytest-cov = {version = "^6.2.1", python = "^3.9"}
60+
pytest-cov = "^6.2.1"
6161

6262
[tool.poetry.scripts]
6363
eciespy = "ecies.__main__:main"

tests/crypt/__init__.py

Whitespace-only changes.

tests/crypt/helper.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from contextlib import contextmanager
2+
3+
4+
from ecies import ECIES_CONFIG
5+
from ecies.config import EllipticCurve
6+
7+
8+
@contextmanager
9+
def config_manager(curve: EllipticCurve):
10+
_curve = ECIES_CONFIG.elliptic_curve
11+
ECIES_CONFIG.elliptic_curve = curve
12+
yield
13+
ECIES_CONFIG.elliptic_curve = _curve
File renamed without changes.

tests/crypt/test_error.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
3+
from ecies import decrypt, encrypt
4+
from ecies.keys import PrivateKey
5+
6+
from .helper import config_manager
7+
8+
9+
@pytest.mark.parametrize("curve", ["secp256k1", "x25519"])
10+
def test_encrypt_error(curve, data):
11+
with pytest.raises(TypeError):
12+
encrypt(1, data) # type: ignore
13+
14+
15+
@pytest.mark.parametrize("curve", ["secp256k1", "x25519"])
16+
def test_decrypt_error(curve, data):
17+
pk_hex = PrivateKey(curve).public_key.to_bytes(True).hex()
18+
with config_manager(curve):
19+
encrypted = encrypt(bytes.fromhex(pk_hex), data)
20+
with pytest.raises(TypeError):
21+
decrypt(1, encrypted) # type: ignore
File renamed without changes.

tests/crypt/test_known.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
3+
from ecies import ECIES_CONFIG, decrypt, encrypt
4+
from ecies.utils import decode_hex
5+
6+
from .helper import config_manager
7+
8+
9+
def __check_known(sk: str, pk: str, data: bytes, encrypted: bytes):
10+
assert encrypt(pk, data) != encrypted
11+
assert decrypt(sk, encrypted) == data
12+
13+
14+
@pytest.mark.parametrize("curve", ["secp256k1", "x25519"])
15+
def test_sym_config(curve, data):
16+
ECIES_CONFIG.symmetric_algorithm = "xchacha20"
17+
if curve == "secp256k1":
18+
sk = "0000000000000000000000000000000000000000000000000000000000000002"
19+
pk = "02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5"
20+
encrypted = decode_hex(
21+
"04e314abc14398e07974cd50221b682ed5f0629e977345fc03e2047208ee6e279f"
22+
"fb2a6942878d3798c968d89e59c999e082b0598d1b641968c48c8d47c570210d0a"
23+
"b1ade95eeca1080c45366562f9983faa423ee3fd3260757053d5843c5f453e1ee6"
24+
"bb955c8e5d4aee8572139357a091909357a8931b"
25+
)
26+
elif curve == "x25519":
27+
sk = "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a"
28+
pk = "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a"
29+
encrypted = decode_hex(
30+
"cfff9c146116355d0e7ce81df984b4d64c5e5c9c055fbfda0ff8169e11d05e12ed"
31+
"f025069032adf3e16b763d886f3812bc8f1902fd29204ed3b6a2ea4e52a01dc440"
32+
"72ed1635aefbad1571bd5b972a7304ba25301f12"
33+
)
34+
else:
35+
raise NotImplementedError
36+
37+
with config_manager(curve):
38+
__check_known(sk, pk, data, encrypted)
39+
40+
ECIES_CONFIG.symmetric_algorithm = "aes-256-gcm"

tests/crypt/test_random.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pytest
2+
3+
from ecies import ECIES_CONFIG, decrypt, encrypt
4+
from ecies.config import EllipticCurve
5+
from ecies.keys import PrivateKey
6+
from ecies.utils import decode_hex
7+
8+
from .helper import config_manager
9+
10+
11+
def __check_random(data: bytes, k: PrivateKey, compressed: bool = False):
12+
sk_hex = k.to_hex()
13+
pk_hex = k.public_key.to_bytes(compressed).hex()
14+
assert data == decrypt(sk_hex, encrypt(pk_hex, data))
15+
assert data == decrypt(decode_hex(sk_hex), encrypt(decode_hex(pk_hex), data))
16+
17+
18+
@pytest.mark.parametrize(
19+
"curve,compressed",
20+
[
21+
("secp256k1", False),
22+
("secp256k1", True),
23+
("x25519", False),
24+
("x25519", True),
25+
],
26+
)
27+
def test_elliptic_ok(data, curve: EllipticCurve, compressed: bool):
28+
with config_manager(curve):
29+
__check_random(data, PrivateKey(curve), compressed)
30+
31+
32+
@pytest.mark.parametrize("curve", ["secp256k1", "x25519"])
33+
def test_hkdf_config(curve, data):
34+
ECIES_CONFIG.is_hkdf_key_compressed = True
35+
with config_manager(curve):
36+
__check_random(data, PrivateKey(curve))
37+
ECIES_CONFIG.is_hkdf_key_compressed = False
38+
39+
40+
@pytest.mark.parametrize("curve", ["secp256k1", "x25519"])
41+
def test_ephemeral_key_config(curve, data):
42+
ECIES_CONFIG.is_ephemeral_key_compressed = True
43+
with config_manager(curve):
44+
__check_random(data, PrivateKey(curve))
45+
ECIES_CONFIG.is_ephemeral_key_compressed = False
46+
47+
48+
@pytest.mark.parametrize("curve", ["secp256k1", "x25519"])
49+
def test_aes_nonce_config(curve, data):
50+
ECIES_CONFIG.symmetric_nonce_length = 12
51+
with config_manager(curve):
52+
__check_random(data, PrivateKey(curve))
53+
ECIES_CONFIG.symmetric_nonce_length = 16

tests/test_crypt.py

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)