|
1 /* |
|
2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "tmontgomeryfb.h" |
|
20 #include "t_input.h" |
|
21 #include "t_output.h" |
|
22 #include "tutils.h" |
|
23 #include <bigint.h> |
|
24 #include <random.h> |
|
25 #include "../../source/bigint/mont.h" |
|
26 |
|
27 CTestAction* CMontgomeryFB::NewL(RFs& aFs, CConsoleBase& aConsole, |
|
28 Output& aOut, const TTestActionSpec& aTestActionSpec) |
|
29 { |
|
30 CTestAction* self = CMontgomeryFB::NewLC(aFs, aConsole, |
|
31 aOut, aTestActionSpec); |
|
32 CleanupStack::Pop(); |
|
33 return self; |
|
34 } |
|
35 |
|
36 CTestAction* CMontgomeryFB::NewLC(RFs& aFs, CConsoleBase& aConsole, |
|
37 Output& aOut, const TTestActionSpec& aTestActionSpec) |
|
38 { |
|
39 CMontgomeryFB* self = new(ELeave) CMontgomeryFB(aFs, aConsole, aOut); |
|
40 CleanupStack::PushL(self); |
|
41 self->ConstructL(aTestActionSpec); |
|
42 return self; |
|
43 } |
|
44 |
|
45 CMontgomeryFB::~CMontgomeryFB() |
|
46 { |
|
47 delete iBody; |
|
48 } |
|
49 |
|
50 CMontgomeryFB::CMontgomeryFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut) |
|
51 : CTestAction(aConsole, aOut), iFs(aFs) |
|
52 { |
|
53 } |
|
54 |
|
55 void CMontgomeryFB::ConstructL(const TTestActionSpec& aTestActionSpec) |
|
56 { |
|
57 CTestAction::ConstructL(aTestActionSpec); |
|
58 |
|
59 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); |
|
60 iBody->Des().Copy(aTestActionSpec.iActionBody); |
|
61 |
|
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; |
|
72 } |
|
73 |
|
74 void CMontgomeryFB::DoPerformPrerequisite(TRequestStatus& aStatus) |
|
75 { |
|
76 TRequestStatus* status = &aStatus; |
|
77 User::RequestComplete(status, KErrNone); |
|
78 iActionState = CTestAction::EAction; |
|
79 } |
|
80 |
|
81 void CMontgomeryFB::DoPerformPostrequisite(TRequestStatus& aStatus) |
|
82 { |
|
83 TRequestStatus* status = &aStatus; |
|
84 iFinished = ETrue; |
|
85 User::RequestComplete(status, KErrNone); |
|
86 } |
|
87 |
|
88 void CMontgomeryFB::DoReportAction(void) |
|
89 { |
|
90 } |
|
91 |
|
92 void CMontgomeryFB::DoCheckResult(TInt) |
|
93 { |
|
94 } |
|
95 |
|
96 void CMontgomeryFB::PerformAction(TRequestStatus& aStatus) |
|
97 { |
|
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; |
|
103 } |
|
104 |
|
105 void CMontgomeryFB::PerformActionL() |
|
106 { |
|
107 __UHEAP_MARK; |
|
108 |
|
109 iResult = ETrue; |
|
110 |
|
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++) |
|
116 { |
|
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); |
|
121 |
|
122 //generate a random number of x | 2 < x < prime |
|
123 RInteger base = RInteger::NewRandomL(TInteger::Two(), prime); |
|
124 CleanupStack::PushL(base); |
|
125 |
|
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); |
|
130 if( y != base ) |
|
131 { |
|
132 iResult = EFalse; |
|
133 iConsole.Printf(_L("X")); |
|
134 iOut.writeString(_L("Failure exponentiating:")); |
|
135 iOut.writeNewLine(); |
|
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); |
|
140 } |
|
141 |
|
142 CleanupStack::PopAndDestroy(3, &prime);//base,mont,prime |
|
143 |
|
144 iConsole.Printf(_L(".")); |
|
145 } |
|
146 |
|
147 __UHEAP_MARKEND; |
|
148 } |
|
149 |