kerneltest/e32utils/nistsecurerng/src/overlappingtemplatematchings.cpp
branchRCL_3
changeset 294 039a3e647356
parent 268 345b1ca54e88
child 295 5460f47b94ad
equal deleted inserted replaced
268:345b1ca54e88 294:039a3e647356
     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                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 double	Pr(int u, double eta);
       
    36 
       
    37 void
       
    38 OverlappingTemplateMatchings(int m, int n)
       
    39 {
       
    40 	int				i, k, match;
       
    41 	double			W_obs, eta, sum, chi2, p_value, lambda;
       
    42 	int				M, N, j, K = 5;
       
    43 	unsigned int	nu[6] = { 0, 0, 0, 0, 0, 0 };
       
    44 	double			pi[6] = { 0.143783, 0.139430, 0.137319, 0.124314, 0.106209, 0.348945 };
       
    45 	BitSequence		*sequence;
       
    46 
       
    47 	M = 1032;
       
    48 	N = n/M;
       
    49 	
       
    50 	if ( (sequence = (BitSequence *) calloc(m, sizeof(BitSequence))) == NULL ) {
       
    51 		fprintf(stats[TEST_OVERLAPPING], "\t\t    OVERLAPPING TEMPLATE OF ALL ONES TEST\n");
       
    52 		fprintf(stats[TEST_OVERLAPPING], "\t\t---------------------------------------------\n");
       
    53 		fprintf(stats[TEST_OVERLAPPING], "\t\tTEMPLATE DEFINITION:  Insufficient memory, Overlapping Template Matchings test aborted!\n");
       
    54 	}
       
    55 	else
       
    56 		for ( i=0; i<m; i++ )
       
    57 			sequence[i] = 1;
       
    58 	
       
    59 	lambda = (double)(M-m+1)/pow(2,m);
       
    60 	eta = lambda/2.0;
       
    61 	sum = 0.0;
       
    62 	for ( i=0; i<K; i++ ) {			/* Compute Probabilities */
       
    63 		pi[i] = Pr(i, eta);
       
    64 		sum += pi[i];
       
    65 	}
       
    66 	pi[K] = 1 - sum;
       
    67 
       
    68 	for ( i=0; i<N; i++ ) {
       
    69 		W_obs = 0;
       
    70 		for ( j=0; j<M-m+1; j++ ) {
       
    71 			match = 1;
       
    72 			for ( k=0; k<m; k++ ) {
       
    73 				if ( sequence[k] != epsilon[i*M+j+k] )
       
    74 					match = 0;
       
    75 			}
       
    76 			if ( match == 1 )
       
    77 				W_obs++;
       
    78 		}
       
    79 		if ( W_obs <= 4 )
       
    80 			nu[(int)W_obs]++;
       
    81 		else
       
    82 			nu[K]++;
       
    83 	}
       
    84 	sum = 0;
       
    85 	chi2 = 0.0;                                   /* Compute Chi Square */
       
    86 	for ( i=0; i<K+1; i++ ) {
       
    87 		chi2 += pow((double)nu[i] - (double)N*pi[i], 2)/((double)N*pi[i]);
       
    88 		sum += nu[i];
       
    89 	}
       
    90 	p_value = cephes_igamc(K/2.0, chi2/2.0);
       
    91 
       
    92 	fprintf(stats[TEST_OVERLAPPING], "\t\t    OVERLAPPING TEMPLATE OF ALL ONES TEST\n");
       
    93 	fprintf(stats[TEST_OVERLAPPING], "\t\t-----------------------------------------------\n");
       
    94 	fprintf(stats[TEST_OVERLAPPING], "\t\tCOMPUTATIONAL INFORMATION:\n");
       
    95 	fprintf(stats[TEST_OVERLAPPING], "\t\t-----------------------------------------------\n");
       
    96 	fprintf(stats[TEST_OVERLAPPING], "\t\t(a) n (sequence_length)      = %d\n", n);
       
    97 	fprintf(stats[TEST_OVERLAPPING], "\t\t(b) m (block length of 1s)   = %d\n", m);
       
    98 	fprintf(stats[TEST_OVERLAPPING], "\t\t(c) M (length of substring)  = %d\n", M);
       
    99 	fprintf(stats[TEST_OVERLAPPING], "\t\t(d) N (number of substrings) = %d\n", N);
       
   100 	fprintf(stats[TEST_OVERLAPPING], "\t\t(e) lambda [(M-m+1)/2^m]     = %f\n", lambda);
       
   101 	fprintf(stats[TEST_OVERLAPPING], "\t\t(f) eta                      = %f\n", eta);
       
   102 	fprintf(stats[TEST_OVERLAPPING], "\t\t-----------------------------------------------\n");
       
   103 	fprintf(stats[TEST_OVERLAPPING], "\t\t   F R E Q U E N C Y\n");
       
   104 	fprintf(stats[TEST_OVERLAPPING], "\t\t  0   1   2   3   4 >=5   Chi^2   P-value  Assignment\n");
       
   105 	fprintf(stats[TEST_OVERLAPPING], "\t\t-----------------------------------------------\n");
       
   106 	fprintf(stats[TEST_OVERLAPPING], "\t\t%3d %3d %3d %3d %3d %3d  %f ",
       
   107 		nu[0], nu[1], nu[2], nu[3], nu[4], nu[5], chi2);
       
   108 
       
   109 	if ( isNegative(p_value) || isGreaterThanOne(p_value) )
       
   110 		fprintf(stats[TEST_OVERLAPPING], "WARNING:  P_VALUE IS OUT OF RANGE.\n");
       
   111 
       
   112 	free(sequence);
       
   113 	fprintf(stats[TEST_OVERLAPPING], "%f %s\n\n", p_value, p_value < ALPHA ? "FAILURE" : "SUCCESS");
       
   114 	fprintf(results[TEST_OVERLAPPING], "%f\n", p_value);
       
   115 }
       
   116 
       
   117 double
       
   118 Pr(int u, double eta)
       
   119 {
       
   120 	int		l;
       
   121 	double	sum, p;
       
   122 	
       
   123 	if ( u == 0 )
       
   124 		p = exp(-eta);
       
   125 	else {
       
   126 		sum = 0.0;
       
   127 		for ( l=1; l<=u; l++ )
       
   128 			sum += exp(-eta-u*log(2)+l*log(eta)-cephes_lgam(l+1)+cephes_lgam(u)-cephes_lgam(l)-cephes_lgam(u-l+1));
       
   129 		p = sum;
       
   130 	}
       
   131 	return p;
       
   132 }