os/security/crypto/weakcrypto/source/bigint/words.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2003-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 the License "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 *
    16 */
    17 
    18 
    19 /**
    20  @file 
    21  @internalComponent
    22 */
    23  
    24 #ifndef __BIGINT_WORDS_H__
    25 #define __BIGINT_WORDS_H__
    26 
    27 #include "../common/inlines.h"
    28 
    29 inline void ArraySetBit(TUint* aS, TUint aJ)
    30 	{
    31 	aS[aJ/WORD_BITS] |= (1L << (aJ%WORD_BITS));
    32 	}
    33 
    34 inline TUint CountWords(const TUint *X, TUint N)
    35 	{
    36 	while (N && X[N-1]==0)
    37 		N--;
    38 	return N;
    39 	}
    40 
    41 inline void SetWords(TUint *r, TUint a, TUint n)
    42 	{
    43 	Mem::Fill(r, WordsToBytes(n), a);
    44 	}
    45 
    46 inline void CopyWords(TUint *r, const TUint *a, TUint n)
    47 	{
    48 	Mem::Copy(r, a, WordsToBytes(n));
    49 	}
    50 
    51 inline TUint ShiftWordsLeftByBits(TUint *r, TUint n, TUint shiftBits)
    52 	{
    53 	assert (shiftBits<WORD_BITS);
    54 	TUint u, carry=0;
    55 	if (shiftBits)
    56 		for (TUint i=0; i<n; i++)
    57 			{
    58 			u = r[i];
    59 			r[i] = (u << shiftBits) | carry;
    60 			carry = u >> (WORD_BITS-shiftBits);
    61 			}
    62 	return carry;
    63 	}
    64 
    65 inline TUint ShiftWordsRightByBits(TUint *r, TUint n, TUint shiftBits)
    66 {
    67 	assert (shiftBits<WORD_BITS);
    68 	TUint u, carry=0;
    69 	if (shiftBits)
    70 		for (int i=n-1; i>=0; i--)
    71 			{
    72 			u = r[i];
    73 			r[i] = (u >> shiftBits) | carry;
    74 			carry = u << (WORD_BITS-shiftBits);
    75 			}
    76 	return carry;
    77 	}
    78 
    79 inline TUint CryptoMin(TUint aLeft, TUint aRight) 
    80 	{
    81 	return(aLeft<aRight ? aLeft : aRight);
    82 	}
    83 
    84 inline TUint CryptoMax(TUint aLeft, TUint aRight)
    85 	{
    86 	return(aLeft<aRight ? aRight : aLeft);
    87 	}
    88 
    89 inline void ShiftWordsLeftByWords(TUint *r, TUint n, TUint shiftWords)
    90 	{
    91 	shiftWords = CryptoMin(shiftWords, n);
    92 	if (shiftWords)
    93 		{
    94 		for (TUint i=n-1; i>=shiftWords; i--)
    95 			r[i] = r[i-shiftWords];
    96 		SetWords(r, 0, shiftWords);
    97 		}
    98 	}
    99 
   100 inline void ShiftWordsRightByWords(TUint *r, TUint n, TUint shiftWords)
   101 	{
   102 	shiftWords = CryptoMin(shiftWords, n);
   103 	if (shiftWords)
   104 		{
   105 		for (TUint i=0; i+shiftWords<n; i++)
   106 			r[i] = r[i+shiftWords];
   107 		SetWords(r+n-shiftWords, 0, shiftWords);
   108 		}
   109 	}
   110 
   111 inline TUint BytePrecision(TUint aValue)
   112 	{
   113 	TUint x=4;
   114 	if(aValue < 0x10000) aValue <<=16, x-=2;
   115 	if(aValue < 0x1000000) x--;
   116 	if(!aValue) x=0;
   117 	return x;
   118 	}
   119 
   120 inline TUint BitPrecision(TUint aValue)
   121 	{
   122 	if(!aValue) return 0;
   123 	TUint x=32;
   124 	if(aValue < 0x10000)    aValue<<=16, x-=16;
   125 	if(aValue < 0x1000000)  aValue<<=8, x-=8;
   126 	if(aValue < 0x10000000) aValue<<=4, x-=4;
   127 	if(aValue < 0x40000000) aValue<<=2, x-=2;
   128 	if(aValue < 0x80000000) --x;
   129 	return x;
   130 	}
   131 
   132 static const TUint RoundupSizeTable[] = {2, 2, 2, 4, 4, 8, 8, 8, 8};
   133     
   134 static inline TUint RoundupSize(TUint n)
   135 	{
   136     if (n<=8)
   137         return RoundupSizeTable[n];
   138     else if (n<=16)
   139         return 16;
   140     else if (n<=32)
   141         return 32;
   142     else if (n<=64)
   143         return 64;
   144     else return 1U << BitPrecision(n-1);
   145 	}
   146 
   147 #endif