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

#include <ed25519_scalar.h>

Public Member Functions

Ed25519_Scalar operator* (const Ed25519_Scalar &other) const
Ed25519_Scalar operator+ (const Ed25519_Scalar &other) const
Ed25519_Scalar operator- () const
void serialize_to (std::span< uint8_t, BYTES > b) const
std::array< uint8_t, BYTES > to_bytes () const

Static Public Member Functions

static Ed25519_Scalar from_bytes (std::span< const uint8_t, BYTES > b)
static std::optional< Ed25519_Scalar > from_canonical_bytes (std::span< const uint8_t, BYTES > b)
static Ed25519_Scalar from_wide_bytes (std::span< const uint8_t, 2 *BYTES > b)
static Ed25519_Scalar one ()
static Ed25519_Scalar zero ()

Static Public Attributes

static constexpr size_t BYTES = 32

Detailed Description

An integer modulo the order of the Ed25519 group, l = 2^252 + 27742317777372353535851937790883648493

Values are always fully reduced, and all operations aside from from_canonical_bytes are constant time.

Definition at line 26 of file ed25519_scalar.h.

Member Function Documentation

◆ from_bytes()

Ed25519_Scalar Botan::Ed25519_Scalar::from_bytes ( std::span< const uint8_t, BYTES > b)
static

Deserialize 32 little endian bytes, reducing modulo l

Definition at line 122 of file ed25519_scalar.cpp.

122 {
123 // x mod l == mont_mul(x, R)
124 return Ed25519_Scalar(sc_mont_mul(sc_load(b), SC_R1));
125}

Referenced by Botan::x25519_basepoint().

◆ from_canonical_bytes()

std::optional< Ed25519_Scalar > Botan::Ed25519_Scalar::from_canonical_bytes ( std::span< const uint8_t, BYTES > b)
static

Deserialize 32 little endian bytes, returning nullopt unless the value is already reduced

Definition at line 135 of file ed25519_scalar.cpp.

135 {
136 const auto w = sc_load(b);
137
138 std::array<word, WORDS> t; // NOLINT(*-member-init)
139 word borrow = 0;
140 for(size_t i = 0; i != WORDS; ++i) {
141 t[i] = word_sub(w[i], SC_L[i], &borrow);
142 }
143
144 // If subtracting l did not borrow then the value was not reduced
145 if(borrow == 0) {
146 return std::nullopt;
147 }
148 return Ed25519_Scalar(w);
149}
constexpr auto word_sub(W x, W y, W *carry) -> W
Definition mp_asmi.h:355
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131

References Botan::word_sub().

◆ from_wide_bytes()

Ed25519_Scalar Botan::Ed25519_Scalar::from_wide_bytes ( std::span< const uint8_t, 2 *BYTES > b)
static

Deserialize 64 little endian bytes (eg a wide hash output), reducing modulo l

Definition at line 127 of file ed25519_scalar.cpp.

127 {
128 const auto lo = sc_load(b.first<BYTES>());
129 const auto hi = sc_load(b.last<BYTES>());
130
131 // (hi * 2^256 + lo) == mont_mul(hi, R^2) + mont_mul(lo, R) (mod l)
132 return Ed25519_Scalar(sc_add(sc_mont_mul(hi, SC_R2), sc_mont_mul(lo, SC_R1)));
133}
static constexpr size_t BYTES

References BYTES.

◆ one()

Ed25519_Scalar Botan::Ed25519_Scalar::one ( )
inlinestatic

Definition at line 35 of file ed25519_scalar.h.

35{ return Ed25519_Scalar(std::array<word, WORDS>{1}); }

Referenced by Botan::ed25519_valid_public_key_point().

◆ operator*()

Ed25519_Scalar Botan::Ed25519_Scalar::operator* ( const Ed25519_Scalar & other) const

Definition at line 175 of file ed25519_scalar.cpp.

175 {
176 // (a*b) == mont_mul(mont_mul(a, R^2), b) (mod l)
177 return Ed25519_Scalar(sc_mont_mul(sc_mont_mul(m_val, SC_R2), other.m_val));
178}

◆ operator+()

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

Definition at line 157 of file ed25519_scalar.cpp.

157 {
158 return Ed25519_Scalar(sc_add(m_val, other.m_val));
159}

◆ operator-()

Ed25519_Scalar Botan::Ed25519_Scalar::operator- ( ) const

Definition at line 161 of file ed25519_scalar.cpp.

161 {
162 // Compute l - x, then use zero instead if x was zero
163 std::array<word, WORDS> t; // NOLINT(*-member-init)
164 word borrow = 0;
165 for(size_t i = 0; i != WORDS; ++i) {
166 t[i] = word_sub(SC_L[i], m_val[i], &borrow);
167 }
168
169 const std::array<word, WORDS> zeros{};
170 const auto x_is_zero = CT::all_zeros(m_val.data(), WORDS).value();
171 CT::conditional_assign_mem(x_is_zero, t.data(), zeros.data(), WORDS);
172 return Ed25519_Scalar(t);
173}
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:790
constexpr Mask< T > conditional_assign_mem(T cnd, T *dest, const T *src, size_t elems)
Definition ct_utils.h:754

References Botan::CT::all_zeros(), Botan::CT::conditional_assign_mem(), and Botan::word_sub().

◆ serialize_to()

void Botan::Ed25519_Scalar::serialize_to ( std::span< uint8_t, BYTES > b) const

Serialize to 32 little endian bytes

Definition at line 151 of file ed25519_scalar.cpp.

151 {
152 for(size_t i = 0; i != WORDS; ++i) {
153 store_le(m_val[i], b.data() + sizeof(word) * i);
154 }
155}
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736

References Botan::store_le().

Referenced by to_bytes().

◆ to_bytes()

std::array< uint8_t, BYTES > Botan::Ed25519_Scalar::to_bytes ( ) const
inline

Definition at line 59 of file ed25519_scalar.h.

59 {
60 std::array<uint8_t, BYTES> b; // NOLINT(*-member-init)
61 serialize_to(b);
62 return b;
63 }
void serialize_to(std::span< uint8_t, BYTES > b) const

References serialize_to().

◆ zero()

Ed25519_Scalar Botan::Ed25519_Scalar::zero ( )
inlinestatic

Definition at line 33 of file ed25519_scalar.h.

33{ return Ed25519_Scalar(std::array<word, WORDS>{}); }

Referenced by Botan::ed25519_valid_public_key_point().

Member Data Documentation

◆ BYTES

size_t Botan::Ed25519_Scalar::BYTES = 32
staticconstexpr

Definition at line 31 of file ed25519_scalar.h.

Referenced by from_wide_bytes().


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