Botan 3.9.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/mem_ops.h>
17#include <botan/internal/ct_utils.h>
18#include <array>
19
20namespace Botan {
21
22/**
23* An element of the field \\Z/(2^255-19)
24*
25* An element t, entries t[0]...t[9], represents the integer
26* t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9].
27* Bounds on each t[i] vary depending on context.
28*/
30 public:
31 /**
32 * Default zero initialization
33 */
34 constexpr Ed25519_FieldElement() : m_fe{} {}
35
36 constexpr static Ed25519_FieldElement zero() { return Ed25519_FieldElement(); }
37
38 constexpr static Ed25519_FieldElement one() {
39 auto o = Ed25519_FieldElement();
40 o.m_fe[0] = 1;
41 return o;
42 }
43
44 // NOLINTNEXTLINE(*-member-init)
45 constexpr explicit Ed25519_FieldElement(std::span<int32_t, 10> fe) { copy_mem(m_fe.data(), fe.data(), 10); }
46
47 // NOLINTNEXTLINE(*-member-init)
48 constexpr Ed25519_FieldElement(int64_t h0,
49 int64_t h1,
50 int64_t h2,
51 int64_t h3,
52 int64_t h4,
53 int64_t h5,
54 int64_t h6,
55 int64_t h7,
56 int64_t h8,
57 int64_t h9) {
58 m_fe[0] = static_cast<int32_t>(h0);
59 m_fe[1] = static_cast<int32_t>(h1);
60 m_fe[2] = static_cast<int32_t>(h2);
61 m_fe[3] = static_cast<int32_t>(h3);
62 m_fe[4] = static_cast<int32_t>(h4);
63 m_fe[5] = static_cast<int32_t>(h5);
64 m_fe[6] = static_cast<int32_t>(h6);
65 m_fe[7] = static_cast<int32_t>(h7);
66 m_fe[8] = static_cast<int32_t>(h8);
67 m_fe[9] = static_cast<int32_t>(h9);
68 }
69
70 static Ed25519_FieldElement deserialize(const uint8_t b[32]);
71
72 void serialize_to(std::span<uint8_t, 32> b) const;
73
74 bool is_zero() const {
75 std::array<uint8_t, 32> value = {};
76 this->serialize_to(value);
77 return CT::all_zeros(value.data(), value.size()).as_bool();
78 }
79
80 /*
81 return 1 if f is in {1,3,5,...,q-2}
82 return 0 if f is in {0,2,4,...,q-1}
83 */
84 bool is_negative() const {
85 // TODO could avoid most of the serialize computation here
86 std::array<uint8_t, 32> s = {};
87 this->serialize_to(s);
88 return (s[0] & 0x01) == 0x01;
89 }
90
93 for(size_t i = 0; i != 10; ++i) {
94 z.m_fe[i] = a.m_fe[i] + b.m_fe[i];
95 }
96 return z;
97 }
98
101 for(size_t i = 0; i != 10; ++i) {
102 z.m_fe[i] = a.m_fe[i] - b.m_fe[i];
103 }
104 return z;
105 }
106
109 for(size_t i = 0; i != 10; ++i) {
110 z.m_fe[i] = -a.m_fe[i];
111 }
112 return z;
113 }
114
116
117 Ed25519_FieldElement sqr_iter(size_t iter) const;
118
119 Ed25519_FieldElement sqr() const { return sqr_iter(1); }
120
121 // Return 2*a^2
123
125
127
128 // TODO remove
129 int32_t operator[](size_t i) const { return m_fe[i]; }
130
131 int32_t& operator[](size_t i) { return m_fe[i]; }
132
133 private:
134 std::array<int32_t, 10> m_fe;
135};
136
140
144
148
152
153} // namespace Botan
154
155#endif
static Ed25519_FieldElement negate(const Ed25519_FieldElement &a)
Definition ed25519_fe.h:107
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:99
Ed25519_FieldElement sqr() const
Definition ed25519_fe.h:119
Ed25519_FieldElement invert() const
constexpr Ed25519_FieldElement(std::span< int32_t, 10 > fe)
Definition ed25519_fe.h:45
Ed25519_FieldElement sqr2() const
static Ed25519_FieldElement add(const Ed25519_FieldElement &a, const Ed25519_FieldElement &b)
Definition ed25519_fe.h:91
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:48
void serialize_to(std::span< uint8_t, 32 > b) const
static constexpr Ed25519_FieldElement one()
Definition ed25519_fe.h:38
Ed25519_FieldElement sqr_iter(size_t iter) const
constexpr Ed25519_FieldElement()
Definition ed25519_fe.h:34
int32_t & operator[](size_t i)
Definition ed25519_fe.h:131
static constexpr Ed25519_FieldElement zero()
Definition ed25519_fe.h:36
int32_t operator[](size_t i) const
Definition ed25519_fe.h:129
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:813
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145
BigInt operator*(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:56
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
BigInt operator-(const BigInt &x, const BigInt &y)
Definition bigint.h:1095