Skip to content

Commit

Permalink
Fix/integration fixes (#278)
Browse files Browse the repository at this point in the history
* version number updated

* adding [] functionality to linked list class

* change ballot_code to code and add spec_version to serializing submitted ballots

* create manifest that can set name and contact information in c#

* Add comments for the new [] operator and new manifest ctor

Co-authored-by: Steve Maier <ysfred4@hotmail.com>
  • Loading branch information
SteveMaier-IRT and YsFred4 authored May 13, 2022
1 parent 97e13e7 commit 32b5589
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(META_PROJECT_EXPORT "ElectionGuard")
set(META_PROJECT_TARGET "electionguard")
set(META_VERSION_MAJOR "0")
set(META_VERSION_MINOR "1")
set(META_VERSION_PATCH "11")
set(META_VERSION_PATCH "12")
set(META_VERSION "${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH}")

set(LIBRARY_PUBLIC_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public void Test_Can_Create_Linked_list()
// Assert
Assert.That(firstValue == "value");
Assert.That(secondValue == "thing");
Assert.That(list["some"] == "value");
Assert.That(list["another"] == "thing");
Assert.That(list["notthere"] == null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ public unsafe string GetValueAt(ulong position)
return Marshal.PtrToStringAnsi(value);
}


/// <summary>
/// Get the value using the designated key
/// </summary>
public unsafe string this[string searchKey]
{
get
{
var count = NativeInterface.LinkedList.GetCount(Handle);
for (ulong position = 0; position < count; position++)
{
var status = NativeInterface.LinkedList.GetElementAt(
Handle, position, out IntPtr key, out IntPtr value);
status.ThrowIfError();
if (Marshal.PtrToStringAnsi(key) == searchKey)
return Marshal.PtrToStringAnsi(value);
}
return null;
}
}

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
protected override unsafe void DisposeUnmanaged()
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<!-- Project -->
<RootNamespace>ElectionGuard</RootNamespace>
<AssemblyName>ElectionGuard.Encryption</AssemblyName>
<Version>0.1.11</Version>
<AssemblyVersion>0.1.11.0</AssemblyVersion>
<AssemblyFileVersion>0.1.11.0</AssemblyFileVersion>
<Version>0.1.12</Version>
<AssemblyVersion>0.1.12.0</AssemblyVersion>
<AssemblyFileVersion>0.1.12.0</AssemblyFileVersion>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -19,7 +19,7 @@
<Title>ElectionGuard Encryption</Title>
<Description>Open source implementation of ElectionGuard's ballot encryption.</Description>
<Authors>Microsoft</Authors>
<PackageVersion>0.1.11</PackageVersion>
<PackageVersion>0.1.12</PackageVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/microsoft/electionguard-cpp</PackageProjectUrl>
<RepositoryUrl>https://github.com/microsoft/electionguard-cpp</RepositoryUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,79 @@ public unsafe Manifest(
}
}

/// <summary>
/// Creates a `Manifest` object
/// </summary>
/// <param name="electionScopeId"></param>
/// <param name="electionType">election type</param>
/// <param name="startDate">start date for election</param>
/// <param name="endDate">end data for the election</param>
/// <param name="gpUnits">array of the `GeopoliticalUnit` for election</param>
/// <param name="parties">array of the `Party` for election</param>
/// <param name="candidates">array of the `Candidate` for election</param>
/// <param name="contests">array of the `ContestDescription` for election</param>
/// <param name="ballotStyles">array of the `BallotStyle` for election</param>
/// <param name="name">name of the election</param>
/// <param name="contact">contact information for the election</param>
public unsafe Manifest(
string electionScopeId, ElectionType electionType,
DateTime startDate, DateTime endDate,
GeopoliticalUnit[] gpUnits, Party[] parties,
Candidate[] candidates, ContestDescription[] contests,
BallotStyle[] ballotStyles, InternationalizedText name, ContactInformation contact)
{
IntPtr[] gpUnitPointers = new IntPtr[gpUnits.Length];
for (var i = 0; i < gpUnits.Length; i++)
{
gpUnitPointers[i] = gpUnits[i].Handle.Ptr;
gpUnits[i].Dispose();
}

IntPtr[] partyPointers = new IntPtr[parties.Length];
for (var i = 0; i < parties.Length; i++)
{
partyPointers[i] = parties[i].Handle.Ptr;
parties[i].Dispose();
}

IntPtr[] candidatePointers = new IntPtr[candidates.Length];
for (var i = 0; i < candidates.Length; i++)
{
candidatePointers[i] = candidates[i].Handle.Ptr;
candidates[i].Dispose();
}

IntPtr[] contestPointers = new IntPtr[contests.Length];
for (var i = 0; i < contests.Length; i++)
{
contestPointers[i] = contests[i].Handle.Ptr;
contests[i].Dispose();
}

IntPtr[] ballotStylePointers = new IntPtr[ballotStyles.Length];
for (var i = 0; i < ballotStyles.Length; i++)
{
ballotStylePointers[i] = ballotStyles[i].Handle.Ptr;
ballotStyles[i].Dispose();
}

var status = NativeInterface.Manifest.New(
electionScopeId, electionType, name.Handle,
(ulong)new DateTimeOffset(startDate).ToUnixTimeMilliseconds(),
(ulong)new DateTimeOffset(endDate).ToUnixTimeMilliseconds(),
gpUnitPointers, (ulong)gpUnitPointers.LongLength,
partyPointers, (ulong)partyPointers.LongLength,
candidatePointers, (ulong)candidatePointers.LongLength,
contestPointers, (ulong)contestPointers.LongLength,
ballotStylePointers, (ulong)ballotStylePointers.LongLength,
contact.Handle,
out Handle);
if (status != Status.ELECTIONGUARD_STATUS_SUCCESS)
{
throw new ElectionGuardException($"Manifest Error Status: {status}");
}
}

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
protected override unsafe void DisposeUnmanaged()
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,31 @@ internal static extern Status New(
ulong ballotStylesSize,
out ManifestHandle handle);

// TODO: eg_election_manifest_new_with_contact
[DllImport(DllName, EntryPoint = "eg_election_manifest_new_with_contact",
CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern Status New(
[MarshalAs(UnmanagedType.LPStr)] string electionScopeId,
ElectionType electionType,
InternationalizedText.InternationalizedTextHandle name,
ulong startDate,
ulong endDate,
// TODO ISSUE #212: type safety
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] gpUnits,
ulong gpUnitsSize,
// TODO ISSUE #212: type safety
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] parties,
ulong partiesSize,
// TODO ISSUE #212: type safety
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] candidates,
ulong candidatesSize,
// TODO ISSUE #212: type safety
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] contests,
ulong contestSize,
// TODO ISSUE #212: type safety
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] ballotStyles,
ulong ballotStylesSize,
ContactInformation.ContactInformationHandle contact,
out ManifestHandle handle);

[DllImport(DllName, EntryPoint = "eg_election_manifest_free",
CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
Expand Down
33 changes: 17 additions & 16 deletions src/electionguard/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ namespace electionguard
serialized.push_back({"contact_information", contactInformation});
}

serialized.push_back({"spec_version", "1.0"});

return serialized;
}

Expand Down Expand Up @@ -1059,7 +1061,7 @@ namespace electionguard
{"manifest_hash", serializable.getManifestHash()->toHex()},
{"code_seed", serializable.getBallotCodeSeed()->toHex()},
{"contests", contests},
{"ballot_code", serializable.getBallotCode()->toHex()},
{"code", serializable.getBallotCode()->toHex()},
{"timestamp", serializable.getTimestamp()},
{"crypto_hash", serializable.getCryptoHash()->toHex()},
};
Expand All @@ -1074,7 +1076,7 @@ namespace electionguard
auto style_id = j["style_id"].get<string>();
auto manifest_hash = j["manifest_hash"].get<string>();
auto code_seed = j["code_seed"].get<string>();
auto ballot_code = j["ballot_code"].get<string>();
auto ballot_code = j["code"].get<string>();
auto timestamp = j["timestamp"].get<uint64_t>();
auto ballot_nonce = j["nonce"].is_null() ? "" : j["nonce"].get<string>();
auto crypto_hash = j["crypto_hash"].get<string>();
Expand Down Expand Up @@ -1265,15 +1267,15 @@ namespace electionguard
{

auto result = SubmittedBallotWrapper::fromObjectWrapper(serializable);
result["state"] = getBallotBoxStateString(serializable.getState());
result["state"] = serializable.getState();

return result;
}

static unique_ptr<electionguard::SubmittedBallot> toObject(json j)
{
auto ciphertext = SubmittedBallotWrapper::toObjectWrapper(j);
auto state = getBallotBoxState(j["state"].get<string>());
auto state = electionguard::BallotBoxState(j["state"].get<uint64_t>());
// TODO: make this a move instead of a copy
return electionguard::SubmittedBallot::from(*ciphertext, state);
}
Expand Down Expand Up @@ -1327,30 +1329,29 @@ namespace electionguard
json plaintext =
CompactPlaintextBallotWrapper::fromObjectWrapper(*serializable.getPlaintext());

json result = {
{"plaintext", plaintext},
{"code_seed", serializable.getBallotCodeSeed()->toHex()},
{"ballot_code", serializable.getBallotCode()->toHex()},
{"nonce", serializable.getNonce()->toHex()},
{"timestamp", serializable.getTimestamp()},
{"ballot_box_state", getBallotBoxStateString(serializable.getBallotBoxState())}};
json result = {{"plaintext", plaintext},
{"code_seed", serializable.getBallotCodeSeed()->toHex()},
{"code", serializable.getBallotCode()->toHex()},
{"nonce", serializable.getNonce()->toHex()},
{"timestamp", serializable.getTimestamp()},
{"ballot_box_state", serializable.getBallotBoxState()}};
return result;
}
static unique_ptr<electionguard::CompactCiphertextBallot> toObject(json j)
{

auto codeSeed = j["code_seed"].get<string>();
auto ballotCode = j["ballot_code"].get<string>();
auto ballotCode = j["code"].get<string>();
auto nonce = j["nonce"].get<string>();
auto timestamp = j["timestamp"].get<uint64_t>();
auto ballotBoxState = j["ballot_box_state"].get<string>();
auto ballotBoxState =
electionguard::BallotBoxState(j["ballot_box_state"].get<uint64_t>());

auto plaintext = CompactPlaintextBallotWrapper::toObjectWrapper(j["plaintext"]);

return make_unique<electionguard::CompactCiphertextBallot>(
move(plaintext), getBallotBoxState(ballotBoxState),
ElementModQ::fromHex(codeSeed), ElementModQ::fromHex(ballotCode), timestamp,
ElementModQ::fromHex(nonce));
move(plaintext), ballotBoxState, ElementModQ::fromHex(codeSeed),
ElementModQ::fromHex(ballotCode), timestamp, ElementModQ::fromHex(nonce));
}

public:
Expand Down

0 comments on commit 32b5589

Please sign in to comment.