swisspost-evoting-go-poc/pkg/zkp/plaintext_equality.go
saymrwulf ec4be74e17 Due-diligence hardening + Rust transport-security layer
Correctness/security review of the whole PoC, with fixes and regression tests.

Cryptographic soundness:
- mixnet: enforce the multi-exponentiation c_{B_m}=commit(0;0) check that was
  stubbed out with an empty if — without it a malicious mixer can prove a
  non-permutation shuffle.
- zkp: derive all four Fiat-Shamir challenges via RecursiveHashToZq instead of
  a biased `hash mod q` (which also capped the challenge space at 256 bits for
  production-sized groups).

Verification honesty:
- protocol: VerifyTally now actually calls zkp.VerifySchnorrProof and returns
  the true aggregate result instead of an unconditional true.
- protocol: persist the padded mix input (event.MixInput) so the verifier checks
  shuffle 0 against the same padding the tally used (fixes false INVALID for N<2).

Other correctness:
- kdf: length-prefix BuildKDFInfo parts so the info encoding is injective.
- math: GqElementFromSquareRoot accepts the valid root q (off-by-one that could
  panic in HashAndSquare); RandomGqElement samples the full canonical range.
- cmd: validate demo --voters/--options instead of panicking on degenerate values.
- protocol: use crypto/rand in the demo driver (drop the last math/rand import).

Transport security (new): pkg/transportsec exposes Ed25519 signatures and X25519
ECDH — implemented in Rust (rust/transportsec: ed25519-dalek, x25519-dalek),
linked into Go via cgo. No RSA. Cross-language conformance test proves the Rust
Ed25519 signatures interoperate with Go's crypto/ed25519. Makefile builds the
Rust static lib before the Go binary.

Tests: added unit/round-trip/tamper coverage for math, hash, elgamal, zkp,
mixnet, kdf, returncodes, protocol (end-to-end), and the Rust FFI bridge.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 14:42:34 +02:00

117 lines
3.6 KiB
Go

package zkp
import (
"github.com/user/evote/pkg/elgamal"
"github.com/user/evote/pkg/hash"
emath "github.com/user/evote/pkg/math"
)
// GenPlaintextEqualityProof generates a proof that two ciphertexts encrypt
// the same plaintext under different keys.
// C1 = Enc(m, r0, h), C2 = Enc(m, r1, h')
func GenPlaintextEqualityProof(
c1, c2 elgamal.Ciphertext,
h, hPrime emath.GqElement,
r0, r1 emath.ZqElement,
group *emath.GqGroup,
auxInfo ...hash.Hashable,
) PlaintextEqualityProof {
zqGroup := emath.ZqGroupFromGqGroup(group)
g := group.Generator()
// 1. Sample random b0, b1
b0 := emath.RandomZqElement(zqGroup)
b1 := emath.RandomZqElement(zqGroup)
// 2. Compute phi(b, h, h') = (g^b0, g^b1, h^b0 / h'^b1)
commit0 := g.Exponentiate(b0)
commit1 := g.Exponentiate(b1)
commit2 := h.Exponentiate(b0).Divide(hPrime.Exponentiate(b1))
commitments := emath.GqVectorOf(commit0, commit1, commit2)
// 3. Statement: y = (gamma1, gamma2, phi1/phi2')
phi1 := c1.GetPhi(0)
phi2 := c2.GetPhi(0)
y := emath.GqVectorOf(c1.Gamma, c2.Gamma, phi1.Divide(phi2))
// 4. Compute challenge
e := plaintextEqualityChallenge(group, h, hPrime, y, commitments, phi1, phi2, zqGroup, auxInfo)
// 5. Response: z0 = b0 + e*r0, z1 = b1 + e*r1
z0 := b0.Add(e.Multiply(r0))
z1 := b1.Add(e.Multiply(r1))
return PlaintextEqualityProof{
E: e,
Z: emath.ZqVectorOf(z0, z1),
}
}
// VerifyPlaintextEqualityProof verifies a plaintext equality proof.
func VerifyPlaintextEqualityProof(
c1, c2 elgamal.Ciphertext,
h, hPrime emath.GqElement,
proof PlaintextEqualityProof,
group *emath.GqGroup,
auxInfo ...hash.Hashable,
) bool {
zqGroup := emath.ZqGroupFromGqGroup(group)
g := group.Generator()
z0 := proof.Z.Get(0)
z1 := proof.Z.Get(1)
// Compute phi(z, h, h') = (g^z0, g^z1, h^z0 / h'^z1)
x0 := g.Exponentiate(z0)
x1 := g.Exponentiate(z1)
x2 := h.Exponentiate(z0).Divide(hPrime.Exponentiate(z1))
// Statement: y = (gamma1, gamma2, phi1/phi2')
phi1 := c1.GetPhi(0)
phi2 := c2.GetPhi(0)
y := emath.GqVectorOf(c1.Gamma, c2.Gamma, phi1.Divide(phi2))
// Reconstruct commitments: c'_i = x_i * y_i^(-e)
negE := proof.E.Negate()
c0 := x0.Multiply(y.Get(0).Exponentiate(negE))
c1Prime := x1.Multiply(y.Get(1).Exponentiate(negE))
c2Prime := x2.Multiply(y.Get(2).Exponentiate(negE))
commitments := emath.GqVectorOf(c0, c1Prime, c2Prime)
// Recompute challenge
ePrime := plaintextEqualityChallenge(group, h, hPrime, y, commitments, phi1, phi2, zqGroup, auxInfo)
return proof.E.Equals(ePrime)
}
func plaintextEqualityChallenge(group *emath.GqGroup, h, hPrime emath.GqElement, y, commitments *emath.GqVector, phi1, phi2 emath.GqElement, zqGroup *emath.ZqGroup, auxInfo []hash.Hashable) emath.ZqElement {
// f = (p, q, g, h, h')
f := hash.HashableList{Elements: []hash.Hashable{
hash.HashableBigInt{Value: group.P()},
hash.HashableBigInt{Value: group.Q()},
hash.HashableBigInt{Value: group.Generator().Value()},
hash.HashableBigInt{Value: h.Value()},
hash.HashableBigInt{Value: hPrime.Value()},
}}
yHash := gqVectorToHashableList(y)
cHash := gqVectorToHashableList(commitments)
// h_aux: ["PlaintextEqualityProof", phi1, phi2] or ["PlaintextEqualityProof", phi1, phi2, i_aux]
auxElements := []hash.Hashable{
hash.HashableString{Value: "PlaintextEqualityProof"},
hash.HashableBigInt{Value: phi1.Value()},
hash.HashableBigInt{Value: phi2.Value()},
}
if len(auxInfo) > 0 {
auxElements = append(auxElements, auxInfo...)
}
hAux := hash.HashableList{Elements: auxElements}
// Uniform Z_q challenge via oversample-then-reduce (Swiss Post spec).
eVal := hash.RecursiveHashToZq(zqGroup.Q(), f, yHash, cHash, hAux)
e, _ := emath.NewZqElement(eVal, zqGroup)
return e
}