Esempio n. 1
0
        static void Main(string[] args)
        {
            int maxNum = 81;
            var nums   = new [] { 0, 1, 2 };

            const int testsCount = 10;
            var       sw         = Stopwatch.StartNew();

            for (int i = 0; i < testsCount; i++)
            {
                int        testResult = 0;
                BigInteger encResult  = 0;

                var homoKeyPair = HomoKeyPair.GenKeyPair(maxNum);

                foreach (var num in nums)
                {
                    testResult += num;
                    var c = Encrypt(num, homoKeyPair.PublicKey);
                    Console.WriteLine($"num {num} c {c}");
                    encResult += c;
                }
                Console.WriteLine();
                var decryptedResult = Decrypt(encResult, homoKeyPair.PrivateKey);

                if (testResult != decryptedResult)
                {
                    Console.WriteLine($"Failed: expected {testResult} got {decryptedResult}");
                    Environment.Exit(1);
                }
            }
            sw.Stop();
            Console.WriteLine($"Passed! Done {testsCount} tests in {sw.Elapsed}");
        }
Esempio n. 2
0
        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);
        }