aboutsummaryrefslogtreecommitdiffstats
path: root/ecdsa/gfp_element.h
blob: e9850df3081e57626430c4e1f3bd10ae0850a0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/******************************************************
 * Arithmetic for prime fields GF(p) (header file)    *
 *                                                    *
 * (C) 2007 Martin Döring                             *
*          doering@cdc.informatik.tu-darmstadt.de    *
*          Christoph Ludwig                          *
*          ludwig@fh-worms.de                        *
*          Falko Strenzke                            *
*          strenzke@flexsecure.de                    *
******************************************************/

#ifndef BOTAN_MATH_GF_GFP_ELEMENT_H_GUARD_
#define BOTAN_MATH_GF_GFP_ELEMENT_H_GUARD_

#include <botan/gfp_modulus.h>
#include <botan/bigint.h>
#include <tr1/memory>

namespace Botan
{

struct Illegal_Transformation : public Exception
{
    Illegal_Transformation(const std::string& err = "Requested transformation is not possible")
    : Exception(err) {}
};

/**
* This class represents one element in GF(p). Enables the convenient, transparent use
* of the montgomery multiplication.
*/
class GFpElement
   {

   private:
      std::tr1::shared_ptr<GFpModulus> mp_mod;
      mutable BigInt m_value; // ordinary residue or m-residue respectively
      mutable BigInt workspace;
      // *****************************************
      // data members for montgomery multiplication
      mutable bool m_use_montgm;
      //mutable BigInt m_mres;
      // this bool tells use whether the m_mres carries
      // the actual value (in this case mValue doesn´t)
      mutable bool m_is_trf;


      void ensure_montgm_precomp() const;
      void trf_to_mres() const;
      void trf_to_ordres() const;

   public:


      /** construct an element of GF(p) with the given value.
      * use_montg defaults to false and determines wether Montgomery multiplications
      * will be use when applying operators '*' , '*='.
      * @param p the prime number of the field
      * @param value the element value
      * @param use_montgm whether this object will use Montgomery multiplication
      */
      explicit GFpElement ( const BigInt& p, const BigInt& value, bool use_montgm = false );


      /** construct an element of GF(p) with the given value (defaults to 0).
      * use_montg defaults to false and determines wether montgomery multiplications
      * will be use when applying operators '*' , '*='.
      * Use this constructor for efficient use of Montgomery multiplication in a context with a
      * fixed a modulus.
      * Warning: do not use this function unless you know in detail about
      * the implications of using
      * the shared GFpModulus objects!
      * @param mod shared pointer to the GFpModulus to be shared
      * @param value the element value
      * @param use_montgm whether this object will use Montgomery multiplication
      */
      explicit GFpElement(std::tr1::shared_ptr<GFpModulus> const mod, const BigInt& value, bool use_mongm = false);

      /**
      * Copy constructor
      * @param other The element to clone
      */
      GFpElement ( GFpElement const& other );

      /**
      * Assignment operator.
      * makes *this a totally independent object
      * (gives *this independent modulus specific values).
      *
      * @param other The element to assign to our object
      */
      GFpElement const& operator= ( GFpElement const& other );

      /**
      * Works like the assignment operator, but lets
      * *this share the modulus dependend value with other.
      * Warning: do not use this function unless you know in detail about
      * the implications of using
      * the shared GFpModulus objects!
      * @param other The element to assign to our object
      */
      void share_assign(GFpElement const& other);

      /**
      * Switch Montgomery multiplcation optimizations ON
      */
      void turn_on_sp_red_mul() const;

      /**
      * Switch Montgomery multiplcation optimizations OFF
      */
      void turn_off_sp_red_mul() const;

      /**
      * += Operator
      * @param rhs the GFpElement to add to the local value
      * @result *this
      */
      GFpElement& operator+= ( GFpElement const& rhs );

      /**
      * -= Operator
      * @param rhs the GFpElement to subtract from the local value
      * @result *this
      */
      GFpElement& operator-= ( GFpElement const& rhs );

      /**
      * *= Operator
      * @param rhs the GFpElement to multiply with the local value
      * @result *this
      */
      GFpElement& operator*= ( GFpElement const& rhs );
      /**
      * /= Operator
      * @param rhs the GFpElement to divide the local value by
      * @result *this
      */
      GFpElement& operator/= ( GFpElement const& rhs );

      /**
      * *= Operator
      * @param rhs the value to multiply with the local value
      * @result *this
      */
      GFpElement& operator*= (u32bit rhs);

      /**
      * Negate internal value ( *this *= -1 )
      * @return *this
      */
      GFpElement& negate();

      /**
      * Assigns the inverse of *this to *this, i.e.
      * *this = (*this)^(-1)
      * @result *this
      */
      GFpElement& inverse_in_place();

      /**
      * checks whether the value is zero (without provoking
      * a backtransformation to the ordinary-residue)
      * @result true, if the value is zero, false otherwise.
      */
      bool is_zero();

      /**
      * return prime number of GF(p)
      * @result a prime number
      */
      BigInt const get_p() const;

      /**
      * Return the represented value in GF(p)
      * @result The value in GF(p)
      */
      BigInt const get_value() const;

      /**
      * Returns the shared pointer to the GFpModulus of *this.
      * Warning: do not use this function unless you know in detail about
      * the implications of using
      * the shared GFpModulus objects!
      * @result the shared pointer to the GFpModulus of *this
      */
      inline std::tr1::shared_ptr<GFpModulus> const get_ptr_mod() const
         {
         return mp_mod;
         }


      /**
      * Sets the shared pointer to the GFpModulus of *this.
      * Warning: do not use this function unless you know in detail about
      * the implications of using
      * the shared GFpModulus objects!
      * @param mod a shared pointer to a GFpModulus that will be held in *this
      */
      void set_shrd_mod(std::tr1::shared_ptr<GFpModulus> const mod);

      /**
      * Tells whether this GFpElement is currently transformed to it´ m-residue,
      * i.e. in the form x_bar = x * r mod m.
      * @result true if it is currently transformed to it´s m-residue.
      */
      bool is_trf_to_mres() const;

      /**
      * Transforms this to x_bar = x * r mod m
      * @result return the value x_bar.
      */
      BigInt const get_mres() const;

      /**
      * Check, if montgomery multiplication is used.
      * @result true, if montgomery multiplication is used, false otherwise
      */
      bool is_use_montgm() const
         {
         return m_use_montgm;
         }

      /**
      * Transforms the arguments in such way that either both
      * are in m-residue representation (returns true) or both are
      * in ordinary residue representation (returns false).
      * m-residue is prefered in case of ambiguity.
      * does not toggle m_use_montgm of the arguments.
      * Don´t be confused about the constness of the arguments:
      * the transformation between normal residue and m-residue is
      * considered as leaving the object const.
      * @param lhs the first operand to be aligned
      * @param rhs the second operand to be aligned
      * @result true if both are transformed to their m-residue,
      * false it both are transformed to their normal residue.
      */
      static bool align_operands_res(GFpElement const& lhs, GFpElement const& rhs);

      //friend declarations for non-member functions

      /**
      * write a GFpElement to an output stream.
      * @param output the output stream to write to
      * @param elem the object to write
      * @result the output stream
      */
      friend class Point_Coords_GFp;

      /**
      * swaps the states of *this and other, does not throw!
      * @param other The value to swap with
      */
      void swap ( GFpElement& other );

   };

// relational operators
bool operator== ( GFpElement const& lhs, GFpElement const& rhs );
inline bool operator!= ( GFpElement const& lhs, GFpElement const& rhs )
   {
   return !operator== ( lhs, rhs );
   }

// arithmetic operators
GFpElement operator+ ( GFpElement const& lhs, GFpElement const& rhs );
GFpElement operator- ( GFpElement const& lhs, GFpElement const& rhs );
GFpElement operator- ( GFpElement const& lhs );

GFpElement operator* ( GFpElement const& lhs, GFpElement const& rhs );
GFpElement operator/ ( GFpElement const& lhs, GFpElement const& rhs );
GFpElement operator* (GFpElement const& lhs, u32bit rhs);
GFpElement operator* (u32bit rhs, GFpElement const& lhs);

// return (*this)^(-1)
GFpElement inverse ( GFpElement const& elem );

// encoding and decoding
SecureVector<byte> FE2OSP ( GFpElement const& elem );
GFpElement OS2FEP ( MemoryRegion<byte> const& os, BigInt p );


// swaps the states of elem1 and elem2, does not throw!
// cf. Meyers, Item 25
inline
void swap ( GFpElement& elem1, GFpElement& elem2 )
   {
   elem1.swap ( elem2 );
   }

} // namespace Botan

namespace std
{

// swaps the states of elem1 and elem2, does not throw!
// cf. Meyers, Item 25
template<>
inline
void swap< Botan::GFpElement>(Botan::GFpElement& elem1,
                              Botan::GFpElement& elem2)
   {
   elem1.swap(elem2);
   }

} // namespace std

#endif