Skip to content

Commit 7b68151

Browse files
jeffspel-cryptodryercashewSteveMaier-IRT
authored
Pre computations (#259)
* This is the first step of the implementation to use precomputed values to encrypt a ballot. There is are two queues in a global (PrecomputedBufferContext) and the queues are populated with a populate method and stopped with a stop_populate method. Then in encryptSelection (encrypt.cpp) if precomputed values are available in the queues then those values are used to generate the encrypted selection. * More changes for precomputed values. * Add testing for the precompute buffers change. * Add benchmarking tests for the new precomputed values APIs. * Update to the benchmarking tests. * Clean up missing xml documentation for exceptions * precompute integration * Making sure calling stop_populate after calling populate in tests. * Fix issue with setting of max queue size. * fixing integration * Remove the dependency on async.hpp. * Updated the code to fix a number of issues pointed out during PR review. * Satisfy rule of 5 * Remove the TripleEntry and QuadrupleEntry objects and store the Triple and Quadruple entries in the queue. * Removed sleeps in test code and in the PrecomputeAPI. * add lock_guards to the precompute * created const for default precompute buffer size. * adding resend for completed events * Moved the assert to the bottom of the tests to get the rest of the results checked first * Adding an init method to the PrecomputeBufferContext. * Created new init function to have the api call to know when it is ready. * updating version number for nuget Co-authored-by: Jeff <spelmaa@wwu.edu> Co-authored-by: SteveMaier-IRT <steve.maier@infernored.com>
1 parent 46b1e70 commit 7b68151

23 files changed

Lines changed: 1889 additions & 110 deletions

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

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ namespace ElectionGuard.Encryption.Tests
1111
[TestFixture]
1212
class TestPrecompute
1313
{
14+
readonly int MAX_COMPLETE_DELAY = 7000;
15+
readonly int SMALL_BUFFER_SIZE = 10;
16+
readonly int DEFAULT_BUFFER_SIZE = 5000;
17+
readonly int LARGE_BUFFER_SIZE = 6000;
18+
1419
[Test]
1520
public void Test_Precompute_Status_NoStarted()
1621
{
@@ -24,31 +29,94 @@ public void Test_Precompute_Status_NoStarted()
2429
[Test]
2530
public void Test_Precompute_Status_Running()
2631
{
32+
var waitHandle = new AutoResetEvent(false);
33+
2734
Precompute precompute = new Precompute();
35+
var keypair = ElGamalKeyPair.FromSecret(Constants.TWO_MOD_Q);
36+
37+
precompute.CompletedEvent += (PrecomputeStatus completedStatus) =>
38+
{
39+
Assert.AreEqual(PrecomputeState.UserStopped, completedStatus.CurrentState);
40+
waitHandle.Set();
41+
};
42+
precompute.StartPrecomputeAsync(keypair.PublicKey, LARGE_BUFFER_SIZE);
43+
var runningStatus = precompute.GetStatus();
44+
precompute.StopPrecompute();
2845

29-
CancellationTokenSource cts = new CancellationTokenSource();
46+
var waitReturn = waitHandle.WaitOne(MAX_COMPLETE_DELAY);
3047

31-
precompute.StartPrecomputeAsync(1, cts.Token);
48+
int count = -1;
49+
int queue_size = -1;
50+
precompute.GetProgress(out count, out queue_size);
3251
var status = precompute.GetStatus();
33-
precompute.StopPrecompute();
3452

35-
Assert.AreEqual(PrecomputeState.Running, status.CurrentState);
53+
Assert.AreEqual(LARGE_BUFFER_SIZE, queue_size);
54+
Assert.AreEqual(PrecomputeState.Running, runningStatus.CurrentState);
55+
Assert.AreEqual(true, waitReturn);
3656
}
3757

3858
[Test]
3959
public void Test_Precompute_Status_Stopped()
4060
{
41-
Precompute precompute = new Precompute();
61+
var waitHandle = new AutoResetEvent(false);
4262

43-
CancellationTokenSource cts = new CancellationTokenSource();
63+
Precompute precompute = new Precompute();
64+
var keypair = ElGamalKeyPair.FromSecret(Constants.TWO_MOD_Q);
4465

45-
precompute.StartPrecomputeAsync(1, cts.Token);
66+
precompute.CompletedEvent += (PrecomputeStatus completedStatus) =>
67+
{
68+
Assert.AreEqual(PrecomputeState.UserStopped, completedStatus.CurrentState);
69+
waitHandle.Set();
70+
};
71+
precompute.StartPrecomputeAsync(keypair.PublicKey);
72+
var statusRunning = precompute.GetStatus();
4673
precompute.StopPrecompute();
47-
Thread.Sleep(1000);
4874

75+
var waitReturn = waitHandle.WaitOne(MAX_COMPLETE_DELAY);
76+
77+
int count = -1;
78+
int queue_size = -1;
79+
precompute.GetProgress(out count, out queue_size);
4980
var status = precompute.GetStatus();
5081

82+
Assert.AreEqual(DEFAULT_BUFFER_SIZE, queue_size);
83+
Assert.AreNotEqual(-1, count);
84+
85+
Assert.AreEqual(PrecomputeState.Running, statusRunning.CurrentState);
5186
Assert.AreEqual(PrecomputeState.UserStopped, status.CurrentState);
87+
88+
Assert.AreEqual(true, waitReturn);
89+
}
90+
91+
[Test]
92+
public void Test_Precompute_Status_Complete()
93+
{
94+
var waitHandle = new AutoResetEvent(false);
95+
96+
Precompute precompute = new Precompute();
97+
var keypair = ElGamalKeyPair.FromSecret(Constants.TWO_MOD_Q);
98+
99+
precompute.CompletedEvent += (PrecomputeStatus completedStatus) =>
100+
{
101+
Assert.AreEqual(SMALL_BUFFER_SIZE, completedStatus.CompletedExponentiationsCount);
102+
Assert.AreEqual(1.0, completedStatus.Percentage);
103+
Assert.AreEqual(PrecomputeState.Completed, completedStatus.CurrentState);
104+
waitHandle.Set();
105+
};
106+
precompute.StartPrecomputeAsync(keypair.PublicKey, SMALL_BUFFER_SIZE);
107+
108+
var waitReturn = waitHandle.WaitOne(MAX_COMPLETE_DELAY);
109+
110+
int count = -1;
111+
int queue_size = -1;
112+
precompute.GetProgress(out count, out queue_size);
113+
var status = precompute.GetStatus();
114+
115+
Assert.AreEqual(SMALL_BUFFER_SIZE, queue_size);
116+
Assert.AreEqual(SMALL_BUFFER_SIZE, count);
117+
118+
Assert.AreEqual(PrecomputeState.Completed, status.CurrentState);
119+
Assert.AreEqual(true, waitReturn);
52120
}
53121

54122
}

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.8</Version>
11-
<AssemblyVersion>0.1.8.0</AssemblyVersion>
12-
<AssemblyFileVersion>0.1.8.0</AssemblyFileVersion>
10+
<Version>0.1.9</Version>
11+
<AssemblyVersion>0.1.9.0</AssemblyVersion>
12+
<AssemblyFileVersion>0.1.9.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.8</PackageVersion>
22+
<PackageVersion>0.1.9</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/ElectionGuardException.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,40 @@ namespace ElectionGuard
77
/// </summary>
88
public class ElectionGuardException : Exception
99
{
10+
/// <summary>
11+
/// Property for the status code of the exception
12+
/// </summary>
1013
public Status Status { get; set; }
1114

15+
/// <summary>
16+
/// Default constructor
17+
/// </summary>
1218
public ElectionGuardException() { }
19+
20+
/// <summary>
21+
/// Parameterized constructor that sets the message
22+
/// </summary>
23+
/// <param name="message">message to be used in the exception handling</param>
24+
/// <returns>New ElectionGuardException object</returns>
1325
public ElectionGuardException(string message) : base(message) { }
26+
27+
/// <summary>
28+
/// Parameterized constructor that sets the message and the error code
29+
/// </summary>
30+
/// <param name="message">message to be used in the exception handling</param>
31+
/// <param name="code">error code to identify the exception</param>
32+
/// <returns>New ElectionGuardException object</returns>
1433
public ElectionGuardException(string message, Status code) : base(message)
1534
{
1635
this.Status = code;
1736
}
37+
38+
/// <summary>
39+
/// Parameterized constructor that uses another exception and wraps it
40+
/// </summary>
41+
/// <param name="message">message to be used in the exception handling</param>
42+
/// <param name="inner">exception to wrap inside the ElectionGuardException</param>
43+
/// <returns>New ElectionGuardException object</returns>
1844
public ElectionGuardException(string message, System.Exception inner) : base(message, inner) { }
1945
}
2046

0 commit comments

Comments
 (0)