Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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/numthry.h>
14 #include <memory>
15
16 namespace Botan {
17
18 class CurveGFp_Repr
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_DLL CurveGFp
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
107 BigInt from_rep(const BigInt& x, secure_vector<word>& ws) const
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,
176 Botan::CurveGFp& curve2) BOTAN_NOEXCEPT
177 {
178 curve1.swap(curve2);
179 }
180
181 } // namespace std
182
183 #endif
Botan::CurveGFp::get_a
const BigInt & get_a() const
Definition: curve_gfp.h:80
Botan::CurveGFp::sqr
void sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:128
Botan::CurveGFp_Repr::get_p
virtual const BigInt & get_p() const =0
Botan::BigInt
Definition: bigint.h:23
Botan::CurveGFp::mul
BigInt mul(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:121
Botan::CurveGFp::CurveGFp
CurveGFp(const BigInt &p, const BigInt &a, const BigInt &b)
Definition: curve_gfp.h:68
Botan::CurveGFp::get_b
const BigInt & get_b() const
Definition: curve_gfp.h:85
Botan::operator!=
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:82
Botan::CurveGFp_Repr
Definition: curve_gfp.h:18
Botan::operator==
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:67
std
Definition: bigint.h:619
Botan::CurveGFp::get_a_rep
const BigInt & get_a_rep() const
Definition: curve_gfp.h:93
Botan::CurveGFp::sqr
BigInt sqr(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:133
BOTAN_NOEXCEPT
#define BOTAN_NOEXCEPT
Definition: compiler.h:116
Botan::CurveGFp_Repr::curve_sqr
virtual void curve_sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const =0
Botan::CurveGFp_Repr::from_curve_rep
virtual void from_curve_rep(BigInt &x, secure_vector< word > &ws) const =0
Botan::CurveGFp_Repr::get_a
virtual const BigInt & get_a() const =0
Botan::CurveGFp_Repr::get_b_rep
virtual const BigInt & get_b_rep() const =0
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::CurveGFp::to_rep
void to_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:97
Botan::CurveGFp::from_rep
void from_rep(BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:102
Botan::CurveGFp_Repr::~CurveGFp_Repr
virtual ~CurveGFp_Repr()=default
Botan::CurveGFp::get_b_rep
const BigInt & get_b_rep() const
Definition: curve_gfp.h:95
Botan::CurveGFp::swap
void swap(CurveGFp &other)
Definition: curve_gfp.h:140
Botan
Definition: alg_id.cpp:13
Botan::CurveGFp_Repr::curve_mul
virtual void curve_mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const =0
Botan::CurveGFp::from_rep
BigInt from_rep(const BigInt &x, secure_vector< word > &ws) const
Definition: curve_gfp.h:107
Botan::CurveGFp::get_p
const BigInt & get_p() const
Definition: curve_gfp.h:91
Botan::CurveGFp_Repr::to_curve_rep
virtual void to_curve_rep(BigInt &x, secure_vector< word > &ws) const =0
Botan::CurveGFp_Repr::get_p_words
virtual size_t get_p_words() const =0
Botan::CurveGFp_Repr::get_a_rep
virtual const BigInt & get_a_rep() const =0
Botan::CurveGFp::mul
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition: curve_gfp.h:116
Botan::CurveGFp_Repr::get_b
virtual const BigInt & get_b() const =0
Botan::CurveGFp
Definition: curve_gfp.h:53