kerneltest/e32utils/nistsecurerng/src/nonOverlappingTemplateMatchings.cpp
changeset 152 657f875b013e
equal deleted inserted replaced
139:95f71bcdcdb7 152:657f875b013e
       
     1 /*
       
     2 * Portions Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * The original NIST Statistical Test Suite code is placed in public domain.
       
    16 * (http://csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.html) 
       
    17 * 
       
    18 * This software was developed at the National Institute of Standards and Technology by 
       
    19 * employees of the Federal Government in the course of their official duties. Pursuant
       
    20 * to title 17 Section 105 of the United States Code this software is not subject to 
       
    21 * copyright protection and is in the public domain. The NIST Statistical Test Suite is
       
    22 * an experimental system. NIST assumes no responsibility whatsoever for its use by other 
       
    23 * parties, and makes no guarantees, expressed or implied, about its quality, reliability, 
       
    24 * or any other characteristic. We would appreciate acknowledgment if the software is used.
       
    25 */
       
    26 
       
    27 #include "openc.h"
       
    28 #include "../include/externs.h"
       
    29 #include "../include/utilities.h"
       
    30 #include "../include/cephes.h"  
       
    31 
       
    32 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
    33           N O N O V E R L A P P I N G  T E M P L A T E  T E S T
       
    34  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    35 
       
    36 void
       
    37 NonOverlappingTemplateMatchings(int m, int n)
       
    38 {
       
    39 	int		numOfTemplates[100] = {0, 0, 2, 4, 6, 12, 20, 40, 74, 148, 284, 568, 1116,
       
    40 						2232, 4424, 8848, 17622, 35244, 70340, 140680, 281076, 562152};
       
    41 	/*----------------------------------------------------------------------------
       
    42 	NOTE:  Should additional templates lengths beyond 21 be desired, they must 
       
    43 	first be constructed, saved into files and then the corresponding 
       
    44 	number of nonperiodic templates for that file be stored in the m-th 
       
    45 	position in the numOfTemplates variable.
       
    46 	----------------------------------------------------------------------------*/
       
    47 	unsigned int	W_obs, *Wj = NULL; 
       
    48 	double			sum, chi2, p_value, lambda, pi[6], varWj;
       
    49 	int				i, j, jj, k, match, SKIP, M, N, K = 5;
       
    50 	
       
    51 	ResetTemplateIndex();
       
    52 
       
    53 	N = 8;
       
    54 	M = n/N;
       
    55 
       
    56 	if ( (Wj = (unsigned int*)calloc(N, sizeof(unsigned int))) == NULL ) {
       
    57 		fprintf(stats[TEST_NONPERIODIC], "\tNONOVERLAPPING TEMPLATES TESTS ABORTED DUE TO ONE OF THE FOLLOWING : \n");
       
    58 		fprintf(stats[TEST_NONPERIODIC], "\tInsufficient memory for required work space.\n");
       
    59 		return;
       
    60 	}
       
    61 	lambda = (M-m+1)/pow(2, m);
       
    62 	varWj = M*(1.0/pow(2.0, m) - (2.0*m-1.0)/pow(2.0, 2.0*m));
       
    63 
       
    64 	if ( isNegative(lambda) || isZero(lambda)) 
       
    65 	    {
       
    66 		fprintf(stats[TEST_NONPERIODIC], "\tNONOVERLAPPING TEMPLATES TESTS ABORTED DUE TO ONE OF THE FOLLOWING : \n");
       
    67 		fprintf(stats[TEST_NONPERIODIC], "\tLambda (%f) not being positive!\n", lambda);
       
    68 		}
       
    69 	else {
       
    70 		fprintf(stats[TEST_NONPERIODIC], "\t\t  NONPERIODIC TEMPLATES TEST\n");
       
    71 		fprintf(stats[TEST_NONPERIODIC], "-------------------------------------------------------------------------------------\n");
       
    72 		fprintf(stats[TEST_NONPERIODIC], "\t\t  COMPUTATIONAL INFORMATION\n");
       
    73 		fprintf(stats[TEST_NONPERIODIC], "-------------------------------------------------------------------------------------\n");
       
    74 		fprintf(stats[TEST_NONPERIODIC], "\tLAMBDA = %f\tM = %d\tN = %d\tm = %d\tn = %d\n", lambda, M, N, m, n);
       
    75 		fprintf(stats[TEST_NONPERIODIC], "-------------------------------------------------------------------------------------\n");
       
    76 		fprintf(stats[TEST_NONPERIODIC], "\t\tF R E Q U E N C Y\n");
       
    77 		fprintf(stats[TEST_NONPERIODIC], "Template   W_1  W_2  W_3  W_4  W_5  W_6  W_7  W_8    Chi^2   P_value Assignment Index\n");
       
    78 		fprintf(stats[TEST_NONPERIODIC], "-------------------------------------------------------------------------------------\n");
       
    79 
       
    80 		if ( numOfTemplates[m] < MAXNUMOFTEMPLATES )
       
    81 			SKIP = 1;
       
    82 		else
       
    83 			SKIP = (int)(numOfTemplates[m]/MAXNUMOFTEMPLATES);
       
    84 		numOfTemplates[m] = (int)numOfTemplates[m]/SKIP;
       
    85 		
       
    86 		sum = 0.0;
       
    87 		for ( i=0; i<2; i++ ) {                      /* Compute Probabilities */
       
    88 			pi[i] = exp(-lambda+i*log(lambda)-cephes_lgam(i+1));
       
    89 			sum += pi[i];
       
    90 		}
       
    91 		pi[0] = sum;
       
    92 		for ( i=2; i<=K; i++ ) {                      /* Compute Probabilities */
       
    93 			pi[i-1] = exp(-lambda+i*log(lambda)-cephes_lgam(i+1));
       
    94 			sum += pi[i-1];
       
    95 		}
       
    96 		pi[K] = 1 - sum;
       
    97 
       
    98         BitSequence templateSequence[KMaxBit];
       
    99 
       
   100         for( jj=0; jj<MIN(MAXNUMOFTEMPLATES, numOfTemplates[m]); jj++ ) {
       
   101 			sum = 0;
       
   102             GetNextTemplateItem(templateSequence);
       
   103 
       
   104 			fprintf(stats[TEST_NONPERIODIC], " ");
       
   105 			for ( i=0; i<N; i++ ) {
       
   106 				W_obs = 0;
       
   107 				for ( j=0; j<M-m+1; j++ ) {
       
   108 					match = 1;
       
   109 					for ( k=0; k<m; k++ ) {
       
   110 					
       
   111                     if ( (int)templateSequence[KMaxBit - k - 1] != (int)epsilon[i*M+j+k] ) {
       
   112 							match = 0;
       
   113 							break;
       
   114 						}
       
   115 					}
       
   116 					if ( match == 1 )
       
   117 						W_obs++;
       
   118 				}
       
   119 				Wj[i] = W_obs;
       
   120 			}
       
   121 			sum = 0;
       
   122 			chi2 = 0.0;                                   /* Compute Chi Square */
       
   123 			for ( i=0; i<N; i++ ) {
       
   124 				if ( m == 10 )
       
   125 					fprintf(stats[TEST_NONPERIODIC], "%3d  ", Wj[i]);
       
   126 				else
       
   127 					fprintf(stats[TEST_NONPERIODIC], "%4d ", Wj[i]);
       
   128 				chi2 += pow(((double)Wj[i] - lambda)/pow(varWj, 0.5), 2);
       
   129 			}
       
   130 			p_value = cephes_igamc(N/2.0, chi2/2.0);
       
   131 		
       
   132 			if ( isNegative(p_value) || isGreaterThanOne(p_value) )
       
   133 				fprintf(stats[TEST_NONPERIODIC], "\t\tWARNING:  P_VALUE IS OUT OF RANGE.\n");
       
   134 
       
   135 			fprintf(stats[TEST_NONPERIODIC], "%9.6f %f %s %3d\n", chi2, p_value, p_value < ALPHA ? "FAILURE" : "SUCCESS", jj);
       
   136 			if ( SKIP > 1 )
       
   137 			    {
       
   138 			    for(int index = 0; index < (SKIP-1)*2*m; ++index)
       
   139 			        {
       
   140 			        GetNextTemplateItem(templateSequence);
       
   141 			        }
       
   142 			    }
       
   143 			fprintf(results[TEST_NONPERIODIC], "%f\n", p_value);
       
   144 		}
       
   145 	}
       
   146 	
       
   147 	fprintf(stats[TEST_NONPERIODIC], "\n");
       
   148 	free(Wj);
       
   149 }