aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/voting/simulation/VotingParameters.java
blob: bc6b987d60d38a7c7a03101feb25ec7d035674f2 (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
package org.gnunet.voting.simulation;

import java.math.BigInteger;
import java.security.SecureRandom;

/**
 * Utilities for the modified ElGamal algorithm.
 * <p/>
 * p is a large prime, and g is a high-order element (or even a generator) of Zp*
 *
 * @author Florian Dold
 */
public class VotingParameters {
    // large prime, p = 2q + 1
    public final BigInteger p;
    // large prime, so that q divides (p-1)
    public final BigInteger q;
    // generator of Gq
    public final BigInteger g;

    public final int authorityCount;
    public final int authorityThreshold;

    public VotingParameters(BigInteger p, BigInteger q, BigInteger g, int authorityCount, int authorityThreshold) {
        this.p = p;
        this.q = q;
        this.g = g;
        this.authorityCount = authorityCount;
        this.authorityThreshold = authorityThreshold;
    }

    /**
     * which generates the p and g values from the given parameters,
     * returning the ElGamalScheme object.
     * <p/>
     * Note: can take a while...
     */
    public static VotingParameters generateRandomParameters(int size, int certainty, int authorityCount, int authorityThreshold) {
        BigInteger[] safePrimes = generateSafePrimes(size, certainty);
        BigInteger p = safePrimes[0];
        BigInteger q = safePrimes[1];
        BigInteger alpha = selectGenerator(p, q);
        BigInteger g = selectSubgroupHigherOrderElement(alpha, p, q);
        if (!g.modPow(q, p).equals(BigInteger.ONE)) {
            throw new AssertionError();
        }
        if (!(g.compareTo(p) < 0)) {
            throw new AssertionError();
        }
        return new VotingParameters(p, q, g, authorityCount, authorityThreshold);
    }

    /**
     * Finds a pair of prime BigInteger's {p, q: p = 2q + 1}, called safe primes.
     * <p/>
     * (see: Handbook of Applied Cryptography 4.86)
     *
     * @return A 2-element array {p,q} of safe primes.
     */
    private static BigInteger[] generateSafePrimes(int size, int certainty) {
        BigInteger p, q;
        int qLength = size - 1;

        while (true) {
            q = new BigInteger(qLength, 2, CryptoUtil.random);

            // p <- 2q + 1
            p = q.shiftLeft(1).add(BigInteger.ONE);

            // XXX(dold): why do we test q for primality again?
            if (p.isProbablePrime(certainty) && (certainty <= 2 || q.isProbablePrime(certainty))) {
                break;
            }
        }

        return new BigInteger[]{p, q};
    }

    /*
    * Select a high order element of the multiplicative group Zn*
    */
    private static BigInteger selectHighOrderElement(BigInteger n, SecureRandom random) {
        BigInteger g;
        final BigInteger nMinusTwo = n.subtract(BigInteger.valueOf(2));
        do {
            BigInteger h = CryptoUtil.createRandomInRange(BigInteger.valueOf(2), nMinusTwo);

            g = h.modPow(BigInteger.valueOf(2), n);
        }
        while (g.equals(BigInteger.valueOf(1)));

        return g;
    }

    /**
     * Returns a higher-order-element of Gq, the subgroup of Zp*, with order q where alpha is a generator of Zp*
     *
     * (see Handbook of Applied Cryptography 4.81)
     */
    private static BigInteger selectSubgroupHigherOrderElement(BigInteger alpha, BigInteger p, BigInteger q) {
        return alpha.modPow(p.subtract(BigInteger.ONE).divide(q), p);
    }

    /**
     * Get the size of the cyclic group Gp used for ElGamal
     *
     * @return the size of the cyclic group Gp used for ElGamal
     */
    public BigInteger getP() {
        return p;
    }

    /**
     * Get the generator of Gq
     *
     * @return the generator of Gq
     */
    public BigInteger getG() {
        return g;
    }

    /**
     * Get the generator of Zp*
     *
     * @return the generator of Zp*
     */
    public BigInteger getQ() {
        return q;
    }

    public BigInteger generateGq() {
        BigInteger r;
        while (true) {
            r = CryptoUtil.createRandomInRange(BigInteger.ZERO, this.p);
            if (r.modPow(q, p).equals(BigInteger.ONE)) {
                break;
            }
        }
        return r;
    }

    public static BigInteger selectGenerator(BigInteger p, BigInteger q) {
        BigInteger pMinusTwo = p.subtract(BigInteger.valueOf(2));
        BigInteger g;
        /*
         * (see: Handbook of Applied Cryptography 4.80)
         */
        do {
            g = CryptoUtil.createRandomInRange(BigInteger.valueOf(2), pMinusTwo);
        }
        while (g.modPow(BigInteger.valueOf(2), p).equals(BigInteger.ONE) || g.modPow(q, p).equals(BigInteger.ONE));
        return g;
    }

    public BigInteger generateZq() {
        return CryptoUtil.createRandomInRange(BigInteger.ZERO, this.q.subtract(BigInteger.ONE));
    }

    public Cyphertext encrypt(BigInteger message, BigInteger publicKey) {
        BigInteger secret = generateZq();
        return new Cyphertext(g.modPow(secret, p), message.multiply(publicKey.modPow(secret, p).mod(p)));
    }


}