|
1 /* |
|
2 * Copyright (c) 2003-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 <bigint.h> |
|
20 #include <e32std.h> |
|
21 #include <random.h> |
|
22 #include "../common/inlines.h" |
|
23 |
|
24 |
|
25 /** |
|
26 * Copy constructor |
|
27 * |
|
28 * This function performs a shallow copy, |
|
29 * i.e. the memory holding the integer is not copied. |
|
30 */ |
|
31 EXPORT_C RInteger::RInteger(const RInteger& aInteger) |
|
32 { |
|
33 *this = aInteger; |
|
34 } |
|
35 |
|
36 /** |
|
37 * Assignment operator |
|
38 * |
|
39 * This function performs a shallow copy, |
|
40 * i.e. the memory holding the integer is not copied. |
|
41 */ |
|
42 EXPORT_C RInteger& RInteger::operator=(const RInteger& aInteger) |
|
43 { |
|
44 iSize = aInteger.iSize; |
|
45 iPtr = aInteger.iPtr; |
|
46 return *this; |
|
47 } |
|
48 |
|
49 |
|
50 /** |
|
51 * Creates a new integer representing 0. |
|
52 * |
|
53 * @return An RInteger by value. |
|
54 */ |
|
55 EXPORT_C RInteger RInteger::NewL(void) |
|
56 { |
|
57 return NewL(TInteger::Zero()); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Creates a new integer object representing a specified value. |
|
62 * |
|
63 * @param aValue A descriptor containing the big-endian binary |
|
64 * representation of the value. |
|
65 * @return An RInteger object representing the value. |
|
66 */ |
|
67 EXPORT_C RInteger RInteger::NewL(const TDesC8& aValue) |
|
68 { |
|
69 RInteger self; |
|
70 //Construct zero's memory beyond the size of aValue after construction |
|
71 self.CreateNewL(BytesToWords(aValue.Size())); |
|
72 self.Construct(aValue); |
|
73 return self; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Creates an exact copy of an \c aInteger object. |
|
78 * |
|
79 * @param aInteger The integer you wish to copy |
|
80 * @return An RInteger object representing an exact copy of |
|
81 * aInteger by value. |
|
82 */ |
|
83 EXPORT_C RInteger RInteger::NewL(const TInteger& aInteger) |
|
84 { |
|
85 RInteger self; |
|
86 //don't need to CleanNewL as we'll copy straight from aInteger |
|
87 self.CreateNewL(aInteger.Size()); |
|
88 self.Construct(aInteger); |
|
89 return self; |
|
90 } |
|
91 |
|
92 /** |
|
93 * Creates a random integer uniformly distributed over [0, 2^aBits]. |
|
94 * |
|
95 * @param aBits The number of bits you wish to randomly select. |
|
96 * @param aAttr Enum specifying whether specific bits in the random number should |
|
97 * be set. See TRandomAttribute for more information. |
|
98 * @return A random RInteger object in the range specified. |
|
99 */ |
|
100 EXPORT_C RInteger RInteger::NewRandomL(TUint aBits, TRandomAttribute aAttr) |
|
101 { |
|
102 RInteger self; |
|
103 self.CleanNewL(BitsToWords(aBits)); |
|
104 CleanupStack::PushL(self); |
|
105 self.RandomizeL(aBits, aAttr); |
|
106 CleanupStack::Pop(&self); |
|
107 return self; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Creates a random integer uniformly distributed over [x | min <= x <= max]. |
|
112 * |
|
113 * @param aMin The smallest possible value for the random integer (inclusive). |
|
114 * @param aMax The largest possible value for the random integer (inclusive). |
|
115 * @return A random RInteger object in the range specified. |
|
116 */ |
|
117 EXPORT_C RInteger RInteger::NewRandomL(const TInteger& aMin, |
|
118 const TInteger& aMax) |
|
119 { |
|
120 RInteger self; |
|
121 self.CleanNewL(aMax.Size()); |
|
122 CleanupStack::PushL(self); |
|
123 self.RandomizeL(aMin, aMax); |
|
124 CleanupStack::Pop(&self); |
|
125 return self; |
|
126 } |
|
127 |
|
128 /** |
|
129 * Finds a random prime integer in the range of [2, 2^aBits]. |
|
130 * |
|
131 * This is done by picking a random integer and using that as a starting point |
|
132 * for a sequential search for a prime. To verify the primality of number, |
|
133 * this algorithm uses a probablistic primality test. This means that it is |
|
134 * possible, although extremely improbable, that the number returned is a pseudoprime. |
|
135 * |
|
136 * @param aBits The number of bits you wish to randomly select your prime from. |
|
137 * @param aAttr Enum specifying whether specific bits in the random number should |
|
138 * be set. See TRandomAttribute for more information. |
|
139 * @return A random RInteger representing a probable prime (with very high |
|
140 * probablity) in the range specified. |
|
141 */ |
|
142 EXPORT_C RInteger RInteger::NewPrimeL(TUint aBits, TRandomAttribute aAttr) |
|
143 { |
|
144 RInteger self; |
|
145 self.CleanNewL(BitsToWords(aBits)); |
|
146 CleanupStack::PushL(self); |
|
147 self.PrimeRandomizeL(aBits, aAttr); |
|
148 CleanupStack::Pop(&self); |
|
149 return self; |
|
150 } |
|
151 |
|
152 /** |
|
153 * Creates a new integer from the value represented by \c aInteger. |
|
154 * |
|
155 * @param aInteger A signed word sized integer. |
|
156 * @return An RInteger representation of aInteger by value. |
|
157 */ |
|
158 EXPORT_C RInteger RInteger::NewL(TInt aInteger) |
|
159 { |
|
160 RInteger self; |
|
161 self.CreateNewL(2); |
|
162 self.Construct(aInteger); |
|
163 return self; |
|
164 } |
|
165 |
|
166 /** |
|
167 * Creates a new integer from the value represented by \c aInteger. |
|
168 * |
|
169 * @param aInteger An unsigned word sized integer. |
|
170 * @return An RInteger representation of aInteger by value. |
|
171 */ |
|
172 EXPORT_C RInteger RInteger::NewL(TUint aInteger) |
|
173 { |
|
174 RInteger self; |
|
175 self.CreateNewL(2); |
|
176 self.Construct(aInteger); |
|
177 return self; |
|
178 } |
|
179 |
|
180 /** |
|
181 * Creates a new integer with a preallocated internal storage of \c aNumWords all |
|
182 * initialised to zero. |
|
183 * |
|
184 * The resulting RInteger object is logically equivalent to RInteger::NewL(0). |
|
185 * The only difference is that the internal storage requirements have been |
|
186 * specified to be larger than the default. This is useful if you are about |
|
187 * to perform an operation on this integer, that you know the resulting size |
|
188 * requirements of, and wish to avoid a heap resize. |
|
189 * |
|
190 * @param aNumWords The number of words for to preallocated and zero fill. |
|
191 * @return An RInteger object representing 0 with a preallocated |
|
192 * zero-filled internal storage of aNumWords. |
|
193 */ |
|
194 EXPORT_C RInteger RInteger::NewEmptyL(TUint aNumWords) |
|
195 { |
|
196 RInteger self; |
|
197 self.CleanNewL(aNumWords); |
|
198 //There's no construct as there isn't anything to do |
|
199 return self; |
|
200 } |
|
201 |
|
202 /** |
|
203 * Creates an RInteger object with no associated internal (heap) storage. |
|
204 * |
|
205 * All data members are initialised to zero. It is safe (although not strictly necessary) |
|
206 * to push such an RInteger object onto the CleanupStack. This is useful, for example, if |
|
207 * you want to pass an RInteger object by reference into a function and have it create |
|
208 * the representation of the actual integer for you. |
|
209 * |
|
210 * Note that performing any operation on such an RInteger object other than the default |
|
211 * assignment operator or copy constructor will panic your code. |
|
212 * |
|
213 * @return A stack based class that has no associated internal storage and thus |
|
214 * does not represent any number. |
|
215 */ |
|
216 EXPORT_C RInteger::RInteger(void) |
|
217 { |
|
218 } |
|
219 |
|
220 /** |
|
221 * An overloaded TCleanupItem() allowing the RIntegers to be pushed, |
|
222 * popped, and destroyed via the CleanupStack like any other CBase derived object. |
|
223 */ |
|
224 EXPORT_C RInteger::operator TCleanupItem(void) |
|
225 { |
|
226 return TCleanupItem(&RInteger::CallClose, this); |
|
227 } |
|
228 |
|
229 /** |
|
230 * Helper function registered with the cleanup stack that just calls Close() for |
|
231 * this RInteger object. |
|
232 * |
|
233 * @param aPtr A pointer to the object for which clean-up is to be performed. |
|
234 */ |
|
235 EXPORT_C void RInteger::CallClose(TAny* aPtr) |
|
236 { |
|
237 ((RInteger*)aPtr)->Close(); |
|
238 } |
|
239 |
|
240 /** |
|
241 * Zeros and then frees any memory owned by this RInteger object. |
|
242 * |
|
243 * An RInteger object that has been closed can safely fall off the stack. |
|
244 */ |
|
245 EXPORT_C void RInteger::Close(void) |
|
246 { |
|
247 if (iPtr) |
|
248 { |
|
249 Mem::FillZ(Ptr(), Size()*4); |
|
250 User::Free(Ptr()); |
|
251 iSize = 0; |
|
252 iPtr = NULL; |
|
253 } |
|
254 } |