Botan 3.14.1
Crypto and TLS for C&
ed25519_scalar.h
Go to the documentation of this file.
1/*
2* Ed25519 scalar
3* (C) 2017 Ribose Inc
4* 2026 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_ED25519_SCALAR_H_
10#define BOTAN_ED25519_SCALAR_H_
11
12#include <botan/types.h>
13#include <array>
14#include <optional>
15#include <span>
16
17namespace Botan {
18
19/**
20* An integer modulo the order of the Ed25519 group,
21* l = 2^252 + 27742317777372353535851937790883648493
22*
23* Values are always fully reduced, and all operations aside from
24* from_canonical_bytes are constant time.
25*/
26class BOTAN_TEST_API Ed25519_Scalar final {
27 private:
28 static constexpr size_t WORDS = 32 / sizeof(word);
29
30 public:
31 static constexpr size_t BYTES = 32;
32
33 static Ed25519_Scalar zero() { return Ed25519_Scalar(std::array<word, WORDS>{}); }
34
35 static Ed25519_Scalar one() { return Ed25519_Scalar(std::array<word, WORDS>{1}); }
36
37 /**
38 * Deserialize 32 little endian bytes, reducing modulo l
39 */
40 static Ed25519_Scalar from_bytes(std::span<const uint8_t, BYTES> b);
41
42 /**
43 * Deserialize 64 little endian bytes (eg a wide hash output),
44 * reducing modulo l
45 */
46 static Ed25519_Scalar from_wide_bytes(std::span<const uint8_t, 2 * BYTES> b);
47
48 /**
49 * Deserialize 32 little endian bytes, returning nullopt unless the
50 * value is already reduced
51 */
52 static std::optional<Ed25519_Scalar> from_canonical_bytes(std::span<const uint8_t, BYTES> b);
53
54 /**
55 * Serialize to 32 little endian bytes
56 */
57 void serialize_to(std::span<uint8_t, BYTES> b) const;
58
59 std::array<uint8_t, BYTES> to_bytes() const {
60 std::array<uint8_t, BYTES> b; // NOLINT(*-member-init)
61 serialize_to(b);
62 return b;
63 }
64
65 Ed25519_Scalar operator+(const Ed25519_Scalar& other) const;
66
67 Ed25519_Scalar operator*(const Ed25519_Scalar& other) const;
68
70
71 private:
72 explicit Ed25519_Scalar(const std::array<word, WORDS>& v) : m_val(v) {}
73
74 std::array<word, WORDS> m_val;
75};
76
77} // namespace Botan
78
79#endif
#define BOTAN_TEST_API
Definition api.h:41
static constexpr size_t BYTES
static Ed25519_Scalar zero()
std::array< uint8_t, BYTES > to_bytes() const
static Ed25519_Scalar one()
void serialize_to(std::span< uint8_t, BYTES > b) const
BigInt operator*(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:57
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
BigInt operator-(const BigInt &x, const BigInt &y)
Definition bigint.h:1216
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