Botan 3.6.1
Crypto and TLS for C&
Botan::EC_Scalar Class Referencefinal

#include <ec_scalar.h>

Public Member Functions

const EC_Scalar_Data_inner () const
 
EC_Scalar add (const EC_Scalar &x) const
 
void assign (const EC_Scalar &x)
 
size_t bytes () const
 
 EC_Scalar (const EC_Group &group, std::span< const uint8_t > bytes)
 
 EC_Scalar (const EC_Scalar &other)
 
 EC_Scalar (EC_Scalar &&other) noexcept
 
EC_Scalar invert () const
 
bool is_eq (const EC_Scalar &x) const
 
bool is_nonzero () const
 
bool is_zero () const
 
EC_Scalar mul (const EC_Scalar &x) const
 
EC_Scalar negate () const
 
EC_Scalaroperator= (const EC_Scalar &other)
 
EC_Scalaroperator= (EC_Scalar &&other) noexcept
 
template<concepts::resizable_byte_buffer T = std::vector<uint8_t>>
T serialize () const
 
void serialize_to (std::span< uint8_t > bytes) const
 
void square_self ()
 
EC_Scalar sub (const EC_Scalar &x) const
 
BigInt to_bigint () const
 
 ~EC_Scalar ()
 

Static Public Member Functions

static EC_Scalar _from_inner (std::unique_ptr< EC_Scalar_Data > inner)
 
static std::optional< EC_Scalardeserialize (const EC_Group &group, std::span< const uint8_t > bytes)
 
static std::optional< std::pair< EC_Scalar, EC_Scalar > > deserialize_pair (const EC_Group &group, std::span< const uint8_t > bytes)
 
static EC_Scalar from_bigint (const EC_Group &group, const BigInt &bn)
 
static EC_Scalar from_bytes_mod_order (const EC_Group &group, std::span< const uint8_t > bytes)
 
static EC_Scalar from_bytes_with_trunc (const EC_Group &group, std::span< const uint8_t > bytes)
 
static EC_Scalar gk_x_mod_order (const EC_Scalar &scalar, RandomNumberGenerator &rng, std::vector< BigInt > &ws)
 
static EC_Scalar one (const EC_Group &group)
 
static EC_Scalar random (const EC_Group &group, RandomNumberGenerator &rng)
 
template<concepts::resizable_byte_buffer T = std::vector<uint8_t>>
static T serialize_pair (const EC_Scalar &r, const EC_Scalar &s)
 
static void serialize_pair_to (std::span< uint8_t > bytes, const EC_Scalar &r, const EC_Scalar &s)
 

Friends

class EC_AffinePoint
 
EC_Scalar operator* (const EC_Scalar &x, const EC_Scalar &y)
 
EC_Scalar operator+ (const EC_Scalar &x, const EC_Scalar &y)
 
EC_Scalar operator- (const EC_Scalar &x, const EC_Scalar &y)
 
bool operator== (const EC_Scalar &x, const EC_Scalar &y)
 

Detailed Description

Represents an integer modulo the prime group order of an elliptic curve

Definition at line 28 of file ec_scalar.h.

Constructor & Destructor Documentation

◆ EC_Scalar() [1/3]

Botan::EC_Scalar::EC_Scalar ( const EC_Group & group,
std::span< const uint8_t > bytes )

Convert a bytestring to an EC_Scalar

This is similar to deserialize but instead of returning nullopt if the input is invalid, it will throw an exception.

Definition at line 122 of file ec_scalar.cpp.

122 {
123 m_scalar = group._data()->scalar_deserialize(bytes);
124 if(!m_scalar) {
125 throw Decoding_Error("EC_Scalar::from_bytes is not a valid scalar value");
126 }
127}
size_t bytes() const
Definition ec_scalar.cpp:41

References Botan::EC_Group::_data(), and bytes().

Referenced by _from_inner(), add(), deserialize(), from_bigint(), from_bytes_mod_order(), from_bytes_with_trunc(), gk_x_mod_order(), invert(), mul(), negate(), one(), random(), and sub().

◆ EC_Scalar() [2/3]

Botan::EC_Scalar::EC_Scalar ( const EC_Scalar & other)

Definition at line 22 of file ec_scalar.cpp.

22: m_scalar(other.inner().clone()) {}

◆ EC_Scalar() [3/3]

Botan::EC_Scalar::EC_Scalar ( EC_Scalar && other)
noexcept

Definition at line 24 of file ec_scalar.cpp.

24: m_scalar(std::move(other.m_scalar)) {}

◆ ~EC_Scalar()

Botan::EC_Scalar::~EC_Scalar ( )
default

Member Function Documentation

◆ _from_inner()

EC_Scalar Botan::EC_Scalar::_from_inner ( std::unique_ptr< EC_Scalar_Data > inner)
static

Definition at line 14 of file ec_scalar.cpp.

14 {
15 return EC_Scalar(std::move(inner));
16}
EC_Scalar(const EC_Group &group, std::span< const uint8_t > bytes)

References EC_Scalar().

◆ _inner()

◆ add()

EC_Scalar Botan::EC_Scalar::add ( const EC_Scalar & x) const

Scalar addition (modulo p)

Definition at line 145 of file ec_scalar.cpp.

145 {
146 return EC_Scalar(inner().add(x.inner()));
147}
EC_Scalar add(const EC_Scalar &x) const

References add(), and EC_Scalar().

Referenced by add().

◆ assign()

void Botan::EC_Scalar::assign ( const EC_Scalar & x)

Assign a scalar

Definition at line 157 of file ec_scalar.cpp.

157 {
158 m_scalar->assign(x.inner());
159}

Referenced by operator=().

◆ bytes()

size_t Botan::EC_Scalar::bytes ( ) const

Return the byte size of this scalar

Definition at line 41 of file ec_scalar.cpp.

41 {
42 return m_scalar->bytes();
43}

Referenced by deserialize(), deserialize_pair(), EC_Scalar(), from_bytes_mod_order(), from_bytes_with_trunc(), serialize_pair(), serialize_pair_to(), serialize_to(), and to_bigint().

◆ deserialize()

std::optional< EC_Scalar > Botan::EC_Scalar::deserialize ( const EC_Group & group,
std::span< const uint8_t > bytes )
static

Deserialize a scalar

The span must be exactly bytes() long; this function does not accept either short inputs (eg [1] to encode the integer 1) or inputs with excess leading zero bytes.

Returns nullopt if the length is incorrect or if the integer is not within the range [0,n) where n is the group order.

Definition at line 114 of file ec_scalar.cpp.

114 {
115 if(auto v = group._data()->scalar_deserialize(bytes)) {
116 return EC_Scalar(std::move(v));
117 } else {
118 return {};
119 }
120}

References Botan::EC_Group::_data(), bytes(), and EC_Scalar().

Referenced by deserialize_pair().

◆ deserialize_pair()

std::optional< std::pair< EC_Scalar, EC_Scalar > > Botan::EC_Scalar::deserialize_pair ( const EC_Group & group,
std::span< const uint8_t > bytes )
static

Deserialize a pair of scalars

Returns nullopt if the length is not 2*bytes(), or if either scalar is out of range or zero

Definition at line 96 of file ec_scalar.cpp.

97 {
98 if(bytes.size() % 2 != 0) {
99 return {};
100 }
101
102 const size_t half = bytes.size() / 2;
103
104 auto r = EC_Scalar::deserialize(group, bytes.first(half));
105 auto s = EC_Scalar::deserialize(group, bytes.last(half));
106
107 if(r && s) {
108 return std::make_pair(r.value(), s.value());
109 } else {
110 return {};
111 }
112}
static std::optional< EC_Scalar > deserialize(const EC_Group &group, std::span< const uint8_t > bytes)

References bytes(), and deserialize().

◆ from_bigint()

EC_Scalar Botan::EC_Scalar::from_bigint ( const EC_Group & group,
const BigInt & bn )
static

Convert from the argument BigInt to a EC_Scalar

Throws an exception if the provided bn is negative or too large

Definition at line 65 of file ec_scalar.cpp.

65 {
66 if(auto data = group._data()->scalar_from_bigint(bn)) {
67 return EC_Scalar(std::move(data));
68 } else {
69 throw Invalid_Argument("EC_Scalar::from_bigint input out of range");
70 }
71}

References Botan::EC_Group::_data(), and EC_Scalar().

◆ from_bytes_mod_order()

EC_Scalar Botan::EC_Scalar::from_bytes_mod_order ( const EC_Group & group,
std::span< const uint8_t > bytes )
static

Convert a bytestring to an EC_Scalar

This reduces the bytes modulo the group order. The input can be at most 2*bytes() long

Definition at line 49 of file ec_scalar.cpp.

49 {
50 if(auto s = group._data()->scalar_from_bytes_mod_order(bytes)) {
51 return EC_Scalar(std::move(s));
52 } else {
53 throw Decoding_Error("EC_Scalar::from_bytes_mod_order input invalid");
54 }
55}

References Botan::EC_Group::_data(), bytes(), and EC_Scalar().

◆ from_bytes_with_trunc()

EC_Scalar Botan::EC_Scalar::from_bytes_with_trunc ( const EC_Group & group,
std::span< const uint8_t > bytes )
static

Convert a bytestring to an EC_Scalar

This uses the truncation rules from ECDSA

Definition at line 45 of file ec_scalar.cpp.

45 {
46 return EC_Scalar(group._data()->scalar_from_bytes_with_trunc(bytes));
47}

References Botan::EC_Group::_data(), bytes(), and EC_Scalar().

◆ gk_x_mod_order()

EC_Scalar Botan::EC_Scalar::gk_x_mod_order ( const EC_Scalar & scalar,
RandomNumberGenerator & rng,
std::vector< BigInt > & ws )
static

Compute the elliptic curve scalar multiplication (g*k) where g is the standard base point on the curve. Then extract the x coordinate of the resulting point, and reduce it modulo the group order.

Workspace argument is transitional

Definition at line 79 of file ec_scalar.cpp.

79 {
80 const auto& group = scalar._inner().group();
81 return EC_Scalar(group->gk_x_mod_order(scalar.inner(), rng, ws));
82}

References _inner(), EC_Scalar(), and Botan::EC_Scalar_Data::group().

◆ invert()

EC_Scalar Botan::EC_Scalar::invert ( ) const

Return the modular inverse of this EC_Scalar

If *this is zero, then invert() returns zero

Definition at line 133 of file ec_scalar.cpp.

133 {
134 return EC_Scalar(inner().invert());
135}
EC_Scalar invert() const

References EC_Scalar(), and invert().

Referenced by invert(), and Botan::EC_PrivateKey_Data::public_key().

◆ is_eq()

bool Botan::EC_Scalar::is_eq ( const EC_Scalar & x) const

Test for equality

Definition at line 161 of file ec_scalar.cpp.

161 {
162 return inner().is_eq(x.inner());
163}
virtual bool is_eq(const EC_Scalar_Data &y) const =0

References Botan::EC_Scalar_Data::is_eq().

◆ is_nonzero()

bool Botan::EC_Scalar::is_nonzero ( ) const
inline

Return true if this EC_Scalar is not zero

Definition at line 147 of file ec_scalar.h.

147{ return !is_zero(); }
bool is_zero() const

◆ is_zero()

bool Botan::EC_Scalar::is_zero ( ) const

Return true if this EC_Scalar is zero

Definition at line 129 of file ec_scalar.cpp.

129 {
130 return inner().is_zero();
131}
virtual bool is_zero() const =0

References Botan::EC_Scalar_Data::is_zero().

◆ mul()

EC_Scalar Botan::EC_Scalar::mul ( const EC_Scalar & x) const

Scalar multiplication (modulo p)

Definition at line 153 of file ec_scalar.cpp.

153 {
154 return EC_Scalar(inner().mul(x.inner()));
155}
EC_Scalar mul(const EC_Scalar &x) const

References EC_Scalar(), and mul().

Referenced by mul().

◆ negate()

EC_Scalar Botan::EC_Scalar::negate ( ) const

Definition at line 137 of file ec_scalar.cpp.

137 {
138 return EC_Scalar(inner().negate());
139}
EC_Scalar negate() const

References EC_Scalar(), and negate().

Referenced by negate().

◆ one()

EC_Scalar Botan::EC_Scalar::one ( const EC_Group & group)
static

Return the scalar value 1

Definition at line 61 of file ec_scalar.cpp.

61 {
62 return EC_Scalar(group._data()->scalar_one());
63}

References Botan::EC_Group::_data(), and EC_Scalar().

◆ operator=() [1/2]

EC_Scalar & Botan::EC_Scalar::operator= ( const EC_Scalar & other)

Definition at line 26 of file ec_scalar.cpp.

26 {
27 if(this != &other) {
28 this->assign(other);
29 }
30 return (*this);
31}
void assign(const EC_Scalar &x)

References assign().

◆ operator=() [2/2]

EC_Scalar & Botan::EC_Scalar::operator= ( EC_Scalar && other)
noexcept

Definition at line 33 of file ec_scalar.cpp.

33 {
34 BOTAN_ARG_CHECK(_inner().group() == other._inner().group(), "Curve mismatch");
35 std::swap(m_scalar, other.m_scalar);
36 return (*this);
37}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
virtual const std::shared_ptr< const EC_Group_Data > & group() const =0
const EC_Scalar_Data & _inner() const
Definition ec_scalar.h:211

References BOTAN_ARG_CHECK.

◆ random()

EC_Scalar Botan::EC_Scalar::random ( const EC_Group & group,
RandomNumberGenerator & rng )
static

Return a new random scalar value

Definition at line 57 of file ec_scalar.cpp.

57 {
58 return EC_Scalar(group._data()->scalar_random(rng));
59}

References Botan::EC_Group::_data(), and EC_Scalar().

◆ serialize()

template<concepts::resizable_byte_buffer T = std::vector<uint8_t>>
T Botan::EC_Scalar::serialize ( ) const
inline

Return the bytes of the encoded scalar in a container

Definition at line 116 of file ec_scalar.h.

116 {
117 T s(this->bytes());
118 this->serialize_to(s);
119 return s;
120 }
void serialize_to(std::span< uint8_t > bytes) const
Definition ec_scalar.cpp:84
FE_25519 T
Definition ge.cpp:34

References T.

◆ serialize_pair()

template<concepts::resizable_byte_buffer T = std::vector<uint8_t>>
static T Botan::EC_Scalar::serialize_pair ( const EC_Scalar & r,
const EC_Scalar & s )
inlinestatic

Return the bytes of the encoded scalar in a container

Definition at line 133 of file ec_scalar.h.

133 {
134 T bytes(r.bytes() + s.bytes());
136 return bytes;
137 }
static void serialize_pair_to(std::span< uint8_t > bytes, const EC_Scalar &r, const EC_Scalar &s)
Definition ec_scalar.cpp:88

References bytes(), and T.

◆ serialize_pair_to()

void Botan::EC_Scalar::serialize_pair_to ( std::span< uint8_t > bytes,
const EC_Scalar & r,
const EC_Scalar & s )
static

Write the fixed length serialization to bytes

The provided span must be exactly bytes() long

Definition at line 88 of file ec_scalar.cpp.

88 {
89 BOTAN_ARG_CHECK(r._inner().group() == s._inner().group(), "Curve mismatch");
90 const size_t scalar_bytes = r.bytes();
91 BOTAN_ARG_CHECK(bytes.size() == 2 * scalar_bytes, "Invalid output length");
92 r.serialize_to(bytes.first(scalar_bytes));
93 s.serialize_to(bytes.last(scalar_bytes));
94}

References _inner(), BOTAN_ARG_CHECK, bytes(), Botan::EC_Scalar_Data::group(), and serialize_to().

◆ serialize_to()

void Botan::EC_Scalar::serialize_to ( std::span< uint8_t > bytes) const

Write the fixed length serialization to bytes

The provided span must be exactly bytes() long

Definition at line 84 of file ec_scalar.cpp.

84 {
85 inner().serialize_to(bytes);
86}
virtual void serialize_to(std::span< uint8_t > bytes) const =0

References bytes(), and Botan::EC_Scalar_Data::serialize_to().

Referenced by serialize_pair_to(), and Botan::EC_PrivateKey_Data::serialize_to().

◆ square_self()

void Botan::EC_Scalar::square_self ( )

Set *this to its own square modulo p

Definition at line 141 of file ec_scalar.cpp.

141 {
142 m_scalar->square_self();
143}

◆ sub()

EC_Scalar Botan::EC_Scalar::sub ( const EC_Scalar & x) const

Scalar subtraction (modulo p)

Definition at line 149 of file ec_scalar.cpp.

149 {
150 return EC_Scalar(inner().sub(x.inner()));
151}
EC_Scalar sub(const EC_Scalar &x) const

References EC_Scalar(), and sub().

Referenced by sub().

◆ to_bigint()

BigInt Botan::EC_Scalar::to_bigint ( ) const

Convert *this to a BigInt

Definition at line 73 of file ec_scalar.cpp.

73 {
74 secure_vector<uint8_t> bytes(m_scalar->bytes());
75 m_scalar->serialize_to(bytes);
77}
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:95
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61

References bytes(), and Botan::BigInt::from_bytes().

Friends And Related Symbol Documentation

◆ EC_AffinePoint

friend class EC_AffinePoint
friend

Definition at line 216 of file ec_scalar.h.

◆ operator*

EC_Scalar operator* ( const EC_Scalar & x,
const EC_Scalar & y )
friend

Definition at line 199 of file ec_scalar.h.

199{ return x.mul(y); }

◆ operator+

EC_Scalar operator+ ( const EC_Scalar & x,
const EC_Scalar & y )
friend

Definition at line 195 of file ec_scalar.h.

195{ return x.add(y); }

◆ operator-

EC_Scalar operator- ( const EC_Scalar & x,
const EC_Scalar & y )
friend

Definition at line 197 of file ec_scalar.h.

197{ return x.sub(y); }

◆ operator==

bool operator== ( const EC_Scalar & x,
const EC_Scalar & y )
friend

Definition at line 201 of file ec_scalar.h.

201{ return x.is_eq(y); }

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