Botan  2.4.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  /*
30  * Returns to_curve_rep(get_a())
31  */
32  virtual const BigInt& get_a_rep() const = 0;
33 
34  /*
35  * Returns to_curve_rep(get_b())
36  */
37  virtual const BigInt& get_b_rep() const = 0;
38 
39  virtual void to_curve_rep(BigInt& x, secure_vector<word>& ws) const = 0;
40 
41  virtual void from_curve_rep(BigInt& x, secure_vector<word>& ws) const = 0;
42 
43  virtual void curve_mul(BigInt& z, const BigInt& x, const BigInt& y,
44  secure_vector<word>& ws) const = 0;
45 
46  virtual void curve_sqr(BigInt& z, const BigInt& x,
47  secure_vector<word>& ws) const = 0;
48  };
49 
50 /**
51 * This class represents an elliptic curve over GF(p)
52 */
53 class BOTAN_PUBLIC_API(2,0) CurveGFp final
54  {
55  public:
56 
57  /**
58  * Create an uninitialized CurveGFp
59  */
60  CurveGFp() = default;
61 
62  /**
63  * Construct the elliptic curve E: y^2 = x^3 + ax + b over GF(p)
64  * @param p prime number of the field
65  * @param a first coefficient
66  * @param b second coefficient
67  */
68  CurveGFp(const BigInt& p, const BigInt& a, const BigInt& b) :
69  m_repr(choose_repr(p, a, b))
70  {
71  }
72 
73  CurveGFp(const CurveGFp&) = default;
74 
75  CurveGFp& operator=(const CurveGFp&) = default;
76 
77  /**
78  * @return curve coefficient a
79  */
80  const BigInt& get_a() const { return m_repr->get_a(); }
81 
82  /**
83  * @return curve coefficient b
84  */
85  const BigInt& get_b() const { return m_repr->get_b(); }
86 
87  /**
88  * Get prime modulus of the field of the curve
89  * @return prime modulus of the field of the curve
90  */
91  const BigInt& get_p() const { return m_repr->get_p(); }
92 
93  const BigInt& get_a_rep() const { return m_repr->get_a_rep(); }
94 
95  const BigInt& get_b_rep() const { return m_repr->get_b_rep(); }
96 
97  void to_rep(BigInt& x, secure_vector<word>& ws) const
98  {
99  m_repr->to_curve_rep(x, ws);
100  }
101 
102  void from_rep(BigInt& x, secure_vector<word>& ws) const
103  {
104  m_repr->from_curve_rep(x, ws);
105  }
106 
108  {
109  BigInt xt(x);
110  m_repr->from_curve_rep(xt, ws);
111  return xt;
112  }
113 
114  // TODO: from_rep taking && ref
115 
116  void mul(BigInt& z, const BigInt& x, const BigInt& y, secure_vector<word>& ws) const
117  {
118  m_repr->curve_mul(z, x, y, ws);
119  }
120 
121  BigInt mul(const BigInt& x, const BigInt& y, secure_vector<word>& ws) const
122  {
123  BigInt z;
124  m_repr->curve_mul(z, x, y, ws);
125  return z;
126  }
127 
128  void sqr(BigInt& z, const BigInt& x, secure_vector<word>& ws) const
129  {
130  m_repr->curve_sqr(z, x, ws);
131  }
132 
133  BigInt sqr(const BigInt& x, secure_vector<word>& ws) const
134  {
135  BigInt z;
136  m_repr->curve_sqr(z, x, ws);
137  return z;
138  }
139 
140  void swap(CurveGFp& other)
141  {
142  std::swap(m_repr, other.m_repr);
143  }
144 
145  private:
146  static std::shared_ptr<CurveGFp_Repr>
147  choose_repr(const BigInt& p, const BigInt& a, const BigInt& b);
148 
149  std::shared_ptr<CurveGFp_Repr> m_repr;
150  };
151 
152 /**
153 * Equality operator
154 * @param lhs a curve
155 * @param rhs a curve
156 * @return true iff lhs is the same as rhs
157 */
158 inline bool operator==(const CurveGFp& lhs, const CurveGFp& rhs)
159  {
160  return (lhs.get_p() == rhs.get_p()) &&
161  (lhs.get_a() == rhs.get_a()) &&
162  (lhs.get_b() == rhs.get_b());
163  }
164 
165 inline bool operator!=(const CurveGFp& lhs, const CurveGFp& rhs)
166  {
167  return !(lhs == rhs);
168  }
169 
170 }
171 
172 namespace std {
173 
174 template<> inline
175 void swap<Botan::CurveGFp>(Botan::CurveGFp& curve1,
177  {
178  curve1.swap(curve2);
179  }
180 
181 } // namespace std
182 
183 #endif
#define BOTAN_UNSTABLE_API
Definition: compiler.h:34
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:90
const BigInt & get_a_rep() const
Definition: curve_gfp.h:93
void to_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:97
const BigInt & get_b_rep() const
Definition: curve_gfp.h:95
CurveGFp(const BigInt &p, const BigInt &a, const BigInt &b)
Definition: curve_gfp.h:68
BigInt from_rep(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:107
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Definition: bigint.h:635
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:116
#define BOTAN_NOEXCEPT
Definition: compiler.h:154
void from_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:102
void swap(CurveGFp &other)
Definition: curve_gfp.h:140
Definition: alg_id.cpp:13
const BigInt & get_b() const
Definition: curve_gfp.h:85
BigInt sqr(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:133
const BigInt & get_a() const
Definition: curve_gfp.h:80
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:75
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
const BigInt & get_p() const
Definition: curve_gfp.h:91
void sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:128
BigInt mul(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:121