kerneltest/e32utils/nistsecurerng/src/matrix.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/matrix.h"
       
    30 
       
    31 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
    32 R A N K  A L G O R I T H M  R O U T I N E S
       
    33 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    34 
       
    35 #define	MATRIX_FORWARD_ELIMINATION	0
       
    36 #define	MATRIX_BACKWARD_ELIMINATION	1
       
    37 
       
    38 int
       
    39 computeRank(int M, int Q, BitSequence **matrix)
       
    40 {
       
    41 	int		i, rank, m=MIN(M,Q);
       
    42 	
       
    43 	/* FORWARD APPLICATION OF ELEMENTARY ROW OPERATIONS */ 
       
    44 	for ( i=0; i<m-1; i++ ) {
       
    45 		if ( matrix[i][i] == 1 ) 
       
    46 			perform_elementary_row_operations(MATRIX_FORWARD_ELIMINATION, i, M, Q, matrix);
       
    47 		else { 	/* matrix[i][i] = 0 */
       
    48 			if ( find_unit_element_and_swap(MATRIX_FORWARD_ELIMINATION, i, M, Q, matrix) == 1 ) 
       
    49 				perform_elementary_row_operations(MATRIX_FORWARD_ELIMINATION, i, M, Q, matrix);
       
    50 		}
       
    51 	}
       
    52 
       
    53 	/* BACKWARD APPLICATION OF ELEMENTARY ROW OPERATIONS */ 
       
    54 	for ( i=m-1; i>0; i-- ) {
       
    55 		if ( matrix[i][i] == 1 )
       
    56 			perform_elementary_row_operations(MATRIX_BACKWARD_ELIMINATION, i, M, Q, matrix);
       
    57 		else { 	/* matrix[i][i] = 0 */
       
    58 			if ( find_unit_element_and_swap(MATRIX_BACKWARD_ELIMINATION, i, M, Q, matrix) == 1 )
       
    59 				perform_elementary_row_operations(MATRIX_BACKWARD_ELIMINATION, i, M, Q, matrix);
       
    60 		}
       
    61 	} 
       
    62 
       
    63 	rank = determine_rank(m, M, Q, matrix);
       
    64 
       
    65 	return rank;
       
    66 }
       
    67 
       
    68 void
       
    69 perform_elementary_row_operations(int flag, int i, int M, int Q, BitSequence **A)
       
    70 {
       
    71 	int		j, k;
       
    72 	
       
    73 	if ( flag == MATRIX_FORWARD_ELIMINATION ) {
       
    74 		for ( j=i+1; j<M;  j++ )
       
    75 			if ( A[j][i] == 1 ) 
       
    76 				for ( k=i; k<Q; k++ ) 
       
    77 					A[j][k] = (BitSequence)((A[j][k] + A[i][k]) % 2);
       
    78 	}
       
    79 	else {
       
    80 		for ( j=i-1; j>=0;  j-- )
       
    81 			if ( A[j][i] == 1 )
       
    82 				for ( k=0; k<Q; k++ )
       
    83 					A[j][k] = (BitSequence)((A[j][k] + A[i][k]) % 2);
       
    84 	}
       
    85 }
       
    86 
       
    87 int
       
    88 find_unit_element_and_swap(int flag, int i, int M, int Q, BitSequence **A)
       
    89 { 
       
    90 	int		index, row_op=0;
       
    91 	
       
    92 	if ( flag == MATRIX_FORWARD_ELIMINATION ) {
       
    93 		index = i+1;
       
    94 		while ( (index < M) && (A[index][i] == 0) ) 
       
    95 			index++;
       
    96 			if ( index < M )
       
    97 				row_op = swap_rows(i, index, Q, A);
       
    98 	}
       
    99 	else {
       
   100 		index = i-1;
       
   101 		while ( (index >= 0) && (A[index][i] == 0) ) 
       
   102 			index--;
       
   103 			if ( index >= 0 )
       
   104 				row_op = swap_rows(i, index, Q, A);
       
   105 	}
       
   106 	
       
   107 	return row_op;
       
   108 }
       
   109 
       
   110 int
       
   111 swap_rows(int i, int index, int Q, BitSequence **A)
       
   112 {
       
   113 	int			p;
       
   114 	BitSequence	temp;
       
   115 	
       
   116 	for ( p=0; p<Q; p++ ) {
       
   117 		temp = A[i][p];
       
   118 		A[i][p] = A[index][p];
       
   119 		A[index][p] = temp;
       
   120 	}
       
   121 	
       
   122 	return 1;
       
   123 }
       
   124 
       
   125 int
       
   126 determine_rank(int m, int M, int Q, BitSequence **A)
       
   127 {
       
   128 	int		i, j, rank, allZeroes;
       
   129 	
       
   130 	/* DETERMINE RANK, THAT IS, COUNT THE NUMBER OF NONZERO ROWS */
       
   131 	
       
   132 	rank = m;
       
   133 	for ( i=0; i<M; i++ ) {
       
   134 		allZeroes = 1; 
       
   135 		for ( j=0; j<Q; j++)  {
       
   136 			if ( A[i][j] == 1 ) {
       
   137 				allZeroes = 0;
       
   138 				break;
       
   139 			}
       
   140 		}
       
   141 		if ( allZeroes == 1 )
       
   142 			rank--;
       
   143 	} 
       
   144 	
       
   145 	return rank;
       
   146 }
       
   147 
       
   148 BitSequence**
       
   149 create_matrix(int M, int Q)
       
   150 {
       
   151 	int			i;
       
   152 	BitSequence	**matrix;
       
   153 	
       
   154 	if ( (matrix = (BitSequence **) calloc(M, sizeof(BitSequence *))) == NULL ) {
       
   155 		printf("ERROR IN FUNCTION create_matrix:  Insufficient memory available.\n");
       
   156 		
       
   157 		return NULL;
       
   158 	}
       
   159 	else {
       
   160 		for ( i=0; i<M; i++ ) {
       
   161 			if ( (matrix[i] = (unsigned char*)calloc(Q, sizeof(BitSequence))) == NULL ) {
       
   162 				printf("ERROR IN FUNCTION create_matrix: Insufficient memory for %dx%d matrix.\n", M, M);
       
   163 
       
   164 				return NULL;
       
   165 			}
       
   166 		}
       
   167 		return matrix;
       
   168 	}
       
   169 }
       
   170 
       
   171 void
       
   172 def_matrix(int M, int Q, BitSequence **m,int k)
       
   173 {
       
   174 	int		i,j;
       
   175 	
       
   176 	for ( i=0; i<M; i++ ) 
       
   177 		for ( j=0; j<Q; j++ )
       
   178 			m[i][j] = epsilon[k*(M*Q)+j+i*M];
       
   179 }
       
   180 
       
   181 void
       
   182 delete_matrix(int M, BitSequence **matrix)
       
   183 {
       
   184 	int		i;
       
   185 
       
   186 	for ( i=0; i<M; i++ )
       
   187 		free(matrix[i]);
       
   188 	free(matrix);
       
   189 }