Botan 3.11.0
Crypto and TLS for C&
ed25519_fe.h
Go to the documentation of this file.
1/*
2* Ed25519 field element
3* (C) 2017 Ribose Inc
4* 2025 Jack Lloyd
5*
6* Based on the public domain code from SUPERCOP ref10 by
7* Peter Schwabe, Daniel J. Bernstein, Niels Duif, Tanja Lange, Bo-Yin Yang
8*
9* Botan is released under the Simplified BSD License (see license.txt)
10*/
11
12#ifndef BOTAN_ED25519_FE_H_
13#define BOTAN_ED25519_FE_H_
14
15#include <botan/exceptn.h>
16#include <botan/internal/ct_utils.h>
17#include <array>
18
19namespace Botan {
20
21/**
22* An element of the field \\Z/(2^255-19)
23*
24* An element t, entries t[0]...t[9], represents the integer
25* t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9].
26* Bounds on each t[i] vary depending on context.
27*/
29 public:
30 /**
31 * Default zero initialization
32 */
33 constexpr Ed25519_FieldElement() : m_fe{} {}
34
35 constexpr static Ed25519_FieldElement zero() { return Ed25519_FieldElement(); }
36
37 constexpr static Ed25519_FieldElement one() {
38 auto o = Ed25519_FieldElement();
39 o.m_fe[0] = 1;
40 return o;
41 }
42
43 // NOLINTNEXTLINE(*-member-init)
44 constexpr explicit Ed25519_FieldElement(std::span<int32_t, 10> fe) {
45 for(size_t i = 0; i != 10; ++i) {
46 m_fe[i] = fe[i];
47 }
48 }
49
50 // NOLINTNEXTLINE(*-member-init)
51 constexpr Ed25519_FieldElement(int64_t h0,
52 int64_t h1,
53 int64_t h2,
54 int64_t h3,
55 int64_t h4,
56 int64_t h5,
57 int64_t h6,
58 int64_t h7,
59 int64_t h8,
60 int64_t h9) {
61 m_fe[0] = static_cast<int32_t>(h0);
62 m_fe[1] = static_cast<int32_t>(h1);
63 m_fe[2] = static_cast<int32_t>(h2);
64 m_fe[3] = static_cast<int32_t>(h3);
65 m_fe[4] = static_cast<int32_t>(h4);
66 m_fe[5] = static_cast<int32_t>(h5);
67 m_fe[6] = static_cast<int32_t>(h6);
68 m_fe[7] = static_cast<int32_t>(h7);
69 m_fe[8] = static_cast<int32_t>(h8);
70 m_fe[9] = static_cast<int32_t>(h9);
71 }
72
73 static Ed25519_FieldElement deserialize(const uint8_t b[32]);
74
75 void serialize_to(std::span<uint8_t, 32> b) const;
76
77 bool is_zero() const {
78 std::array<uint8_t, 32> value = {};
79 this->serialize_to(value);
80 return CT::all_zeros(value.data(), value.size()).as_bool();
81 }
82
83 /*
84 return 1 if f is in {1,3,5,...,q-2}
85 return 0 if f is in {0,2,4,...,q-1}
86 */
87 bool is_negative() const {
88 // TODO could avoid most of the serialize computation here
89 std::array<uint8_t, 32> s = {};
90 this->serialize_to(s);
91 return (s[0] & 0x01) == 0x01;
92 }
93
96 for(size_t i = 0; i != 10; ++i) {
97 z.m_fe[i] = a.m_fe[i] + b.m_fe[i];
98 }
99 return z;
100 }
101
104 for(size_t i = 0; i != 10; ++i) {
105 z.m_fe[i] = a.m_fe[i] - b.m_fe[i];
106 }
107 return z;
108 }
109
112 for(size_t i = 0; i != 10; ++i) {
113 z.m_fe[i] = -a.m_fe[i];
114 }
115 return z;
116 }
117
119
120 Ed25519_FieldElement sqr_iter(size_t iter) const;
121
122 Ed25519_FieldElement sqr() const { return sqr_iter(1); }
123
124 // Return 2*a^2
126
128
130
131 // TODO remove
132 int32_t operator[](size_t i) const { return m_fe[i]; }
133
134 int32_t& operator[](size_t i) { return m_fe[i]; }
135
136 private:
137 std::array<int32_t, 10> m_fe;
138};
139
143
147
151
155
156} // namespace Botan
157
158#endif
static Ed25519_FieldElement negate(const Ed25519_FieldElement &a)
Definition ed25519_fe.h:110
static Ed25519_FieldElement deserialize(const uint8_t b[32])
static Ed25519_FieldElement sub(const Ed25519_FieldElement &a, const Ed25519_FieldElement &b)
Definition ed25519_fe.h:102
Ed25519_FieldElement sqr() const
Definition ed25519_fe.h:122
Ed25519_FieldElement invert() const
constexpr Ed25519_FieldElement(std::span< int32_t, 10 > fe)
Definition ed25519_fe.h:44
Ed25519_FieldElement sqr2() const
static Ed25519_FieldElement add(const Ed25519_FieldElement &a, const Ed25519_FieldElement &b)
Definition ed25519_fe.h:94
static Ed25519_FieldElement mul(const Ed25519_FieldElement &a, const Ed25519_FieldElement &b)
Ed25519_FieldElement pow_22523() const
constexpr Ed25519_FieldElement(int64_t h0, int64_t h1, int64_t h2, int64_t h3, int64_t h4, int64_t h5, int64_t h6, int64_t h7, int64_t h8, int64_t h9)
Definition ed25519_fe.h:51
void serialize_to(std::span< uint8_t, 32 > b) const
static constexpr Ed25519_FieldElement one()
Definition ed25519_fe.h:37
Ed25519_FieldElement sqr_iter(size_t iter) const
constexpr Ed25519_FieldElement()
Definition ed25519_fe.h:33
int32_t & operator[](size_t i)
Definition ed25519_fe.h:134
static constexpr Ed25519_FieldElement zero()
Definition ed25519_fe.h:35
int32_t operator[](size_t i) const
Definition ed25519_fe.h:132
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:785
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:1111