Skip to content

Commit 7dc350f

Browse files
andrew000vsoftco
andauthored
Beautify code (#97)
* Move project to `uv` Add typing Add pre-commit Reformat code Make code more secure Update workflow python_simplified.yml Update workflow python_detailed.yml Signed-off-by: andrew000 <[email protected]> * Replace `pipe operator` to `Union`, to support py3.9 Bump deps Signed-off-by: andrew000 <[email protected]> * Resolve conflicts Bump deps Signed-off-by: andrew000 <[email protected]> * Fix `_fields_` in `Signature` Add stream handler to logger Signed-off-by: andrew000 <[email protected]> * Add stream handler to logger in rand.py and kem.py Signed-off-by: andrew000 <[email protected]> * Add stream handler to logger in sig.py Signed-off-by: andrew000 <[email protected]> * Updated examples * Bump `ruff`, `0.9.3` -> `0.9.4` Signed-off-by: andrew000 <[email protected]> * Change type hint of `_fields_` from `list` to `Sequence` Signed-off-by: andrew000 <[email protected]> * Added COM812 to tools.ruff.lint.ignore Signed-off-by: Vlad Gheorghiu <[email protected]> --------- Signed-off-by: andrew000 <[email protected]> Signed-off-by: Vlad Gheorghiu <[email protected]> Co-authored-by: Vlad Gheorghiu <[email protected]>
1 parent 720f2cc commit 7dc350f

File tree

17 files changed

+717
-356
lines changed

17 files changed

+717
-356
lines changed

.github/workflows/python_detailed.yml

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ name: GitHub actions detailed
22

33
on:
44
push:
5-
branches: ["**"]
5+
branches: [ "**" ]
66
pull_request:
7-
branches: ["**"]
7+
branches: [ "**" ]
88
repository_dispatch:
9-
types: ["**"]
9+
types: [ "**" ]
1010

1111
permissions:
1212
contents: read
@@ -20,21 +20,24 @@ jobs:
2020
build:
2121
strategy:
2222
matrix:
23-
os: [ubuntu-latest, macos-latest, windows-latest]
23+
os: [ ubuntu-latest, macos-latest, windows-latest ]
2424
runs-on: ${{ matrix.os }}
2525

2626
steps:
27-
- uses: actions/checkout@v3
27+
- uses: actions/checkout@v4
2828

29-
- name: Set up Python 3.10
30-
uses: actions/setup-python@v3
29+
- name: Install uv
30+
uses: astral-sh/setup-uv@v5
3131
with:
32-
python-version: "3.10"
32+
version: "latest"
33+
enable-cache: true
34+
cache-dependency-glob: "**/pyproject.toml"
35+
36+
- name: Set up Python 3.9
37+
run: uv python install 3.9
3338

3439
- name: Install dependencies
35-
run: |
36-
python -m pip install --upgrade pip
37-
pip install nose2
40+
run: uv sync --extra dev
3841

3942
- name: Install liboqs POSIX
4043
if: matrix.os != 'windows-latest'
@@ -47,17 +50,17 @@ jobs:
4750
- name: Run examples POSIX
4851
if: matrix.os != 'windows-latest'
4952
run: |
50-
pip install .
51-
python examples/kem.py
53+
uv sync --extra dev
54+
uv run examples/kem.py
5255
echo
53-
python examples/sig.py
56+
uv run examples/sig.py
5457
echo
55-
python examples/rand.py
58+
uv run examples/rand.py
5659
5760
- name: Run unit tests POSIX
5861
if: matrix.os != 'windows-latest'
5962
run: |
60-
nose2 --verbose
63+
uv run nose2 --verbose
6164
6265
- name: Install liboqs Windows
6366
if: matrix.os == 'windows-latest'
@@ -73,16 +76,16 @@ jobs:
7376
shell: cmd
7477
run: |
7578
set PATH=%PATH%;${{env.WIN_LIBOQS_INSTALL_PATH}}\bin
76-
pip install .
77-
python examples/kem.py
79+
uv sync --extra dev
80+
uv run examples/kem.py
7881
echo.
79-
python examples/sig.py
82+
uv run examples/sig.py
8083
echo.
81-
python examples/rand.py
84+
uv run examples/rand.py
8285
8386
- name: Run unit tests Windows
8487
shell: cmd
8588
if: matrix.os == 'windows-latest'
8689
run: |
8790
set PATH=%PATH%;${{env.WIN_LIBOQS_INSTALL_PATH}}\bin
88-
nose2 --verbose
91+
uv run nose2 --verbose

.github/workflows/python_simplified.yml

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ name: GitHub actions simplified
22

33
on:
44
push:
5-
branches: ["**"]
5+
branches: [ "**" ]
66
pull_request:
7-
branches: ["**"]
7+
branches: [ "**" ]
88
repository_dispatch:
9-
types: ["**"]
9+
types: [ "**" ]
1010

1111
permissions:
1212
contents: read
@@ -15,25 +15,29 @@ jobs:
1515
build:
1616
strategy:
1717
matrix:
18-
os: [ubuntu-latest, macos-latest, windows-latest]
18+
os: [ ubuntu-latest, macos-latest, windows-latest ]
1919
runs-on: ${{ matrix.os }}
2020

2121
steps:
22-
- uses: actions/checkout@v3
22+
- uses: actions/checkout@v4
2323

24-
- name: Set up Python 3.10
25-
uses: actions/setup-python@v3
24+
- name: Install uv
25+
uses: astral-sh/setup-uv@v5
2626
with:
27-
python-version: "3.10"
27+
version: "latest"
28+
enable-cache: true
29+
cache-dependency-glob: "**/pyproject.toml"
30+
31+
- name: Set up Python 3.9
32+
run: uv python install 3.9
2833

2934
- name: Run examples
3035
run: |
31-
python -m pip install --upgrade pip
32-
pip install .
33-
python examples/kem.py
34-
python examples/sig.py
35-
python examples/rand.py
36+
uv sync --extra dev
37+
uv run examples/kem.py
38+
uv run examples/sig.py
39+
uv run examples/rand.py
3640
3741
- name: Run unit tests
3842
run: |
39-
nose2 --verbose
43+
uv run nose2 --verbose

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ pip-selfcheck.json
117117
pyvenv.cfg
118118

119119
# vim
120-
*.swp
120+
*.swp
121+
/uv.lock

.pre-commit-config.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
fail_fast: false
2+
repos:
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v5.0.0
5+
hooks:
6+
- id: "trailing-whitespace"
7+
- id: "check-case-conflict"
8+
- id: "check-merge-conflict"
9+
- id: "debug-statements"
10+
- id: "end-of-file-fixer"
11+
- id: "mixed-line-ending"
12+
args: [ "--fix", "crlf" ]
13+
types:
14+
- python
15+
- yaml
16+
- toml
17+
- text
18+
- id: "detect-private-key"
19+
- id: "check-yaml"
20+
- id: "check-toml"
21+
- id: "check-json"
22+
23+
- repo: https://github.com/charliermarsh/ruff-pre-commit
24+
rev: v0.9.4
25+
hooks:
26+
- id: ruff
27+
args: [ "--fix" ]
28+
files: "oqs"
29+
30+
- id: ruff-format
31+
files: "oqs"
32+
33+
- repo: https://github.com/pycqa/isort
34+
rev: 5.13.2
35+
hooks:
36+
- id: isort
37+
name: isort (python)
38+
files: "oqs"

Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Code checker/formatter
2+
#
3+
# Pre-requisites
4+
#
5+
# isort
6+
# mypy
7+
# ruff
8+
# uv
9+
10+
src-dir = oqs
11+
tests-dir = tests
12+
examples-dir = examples
13+
14+
.PHONY lint:
15+
lint:
16+
echo "Running ruff..."
17+
uv run ruff check --config pyproject.toml --diff $(src-dir) $(tests-dir) $(examples-dir)
18+
19+
.PHONY format:
20+
format:
21+
echo "Running ruff check with --fix..."
22+
uv run ruff check --config pyproject.toml --fix --unsafe-fixes $(src-dir) $(tests-dir) $(examples-dir)
23+
24+
echo "Running ruff..."
25+
uv run ruff format --config pyproject.toml $(src-dir) $(tests-dir) $(examples-dir)
26+
27+
echo "Running isort..."
28+
uv run isort --settings-file pyproject.toml $(src-dir) $(tests-dir) $(examples-dir)
29+
30+
.PHONE mypy:
31+
mypy:
32+
echo "Running MyPy..."
33+
uv run mypy --config-file pyproject.toml $(src-dir)
34+
35+
.PHONY outdated:
36+
outdated:
37+
uv tree --outdated --universal
38+
39+
.PHONY sync:
40+
sync:
41+
uv sync --extra dev --extra lint

examples/__init__.py

Whitespace-only changes.

examples/kem.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
# Key encapsulation Python example
22

3+
import logging
4+
from pprint import pformat
5+
from sys import stdout
6+
37
import oqs
4-
from pprint import pprint
58

6-
print("liboqs version:", oqs.oqs_version())
7-
print("liboqs-python version:", oqs.oqs_python_version())
8-
print("Enabled KEM mechanisms:")
9-
kems = oqs.get_enabled_kem_mechanisms()
10-
pprint(kems, compact=True)
9+
logger = logging.getLogger(__name__)
10+
logger.setLevel(logging.INFO)
11+
logger.addHandler(logging.StreamHandler(stdout))
12+
13+
logger.info("liboqs version: %s", oqs.oqs_version())
14+
logger.info("liboqs-python version: %s", oqs.oqs_python_version())
15+
logger.info(
16+
"Enabled KEM mechanisms:\n%s",
17+
pformat(oqs.get_enabled_kem_mechanisms(), compact=True),
18+
)
1119

1220
# Create client and server with sample KEM mechanisms
1321
kemalg = "ML-KEM-512"
1422
with oqs.KeyEncapsulation(kemalg) as client:
1523
with oqs.KeyEncapsulation(kemalg) as server:
16-
print("\nKey encapsulation details:")
17-
pprint(client.details)
24+
logger.info("Key encapsulation details:\n%s", pformat(client.details))
1825

1926
# Client generates its keypair
2027
public_key_client = client.generate_keypair()
@@ -31,6 +38,7 @@
3138
# The client decapsulates the server's ciphertext to obtain the shared secret
3239
shared_secret_client = client.decap_secret(ciphertext)
3340

34-
print(
35-
"\nShared secretes coincide:", shared_secret_client == shared_secret_server
36-
)
41+
logger.info(
42+
"Shared secretes coincide: %s",
43+
shared_secret_client == shared_secret_server,
44+
)

examples/rand.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
# Various RNGs Python example
22

3+
import logging
34
import platform # to learn the OS we're on
5+
from sys import stdout
6+
47
import oqs.rand as oqsrand # must be explicitly imported
5-
from oqs import oqs_version, oqs_python_version
8+
from oqs import oqs_python_version, oqs_version
9+
10+
logger = logging.getLogger(__name__)
11+
logger.setLevel(logging.INFO)
12+
logger.addHandler(logging.StreamHandler(stdout))
613

7-
print("liboqs version:", oqs_version())
8-
print("liboqs-python version:", oqs_python_version())
14+
logger.info("liboqs version: %s", oqs_version())
15+
logger.info("liboqs-python version: %s", oqs_python_version())
916

1017
oqsrand.randombytes_switch_algorithm("system")
11-
print(
12-
"{:17s}".format("System (default):"),
13-
" ".join("{:02X}".format(x) for x in oqsrand.randombytes(32)),
18+
logger.info(
19+
"System (default): %s",
20+
" ".join(f"{x:02X}" for x in oqsrand.randombytes(32)),
1421
)
1522

1623
# We do not yet support OpenSSL under Windows
1724
if platform.system() != "Windows":
1825
oqsrand.randombytes_switch_algorithm("OpenSSL")
19-
print(
20-
"{:17s}".format("OpenSSL:"),
21-
" ".join("{:02X}".format(x) for x in oqsrand.randombytes(32)),
26+
logger.info(
27+
"OpenSSL: %s",
28+
" ".join(f"{x:02X}" for x in oqsrand.randombytes(32)),
2229
)

examples/sig.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
11
# Signature Python example
22

3+
import logging
4+
from pprint import pformat
5+
from sys import stdout
6+
37
import oqs
4-
from pprint import pprint
58

6-
print("liboqs version:", oqs.oqs_version())
7-
print("liboqs-python version:", oqs.oqs_python_version())
8-
print("Enabled signature mechanisms:")
9-
sigs = oqs.get_enabled_sig_mechanisms()
10-
pprint(sigs, compact=True)
9+
logger = logging.getLogger(__name__)
10+
logger.setLevel(logging.INFO)
11+
logger.addHandler(logging.StreamHandler(stdout))
12+
13+
logger.info("liboqs version: %s", oqs.oqs_version())
14+
logger.info("liboqs-python version: %s", oqs.oqs_python_version())
15+
logger.info(
16+
"Enabled signature mechanisms:\n%s",
17+
pformat(oqs.get_enabled_sig_mechanisms(), compact=True),
18+
)
1119

12-
message = "This is the message to sign".encode()
20+
message = b"This is the message to sign"
1321

1422
# Create signer and verifier with sample signature mechanisms
1523
sigalg = "ML-DSA-44"
16-
with oqs.Signature(sigalg) as signer:
17-
with oqs.Signature(sigalg) as verifier:
18-
print("\nSignature details:")
19-
pprint(signer.details)
24+
with oqs.Signature(sigalg) as signer, oqs.Signature(sigalg) as verifier:
25+
logger.info("Signature details:\n%s", pformat(signer.details))
2026

21-
# Signer generates its keypair
22-
signer_public_key = signer.generate_keypair()
23-
# Optionally, the secret key can be obtained by calling export_secret_key()
24-
# and the signer can later be re-instantiated with the key pair:
25-
# secret_key = signer.export_secret_key()
27+
# Signer generates its keypair
28+
signer_public_key = signer.generate_keypair()
29+
# Optionally, the secret key can be obtained by calling export_secret_key()
30+
# and the signer can later be re-instantiated with the key pair:
31+
# secret_key = signer.export_secret_key()
2632

27-
# Store key pair, wait... (session resumption):
28-
# signer = oqs.Signature(sigalg, secret_key)
33+
# Store key pair, wait... (session resumption):
34+
# signer = oqs.Signature(sigalg, secret_key)
2935

30-
# Signer signs the message
31-
signature = signer.sign(message)
36+
# Signer signs the message
37+
signature = signer.sign(message)
3238

33-
# Verifier verifies the signature
34-
is_valid = verifier.verify(message, signature, signer_public_key)
39+
# Verifier verifies the signature
40+
is_valid = verifier.verify(message, signature, signer_public_key)
3541

36-
print("\nValid signature?", is_valid)
42+
logger.info("Valid signature? %s", is_valid)

0 commit comments

Comments
 (0)