private BigInteger[] TryMerge(BigInteger[] voteResults, Vote v) { if(voteResults == null || v == null || v.EncryptedVector == null || voteResults.Length != v.EncryptedVector.Length) return null; return Enumerable.Range(0, voteResults.Length).Select(i => voteResults[i] + v.EncryptedVector[i]).ToArray(); }
public bool Vote(Guid electionId, User user, BigInteger[] voteArray) { Election election; if(!electionsDict.TryGetValue(electionId, out election)) return false; lock(election) { if(!election.IsNominationFinished || election.IsFinished) return false; if(election.Votes.Count == MaxVotesPerElection) return false; if(election.Candidates.Count != voteArray.Length) return false; if(election.Votes.Any(v => v.UserId == user.Id)) return false; var vote = new Vote { UserId = user.Id, EncryptedVector = voteArray }; var result = election.EncryptedResult ?? new BigInteger[vote.EncryptedVector.Length]; election.EncryptedResult = TryMerge(result, vote); election.Votes.Add(vote); } return true; }