/// <summary> /// Prepares the authority for voting procedure. /// </summary> /// <param name="index">Index given to this authority.</param> /// <param name="parameters">Voting parameters for the procedure.</param> public void Prepare(int index, Signed<VotingParameters> signedParameters) { if (index < 1) throw new ArgumentException("Index must be at least 1."); if (signedParameters == null) throw new ArgumentNullException("signedParameters"); this.signedParameters = signedParameters; this.parameters = signedParameters.Value; this.authority = new Authority(index, this.parameters); this.authority.CreatePolynomial(); this.authorities = new Dictionary<int, Certificate>(); }
/// <summary> /// Load data of authority from file. /// </summary> /// <param name="fileName">Name of file to load.</param> private void Load(string fileName) { FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); DeserializeContext context = new DeserializeContext(fileStream); this.parameters = context.ReadObject<VotingParameters>(); this.signedParameters = context.ReadObject<Signed<VotingParameters>>(); this.authorities = new Dictionary<int,Certificate>(); int count = context.ReadInt32(); count.Times(() => this.authorities.Add(context.ReadInt32(), context.ReadObject<Certificate>())); this.authority = new Authority(context, this.parameters); context.Close(); fileStream.Close(); }
/// <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."); } } }