⚖️
Huntington-Hill · AES-256-GCM · Python + Flask

Secure Apportionment
System

Splits parliament seats with the Huntington-Hill method and seals the results with authenticated encryption (AES-256-GCM).

Directed by Alborz Nazari ✦ Built with an AI coding assistant ✦ Source on GitHub
Try the live demo View the code
435 / 4352020 U.S. House seats reproduced exactly
122 µsto apportion the House (50 states, 435 seats)
403automated tests (Python + JavaScript)
0tampered files accepted in tests
§ I

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.

§ II

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

Press “Allocate seats” to see the result.

3 · Encrypt, then try to tamper

A new 256-bit key is generated in this page and never leaves it. Output = nonce (12 bytes) + ciphertext + tag (16 bytes).
—
Try it: Decrypt (works) → Change first party's seats (an attacker edits the file) → Decrypt again (refused: that is the point) → Undo change.
§ III

How Huntington-Hill works

  1. Every party with at least one vote gets 1 seat.
  2. Each party gets a priority score. The next seat goes to the highest score.
  3. That party's score drops. Repeat until all seats are given out.
Priority = votes / √(n × (n + 1)) n = seats the party holds now

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.

§ IV

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.

§ V

Security design and its limits

🔐

What it does

  • AES-256-GCM via the Python cryptography library.
  • 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.bin next 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.
§ VI

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:

CaseTime per call
U.S. House 2020 (50 states, 435 seats)122 µs
12 parties, 650 seats168 µs
Same, every vote count ×1000178 µs
1,000 parties, 10,000 seats3.3 ms
1,000 parties, 100,000 seats34 ms
AES-256-GCM encrypt, 785-byte result7 µs

Time grows with the number of seats and parties, not with the number of votes.

§ VII

Roadmap

  1. Keep the key out of the results folder: read it from an environment variable or the OS keyring.
  2. Encrypt results in the web app, not only the command-line tool.
  3. Harden the web app: cap the seat count, escape party names, turn off debug mode, use a production server.
  4. Upgrade cryptography and Flask, and run a dependency audit in CI.
  5. Sign results so readers can check who produced them.

Read the code

Source, tests, benchmark and the plain-language guide are on GitHub.

View on GitHub How it works