From 76407c61433ebbebaa6ee785d2e70b60691b937f Mon Sep 17 00:00:00 2001 From: Satya Date: Mon, 5 Oct 2026 00:04:04 +0800 Subject: [PATCH 01/12] private-voting: encrypted ballots with a homomorphic tally (fixes #7) Ballots were Poseidon(vote, nullifier) with a public nullifier, so every vote could be decoded from chain data. Replace them with exponential ElGamal on Jubjub under an election key shared n-of-n by three trustees (ADR-0005). The ballot proof shows eligibility, the nullifier, and that (A, B) encrypts 0 or 1; the sum of all ballots is decrypted once, after an on-chain deadline, with a Groth16 DLEQ proof per trustee share. Also closes binding gaps found while redesigning: - G1: election id, voter root and election key are script parameters - G2: the list key is the nullifier; the stored ballot is the proved one; exactly one unit of the nullifier token may exist in the outputs - G3: an insert spends exactly one list node; node values are exact - G4: ballots must be valid no later than the voting deadline - G5: the list root is one-shot (seed output) The tally walks the list from its root and fails closed, publishes a ballot-set digest, and re-verifies the manifest, script hashes, key proofs, share proofs and the total from public data. Tests: host ElGamal vs an independent Python reference, circuit negatives, Plutus VM mutations for every gap, DevKit E2E election. Builds against a local ZeroJ 0.1.0-pre13 snapshot (bloxbean/zeroj#73). Co-Authored-By: Claude Opus 5.5 --- .../0005-private-ballot-homomorphic-tally.md | 365 +++++++++++ docs/adr/0006-pedersen-commitment-demos.md | 307 +++++++++ private-voting/README.md | 136 +++- private-voting/build.gradle | 2 + private-voting/frontend/src/App.svelte | 138 ++-- .../voting/circuit/JubjubElGamalGadget.java | 111 ++++ .../voting/circuit/PrivateBallotProof.java | 77 +++ .../voting/circuit/PrivateVoteProof.java | 56 -- .../voting/circuit/TrusteeShareProof.java | 43 ++ .../voting/controller/ElectionController.java | 81 ++- .../voting/controller/VoteController.java | 136 ++-- .../usecases/voting/crypto/JubjubElGamal.java | 171 +++++ .../usecases/voting/onchain/VoteListLib.java | 138 +++- .../voting/onchain/VoteListValidator.java | 14 +- .../voting/onchain/VoteZkMintingPolicy.java | 147 ++++- .../voting/service/ElectionService.java | 167 +++-- .../usecases/voting/service/KeyedCircuit.java | 127 ++++ .../voting/service/LocalJulcEvaluator.java | 22 +- .../voting/service/OnChainVoteService.java | 619 +++++++++--------- .../voting/service/ProofCompressor.java | 70 -- .../usecases/voting/service/TallyService.java | 207 +++++- .../voting/service/VoteCircuitService.java | 245 +++---- .../src/main/resources/application.yml | 7 +- .../resources/static/assets/index-BgvPT-Ps.js | 2 - .../resources/static/assets/index-DLI6Fmnh.js | 7 + ...{index-pLm8h1ey.css => index-DmcXqzQy.css} | 2 +- .../src/main/resources/static/index.html | 4 +- .../voting/PrivateVotingYaciE2ETest.java | 153 +++++ .../zeroj/usecases/voting/VotingFixture.java | 90 +++ .../voting/circuit/BallotCircuitTest.java | 202 ++++++ .../voting/crypto/JubjubElGamalTest.java | 156 +++++ .../voting/onchain/VoteScriptsVmTest.java | 359 ++++++++++ .../elgamal-reference/elgamal_reference.py | 100 +++ .../elgamal-reference/reference-output.txt | 35 + version.properties | 2 +- 35 files changed, 3694 insertions(+), 804 deletions(-) create mode 100644 docs/adr/0005-private-ballot-homomorphic-tally.md create mode 100644 docs/adr/0006-pedersen-commitment-demos.md create mode 100644 private-voting/src/main/java/com/bloxbean/cardano/zeroj/usecases/voting/circuit/JubjubElGamalGadget.java create mode 100644 private-voting/src/main/java/com/bloxbean/cardano/zeroj/usecases/voting/circuit/PrivateBallotProof.java delete mode 100644 private-voting/src/main/java/com/bloxbean/cardano/zeroj/usecases/voting/circuit/PrivateVoteProof.java create mode 100644 private-voting/src/main/java/com/bloxbean/cardano/zeroj/usecases/voting/circuit/TrusteeShareProof.java create mode 100644 private-voting/src/main/java/com/bloxbean/cardano/zeroj/usecases/voting/crypto/JubjubElGamal.java create mode 100644 private-voting/src/main/java/com/bloxbean/cardano/zeroj/usecases/voting/service/KeyedCircuit.java delete mode 100644 private-voting/src/main/java/com/bloxbean/cardano/zeroj/usecases/voting/service/ProofCompressor.java delete mode 100644 private-voting/src/main/resources/static/assets/index-BgvPT-Ps.js create mode 100644 private-voting/src/main/resources/static/assets/index-DLI6Fmnh.js rename private-voting/src/main/resources/static/assets/{index-pLm8h1ey.css => index-DmcXqzQy.css} (92%) create mode 100644 private-voting/src/test/java/com/bloxbean/cardano/zeroj/usecases/voting/PrivateVotingYaciE2ETest.java create mode 100644 private-voting/src/test/java/com/bloxbean/cardano/zeroj/usecases/voting/VotingFixture.java create mode 100644 private-voting/src/test/java/com/bloxbean/cardano/zeroj/usecases/voting/circuit/BallotCircuitTest.java create mode 100644 private-voting/src/test/java/com/bloxbean/cardano/zeroj/usecases/voting/crypto/JubjubElGamalTest.java create mode 100644 private-voting/src/test/java/com/bloxbean/cardano/zeroj/usecases/voting/onchain/VoteScriptsVmTest.java create mode 100644 private-voting/src/test/resources/elgamal-reference/elgamal_reference.py create mode 100644 private-voting/src/test/resources/elgamal-reference/reference-output.txt diff --git a/docs/adr/0005-private-ballot-homomorphic-tally.md b/docs/adr/0005-private-ballot-homomorphic-tally.md new file mode 100644 index 0000000..6d4c5a1 --- /dev/null +++ b/docs/adr/0005-private-ballot-homomorphic-tally.md @@ -0,0 +1,365 @@ +# ADR-0005: Private ballots with a homomorphic tally (private-voting) + +- **Status:** Proposed. Implementation is on `feat/pedersen-private-ballot-and-demos`. +- **Date:** 2026-10-04 (r2: amended after the design review) +- **Fixes:** [zeroj-usecases#7](https://github.com/bloxbean/zeroj-usecases/issues/7) +- **Related:** ZeroJ ADR-0051 (Pedersen commitment profiles; finding F7, scope item D8), + ZeroJ ADR-0037/0038 (Jubjub gadget hardening), [ADR-0006](0006-pedersen-commitment-demos.md). +- **Risk:** R2. This is a protocol integration with on-chain binding. It uses + only primitives ZeroJ already implements and tests: Jubjub arithmetic, Poseidon, Groth16, and + the Plutus V3 Groth16 verifier. No new primitive is implemented. The ballot and decryption relations are new + to this repository, though, so they are specified exactly here and reviewed like R3 relations. + +## Context + +The demo claims that "the proof reveals nothing about which voter cast the vote or how they +voted". Issue #7 shows the claim is false for the vote. The ballot is `Poseidon(vote, nullifier)` +and the nullifier is public. Since `vote ∈ {0, 1}`, anyone can hash both candidates and compare. +`TallyService` does exactly this to count votes. + +Re-reading the on-chain path while designing the fix found four more gaps. Each one breaks the +tally's integrity, and each would also break a homomorphic tally: + +| # | Gap | Consequence | +|---|---|---| +| G1 | `VoteZkMintingPolicy` takes `electionId`, `voterRoot` and `commitment` from the redeemer and checks them against nothing. | A proof against an attacker's own voter tree is accepted. Eligibility is not enforced, and one person can cast any number of ballots. | +| G2 | The list node's key, and the ballot stored in its datum, are not tied to the proved nullifier and commitment. | A voter can mint the nullifier token again and insert it under a fresh list key, and so vote twice. The stored ballot need not be the proved one. | +| G3 | The list spend validator only requires the list policy to run. An insert checks the anchor, but not other spent list nodes. | An insert transaction can also spend other voters' nodes, then drop or rewrite their ballots. | +| G4 | No voting deadline. | Ballots can be added at any time. With a decryptable tally, decrypting before and after a ballot reveals that ballot. | +| G5 | `InitList` is not one-shot. | A second root starts a parallel list in which the same nullifier can be inserted again. | + +## Threat model and trust assumptions + +**Adversaries.** + +| Adversary | Goal | Capabilities | +|---|---|---| +| Malicious voter | Vote twice; cast an invalid ballot (a value other than 0 or 1, or a different key); replace or remove other ballots; learn other voters' votes. | Builds arbitrary transactions; mints its own nullifier token outside the list; sends junk to the list address. | +| Outsider | Same goals, without being eligible. | — | +| Public observer | Learn individual votes from chain data. | — | +| Up to n − 1 colluding trustees | Decrypt individual ballots; publish a wrong total. | — | +| Chain reorganisation | Change the ballot set after the tally. | — | + +**Trusted.** +- At least one trustee is honest and available. +- The Groth16 setups of both circuits are honest. With the dev setup this demo uses, whoever + holds the toxic waste can forge every proof, including a proof of possession for a rogue key. +- The election administrator publishes the manifest (below) and deploys scripts that match it. + Anyone can check this. +- Plutus V3 and the ledger enforce validity intervals and minting as specified. + +**Not trusted.** Redeemer contents, every output that a validator has not checked, and every +UTxO at the list address that does not satisfy the node rules below. + +## Decision + +Replace the ballot with **exponential ElGamal over the Jubjub prime-order subgroup** under an +election key that is **additively shared by n trustees**. Ballots are proved well-formed with +Groth16 and verified on-chain. Their sum is decrypted **once, after an on-chain deadline and +finality**. Each trustee proves its decryption share, and anyone can check the tally from chain +data and the election manifest. This is the Cramer–Gennaro–Schoenmakers construction [CGS97] +with n-of-n sharing in place of threshold sharing. + +The ballot's second component, `B = [v]·G + [k]·PK`, is a Pedersen commitment to the vote whose +blinding base is the election key. Nobody can open a single ballot. The trustees can jointly +remove the blinding from the *sum*, because they hold the key behind that base. + +### Notation + +- `𝔾` is the Jubjub prime-order subgroup, of order `l`. `G` is `JubjubPoint.SUBGROUP_GENERATOR`, + the value base of `pedersen-jubjub-v1`. `p` is the BLS12-381 scalar field. +- Random scalars are 64 bytes reduced mod `l` (`PedersenCommitment.randomBlinding`, as in ZeroJ + ADR-0051 D2). +- `I2OSP32(x)` is the 32-byte big-endian encoding. + +### Election key + +Each trustee `j ∈ {1..n}` (the demo uses `n = 3`): + +1. samples `sk_j ∈ [1, l)` and publishes `PK_j = [sk_j]·G`; +2. publishes a **key proof** `π_key_j`, a Groth16 proof of `R_dleq` (below) with `X = G` and + `D = P = PK_j`. This proves knowledge of `sk_j` (proof of possession). The verifier always + sets `X = G` itself, so a decryption-share proof cannot pass as a key proof. + +The election key is `PK = Σ_j PK_j`. It is accepted only if every `π_key_j` verifies, the +`PK_j` are pairwise distinct, and neither any `PK_j` nor `PK` is the identity. Proof of +possession blocks the rogue-key attack: a trustee who publishes `PK_n = [x]·G − Σ_{j