Botan 3.13.0
Crypto and TLS for C&
Botan::Ed448Point Class Referencefinal

Representation of a point on the Ed448 curve. More...

#include <ed448_internal.h>

Public Member Functions

void ct_conditional_assign (CT::Mask< uint64_t > mask, const Ed448Point &other)
 Assign other to this if mask is set (constant time).
Ed448Point double_point () const
 Double a point (RFC 8032 5.2.4).
 Ed448Point (const Gf448Elem &x, const Gf448Elem &y)
 Create a point from its coordinates x, y.
 Ed448Point (const Gf448Elem &x, const Gf448Elem &y, const Gf448Elem &z)
 Create a point from its projective coordinates X, Y, Z.
std::array< uint8_t, ED448_LENencode () const
 Encode the point to its 57-byte representation (RFC 8032 5.2.2).
Ed448Point negate () const
 Negate the point.
Ed448Point operator+ (const Ed448Point &other) const
 Add two points (RFC 8032 5.2.4).
bool operator== (const Ed448Point &other) const
 Check if two points are equal (constant time).
Ed448Point scalar_mul (const Scalar448 &scalar) const
 Scalar multiplication.
Gf448Elem x () const
 Getter for point coordinate x.
Gf448Elem x_proj () const
 Getter for projective coordinate X.
Gf448Elem y () const
 Getter for point coordinate y.
Gf448Elem y_proj () const
 Getter for projective coordinate Y.
Gf448Elem z_proj () const
 Getter for projective coordinate Z.

Static Public Member Functions

static Ed448Point base_point ()
 Create the curve's base point ('B' in RFC 8032 5.2).
static Ed448Point base_point_mul (const Scalar448 &scalar)
 Fixed base point scalar multiplication (precomputed table, no doublings).
static Ed448Point decode (std::span< const uint8_t, ED448_LEN > enc)
 Decode a point from its 57-byte encoding (RFC 8032 5.2.3).
static Ed448Point double_scalar_mul_vartime (const Scalar448 &s1, const Ed448Point &p1, const Scalar448 &s2, const Ed448Point &p2)
 Variable-time double scalar multiplication using Shamir's trick: [s1]P + [s2]Q.
static Ed448Point identity ()
 Return the identity element.

Detailed Description

Representation of a point on the Ed448 curve.

The point is represented in projective coordinates (X, Y, Z). All operations are constant time.

Definition at line 26 of file ed448_internal.h.

Constructor & Destructor Documentation

◆ Ed448Point() [1/2]

Botan::Ed448Point::Ed448Point ( const Gf448Elem & x,
const Gf448Elem & y,
const Gf448Elem & z )
inline

Create a point from its projective coordinates X, Y, Z.

Definition at line 35 of file ed448_internal.h.

35: m_x(x), m_y(y), m_z(z) {}
Gf448Elem y() const
Getter for point coordinate y.
Gf448Elem x() const
Getter for point coordinate x.

References x(), and y().

Referenced by base_point(), base_point_mul(), ct_conditional_assign(), decode(), double_point(), double_scalar_mul_vartime(), identity(), negate(), operator+(), operator==(), and scalar_mul().

◆ Ed448Point() [2/2]

Botan::Ed448Point::Ed448Point ( const Gf448Elem & x,
const Gf448Elem & y )
inline

Create a point from its coordinates x, y.

Definition at line 38 of file ed448_internal.h.

38: m_x(x), m_y(y), m_z(1) {}

References x(), and y().

Member Function Documentation

◆ base_point()

Ed448Point Botan::Ed448Point::base_point ( )
static

Create the curve's base point ('B' in RFC 8032 5.2).

Definition at line 127 of file ed448_internal.cpp.

127 {
128 constexpr std::array<const uint64_t, WORDS_448> x = {0x2626a82bc70cc05e,
129 0x433b80e18b00938e,
130 0x12ae1af72ab66511,
131 0xea6de324a3d3a464,
132 0x9e146570470f1767,
133 0x221d15a622bf36da,
134 0x4f1970c66bed0ded};
135 constexpr std::array<const uint64_t, WORDS_448> y = {0x9808795bf230fa14,
136 0xfdbd132c4ed7c8ad,
137 0x3ad3ff1ce67c39c4,
138 0x87789c1e05a0c2d7,
139 0x4bea73736ca39840,
140 0x8876203756c9c762,
141 0x693f46716eb6bc24};
142 return Ed448Point(Gf448Elem(x), Gf448Elem(y));
143}
Ed448Point(const Gf448Elem &x, const Gf448Elem &y, const Gf448Elem &z)
Create a point from its projective coordinates X, Y, Z.

References Ed448Point(), x(), and y().

Referenced by base_point_mul(), and Botan::verify_signature().

◆ base_point_mul()

Ed448Point Botan::Ed448Point::base_point_mul ( const Scalar448 & scalar)
static

Fixed base point scalar multiplication (precomputed table, no doublings).

Definition at line 247 of file ed448_internal.cpp.

247 {
248 /*
249 Fixed base point multiplication
250
251 Same idea as base point multiply used in pcurves
252 */
253 constexpr size_t W = 4;
254 constexpr size_t WindowElements = (1 << W) - 1; // 15
255 constexpr size_t Windows = (448 + W - 1) / W; // 112
256 constexpr size_t TableSize = Windows * WindowElements; // 1680
257
258 static const auto table = []() {
259 std::vector<Ed448Point> tbl(TableSize, Ed448Point::identity());
260
261 auto accum = Ed448Point::base_point();
262
263 for(size_t i = 0; i < TableSize; i += WindowElements) {
264 tbl[i] = accum;
265
266 for(size_t j = 1; j < WindowElements; ++j) {
267 if(j % 2 == 1) {
268 tbl[i + j] = tbl[i + j / 2].double_point();
269 } else {
270 tbl[i + j] = tbl[i + j - 1] + tbl[i];
271 }
272 }
273
274 accum = tbl[i + (WindowElements / 2)].double_point();
275 }
276
277 return tbl;
278 }();
279
280 auto res = Ed448Point::identity();
281
282 for(size_t i = 0; i != Windows; ++i) {
283 const uint8_t w = static_cast<uint8_t>(scalar.get_window(i * W, W));
284
285 // Constant-time table lookup from this window's 15-entry subtable
286 auto selected = Ed448Point::identity();
287 for(size_t j = 0; j != WindowElements; ++j) {
288 const auto assign = CT::Mask<uint64_t>::is_equal(j + 1, w);
289 selected.ct_conditional_assign(assign, table[i * WindowElements + j]);
290 }
291
292 res = res + selected;
293 }
294
295 return res;
296}
static constexpr Mask< T > is_equal(T x, T y)
Definition ct_utils.h:442
static Ed448Point identity()
Return the identity element.
Ed448Point double_point() const
Double a point (RFC 8032 5.2.4).
static Ed448Point base_point()
Create the curve's base point ('B' in RFC 8032 5.2).

References base_point(), double_point(), Ed448Point(), Botan::Scalar448::get_window(), identity(), and Botan::CT::Mask< T >::is_equal().

Referenced by Botan::create_pk_from_sk(), and Botan::sign_message().

◆ ct_conditional_assign()

void Botan::Ed448Point::ct_conditional_assign ( CT::Mask< uint64_t > mask,
const Ed448Point & other )

Assign other to this if mask is set (constant time).

Definition at line 364 of file ed448_internal.cpp.

364 {
365 m_x.ct_cond_assign(mask, other.m_x);
366 m_y.ct_cond_assign(mask, other.m_y);
367 m_z.ct_cond_assign(mask, other.m_z);
368}

References Ed448Point().

◆ decode()

Ed448Point Botan::Ed448Point::decode ( std::span< const uint8_t, ED448_LEN > enc)
static

Decode a point from its 57-byte encoding (RFC 8032 5.2.3).

Definition at line 73 of file ed448_internal.cpp.

73 {
74 // RFC 8032 5.2.3 Decoding
75 // 1. First, interpret the string as an integer in little-endian
76 // representation. Bit 455 of this number is the least significant
77 // bit of the x-coordinate, and denote this value x_0. The
78 // y-coordinate is recovered simply by clearing this bit. If the
79 // resulting value is >= p, decoding fails.
80 if((enc.back() & 0x7F) != 0) { // last byte is either 0x00 or 0x80
81 throw Decoding_Error("Ed448 point has unacceptable x-distinguisher");
82 }
83 std::array<uint8_t, ED448_LEN> identity_element{};
84 identity_element[0] = 1;
85 if(CT::is_equal(enc.data(), identity_element.data(), ED448_LEN).as_bool()) {
86 throw Decoding_Error("Ed448 point is the identity element");
87 }
88 const bool x_distinguisher = enc.back() != 0;
89 const auto y_data = std::span(enc).first<56>();
91 throw Decoding_Error("Ed448 y-coordinate is not smaller than p");
92 }
93 const auto y = Gf448Elem(y_data);
94
95 // 2. To recover the x-coordinate, the curve equation implies
96 // x^2 = (y^2 - 1) / (d y^2 - 1) (mod p). The denominator is always
97 // non-zero mod p. Let u = y^2 - 1 and v = d y^2 - 1. To compute
98 // the square root of (u/v), the first step is to compute the
99 // candidate root x = (u/v)^((p+1)/4). This can be done using the
100 // following trick, to use a single modular powering for both the
101 // inversion of v and the square root:
102 // (p+1)/4 3 (p-3)/4
103 // x = (u/v) = u v (u^5 v^3) (mod p)
104 const auto u = square(Gf448Elem(y)) - Gf448Elem::one();
105 const auto v = -mul_a24(square(Gf448Elem(y))) - Gf448Elem::one();
106 const auto maybe_x = (u * square(u)) * v * root((square(square(u)) * u) * square(v) * v);
107
108 // 3. If v * x^2 = u, the recovered x-coordinate is x. Otherwise, no
109 // square root exists, and the decoding fails.
110 if(v * square(maybe_x) != u) {
111 throw Decoding_Error("Square root does not exist");
112 }
113 // 4. Finally, use the x_0 bit to select the right square root. If
114 // x = 0, and x_0 = 1, decoding fails. Otherwise, if x_0 != x mod
115 // 2, set x <-- p - x. Return the decoded point (x,y).
116 if(maybe_x.is_zero() && x_distinguisher) {
117 throw Decoding_Error("Square root of zero cannot be odd");
118 }
119 const bool maybe_x_parity = maybe_x.is_odd();
120 std::array<uint64_t, WORDS_448> x_data{};
121 CT::Mask<uint64_t>::expand_bool(maybe_x_parity == x_distinguisher)
122 .select_n(x_data.data(), maybe_x.words().data(), (-maybe_x).words().data(), WORDS_448);
123
124 return {Gf448Elem(x_data), y};
125}
static constexpr Mask< T > expand_bool(bool v)
Definition ct_utils.h:397
static bool bytes_are_canonical_representation(std::span< const uint8_t, BYTES_448 > x)
Given 56 bytes, checks that the (little endian) number from this bytes is a valid GF element,...
bool is_odd() const
Return true iff this element is odd. Constant time.
static Gf448Elem one()
Definition curve448_gf.h:64
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:798
Gf448Elem mul_a24(const Gf448Elem &a)
Multiply a field element by the Curve448 constant a24 = 39081.
Gf448Elem root(const Gf448Elem &elem)
Compute the root of elem in the field.
BigInt square(const BigInt &x)
Definition numthry.cpp:184
constexpr size_t ED448_LEN
constexpr size_t WORDS_448
Definition curve448_gf.h:23

References Botan::Gf448Elem::bytes_are_canonical_representation(), Botan::ED448_LEN, Ed448Point(), Botan::CT::Mask< T >::expand_bool(), Botan::CT::is_equal(), Botan::Gf448Elem::is_odd(), Botan::mul_a24(), Botan::Gf448Elem::one(), Botan::root(), Botan::square(), Botan::WORDS_448, and y().

Referenced by Botan::Ed448_PublicKey::check_key(), and Botan::verify_signature().

◆ double_point()

Ed448Point Botan::Ed448Point::double_point ( ) const

Double a point (RFC 8032 5.2.4).

Definition at line 179 of file ed448_internal.cpp.

179 {
180 // RFC 8032 5.2.4. - Point Addition (Double)
181 const Gf448Elem B = square(m_x + m_y);
182 const Gf448Elem C = square(m_x);
183 const Gf448Elem D = square(m_y);
184 const Gf448Elem E = C + D;
185 const Gf448Elem H = square(m_z);
186 const Gf448Elem J = E - (H + H);
187 const Gf448Elem X3 = (B - E) * J;
188 const Gf448Elem Y3 = E * (C - D);
189 const Gf448Elem Z3 = E * J;
190
191 return Ed448Point(X3, Y3, Z3);
192}

References Ed448Point(), and Botan::square().

Referenced by base_point_mul(), and double_scalar_mul_vartime().

◆ double_scalar_mul_vartime()

Ed448Point Botan::Ed448Point::double_scalar_mul_vartime ( const Scalar448 & s1,
const Ed448Point & p1,
const Scalar448 & s2,
const Ed448Point & p2 )
static

Variable-time double scalar multiplication using Shamir's trick: [s1]P + [s2]Q.

Definition at line 298 of file ed448_internal.cpp.

301 {
302 // 2-bit 2-ary Shamir's trick (variable time)
303 // Process 2 bits from each scalar per iteration, using a 16-entry table.
304 // table[w1 | (w2 << 2)] = w1*p1 + w2*p2, for w1,w2 in 0..3.
305
306 // Precompute small multiples of each point
307 const auto p1x2 = p1.double_point();
308 const auto p1x3 = p1x2 + p1;
309 const auto p2x2 = p2.double_point();
310 const auto p2x3 = p2x2 + p2;
311
312 // Build table indexed by (w2 << 2) | w1, excluding identity at index 0
313 const std::array<Ed448Point, 15> table = {
314 p1, // 1*p1 + 0*p2
315 p1x2, // 2*p1 + 0*p2
316 p1x3, // 3*p1 + 0*p2
317 p2, // 0*p1 + 1*p2
318 p1 + p2, // 1*p1 + 1*p2
319 p1x2 + p2, // 2*p1 + 1*p2
320 p1x3 + p2, // 3*p1 + 1*p2
321 p2x2, // 0*p1 + 2*p2
322 p1 + p2x2, // 1*p1 + 2*p2
323 p1x2 + p2x2, // 2*p1 + 2*p2
324 p1x3 + p2x2, // 3*p1 + 2*p2
325 p2x3, // 0*p1 + 3*p2
326 p1 + p2x3, // 1*p1 + 3*p2
327 p1x2 + p2x3, // 2*p1 + 3*p2
328 p1x3 + p2x3, // 3*p1 + 3*p2
329 };
330
331 auto res = Ed448Point::identity();
332
333 // 446 bits / 2 = 223 windows, covering bit positions 0..445
334 for(int window = 222; window >= 0; --window) {
335 res = res.double_point();
336 res = res.double_point();
337
338 const size_t bit_pos = static_cast<size_t>(window) * 2;
339 const size_t idx = s1.get_window(bit_pos, 2) | (s2.get_window(bit_pos, 2) << 2);
340
341 if(idx > 0) {
342 res = res + table[idx - 1];
343 }
344 }
345
346 return res;
347}

References double_point(), Ed448Point(), Botan::Scalar448::get_window(), and identity().

Referenced by Botan::verify_signature().

◆ encode()

std::array< uint8_t, ED448_LEN > Botan::Ed448Point::encode ( ) const

Encode the point to its 57-byte representation (RFC 8032 5.2.2).

Definition at line 145 of file ed448_internal.cpp.

145 {
146 std::array<uint8_t, ED448_LEN> res_buf = {0};
147
148 // RFC 8032 5.2.2
149 // All values are coded as octet strings, and integers are coded using
150 // little-endian convention. [...]
151 // First, encode the y-coordinate as a little-endian string of 57 octets.
152 // The final octet is always zero.
153 y().to_bytes(std::span(res_buf).first<56>());
154
155 // To form the encoding of the point, copy the least significant bit of
156 // the x-coordinate to the most significant bit of the final octet.
157 res_buf.back() = (static_cast<uint8_t>(x().is_odd()) << 7);
158
159 return res_buf;
160}
void to_bytes(std::span< uint8_t, BYTES_448 > out) const
Store the canonical representation of the GF element as 56 bytes in little-endian order.

References Botan::Gf448Elem::is_odd(), Botan::Gf448Elem::to_bytes(), x(), and y().

Referenced by Botan::create_pk_from_sk(), and Botan::sign_message().

◆ identity()

Ed448Point Botan::Ed448Point::identity ( )
inlinestatic

Return the identity element.

Definition at line 41 of file ed448_internal.h.

static Gf448Elem zero()
Definition curve448_gf.h:59

References Ed448Point(), Botan::Gf448Elem::one(), and Botan::Gf448Elem::zero().

Referenced by base_point_mul(), double_scalar_mul_vartime(), and scalar_mul().

◆ negate()

Ed448Point Botan::Ed448Point::negate ( ) const
inline

Negate the point.

Definition at line 65 of file ed448_internal.h.

65{ return Ed448Point(-m_x, m_y, m_z); }

References Ed448Point().

Referenced by Botan::verify_signature().

◆ operator+()

Ed448Point Botan::Ed448Point::operator+ ( const Ed448Point & other) const

Add two points (RFC 8032 5.2.4).

Definition at line 162 of file ed448_internal.cpp.

162 {
163 // RFC 8032 5.2.4. - Point Addition (Add)
164 const Gf448Elem A = m_z * other.m_z;
165 const Gf448Elem B = square(A);
166 const Gf448Elem C = m_x * other.m_x;
167 const Gf448Elem D = m_y * other.m_y;
168 const Gf448Elem E = -mul_a24(C * D);
169 const Gf448Elem F = B - E;
170 const Gf448Elem G = B + E;
171 const Gf448Elem H = (m_x + m_y) * (other.m_x + other.m_y);
172 const Gf448Elem X3 = A * F * (H - C - D);
173 const Gf448Elem Y3 = A * G * (D - C);
174 const Gf448Elem Z3 = F * G;
175
176 return Ed448Point(X3, Y3, Z3);
177}

References Ed448Point(), Botan::mul_a24(), and Botan::square().

◆ operator==()

bool Botan::Ed448Point::operator== ( const Ed448Point & other) const

Check if two points are equal (constant time).

Definition at line 349 of file ed448_internal.cpp.

349 {
350 // Compare in projective coordinates: (X1:Y1:Z1) == (X2:Y2:Z2)
351 // iff X1*Z2 == X2*Z1 && Y1*Z2 == Y2*Z1
352 // This avoids two field inversions that x() and y() would require.
353 const auto lhs_x = m_x * other.m_z;
354 const auto rhs_x = other.m_x * m_z;
355 const auto lhs_y = m_y * other.m_z;
356 const auto rhs_y = other.m_y * m_z;
357
358 const auto mask_x = CT::Mask<uint8_t>::expand_bool(lhs_x == rhs_x);
359 const auto mask_y = CT::Mask<uint8_t>::expand_bool(lhs_y == rhs_y);
360
361 return (mask_x & mask_y).as_bool();
362}

References Ed448Point(), and Botan::CT::Mask< T >::expand_bool().

◆ scalar_mul()

Ed448Point Botan::Ed448Point::scalar_mul ( const Scalar448 & scalar) const

Scalar multiplication.

Definition at line 194 of file ed448_internal.cpp.

194 {
195 // 4-bit windowed scalar multiplication.
196 std::array<Ed448Point, 16> table = {Ed448Point::identity(),
197 *this,
212
213 for(size_t i = 2; i < 16; ++i) {
214 if(i % 2 == 0) {
215 table[i] = table[i / 2].double_point();
216 } else {
217 table[i] = table[i - 1] + *this;
218 }
219 }
220
221 // Process 448 bits (446-bit scalar + 2 leading zero bits) in 112 4-bit windows
222 auto res = Ed448Point::identity();
223
224 for(int window = 111; window >= 0; --window) {
225 // Double 4 times
226 res = res.double_point();
227 res = res.double_point();
228 res = res.double_point();
229 res = res.double_point();
230
231 // Extract 4-bit window value. Bits at position >= 446 are zero.
232 const uint64_t w = s.get_window(static_cast<size_t>(window) * 4, 4);
233
234 // Constant-time table lookup
235 auto selected = Ed448Point::identity();
236 for(size_t i = 0; i < 16; ++i) {
237 const auto correct_idx = CT::Mask<uint64_t>::is_equal(static_cast<uint64_t>(i), w);
238 selected.ct_conditional_assign(correct_idx, table[i]);
239 }
240
241 res = res + selected;
242 }
243
244 return res;
245}

References Ed448Point(), Botan::Scalar448::get_window(), identity(), and Botan::CT::Mask< T >::is_equal().

Referenced by Botan::operator*().

◆ x()

Gf448Elem Botan::Ed448Point::x ( ) const
inline

Getter for point coordinate x.

Definition at line 77 of file ed448_internal.h.

77{ return m_x / m_z; }

Referenced by base_point(), Ed448Point(), Ed448Point(), and encode().

◆ x_proj()

Gf448Elem Botan::Ed448Point::x_proj ( ) const
inline

Getter for projective coordinate X.

Definition at line 68 of file ed448_internal.h.

68{ return m_x; }

◆ y()

Gf448Elem Botan::Ed448Point::y ( ) const
inline

Getter for point coordinate y.

Definition at line 80 of file ed448_internal.h.

80{ return m_y / m_z; }

Referenced by base_point(), decode(), Ed448Point(), Ed448Point(), and encode().

◆ y_proj()

Gf448Elem Botan::Ed448Point::y_proj ( ) const
inline

Getter for projective coordinate Y.

Definition at line 71 of file ed448_internal.h.

71{ return m_y; }

◆ z_proj()

Gf448Elem Botan::Ed448Point::z_proj ( ) const
inline

Getter for projective coordinate Z.

Definition at line 74 of file ed448_internal.h.

74{ return m_z; }

The documentation for this class was generated from the following files: