aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/voting/EncryptedVote.java
blob: 1a38bc0fc6b551f4c58e16447644e979acf4133c (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
/*
 This file is part of GNUnet.
 Copyright (C) 2014 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.base.Optional;
import org.gnunet.construct.Construct;
import org.gnunet.construct.Message;
import org.gnunet.construct.NestedMessage;
import org.gnunet.secretsharing.Ciphertext;
import org.gnunet.secretsharing.Parameters;
import org.gnunet.secretsharing.ThresholdPublicKey;
import org.gnunet.util.BigIntegers;
import org.gnunet.util.Configuration;
import org.gnunet.util.Strings;
import org.gnunet.util.crypto.EcdsaPublicKey;
import org.gnunet.util.crypto.SignedContentMessage;

import java.math.BigInteger;
import java.util.Arrays;

public class EncryptedVote implements Message, SignedContentMessage {
    @NestedMessage
    public Ciphertext v;

    @NestedMessage
    public DisjunctionZkp disjunctionZkp;

    @NestedMessage
    public EcdsaPublicKey voterPublicKey;

    /**
     * Construct an EncryptedVote by parsing it from the configuration.
     * The voter public key must be passed separately.
     *
     * @param cfg configuration
     * @param voterPublicKey public key of the voter
     * @return the encrypted vote
     */
    public static EncryptedVote parseFromConfiguration(Configuration cfg, EcdsaPublicKey voterPublicKey) {
        Optional<String> optVal = cfg.getValueString("vote", "ENCRYPTED_VOTE_VAL");
        if (!optVal.isPresent()) {
            return null;
        }
        Optional<String> optZkp = cfg.getValueString("vote", "ZKP");
        if (!optZkp.isPresent()) {
            return null;
        }
        EncryptedVote encryptedVote = new EncryptedVote();
        encryptedVote.voterPublicKey = voterPublicKey;
        encryptedVote.v = Ciphertext.fromString(optVal.get());
        System.out.println("reading, string size " + optZkp.get().length());
        byte[] zkpData = Strings.stringToData(optZkp.get());
        if (null == zkpData) {
            throw new InvalidBallotException("could not read ZKP from ballot");
        }
        encryptedVote.disjunctionZkp = Construct.parseAs(zkpData, DisjunctionZkp.class);
        return encryptedVote;
    }

    public static EncryptedVote fromChoice(int choiceId, ThresholdPublicKey thresholdPublicKey, EcdsaPublicKey voterPublicKey,
                                           BigInteger[] generators) {

        EncryptedVote encryptedVote = new EncryptedVote();
        encryptedVote.v = new Ciphertext();
        encryptedVote.voterPublicKey = voterPublicKey;
        encryptedVote.disjunctionZkp = new DisjunctionZkp();

        encryptedVote.disjunctionZkp.chaumPedersenZkps = new ChaumPedersenZkp[generators.length];
        encryptedVote.disjunctionZkp.numProofs = generators.length;

        // the discrete logarithm
        BigInteger alpha = Parameters.randomQ();
        // the secret for the ZKP
        BigInteger w = Parameters.randomQ();

        BigInteger h = new BigInteger(1, thresholdPublicKey.bits);
        BigInteger g = Parameters.elgamalG;
        BigInteger p = Parameters.elgamalP;
        BigInteger q = Parameters.elgamalQ;

        BigInteger x = Parameters.modPowG(alpha);
        BigInteger y = h.modPow(alpha, p).multiply(generators[choiceId]).mod(p);

        encryptedVote.v.c_1 = BigIntegers.serializeUnsigned(x, Parameters.elgamalBits);
        encryptedVote.v.c_2 = BigIntegers.serializeUnsigned(y, Parameters.elgamalBits);

        // sum of all the simulation challenges
        BigInteger d_sim_sum = BigInteger.ZERO;

        // generate simulated proofs
        for (int i = 0; i < generators.length; i++) {
            if (i == choiceId) {
                continue;
            }
            BigInteger r = Parameters.randomQ();
            BigInteger d = Parameters.randomQ();
            BigInteger a = g.modPow(r, p).multiply(x.modPow(d, p)).mod(p);
            BigInteger b = h.modPow(r, p).multiply(y.multiply(generators[i].modInverse(p)).modPow(d, p)).mod(p);

            d_sim_sum = d_sim_sum.add(d);

            ChaumPedersenZkp zkp = new ChaumPedersenZkp();
            zkp.challenge_d = BigIntegers.serializeUnsigned(d, Parameters.elgamalBits);
            zkp.response_r = BigIntegers.serializeUnsigned(r, Parameters.elgamalBits);
            zkp.commit_a = BigIntegers.serializeUnsigned(a, Parameters.elgamalBits);
            zkp.commit_b = BigIntegers.serializeUnsigned(b, Parameters.elgamalBits);

            if (!zkp.verifySim(x, y, generators[i], h)) {
                throw new AssertionError("crypto not working");
            }

            encryptedVote.disjunctionZkp.chaumPedersenZkps[i] = zkp;
        }

        ChaumPedersenZkp zkp = new ChaumPedersenZkp();
        encryptedVote.disjunctionZkp.chaumPedersenZkps[choiceId] = zkp;
        BigInteger a = g.modPow(w, p);
        BigInteger b = h.modPow(w, p);
        zkp.commit_a = BigIntegers.serializeUnsigned(a, Parameters.elgamalBits);
        zkp.commit_b = BigIntegers.serializeUnsigned(b, Parameters.elgamalBits);


        BigInteger c = encryptedVote.disjunctionZkp.computeChallengeFromCommits();

        BigInteger d = c.subtract(d_sim_sum).mod(q);
        BigInteger r = w.subtract(alpha.multiply(d)).mod(q);


        zkp.challenge_d = BigIntegers.serializeUnsigned(d, Parameters.elgamalBits);
        zkp.response_r = BigIntegers.serializeUnsigned(r, Parameters.elgamalBits);


        if (!zkp.verifySim(x,y,generators[choiceId], h)) {
            throw new AssertionError("crypto (2) not working");
        }

        encryptedVote.disjunctionZkp.challenge_c = BigIntegers.serializeUnsigned(c, Parameters.elgamalBits);

        if (!encryptedVote.disjunctionZkp.verifyChallenge()) {
            throw new AssertionError("crypto not working (3)");
        }

        return encryptedVote;
    }

    public void writeToConfiguration(Configuration cfg) {
        cfg.setValueString("vote", "ENCRYPTED_VOTE_VAL", v.toString());
        byte[] zkpData = Construct.toBinary(disjunctionZkp);
        String zkpString = Strings.dataToString(zkpData);
        if (zkpString.length() != Strings.getEncodedStringLength(zkpData.length))
            throw new AssertionError("fail");
        if (zkpData.length != Strings.getDecodedDataLength(zkpString.length()))
            throw new AssertionError("fail, got " + zkpData.length + " expected " +
                    Strings.getDecodedDataLength(zkpString.length()) + "for string length" + zkpString.length());
        System.out.println("everyting ok, size binary size" + zkpData.length +" str size " + zkpString.length());
        byte[] zkpData2 = Strings.stringToData(zkpString);
        if (!Arrays.equals(zkpData, zkpData2)) {
            throw new AssertionError("something wrong");
        }
        System.out.println("everyting ok, size binary size" + zkpData.length +" str size " + zkpString.length());
        cfg.setValueString("vote", "ZKP", zkpString);
    }

    public boolean verify() {
        return false;
    }
}