Botan  2.8.0
Crypto and TLS for C++11
curve_gfp.h
Go to the documentation of this file.
1 /*
2 * Elliptic curves over GF(p)
3 *
4 * (C) 2007 Martin Doering, Christoph Ludwig, Falko Strenzke
5 * 2010-2011,2012,2014 Jack Lloyd
6 *
7 * Botan is released under the Simplified BSD License (see license.txt)
8 */
9 
10 #ifndef BOTAN_GFP_CURVE_H_
11 #define BOTAN_GFP_CURVE_H_
12 
13 #include <botan/bigint.h>
14 #include <memory>
15 
16 namespace Botan {
17 
19  {
20  public:
21  virtual ~CurveGFp_Repr() = default;
22 
23  virtual const BigInt& get_p() const = 0;
24  virtual const BigInt& get_a() const = 0;
25  virtual const BigInt& get_b() const = 0;
26 
27  virtual size_t get_p_words() const = 0;
28 
29  virtual size_t get_ws_size() const = 0;
30 
31  virtual bool is_one(const BigInt& x) const = 0;
32 
33  virtual bool a_is_zero() const = 0;
34 
35  virtual bool a_is_minus_3() const = 0;
36 
37  /*
38  * Returns to_curve_rep(get_a())
39  */
40  virtual const BigInt& get_a_rep() const = 0;
41 
42  /*
43  * Returns to_curve_rep(get_b())
44  */
45  virtual const BigInt& get_b_rep() const = 0;
46 
47  /*
48  * Returns to_curve_rep(1)
49  */
50  virtual const BigInt& get_1_rep() const = 0;
51 
52  virtual void redc_mod_p(BigInt& z, secure_vector<word>& ws) const = 0;
53 
54  virtual BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const = 0;
55 
56  virtual void to_curve_rep(BigInt& x, secure_vector<word>& ws) const = 0;
57 
58  virtual void from_curve_rep(BigInt& x, secure_vector<word>& ws) const = 0;
59 
60  void curve_mul(BigInt& z, const BigInt& x, const BigInt& y,
61  secure_vector<word>& ws) const
62  {
63  BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
64  curve_mul_words(z, x.data(), x.size(), y, ws);
65  }
66 
67  virtual void curve_mul_words(BigInt& z,
68  const word x_words[],
69  const size_t x_size,
70  const BigInt& y,
71  secure_vector<word>& ws) const = 0;
72 
73  void curve_sqr(BigInt& z, const BigInt& x,
74  secure_vector<word>& ws) const
75  {
76  BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
77  curve_sqr_words(z, x.data(), x.size(), ws);
78  }
79 
80  virtual void curve_sqr_words(BigInt& z,
81  const word x_words[],
82  size_t x_size,
83  secure_vector<word>& ws) const = 0;
84  };
85 
86 /**
87 * This class represents an elliptic curve over GF(p)
88 *
89 * There should not be any reason for applications to use this type.
90 * If you need EC primitives use the interfaces EC_Group and PointGFp
91 *
92 * It is likely this class will be removed entirely in a future major
93 * release.
94 */
96  {
97  public:
98 
99  /**
100  * Create an uninitialized CurveGFp
101  */
102  CurveGFp() = default;
103 
104  /**
105  * Construct the elliptic curve E: y^2 = x^3 + ax + b over GF(p)
106  * @param p prime number of the field
107  * @param a first coefficient
108  * @param b second coefficient
109  */
110  CurveGFp(const BigInt& p, const BigInt& a, const BigInt& b) :
111  m_repr(choose_repr(p, a, b))
112  {
113  }
114 
115  CurveGFp(const CurveGFp&) = default;
116 
117  CurveGFp& operator=(const CurveGFp&) = default;
118 
119  /**
120  * @return curve coefficient a
121  */
122  const BigInt& get_a() const { return m_repr->get_a(); }
123 
124  /**
125  * @return curve coefficient b
126  */
127  const BigInt& get_b() const { return m_repr->get_b(); }
128 
129  /**
130  * Get prime modulus of the field of the curve
131  * @return prime modulus of the field of the curve
132  */
133  const BigInt& get_p() const { return m_repr->get_p(); }
134 
135  size_t get_p_words() const { return m_repr->get_p_words(); }
136 
137  size_t get_ws_size() const { return m_repr->get_ws_size(); }
138 
139  const BigInt& get_a_rep() const { return m_repr->get_a_rep(); }
140 
141  const BigInt& get_b_rep() const { return m_repr->get_b_rep(); }
142 
143  const BigInt& get_1_rep() const { return m_repr->get_1_rep(); }
144 
145  bool a_is_minus_3() const { return m_repr->a_is_minus_3(); }
146  bool a_is_zero() const { return m_repr->a_is_zero(); }
147 
148  bool is_one(const BigInt& x) const { return m_repr->is_one(x); }
149 
151  {
152  return m_repr->invert_element(x, ws);
153  }
154 
155  void to_rep(BigInt& x, secure_vector<word>& ws) const
156  {
157  m_repr->to_curve_rep(x, ws);
158  }
159 
160  void from_rep(BigInt& x, secure_vector<word>& ws) const
161  {
162  m_repr->from_curve_rep(x, ws);
163  }
164 
166  {
167  BigInt xt(x);
168  m_repr->from_curve_rep(xt, ws);
169  return xt;
170  }
171 
172  // TODO: from_rep taking && ref
173 
175  {
176  m_repr->redc_mod_p(z, ws);
177  }
178 
179  void mul(BigInt& z, const BigInt& x, const BigInt& y, secure_vector<word>& ws) const
180  {
181  m_repr->curve_mul(z, x, y, ws);
182  }
183 
184  void mul(BigInt& z, const word x_w[], size_t x_size,
185  const BigInt& y, secure_vector<word>& ws) const
186  {
187  m_repr->curve_mul_words(z, x_w, x_size, y, ws);
188  }
189 
190  void sqr(BigInt& z, const BigInt& x, secure_vector<word>& ws) const
191  {
192  m_repr->curve_sqr(z, x, ws);
193  }
194 
195  void sqr(BigInt& z, const word x_w[], size_t x_size, secure_vector<word>& ws) const
196  {
197  m_repr->curve_sqr_words(z, x_w, x_size, ws);
198  }
199 
200  BigInt mul(const BigInt& x, const BigInt& y, secure_vector<word>& ws) const
201  {
202  return mul_to_tmp(x, y, ws);
203  }
204 
205  BigInt sqr(const BigInt& x, secure_vector<word>& ws) const
206  {
207  return sqr_to_tmp(x, ws);
208  }
209 
210  BigInt mul_to_tmp(const BigInt& x, const BigInt& y, secure_vector<word>& ws) const
211  {
212  BigInt z;
213  m_repr->curve_mul(z, x, y, ws);
214  return z;
215  }
216 
218  {
219  BigInt z;
220  m_repr->curve_sqr(z, x, ws);
221  return z;
222  }
223 
224  void swap(CurveGFp& other)
225  {
226  std::swap(m_repr, other.m_repr);
227  }
228 
229  /**
230  * Equality operator
231  * @param other a curve
232  * @return true iff *this is the same as other
233  */
234  inline bool operator==(const CurveGFp& other) const
235  {
236  if(m_repr.get() == other.m_repr.get())
237  return true;
238 
239  return (get_p() == other.get_p()) &&
240  (get_a() == other.get_a()) &&
241  (get_b() == other.get_b());
242  }
243 
244  private:
245  static std::shared_ptr<CurveGFp_Repr>
246  choose_repr(const BigInt& p, const BigInt& a, const BigInt& b);
247 
248  std::shared_ptr<CurveGFp_Repr> m_repr;
249  };
250 
251 inline bool operator!=(const CurveGFp& lhs, const CurveGFp& rhs)
252  {
253  return !(lhs == rhs);
254  }
255 
256 }
257 
258 namespace std {
259 
260 template<> inline
261 void swap<Botan::CurveGFp>(Botan::CurveGFp& curve1,
263  {
264  curve1.swap(curve2);
265  }
266 
267 } // namespace std
268 
269 #endif
#define BOTAN_UNSTABLE_API
Definition: compiler.h:34
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:90
bool a_is_minus_3() const
Definition: curve_gfp.h:145
const BigInt & get_a_rep() const
Definition: curve_gfp.h:139
void to_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:155
const BigInt & get_b_rep() const
Definition: curve_gfp.h:141
CurveGFp(const BigInt &p, const BigInt &a, const BigInt &b)
Definition: curve_gfp.h:110
size_t get_p_words() const
Definition: curve_gfp.h:135
BigInt from_rep(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:165
int(* final)(unsigned char *, CTX *)
void redc_mod_p(BigInt &z, secure_vector< word > &ws) const
Definition: curve_gfp.h:174
Definition: bigint.h:891
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:179
size_t get_ws_size() const
Definition: curve_gfp.h:137
void sqr(BigInt &z, const word x_w[], size_t x_size, secure_vector< word > &ws) const
Definition: curve_gfp.h:195
#define BOTAN_NOEXCEPT
Definition: compiler.h:154
void from_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:160
const word * data() const
Definition: bigint.h:569
void curve_mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:60
const BigInt & get_1_rep() const
Definition: curve_gfp.h:143
BigInt invert_element(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:150
void curve_sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:73
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
void mul(BigInt &z, const word x_w[], size_t x_size, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:184
bool operator==(const CurveGFp &other) const
Definition: curve_gfp.h:234
size_t size() const
Definition: bigint.h:531
bool is_one(const BigInt &x) const
Definition: curve_gfp.h:148
BigInt sqr_to_tmp(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:217
void swap(CurveGFp &other)
Definition: curve_gfp.h:224
Definition: alg_id.cpp:13
size_t sig_words() const
Definition: bigint.h:537
BigInt mul_to_tmp(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:210
const BigInt & get_b() const
Definition: curve_gfp.h:127
BigInt sqr(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:205
const BigInt & get_a() const
Definition: curve_gfp.h:122
bool a_is_zero() const
Definition: curve_gfp.h:146
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
const BigInt & get_p() const
Definition: curve_gfp.h:133
void sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:190
BigInt mul(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:200