kerneltest/e32utils/nistsecurerng/src/rank.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/cephes.h"
       
    30 #include "../include/matrix.h"
       
    31 
       
    32 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
    33                               R A N K  T E S T
       
    34  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    35 
       
    36 void
       
    37 Rank(int n)
       
    38 {
       
    39 	int			N, i, k, r;
       
    40 	double		p_value, product, chi_squared, arg1, p_32, p_31, p_30, R, F_32, F_31, F_30;
       
    41 	
       
    42 	N = n/(32*32);
       
    43 	if ( isZero(N) ) {
       
    44 		fprintf(stats[TEST_RANK], "\t\t\t\tRANK TEST\n");
       
    45 		fprintf(stats[TEST_RANK], "\t\tError: Insuffucient # Of Bits To Define An 32x32 (%dx%d) Matrix\n", 32, 32);
       
    46 		p_value = 0.00;
       
    47 	}
       
    48 	else {
       
    49         BitSequence **matrix = create_matrix(32, 32);
       
    50 		
       
    51         r = 32;					/* COMPUTE PROBABILITIES */
       
    52 		product = 1;
       
    53 		for ( i=0; i<=r-1; i++ )
       
    54 			product *= ((1.e0-pow(2, i-32))*(1.e0-pow(2, i-32)))/(1.e0-pow(2, i-r));
       
    55 		p_32 = pow(2, r*(32+32-r)-32*32) * product;
       
    56 		
       
    57 		r = 31;
       
    58 		product = 1;
       
    59 		for ( i=0; i<=r-1; i++ )
       
    60 			product *= ((1.e0-pow(2, i-32))*(1.e0-pow(2, i-32)))/(1.e0-pow(2, i-r));
       
    61 		p_31 = pow(2, r*(32+32-r)-32*32) * product;
       
    62 		
       
    63 		p_30 = 1 - (p_32+p_31);
       
    64 		
       
    65 		F_32 = 0;
       
    66 		F_31 = 0;
       
    67 		for ( k=0; k<N; k++ ) {			/* FOR EACH 32x32 MATRIX   */
       
    68 			def_matrix(32, 32, matrix, k);
       
    69 #if (DISPLAY_MATRICES == 1)
       
    70 			display_matrix(32, 32, matrix);
       
    71 #endif
       
    72 			R = computeRank(32, 32, matrix);
       
    73 			if ( R == 32 )
       
    74 				F_32++;			/* DETERMINE FREQUENCIES */
       
    75 			if ( R == 31 )
       
    76 				F_31++;
       
    77 		}
       
    78 		F_30 = (double)N - (F_32+F_31);
       
    79 		
       
    80 		chi_squared =(pow(F_32 - N*p_32, 2)/(double)(N*p_32) +
       
    81 					  pow(F_31 - N*p_31, 2)/(double)(N*p_31) +
       
    82 					  pow(F_30 - N*p_30, 2)/(double)(N*p_30));
       
    83 		
       
    84 		arg1 = -chi_squared/2.e0;
       
    85 
       
    86 		fprintf(stats[TEST_RANK], "\t\t\t\tRANK TEST\n");
       
    87 		fprintf(stats[TEST_RANK], "\t\t---------------------------------------------\n");
       
    88 		fprintf(stats[TEST_RANK], "\t\tCOMPUTATIONAL INFORMATION:\n");
       
    89 		fprintf(stats[TEST_RANK], "\t\t---------------------------------------------\n");
       
    90 		fprintf(stats[TEST_RANK], "\t\t(a) Probability P_%d = %f\n", 32,p_32);
       
    91 		fprintf(stats[TEST_RANK], "\t\t(b)             P_%d = %f\n", 31,p_31);
       
    92 		fprintf(stats[TEST_RANK], "\t\t(c)             P_%d = %f\n", 30,p_30);
       
    93 		fprintf(stats[TEST_RANK], "\t\t(d) Frequency   F_%d = %d\n", 32,(int)F_32);
       
    94 		fprintf(stats[TEST_RANK], "\t\t(e)             F_%d = %d\n", 31,(int)F_31);
       
    95 		fprintf(stats[TEST_RANK], "\t\t(f)             F_%d = %d\n", 30,(int)F_30);
       
    96 		fprintf(stats[TEST_RANK], "\t\t(g) # of matrices    = %d\n", N);
       
    97 		fprintf(stats[TEST_RANK], "\t\t(h) Chi^2            = %f\n", chi_squared);
       
    98 		fprintf(stats[TEST_RANK], "\t\t(i) NOTE: %d BITS WERE DISCARDED.\n", n%(32*32));
       
    99 		fprintf(stats[TEST_RANK], "\t\t---------------------------------------------\n");
       
   100 
       
   101 		p_value = exp(arg1);
       
   102 		if ( isNegative(p_value) || isGreaterThanOne(p_value) )
       
   103 			fprintf(stats[TEST_RANK], "WARNING:  P_VALUE IS OUT OF RANGE.\n");
       
   104 
       
   105 		for ( i=0; i<32; i++ )				/* DEALLOCATE MATRIX  */
       
   106 			free(matrix[i]);
       
   107 		free(matrix);
       
   108 	}
       
   109 	fprintf(stats[TEST_RANK], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value);
       
   110 	fprintf(results[TEST_RANK], "%f\n", p_value);
       
   111 }