Botan 3.14.1
Crypto and TLS for C&
Botan::Ed25519_FieldElement Class Referencefinal

#include <ed25519_fe.h>

Public Member Functions

constexpr void conditional_assign (CT::Choice cond, const Ed25519_FieldElement &other)
constexpr Ed25519_FieldElement ()
constexpr Ed25519_FieldElement invert () const
constexpr bool is_negative () const
constexpr bool is_zero () const
constexpr Ed25519_FieldElement pow_22523 () const
void serialize_to (std::span< uint8_t, 32 > b) const
constexpr Ed25519_FieldElement sqr () const
constexpr Ed25519_FieldElement sqr2 () const
constexpr Ed25519_FieldElement sqr_iter (size_t iter) const

Static Public Member Functions

static constexpr Ed25519_FieldElement add (const Ed25519_FieldElement &a, const Ed25519_FieldElement &b)
static constexpr void conditional_swap (CT::Choice cond, Ed25519_FieldElement &x, Ed25519_FieldElement &y)
static std::optional< Ed25519_FieldElement > deserialize (const uint8_t b[32])
static constexpr Ed25519_FieldElement from_word (word x)
static constexpr Ed25519_FieldElement mul (const Ed25519_FieldElement &a, const Ed25519_FieldElement &b)
static constexpr Ed25519_FieldElement negate (const Ed25519_FieldElement &a)
static constexpr Ed25519_FieldElement one ()
static constexpr Ed25519_FieldElement sub (const Ed25519_FieldElement &a, const Ed25519_FieldElement &b)
static constexpr Ed25519_FieldElement zero ()

Detailed Description

An element of the field Z/(2^255-19)

The value is stored as saturated words. Aside from a value being reduced below 2^256 (rather than below p), there are no per-word bounds to track: additions and subtractions fold their carry or borrow back in immediately using 2^256 == 38 (mod p), and multiplications reduce below 2^255 using the same identity. Only serialization produces the canonical value.

Definition at line 42 of file ed25519_fe.h.

Constructor & Destructor Documentation

◆ Ed25519_FieldElement()

Botan::Ed25519_FieldElement::Ed25519_FieldElement ( )
inlineconstexpr

Default zero initialization

Definition at line 144 of file ed25519_fe.h.

144: m_val{} {}

Referenced by add(), conditional_assign(), conditional_swap(), deserialize(), from_word(), invert(), mul(), negate(), one(), pow_22523(), sqr(), sqr2(), sqr_iter(), sub(), and zero().

Member Function Documentation

◆ add()

constexpr Ed25519_FieldElement Botan::Ed25519_FieldElement::add ( const Ed25519_FieldElement & a,
const Ed25519_FieldElement & b )
inlinestaticconstexpr

Definition at line 212 of file ed25519_fe.h.

212 {
214 W carry = 0;
215 for(size_t i = 0; i != N; ++i) {
216 z.m_val[i] = word_add(a.m_val[i], b.m_val[i], &carry);
217 }
218 z.fold_carry(carry);
219 return z;
220 }
constexpr auto word_add(W x, W y, W *carry) -> W
Definition mp_asmi.h:260

References Ed25519_FieldElement(), and Botan::word_add().

Referenced by Botan::operator+(), and sqr2().

◆ conditional_assign()

void Botan::Ed25519_FieldElement::conditional_assign ( CT::Choice cond,
const Ed25519_FieldElement & other )
inlineconstexpr

If cond is set, assign other to this

Definition at line 281 of file ed25519_fe.h.

281 {
282 const W mask = cond.into_bitmask<W>();
283 for(size_t i = 0; i != N; ++i) {
284 m_val[i] = choose(mask, other.m_val[i], m_val[i]);
285 }
286 }
BOTAN_FORCE_INLINE constexpr T choose(T mask, T a, T b)
Definition bit_ops.h:217

References Botan::choose(), Ed25519_FieldElement(), and Botan::CT::Choice::into_bitmask().

◆ conditional_swap()

constexpr void Botan::Ed25519_FieldElement::conditional_swap ( CT::Choice cond,
Ed25519_FieldElement & x,
Ed25519_FieldElement & y )
inlinestaticconstexpr

If cond is set, swap x and y

Definition at line 291 of file ed25519_fe.h.

291 {
292 const W mask = cond.into_bitmask<W>();
293 for(size_t i = 0; i != N; ++i) {
294 const W nx = choose(mask, y.m_val[i], x.m_val[i]);
295 const W ny = choose(mask, x.m_val[i], y.m_val[i]);
296 x.m_val[i] = nx;
297 y.m_val[i] = ny;
298 }
299 }

References Botan::choose(), Ed25519_FieldElement(), and Botan::CT::Choice::into_bitmask().

◆ deserialize()

std::optional< Ed25519_FieldElement > Botan::Ed25519_FieldElement::deserialize ( const uint8_t b[32])
inlinestatic

Deserialize 32 little endian bytes, ignoring the high bit

RFC 8032 5.1.3 requires that the encoded value (with the sign bit cleared) is already less than p; a value in [p, 2^255) is rejected rather than reduced.

Definition at line 167 of file ed25519_fe.h.

167 {
169 for(size_t i = 0; i != N; ++i) {
170 r.m_val[i] = load_le<W>(b, i);
171 }
172 r.m_val[N - 1] &= TOP_MASK;
173
174 // Reject unless the value is already reduced, ie subtracting p borrows
175 W borrow = 0;
176 for(size_t i = 0; i != N; ++i) {
177 (void)word_sub(r.m_val[i], P[i], &borrow);
178 }
179 if(borrow == 0) {
180 return std::nullopt;
181 }
182
183 return r;
184 }
constexpr auto word_sub(W x, W y, W *carry) -> W
Definition mp_asmi.h:355
constexpr auto load_le(ParamTs &&... params)
Definition loadstor.h:495

References Ed25519_FieldElement(), Botan::load_le(), and Botan::word_sub().

◆ from_word()

constexpr Ed25519_FieldElement Botan::Ed25519_FieldElement::from_word ( word x)
inlinestaticconstexpr

Definition at line 154 of file ed25519_fe.h.

154 {
156 r.m_val[0] = x;
157 return r;
158 }

References Ed25519_FieldElement().

◆ invert()

Ed25519_FieldElement Botan::Ed25519_FieldElement::invert ( ) const
constexpr

Definition at line 321 of file ed25519_fe.h.

321 {
322 auto t0 = this->sqr();
323 auto t1 = t0.sqr_iter(2);
324 t1 = *this * t1;
325 t0 = t0 * t1;
326 auto t2 = t0.sqr();
327 t1 = t1 * t2;
328 t2 = t1.sqr_iter(5);
329 t1 = t2 * t1;
330 t2 = t1.sqr_iter(10);
331 t2 = t2 * t1;
332 auto t3 = t2.sqr_iter(20);
333 t2 = t3 * t2;
334 t2 = t2.sqr_iter(10);
335 t1 = t2 * t1;
336 t2 = t1.sqr_iter(50);
337 t2 = t2 * t1;
338 t3 = t2.sqr_iter(100);
339 t2 = t3 * t2;
340 t2 = t2.sqr_iter(50);
341 t1 = t2 * t1;
342 t1 = t1.sqr_iter(5);
343
344 t0 = t1 * t0;
345 return t0;
346}
constexpr Ed25519_FieldElement sqr() const
Definition ed25519_fe.h:250

References Ed25519_FieldElement(), and sqr().

◆ is_negative()

bool Botan::Ed25519_FieldElement::is_negative ( ) const
inlineconstexpr

Definition at line 210 of file ed25519_fe.h.

210{ return (canonical()[0] & 1) == 1; }

◆ is_zero()

bool Botan::Ed25519_FieldElement::is_zero ( ) const
inlineconstexpr

Definition at line 193 of file ed25519_fe.h.

193 {
194 const auto w = canonical();
195 if(std::is_constant_evaluated()) {
196 for(const auto x : w) {
197 if(x != 0) {
198 return false;
199 }
200 }
201 return true;
202 }
203 return CT::all_zeros(w.data(), w.size()).as_bool();
204 }
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:790

References Botan::CT::all_zeros().

◆ mul()

constexpr Ed25519_FieldElement Botan::Ed25519_FieldElement::mul ( const Ed25519_FieldElement & a,
const Ed25519_FieldElement & b )
inlinestaticconstexpr

Definition at line 242 of file ed25519_fe.h.

242 {
243 std::array<W, 2 * N> z; // NOLINT(*-member-init)
244 comba_mul<N>(z.data(), a.m_val.data(), b.m_val.data());
246 r.reduce_wide(z);
247 return r;
248 }
constexpr void comba_mul(W z[2 *N], const W x[N], const W y[N])
Definition mp_core.h:981

References Botan::comba_mul(), and Ed25519_FieldElement().

Referenced by Botan::operator*().

◆ negate()

constexpr Ed25519_FieldElement Botan::Ed25519_FieldElement::negate ( const Ed25519_FieldElement & a)
inlinestaticconstexpr

Definition at line 232 of file ed25519_fe.h.

232 {
233 // Hide the zero, since subtracting from a constant zero GCC 14 at -O1
234 // emits a branch on the borrow
236 for(size_t i = 0; i != N; ++i) {
237 z.m_val[i] = CT::value_barrier<W>(0);
238 }
239 return sub(z, a);
240 }
static constexpr Ed25519_FieldElement sub(const Ed25519_FieldElement &a, const Ed25519_FieldElement &b)
Definition ed25519_fe.h:222
constexpr T value_barrier(T x)

References Ed25519_FieldElement(), sub(), and Botan::CT::value_barrier().

Referenced by Botan::operator-().

◆ one()

constexpr Ed25519_FieldElement Botan::Ed25519_FieldElement::one ( )
inlinestaticconstexpr

Definition at line 148 of file ed25519_fe.h.

148 {
150 o.m_val[0] = 1;
151 return o;
152 }

References Ed25519_FieldElement().

◆ pow_22523()

Ed25519_FieldElement Botan::Ed25519_FieldElement::pow_22523 ( ) const
constexpr

Definition at line 348 of file ed25519_fe.h.

348 {
349 auto t0 = this->sqr();
350 auto t1 = t0.sqr_iter(2);
351 t1 = (*this) * t1;
352 t0 = t0 * t1;
353 t0 = t0.sqr();
354 t0 = t1 * t0;
355 t1 = t0.sqr_iter(5);
356 t0 = t1 * t0;
357 t1 = t0.sqr_iter(10);
358 t1 = t1 * t0;
359 auto t2 = t1.sqr_iter(20);
360 t1 = t2 * t1;
361 t1 = t1.sqr_iter(10);
362 t0 = t1 * t0;
363 t1 = t0.sqr_iter(50);
364 t1 = t1 * t0;
365 t2 = t1.sqr_iter(100);
366 t1 = t2 * t1;
367 t1 = t1.sqr_iter(50);
368 t0 = t1 * t0;
369 t0 = t0.sqr_iter(2);
370
371 t0 = t0 * (*this);
372 return t0;
373}

References Ed25519_FieldElement(), and sqr().

◆ serialize_to()

void Botan::Ed25519_FieldElement::serialize_to ( std::span< uint8_t, 32 > b) const
inline

Definition at line 186 of file ed25519_fe.h.

186 {
187 const auto w = canonical();
188 for(size_t i = 0; i != N; ++i) {
189 store_le(w[i], b.data() + sizeof(W) * i);
190 }
191 }
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736

References Botan::store_le().

◆ sqr()

Ed25519_FieldElement Botan::Ed25519_FieldElement::sqr ( ) const
inlineconstexpr

Definition at line 250 of file ed25519_fe.h.

250 {
251 std::array<W, 2 * N> z; // NOLINT(*-member-init)
252 comba_sqr<N>(z.data(), m_val.data());
254 r.reduce_wide(z);
255 return r;
256 }
constexpr void comba_sqr(W z[2 *N], const W x[N])
Definition mp_core.h:1017

References Botan::comba_sqr(), and Ed25519_FieldElement().

Referenced by invert(), pow_22523(), and sqr2().

◆ sqr2()

Ed25519_FieldElement Botan::Ed25519_FieldElement::sqr2 ( ) const
inlineconstexpr

Definition at line 269 of file ed25519_fe.h.

269 {
270 const auto z = sqr();
271 return add(z, z);
272 }
static constexpr Ed25519_FieldElement add(const Ed25519_FieldElement &a, const Ed25519_FieldElement &b)
Definition ed25519_fe.h:212

References add(), Ed25519_FieldElement(), and sqr().

◆ sqr_iter()

Ed25519_FieldElement Botan::Ed25519_FieldElement::sqr_iter ( size_t iter) const
inlineconstexpr

Definition at line 258 of file ed25519_fe.h.

258 {
259 auto r = *this;
260 std::array<W, 2 * N> z; // NOLINT(*-member-init)
261 for(size_t i = 0; i != iter; ++i) {
262 comba_sqr<N>(z.data(), r.m_val.data());
263 r.reduce_wide(z);
264 }
265 return r;
266 }

References Botan::comba_sqr(), and Ed25519_FieldElement().

◆ sub()

constexpr Ed25519_FieldElement Botan::Ed25519_FieldElement::sub ( const Ed25519_FieldElement & a,
const Ed25519_FieldElement & b )
inlinestaticconstexpr

Definition at line 222 of file ed25519_fe.h.

222 {
224 W borrow = 0;
225 for(size_t i = 0; i != N; ++i) {
226 z.m_val[i] = word_sub(a.m_val[i], b.m_val[i], &borrow);
227 }
228 z.fold_borrow(borrow);
229 return z;
230 }

References Ed25519_FieldElement(), and Botan::word_sub().

Referenced by negate(), and Botan::operator-().

◆ zero()

constexpr Ed25519_FieldElement Botan::Ed25519_FieldElement::zero ( )
inlinestaticconstexpr

Definition at line 146 of file ed25519_fe.h.

146{ return Ed25519_FieldElement(); }

References Ed25519_FieldElement().


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