Tuesday, 24 October 2017
rankTest.cpp (Rank random number test 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
As per NIST FIPS 800-22rev1a   Get FREE C++ Source Code  
Get Eclipse project and source files !!! visual-studio-2008.png

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
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.5.0.0.1
//
// Copyright � 2008-2011 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.
// 
 
#include <stdexcept>
#include <stdlib.h>
#include <math.h>
#include "rankTest.h"
 
 
using namespace std;
 
 
namespace DiceLockSecurity {
  
  namespace RandomTest {
 
  // Random Test Class enumerator name
  const RandomTests RankTest::test = Rank;
  // Random Test Class minimum stream length
  const unsigned int  RankTest::minimumLength = 38912;
 
  // Constructor, default 
  RankTest::RankTest() {
 
    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 
  RankTest::RankTest(MathematicalFunctions* 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
  RankTest::~RankTest() {
 
    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
  bool RankTest::IsRandom(void) {
 
    return BaseRandomTest::IsRandom();
  }
 
  // Tests randomness of the BaseCryptoRandomStream and returns the random value
  bool RankTest::IsRandom(BaseCryptoRandomStream* bitStream) {
    int        r;
    double     product;
    int        i, k;
    double     arg1;
    double     R;          
    unsigned char** matrix = CreateMatrix(32, 32);
  
 
    if (matrix != NULL) {
      if (bitStream->GetBitLength() < this->GetMinimumLength()) {
        this->error = InsufficientNumberOfBits;
        this->random = false;
        return this->random;
      }
      this->error = NoError;
      this->matrixNumber = (int) floor((long double)bitStream->GetBitLength()/(32*32));  
      if (isZero(this->matrixNumber)) {
        this->error = 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-pow((double)2,i-32))*(1.e0-pow((double)2,i-32)))/(1.e0-pow((double)2,i-r));
        this->p32 = pow((double)2,r*(32+32-r)-32*32) * product;
        r = 31;
        product = 1;
        for(i = 0; i <= r-1; i++)
          product *= ((1.e0-pow((double)2,i-32))*(1.e0-pow((double)2,i-32)))/(1.e0-pow((double)2,i-r));
        this->p31 = pow((double)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++) {      
          DefineMatrix(bitStream, 32, 32, matrix, k);
          R = 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 =(double)((pow(this->f32 - this->matrixNumber*this->p32,2)/(double)(this->matrixNumber*this->p32) +
                              pow(this->f31 - this->matrixNumber*this->p31,2)/(double)(this->matrixNumber*this->p31) +
                              pow(this->f30 - this->matrixNumber*this->p30,2)/(double)(this->matrixNumber*this->p30)));
        arg1 = -this->chiSquared/2.e0;
        this->pValue = exp(arg1);
        if (this->pValue < this->alpha) {  
            this->random = false;
        }
        else {
          this->random = true;
        }
        for(i = 0; i < 32; i++)      
          free(matrix[i]);
        free(matrix);
      }
      if (isNegative(this->pValue) || isGreaterThanOne(this->pValue)) {
        this->error = PValueOutOfRange;
        this->random = false;
      }
    }  
    else {
        this->error = InsufficientMemory;
      this->random = false; 
    }
    return this->random;
  }
 
  // Initializes the object
  void RankTest::Initialize(void) {
 
    BaseRandomTest::Initialize();
    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 type of the object
  RandomTests RankTest::GetType(void) {
 
    return this->test;
  }
 
  // Gets the minimum random stream length
  unsigned int RankTest::GetMinimumLength(void) {
 
    return this->minimumLength;
  }
 
  // Gets the "chiSquared" result
  double RankTest::GetChiSquared(void) {
 
    return this->chiSquared;
  }
 
  // Gets the "matrixNumber" result
  int RankTest::GetMatrixNumber(void) {
 
    return this->matrixNumber;
  }
 
  // Gets the "bitsDiscarded" result
  int RankTest::GetBitsDiscarded(void) {
 
    return this->bitsDiscarded;
  }
 
  // Gets the "p30" result
  double RankTest::GetP30(void) {
 
    return this->p30;
  }
 
  // Gets the "p31" result
  double RankTest::GetP31(void) {
 
    return this->p31;
  }
  // Gets the "p32" result
  double RankTest::GetP32(void) {
 
    return this->p32;
  }
 
  // Gets the "f30" result
  double RankTest::GetF30(void) {
 
    return this->f30;
  }
 
  // Gets the "f31" result
  double RankTest::GetF31(void) {
 
    return this->f31;
  }
  // Gets the "f32" result
  double RankTest::GetF32(void) {
 
    return this->f32;
  }
 
  // Create Matrix 
  unsigned char** RankTest::CreateMatrix(int M, int Q) {
    int i;
    unsigned char** matrix;  
 
    if ((matrix = (unsigned char**) calloc(M, sizeof(unsigned char *))) == NULL) {
      this->error = InsufficientMemory;
      return matrix;  
    }
    else {
      for (i = 0; i < M; i++) {
        if ((matrix[i] = (unsigned char*)calloc(Q, sizeof(unsigned char))) == NULL) {
          this->error = InsufficientMemory;
              return NULL;
        }
      }
      return matrix;
    }
  }
 
  // Define Matrix 
  void RankTest::DefineMatrix(BaseCryptoRandomStream* stream, int M, int Q, unsigned char** 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);
      }
    return;
  }
 
  // Deletes matrix 
  void RankTest::DeleteMatrix(int M, unsigned char** matrix) {
    int i;
  
    for (i = 0; i < M; i++)
      free(matrix[i]);
    free(matrix);
  }
 
  // Computes rank 
  int RankTest::ComputeRank(int M, int Q, unsigned char** matrix) {
    int i;
    int rank;
    int m = MIN(M,Q);
 
    for(i = 0; i < m-1; i++) {
      if (matrix[i][i] == 1) 
        PerformElementaryRowOperations(0, i, M, Q, matrix);
      else {   
        if (FindUnitElementAndSwap(0, i, M, Q, matrix) == 1) 
          PerformElementaryRowOperations(0, i, M, Q, matrix);
      }  
     }  
    for(i = m-1; i > 0; i--) {
      if (matrix[i][i] == 1)
        PerformElementaryRowOperations(1, i, M, Q, matrix);
      else {   
        if (FindUnitElementAndSwap(1, i, M, Q, matrix) == 1) 
          PerformElementaryRowOperations(1, i, M, Q, matrix);
      }
    } 
    rank = DetermineRank(m,M,Q,matrix);
    return rank;
  }  
 
  // Perform Elementary Row Operations
  void RankTest::PerformElementaryRowOperations(int flag, int i, int M, int Q, unsigned char** 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] = (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] = (A[j][k] + A[i][k]) % 2;
          break;
    }
    return;
  }
 
  // Find Unit Element And Swap
  int  RankTest::FindUnitElementAndSwap(int flag, int i, int M, int Q, unsigned char** 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 = SwapRows(i,index,Q,A);
          break;
      case 1:
          index = i-1;
          while ((index >= 0) && (A[index][i] == 0)) 
            index--;
          if (index >= 0) 
            row_op = SwapRows(i,index,Q,A);
          break;
    }
    return row_op;
  }
 
  // Swap Rows 
  int  RankTest::SwapRows(int i, int index, int Q, unsigned char** A) {
    int p;
    unsigned char temp;
 
    for(p = 0; p < Q; p++) {
      temp = A[i][p];
      A[i][p] = A[index][p];
      A[index][p] = temp;
    }
    return 1;
  }
 
  // Determine Rank 
  int  RankTest::DetermineRank(int m, int M, int Q, unsigned char** 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;
  }
  }
}