sl@0: /* sl@0: * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "tmontgomeryfb.h" sl@0: #include "t_input.h" sl@0: #include "t_output.h" sl@0: #include "tutils.h" sl@0: #include sl@0: #include sl@0: #include "../../source/bigint/mont.h" sl@0: sl@0: CTestAction* CMontgomeryFB::NewL(RFs& aFs, CConsoleBase& aConsole, sl@0: Output& aOut, const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CMontgomeryFB::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CMontgomeryFB::NewLC(RFs& aFs, CConsoleBase& aConsole, sl@0: Output& aOut, const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CMontgomeryFB* self = new(ELeave) CMontgomeryFB(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CMontgomeryFB::~CMontgomeryFB() sl@0: { sl@0: delete iBody; sl@0: } sl@0: sl@0: CMontgomeryFB::CMontgomeryFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut) sl@0: : CTestAction(aConsole, aOut), iFs(aFs) sl@0: { sl@0: } sl@0: sl@0: void CMontgomeryFB::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction::ConstructL(aTestActionSpec); sl@0: sl@0: iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); sl@0: iBody->Des().Copy(aTestActionSpec.iActionBody); sl@0: sl@0: HBufC8* length = Input::ParseElementHexL(*iBody, _L8("")); sl@0: CleanupStack::PushL(length); sl@0: RInteger clength = RInteger::NewL(*length); sl@0: CleanupStack::PopAndDestroy(length); sl@0: CleanupStack::PushL(clength); sl@0: TUint bits = clength.ConvertToLongL(); sl@0: CleanupStack::PopAndDestroy();//clength sl@0: // the final /7 gives the number of times we have to increment by 7 to get sl@0: // to that number of bytes and hence bits. sl@0: iIterations = ((bits+7)/8)/7 + 1; sl@0: } sl@0: sl@0: void CMontgomeryFB::DoPerformPrerequisite(TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, KErrNone); sl@0: iActionState = CTestAction::EAction; sl@0: } sl@0: sl@0: void CMontgomeryFB::DoPerformPostrequisite(TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: iFinished = ETrue; sl@0: User::RequestComplete(status, KErrNone); sl@0: } sl@0: sl@0: void CMontgomeryFB::DoReportAction(void) sl@0: { sl@0: } sl@0: sl@0: void CMontgomeryFB::DoCheckResult(TInt) sl@0: { sl@0: } sl@0: sl@0: void CMontgomeryFB::PerformAction(TRequestStatus& aStatus) sl@0: { sl@0: TRAPD(res, PerformActionL()); sl@0: __ASSERT_ALWAYS(!res, User::Panic(_L("CMontgomeryFB::PerformAction"), res)); sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, KErrNone); sl@0: iActionState = CTestAction::EPostrequisite; sl@0: } sl@0: sl@0: void CMontgomeryFB::PerformActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: iResult = ETrue; sl@0: sl@0: //Generate iIterations*7 byte random sequences we are using 7 as it's a sl@0: //generator mod 8. Thus we'll cycle through every value (0-7) every 8 sl@0: //iterations. This gives us a better feeling that certain byte lengths sl@0: //(and thus bit lengths as the byte is chosen randomly) don't have errors. sl@0: for(TUint i=1; i<=iIterations; i++) sl@0: { sl@0: //generate a prime of roughly i*7*8 bits sl@0: RInteger prime = RInteger::NewPrimeL(i*7*8); sl@0: CleanupStack::PushL(prime); sl@0: CMontgomeryStructure* mont = CMontgomeryStructure::NewLC(prime); sl@0: sl@0: //generate a random number of x | 2 < x < prime sl@0: RInteger base = RInteger::NewRandomL(TInteger::Two(), prime); sl@0: CleanupStack::PushL(base); sl@0: sl@0: //This is using Fermat's Little Theorem sl@0: // (base ^ prime) % prime == base or sl@0: // (base ^ prime-1) % prime == 1 sl@0: const TInteger& y = mont->ExponentiateL(base, prime); sl@0: if( y != base ) sl@0: { sl@0: iResult = EFalse; sl@0: iConsole.Printf(_L("X")); sl@0: iOut.writeString(_L("Failure exponentiating:")); sl@0: iOut.writeNewLine(); sl@0: Utils::DumpInteger(iOut, _L("base: "), base); sl@0: Utils::DumpInteger(iOut, _L("prime: "), prime); sl@0: Utils::DumpInteger(iOut, _L("output: "), (const RInteger&)y); sl@0: Utils::DumpInteger(iOut, _L("expected: "), base); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(3, &prime);//base,mont,prime sl@0: sl@0: iConsole.Printf(_L(".")); sl@0: } sl@0: sl@0: __UHEAP_MARKEND; sl@0: } sl@0: