/// <summary> /// Crypto base test. /// </summary> /// <remarks> /// Only used during development. /// </remarks> public void BaseTest() { BigInt prime = Prime.Find(80); BigInt safePrime = Prime.FindSafe(88); BaseParameters parameters = new BaseParameters(); parameters.GenerateNumbers(Files.TestDataPath); Question question = new Question(new MultiLanguageString(string.Empty), new MultiLanguageString(string.Empty), new MultiLanguageString(string.Empty), 1); question.AddOption(new Option(new MultiLanguageString("Ja"), new MultiLanguageString(string.Empty), new MultiLanguageString(string.Empty))); question.AddOption(new Option(new MultiLanguageString("Nein"), new MultiLanguageString(string.Empty), new MultiLanguageString(string.Empty))); parameters.AddQuestion(question); Authority[] auths = new Authority[5]; for (int aI = 0; aI < parameters.AuthorityCount; aI++) { auths[aI] = new Authority(aI + 1, parameters); } foreach (Authority a in auths) { a.CreatePolynomial(); } foreach (Authority a in auths) { List<Share> shares = new List<Share>(); List<List<VerificationValue>> As = new List<List<VerificationValue>>(); foreach (Authority b in auths) { shares.Add(b.Share(a.Index)); As.Add(new List<VerificationValue>()); for (int l = 0; l <= parameters.Thereshold; l++) { As[As.Count - 1].Add(b.VerificationValue(l)); } } a.VerifySharing(shares, As); } BigInt publicKey = new BigInt(1); foreach (Authority a in auths) { publicKey = (publicKey * a.PublicKeyPart).Mod(parameters.P); } List<Ballot> ballots = new List<Ballot>(); ballots.Add(new Ballot(new int[] { 0, 1 }, parameters, question, publicKey, null)); ballots.Add(new Ballot(new int[] { 0, 1 }, parameters, question, publicKey, null)); ballots.Add(new Ballot(new int[] { 1, 0 }, parameters, question, publicKey, null)); ballots.Add(new Ballot(new int[] { 0, 1 }, parameters, question, publicKey, null)); ballots.Add(new Ballot(new int[] { 1, 0 }, parameters, question, publicKey, null)); ballots.Add(new Ballot(new int[] { 0, 1 }, parameters, question, publicKey, null)); if (!ballots.All(ballot => ballot.Verify(publicKey, parameters, question, RandomNumberGenerator.Create(), BaseParameters.StandardProofCount, new Progress(null)))) throw new Exception("Bad proof."); for (int optionIndex = 0; optionIndex < question.Options.Count(); optionIndex++) { IEnumerable<Vote> votes = ballots.Select(ballot => ballot.Votes[optionIndex]); Vote sum = null; votes.Foreach(vote => sum = sum == null ? vote : sum + vote); List<PartialDecipher> partialDeciphers = new List<PartialDecipher>(); auths.Foreach(authority => partialDeciphers.AddRange(authority.PartialDeciphers(1, sum, 0, optionIndex))); IEnumerable<BigInt> partialDeciphers0 = partialDeciphers .Where(partialDecipher => partialDecipher.GroupIndex == 1) .Select(partialDecipher => partialDecipher.Value); int v0 = sum.Decrypt(partialDeciphers0, parameters); IEnumerable<BigInt> partialDeciphers1 = partialDeciphers .Where(partialDecipher => partialDecipher.GroupIndex == 1) .Select(partialDecipher => partialDecipher.Value); int v1 = sum.Decrypt(partialDeciphers1, parameters); IEnumerable<BigInt> partialDeciphers2 = partialDeciphers .Where(partialDecipher => partialDecipher.GroupIndex == 1) .Select(partialDecipher => partialDecipher.Value); int v2 = sum.Decrypt(partialDeciphers2, parameters); IEnumerable<BigInt> partialDeciphers3 = partialDeciphers .Where(partialDecipher => partialDecipher.GroupIndex == 1) .Select(partialDecipher => partialDecipher.Value); int v3 = sum.Decrypt(partialDeciphers3, parameters); IEnumerable<BigInt> partialDeciphers4 = partialDeciphers .Where(partialDecipher => partialDecipher.GroupIndex == 1) .Select(partialDecipher => partialDecipher.Value); int v4 = sum.Decrypt(partialDeciphers4, parameters); if (v0 == v1 && v0 == v2 && v0 == v3 && v0 == v4) { throw new Exception("Everything ok."); } else { throw new Exception("Bad vote."); } } }