Botan 3.6.1
Crypto and TLS for C&
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// Currently exposed in EC_Point
17//BOTAN_FUTURE_INTERNAL_HEADER(curve_gfp.h)
18
19namespace Botan {
20
22 public:
23 virtual ~CurveGFp_Repr() = default;
24
25 friend class CurveGFp;
26
27 protected:
28 virtual const BigInt& get_p() const = 0;
29 virtual const BigInt& get_a() const = 0;
30 virtual const BigInt& get_b() const = 0;
31
32 size_t get_p_words() const {
33 const size_t W_bits = sizeof(word) * 8;
34 return (get_p_bits() + W_bits - 1) / W_bits;
35 }
36
37 virtual size_t get_p_bits() const = 0;
38
39 virtual size_t get_ws_size() const = 0;
40
41 virtual bool is_one(const BigInt& x) const = 0;
42
43 virtual bool a_is_zero() const = 0;
44
45 virtual bool a_is_minus_3() const = 0;
46
47 /*
48 * Returns to_curve_rep(get_a())
49 */
50 virtual const BigInt& get_a_rep() const = 0;
51
52 /*
53 * Returns to_curve_rep(get_b())
54 */
55 virtual const BigInt& get_b_rep() const = 0;
56
57 /*
58 * Returns to_curve_rep(1)
59 */
60 virtual const BigInt& get_1_rep() const = 0;
61
62 virtual BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const = 0;
63
64 virtual void to_curve_rep(BigInt& x, secure_vector<word>& ws) const = 0;
65
66 virtual void from_curve_rep(BigInt& x, secure_vector<word>& ws) const = 0;
67
68 void curve_mul(BigInt& z, const BigInt& x, const BigInt& y, secure_vector<word>& ws) const {
69 BOTAN_DEBUG_ASSERT(x.sig_words() <= get_p_words());
70 curve_mul_words(z, x._data(), x.size(), y, ws);
71 }
72
73 virtual void curve_mul_words(
74 BigInt& z, const word x_words[], size_t x_size, const BigInt& y, secure_vector<word>& ws) const = 0;
75
76 void curve_sqr(BigInt& z, const BigInt& x, secure_vector<word>& ws) const {
77 BOTAN_DEBUG_ASSERT(x.sig_words() <= get_p_words());
78 curve_sqr_words(z, x._data(), x.size(), ws);
79 }
80
81 virtual void curve_sqr_words(BigInt& z, const word x_words[], size_t x_size, secure_vector<word>& ws) const = 0;
82};
83
84/**
85* This class represents an elliptic curve over GF(p)
86*
87* There should not be any reason for applications to use this type.
88* If you need EC primitives use the interfaces EC_Group and EC_Point
89*
90* It is likely this class will be removed entirely in a future major
91* release.
92*/
94 public:
95 /**
96 * @return curve coefficient a
97 */
98 const BigInt& get_a() const { return m_repr->get_a(); }
99
100 /**
101 * @return curve coefficient b
102 */
103 const BigInt& get_b() const { return m_repr->get_b(); }
104
105 /**
106 * Get prime modulus of the field of the curve
107 * @return prime modulus of the field of the curve
108 */
109 const BigInt& get_p() const { return m_repr->get_p(); }
110
111 private:
112 friend class EC_Point;
113 friend class EC_Group;
114 friend class EC_Group_Data;
118
119 /**
120 * Create an uninitialized CurveGFp
121 */
122 CurveGFp() = default;
123
124 /**
125 * Construct the elliptic curve E: y^2 = x^3 + ax + b over GF(p)
126 * @param p prime number of the field
127 * @param a first coefficient
128 * @param b second coefficient
129 */
130 CurveGFp(const BigInt& p, const BigInt& a, const BigInt& b) : m_repr(choose_repr(p, a, b)) {}
131
132 CurveGFp(const CurveGFp&) = default;
133
134 CurveGFp& operator=(const CurveGFp&) = default;
135
136 size_t get_p_words() const { return m_repr->get_p_words(); }
137
138 size_t get_p_bits() const { return m_repr->get_p_bits(); }
139
140 size_t get_p_bytes() const { return (get_p_bits() + 7) / 8; }
141
142 size_t get_ws_size() const { return m_repr->get_ws_size(); }
143
144 const BigInt& get_a_rep() const { return m_repr->get_a_rep(); }
145
146 const BigInt& get_b_rep() const { return m_repr->get_b_rep(); }
147
148 const BigInt& get_1_rep() const { return m_repr->get_1_rep(); }
149
150 bool a_is_minus_3() const { return m_repr->a_is_minus_3(); }
151
152 bool a_is_zero() const { return m_repr->a_is_zero(); }
153
154 bool is_one(const BigInt& x) const { return m_repr->is_one(x); }
155
156 BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const { return m_repr->invert_element(x, ws); }
157
158 void to_rep(BigInt& x, secure_vector<word>& ws) const { m_repr->to_curve_rep(x, ws); }
159
160 void from_rep(BigInt& x, secure_vector<word>& ws) const { m_repr->from_curve_rep(x, ws); }
161
162 BigInt from_rep_to_tmp(const BigInt& x, secure_vector<word>& ws) const {
163 BigInt xt(x);
164 m_repr->from_curve_rep(xt, ws);
165 return xt;
166 }
167
168 // TODO: from_rep taking && ref
169
170 void mul(BigInt& z, const BigInt& x, const BigInt& y, secure_vector<word>& ws) const {
171 m_repr->curve_mul(z, x, y, ws);
172 }
173
174 void mul(BigInt& z, const word x_w[], size_t x_size, const BigInt& y, secure_vector<word>& ws) const {
175 m_repr->curve_mul_words(z, x_w, x_size, y, ws);
176 }
177
178 void sqr(BigInt& z, const BigInt& x, secure_vector<word>& ws) const { m_repr->curve_sqr(z, x, ws); }
179
180 void sqr(BigInt& z, const word x_w[], size_t x_size, secure_vector<word>& ws) const {
181 m_repr->curve_sqr_words(z, x_w, x_size, ws);
182 }
183
184 BigInt mul(const BigInt& x, const BigInt& y, secure_vector<word>& ws) const { return mul_to_tmp(x, y, ws); }
185
186 BigInt sqr(const BigInt& x, secure_vector<word>& ws) const { return sqr_to_tmp(x, ws); }
187
188 BigInt mul_to_tmp(const BigInt& x, const BigInt& y, secure_vector<word>& ws) const {
189 BigInt z;
190 m_repr->curve_mul(z, x, y, ws);
191 return z;
192 }
193
194 BigInt sqr_to_tmp(const BigInt& x, secure_vector<word>& ws) const {
195 BigInt z;
196 m_repr->curve_sqr(z, x, ws);
197 return z;
198 }
199
200 void swap(CurveGFp& other) { std::swap(m_repr, other.m_repr); }
201
202 friend void swap(CurveGFp& x, CurveGFp& y) { x.swap(y); }
203
204 /**
205 * Equality operator
206 * @param other a curve
207 * @return true iff *this is the same as other
208 */
209 inline bool operator==(const CurveGFp& other) const {
210 if(m_repr.get() == other.m_repr.get()) {
211 return true;
212 }
213
214 return (get_p() == other.get_p()) && (get_a() == other.get_a()) && (get_b() == other.get_b());
215 }
216
217 inline bool operator!=(const CurveGFp& other) const = default;
218
219 private:
220 static std::shared_ptr<CurveGFp_Repr> choose_repr(const BigInt& p, const BigInt& a, const BigInt& b);
221
222 std::shared_ptr<CurveGFp_Repr> m_repr;
223};
224
225} // namespace Botan
226
227#endif
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:98
size_t sig_words() const
Definition bigint.h:616
size_t size() const
Definition bigint.h:610
const word * _data() const
Definition bigint.h:936
void curve_mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition curve_gfp.h:68
virtual BigInt invert_element(const BigInt &x, secure_vector< word > &ws) const =0
virtual ~CurveGFp_Repr()=default
virtual const BigInt & get_a() const =0
virtual void to_curve_rep(BigInt &x, secure_vector< word > &ws) const =0
virtual const BigInt & get_b() const =0
virtual bool is_one(const BigInt &x) const =0
virtual void curve_sqr_words(BigInt &z, const word x_words[], size_t x_size, secure_vector< word > &ws) const =0
virtual size_t get_p_bits() const =0
virtual const BigInt & get_a_rep() const =0
size_t get_p_words() const
Definition curve_gfp.h:32
virtual void curve_mul_words(BigInt &z, const word x_words[], size_t x_size, const BigInt &y, secure_vector< word > &ws) const =0
void curve_sqr(BigInt &z, const BigInt &x, secure_vector< word > &ws) const
Definition curve_gfp.h:76
virtual const BigInt & get_b_rep() const =0
virtual void from_curve_rep(BigInt &x, secure_vector< word > &ws) const =0
virtual const BigInt & get_p() const =0
virtual size_t get_ws_size() const =0
virtual bool a_is_minus_3() const =0
virtual const BigInt & get_1_rep() const =0
virtual bool a_is_zero() const =0
friend void swap(CurveGFp &x, CurveGFp &y)
Definition curve_gfp.h:202
const BigInt & get_a() const
Definition curve_gfp.h:98
const BigInt & get_p() const
Definition curve_gfp.h:109
const BigInt & get_b() const
Definition curve_gfp.h:103
int(* final)(unsigned char *, CTX *)
#define BOTAN_UNSTABLE_API
Definition compiler.h:44
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:69
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:54
const SIMD_8x32 & b
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61