|
randomTestSuite.cpp (2) (Random number test suite source code second part file) |
|
|
|
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
|
|
|
FREE C++ Source Code
|
|
| Eclipse project and source files !!! |
|
|
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
413
414
415
416
417
418
419
420
421
422
423
424
|
// GETTING RANDOM TESTS
// Gets a random test to the suite based in the enumerated random tests
BaseRandomTest* RandomTestSuite::GetRandomTest(RandomTests test) {
return this->suite[test];
}
// Gets the defined random test to the suite
FrequencyTest* RandomTestSuite::GetFrequencyTest(void) {
return (FrequencyTest *)this->suite[Frequency];
}
// Gets the defined random test to the suite
BlockFrequencyTest* RandomTestSuite::GetBlockFrequencyTest(void) {
return (BlockFrequencyTest *)this->suite[BlockFrequency];
}
// Gets the defined random test to the suite
RunsTest* RandomTestSuite::GetRunsTest(void) {
return (RunsTest *)this->suite[Runs];
}
// Gets the defined random test to the suite
LongestRunOfOnesTest* RandomTestSuite::GetLongestRunOfOnesTest(void) {
return (LongestRunOfOnesTest *)this->suite[LongestRunOfOnes];
}
// Gets the defined random test to the suite
CumulativeSumForwardTest* RandomTestSuite::GetCumulativeSumForwardTest(void) {
return (CumulativeSumForwardTest *)this->suite[CumulativeSumForward];
}
// Gets the defined random test to the suite
CumulativeSumReverseTest* RandomTestSuite::GetCumulativeSumReverseTest(void) {
return (CumulativeSumReverseTest *)this->suite[CumulativeSumReverse];
}
// Gets the defined random test to the suite
RankTest* RandomTestSuite::GetRankTest(void) {
return (RankTest *)this->suite[Rank];
}
// Gets the defined random test to the suite
UniversalTest* RandomTestSuite::GetUniversalTest(void) {
return (UniversalTest *)this->suite[Universal];
}
// Gets the defined random test to the suite
ApproximateEntropyTest* RandomTestSuite::GetApproximateEntropyTest(void) {
return (ApproximateEntropyTest *)this->suite[ApproximateEntropy];
}
// Gets the defined random test to the suite
SerialTest* RandomTestSuite::GetSerialTest(void) {
return (SerialTest *)this->suite[Serial];
}
// Gets the defined random test to the suite
DiscreteFourierTransformTest* RandomTestSuite::GetDiscreteFourierTransformTest(void) {
return (DiscreteFourierTransformTest *)this->suite[DiscreteFourierTransform];
}
// REMOVING RANDOM TESTS
// Removes a pointed random test from the suite
void RandomTestSuite::Remove(BaseRandomTest* test) {
RandomTests randomTest;
randomTest = test->GetType();
if ((this->suite[randomTest] != NULL) && (this->suite[randomTest] == test)) {
if (this->selfCreatedTest[randomTest]) {
delete this->suite[randomTest];
}
this->suite[randomTest] = NULL;
this->selfCreatedTest[randomTest] = false;
this->instantiatedTests--;
}
}
// Removes a random test to the suite based in the enumerated random tests
void RandomTestSuite::Remove(RandomTests test) {
if (this->suite[test] != NULL) {
if (this->selfCreatedTest[test]) {
delete this->suite[test];
}
this->suite[test] = NULL;
this->selfCreatedTest[test] = false;
this->instantiatedTests--;
}
}
// Removes all random test of the suite
void RandomTestSuite::RemoveAll(void) {
int i;
for (i=this->GetFirstTest(); i<this->GetMaximumNumberOfTests(); i++) {
if (this->suite[i] != NULL) {
if (this->selfCreatedTest[i]) {
delete this->suite[i];
}
this->suite[i] = NULL;
this->selfCreatedTest[i] = false;
}
}
this->instantiatedTests = 0;
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveFrequencyTest(void) {
if (this->suite[Frequency] != NULL) {
if (this->selfCreatedTest[Frequency]) {
delete this->suite[Frequency];
}
this->suite[Frequency] = NULL;
this->selfCreatedTest[Frequency] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveBlockFrequencyTest(void) {
if (this->suite[BlockFrequency] != NULL) {
if (this->selfCreatedTest[BlockFrequency]) {
delete this->suite[BlockFrequency];
}
this->suite[BlockFrequency] = NULL;
this->selfCreatedTest[BlockFrequency] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveRunsTest(void) {
if (this->suite[Runs] != NULL) {
if (this->selfCreatedTest[Runs]) {
delete this->suite[Runs];
}
this->suite[Runs] = NULL;
this->selfCreatedTest[Runs] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveLongestRunOfOnesTest(void) {
if (this->suite[LongestRunOfOnes] != NULL) {
if (this->selfCreatedTest[LongestRunOfOnes]) {
delete this->suite[LongestRunOfOnes];
}
this->suite[LongestRunOfOnes] = NULL;
this->selfCreatedTest[LongestRunOfOnes] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveCumulativeSumForwardTest(void) {
if (this->suite[CumulativeSumForward] != NULL) {
if (this->selfCreatedTest[CumulativeSumForward]) {
delete this->suite[CumulativeSumForward];
}
this->suite[CumulativeSumForward] = NULL;
this->selfCreatedTest[CumulativeSumForward] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveCumulativeSumReverseTest(void) {
if (this->suite[CumulativeSumReverse] != NULL) {
if (this->selfCreatedTest[CumulativeSumReverse]) {
delete this->suite[CumulativeSumReverse];
}
this->suite[CumulativeSumReverse] = NULL;
this->selfCreatedTest[CumulativeSumReverse] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveRankTest(void) {
if (this->suite[Rank] != NULL) {
if (this->selfCreatedTest[Rank]) {
delete this->suite[Rank];
}
this->suite[Rank] = NULL;
this->selfCreatedTest[Rank] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveUniversalTest(void) {
if (this->suite[Universal] != NULL) {
if (this->selfCreatedTest[Universal]) {
delete this->suite[Universal];
}
this->suite[Universal] = NULL;
this->selfCreatedTest[Universal] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveApproximateEntropyTest(void) {
if (this->suite[ApproximateEntropy] != NULL) {
if (this->selfCreatedTest[ApproximateEntropy]) {
delete this->suite[ApproximateEntropy];
}
this->suite[ApproximateEntropy] = NULL;
this->selfCreatedTest[ApproximateEntropy] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveSerialTest(void) {
if (this->suite[Serial] != NULL) {
if (this->selfCreatedTest[Serial]) {
delete this->suite[Serial];
}
this->suite[Serial] = NULL;
this->selfCreatedTest[Serial] = false;
this->instantiatedTests--;
}
}
// Removes the defined random test to the suite
void RandomTestSuite::RemoveDiscreteFourierTransformTest(void) {
if (this->suite[DiscreteFourierTransform] != NULL) {
if (this->selfCreatedTest[DiscreteFourierTransform]) {
delete this->suite[DiscreteFourierTransform];
}
this->suite[DiscreteFourierTransform] = NULL;
this->selfCreatedTest[DiscreteFourierTransform] = false;
this->instantiatedTests--;
}
}
// CHECKING RANDOMNESS
// Tests the BaseCryptoRandomStream untill an error is found with all instantiated random tests and returns the random value
bool RandomTestSuite::IsRandom(BaseCryptoRandomStream* stream) {
int i;
this->random = true;
i = this->GetFirstTest();
while ((i < this->GetMaximumNumberOfTests()) && (this->random)) {
if (this->suite[i] != NULL) {
this->random &= this->suite[i]->IsRandom(stream);
nonRandomTest = this->suite[i]->GetType();
if (this->suite[i]->GetError() != NoError) {
errorTest = this->suite[i]->GetType();
this->numberOfErrors = 1;
}
}
i++;
}
return this->random;
}
// Tests the BaseCryptoRandomStream with all instantiated random tests and returns the random value
bool RandomTestSuite::TestRandom(BaseCryptoRandomStream* stream) {
int i;
this->random = true;
for ( i = this->GetFirstTest(); i < this->GetMaximumNumberOfTests(); i++ ) {
if (this->suite[i] != NULL) {
this->random &= this->suite[i]->IsRandom(stream);
if (!(this->suite[i]->IsRandom())) {
if (this->suite[i]->GetError() != NoError) {
if (this->error == NoError)
this->error = this->suite[i]->GetError();
else
if (this->error != this->suite[i]->GetError())
this->error = MultipleErrors;
numberOfErrors++;
}
}
}
}
return this->random;
}
// INITIALIZE SUITE
// Initializes all random tests in the suite
void RandomTestSuite::Initialize(void) {
int i;
for ( i = this->GetFirstTest(); i < this->GetMaximumNumberOfTests(); i++ ) {
if (this->suite[i] != NULL) {
this->suite[i]->Initialize();
}
}
this->random = false;
this->error = NoError;
this->numberOfErrors = 0;
this->errorTest = NotDefined;
}
// Sets Alpha all random tests in the suite
void RandomTestSuite::SetAlpha(double alpha) {
int i;
for (i=0; i<NumberOfTests; i++)
if (this->suite[i] != NULL)
this->suite[i]->SetAlpha(alpha);
}
// GETTING SUITE RESULTS
// Gets the RandomTestSuite random state of the last executed BaseCryptoRandomStream
bool RandomTestSuite::IsRandom(void) {
return this->random;
}
// Gets the number of Random Tests that contains the suite
int RandomTestSuite::GetInstantiatedTests(void) {
return this->instantiatedTests;
}
// Gets the minimum random stream length in bits corresponding
// to random number test with higher random stream length
unsigned int RandomTestSuite::GetMinimumLength(void) {
unsigned int minimumMax;
int i;
minimumMax = 0;
for ( i = this->GetFirstTest(); i < this->GetMaximumNumberOfTests(); i++ ) {
if ( this->Exist((RandomTests)i) ) {
if ( this->GetRandomTest((RandomTests)i)->GetMinimumLength() > minimumMax ) {
minimumMax = this->GetRandomTest((RandomTests)i)->GetMinimumLength();
}
}
}
return minimumMax;
}
// Gets the corresponding random number test
// with higher minimum random stream length in bits
RandomTests RandomTestSuite::GetMinimumLengthRandomTest(void) {
unsigned int minimumMax;
int i;
RandomTests randomTest;
minimumMax = 0;
randomTest = NotDefined;
for ( i = this->GetFirstTest(); i < this->GetMaximumNumberOfTests(); i++ ) {
if ( this->Exist((RandomTests)i) ) {
if ( this->GetRandomTest((RandomTests)i)->GetMinimumLength() > minimumMax ) {
minimumMax = this->GetRandomTest((RandomTests)i)->GetMinimumLength();
randomTest = (RandomTests)i;
}
}
}
return randomTest;
}
// Gets the failed random test in the RandomTestSuite
RandomTests RandomTestSuite::GetNonRandomTest(void) {
return this->nonRandomTest;
}
// Gets the RandomTestError error of the last executed BaseCryptoRandomStream
RandomTestErrors RandomTestSuite::GetError(void) {
return this->error;
}
// Gets the RandomTest that produced the error of the last executed BaseCryptoRandomStream
RandomTests RandomTestSuite::GetErrorTest(void) {
return this->errorTest;
}
// Indicates if a random test object exists in the suite
bool RandomTestSuite::Exist(RandomTests test) {
return (this->suite[test] != NULL);
}
// Gets the first random test in the RandomTestSuite
RandomTests RandomTestSuite::GetFirstTest(void) {
return this->firstTest;
}
// Gets the number of random tests that can be used in the RandomTestSuite
RandomTests RandomTestSuite::GetMaximumNumberOfTests(void) {
return NumberOfTests;
}
}
}
|
|