Botan  1.10.9
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 Jack Lloyd
6 *
7 * Distributed under the terms of the Botan license
8 */
9 
10 #ifndef BOTAN_GFP_CURVE_H__
11 #define BOTAN_GFP_CURVE_H__
12 
13 #include <botan/numthry.h>
14 
15 namespace Botan {
16 
17 /**
18 * This class represents an elliptic curve over GF(p)
19 */
20 class BOTAN_DLL CurveGFp
21  {
22  public:
23 
24  /**
25  * Create an uninitialized CurveGFp
26  */
27  CurveGFp() : p_words(0), p_dash(0) {}
28 
29  /**
30  * Construct the elliptic curve E: y^2 = x^3 + ax + b over GF(p)
31  * @param p prime number of the field
32  * @param a first coefficient
33  * @param b second coefficient
34  */
35  CurveGFp(const BigInt& p, const BigInt& a, const BigInt& b) :
36  p(p), a(a), b(b), p_words(p.sig_words())
37  {
38  BigInt r(BigInt::Power2, p_words * BOTAN_MP_WORD_BITS);
39 
40  p_dash = (((r * inverse_mod(r, p)) - 1) / p).word_at(0);
41 
42  r2 = (r * r) % p;
43  a_r = (a * r) % p;
44  b_r = (b * r) % p;
45  }
46 
47  // CurveGFp(const CurveGFp& other) = default;
48  // CurveGFp& operator=(const CurveGFp& other) = default;
49 
50  /**
51  * @return curve coefficient a
52  */
53  const BigInt& get_a() const { return a; }
54 
55  /**
56  * @return curve coefficient b
57  */
58  const BigInt& get_b() const { return b; }
59 
60  /**
61  * Get prime modulus of the field of the curve
62  * @return prime modulus of the field of the curve
63  */
64  const BigInt& get_p() const { return p; }
65 
66  /**
67  * @return Montgomery parameter r^2 % p
68  */
69  const BigInt& get_r2() const { return r2; }
70 
71  /**
72  * @return a * r mod p
73  */
74  const BigInt& get_a_r() const { return a_r; }
75 
76  /**
77  * @return b * r mod p
78  */
79  const BigInt& get_b_r() const { return b_r; }
80 
81  /**
82  * @return Montgomery parameter p-dash
83  */
84  word get_p_dash() const { return p_dash; }
85 
86  /**
87  * @return p.sig_words()
88  */
89  size_t get_p_words() const { return p_words; }
90 
91  /**
92  * swaps the states of *this and other, does not throw
93  * @param other curve to swap values with
94  */
95  void swap(CurveGFp& other)
96  {
97  std::swap(p, other.p);
98 
99  std::swap(a, other.a);
100  std::swap(b, other.b);
101 
102  std::swap(a_r, other.a_r);
103  std::swap(b_r, other.b_r);
104 
105  std::swap(p_words, other.p_words);
106 
107  std::swap(r2, other.r2);
108  std::swap(p_dash, other.p_dash);
109  }
110 
111  /**
112  * Equality operator
113  * @param other curve to compare with
114  * @return true iff this is the same curve as other
115  */
116  bool operator==(const CurveGFp& other) const
117  {
118  /*
119  Relies on choice of R, but that is fixed by constructor based
120  on size of p
121  */
122  return (p == other.p && a_r == other.a_r && b_r == other.b_r);
123  }
124 
125  private:
126  // Curve parameters
127  BigInt p, a, b;
128 
129  size_t p_words; // cache of p.sig_words()
130 
131  // Montgomery parameters
132  BigInt r2, a_r, b_r;
133  word p_dash;
134  };
135 
136 /**
137 * Equality operator
138 * @param lhs a curve
139 * @param rhs a curve
140 * @return true iff lhs is not the same as rhs
141 */
142 inline bool operator!=(const CurveGFp& lhs, const CurveGFp& rhs)
143  {
144  return !(lhs == rhs);
145  }
146 
147 }
148 
149 namespace std {
150 
151 template<> inline
152 void swap<Botan::CurveGFp>(Botan::CurveGFp& curve1,
153  Botan::CurveGFp& curve2)
154  {
155  curve1.swap(curve2);
156  }
157 
158 } // namespace std
159 
160 #endif
const BigInt & get_a() const
Definition: curve_gfp.h:53
bool operator!=(const OctetString &s1, const OctetString &s2)
Definition: symkey.cpp:106
CurveGFp(const BigInt &p, const BigInt &a, const BigInt &b)
Definition: curve_gfp.h:35
const BigInt & get_b() const
Definition: curve_gfp.h:58
const BigInt & get_b_r() const
Definition: curve_gfp.h:79
Definition: secmem.h:422
bool operator==(const CurveGFp &other) const
Definition: curve_gfp.h:116
size_t get_p_words() const
Definition: curve_gfp.h:89
const BigInt & get_r2() const
Definition: curve_gfp.h:69
const BigInt & get_a_r() const
Definition: curve_gfp.h:74
void swap(CurveGFp &other)
Definition: curve_gfp.h:95
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:202
const BigInt & get_p() const
Definition: curve_gfp.h:64
BigInt r
Definition: numthry.cpp:26
word get_p_dash() const
Definition: curve_gfp.h:84
void swap(Botan::MemoryRegion< T > &x, Botan::MemoryRegion< T > &y)
Definition: secmem.h:425