Skip to content

Commit 32b5589

Browse files
Fix/integration fixes (#278)
* 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 <[email protected]>
1 parent 97e13e7 commit 32b5589

File tree

7 files changed

+144
-22
lines changed

7 files changed

+144
-22
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set(META_PROJECT_EXPORT "ElectionGuard")
77
set(META_PROJECT_TARGET "electionguard")
88
set(META_VERSION_MAJOR "0")
99
set(META_VERSION_MINOR "1")
10-
set(META_VERSION_PATCH "11")
10+
set(META_VERSION_PATCH "12")
1111
set(META_VERSION "${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH}")
1212

1313
set(LIBRARY_PUBLIC_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestCollections.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public void Test_Can_Create_Linked_list()
2323
// Assert
2424
Assert.That(firstValue == "value");
2525
Assert.That(secondValue == "thing");
26+
Assert.That(list["some"] == "value");
27+
Assert.That(list["another"] == "thing");
28+
Assert.That(list["notthere"] == null);
2629
}
2730
}
2831
}

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/Collections.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ public unsafe string GetValueAt(ulong position)
7777
return Marshal.PtrToStringAnsi(value);
7878
}
7979

80+
81+
/// <summary>
82+
/// Get the value using the designated key
83+
/// </summary>
84+
public unsafe string this[string searchKey]
85+
{
86+
get
87+
{
88+
var count = NativeInterface.LinkedList.GetCount(Handle);
89+
for (ulong position = 0; position < count; position++)
90+
{
91+
var status = NativeInterface.LinkedList.GetElementAt(
92+
Handle, position, out IntPtr key, out IntPtr value);
93+
status.ThrowIfError();
94+
if (Marshal.PtrToStringAnsi(key) == searchKey)
95+
return Marshal.PtrToStringAnsi(value);
96+
}
97+
return null;
98+
}
99+
}
100+
80101
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
81102
protected override unsafe void DisposeUnmanaged()
82103
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/ElectionGuard.Encryption.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<!-- Project -->
88
<RootNamespace>ElectionGuard</RootNamespace>
99
<AssemblyName>ElectionGuard.Encryption</AssemblyName>
10-
<Version>0.1.11</Version>
11-
<AssemblyVersion>0.1.11.0</AssemblyVersion>
12-
<AssemblyFileVersion>0.1.11.0</AssemblyFileVersion>
10+
<Version>0.1.12</Version>
11+
<AssemblyVersion>0.1.12.0</AssemblyVersion>
12+
<AssemblyFileVersion>0.1.12.0</AssemblyFileVersion>
1313
</PropertyGroup>
1414

1515
<PropertyGroup>
@@ -19,7 +19,7 @@
1919
<Title>ElectionGuard Encryption</Title>
2020
<Description>Open source implementation of ElectionGuard's ballot encryption.</Description>
2121
<Authors>Microsoft</Authors>
22-
<PackageVersion>0.1.11</PackageVersion>
22+
<PackageVersion>0.1.12</PackageVersion>
2323
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2424
<PackageProjectUrl>https://github.com/microsoft/electionguard-cpp</PackageProjectUrl>
2525
<RepositoryUrl>https://github.com/microsoft/electionguard-cpp</RepositoryUrl>

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/Manifest.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,6 +2209,79 @@ public unsafe Manifest(
22092209
}
22102210
}
22112211

2212+
/// <summary>
2213+
/// Creates a `Manifest` object
2214+
/// </summary>
2215+
/// <param name="electionScopeId"></param>
2216+
/// <param name="electionType">election type</param>
2217+
/// <param name="startDate">start date for election</param>
2218+
/// <param name="endDate">end data for the election</param>
2219+
/// <param name="gpUnits">array of the `GeopoliticalUnit` for election</param>
2220+
/// <param name="parties">array of the `Party` for election</param>
2221+
/// <param name="candidates">array of the `Candidate` for election</param>
2222+
/// <param name="contests">array of the `ContestDescription` for election</param>
2223+
/// <param name="ballotStyles">array of the `BallotStyle` for election</param>
2224+
/// <param name="name">name of the election</param>
2225+
/// <param name="contact">contact information for the election</param>
2226+
public unsafe Manifest(
2227+
string electionScopeId, ElectionType electionType,
2228+
DateTime startDate, DateTime endDate,
2229+
GeopoliticalUnit[] gpUnits, Party[] parties,
2230+
Candidate[] candidates, ContestDescription[] contests,
2231+
BallotStyle[] ballotStyles, InternationalizedText name, ContactInformation contact)
2232+
{
2233+
IntPtr[] gpUnitPointers = new IntPtr[gpUnits.Length];
2234+
for (var i = 0; i < gpUnits.Length; i++)
2235+
{
2236+
gpUnitPointers[i] = gpUnits[i].Handle.Ptr;
2237+
gpUnits[i].Dispose();
2238+
}
2239+
2240+
IntPtr[] partyPointers = new IntPtr[parties.Length];
2241+
for (var i = 0; i < parties.Length; i++)
2242+
{
2243+
partyPointers[i] = parties[i].Handle.Ptr;
2244+
parties[i].Dispose();
2245+
}
2246+
2247+
IntPtr[] candidatePointers = new IntPtr[candidates.Length];
2248+
for (var i = 0; i < candidates.Length; i++)
2249+
{
2250+
candidatePointers[i] = candidates[i].Handle.Ptr;
2251+
candidates[i].Dispose();
2252+
}
2253+
2254+
IntPtr[] contestPointers = new IntPtr[contests.Length];
2255+
for (var i = 0; i < contests.Length; i++)
2256+
{
2257+
contestPointers[i] = contests[i].Handle.Ptr;
2258+
contests[i].Dispose();
2259+
}
2260+
2261+
IntPtr[] ballotStylePointers = new IntPtr[ballotStyles.Length];
2262+
for (var i = 0; i < ballotStyles.Length; i++)
2263+
{
2264+
ballotStylePointers[i] = ballotStyles[i].Handle.Ptr;
2265+
ballotStyles[i].Dispose();
2266+
}
2267+
2268+
var status = NativeInterface.Manifest.New(
2269+
electionScopeId, electionType, name.Handle,
2270+
(ulong)new DateTimeOffset(startDate).ToUnixTimeMilliseconds(),
2271+
(ulong)new DateTimeOffset(endDate).ToUnixTimeMilliseconds(),
2272+
gpUnitPointers, (ulong)gpUnitPointers.LongLength,
2273+
partyPointers, (ulong)partyPointers.LongLength,
2274+
candidatePointers, (ulong)candidatePointers.LongLength,
2275+
contestPointers, (ulong)contestPointers.LongLength,
2276+
ballotStylePointers, (ulong)ballotStylePointers.LongLength,
2277+
contact.Handle,
2278+
out Handle);
2279+
if (status != Status.ELECTIONGUARD_STATUS_SUCCESS)
2280+
{
2281+
throw new ElectionGuardException($"Manifest Error Status: {status}");
2282+
}
2283+
}
2284+
22122285
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
22132286
protected override unsafe void DisposeUnmanaged()
22142287
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/NativeInterface.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,31 @@ internal static extern Status New(
18881888
ulong ballotStylesSize,
18891889
out ManifestHandle handle);
18901890

1891-
// TODO: eg_election_manifest_new_with_contact
1891+
[DllImport(DllName, EntryPoint = "eg_election_manifest_new_with_contact",
1892+
CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
1893+
internal static extern Status New(
1894+
[MarshalAs(UnmanagedType.LPStr)] string electionScopeId,
1895+
ElectionType electionType,
1896+
InternationalizedText.InternationalizedTextHandle name,
1897+
ulong startDate,
1898+
ulong endDate,
1899+
// TODO ISSUE #212: type safety
1900+
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] gpUnits,
1901+
ulong gpUnitsSize,
1902+
// TODO ISSUE #212: type safety
1903+
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] parties,
1904+
ulong partiesSize,
1905+
// TODO ISSUE #212: type safety
1906+
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] candidates,
1907+
ulong candidatesSize,
1908+
// TODO ISSUE #212: type safety
1909+
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] contests,
1910+
ulong contestSize,
1911+
// TODO ISSUE #212: type safety
1912+
[MarshalAs(UnmanagedType.LPArray)] IntPtr[] ballotStyles,
1913+
ulong ballotStylesSize,
1914+
ContactInformation.ContactInformationHandle contact,
1915+
out ManifestHandle handle);
18921916

18931917
[DllImport(DllName, EntryPoint = "eg_election_manifest_free",
18941918
CallingConvention = CallingConvention.Cdecl, SetLastError = true)]

src/electionguard/serialize.hpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ namespace electionguard
513513
serialized.push_back({"contact_information", contactInformation});
514514
}
515515

516+
serialized.push_back({"spec_version", "1.0"});
517+
516518
return serialized;
517519
}
518520

@@ -1059,7 +1061,7 @@ namespace electionguard
10591061
{"manifest_hash", serializable.getManifestHash()->toHex()},
10601062
{"code_seed", serializable.getBallotCodeSeed()->toHex()},
10611063
{"contests", contests},
1062-
{"ballot_code", serializable.getBallotCode()->toHex()},
1064+
{"code", serializable.getBallotCode()->toHex()},
10631065
{"timestamp", serializable.getTimestamp()},
10641066
{"crypto_hash", serializable.getCryptoHash()->toHex()},
10651067
};
@@ -1074,7 +1076,7 @@ namespace electionguard
10741076
auto style_id = j["style_id"].get<string>();
10751077
auto manifest_hash = j["manifest_hash"].get<string>();
10761078
auto code_seed = j["code_seed"].get<string>();
1077-
auto ballot_code = j["ballot_code"].get<string>();
1079+
auto ballot_code = j["code"].get<string>();
10781080
auto timestamp = j["timestamp"].get<uint64_t>();
10791081
auto ballot_nonce = j["nonce"].is_null() ? "" : j["nonce"].get<string>();
10801082
auto crypto_hash = j["crypto_hash"].get<string>();
@@ -1265,15 +1267,15 @@ namespace electionguard
12651267
{
12661268

12671269
auto result = SubmittedBallotWrapper::fromObjectWrapper(serializable);
1268-
result["state"] = getBallotBoxStateString(serializable.getState());
1270+
result["state"] = serializable.getState();
12691271

12701272
return result;
12711273
}
12721274

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

1330-
json result = {
1331-
{"plaintext", plaintext},
1332-
{"code_seed", serializable.getBallotCodeSeed()->toHex()},
1333-
{"ballot_code", serializable.getBallotCode()->toHex()},
1334-
{"nonce", serializable.getNonce()->toHex()},
1335-
{"timestamp", serializable.getTimestamp()},
1336-
{"ballot_box_state", getBallotBoxStateString(serializable.getBallotBoxState())}};
1332+
json result = {{"plaintext", plaintext},
1333+
{"code_seed", serializable.getBallotCodeSeed()->toHex()},
1334+
{"code", serializable.getBallotCode()->toHex()},
1335+
{"nonce", serializable.getNonce()->toHex()},
1336+
{"timestamp", serializable.getTimestamp()},
1337+
{"ballot_box_state", serializable.getBallotBoxState()}};
13371338
return result;
13381339
}
13391340
static unique_ptr<electionguard::CompactCiphertextBallot> toObject(json j)
13401341
{
13411342

13421343
auto codeSeed = j["code_seed"].get<string>();
1343-
auto ballotCode = j["ballot_code"].get<string>();
1344+
auto ballotCode = j["code"].get<string>();
13441345
auto nonce = j["nonce"].get<string>();
13451346
auto timestamp = j["timestamp"].get<uint64_t>();
1346-
auto ballotBoxState = j["ballot_box_state"].get<string>();
1347+
auto ballotBoxState =
1348+
electionguard::BallotBoxState(j["ballot_box_state"].get<uint64_t>());
13471349

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

13501352
return make_unique<electionguard::CompactCiphertextBallot>(
1351-
move(plaintext), getBallotBoxState(ballotBoxState),
1352-
ElementModQ::fromHex(codeSeed), ElementModQ::fromHex(ballotCode), timestamp,
1353-
ElementModQ::fromHex(nonce));
1353+
move(plaintext), ballotBoxState, ElementModQ::fromHex(codeSeed),
1354+
ElementModQ::fromHex(ballotCode), timestamp, ElementModQ::fromHex(nonce));
13541355
}
13551356

13561357
public:

0 commit comments

Comments
 (0)