kerneltest/e32utils/nistsecurerng/src/serial.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/cephes.h"  
       
    30 
       
    31 double psi2(int m, int n);
       
    32 
       
    33 void
       
    34 Serial(int m, int n)
       
    35 {
       
    36 	double	p_value1, p_value2, psim0, psim1, psim2, del1, del2;
       
    37 	
       
    38 	psim0 = psi2(m, n);
       
    39 	psim1 = psi2(m-1, n);
       
    40 	psim2 = psi2(m-2, n);
       
    41 	del1 = psim0 - psim1;
       
    42 	del2 = psim0 - 2.0*psim1 + psim2;
       
    43 	p_value1 = cephes_igamc(pow(2, m-1)/2, del1/2.0);
       
    44 	p_value2 = cephes_igamc(pow(2, m-2)/2, del2/2.0);
       
    45 	
       
    46 	fprintf(stats[TEST_SERIAL], "\t\t\t       SERIAL TEST\n");
       
    47 	fprintf(stats[TEST_SERIAL], "\t\t---------------------------------------------\n");
       
    48 	fprintf(stats[TEST_SERIAL], "\t\t COMPUTATIONAL INFORMATION:		  \n");
       
    49 	fprintf(stats[TEST_SERIAL], "\t\t---------------------------------------------\n");
       
    50 	fprintf(stats[TEST_SERIAL], "\t\t(a) Block length    (m) = %d\n", m);
       
    51 	fprintf(stats[TEST_SERIAL], "\t\t(b) Sequence length (n) = %d\n", n);
       
    52 	fprintf(stats[TEST_SERIAL], "\t\t(c) Psi_m               = %f\n", psim0);
       
    53 	fprintf(stats[TEST_SERIAL], "\t\t(d) Psi_m-1             = %f\n", psim1);
       
    54 	fprintf(stats[TEST_SERIAL], "\t\t(e) Psi_m-2             = %f\n", psim2);
       
    55 	fprintf(stats[TEST_SERIAL], "\t\t(f) Del_1               = %f\n", del1);
       
    56 	fprintf(stats[TEST_SERIAL], "\t\t(g) Del_2               = %f\n", del2);
       
    57 	fprintf(stats[TEST_SERIAL], "\t\t---------------------------------------------\n");
       
    58 
       
    59 	fprintf(stats[TEST_SERIAL], "%s\t\tp_value1 = %f\n", p_value1 < ALPHA ? "FAILURE" : "SUCCESS", p_value1);
       
    60 	fprintf(results[TEST_SERIAL], "%f\n", p_value1);
       
    61 
       
    62 	fprintf(stats[TEST_SERIAL], "%s\t\tp_value2 = %f\n\n", p_value2 < ALPHA ? "FAILURE" : "SUCCESS", p_value2);
       
    63 	fprintf(results[TEST_SERIAL], "%f\n", p_value2);
       
    64 }
       
    65 
       
    66 double
       
    67 psi2(int m, int n)
       
    68 {
       
    69 	int				i, j, k, powLen;
       
    70 	double			sum, numOfBlocks;
       
    71 	unsigned int	*P;
       
    72 	
       
    73 	if ( (m == 0) || (m == -1) )
       
    74 		return 0.0;
       
    75 	numOfBlocks = n;
       
    76 	powLen = (int)pow(2, m+1)-1;
       
    77 	if ( (P = (unsigned int*)calloc(powLen,sizeof(unsigned int)))== NULL ) {
       
    78 		fprintf(stats[TEST_SERIAL], "Serial Test:  Insufficient memory available.\n");
       
    79 		fflush(stats[TEST_SERIAL]);
       
    80 		return 0.0;
       
    81 	}
       
    82 	for ( i=1; i<powLen-1; i++ )
       
    83 		P[i] = 0;	  /* INITIALIZE NODES */
       
    84 	for ( i=0; i<numOfBlocks; i++ ) {		 /* COMPUTE FREQUENCY */
       
    85 		k = 1;
       
    86 		for ( j=0; j<m; j++ ) {
       
    87 			if ( epsilon[(i+j)%n] == 0 )
       
    88 				k *= 2;
       
    89 			else if ( epsilon[(i+j)%n] == 1 )
       
    90 				k = 2*k+1;
       
    91 		}
       
    92 		P[k-1]++;
       
    93 	}
       
    94 	sum = 0.0;
       
    95 	for ( i=(int)pow(2, m)-1; i<(int)pow(2, m+1)-1; i++ )
       
    96 		sum += pow(P[i], 2);
       
    97 	sum = (sum * pow(2, m)/(double)n) - (double)n;
       
    98 	free(P);
       
    99 	
       
   100 	return sum;
       
   101 }