Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a Rust implementation of the RSA toy project alongside the existing Python implementation, and introduces benchmarking support for both languages.
Changes:
- Added Rust RSA + prime generation + modular inverse implementations with a Clap-based CLI.
- Added benchmarking tooling: Python benchmarks via
pytest-benchmarkand Rust benchmarks viacriterion, plus a conveniencescripts/bench.sh. - Updated Python packaging/config to support benchmarks and a
src-pythonmodule root.
Reviewed changes
Copilot reviewed 12 out of 21 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| uv.lock | Adds locked dev dependencies for pytest-benchmark (and py-cpuinfo). |
| test/utils/test_rabin_miller.py | Removes a stray blank line in imports. |
| src/rsa.rs | Introduces Rust RSA keygen/encrypt/decrypt helpers (chunked byte encryption). |
| src/rabin_miller.rs | Adds Rust Miller–Rabin primality test + prime generation utilities. |
| src/modular_arithmetic.rs | Adds Rust extended-Euclid modular inverse implementation. |
| src/main.rs | Adds Rust CLI for key generation and file encrypt/decrypt. |
| src/codecs.rs | Adds Rust int/bytes conversion helpers. |
| src-python/rsa/utils/rabin_miller.py | Adds Python Miller–Rabin + prime generation utilities. |
| src-python/rsa/utils/modular_arithmatic.py | Adds Python modular inverse utility (note: misspelled module name). |
| src-python/rsa/utils/codecs.py | Adds Python int/bytes conversion utilities. |
| src-python/rsa/utils/init.py | Initializes Python utils package. |
| src-python/rsa/rsa.py | Adjusts Python RSA chunk encryption to pad ciphertext chunks. |
| src-python/rsa/console.py | Adds Python Click CLI for key generation and file encrypt/decrypt. |
| src-python/rsa/init.py | Initializes Python rsa package. |
| scripts/bench.sh | Adds a script to run Python and Rust benchmarks. |
| pyproject.toml | Adds pytest-benchmark, pytest discovery config, and uv module root settings. |
| Cargo.toml | Adds Rust crate definition + dependencies + Criterion benchmark target. |
| Cargo.lock | Adds Rust dependency lockfile. |
| benches/rsa_bench.rs | Adds Criterion benchmarks for Rust RSA and prime generation. |
| bench/bench_rsa.py | Adds pytest-benchmark benchmarks for Python RSA and prime generation. |
| .gitignore | Cleans up .coverage entry and ignores Rust /target. |
Comments suppressed due to low confidence (1)
src-python/rsa/rsa.py:112
encrypt_bytescurrently chunks plaintext at the full modulus byte length (chunk_size = ceil(n_bits/8)). For larger inputs this can produce chunks whose integer value is >= n, which will either fail theassert plain_text < ninencrypt_valueor (if the assert is removed) silently encryptm mod nand break round-tripping. Fix by using a smaller plaintext chunk size (commonly modulus_len_bytes - 1) and keeping ciphertext blocks padded to the full modulus length;decrypt_bytesshould then re-pad intermediate decrypted chunks to preserve leading 0x00 bytes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+52
to
+60
| pub fn encrypt_bytes(plain_bytes: &[u8], key: &BigUint, n: &BigUint) -> Vec<u8> { | ||
| let chunk_size = (n.bits() as usize + 7) / 8; | ||
| let mut encrypted = Vec::new(); | ||
| for chunk in plain_bytes.chunks(chunk_size) { | ||
| let enc = encrypt_chunk(chunk, key, n, chunk_size); | ||
| encrypted.extend_from_slice(&enc); | ||
| } | ||
| encrypted | ||
| } |
Comment on lines
+62
to
+70
| pub fn decrypt_bytes(encrypted_bytes: &[u8], key: &BigUint, n: &BigUint) -> Vec<u8> { | ||
| let chunk_size = (n.bits() as usize + 7) / 8; | ||
| let mut decrypted = Vec::new(); | ||
| for chunk in encrypted_bytes.chunks(chunk_size) { | ||
| let dec = decrypt_chunk(chunk, key, n); | ||
| decrypted.extend_from_slice(&dec); | ||
| } | ||
| decrypted | ||
| } |
Comment on lines
+11
to
+13
| let p = choose_prime(key_size / 2); | ||
| let q = choose_prime(key_size / 2); | ||
| let n = &p * &q; |
Comment on lines
+4
to
+9
| echo "=== Python benchmarks (pytest-benchmark) ===" | ||
| uv run pytest bench/ --benchmark-only --benchmark-sort=mean "$@" | ||
|
|
||
| echo "" | ||
| echo "=== Rust benchmarks (criterion) ===" | ||
| cargo bench "$@" |
Comment on lines
+6
to
+11
| [dependencies] | ||
| num-bigint = { version = "0.4", features = ["rand"] } | ||
| num-integer = "0.1" | ||
| num-traits = "0.2" | ||
| rand = "0.8" | ||
| clap = { version = "4", features = ["derive"] } |
Comment on lines
+5
to
+13
| // Pull in the library modules directly from src/ | ||
| #[path = "../src/codecs.rs"] | ||
| mod codecs; | ||
| #[path = "../src/modular_arithmetic.rs"] | ||
| mod modular_arithmetic; | ||
| #[path = "../src/rabin_miller.rs"] | ||
| mod rabin_miller; | ||
| #[path = "../src/rsa.rs"] | ||
| mod rsa; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.