Friday, 20 October 2017
arrow JDiceChecker 5.0.0.1 arrow RandomTest arrow DiscreteFourierTransformTest.java (3)
DiscreteFourierTransformTest.java (3rd part) (Discrete Fourier Transform 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
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
 
  /**
   * drftf1 method
   * 
   * @param    n     int 
   * @param    c    double array  
   * @param    ch    double array  
   * @param    ifac  int array  
   */ 
    protected void drftf1(int n, double[] c, double[] ch, int[] ifac) {
      int i,k1,l1,l2;
      int na,kh,nf;
      int ip,iw,ido,idl1,ix2,ix3;
      DoubleBuffer wa_iw_1;
      DoubleBuffer wa_ix2_1;
      DoubleBuffer wa_ix3_1;
 
      nf = ifac[1];
      na = 1;
      l2 = n;
      iw = n;
      for (k1=0; k1<nf; k1++) {
        kh = nf-k1;
        ip = ifac[kh+1];
        l1 = l2/ip;
        ido = n/l2;
        idl1 = ido*l1;
        iw -= (ip-1)*ido;
        na = 1-na;
        if (ip == 4) {
          ix2 = iw+ido;
          ix3 = ix2+ido;
          wa_iw_1 = DoubleBuffer.wrap(ch, n + iw - 1, ch.length - (n + iw - 1)).slice();
          wa_ix2_1 = DoubleBuffer.wrap(ch, n + ix2 - 1, ch.length - (n + ix2 - 1)).slice();
          wa_ix3_1 = DoubleBuffer.wrap(ch, n + ix3 - 1, ch.length - (n + ix3 - 1)).slice();
          if (na != 0) {
            this.dradf4(ido, l1, ch, c, wa_iw_1, wa_ix2_1, wa_ix3_1);
          }
          else {
            this.dradf4(ido, l1, c, ch, wa_iw_1, wa_ix2_1, wa_ix3_1);
          }
        }
        else {
          if (ip == 2) {
            wa_iw_1 = DoubleBuffer.wrap(ch, n + iw - 1, ch.length - (n + iw - 1)).slice();
            if (na == 0) {
              this.dradf2(ido, l1, c, ch, wa_iw_1);
            }
            else {
              this.dradf2(ido, l1, ch, c, wa_iw_1);
            }
          }
          else {
            if (ido == 1)
              na = 1-na;
            wa_iw_1 = DoubleBuffer.wrap(ch, n + iw - 1, ch.length - (n + iw - 1)).slice();
            if (na == 0) {
              this.dradfg(ido, ip, l1, idl1, c, c, c, ch, ch, wa_iw_1);
              na = 1;
            }
            else {
              this.dradfg(ido, ip, l1, idl1, ch, ch, ch, c, c, wa_iw_1);
              na = 0;
            }
          }
        }
        l2 = l1;
      }
      if (na != 1) {
        for (i=0; i<n; i++)
          c[i] = ch[i];
      }
    }
 
  /**
   * Constructor, default 
   */ 
    public DiscreteFourierTransformTest() {
 
      super();
 
        this.percentile = 0.0;
        this.observedPeaks = 0.0;
        this.expectedPeaks = 0.0;
        this.normalizedDifference = 0.0;
    }
 
  /**
   * Constructor with a MathematicalFunctions object instantiated 
   * 
   * @param     mathFuncObj   mathematicalFunctions object that will be used by this object
   */ 
    public DiscreteFourierTransformTest(MathematicalFunctions mathFuncObj) {
            
        super(mathFuncObj);
 
        this.percentile = 0.0;
        this.observedPeaks = 0.0;
        this.expectedPeaks = 0.0;
        this.normalizedDifference = 0.0;
    }
 
  /**
   * Destructor, zeroes all data
   * 
   */ 
    public void finalize() {
            
        this.percentile = 0.0;
        this.observedPeaks = 0.0;
        this.expectedPeaks = 0.0;
        this.normalizedDifference = 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) {
      double    upperBound;
      double[]  m;
      double[]  X;
      int       i, count;
      double[]  wsave;
      int[]     ifac;
  
      if (bitStream.GetBitLength() < this.GetMinimumLength()) {
        this.error = RandomTestErrors.InsufficientNumberOfBits;
        this.random = false;
        return this.random;
      }
      this.error = RandomTestErrors.NoError;
      X = new double[bitStream.GetBitLength() + 1];
      wsave = new double[2 * bitStream.GetBitLength()];
      ifac = new int[15];
      m = new double[(bitStream.GetBitLength()/2)+1];
 
      if ( (X == null) || (wsave == null) || (ifac == null) || (m == null) ) {
        this.error = RandomTestErrors.InsufficientMemory;
        if (X != null) 
          X = null;
        if (wsave != null) 
          wsave = null;
        if (ifac != null) 
          ifac = null;
        if (m != null) 
          m = null;
        this.random = false;
        return this.random;
      }
      else {
        for (i = 0; i < X.length; i++) {
          X[i] = 0.0;
        }
        for (i = 0; i < wsave.length; i++) {
          wsave[i] = 0.0;
        }
        for (i = 0; i < ifac.length; i++) {
          ifac[i] = 0;
        }
        for (i = 0; i < m.length; i++) {
          m[i] = 0.0;
        }
        for (i = 0; i < (int)bitStream.GetBitLength(); i++)
          X[i] = 2*(int)bitStream.GetBitPosition(i) - 1;
        this.drfti1(bitStream.GetBitLength(), wsave, ifac);
        this.drftf1(bitStream.GetBitLength(), X, wsave, ifac);
        m[0] = Math.sqrt(X[0] * X[0]);      
        for (i = 0; i < (int)(bitStream.GetBitLength()/2); i++) {
          m[i+1] = Math.sqrt(Math.pow(X[(2*i)+1], 2) + Math.pow(X[(2*i)+2], 2)); 
        }
        count = 0;               
        upperBound = Math.sqrt(2.995732274 * bitStream.GetBitLength());
        for (i = 0; i < (int)bitStream.GetBitLength()/2; i++) {
          if (m[i] < upperBound) {
            count++;
          }
        }
        this.percentile = (double)count/(bitStream.GetBitLength()/2) * 100;
        this.observedPeaks = count;       
        this.expectedPeaks = 0.95 * bitStream.GetBitLength()/2.0;
        this.normalizedDifference = (this.observedPeaks - this.expectedPeaks)/Math.sqrt(bitStream.GetBitLength()/4.0 * 0.95 * 0.05);
        this.pValue = this.mathFuncs.ErFc(Math.abs(this.normalizedDifference)/Math.sqrt(2.0));
        if (this.pValue < this.alpha) {            
          this.random = false;
        }
        else {
          this.random = true;
        }
      }
      if (X != null) 
        X = null;
      if (wsave != null) 
        wsave = null;
      if (ifac != null) 
        ifac = null;
      if (m != null) 
        m = null;
      return this.random;
    }
 
  /**
   * Initializes the object
   * 
   */ 
    public void Initialize() {
            
        super.Initialize();
        this.percentile = 0.0;
        this.observedPeaks = 0.0;
        this.expectedPeaks = 0.0;
        this.normalizedDifference = 0.0;
    }
 
  /**
   * Gets the type of the object
   * 
   * @return    RandomTests     the concrete type class of the random number test, DiscreteFourierTransform test for this class
   */ 
    public RandomTests GetType() {
            
        return DiscreteFourierTransformTest.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 DiscreteFourierTransformTest.MINIMUMLENGTH;
    }
 
  /**
   * Gets the "percentile" result
   * 
   * @return    double:   "percentile" result of last computed CryptoRandomStream
   */ 
    public double GetPercentile() {
            
      return this.percentile;
    }
 
  /**
   * Gets the "observedPeaks" result
   * 
   * @return    double:   "observedPeaks" result of last computed CryptoRandomStream
   */ 
    public double GetObservedPeaks() {
            
        return this.observedPeaks;
    }
 
  /**
   * Gets the "expectedPeaks" result
   * 
   * @return    double:   "expectedPeaks" result of last computed CryptoRandomStream
   */ 
    public double GetExpectedPeaks() {
            
        return this.expectedPeaks;
    }
 
  /**
   * Gets the "normalizedDifference" result
   * 
   * @return    double:   "normalizedDifference" result of last computed CryptoRandomStream
   */ 
    public double GetNormalizedDifference() {
            
        return this.normalizedDifference;
    }
 
}