aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/voting/TallyAuthorityDaemon.java
blob: b08ec4f655e9edab518cb10b080bb0801a2d745e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 This file is part of GNUnet.
  (C) 2012, 2013 Christian Grothoff (and other contributing authors)

  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3, or (at your
  option) any later version.

  GNUnet is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA.
 */
package org.gnunet.voting;


import com.google.common.collect.Maps;
import org.gnunet.consensus.Consensus;
import org.gnunet.consensus.ConsensusCallback;
import org.gnunet.consensus.ConsensusElement;
import org.gnunet.construct.Construct;
import org.gnunet.cadet.Cadet;
import org.gnunet.cadet.CadetRunabout;
import org.gnunet.cadet.ChannelEndHandler;
import org.gnunet.secretsharing.*;
import org.gnunet.testbed.CompressedConfig;
import org.gnunet.util.*;
import org.gnunet.util.crypto.EcdsaPublicKey;
import org.gnunet.util.crypto.EddsaPrivateKey;
import org.gnunet.util.crypto.EddsaPublicKey;
import org.gnunet.util.crypto.EddsaSignature;
import org.gnunet.voting.messages.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;


/**
 * Daemon that is responsible for accepting and counting votes.
 */
public class TallyAuthorityDaemon extends Program {
    private static final Logger logger = LoggerFactory
            .getLogger(TallyAuthorityDaemon.class);

    /**
     * Cadet port used to connect to to the tally authority daemon.
     */
    public static final int CADET_PORT = 1002;

    /**
     * Cadet handle.
     */
    private Cadet cadet;

    /**
     * Private key of the local peer.
     */
    private EddsaPrivateKey authorityPrivateKey;

    /**
     * Public key of the local peer.
     */
    private EddsaPublicKey authorityPublicKey;

    /**
     * All elections known to this authority
     */
    private HashMap<HashCode, ElectionState> elections = Maps.newHashMap();

    /**
     * State of one election.
     */
    class ElectionState {
        /**
         * The ballot that describes this election.
         */
        Ballot ballot;

        /**
         * The threshold crypto share, null if the key has not yet been
         * established.
         */
        Share share;

        /**
         * A voter is in this set if its vote has been in the consensus.
         */
        Set<EcdsaPublicKey> countedVoters = new HashSet<EcdsaPublicKey>();

        /**
         * Key generation session.
         */
        KeyGeneration keyGeneration;

        /**
         * Consensus with the other authorities on the set of ballots.
         */
        Consensus consensus;

        /**
         * Are we done with the vote consensus?
         */
        boolean consensusDone;

        /**
         * Product of all encrypted votes (mod q), used to compute the final tally.
         */
        Ciphertext voteProduct = Ciphertext.identity();

        /**
         * Maping from choice to number of votes for that choice.
         * In our currently simplified implementation, tally.length is always 2.
         * If the tally has not been counted yet, 'tally' is null.
         */
        long[] tally;

        /**
         * The decrypt session.
         */
        Decryption decryption;
    }

    /**
     * Callbacks for the vote consensus.
     */
    class ElectionConsensusConclude implements ConsensusCallback {
        private final ElectionState electionState;

        public ElectionConsensusConclude(ElectionState electionState) {
            this.electionState = electionState;
        }
        @Override
        public void onElement(ConsensusElement element) {
            System.out.println("got element from consensus");
            EncryptedVote vote = Construct.parseAs(element.data, EncryptedVote.class);
            System.out.println("got vote from consensus, ciphertext: " + vote.v.toString());
            if (electionState.countedVoters.contains(vote.voterPublicKey)) {
                // Complain.  FIXME: keep lexically largest vote, so ballot is unambigous
                logger.error("voter {} voted twice", vote.voterPublicKey);
                return;
            }
            electionState.voteProduct = electionState.voteProduct.multiply(vote.v);
            electionState.countedVoters.add(vote.voterPublicKey);

            System.out.println("threshold key (of this authority): " + electionState.share.publicKey.toString());
        }

        @Override
        public void onDone() {
            System.out.println("consensus concluded");
            electionState.consensusDone = true;
            electionState.consensus.destroy();
            electionState.consensus = null;

            electionState.decryption = new Decryption(
                    getConfiguration(),
                    electionState.share,
                    electionState.voteProduct,
                    electionState.ballot.concludeTime,
                    electionState.ballot.queryTime,
                    new DecryptCallback() {
                        @Override
                        public void onResult(Plaintext plaintext) {
                            logger.info("got decypt result");
                            long l = electionState.countedVoters.size();
                            long t[] = plaintext.bruteForceDiscreteLog(l, electionState.ballot.generators);
                            if (null == t) {
                                logger.warn("could not brute-force result");
                            } else {
                                logger.info("brute-forced result");
                                electionState.tally = t;
                            }
                        }
                    });
        }
    }

    class ConsensusConcludeTask implements Scheduler.Task {
        /**
         * Which election on this authority is the consensus conclude for?
         */
        private final ElectionState electionState;

        public ConsensusConcludeTask(ElectionState electionState) {
            this.electionState = electionState;
        }
        @Override
        public void run(Scheduler.RunContext ctx) {
            electionState.consensus.conclude(new ElectionConsensusConclude(electionState));
        }
    }

    static class SecretReady implements SecretReadyCallback {
        private final ElectionState electionState;

        public SecretReady(ElectionState electionState) {
            this.electionState = electionState;
        }

        @Override
        public void onSecretReady(Share share) {
            electionState.keyGeneration = null;
            electionState.share = share;
        }
    }

    private SubmitFailureMessage.SignedAuthorityTime getTimeSigMessage() {
        SubmitFailureMessage.SignedAuthorityTime tm = new SubmitFailureMessage.SignedAuthorityTime();
        // FIXME!
        tm.purpose = 0;
        tm.time = AbsoluteTime.now().asMessage();
        tm.signature = authorityPrivateKey.sign(Construct.toBinary(tm.time), tm.purpose,authorityPublicKey);
        return tm;
    }

    private class TallyCadetReceiver extends CadetRunabout {
        public void visit(SubmitMessage m) {
            logger.debug("got submit message");
            ElectionState electionState = elections.get(m.ballotGuid);
            if (null == electionState) {
                SubmitFailureMessage fm = new SubmitFailureMessage();
                fm.reason = "no matching ballot found";
                getSender().send(fm);
            } else if (!electionState.ballot.startTime.isDue()) {
                SubmitFailureMessage fm = new SubmitFailureMessage();
                fm.reason = "too early to submit vote";
                fm.signedAuthorityTime = getTimeSigMessage();
                getSender().send(fm);
            } else if (electionState.ballot.closingTime.isDue()) {
                SubmitFailureMessage fm = new SubmitFailureMessage();
                fm.reason = "too late to submit vote";
                fm.signedAuthorityTime = getTimeSigMessage();
                getSender().send(fm);
            }
            // FIXME: check signatures of voter and CA
            else {
                // we do *not* check for duplicate votes here,
                // as consensus takes care of this, and there is no harm in sending
                // exact duplicates
                byte[] elem = Construct.toBinary(m.encryptedVote);
                electionState.consensus.insertElement(new ConsensusElement(elem, 0));
                SubmitSuccessMessage sm = new SubmitSuccessMessage();
                sm.confirmationSig = EddsaSignature.randomGarbage();
                getSender().send(sm);
            }

            getSender().receiveDone();
        }

        public void visit(BallotRegisterRequestMessage m) {
            logger.info("ballot register requested");
            CompressedConfig ccfg = new CompressedConfig(m.compressedBallotConfig);
            Ballot b;
            HashCode guid;
            try {
                b = new Ballot(ccfg.decompress());
                guid = b.getBallotGuid();
            } catch (InvalidBallotException e) {
                BallotRegisterFailureMessage fm = new BallotRegisterFailureMessage();
                fm.reason = "invalid ballot (" + e.getMessage() + ")";
                getSender().send(fm);
                getSender().receiveDone();
                return;
            }
            if (elections.containsKey(guid)) {
                BallotRegisterFailureMessage fm = new BallotRegisterFailureMessage();
                fm.reason = "ballot with same GUID already registered";
                getSender().send(fm);
                return;
            }
            ElectionState electionState = new ElectionState();
            electionState.ballot = b;
            PeerIdentity[] ids = new PeerIdentity[b.getAuthorities().size()];
            ids = b.getAuthorities().toArray(ids);
            electionState.consensus = new Consensus(
                    getConfiguration(),
                    ids,
                    b.getBallotGuid(),
                    electionState.ballot.closingTime,
                    electionState.ballot.concludeTime);

            ConsensusConcludeTask t = new ConsensusConcludeTask(electionState);
            if (b.concludeTime.isDue()) {
                logger.info("concluding now");
                Scheduler.add(t);
            } else {
                logger.info("concluding in {}", b.closingTime.getRemaining().getSeconds());
                Scheduler.addDelayed(b.closingTime.getRemaining(), t);
            }
            System.out.println("authority threshold: " + electionState.ballot.threshold);
            System.out.println("authority num_peers: " + electionState.ballot.authorities.size());
            // we hash the GUID a second time, so that there's no
            // collision with the consensus (as secretsharing also uses consensus internally)
            electionState.keyGeneration = new KeyGeneration(
                    getConfiguration(),
                    ids,
                    HashCode.hash(b.getBallotGuid().data),
                    electionState.ballot.keygenStartTime,
                    electionState.ballot.keygenEndTime,
                    electionState.ballot.threshold, new SecretReady(electionState));
            elections.put(guid, electionState);

            BallotRegisterSuccessMessage rm = new BallotRegisterSuccessMessage();
            rm.registrationSignature = EddsaSignature.randomGarbage();
            getSender().send(rm);
        }

        public void visit(ResultQueryMessage m) {
            logger.debug("got result query message");
            ElectionState electionState = elections.get(m.ballotGuid);
            if (null == electionState) {
                ResultQueryFailureMessage rm = new ResultQueryFailureMessage();
                rm.reason = "no matching ballot found";
                getSender().send(rm);
            } else {
                if (!electionState.ballot.queryTime.isDue()) {
                    ResultQueryFailureMessage rm = new ResultQueryFailureMessage();
                    rm.reason = "result query not allowed yet";
                    getSender().send(rm);
                }
                else if (null == electionState.tally) {
                    ResultQueryFailureMessage rm = new ResultQueryFailureMessage();
                    rm.reason = "tally not yet available";
                    getSender().send(rm);
                }
                else {
                    ResultQueryResponseMessage rm = new ResultQueryResponseMessage();
                    rm.results = electionState.tally;
                    getSender().send(rm);
                }
            }
            getSender().receiveDone();
        }

        public void visit(KeyQueryMessage m) {
            logger.debug("got key query message");
            getSender().receiveDone();
            ElectionState electionState = elections.get(m.ballotGuid);
            if (null == electionState) {
                KeyQueryFailureMessage rm = new KeyQueryFailureMessage();
                rm.reason = "no matching ballot found";
                getSender().send(rm);
                return;
            }
            if (!electionState.ballot.keygenEndTime.isDue()) {
                KeyQueryFailureMessage rm = new KeyQueryFailureMessage();
                rm.reason = "key query not allowed yet";
                getSender().send(rm);
                return;
            }
            if (null == electionState.share) {
                KeyQueryFailureMessage rm = new KeyQueryFailureMessage();
                rm.reason = "key not yet established";
                getSender().send(rm);
                return;
            }
            KeyQueryResponseMessage.BallotPublicKey ballotPublicKey = new KeyQueryResponseMessage.BallotPublicKey();
            ballotPublicKey.ballotGuid = electionState.ballot.getBallotGuid();
            ballotPublicKey.publicKey = electionState.share.publicKey;

            KeyQueryResponseMessage rm = new KeyQueryResponseMessage();
            rm.signedGuidKey = ballotPublicKey;
            // FIXME!
            rm.purpose = 0;
            rm.signature = authorityPrivateKey.sign(Construct.toBinary(rm.signedGuidKey),
                    rm.purpose, authorityPublicKey);
            getSender().send(rm);
        }
    }

    public TallyAuthorityDaemon() {
        authorityPrivateKey = EddsaPrivateKey.createRandom();
        authorityPublicKey = authorityPrivateKey.getPublicKey();
    }

    public static void main(String[] args) {
        TallyAuthorityDaemon daemon = new TallyAuthorityDaemon();
        int ret = daemon.start(args);
        System.exit(ret);
    }

    @Override
    public void run() {
        logger.info("running tally daemon");
        cadet = new Cadet(getConfiguration(), null, new ChannelEndHandler() {
            @Override
            public void onChannelEnd(Cadet.Channel channel) {
                logger.warn("on channel end");
            }
        }, new TallyCadetReceiver(), CADET_PORT);

        Scheduler.addDelayed(RelativeTime.FOREVER, new Scheduler.Task() {
            @Override
            public void run(Scheduler.RunContext ctx) {
                if (null != cadet) {
                    cadet.destroy();
                    cadet = null;
                }
            }
        });
    }
}