public static Election NominateUsers(string host, Election election, User[] candidates) { log.InfoFormat("Nominating rest {0} candidates for election...", candidates.Length); var nominateTasks = candidates.Select(candidate => new { candidate, task = ElectroClient.NominateAsync(host, Program.PORT, candidate.Cookies, election.Id) }).ToArray(); Election[] elections; try { elections = nominateTasks.Select(arg => arg.task.Result).ToArray(); } catch(Exception e) { throw new ServiceException(ExitCode.DOWN, string.Format("Failed to nominate {0} candidates in parallel: {1}", nominateTasks.Length, e)); } var electionId = election.Id; election = elections.FirstOrDefault(election1 => election1 != null && election1.Candidates.Count >= candidates.Length + 1); if(election == null) throw new ServiceException(ExitCode.MUMBLE, string.Format("Nominated '{0}' candidates for election '{1}', but got less in result", candidates.Length + 1, electionId)); log.Info("Candidates nomindated"); return election; }
public bool TryDecryptElectionResultIfFinished(Election election) { if(!election.IsFinished) return false; PrivateKey privateKey; if(!electionPrivateKeys.TryGetValue(election.Id, out privateKey)) return false; election.DecryptedResult = (election.EncryptedResult ?? Enumerable.Repeat(BigInteger.Zero, election.Candidates.Count)).Select(voteElement => HomoCrypto.Decrypt(voteElement, privateKey)).ToArray(); return true; }
public Guid StartElection(string electionName, User firstCandidate, bool isPublic, DateTime nominateTill, DateTime till) { var homoKeyPair = HomoKeyPair.GenKeyPair(MaxVotesPerElection); var election = new Election { Id = Guid.NewGuid(), Name = electionName, NominateTill = nominateTill, VoteTill = till, PublicKey = homoKeyPair.PublicKey, Votes = new List<Vote>(), Candidates = new List<CandidateInfo> { CandidateInfo.Create(firstCandidate) }, IsPublic = isPublic }; statePersister.SaveKey(election.Id, homoKeyPair.PrivateKey); lock (electionsList) { electionPrivateKeys[election.Id] = homoKeyPair.PrivateKey; electionsDict[election.Id] = election; electionsList.AddFirst(election); while(electionsList.Count > MaxElections) { var node = electionsList.Last; Election dummy; electionsDict.TryRemove(node.Value.Id, out dummy); electionsList.RemoveLast(); } } return election.Id; }
public static ElectionPublicCore Create(Election election) { return new ElectionPublicCore { Id = election.Id, Name = election.Name, NominateTill = election.NominateTill, VoteTill = election.VoteTill, Winner = election.FindWinner() }; }
private static int[][] GenVotes(string flag, Election election) { var candidateInfos = election.Candidates.ToArray(); var votes = flag.Select(c => { var votePos = candidateInfos.IndexOf(info => info.PublicMessage == c.ToString()); if(votePos < 0) throw new ServiceException(ExitCode.MUMBLE, "Nominated candidates for all flag characters but in resulting election have not for all"); return Utils.GenVoteVector(candidateInfos.Length, votePos); }).ToArray(); return votes; }
private static string ExtractFlag(Election election, PrivateKey privateKey, int flagLen) { if(election.Votes == null) throw new ServiceException(ExitCode.CORRUPT, "Election has no votes"); if(election.Candidates == null) throw new ServiceException(ExitCode.CORRUPT, "Election has no candidates"); return string.Join("", election.Votes.Take(flagLen).Select(vote => { var decryptedVote = vote.DecryptVote(privateKey); int candidateNum = decryptedVote.IndexOf(i => i > 0); if(candidateNum < 0 || candidateNum >= election.Candidates.Count) throw new ServiceException(ExitCode.CORRUPT, string.Format("Election has no candidate corresponding to vector {0}", string.Join(",", decryptedVote))); return election.Candidates[candidateNum].PublicMessage; })); }