|
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 "tmontgomeryvector.h" |
|
20 #include "t_input.h" |
|
21 #include <bigint.h> |
|
22 #include "../../source/bigint/mont.h" |
|
23 |
|
24 CTestAction* CMontgomeryVector::NewL(RFs& aFs, CConsoleBase& aConsole, |
|
25 Output& aOut, const TTestActionSpec& aTestActionSpec) |
|
26 { |
|
27 CTestAction* self = CMontgomeryVector::NewLC(aFs, aConsole, |
|
28 aOut, aTestActionSpec); |
|
29 CleanupStack::Pop(); |
|
30 return self; |
|
31 } |
|
32 |
|
33 CTestAction* CMontgomeryVector::NewLC(RFs& aFs, CConsoleBase& aConsole, |
|
34 Output& aOut, const TTestActionSpec& aTestActionSpec) |
|
35 { |
|
36 CMontgomeryVector* self = new(ELeave) CMontgomeryVector(aFs, aConsole, aOut); |
|
37 CleanupStack::PushL(self); |
|
38 self->ConstructL(aTestActionSpec); |
|
39 return self; |
|
40 } |
|
41 |
|
42 CMontgomeryVector::~CMontgomeryVector() |
|
43 { |
|
44 delete iBody; |
|
45 delete iA; |
|
46 delete iB; |
|
47 delete iModulus; |
|
48 delete iAns; |
|
49 } |
|
50 |
|
51 CMontgomeryVector::CMontgomeryVector(RFs& aFs, CConsoleBase& aConsole, |
|
52 Output& aOut) : CTestAction(aConsole, aOut), iFs(aFs) |
|
53 { |
|
54 } |
|
55 |
|
56 void CMontgomeryVector::ConstructL(const TTestActionSpec& aTestActionSpec) |
|
57 { |
|
58 CTestAction::ConstructL(aTestActionSpec); |
|
59 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); |
|
60 iBody->Des().Copy(aTestActionSpec.iActionBody); |
|
61 |
|
62 iA = Input::ParseElementHexL(*iBody, _L8("<a>")); |
|
63 iB = Input::ParseElementHexL(*iBody, _L8("<b>")); |
|
64 iModulus = Input::ParseElementHexL(*iBody, _L8("<modulus>")); |
|
65 iAns = Input::ParseElementHexL(*iBody, _L8("<ans>")); |
|
66 TPtrC8 op = Input::ParseElement(*iBody, _L8("<op>")); |
|
67 if( op == _L8("multiply") ) |
|
68 { |
|
69 iOp = EMultiply; |
|
70 } |
|
71 else if( op == _L8("square") ) |
|
72 { |
|
73 iOp = ESquare; |
|
74 } |
|
75 else if( op == _L8("reduce") ) |
|
76 { |
|
77 iOp = EReduce; |
|
78 } |
|
79 else if( op == _L8("exponentiate") ) |
|
80 { |
|
81 iOp = EExponentiate; |
|
82 } |
|
83 else |
|
84 { |
|
85 User::Panic(_L("tmontgomeryvector"), 1); |
|
86 } |
|
87 } |
|
88 |
|
89 void CMontgomeryVector::DoPerformPrerequisite(TRequestStatus& aStatus) |
|
90 { |
|
91 TRequestStatus* status = &aStatus; |
|
92 User::RequestComplete(status, KErrNone); |
|
93 iActionState = CTestAction::EAction; |
|
94 } |
|
95 |
|
96 void CMontgomeryVector::DoPerformPostrequisite(TRequestStatus& aStatus) |
|
97 { |
|
98 TRequestStatus* status = &aStatus; |
|
99 iFinished = ETrue; |
|
100 User::RequestComplete(status, KErrNone); |
|
101 } |
|
102 |
|
103 void CMontgomeryVector::DoReportAction(void) |
|
104 { |
|
105 } |
|
106 |
|
107 void CMontgomeryVector::DoCheckResult(TInt) |
|
108 { |
|
109 } |
|
110 |
|
111 void CMontgomeryVector::PerformAction(TRequestStatus& aStatus) |
|
112 { |
|
113 __UHEAP_MARK; |
|
114 TRequestStatus* status = &aStatus; |
|
115 iResult = ETrue; |
|
116 |
|
117 RInteger a = RInteger::NewL(*iA); |
|
118 CleanupStack::PushL(a); |
|
119 RInteger b = RInteger::NewL(*iB); |
|
120 CleanupStack::PushL(b); |
|
121 RInteger modulus = RInteger::NewL(*iModulus); |
|
122 CleanupStack::PushL(modulus); |
|
123 RInteger ans = RInteger::NewL(*iAns); |
|
124 CleanupStack::PushL(ans); |
|
125 CMontgomeryStructure* mont = CMontgomeryStructure::NewL(modulus); |
|
126 CleanupStack::PushL(mont); |
|
127 //we don't own out at any point, it remains the propery of mont |
|
128 const TInteger* out = 0; |
|
129 switch(iOp) |
|
130 { |
|
131 case EMultiply: |
|
132 out = &(mont->MultiplyL(a, b)); |
|
133 break; |
|
134 case ESquare: |
|
135 out = &(mont->SquareL(a)); |
|
136 break; |
|
137 case EReduce: |
|
138 out = &(mont->ReduceL(a)); |
|
139 break; |
|
140 case EExponentiate: |
|
141 out = &(mont->ExponentiateL(a, b)); |
|
142 break; |
|
143 default: |
|
144 User::Panic(_L("tbasicmathsvector"), 2); |
|
145 break; |
|
146 } |
|
147 |
|
148 if( *out != ans ) |
|
149 { |
|
150 iResult = EFalse; |
|
151 } |
|
152 CleanupStack::PopAndDestroy(mont); |
|
153 CleanupStack::PopAndDestroy(4); //ans, modulus, b,a |
|
154 |
|
155 User::RequestComplete(status, KErrNone); |
|
156 iActionState = CTestAction::EPostrequisite; |
|
157 __UHEAP_MARKEND; |
|
158 } |
|
159 |