Botan 3.0.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*
5* Based on the public domain code from SUPERCOP ref10 by
6* Peter Schwabe, Daniel J. Bernstein, Niels Duif, Tanja Lange, Bo-Yin Yang
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#ifndef BOTAN_ED25519_FE_H_
12#define BOTAN_ED25519_FE_H_
13
14#include <botan/mem_ops.h>
15#include <botan/exceptn.h>
16
17namespace Botan {
18
19/**
20* An element of the field \\Z/(2^255-19)
21*/
23 {
24 public:
25 ~FE_25519() { secure_scrub_memory(m_fe, sizeof(m_fe)); }
26
27 /**
28 * Zero element
29 */
30 FE_25519(int init = 0)
31 {
32 if(init != 0 && init != 1)
33 throw Invalid_Argument("Invalid FE_25519 initial value");
34 clear_mem(m_fe, 10);
35 m_fe[0] = init;
36 }
37
38 FE_25519(std::initializer_list<int32_t> x)
39 {
40 if(x.size() != 10)
41 throw Invalid_Argument("Invalid FE_25519 initializer list");
42 copy_mem(m_fe, x.begin(), 10);
43 }
44
45 FE_25519(int64_t h0, int64_t h1, int64_t h2, int64_t h3, int64_t h4,
46 int64_t h5, int64_t h6, int64_t h7, int64_t h8, int64_t h9)
47 {
48 m_fe[0] = static_cast<int32_t>(h0);
49 m_fe[1] = static_cast<int32_t>(h1);
50 m_fe[2] = static_cast<int32_t>(h2);
51 m_fe[3] = static_cast<int32_t>(h3);
52 m_fe[4] = static_cast<int32_t>(h4);
53 m_fe[5] = static_cast<int32_t>(h5);
54 m_fe[6] = static_cast<int32_t>(h6);
55 m_fe[7] = static_cast<int32_t>(h7);
56 m_fe[8] = static_cast<int32_t>(h8);
57 m_fe[9] = static_cast<int32_t>(h9);
58 }
59
60 FE_25519(const FE_25519& other) = default;
61 FE_25519& operator=(const FE_25519& other) = default;
62
63 FE_25519(FE_25519&& other) = default;
64 FE_25519& operator=(FE_25519&& other) = default;
65
66 void from_bytes(const uint8_t b[32]);
67 void to_bytes(uint8_t b[32]) const;
68
69 bool is_zero() const
70 {
71 uint8_t s[32];
72 to_bytes(s);
73
74 uint8_t sum = 0;
75 for(size_t i = 0; i != 32; ++i)
76 { sum |= s[i]; }
77
78 return (sum == 0);
79 }
80
81 /*
82 return 1 if f is in {1,3,5,...,q-2}
83 return 0 if f is in {0,2,4,...,q-1}
84 */
85 bool is_negative() const
86 {
87 // TODO could avoid most of the to_bytes computation here
88 uint8_t s[32];
89 to_bytes(s);
90 return s[0] & 1;
91 }
92
93 static FE_25519 add(const FE_25519& a, const FE_25519& b)
94 {
95 FE_25519 z;
96 for(size_t i = 0; i != 10; ++i)
97 { z[i] = a[i] + b[i]; }
98 return z;
99 }
100
101 static FE_25519 sub(const FE_25519& a, const FE_25519& b)
102 {
103 FE_25519 z;
104 for(size_t i = 0; i != 10; ++i)
105 { z[i] = a[i] - b[i]; }
106 return z;
107 }
108
109 static FE_25519 negate(const FE_25519& a)
110 {
111 FE_25519 z;
112 for(size_t i = 0; i != 10; ++i)
113 { z[i] = -a[i]; }
114 return z;
115 }
116
117 static FE_25519 mul(const FE_25519& a, const FE_25519& b);
118 static FE_25519 sqr_iter(const FE_25519& a, size_t iter);
119 static FE_25519 sqr(const FE_25519& a) { return sqr_iter(a, 1); }
120 static FE_25519 sqr2(const FE_25519& a);
121 static FE_25519 pow_22523(const FE_25519& a);
122 static FE_25519 invert(const FE_25519& a);
123
124 // TODO remove
125 int32_t operator[](size_t i) const { return m_fe[i]; }
126 int32_t& operator[](size_t i) { return m_fe[i]; }
127
128 private:
129
130 int32_t m_fe[10];
131 };
132
133typedef FE_25519 fe;
134
135/*
136fe means field element.
137Here the field is
138An element t, entries t[0]...t[9], represents the integer
139t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9].
140Bounds on each t[i] vary depending on context.
141*/
142
143inline void fe_frombytes(fe& x, const uint8_t* b)
144 {
145 x.from_bytes(b);
146 }
147
148inline void fe_tobytes(uint8_t* b, const fe& x)
149 {
150 x.to_bytes(b);
151 }
152
153inline void fe_copy(fe& a, const fe& b)
154 {
155 a = b;
156 }
157
158inline int fe_isnonzero(const fe& x)
159 {
160 return x.is_zero() ? 0 : 1;
161 }
162
163inline int fe_isnegative(const fe& x)
164 {
165 return x.is_negative();
166 }
167
168
169inline void fe_0(fe& x)
170 {
171 x = FE_25519();
172 }
173
174inline void fe_1(fe& x)
175 {
176 x = FE_25519(1);
177 }
178
179inline void fe_add(fe& x, const fe& a, const fe& b)
180 {
181 x = FE_25519::add(a, b);
182 }
183
184inline void fe_sub(fe& x, const fe& a, const fe& b)
185 {
186 x = FE_25519::sub(a, b);
187 }
188
189inline void fe_neg(fe& x, const fe& z)
190 {
191 x = FE_25519::negate(z);
192 }
193
194inline void fe_mul(fe& x, const fe& a, const fe& b)
195 {
196 x = FE_25519::mul(a, b);
197 }
198
199inline void fe_sq(fe& x, const fe& z)
200 {
201 x = FE_25519::sqr(z);
202 }
203
204inline void fe_sq_iter(fe& x, const fe& z, size_t iter)
205 {
206 x = FE_25519::sqr_iter(z, iter);
207 }
208
209inline void fe_sq2(fe& x, const fe& z)
210 {
211 x = FE_25519::sqr2(z);
212 }
213
214inline void fe_invert(fe& x, const fe& z)
215 {
216 x = FE_25519::invert(z);
217 }
218
219inline void fe_pow22523(fe& x, const fe& y)
220 {
222 }
223
224}
225
226#endif
static SIMD_4x64 y
static FE_25519 add(const FE_25519 &a, const FE_25519 &b)
Definition: ed25519_fe.h:93
FE_25519 & operator=(FE_25519 &&other)=default
static FE_25519 sub(const FE_25519 &a, const FE_25519 &b)
Definition: ed25519_fe.h:101
FE_25519(const FE_25519 &other)=default
bool is_negative() const
Definition: ed25519_fe.h:85
FE_25519(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:45
void to_bytes(uint8_t b[32]) const
Definition: ed25519_fe.cpp:667
void from_bytes(const uint8_t b[32])
Definition: ed25519_fe.cpp:605
static FE_25519 invert(const FE_25519 &a)
Definition: ed25519_fe.cpp:17
static FE_25519 sqr(const FE_25519 &a)
Definition: ed25519_fe.h:119
FE_25519(std::initializer_list< int32_t > x)
Definition: ed25519_fe.h:38
FE_25519 & operator=(const FE_25519 &other)=default
static FE_25519 mul(const FE_25519 &a, const FE_25519 &b)
Definition: ed25519_fe.cpp:115
FE_25519(FE_25519 &&other)=default
int32_t & operator[](size_t i)
Definition: ed25519_fe.h:126
static FE_25519 negate(const FE_25519 &a)
Definition: ed25519_fe.h:109
int32_t operator[](size_t i) const
Definition: ed25519_fe.h:125
static FE_25519 pow_22523(const FE_25519 &a)
Definition: ed25519_fe.cpp:50
FE_25519(int init=0)
Definition: ed25519_fe.h:30
static FE_25519 sqr_iter(const FE_25519 &a, size_t iter)
Definition: ed25519_fe.cpp:337
bool is_zero() const
Definition: ed25519_fe.h:69
static FE_25519 sqr2(const FE_25519 &a)
Definition: ed25519_fe.cpp:479
int(* init)(CTX *)
Definition: alg_id.cpp:12
void fe_sq2(fe &x, const fe &z)
Definition: ed25519_fe.h:209
void fe_0(fe &x)
Definition: ed25519_fe.h:169
int fe_isnegative(const fe &x)
Definition: ed25519_fe.h:163
int fe_isnonzero(const fe &x)
Definition: ed25519_fe.h:158
void fe_1(fe &x)
Definition: ed25519_fe.h:174
void fe_mul(fe &x, const fe &a, const fe &b)
Definition: ed25519_fe.h:194
void fe_sq(fe &x, const fe &z)
Definition: ed25519_fe.h:199
void fe_neg(fe &x, const fe &z)
Definition: ed25519_fe.h:189
FE_25519 fe
Definition: ed25519_fe.h:133
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:126
void secure_scrub_memory(void *ptr, size_t n)
Definition: os_utils.cpp:81
void fe_sq_iter(fe &x, const fe &z, size_t iter)
Definition: ed25519_fe.h:204
void fe_copy(fe &a, const fe &b)
Definition: ed25519_fe.h:153
void fe_add(fe &x, const fe &a, const fe &b)
Definition: ed25519_fe.h:179
void fe_sub(fe &x, const fe &a, const fe &b)
Definition: ed25519_fe.h:184
void fe_tobytes(uint8_t *b, const fe &x)
Definition: ed25519_fe.h:148
void fe_pow22523(fe &x, const fe &y)
Definition: ed25519_fe.h:219
void fe_invert(fe &x, const fe &z)
Definition: ed25519_fe.h:214
void fe_frombytes(fe &x, const uint8_t *b)
Definition: ed25519_fe.h:143
constexpr void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:115