Exemplo n.º 1
0
        public void Test_Encrypt_Ballot_Simple_Succeeds()
        {
            // Configure the election context
            var keypair = ElGamalKeyPair.FromSecret(Constants.TWO_MOD_Q);
            var description = new Manifest(manifest_json);
            var manifest = new InternalManifest(description);
            var context = new CiphertextElectionContext(
                1UL, 1UL, keypair.PublicKey, Constants.TWO_MOD_Q, manifest.ManifestHash);
            var device = new EncryptionDevice(12345UL, 23456UL, 34567UL, "Location");
            var mediator = new EncryptionMediator(manifest, context, device);

            var ballot = new PlaintextBallot(plaintext_json);

            // Act
            var ciphertext = mediator.Encrypt(ballot);

            // Assert
            
            // a property
            Assert.That(ciphertext.ObjectId == ballot.ObjectId);

            // json serialization
            var json = ciphertext.Json;
            var fromJson = new CiphertextBallot(json);
            Assert.That(ciphertext.ObjectId == fromJson.ObjectId);

            // binary serialization
            var bson = ciphertext.Bson;
            var fromBson = CiphertextBallot.FromBson(bson);
            Assert.That(ciphertext.ObjectId == fromBson.ObjectId);

            // submitted ballot
            var submitted = SubmittedBallot.From(ciphertext, BallotBoxState.cast);
            Assert.That(ciphertext.ObjectId == submitted.ObjectId);
        }
Exemplo n.º 2
0
        public void TestGuardianCreation()
        {
            var guardianApi = new GuardianClient("http://localhost:8001");
            var mediatorApi = new MediatorClient("http://localhost:8000");
            var edesc       = JsonSerializer.Deserialize <ElectionDescription>(TestData.description2);
            var guardians   = new string[] { "A B", "C D", "E F", "G H", "I J" };
            var glist       = new List <Guardian>();

            for (var i = 0; i < guardians.Length; i++)
            {
                glist.Add(guardianApi.Guardian(null, null, guardians[i], 5, 3, i));
            }
            var pkeys = glist.ConvertAll(g => g.election_key_pair.public_key).ToArray();
            var r     = mediatorApi.ElectionCombine(pkeys);
            var ctxt  = mediatorApi.ElectionContext(edesc, r.joint_key, 5, 3);

            var ballots = new PlaintextBallot[4];

            for (int i = 0; i < ballots.Length; i++)
            {
                ballots[i]           = JsonSerializer.Deserialize <PlaintextBallot>(TestData.ballotData);
                ballots[i].object_id = $"ballot-{i}";
            }

            var nonce     = "110191403412906482859082647039385908787148325839889522238592336039604240167009";
            var seed_hash = "110191403412906482859082647039385908787148325839889522238592336039604240167009";

            var segments = new[] { ballots[0..2], ballots[2..4] };
Exemplo n.º 3
0
        public void Test_Ballot_Property_Getters()
        {
            // Arrange
            string data = @"{""style_id"":""ballot-style-1"",""contests"":[{""ballot_selections"":[{""object_id"":""contest-1-selection-1-id"",""vote"":1},{""object_id"":""contest-1-selection-2-id"",""vote"":0},{""object_id"":""contest-1-selection-3-id"",""vote"":0}],""object_id"":""contest-1-id""},{""ballot_selections"":[{""object_id"":""contest-2-selection-1-id"",""vote"":1},{""object_id"":""contest-2-selection-2-id"",""vote"":0}],""object_id"":""contest-2-id""}],""object_id"":""ballot-id-123""}";

            // Act
            var ballot = new PlaintextBallot(data);

            // Assert

            // verify the ballot property accessors
            var ballotId = ballot.ObjectId;

            Assert.That(ballotId == "ballot-id-123");

            // iterate over the contests in the ballot
            for (ulong i = 0; i < ballot.ContestsSize; i++)
            {
                var contest = ballot.GetContestAt(i);

                // verify the contest property accessors
                var contestId = contest.ObjectId;
                Assert.That(contestId == $"contest-{i + 1}-id");

                // iterate over the selections in the contest
                for (ulong j = 0; j < contest.SelectionsSize; j++)
                {
                    var selection = contest.GetSelectionAt(j);

                    // verify the selection property accessors
                    var selectionId = selection.ObjectId;
                    Assert.That(selectionId == $"contest-{i + 1}-selection-{j+1}-id");
                }
            }
        }
        private PlaintextBallot Make_No_Ballot()
        {
            string data = @"{""ballot_style"":""ballot-style-1"",""contests"":[{""ballot_selections"":[{""object_id"":""contest-1-yes-selection-id"",""vote"":0},{""object_id"":""contest-1-no-selection-id"",""vote"":1}],""object_id"":""contest-1-id""},{""ballot_selections"":[{""object_id"":""contest-2-selection-1-id"",""vote"":1},{""object_id"":""contest-2-selection-2-id"",""vote"":0}],""object_id"":""contest-2-id""}],""object_id"":""ballot-id-123""}";;

            PlaintextBallot plaintext = new PlaintextBallot(data);

            return(plaintext);
        }
        public void Test_Compact_Encrypt_Ballot_Simple_Succeeds()
        {
            var keypair     = ElGamalKeyPair.FromSecret(Constants.TWO_MOD_Q);
            var description = new Manifest(jsonManifest);
            var manifest    = new InternalManifest(description);
            var context     = new CiphertextElectionContext(1UL, 1UL, keypair.PublicKey, Constants.TWO_MOD_Q, manifest.ManifestHash);
            var device      = new EncryptionDevice(12345UL, 23456UL, 34567UL, "Location");
            var mediator    = new EncryptionMediator(manifest, context, device);

            var ballot = new PlaintextBallot(plaintext);

            // Act
            var compact = mediator.CompactEncrypt(ballot);

            // Assert
            Assert.That(compact.ObjectId == ballot.ObjectId);

            var msgpack     = compact.ToMsgPack();
            var fromMsgpack = new CompactCiphertextBallot(msgpack);

            Assert.That(compact.ObjectId == fromMsgpack.ObjectId);
        }
Exemplo n.º 6
0
        public void Test_Ballot_Property_Getters()
        {
            // Act
            var ballot = new PlaintextBallot(data);

            // Assert

            // verify the ballot property accessors
            var ballotId = ballot.ObjectId;

            Assert.That(ballotId == "ballot-id-123");

            int index = 0;

            // iterate over the contests in the ballot
            foreach (var contest in ballot.Contests)
            {
                // verify the contest property accessors
                var contestId = contest.ObjectId;
                Assert.That(contestId == $"contest-{index + 1}-id");

                var jindex = 0;
                // iterate over the selections in the contest
                foreach (var selection in contest.Selections)
                {
                    // verify the selection property accessors
                    var selectionId = selection.ObjectId;
                    Assert.That(selectionId == $"contest-{index + 1}-selection-{jindex + 1}-id");
                    jindex++;
                }

                index++;
            }

            var bson     = ballot.Bson;
            var fromBson = PlaintextBallot.FromBson(bson);

            Assert.That(ballot.ObjectId == fromBson.ObjectId);
        }
        void No_Ballot_Button_Clicked(System.Object sender, System.EventArgs e)
        {
            PlaintextBallot plaintext = Make_No_Ballot();

            ((Button)sender).Text = $"No-vote created!";
        }