aboutsummaryrefslogtreecommitdiffstats
path: root/ecdsa/curve_gfp.h
blob: 49688b5dca3dbf1ae3f4d0e1590bb1b86ca661e6 (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
/******************************************************
 * Elliptic curves over GF(p) (header file)           *
 *                                                    *
 * (C) 2007 Martin Doering                             *
 *          doering@cdc.informatik.tu-darmstadt.de    *
 *          Christoph Ludwig                          *
 *          ludwig@fh-worms.de                        *
 *          Falko Strenzke                            *
 *          strenzke@flexsecure.de                    *
 ******************************************************/

#ifndef BOTAN_EC_CURVE_GFP_H__
#define BOTAN_EC_CURVE_GFP_H__

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

namespace Botan {

/**
* This class represents an elliptic curve over GF(p)
*/
class CurveGFp
   {
   public:
      /**
      * Construct the elliptic curve E: y^2 = x^3 + ax + b over GF(p)
      * @param a first coefficient
      * @param b second coefficient
      * @param p prime number of the field
      */
      CurveGFp(GFpElement const& a, GFpElement const& b,
               const BigInt& p);

      /**
      * Copy constructor
      * @param other The curve to clone
      */
      CurveGFp(CurveGFp const& other);

      /**
      * Assignment operator
      * @param other The curve to use as source for the assignment
      */
      CurveGFp const& operator=(CurveGFp const& other);

      /**
      * Set the shared GFpModulus object.
      * Warning: do not use this function unless you know in detail how
      * the sharing of values
      * in the various EC related objects works.
      * Do NOT spread pointers to a GFpModulus over different threads!
      * @param mod a shared pointer to a GFpModulus object suitable for
      * *this.
      */
      void set_shrd_mod(std::tr1::shared_ptr<Botan::GFpModulus> const mod);

      // getters

      /**
      * Get coefficient a
      * @result coefficient a
      */
      GFpElement const get_a() const;

      /**
      * Get coefficient b
      * @result coefficient b
      */
      GFpElement const get_b() const;

      /**
      * Get the GFpElement coefficient a  transformed
      * to its m-residue. This can be used for efficency reasons: the curve
      * stores the transformed version after the first invocation of this
      * function.
      * @result the coefficient a, transformed to its m-residue
      */
      GFpElement const get_mres_a() const;

      /**
      * Get the GFpElement coefficient b transformed
      * to it´s m-residue. This can be used for efficency reasons: the curve
      * stores the transformed version after the first invocation of this
      * function.
      * @result the coefficient b, transformed to it´s m-residue
      */
      GFpElement const get_mres_b() const;


      /**
      * Get the GFpElement 1  transformed
      * to it´s m-residue. This can be used for efficency reasons: the curve
      * stores the transformed version after the first invocation of this
      * function.
      * @result the GFpElement 1, transformed to it´s m-residue
      */
      std::tr1::shared_ptr<GFpElement const> const get_mres_one() const;

      /**
      * Get prime modulus of the field of the curve
      * @result prime modulus of the field of the curve
      */
      BigInt const get_p() const;
      /*inline std::tr1::shared_ptr<BigInt> const get_ptr_p() const
      {
      return mp_p;
      }*/

      /**
      * Retrieve a shared pointer to the curves GFpModulus object for efficient storage
      * and computation of montgomery multiplication related data members and functions.
      * Warning: do not use this function unless you know in detail how the sharing of values
      * in the various EC related objects works.
      * Do NOT spread pointers to a GFpModulus over different threads!
      * @result a shared pointer to a GFpModulus object
      */
      inline std::tr1::shared_ptr<Botan::GFpModulus> const get_ptr_mod() const
         {
         return mp_mod;
         }

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

   private:
      std::tr1::shared_ptr<Botan::GFpModulus> mp_mod;
      GFpElement mA;
      GFpElement mB;
      mutable std::tr1::shared_ptr<GFpElement> mp_mres_a;
      mutable std::tr1::shared_ptr<GFpElement> mp_mres_b;
      mutable std::tr1::shared_ptr<GFpElement> mp_mres_one;
   };

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

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

}

namespace std {

template<> inline void swap<Botan::CurveGFp>(
   Botan::CurveGFp& curve1,
   Botan::CurveGFp& curve2)
   {
   curve1.swap(curve2);
   }

}

#endif