kerneltest/e32utils/nistsecurerng/src/blockFrequency.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 
       
    28 #include "openc.h"
       
    29 #include "../include/externs.h"
       
    30 #include "../include/cephes.h"
       
    31 
       
    32 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
    33                     B L O C K  F R E Q U E N C Y  T E S T
       
    34  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    35 
       
    36 void
       
    37 BlockFrequency(int M, int n)
       
    38 {
       
    39 	int		i, j, N, blockSum;
       
    40 	double	p_value, sum, pi, v, chi_squared;
       
    41 	
       
    42 	N = n/M; 		/* # OF SUBSTRING BLOCKS      */
       
    43 	sum = 0.0;
       
    44 	
       
    45 	for ( i=0; i<N; i++ ) {
       
    46 		blockSum = 0;
       
    47 		for ( j=0; j<M; j++ )
       
    48 			blockSum += epsilon[j+i*M];
       
    49 		pi = (double)blockSum/(double)M;
       
    50 		v = pi - 0.5;
       
    51 		sum += v*v;
       
    52 	}
       
    53 	chi_squared = 4.0 * M * sum;
       
    54 	p_value = cephes_igamc(N/2.0, chi_squared/2.0);
       
    55 
       
    56 	fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t\tBLOCK FREQUENCY TEST\n");
       
    57 	fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t---------------------------------------------\n");
       
    58 	fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\tCOMPUTATIONAL INFORMATION:\n");
       
    59 	fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t---------------------------------------------\n");
       
    60 	fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t(a) Chi^2           = %f\n", chi_squared);
       
    61 	fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t(b) # of substrings = %d\n", N);
       
    62 	fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t(c) block length    = %d\n", M);
       
    63 	fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t(d) Note: %d bits were discarded.\n", n % M);
       
    64 	fprintf(stats[TEST_BLOCK_FREQUENCY], "\t\t---------------------------------------------\n");
       
    65 
       
    66 	fprintf(stats[TEST_BLOCK_FREQUENCY], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value);
       
    67 	fprintf(results[TEST_BLOCK_FREQUENCY], "%f\n", p_value);
       
    68 }