Update contrib.
2 * Copyright (c) 2002-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.
19 #include "tmontgomeryfb.h"
25 #include "../../source/bigint/mont.h"
27 CTestAction* CMontgomeryFB::NewL(RFs& aFs, CConsoleBase& aConsole,
28 Output& aOut, const TTestActionSpec& aTestActionSpec)
30 CTestAction* self = CMontgomeryFB::NewLC(aFs, aConsole,
31 aOut, aTestActionSpec);
36 CTestAction* CMontgomeryFB::NewLC(RFs& aFs, CConsoleBase& aConsole,
37 Output& aOut, const TTestActionSpec& aTestActionSpec)
39 CMontgomeryFB* self = new(ELeave) CMontgomeryFB(aFs, aConsole, aOut);
40 CleanupStack::PushL(self);
41 self->ConstructL(aTestActionSpec);
45 CMontgomeryFB::~CMontgomeryFB()
50 CMontgomeryFB::CMontgomeryFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
51 : CTestAction(aConsole, aOut), iFs(aFs)
55 void CMontgomeryFB::ConstructL(const TTestActionSpec& aTestActionSpec)
57 CTestAction::ConstructL(aTestActionSpec);
59 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
60 iBody->Des().Copy(aTestActionSpec.iActionBody);
62 HBufC8* length = Input::ParseElementHexL(*iBody, _L8("<bits>"));
63 CleanupStack::PushL(length);
64 RInteger clength = RInteger::NewL(*length);
65 CleanupStack::PopAndDestroy(length);
66 CleanupStack::PushL(clength);
67 TUint bits = clength.ConvertToLongL();
68 CleanupStack::PopAndDestroy();//clength
69 // the final /7 gives the number of times we have to increment by 7 to get
70 // to that number of bytes and hence bits.
71 iIterations = ((bits+7)/8)/7 + 1;
74 void CMontgomeryFB::DoPerformPrerequisite(TRequestStatus& aStatus)
76 TRequestStatus* status = &aStatus;
77 User::RequestComplete(status, KErrNone);
78 iActionState = CTestAction::EAction;
81 void CMontgomeryFB::DoPerformPostrequisite(TRequestStatus& aStatus)
83 TRequestStatus* status = &aStatus;
85 User::RequestComplete(status, KErrNone);
88 void CMontgomeryFB::DoReportAction(void)
92 void CMontgomeryFB::DoCheckResult(TInt)
96 void CMontgomeryFB::PerformAction(TRequestStatus& aStatus)
98 TRAPD(res, PerformActionL());
99 __ASSERT_ALWAYS(!res, User::Panic(_L("CMontgomeryFB::PerformAction"), res));
100 TRequestStatus* status = &aStatus;
101 User::RequestComplete(status, KErrNone);
102 iActionState = CTestAction::EPostrequisite;
105 void CMontgomeryFB::PerformActionL()
111 //Generate iIterations*7 byte random sequences we are using 7 as it's a
112 //generator mod 8. Thus we'll cycle through every value (0-7) every 8
113 //iterations. This gives us a better feeling that certain byte lengths
114 //(and thus bit lengths as the byte is chosen randomly) don't have errors.
115 for(TUint i=1; i<=iIterations; i++)
117 //generate a prime of roughly i*7*8 bits
118 RInteger prime = RInteger::NewPrimeL(i*7*8);
119 CleanupStack::PushL(prime);
120 CMontgomeryStructure* mont = CMontgomeryStructure::NewLC(prime);
122 //generate a random number of x | 2 < x < prime
123 RInteger base = RInteger::NewRandomL(TInteger::Two(), prime);
124 CleanupStack::PushL(base);
126 //This is using Fermat's Little Theorem
127 // (base ^ prime) % prime == base or
128 // (base ^ prime-1) % prime == 1
129 const TInteger& y = mont->ExponentiateL(base, prime);
133 iConsole.Printf(_L("X"));
134 iOut.writeString(_L("Failure exponentiating:"));
136 Utils::DumpInteger(iOut, _L("base: "), base);
137 Utils::DumpInteger(iOut, _L("prime: "), prime);
138 Utils::DumpInteger(iOut, _L("output: "), (const RInteger&)y);
139 Utils::DumpInteger(iOut, _L("expected: "), base);
142 CleanupStack::PopAndDestroy(3, &prime);//base,mont,prime
144 iConsole.Printf(_L("."));