First public contribution.
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 CMontgomeryStructure* CMontgomeryStructure::NewL(
27 const TInteger& aModulus)
29 CMontgomeryStructure* self = CMontgomeryStructure::NewLC(aModulus);
30 CleanupStack::Pop(self);
34 CMontgomeryStructure* CMontgomeryStructure::NewLC(
35 const TInteger& aModulus)
37 CMontgomeryStructure* self = new(ELeave) CMontgomeryStructure;
38 CleanupStack::PushL(self);
39 self->ConstructL(aModulus);
43 CMontgomeryStructure::~CMontgomeryStructure()
51 void CMontgomeryStructure::ConstructL(const TInteger& aModulus)
53 User::LeaveIfError(aModulus.IsOdd() ? KErrNone : KErrArgument);
55 iModulusInv = RInteger::NewEmptyL(aModulus.Size());
56 iWorkspace = RInteger::NewEmptyL(5*aModulus.Size());
57 iModulus = RInteger::NewL(aModulus);
58 iResult = RInteger::NewEmptyL(aModulus.Size());
59 RecursiveInverseModPower2(iModulusInv.Ptr(), iWorkspace.Ptr(),
60 iModulus.Ptr(), iModulus.Size());
63 CMontgomeryStructure::CMontgomeryStructure()
67 TInteger& CMontgomeryStructure::ConvertInL(TInteger& aInteger) const
69 aInteger <<= WordsToBits(iModulus.Size());
74 TInteger& CMontgomeryStructure::ConvertOutL(TInteger& aInteger) const
76 TUint* const T = iWorkspace.Ptr();
77 TUint* const R = aInteger.Ptr();
78 const TUint N = iModulus.Size();
79 User::LeaveIfError((aInteger.Size() <= N) ? KErrNone : KErrArgument);
81 CopyWords(T, aInteger.Ptr(), aInteger.Size());
82 SetWords(T + aInteger.Size(), 0, 2*N - aInteger.Size());
83 MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
87 const TInteger& CMontgomeryStructure::ReduceL(
88 const TInteger& aInteger) const
90 RInteger temp = RInteger::NewL(aInteger);
91 CleanupStack::PushL(temp);
93 iResult.CopyL(ConvertOutL(temp), EFalse);
94 CleanupStack::PopAndDestroy(&temp);
98 const TInteger& CMontgomeryStructure::MultiplyL(const TInteger& aA,
99 const TInteger& aB) const
101 RInteger a = RInteger::NewL(aA);
102 CleanupStack::PushL(a);
103 RInteger b = RInteger::NewL(aB);
104 CleanupStack::PushL(b);
105 DoMultiplyL(iResult, ConvertInL(a), ConvertInL(b));
106 ConvertOutL(iResult);
107 CleanupStack::PopAndDestroy(&b);
108 CleanupStack::PopAndDestroy(&a);
112 void CMontgomeryStructure::DoMultiplyL(TInteger& aResult, const TInteger& aA,
113 const TInteger& aB) const
115 User::LeaveIfError((aResult.Size() == iModulus.Size()) ? KErrNone : KErrArgument);
117 TUint* const T = iWorkspace.Ptr();
118 TUint* const R = aResult.Ptr();
119 const TUint N = iModulus.Size();
120 const TUint* const aReg = aA.Ptr();
121 const TUint* const bReg = aB.Ptr();
122 const TUint aSize = aA.Size();
123 const TUint bSize = aB.Size();
124 User::LeaveIfError((aSize <= N && bSize <= N) ? KErrNone : KErrArgument);
126 AsymmetricMultiply(T, T+2*N, aReg, aSize, bReg, bSize);
127 SetWords(T+aSize+bSize, 0, 2*N - aSize - bSize);
128 MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
131 const TInteger& CMontgomeryStructure::SquareL(const TInteger& aA) const
133 RInteger a = RInteger::NewL(aA);
134 CleanupStack::PushL(a);
135 DoSquareL(iResult, ConvertInL(a));
136 ConvertOutL(iResult);
137 CleanupStack::PopAndDestroy(&a);
141 void CMontgomeryStructure::DoSquareL(TInteger& aResult, const TInteger& aA) const
143 User::LeaveIfError((aResult.Size() == iModulus.Size()) ? KErrNone : KErrArgument);
144 TUint* const T = iWorkspace.Ptr();
145 TUint* const R = aResult.Ptr();
146 const TUint N = iModulus.Size();
147 const TUint* const aReg = aA.Ptr();
148 const TUint aSize = aA.Size();
150 User::LeaveIfError((aSize <= N) ? KErrNone : KErrArgument);
152 RecursiveSquare(T, T+2*N, aReg, aSize);
153 SetWords(T+2*aSize, 0, 2*N-2*aSize);
154 MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
157 const TInteger& CMontgomeryStructure::ExponentiateL(
158 const TInteger& aBase, const TInteger& aExponent) const
161 if ((iResult.Size() != iModulus.Size()) ||
162 (aBase >= iModulus) ||
163 (!aBase.IsPositive()) ||
164 (!aExponent.IsPositive()))
166 User::Leave(KErrArgument);
169 // 1.1 Precomputation
172 RInteger g2 = RInteger::NewL(aBase);
173 CleanupStack::PushL(g2);
175 //ConvertIn can shrink g2, because we call DoSquare on g2, g2 must be the same size as the modulus
176 g2.CleanGrowL(iModulus.Size());
177 RInteger g1 = RInteger::NewL(g2);
178 CleanupStack::PushL(g1);
181 TWindowSlider slider(aExponent);
184 // For i from 1 to (2^(k-1) -1) do g2i+1 <- g2i-1 * g2
185 TUint count = (1 << (slider.WindowSize()-1)) - 1; //2^(k-1) -1
186 RRArray<RInteger> powerArray(count+1); //+1 because we append g1
187 User::LeaveIfError(powerArray.Append(g1));
188 CleanupStack::Pop(&g1);
189 CleanupClosePushL(powerArray);
190 for(TUint k=1; k <= count; k++)
192 RInteger gi = RInteger::NewEmptyL(iModulus.Size());
193 DoMultiplyL(gi, g2, powerArray[k-1]);
194 User::LeaveIfError(powerArray.Append(gi));
198 RInteger temp = RInteger::NewL(TInteger::One());
199 CleanupStack::PushL(temp);
202 RInteger& A = iResult;
203 //Set A to one converted in for this modulus without changing the memory size of A (iResult)
204 A.CopyL(temp, EFalse);
205 CleanupStack::PopAndDestroy(&temp);
207 TInt i = aExponent.BitCount() - 1;
212 // 3.1 If ei == 0 then A <- A^2
213 if(!aExponent.Bit(i))
218 // 3.2 Find longest bitstring ei,ei-1,...,el s.t. i-l+1<=k and el==1
220 // A <- (A^2^(i-l+1)) * g[the index indicated by the bitstring value]
223 slider.FindNextWindow(i);
224 assert(slider.Length() >= 1);
225 for(TUint j=0; j<slider.Length(); j++)
229 DoMultiplyL(A, A, powerArray[slider.Value()>>1]);
230 i -= slider.Length();
233 CleanupStack::PopAndDestroy(2, &g2); //powerArray, g2
234 return ConvertOutL(A); // A == iResult