aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/gnunet/voting/TestVotingCrypto.java
blob: 0f6204e9f45127522fff1ad4db09d58a19c8698d (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
/*
 This file is part of GNUnet.
 (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 org.gnunet.construct.Construct;
import org.gnunet.secretsharing.*;
import org.gnunet.testing.TestingFixture;
import org.gnunet.testing.TestingSubsystem;
import org.gnunet.util.*;
import org.gnunet.util.crypto.EcdsaPrivateKey;
import org.gnunet.util.crypto.EcdsaPublicKey;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

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

public class TestVotingCrypto extends TestingFixture {

    static BigInteger[] generators2;

    @BeforeClass
    public static void generateGenerators() {
        generators2 = Parameters.generateGenerators(2);
    }

    @Test
    public void testEmptyTally() {
        final Wrapper<Boolean> success = new Wrapper<Boolean>();
        Configuration armConf = new Configuration();
        armConf.setValueString("arm", "DEFAULTSERVICES", "consensus set secretsharing");
        Program.configureLogging("DEBUG");
        // for testing, we vote with the same voter identity multiple times,
        // does not matter here ...
        final EcdsaPrivateKey privateKey = EcdsaPrivateKey.createRandom();
        final EcdsaPublicKey publicKey = privateKey.getPublicKey();
        final TestingSubsystem ts = new TestingSubsystem("arm", armConf.writeTemp().getAbsolutePath());
        KeyGeneration kg = new KeyGeneration(ts.getConfiguration(), new PeerIdentity[0],
                HashCode.random(), AbsoluteTime.now(), AbsoluteTime.now().add(RelativeTime.fromSeconds(5)),
                1, new SecretReadyCallback() {
            @Override
            public void onSecretReady(Share share) {
                Assert.assertNotNull(share);
                Ciphertext sum = Ciphertext.identity();
                Decryption decryption = new Decryption(ts.getConfiguration(), share, sum,
                        AbsoluteTime.now(), AbsoluteTime.now().add(RelativeTime.fromSeconds(5)), new DecryptCallback() {
                    @Override
                    public void onResult(Plaintext plaintext) {
                        Assert.assertNotNull(plaintext);
                        System.out.println("Plaintext: " + plaintext);
                        long x[] = plaintext.bruteForceDiscreteLog(0, new BigInteger[0]);
                        Assert.assertNotNull(x);
                        success.set(true);
                    }
                });
            }
        });
        Scheduler.run();
        Assert.assertTrue(success.get());
    }


    @Test
    public void testVoteTally() {
        final Wrapper<Boolean> success = new Wrapper<Boolean>();
        Configuration armConf = new Configuration();
        armConf.setValueString("arm", "DEFAULTSERVICES", "consensus set secretsharing");
        Program.configureLogging("DEBUG");
        // for testing, we vote with the same voter identity multiple times,
        // does not matter here ...
        final EcdsaPrivateKey privateKey = EcdsaPrivateKey.createRandom();
        final EcdsaPublicKey publicKey = privateKey.getPublicKey();
        final TestingSubsystem ts = new TestingSubsystem("arm", armConf.writeTemp().getAbsolutePath());
        KeyGeneration kg = new KeyGeneration(ts.getConfiguration(), new PeerIdentity[0],
                HashCode.random(), AbsoluteTime.now(), AbsoluteTime.now().add(RelativeTime.fromSeconds(5)),
                1, new SecretReadyCallback() {
            @Override
            public void onSecretReady(Share share) {
                Assert.assertNotNull(share);
                EncryptedVote encryptedVote1 = EncryptedVote.fromChoice(1, share.publicKey, publicKey, generators2);
                EncryptedVote encryptedVote2 = EncryptedVote.fromChoice(0, share.publicKey, publicKey, generators2);
                EncryptedVote encryptedVote3 = EncryptedVote.fromChoice(0, share.publicKey, publicKey, generators2);

                Ciphertext sum = Ciphertext.identity().multiply(encryptedVote1.v.multiply(encryptedVote2.v).multiply(encryptedVote3.v));

                Decryption decryption = new Decryption(ts.getConfiguration(), share, sum,
                        AbsoluteTime.now(), AbsoluteTime.now().add(RelativeTime.fromSeconds(5)), new DecryptCallback() {

                    @Override
                    public void onResult(Plaintext plaintext) {
                        Assert.assertNotNull(plaintext);
                        long[] t = plaintext.bruteForceDiscreteLog(3, generators2);
                        Assert.assertNotNull(t);
                        Assert.assertEquals(t[0], 2);
                        Assert.assertEquals(t[1], 1);
                        success.set(true);
                    }
                });
            }
        });
        Scheduler.run();
        Assert.assertTrue(success.get());
    }


    @Test
    public void testSerialization() {
        final Wrapper<Boolean> success = new Wrapper<Boolean>();
        Configuration armConf = new Configuration();
        armConf.setValueString("arm", "DEFAULTSERVICES", "consensus set secretsharing");
        Program.configureLogging("DEBUG");
        final TestingSubsystem ts = new TestingSubsystem("arm", armConf.writeTemp().getAbsolutePath());
        final EcdsaPrivateKey privateKey = EcdsaPrivateKey.createRandom();
        final EcdsaPublicKey publicKey = privateKey.getPublicKey();
        final KeyGeneration kg = new KeyGeneration(ts.getConfiguration(), new PeerIdentity[0],
                HashCode.random(), AbsoluteTime.now(), AbsoluteTime.now().add(RelativeTime.fromSeconds(5)),
                1, new SecretReadyCallback() {
            @Override
            public void onSecretReady(Share share) {
                Assert.assertNotNull(share);
                EncryptedVote encryptedVote1 = EncryptedVote.fromChoice(1, share.publicKey, publicKey, generators2);
                Assert.assertTrue(encryptedVote1.disjunctionZkp.verifyChallenge());
                Configuration c = new Configuration();
                encryptedVote1.writeToConfiguration(c);
                System.out.println("Challenge:");
                System.out.println(Arrays.toString(encryptedVote1.disjunctionZkp.challenge_c));
                System.out.println("ZKP:");
                System.out.println(Arrays.toString(Construct.toBinary(encryptedVote1.disjunctionZkp)));
                System.out.println("configuration: ");
                System.out.println(c.serialize());
                EncryptedVote encryptedVote2 = EncryptedVote.parseFromConfiguration(c, publicKey);
                Assert.assertTrue(encryptedVote1.disjunctionZkp.verifyChallenge());
                Assert.assertEquals(encryptedVote1.v, encryptedVote2.v);
                success.set(true);
            }

        });
        Scheduler.run();
        Assert.assertTrue(success.get());
    }

}