Friday, 15 December 2017
arrow JDiceChecker 5.0.0.1 arrow RandomTest arrow RankTest.java (2)
RankTest.java (2nd part) (Rank random number test class) 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
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
 
  /**
   * 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;
    }
 
  /**
   * Initializes the object
   * 
   */ 
    public void Initialize() {
          
      super.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
   * 
   * @return    RandomTests:     the concrete type class of the random number test, Rank test for this class
   */ 
    public RandomTests GetType() {
          
      return RankTest.TEST;
    }
 
  /**
   * Gets the minimum random stream length
   * 
   * @return    int:    minimum length in bits of streams that can be checked by this test 
   */ 
    public int GetMinimumLength() {
          
      return RankTest.MINIMUMLENGTH;
    }
 
  /**
   * Gets the "chiSquared" result
   * 
   * @return    double:   "chiSquared" result
   */ 
    public double GetChiSquared() {
          
      return this.chiSquared;
    }
 
  /**
   * Gets the "matrixNumber" result
   * 
   * @return    int:   "matrixNumber" result, number of 32 x 32 byte matrixes that divides bit stream
   */ 
    public int GetMatrixNumber() {
          
      return this.matrixNumber;
    }
 
  /**
   * Gets the "bitsDiscarded" result
   * 
   * @return    int:   "bitsDiscarded" result, bit stream number of bits that have not been tested
   */ 
    public int GetBitsDiscarded() {
          
      return this.bitsDiscarded;
    }
 
  /**
   * Gets the "p30" result
   * 
   * @return    double:   "p30" probability result
   */ 
    public double GetP30() {
          
      return this.p30;
    }
 
  /**
   * Gets the "p31" result
   * 
   * @return    double:   "p31" probability result
   */ 
    public double GetP31() {
          
      return this.p31;
    }
 
  /**
   * Gets the "p32" result
   * 
   * @return    double:   "p32" probability result
   */ 
    public double GetP32() {
          
      return this.p32;
    }
 
  /**
   * Gets the "f30" result
   * 
   * @return    double:   "f30" frequency result
   */ 
    public double GetF30() {
          
      return this.f30;
    }
 
  /**
   * Gets the "f31" result
   * 
   * @return    double:   "f31" frequency result
   */ 
    public double GetF31() {
          
      return this.f31;
    }
 
  /**
   * Gets the "f32" result
   * 
   * @return    double:   "f32" frequency result
   */ 
    public double GetF32() {
          
      return this.f32;
    }
 
}