From 58902b72e84a4803b41ace14916d50675a084f43 Mon Sep 17 00:00:00 2001 From: Piotr Roslaniec Date: Fri, 3 Nov 2023 12:16:23 +0100 Subject: [PATCH] chore(linter): add ruff linter --- .github/workflows/workspace.yml | 6 +- ferveo-python/ferveo/__init__.pyi | 245 +++++++---------------- ferveo-python/pyproject.toml | 2 + ferveo-python/setup.py | 8 +- ferveo-python/test/test_ferveo.py | 21 +- ferveo-python/test/test_serialization.py | 2 +- 6 files changed, 96 insertions(+), 188 deletions(-) diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index c8d43415..709f4080 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -176,7 +176,7 @@ jobs: working-directory: ferveo-python - name: Install pip dependencies - run: pip install pytest mypy + run: pip install pytest mypy ruff - name: Run pytest run: pytest @@ -186,6 +186,10 @@ jobs: run: python -m mypy.stubtest ferveo working-directory: ferveo-python + - name: Run ruff + run: ruff check . + working-directory: ferveo-python + codecov: runs-on: ubuntu-latest needs: [ test ] diff --git a/ferveo-python/ferveo/__init__.pyi b/ferveo-python/ferveo/__init__.pyi index 77c1746e..a7c5abb6 100644 --- a/ferveo-python/ferveo/__init__.pyi +++ b/ferveo-python/ferveo/__init__.pyi @@ -1,121 +1,78 @@ from typing import Sequence, final - @final class Keypair: @staticmethod - def random() -> Keypair: - ... - + def random() -> Keypair: ... @staticmethod - def from_secure_randomness(secure_randomness: bytes) -> Keypair: - ... - + def from_secure_randomness(secure_randomness: bytes) -> Keypair: ... @staticmethod - def secure_randomness_size() -> int: - ... - + def secure_randomness_size() -> int: ... @staticmethod - def from_bytes(data: bytes) -> Keypair: - ... - - def __bytes__(self) -> bytes: - ... - - def public_key(self) -> FerveoPublicKey: - ... - + def from_bytes(data: bytes) -> Keypair: ... + def __bytes__(self) -> bytes: ... + def public_key(self) -> FerveoPublicKey: ... @final class FerveoPublicKey: @staticmethod - def from_bytes(data: bytes) -> FerveoPublicKey: - ... - - def __bytes__(self) -> bytes: - ... - - def __hash__(self) -> int: - ... - + def from_bytes(data: bytes) -> FerveoPublicKey: ... + def __bytes__(self) -> bytes: ... + def __hash__(self) -> int: ... @staticmethod - def serialized_size() -> int: - ... - - def __eq__(self, other: object) -> bool: - ... - + def serialized_size() -> int: ... + def __eq__(self, other: object) -> bool: ... @final class Validator: - - def __init__(self, address: str, public_key: FerveoPublicKey): - ... + def __init__(self, address: str, public_key: FerveoPublicKey): ... address: str public_key: FerveoPublicKey - @final class Transcript: @staticmethod - def from_bytes(data: bytes) -> Transcript: - ... - - def __bytes__(self) -> bytes: - ... - + def from_bytes(data: bytes) -> Transcript: ... + def __bytes__(self) -> bytes: ... @final class DkgPublicKey: @staticmethod - def from_bytes(data: bytes) -> DkgPublicKey: - ... - - def __bytes__(self) -> bytes: - ... - + def from_bytes(data: bytes) -> DkgPublicKey: ... + def __bytes__(self) -> bytes: ... @staticmethod - def serialized_size() -> int: - ... - + def serialized_size() -> int: ... @final class ValidatorMessage: - def __init__( - self, - validator: Validator, - transcript: Transcript, - ): - ... + self, + validator: Validator, + transcript: Transcript, + ): ... validator: Validator transcript: Transcript - @final class Dkg: - def __init__( - self, - tau: int, - shares_num: int, - security_threshold: int, - validators: Sequence[Validator], - me: Validator, - ): - ... + self, + tau: int, + shares_num: int, + security_threshold: int, + validators: Sequence[Validator], + me: Validator, + ): ... public_key: DkgPublicKey - def generate_transcript(self) -> Transcript: - ... - - def aggregate_transcripts(self, messages: Sequence[ValidatorMessage]) -> AggregatedTranscript: - ... - + def generate_transcript(self) -> Transcript: ... + def aggregate_transcripts( + self, messages: Sequence[ValidatorMessage] + ) -> AggregatedTranscript: ... @final class Ciphertext: @@ -123,184 +80,120 @@ class Ciphertext: payload: bytes @staticmethod - def from_bytes(data: bytes) -> Ciphertext: - ... - - def __bytes__(self) -> bytes: - ... - + def from_bytes(data: bytes) -> Ciphertext: ... + def __bytes__(self) -> bytes: ... @final class CiphertextHeader: @staticmethod - def from_bytes(data: bytes) -> CiphertextHeader: - ... - - def __bytes__(self) -> bytes: - ... - + def from_bytes(data: bytes) -> CiphertextHeader: ... + def __bytes__(self) -> bytes: ... @final class DecryptionShareSimple: @staticmethod - def from_bytes(data: bytes) -> DecryptionShareSimple: - ... - - def __bytes__(self) -> bytes: - ... - + def from_bytes(data: bytes) -> DecryptionShareSimple: ... + def __bytes__(self) -> bytes: ... @final class DecryptionSharePrecomputed: @staticmethod - def from_bytes(data: bytes) -> DecryptionSharePrecomputed: - ... - - def __bytes__(self) -> bytes: - ... - + def from_bytes(data: bytes) -> DecryptionSharePrecomputed: ... + def __bytes__(self) -> bytes: ... @final class AggregatedTranscript: - - def __init__(self, messages: Sequence[ValidatorMessage]): - ... - - def verify(self, shares_num: int, messages: Sequence[ValidatorMessage]) -> bool: - ... - + def __init__(self, messages: Sequence[ValidatorMessage]): ... + def verify(self, shares_num: int, messages: Sequence[ValidatorMessage]) -> bool: ... def create_decryption_share_simple( - self, - dkg: Dkg, - ciphertext_header: CiphertextHeader, - aad: bytes, - validator_keypair: Keypair - ) -> DecryptionShareSimple: - ... - + self, + dkg: Dkg, + ciphertext_header: CiphertextHeader, + aad: bytes, + validator_keypair: Keypair, + ) -> DecryptionShareSimple: ... def create_decryption_share_precomputed( - self, - dkg: Dkg, - ciphertext_header: CiphertextHeader, - aad: bytes, - validator_keypair: Keypair - ) -> DecryptionSharePrecomputed: - ... - + self, + dkg: Dkg, + ciphertext_header: CiphertextHeader, + aad: bytes, + validator_keypair: Keypair, + ) -> DecryptionSharePrecomputed: ... @staticmethod - def from_bytes(data: bytes) -> AggregatedTranscript: - ... - - def __bytes__(self) -> bytes: - ... - + def from_bytes(data: bytes) -> AggregatedTranscript: ... + def __bytes__(self) -> bytes: ... @final class SharedSecret: - @staticmethod - def from_bytes(data: bytes) -> SharedSecret: - ... - - def __bytes__(self) -> bytes: - ... - + def from_bytes(data: bytes) -> SharedSecret: ... + def __bytes__(self) -> bytes: ... @final class FerveoVariant: Simple: FerveoVariant Precomputed: FerveoVariant - def __eq__(self, other: object) -> bool: - ... - - def __hash__(self) -> int: - ... - - -def encrypt(message: bytes, aad: bytes, dkg_public_key: DkgPublicKey) -> Ciphertext: - ... - + def __eq__(self, other: object) -> bool: ... + def __hash__(self) -> int: ... +def encrypt(message: bytes, aad: bytes, dkg_public_key: DkgPublicKey) -> Ciphertext: ... def combine_decryption_shares_simple( - decryption_shares: Sequence[DecryptionShareSimple], -) -> SharedSecret: - ... - - + decryption_shares: Sequence[DecryptionShareSimple], +) -> SharedSecret: ... def combine_decryption_shares_precomputed( - decryption_shares: Sequence[DecryptionSharePrecomputed], -) -> SharedSecret: - ... - - + decryption_shares: Sequence[DecryptionSharePrecomputed], +) -> SharedSecret: ... def decrypt_with_shared_secret( - ciphertext: Ciphertext, - aad: bytes, - shared_secret: SharedSecret, -) -> bytes: - ... - + ciphertext: Ciphertext, + aad: bytes, + shared_secret: SharedSecret, +) -> bytes: ... class ThresholdEncryptionError(Exception): pass - class InvalidDkgStateToDeal(Exception): pass - class InvalidDkgStateToAggregate(Exception): pass - class InvalidDkgStateToVerify(Exception): pass - class InvalidDkgStateToIngest(Exception): pass - class DealerNotInValidatorSet(Exception): pass - class UnknownDealer(Exception): pass - class DuplicateDealer(Exception): pass - class InvalidPvssTranscript(Exception): pass - class InsufficientTranscriptsForAggregate(Exception): pass - class InvalidDkgPublicKey(Exception): pass - class InsufficientValidators(Exception): pass - class InvalidTranscriptAggregate(Exception): pass - class ValidatorsNotSorted(Exception): pass - class ValidatorPublicKeyMismatch(Exception): pass - class SerializationError(Exception): pass diff --git a/ferveo-python/pyproject.toml b/ferveo-python/pyproject.toml index f92911b2..610492e4 100644 --- a/ferveo-python/pyproject.toml +++ b/ferveo-python/pyproject.toml @@ -1,4 +1,6 @@ [build-system] requires = ["setuptools", "wheel", "setuptools-rust"] +[tool.ruff] +exclude = ["ferveo/__init__.py"] diff --git a/ferveo-python/setup.py b/ferveo-python/setup.py index 25e9d1de..1c8ea4fb 100644 --- a/ferveo-python/setup.py +++ b/ferveo-python/setup.py @@ -2,6 +2,7 @@ from setuptools_rust import Binding, RustExtension from pathlib import Path + this_directory = Path(__file__).parent long_description = (this_directory / "README.md").read_text() @@ -14,11 +15,12 @@ author="Piotr Roslaniec", author_email="p.roslaniec@gmail.com", url="https://github.com/nucypher/ferveo/tree/master/ferveo-python", - rust_extensions=[RustExtension( - "ferveo._ferveo", binding=Binding.PyO3, debug=False)], + rust_extensions=[ + RustExtension("ferveo._ferveo", binding=Binding.PyO3, debug=False) + ], packages=["ferveo"], package_data={ - 'ferveo': ['py.typed', '__init__.pyi'], + "ferveo": ["py.typed", "__init__.pyi"], }, # rust extensions are not zip safe, just like C-extensions. zip_safe=False, diff --git a/ferveo-python/test/test_ferveo.py b/ferveo-python/test/test_ferveo.py index 42a82a84..6f00b6df 100644 --- a/ferveo-python/test/test_ferveo.py +++ b/ferveo-python/test/test_ferveo.py @@ -9,10 +9,9 @@ Validator, ValidatorMessage, Dkg, - AggregatedTranscript, DkgPublicKey, ThresholdEncryptionError, - FerveoVariant + FerveoVariant, ) @@ -114,19 +113,27 @@ def scenario_for_variant(variant: FerveoVariant, shares_num, threshold, shares_t def test_simple_tdec_has_enough_messages(): - scenario_for_variant(FerveoVariant.Simple, shares_num=4, threshold=3, shares_to_use=3) + scenario_for_variant( + FerveoVariant.Simple, shares_num=4, threshold=3, shares_to_use=3 + ) def test_simple_tdec_doesnt_have_enough_messages(): - scenario_for_variant(FerveoVariant.Simple, shares_num=4, threshold=3, shares_to_use=2) + scenario_for_variant( + FerveoVariant.Simple, shares_num=4, threshold=3, shares_to_use=2 + ) def test_precomputed_tdec_has_enough_messages(): - scenario_for_variant(FerveoVariant.Precomputed, shares_num=4, threshold=4, shares_to_use=4) + scenario_for_variant( + FerveoVariant.Precomputed, shares_num=4, threshold=4, shares_to_use=4 + ) def test_precomputed_tdec_doesnt_have_enough_messages(): - scenario_for_variant(FerveoVariant.Precomputed, shares_num=4, threshold=4, shares_to_use=3) + scenario_for_variant( + FerveoVariant.Precomputed, shares_num=4, threshold=4, shares_to_use=3 + ) PARAMS = [ @@ -143,7 +150,7 @@ def test_precomputed_tdec_doesnt_have_enough_messages(): ] TEST_CASES_WITH_THRESHOLD_RANGE = [] -for (shares_num, variant) in PARAMS: +for shares_num, variant in PARAMS: for threshold in range(1, shares_num): TEST_CASES_WITH_THRESHOLD_RANGE.append((variant, shares_num, threshold)) diff --git a/ferveo-python/test/test_serialization.py b/ferveo-python/test/test_serialization.py index 8533d437..57032c6a 100644 --- a/ferveo-python/test/test_serialization.py +++ b/ferveo-python/test/test_serialization.py @@ -5,7 +5,6 @@ DkgPublicKey, FerveoPublicKey, FerveoVariant, - SharedSecret, ) @@ -54,6 +53,7 @@ def make_pk(): # # assert shared_secret == deserialized # assert serialized == bytes(deserialized) + def test_keypair_serialization(): keypair = Keypair.random() serialized = bytes(keypair)