Two problems
Splitting seats fairly is a math problem. Votes divide into fractions, but seats are whole. Every rounding rule favours someone. Some rules even take a seat away from a party when the parliament grows (the Alabama paradox).
Storing the result safely is a security problem. A results file is only useful if nobody can quietly change it. Hiding the numbers is not enough; you must also detect edits.
This project solves the first with the Huntington-Hill method, the method the U.S. House of Representatives has used since 1941. It solves the second with AES-256-GCM, which both encrypts the results and refuses to decrypt them if a single bit has changed.
Live demo
Everything below runs in your browser. Nothing is uploaded. The seat calculation is a JavaScript port of src/apportionment.py, tested to give the same answers as the Python code. Encryption uses your browser's built-in Web Crypto AES-256-GCM, in the same byte format as src/crypto.py.
1 · Votes and seats
2 · Result
3 · Encrypt, then try to tamper
How Huntington-Hill works
- Every party with at least one vote gets 1 seat.
- Each party gets a priority score. The next seat goes to the highest score.
- That party's score drops. Repeat until all seats are given out.
Example: A = 5,000, B = 3,000 and C = 2,000 votes share 10 seats. Each starts with 1 seat, so the first scores are A ≈ 3,536, B ≈ 2,121 and C ≈ 1,414. Seven rounds later the result is A 5, B 3, C 2. The full round-by-round table is in docs/HOW_IT_WORKS.md.
Adding a seat never takes one away from a party, so the Alabama paradox cannot happen. Like every method, Huntington-Hill has trade-offs: it can occasionally give a party more or fewer seats than its exact share rounded up or down, and it leans slightly toward small parties compared with Webster's method.
The hard part: encryption that notices edits
The first design encrypted the results with AES-256-CBC. It hid the numbers, but it did not detect tampering. A security audit proved it: without the key, editing the first 16 bytes of a real results.json.enc changed a party's seat count from 2 to 8, and the file still decrypted without any error.
So the project moved to authenticated encryption, AES-256-GCM. GCM adds a 16-byte tag computed from the key and the data. Decryption recomputes it and refuses the file if anything changed. Tests prove it: the same 2 → 8 edit, every single-byte change, flipped bits in each part of the file, cut-off and extended files, and a wrong key are all rejected.
The demo above lets you try the edit yourself.
Security design and its limits
What it does
- AES-256-GCM via the Python
cryptographylibrary. - A fresh random 12-byte nonce for every encryption.
- A 16-byte tag: a wrong key or any changed byte is rejected.
- File format: nonce + ciphertext + tag.
What it does not do (yet)
- The command-line tool saves the key to
key.binnext to the results. Anyone with both files can read them, or re-seal edited ones. - The Flask web app does not encrypt anything.
- The web app has no login, no seat limit and does not escape party names. Run it only on your own machine.
- The tag proves the file was not changed. It does not prove who made it.
Tests and benchmark
403 automated tests run on GitHub Actions for every push (Python 3.10–3.13 and Node.js). 392 are Python tests and 11 are JavaScript tests. They check that:
- The result matches the official 2020 Census apportionment for all 50 states.
- Seats always add up to the total.
- Adding a seat never removes one from any party.
- AES-256-GCM rejects every tampered file in the tests.
- The JavaScript demo gives the same seats as the Python code.
Benchmark (python scripts/benchmark.py), Intel Core i9-12900K, Windows 11, Python 3.14.3, median of 7 repeats:
| Case | Time per call |
|---|---|
| U.S. House 2020 (50 states, 435 seats) | 122 µs |
| 12 parties, 650 seats | 168 µs |
| Same, every vote count ×1000 | 178 µs |
| 1,000 parties, 10,000 seats | 3.3 ms |
| 1,000 parties, 100,000 seats | 34 ms |
| AES-256-GCM encrypt, 785-byte result | 7 µs |
Time grows with the number of seats and parties, not with the number of votes.
Roadmap
- Keep the key out of the results folder: read it from an environment variable or the OS keyring.
- Encrypt results in the web app, not only the command-line tool.
- Harden the web app: cap the seat count, escape party names, turn off debug mode, use a production server.
- Upgrade
cryptographyand Flask, and run a dependency audit in CI. - Sign results so readers can check who produced them.