From 9e8f3b235f5532aa688ef5ed2023f95a47c1c267 Mon Sep 17 00:00:00 2001 From: Steve Maier <82616727+SteveMaier-IRT@users.noreply.github.com> Date: Thu, 30 Jun 2022 12:51:51 -0400 Subject: [PATCH] Change string handling to use begin and end instead of front (#318) * Change string handling to use begin and end instead of front Added unit test to make sure the extended data is not null * Fix multiple .net tests that had intermittent failures because of how c++ was handling data --- .../ElectionGuard.Encryption.Tests/TestChaumPedersen.cs | 4 ++-- .../ElectionGuard.Encryption.Tests/TestElection.cs | 2 +- src/electionguard/encrypt.cpp | 3 +-- test/electionguard/test_encrypt.cpp | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestChaumPedersen.cs b/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestChaumPedersen.cs index f6f9884e..6e8075e4 100644 --- a/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestChaumPedersen.cs +++ b/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestChaumPedersen.cs @@ -10,13 +10,13 @@ public class TestChaumPedersen [Test] public void Test_DisjunctiveChaumPedersen() { - var nonce = Constants.ONE_MOD_Q; + var nonce = new ElementModQ(Constants.ONE_MOD_Q.Data); var keyPair = ElGamalKeyPair.FromSecret(Constants.TWO_MOD_Q); const ulong vote = 0UL; var ciphertext = Elgamal.Encrypt(vote, nonce, keyPair.PublicKey); var proof = new DisjunctiveChaumPedersenProof( - ciphertext, nonce, keyPair.PublicKey, Constants.ONE_MOD_Q, vote); + ciphertext, nonce, keyPair.PublicKey, new ElementModQ(Constants.ONE_MOD_Q.Data), vote); Assert.That(proof.IsValid(ciphertext, keyPair.PublicKey, Constants.ONE_MOD_Q)); } diff --git a/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestElection.cs b/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestElection.cs index 65ad7ef3..88b5ec13 100644 --- a/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestElection.cs +++ b/bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestElection.cs @@ -27,7 +27,7 @@ public void Test_Can_Create_Party() "2022-holidays", new InternationalizedText(new[] { candidateName }), "new-years-id", - null, + string.Empty, false); diff --git a/src/electionguard/encrypt.cpp b/src/electionguard/encrypt.cpp index 31136a1b..e6aa92a3 100644 --- a/src/electionguard/encrypt.cpp +++ b/src/electionguard/encrypt.cpp @@ -510,8 +510,7 @@ namespace electionguard make_unique(*sharedNonce->clone(), "constant-extended-data"); auto extendedDataNonce = noncesForExtendedData->get(0); - vector extendedData_plaintext( - (uint8_t *)&extendedData.front(), (uint8_t *)&extendedData.front() + extendedData.size()); + vector extendedData_plaintext(extendedData.begin(), extendedData.end()); // Perform HashedElGamalCiphertext calculation unique_ptr hashedElGamal = diff --git a/test/electionguard/test_encrypt.cpp b/test/electionguard/test_encrypt.cpp index ea5a8535..37ef23ed 100644 --- a/test/electionguard/test_encrypt.cpp +++ b/test/electionguard/test_encrypt.cpp @@ -141,6 +141,7 @@ TEST_CASE("Encrypt PlaintextBallot with EncryptionMediator against constructed " // Assert CHECK(ciphertext->isValidEncryption(*context->getManifestHash(), *keypair->getPublicKey(), *context->getCryptoExtendedBaseHash()) == true); + CHECK(ciphertext->getContests().front().get().getHashedElGamalCiphertext().get() != nullptr); } TEST_CASE("Encrypt PlaintextBallot undervote succeeds") @@ -490,7 +491,6 @@ TEST_CASE("Verify placeholder flag") auto ciphertext = encryptBallot(*ballot, *internal, *context, *device->getHash()); // Assert - // TODO: compare other values CHECK( ciphertext->getContests().front().get().getSelections().front().get().getIsPlaceholder() == false);