os/security/crypto/weakcryptospi/source/bigint/words.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
/**
sl@0
    20
 @file 
sl@0
    21
 @internalComponent
sl@0
    22
*/
sl@0
    23
 
sl@0
    24
#ifndef __BIGINT_WORDS_H__
sl@0
    25
#define __BIGINT_WORDS_H__
sl@0
    26
sl@0
    27
#include "../common/inlines.h"
sl@0
    28
sl@0
    29
inline void ArraySetBit(TUint* aS, TUint aJ)
sl@0
    30
	{
sl@0
    31
	aS[aJ/WORD_BITS] |= (1L << (aJ%WORD_BITS));
sl@0
    32
	}
sl@0
    33
sl@0
    34
inline TUint CountWords(const TUint *X, TUint N)
sl@0
    35
	{
sl@0
    36
	while (N && X[N-1]==0)
sl@0
    37
		N--;
sl@0
    38
	return N;
sl@0
    39
	}
sl@0
    40
sl@0
    41
inline void SetWords(TUint *r, TUint a, TUint n)
sl@0
    42
	{
sl@0
    43
	Mem::Fill(r, WordsToBytes(n), a);
sl@0
    44
	}
sl@0
    45
sl@0
    46
inline void CopyWords(TUint *r, const TUint *a, TUint n)
sl@0
    47
	{
sl@0
    48
	Mem::Copy(r, a, WordsToBytes(n));
sl@0
    49
	}
sl@0
    50
sl@0
    51
inline TUint ShiftWordsLeftByBits(TUint *r, TUint n, TUint shiftBits)
sl@0
    52
	{
sl@0
    53
	assert (shiftBits<WORD_BITS);
sl@0
    54
	TUint u, carry=0;
sl@0
    55
	if (shiftBits)
sl@0
    56
		for (TUint i=0; i<n; i++)
sl@0
    57
			{
sl@0
    58
			u = r[i];
sl@0
    59
			r[i] = (u << shiftBits) | carry;
sl@0
    60
			carry = u >> (WORD_BITS-shiftBits);
sl@0
    61
			}
sl@0
    62
	return carry;
sl@0
    63
	}
sl@0
    64
sl@0
    65
inline TUint ShiftWordsRightByBits(TUint *r, TUint n, TUint shiftBits)
sl@0
    66
{
sl@0
    67
	assert (shiftBits<WORD_BITS);
sl@0
    68
	TUint u, carry=0;
sl@0
    69
	if (shiftBits)
sl@0
    70
		for (int i=n-1; i>=0; i--)
sl@0
    71
			{
sl@0
    72
			u = r[i];
sl@0
    73
			r[i] = (u >> shiftBits) | carry;
sl@0
    74
			carry = u << (WORD_BITS-shiftBits);
sl@0
    75
			}
sl@0
    76
	return carry;
sl@0
    77
	}
sl@0
    78
sl@0
    79
inline TUint CryptoMin(TUint aLeft, TUint aRight) 
sl@0
    80
	{
sl@0
    81
	return(aLeft<aRight ? aLeft : aRight);
sl@0
    82
	}
sl@0
    83
sl@0
    84
inline TUint CryptoMax(TUint aLeft, TUint aRight)
sl@0
    85
	{
sl@0
    86
	return(aLeft<aRight ? aRight : aLeft);
sl@0
    87
	}
sl@0
    88
sl@0
    89
inline void ShiftWordsLeftByWords(TUint *r, TUint n, TUint shiftWords)
sl@0
    90
	{
sl@0
    91
	shiftWords = CryptoMin(shiftWords, n);
sl@0
    92
	if (shiftWords)
sl@0
    93
		{
sl@0
    94
		for (TUint i=n-1; i>=shiftWords; i--)
sl@0
    95
			r[i] = r[i-shiftWords];
sl@0
    96
		SetWords(r, 0, shiftWords);
sl@0
    97
		}
sl@0
    98
	}
sl@0
    99
sl@0
   100
inline void ShiftWordsRightByWords(TUint *r, TUint n, TUint shiftWords)
sl@0
   101
	{
sl@0
   102
	shiftWords = CryptoMin(shiftWords, n);
sl@0
   103
	if (shiftWords)
sl@0
   104
		{
sl@0
   105
		for (TUint i=0; i+shiftWords<n; i++)
sl@0
   106
			r[i] = r[i+shiftWords];
sl@0
   107
		SetWords(r+n-shiftWords, 0, shiftWords);
sl@0
   108
		}
sl@0
   109
	}
sl@0
   110
sl@0
   111
inline TUint BytePrecision(TUint aValue)
sl@0
   112
	{
sl@0
   113
	TUint x=4;
sl@0
   114
	if(aValue < 0x10000) aValue <<=16, x-=2;
sl@0
   115
	if(aValue < 0x1000000) x--;
sl@0
   116
	if(!aValue) x=0;
sl@0
   117
	return x;
sl@0
   118
	}
sl@0
   119
sl@0
   120
inline TUint BitPrecision(TUint aValue)
sl@0
   121
	{
sl@0
   122
	if(!aValue) return 0;
sl@0
   123
	TUint x=32;
sl@0
   124
	if(aValue < 0x10000)    aValue<<=16, x-=16;
sl@0
   125
	if(aValue < 0x1000000)  aValue<<=8, x-=8;
sl@0
   126
	if(aValue < 0x10000000) aValue<<=4, x-=4;
sl@0
   127
	if(aValue < 0x40000000) aValue<<=2, x-=2;
sl@0
   128
	if(aValue < 0x80000000) --x;
sl@0
   129
	return x;
sl@0
   130
	}
sl@0
   131
sl@0
   132
static const TUint RoundupSizeTable[] = {2, 2, 2, 4, 4, 8, 8, 8, 8};
sl@0
   133
    
sl@0
   134
static inline TUint RoundupSize(TUint n)
sl@0
   135
	{
sl@0
   136
    if (n<=8)
sl@0
   137
        return RoundupSizeTable[n];
sl@0
   138
    else if (n<=16)
sl@0
   139
        return 16;
sl@0
   140
    else if (n<=32)
sl@0
   141
        return 32;
sl@0
   142
    else if (n<=64)
sl@0
   143
        return 64;
sl@0
   144
    else return 1U << BitPrecision(n-1);
sl@0
   145
	}
sl@0
   146
sl@0
   147
#endif