Skip to content

Rust - #2

Open
liamlundy wants to merge 3 commits into
masterfrom
rust
Open

Rust#2
liamlundy wants to merge 3 commits into
masterfrom
rust

Conversation

@liamlundy

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-benchmark and Rust benchmarks via criterion, plus a convenience scripts/bench.sh.
  • Updated Python packaging/config to support benchmarks and a src-python module 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_bytes currently 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 the assert plain_text < n in encrypt_value or (if the assert is removed) silently encrypt m mod n and 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_bytes should 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 thread src/rsa.rs
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 thread src/rsa.rs
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 thread src/rsa.rs
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 thread scripts/bench.sh
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 thread Cargo.toml
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 thread benches/rsa_bench.rs
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants