Update contrib.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
21 #include "algorithms.h"
23 #include "windowslider.h"
26 EXPORT_C CMontgomeryStructure* CMontgomeryStructure::NewLC(
27 const TInteger& aModulus)
29 CMontgomeryStructure* self = new(ELeave) CMontgomeryStructure;
30 CleanupStack::PushL(self);
31 self->ConstructL(aModulus);
35 CMontgomeryStructure::~CMontgomeryStructure()
43 void CMontgomeryStructure::ConstructL(const TInteger& aModulus)
45 User::LeaveIfError(aModulus.IsOdd() ? KErrNone : KErrArgument);
47 iModulusInv = RInteger::NewEmptyL(aModulus.Size());
48 iWorkspace = RInteger::NewEmptyL(5*aModulus.Size());
49 iModulus = RInteger::NewL(aModulus);
50 iResult = RInteger::NewEmptyL(aModulus.Size());
51 RecursiveInverseModPower2(iModulusInv.Ptr(), iWorkspace.Ptr(),
52 iModulus.Ptr(), iModulus.Size());
55 CMontgomeryStructure::CMontgomeryStructure()
59 TInteger& CMontgomeryStructure::ConvertInL(TInteger& aInteger) const
61 aInteger <<= WordsToBits(iModulus.Size());
66 TInteger& CMontgomeryStructure::ConvertOutL(TInteger& aInteger) const
68 TUint* const T = iWorkspace.Ptr();
69 TUint* const R = aInteger.Ptr();
70 const TUint N = iModulus.Size();
71 User::LeaveIfError((aInteger.Size() <= N) ? KErrNone : KErrArgument);
73 CopyWords(T, aInteger.Ptr(), aInteger.Size());
74 SetWords(T + aInteger.Size(), 0, 2*N - aInteger.Size());
75 MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
79 void CMontgomeryStructure::DoMultiplyL(TInteger& aResult, const TInteger& aA,
80 const TInteger& aB) const
82 User::LeaveIfError((aResult.Size() == iModulus.Size()) ? KErrNone : KErrArgument);
84 TUint* const T = iWorkspace.Ptr();
85 TUint* const R = aResult.Ptr();
86 const TUint N = iModulus.Size();
87 const TUint* const aReg = aA.Ptr();
88 const TUint* const bReg = aB.Ptr();
89 const TUint aSize = aA.Size();
90 const TUint bSize = aB.Size();
91 User::LeaveIfError((aSize <= N && bSize <= N) ? KErrNone : KErrArgument);
93 AsymmetricMultiply(T, T+2*N, aReg, aSize, bReg, bSize);
94 SetWords(T+aSize+bSize, 0, 2*N - aSize - bSize);
95 MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
98 const TInteger& CMontgomeryStructure::SquareL(const TInteger& aA) const
100 RInteger a = RInteger::NewL(aA);
101 CleanupStack::PushL(a);
102 DoSquareL(iResult, ConvertInL(a));
103 ConvertOutL(iResult);
104 CleanupStack::PopAndDestroy(&a);
108 void CMontgomeryStructure::DoSquareL(TInteger& aResult, const TInteger& aA) const
110 User::LeaveIfError((aResult.Size() == iModulus.Size()) ? KErrNone : KErrArgument);
111 TUint* const T = iWorkspace.Ptr();
112 TUint* const R = aResult.Ptr();
113 const TUint N = iModulus.Size();
114 const TUint* const aReg = aA.Ptr();
115 const TUint aSize = aA.Size();
117 User::LeaveIfError((aSize <= N) ? KErrNone : KErrArgument);
119 RecursiveSquare(T, T+2*N, aReg, aSize);
120 SetWords(T+2*aSize, 0, 2*N-2*aSize);
121 MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
124 EXPORT_C const TInteger& CMontgomeryStructure::ExponentiateL(
125 const TInteger& aBase, const TInteger& aExponent) const
128 if ((iResult.Size() != iModulus.Size()) ||
129 (aBase >= iModulus) ||
130 (!aBase.IsPositive()) ||
131 (!aExponent.IsPositive()))
133 User::Leave(KErrArgument);
136 // 1.1 Precomputation
139 RInteger g2 = RInteger::NewL(aBase);
140 CleanupStack::PushL(g2);
142 //ConvertInL can shrink g2, because we call DoSquare on g2, g2 must be the same size as the modulus
143 g2.CleanGrowL(iModulus.Size());
144 RInteger g1 = RInteger::NewL(g2);
145 CleanupStack::PushL(g1);
148 TWindowSlider slider(aExponent);
151 // For i from 1 to (2^(k-1) -1) do g2i+1 <- g2i-1 * g2
152 TUint count = (1 << (slider.WindowSize()-1)) - 1; //2^(k-1) -1
153 RRArray<RInteger> powerArray(count+1); //+1 because we append g1
154 User::LeaveIfError(powerArray.Append(g1));
155 CleanupStack::Pop(&g1);
156 CleanupClosePushL(powerArray);
157 for(TUint k=1; k <= count; k++)
159 RInteger gi = RInteger::NewEmptyL(iModulus.Size());
160 DoMultiplyL(gi, g2, powerArray[k-1]);
161 User::LeaveIfError(powerArray.Append(gi));
165 RInteger temp = RInteger::NewL(TInteger::One());
166 CleanupStack::PushL(temp);
169 RInteger& A = iResult;
170 //Set A to one converted in for this modulus without changing the memory size of A (iResult)
171 A.CopyL(temp, EFalse);
172 CleanupStack::PopAndDestroy(&temp);
174 TInt i = aExponent.BitCount() - 1;
179 // 3.1 If ei == 0 then A <- A^2
180 if(!aExponent.Bit(i))
185 // 3.2 Find longest bitstring ei,ei-1,...,el s.t. i-l+1<=k and el==1
187 // A <- (A^2^(i-l+1)) * g[the index indicated by the bitstring value]
190 slider.FindNextWindow(i);
191 assert(slider.Length() >= 1);
192 for(TUint j=0; j<slider.Length(); j++)
196 DoMultiplyL(A, A, powerArray[slider.Value()>>1]);
197 i -= slider.Length();
200 CleanupStack::PopAndDestroy(2, &g2); //powerArray, g2
201 return ConvertOutL(A); // A == iResult
204 // Methods are excluded from coverage due to the problem with BullsEye on ONB.
205 // Manually verified that these methods are functionally covered.
206 #ifdef _BullseyeCoverage
207 #pragma suppress_warnings on
208 #pragma BullseyeCoverage off
209 #pragma suppress_warnings off
212 const TInteger& CMontgomeryStructure::ReduceL(
213 const TInteger& aInteger) const
215 RInteger temp = RInteger::NewL(aInteger);
216 CleanupStack::PushL(temp);
218 iResult.CopyL(ConvertOutL(temp), EFalse);
219 CleanupStack::PopAndDestroy(&temp);
223 CMontgomeryStructure* CMontgomeryStructure::NewL(
224 const TInteger& aModulus)
226 CMontgomeryStructure* self = CMontgomeryStructure::NewLC(aModulus);
227 CleanupStack::Pop(self);
231 const TInteger& CMontgomeryStructure::MultiplyL(const TInteger& aA,
232 const TInteger& aB) const
234 RInteger a = RInteger::NewL(aA);
235 CleanupStack::PushL(a);
236 RInteger b = RInteger::NewL(aB);
237 CleanupStack::PushL(b);
238 DoMultiplyL(iResult, ConvertInL(a), ConvertInL(b));
239 ConvertOutL(iResult);
240 CleanupStack::PopAndDestroy(&b);
241 CleanupStack::PopAndDestroy(&a);