From 895782543c9581c59c2db71db793be13e4e5d1e9 Mon Sep 17 00:00:00 2001 From: saymrwulf Date: Mon, 6 Jul 2026 15:42:29 +0200 Subject: [PATCH] Pin plaintext-equality proof soundness with a correct-usage test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The plaintext-equality primitive itself is sound: statement (γ1, γ2, φ1/φ2) with witnesses (r0, r1) correctly proves two ciphertexts encrypt the same plaintext under different keys. Finding F5 was a caller error in the old vote path, not a defect here. This test pins the correct contract (accepts equal plaintexts, rejects different plaintexts and mismatched aux) so the upcoming cast-as-intended return-code path can rely on it. Co-Authored-By: Claude Fable 5 --- pkg/zkp/plaintext_equality_test.go | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 pkg/zkp/plaintext_equality_test.go diff --git a/pkg/zkp/plaintext_equality_test.go b/pkg/zkp/plaintext_equality_test.go new file mode 100644 index 0000000..615ba16 --- /dev/null +++ b/pkg/zkp/plaintext_equality_test.go @@ -0,0 +1,52 @@ +package zkp + +import ( + "testing" + + "github.com/user/evote/pkg/elgamal" + "github.com/user/evote/pkg/hash" + emath "github.com/user/evote/pkg/math" +) + +func hashStr(s string) hash.HashableString { return hash.HashableString{Value: s} } + +// TestPlaintextEqualityProofSound confirms the proof primitive is sound when +// used correctly: it accepts two single-component ciphertexts that encrypt the +// SAME plaintext under different public keys, and rejects when the plaintexts +// differ. (Finding F5 was a caller error in the old vote path, not a defect in +// this primitive — this test pins the correct contract.) +func TestPlaintextEqualityProofSound(t *testing.T) { + group := testGroup(t) + zq := emath.ZqGroupFromGqGroup(group) + g := group.Generator() + + // Two independent public keys h = g^x1, h' = g^x2. + h := g.Exponentiate(emath.RandomZqElement(zq)) + hPrime := g.Exponentiate(emath.RandomZqElement(zq)) + pk1 := elgamal.PublicKey{Elements: emath.GqVectorOf(h)} + pk2 := elgamal.PublicKey{Elements: emath.GqVectorOf(hPrime)} + + m := emath.RandomGqElement(group) + r0 := emath.RandomZqElement(zq) + r1 := emath.RandomZqElement(zq) + c1 := elgamal.Encrypt(elgamal.NewMessage(emath.GqVectorOf(m)), r0, pk1) + c2 := elgamal.Encrypt(elgamal.NewMessage(emath.GqVectorOf(m)), r1, pk2) + + proof := GenPlaintextEqualityProof(c1, c2, h, hPrime, r0, r1, group) + if !VerifyPlaintextEqualityProof(c1, c2, h, hPrime, proof, group) { + t.Fatal("honest same-plaintext proof rejected") + } + + // Different plaintext in c2 must fail. + m2 := m.Multiply(g) + c2bad := elgamal.Encrypt(elgamal.NewMessage(emath.GqVectorOf(m2)), r1, pk2) + if VerifyPlaintextEqualityProof(c1, c2bad, h, hPrime, proof, group) { + t.Fatal("proof verified against a different plaintext") + } + + // Aux-info binding: mismatched aux must fail. + p2 := GenPlaintextEqualityProof(c1, c2, h, hPrime, r0, r1, group, hashStr("ctx-A")) + if VerifyPlaintextEqualityProof(c1, c2, h, hPrime, p2, group, hashStr("ctx-B")) { + t.Fatal("proof verified under mismatched aux info") + } +}