~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/engine/random/CryptoRandomSource.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or modify
 
3
 * it under the terms of the GNU General Public License as published by
 
4
 * the Free Software Foundation; either version 2 of the License, or
 
5
 * (at your option) any later version.
 
6
 * This program is distributed in the hope that it will be useful,
 
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
 * GNU General Public License for more details.
 
10
 * You should have received a copy of the GNU General Public License
 
11
 * along with this program; if not, write to the Free Software
 
12
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
13
 */
 
14
 
 
15
package games.strategy.engine.random;
 
16
 
 
17
import games.strategy.engine.data.PlayerID;
 
18
import games.strategy.engine.framework.*;
 
19
import games.strategy.engine.vault.*;
 
20
 
 
21
/**
 
22
 * A random source that generates numbers using a secure algorithm shared
 
23
 * between two players.
 
24
 * 
 
25
 * Code originally contributed by Ben Giddings.
 
26
 */
 
27
 
 
28
public class CryptoRandomSource implements IRandomSource
 
29
{
 
30
 
 
31
    private final IRandomSource m_plainRandom = new PlainRandomSource();
 
32
 
 
33
    /**
 
34
     * converts an int[] to a bytep[
 
35
     */
 
36
    public static byte[] intsToBytes(int[] ints)
 
37
    {
 
38
        byte[] rVal = new byte[ints.length * 4];
 
39
 
 
40
        for (int i = 0; i < ints.length; i++)
 
41
        {
 
42
            rVal[4 * i] = (byte) (0x000000FF & ints[i]);
 
43
            rVal[(4 * i) + 1] = (byte) ((0x000000FF & (ints[i] >> 8)));
 
44
            rVal[(4 * i) + 2] = (byte) ((0x000000FF & (ints[i] >> 16)));
 
45
            rVal[(4 * i) + 3] = (byte) ((0x000000FF & (ints[i] >> 24)));
 
46
        }
 
47
 
 
48
        return rVal;
 
49
    }
 
50
 
 
51
    static int byteToIntUnsigned(byte val)
 
52
    {
 
53
        return val & 0xff;  
 
54
    }
 
55
    
 
56
    public static int[] bytesToInts(byte[] bytes)
 
57
    {
 
58
        int[] rVal = new int[bytes.length / 4];
 
59
        for (int i = 0; i < rVal.length; i++)
 
60
        {
 
61
            rVal[i] = byteToIntUnsigned(bytes[4*i]) +
 
62
                          (byteToIntUnsigned(bytes[4*i + 1]) << 8) +
 
63
                          (byteToIntUnsigned(bytes[4*i + 2]) << 16) +
 
64
                          (byteToIntUnsigned(bytes[4*i + 3]) << 24);
 
65
        }
 
66
 
 
67
        return rVal;
 
68
    }
 
69
 
 
70
    public static int[] xor(int[] val1, int[] val2, int max)
 
71
    {
 
72
        if (val1.length != val2.length)
 
73
        {
 
74
            throw new IllegalArgumentException("Arrays not of same length");
 
75
        }
 
76
        int[] rVal = new int[val1.length];
 
77
        for (int i = 0; i < val1.length; i++)
 
78
        {
 
79
            rVal[i] = (val1[i] + val2[i]) % max;
 
80
        }
 
81
 
 
82
        return rVal;
 
83
 
 
84
    }
 
85
 
 
86
    //the remote players who involved in rolling the dice
 
87
    //dice are rolled securly between us and her
 
88
    final private PlayerID m_remotePlayer;
 
89
 
 
90
    final private IGame m_game;
 
91
 
 
92
    public CryptoRandomSource(PlayerID remotePlayer, IGame game)
 
93
    {
 
94
        m_remotePlayer = remotePlayer;
 
95
        m_game = game;
 
96
 
 
97
    }
 
98
 
 
99
    /**
 
100
     * All delegates should use random data that comes from both players so that
 
101
     * neither player cheats.
 
102
     */
 
103
    public int getRandom(int max, String annotation)
 
104
    {
 
105
        return getRandom(max, 1, annotation)[0];
 
106
    }
 
107
 
 
108
    /**
 
109
     * Delegates should not use random data that comes from any other source.
 
110
     */
 
111
    public int[] getRandom(int max, int count, String annotation)
 
112
    {
 
113
        if (count <= 0)
 
114
            throw new IllegalArgumentException("Invalid count:" + count);
 
115
 
 
116
        Vault vault = m_game.getVault();
 
117
 
 
118
        //generate numbers locally, and put them in the vault
 
119
        int[] localRandom = m_plainRandom.getRandom(max, count, annotation);
 
120
        
 
121
        //lock it so the client knows that its there, but cant read it
 
122
        VaultID localID = vault.lock(intsToBytes(localRandom));
 
123
 
 
124
        //ask the remote to generate numbers
 
125
        IRemoteRandom remote = (IRemoteRandom) (m_game.getRemoteMessenger().getRemote(ServerGame.getRemoteRandomName(m_remotePlayer)));
 
126
        int[] remoteNumbers = remote.generate(max, count, annotation, localID);
 
127
 
 
128
 
 
129
        //unlock ours, tell the client he can verify
 
130
        vault.unlock(localID);
 
131
        remote.verifyNumbers();
 
132
        
 
133
        //finally, we join the two together to get the real value
 
134
        return xor(localRandom, remoteNumbers, max);
 
135
 
 
136
    }
 
137
}
 
 
b'\\ No newline at end of file'