Friday, 15 December 2017
RankTest.java (1st part) (Rank random number test Java source code file) Print E-mail
Get C++ DiceLock cipher architecture source code packages of DiceLock for Microsoft Visual Studio 2013 and DiceLock-x for Linux with Test Driver Programs and Response Test Vector files to verify that both them work as expected.
DiceLock for Windows and Linux
DiceLock Security governing software licenses are Free/Libre Source Code License and Educational and Research License

DiceLock is protected by US patent 7508945 and European Patent 1182777 where applicable.

DiceLock and DiceLock logo are trademarks or registered trademarks in the EC, USA and others.

License information can be obtained at our corporate web site




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
412
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.6.0.0.1
//
// Copyright (C) 2011-2012 DiceLock Security, LLC. All rights reserved.
//
//                               DISCLAIMER
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// DICELOCK IS A REGISTERED TRADEMARK OR TRADEMARK OF THE OWNERS.
//
// Environment:
// java version "1.6.0_29"
// Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
// Java HotSpot(TM) Server VM (build 20.4-b02, mixed mode)
//
 
package com.dicelocksecurity.jdicelock.RandomTest;
 
import com.dicelocksecurity.jdicelock.CryptoRandomStream.BaseCryptoRandomStream;
 
/**
 * Class implementing Rank random number test
 *
 * @author      Angel Ferré @ DiceLock Security
 * @version     6.0.0.1
 * @since       2011-09-30
 */
public class RankTest extends BaseRandomTest {
 
    /**
     * Random Test Class enumerator name
     */
    protected static final RandomTests TEST = RandomTests.Rank;
 
    /**
     * Random Test Class minimum stream length
     */
    protected static final int MINIMUMLENGTH = 38912;
 
    /**
     * "chiSquare" result
     */
    protected double chiSquared;
 
    /**
     * "matrixNumber" result
     */
    protected int matrixNumber;
 
    /**
     *  "bitsDiscarded" result, number of bits from the stream that has not been verified
     */
    protected int bitsDiscarded;
 
    /**
     * "p30, p31, p32" probailities
     */
    protected double p30, p31, p32; // Probabilities
 
    /**
     * "f30, f31, f32" frequencies
     */
    protected double f30, f31, f32; // Frequencies
 
    /**
     * Create Matrix
     *
     * @param   M           integer "M" value indicating matrix number of rows
     * @param   Q           integer "Q" value indicating matrix number of columns
     * @return  byte[][]:   matrix of bytes of M rows and Q columns
     */
    protected byte[][] CreateMatrix(int M, int Q) {
        byte[][] matrix;
 
        matrix = new byte[M][Q];
        if (matrix == null)
            this.error = RandomTestErrors.InsufficientMemory;
        return matrix;
    }
 
    /**
     * Define Matrix
     *
     * @param     stream      bit stream to fill matrix based on the other parameters
     * @param     M           integer "M" value indicating matrix number of rows
     * @param     Q           integer "Q" value indicating matrix number of columns
     * @param     m           byte matrix of M rows and Q columns that will be filled based on k parameter
     * @param     k           integer value that allows to traverse bit stream
     */
    protected void DefineMatrix(BaseCryptoRandomStream stream, int M, int Q, byte[][] m, int k) {
        int i, j;
 
        for (i = 0; i < M; i++)
            for (j = 0; j < Q; j++) {
                m[i][j] = stream.GetBitPosition(k * (M * Q) + j + i * M);
            }
    }
 
    /**
     * Deletes matrix
     *
     * @param     matrix      matrix that will be erased
     */
    protected void DeleteMatrix(byte[][] matrix) {
 
        matrix = null;
    }
 
    /**
     * Perform Elementary Row Operations
     *
     * @param     flag    integer indicating direction
     * @param     i       integer index from where to start direction
     * @param     M       integer indicating matrix row number
     * @param     Q       integer indicating matrix column number
     * @param     A       byte matrix of M rows and Q columns
     */
    protected void PerformElementaryRowOperations(int flag, int i, int M, int Q, byte[][] A) {
        int j, k;
 
        switch (flag) {
        case 0:
            for (j = i + 1; j < M; j++)
                if (A[j][i] == 1)
                    for (k = i; k < Q; k++)
                        A[j][k] = (byte)((A[j][k] + A[i][k]) % 2);
            break;
        case 1:
            for (j = i - 1; j >= 0; j--)
                if (A[j][i] == 1)
                    for (k = 0; k < Q; k++)
                        A[j][k] = (byte)((A[j][k] + A[i][k]) % 2);
            break;
        }
    }
 
    /**
     * Swap Rows
     *
     * @param     i       integer row to swap
     * @param     index   integer row to be swapped with i row
     * @param     Q       integer indicating matrix column number
     * @param     A       byte matrix of M rows and Q columns
     * @return    int:    always return "1" value
     */
    protected int SwapRows(int i, int index, int Q, byte[][] A) {
        int p;
        byte temp;
 
        for (p = 0; p < Q; p++) {
            temp = A[i][p];
            A[i][p] = A[index][p];
            A[index][p] = temp;
        }
        return 1;
    }
 
    /**
     * Find Unit Element And Swap
     *
     * @param     flag    integer indicating direction of search, if 0 value, from i to M, if 1 value from i to 0
     * @param     i       integer index pointing the column fromwhere to start search
     * @param     M       integer indicating matrix row number
     * @param     Q       integer indicating matrix column number
     * @param     A       byte matrix of M rows and Q columns
     * @return    int:    integer value of swap row operations performed
     */
    protected int FindUnitElementAndSwap(int flag, int i, int M, int Q, byte[][] A) {
        int index;
        int row_op = 0;
 
        switch (flag) {
        case 0:
            index = i + 1;
            while ((index < M) && (A[index][i] == 0))
                index++;
            if (index < M)
                row_op = this.SwapRows(i, index, Q, A);
            break;
        case 1:
            index = i - 1;
            while ((index >= 0) && (A[index][i] == 0))
                index--;
            if (index >= 0)
                row_op = this.SwapRows(i, index, Q, A);
            break;
        }
        return row_op;
    }
 
    /**
     * Determine Rank
     *
     * @param     m       integer to determine rank
     * @param     M       integer indicating matrix row number
     * @param     Q       integer indicating matrix column number
     * @param     A       byte matrix of M rows and Q columns
     * @return    int:    integer that determines rank
     */
    protected int DetermineRank(int m, int M, int Q, byte[][] A) {
        int i, j, rank, allZeroes;
 
        rank = m;
        for (i = 0; i < M; i++) {
            allZeroes = 1;
            for (j = 0; j < Q; j++) {
                if (A[i][j] == 1) {
                    allZeroes = 0;
                    break;
                }
            }
            if (allZeroes == 1)
                rank--;
        }
        return rank;
    }
 
    /**
     * Computes rank
     *
     * @param     M         integer indicating matrix row number
     * @param     Q         integer indicating matrix column number
     * @param     matrix    byte matrix of M rows adn Q columns
     * @return    int:      rank computed on byte matrix
     */
    protected int ComputeRank(int M, int Q, byte[][] matrix) {
        int i;
        int rank;
        int m = (int)this.mathFuncs.min(M, Q);
 
        for (i = 0; i < m - 1; i++) {
            if (matrix[i][i] == 1)
                this.PerformElementaryRowOperations(0, i, M, Q, matrix);
            else {
                if (this.FindUnitElementAndSwap(0, i, M, Q, matrix) == 1)
                    this.PerformElementaryRowOperations(0, i, M, Q, matrix);
            }
        }
        for (i = m - 1; i > 0; i--) {
            if (matrix[i][i] == 1)
                this.PerformElementaryRowOperations(1, i, M, Q, matrix);
            else {
                if (this.FindUnitElementAndSwap(1, i, M, Q, matrix) == 1)
                    this.PerformElementaryRowOperations(1, i, M, Q, matrix);
            }
        }
        rank = this.DetermineRank(m, M, Q, matrix);
        return rank;
    }
 
    /**
     * Constructor, default
     */
    public RankTest() {
 
        super();
 
        this.chiSquared = 0.0;
        this.matrixNumber = 0;
        this.bitsDiscarded = 0;
        this.p30 = 0.0;
        this.p31 = 0.0;
        this.p32 = 0.0;
        this.f30 = 0.0;
        this.f31 = 0.0;
        this.f32 = 0.0;
    }
 
    /**
     * Constructor with a MathematicalFunctions object instantiated
     *
     * @param     mathFuncObj   mathematicalFunctions object that will be used by this object
     */
    public RankTest(MathematicalFunctions mathFuncObj) {
 
        super(mathFuncObj);
 
        this.chiSquared = 0.0;
        this.matrixNumber = 0;
        this.bitsDiscarded = 0;
        this.p30 = 0.0;
        this.p31 = 0.0;
        this.p32 = 0.0;
        this.f30 = 0.0;
        this.f31 = 0.0;
        this.f32 = 0.0;
    }
 
    /**
     * Destructor, zeroes all data
     *
     */
    public void finalize() {
 
        this.chiSquared = 0.0;
        this.matrixNumber = 0;
        this.bitsDiscarded = 0;
        this.p30 = 0.0;
        this.p31 = 0.0;
        this.p32 = 0.0;
        this.f30 = 0.0;
        this.f31 = 0.0;
        this.f32 = 0.0;
    }
 
    /**
     * Gets the BaseRandomTest random state of the last executed BaseCryptoRandomStream
     *
     * @return    boolean indication if last computed CryptoRandomStream was a randomized stream
     *            true:   last verified stream was randomized
     *            false:  last verified stream was not randomized
     */
    public boolean IsRandom() {
 
        return super.IsRandom();
    }
 
    /**
     * Tests the BaseCryptoRandomStream executed and returns the random value
     *
     * @param     bitStream   bitStream to be verified for randomness properties
     * @return    boolean     indication if CryptoRandomStream is a randomized stream
     *            true:       last verified stream was randomized
     *            false:      last verified stream was not randomized
     */
    public boolean IsRandom(BaseCryptoRandomStream bitStream) {
        int r;
        double product;
        int i, k;
        double arg1;
        double R;
        byte[][] matrix = this.CreateMatrix(32, 32);
 
 
        if (matrix != null) {
            if (bitStream.GetBitLength() < this.GetMinimumLength()) {
                this.error = RandomTestErrors.InsufficientNumberOfBits;
                this.random = false;
                return this.random;
            }
            this.error = RandomTestErrors.NoError;
            this.matrixNumber = (int)Math.floor((double)bitStream.GetBitLength() / (32 * 32));
            if (this.mathFuncs.isZero(this.matrixNumber)) {
                this.error = RandomTestErrors.InsufficientNumberOfBits;
                this.pValue = 0.00;
                this.random = false;
            } else {
                this.bitsDiscarded = bitStream.GetBitLength() % (32 * 32);
                r = 32;
                product = 1;
                for (i = 0; i <= r - 1; i++)
                    product *=
                            ((1.e0 - Math.pow(2, i - 32)) * (1.e0 - Math.pow(2, i - 32))) / (1.e0 - Math.pow(2, i - r));
                this.p32 = Math.pow(2, r * (32 + 32 - r) - 32 * 32) * product;
                r = 31;
                product = 1;
                for (i = 0; i <= r - 1; i++)
                    product *=
                            ((1.e0 - Math.pow(2, i - 32)) * (1.e0 - Math.pow(2, i - 32))) / (1.e0 - Math.pow(2, i - r));
                this.p31 = Math.pow(2, r * (32 + 32 - r) - 32 * 32) * product;
                this.p30 = 1 - (this.p32 + this.p31);
                this.f32 = 0;
                this.f31 = 0;
                for (k = 0; k < this.matrixNumber; k++) {
                    this.DefineMatrix(bitStream, 32, 32, matrix, k);
                    R = this.ComputeRank(32, 32, matrix);
                    if (R == 32)
                        this.f32++;
                    if (R == 31)
                        this.f31++;
                }
                this.f30 = (double)this.matrixNumber - (this.f32 + this.f31);
                this.chiSquared =
                        ((Math.pow(this.f32 - this.matrixNumber * this.p32, 2) / (double)(this.matrixNumber * this.p32) +
                          Math.pow(this.f31 - this.matrixNumber * this.p31, 2) /
                          (double)(this.matrixNumber * this.p31) +
                          Math.pow(this.f30 - this.matrixNumber * this.p30, 2) /
                          (double)(this.matrixNumber * this.p30)));
                arg1 = -this.chiSquared / 2.e0;
                this.pValue = Math.exp(arg1);
                if (this.pValue < this.alpha) {
                    this.random = false;
                } else {
                    this.random = true;
                }
                matrix = null;
            }
            if (this.mathFuncs.isNegative(this.pValue) || this.mathFuncs.isGreaterThanOne(this.pValue)) {
                this.error = RandomTestErrors.PValueOutOfRange;
                this.random = false;
            }
        } else {
            this.error = RandomTestErrors.InsufficientMemory;
            this.random = false;
        }
        return this.random;
    }