780d74a122d3f04689d023199958504821d5e9a5
[openssl.git] / crypto / ec / curve448 / point_448.h
1 /**
2  * @file decaf/point_448.h
3  * @author Mike Hamburg
4  *
5  * @copyright
6  *   Copyright (c) 2015-2016 Cryptography Research, Inc.  \n
7  *   Released under the MIT License.  See LICENSE.txt for license information.
8  *
9  * @brief A group of prime order p, based on Ed448-Goldilocks.
10  *
11  * @warning This file was automatically generated in Python.
12  * Please do not edit it.
13  */
14
15 #ifndef __DECAF_POINT_448_H__
16 #define __DECAF_POINT_448_H__ 1
17
18 #include "curve448utils.h"
19 #include "field.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 /** @cond internal */
26 #define DECAF_448_SCALAR_LIMBS ((446-1)/DECAF_WORD_BITS+1)
27 /** @endcond */
28
29 /** The number of bits in a scalar */
30 #define DECAF_448_SCALAR_BITS 446
31
32 /** Number of bytes in a serialized point. */
33 #define DECAF_448_SER_BYTES 56
34
35 /** Number of bytes in an elligated point.  For now set the same as SER_BYTES
36  * but could be different for other curves.
37  */
38 #define DECAF_448_HASH_BYTES 56
39
40 /** Number of bytes in a serialized scalar. */
41 #define DECAF_448_SCALAR_BYTES 56
42
43 /** Number of bits in the "which" field of an elligator inverse */
44 #define DECAF_448_INVERT_ELLIGATOR_WHICH_BITS 3
45
46 /** The cofactor the curve would have, if we hadn't removed it */
47 #define DECAF_448_REMOVED_COFACTOR 4
48
49 /** X448 encoding ratio. */
50 #define DECAF_X448_ENCODE_RATIO 2
51
52 /** Number of bytes in an x448 public key */
53 #define DECAF_X448_PUBLIC_BYTES 56
54
55 /** Number of bytes in an x448 private key */
56 #define DECAF_X448_PRIVATE_BYTES 56
57
58 /** Twisted Edwards extended homogeneous coordinates */
59 typedef struct curve448_point_s {
60     /** @cond internal */
61     gf_448_t x,y,z,t;
62     /** @endcond */
63 } curve448_point_t[1];
64
65 /** Precomputed table based on a point.  Can be trivial implementation. */
66 struct curve448_precomputed_s;
67
68 /** Precomputed table based on a point.  Can be trivial implementation. */
69 typedef struct curve448_precomputed_s curve448_precomputed_s; 
70
71 /** Scalar is stored packed, because we don't need the speed. */
72 typedef struct curve448_scalar_s {
73     /** @cond internal */
74     decaf_word_t limb[DECAF_448_SCALAR_LIMBS];
75     /** @endcond */
76 } curve448_scalar_t[1];
77
78 /** A scalar equal to 1. */
79 extern const curve448_scalar_t curve448_scalar_one;
80
81 /** A scalar equal to 0. */
82 extern const curve448_scalar_t curve448_scalar_zero;
83
84 /** The identity point on the curve. */
85 extern const curve448_point_t curve448_point_identity;
86
87 /** An arbitrarily chosen base point on the curve. */
88 extern const curve448_point_t curve448_point_base;
89
90 /** Precomputed table for the base point on the curve. */
91 extern const struct curve448_precomputed_s *curve448_precomputed_base;
92
93 /**
94  * @brief Read a scalar from wire format or from bytes.
95  *
96  * @param [in] ser Serialized form of a scalar.
97  * @param [out] out Deserialized form.
98  *
99  * @retval DECAF_SUCCESS The scalar was correctly encoded.
100  * @retval DECAF_FAILURE The scalar was greater than the modulus,
101  * and has been reduced modulo that modulus.
102  */
103 __owur decaf_error_t curve448_scalar_decode (
104     curve448_scalar_t out,
105     const unsigned char ser[DECAF_448_SCALAR_BYTES]
106 ) DECAF_NOINLINE;
107
108 /**
109  * @brief Read a scalar from wire format or from bytes.  Reduces mod
110  * scalar prime.
111  *
112  * @param [in] ser Serialized form of a scalar.
113  * @param [in] ser_len Length of serialized form.
114  * @param [out] out Deserialized form.
115  */
116 void curve448_scalar_decode_long (
117     curve448_scalar_t out,
118     const unsigned char *ser,
119     size_t ser_len
120 ) DECAF_NOINLINE;
121     
122 /**
123  * @brief Serialize a scalar to wire format.
124  *
125  * @param [out] ser Serialized form of a scalar.
126  * @param [in] s Deserialized scalar.
127  */
128 void curve448_scalar_encode (
129     unsigned char ser[DECAF_448_SCALAR_BYTES],
130     const curve448_scalar_t s
131 ) DECAF_NOINLINE DECAF_NOINLINE;
132         
133 /**
134  * @brief Add two scalars.  The scalars may use the same memory.
135  * @param [in] a One scalar.
136  * @param [in] b Another scalar.
137  * @param [out] out a+b.
138  */
139 void curve448_scalar_add (
140     curve448_scalar_t out,
141     const curve448_scalar_t a,
142     const curve448_scalar_t b
143 ) DECAF_NOINLINE;
144
145 /**
146  * @brief Subtract two scalars.  The scalars may use the same memory.
147  * @param [in] a One scalar.
148  * @param [in] b Another scalar.
149  * @param [out] out a-b.
150  */  
151 void curve448_scalar_sub (
152     curve448_scalar_t out,
153     const curve448_scalar_t a,
154     const curve448_scalar_t b
155 ) DECAF_NOINLINE;
156
157 /**
158  * @brief Multiply two scalars.  The scalars may use the same memory.
159  * @param [in] a One scalar.
160  * @param [in] b Another scalar.
161  * @param [out] out a*b.
162  */  
163 void curve448_scalar_mul (
164     curve448_scalar_t out,
165     const curve448_scalar_t a,
166     const curve448_scalar_t b
167 ) DECAF_NOINLINE;
168         
169 /**
170 * @brief Halve a scalar.  The scalars may use the same memory.
171 * @param [in] a A scalar.
172 * @param [out] out a/2.
173 */
174 void curve448_scalar_halve (
175    curve448_scalar_t out,
176    const curve448_scalar_t a
177 ) DECAF_NOINLINE;
178
179 /**
180  * @brief Copy a scalar.  The scalars may use the same memory, in which
181  * case this function does nothing.
182  * @param [in] a A scalar.
183  * @param [out] out Will become a copy of a.
184  */
185 static inline void curve448_scalar_copy (
186     curve448_scalar_t out,
187     const curve448_scalar_t a
188 ) {
189     *out = *a;
190 }
191
192 /**
193  * @brief Copy a point.  The input and output may alias,
194  * in which case this function does nothing.
195  *
196  * @param [out] a A copy of the point.
197  * @param [in] b Any point.
198  */
199 static inline void curve448_point_copy (
200     curve448_point_t a,
201     const curve448_point_t b
202 ) {
203     *a=*b;
204 }
205
206 /**
207  * @brief Test whether two points are equal.  If yes, return
208  * DECAF_TRUE, else return DECAF_FALSE.
209  *
210  * @param [in] a A point.
211  * @param [in] b Another point.
212  * @retval DECAF_TRUE The points are equal.
213  * @retval DECAF_FALSE The points are not equal.
214  */
215 __owur decaf_bool_t curve448_point_eq (
216     const curve448_point_t a,
217     const curve448_point_t b
218 ) DECAF_NOINLINE;
219
220 /**
221  * @brief Double a point.  Equivalent to
222  * curve448_point_add(two_a,a,a), but potentially faster.
223  *
224  * @param [out] two_a The sum a+a.
225  * @param [in] a A point.
226  */
227 void curve448_point_double (
228     curve448_point_t two_a,
229     const curve448_point_t a
230 );
231
232 /**
233  * @brief RFC 7748 Diffie-Hellman scalarmul.  This function uses a different
234  * (non-Decaf) encoding.
235  *
236  * @param [out] scaled The scaled point base*scalar
237  * @param [in] base The point to be scaled.
238  * @param [in] scalar The scalar to multiply by.
239  *
240  * @retval DECAF_SUCCESS The scalarmul succeeded.
241  * @retval DECAF_FAILURE The scalarmul didn't succeed, because the base
242  * point is in a small subgroup.
243  */
244 __owur decaf_error_t decaf_x448 (
245     uint8_t out[DECAF_X448_PUBLIC_BYTES],
246     const uint8_t base[DECAF_X448_PUBLIC_BYTES],
247     const uint8_t scalar[DECAF_X448_PRIVATE_BYTES]
248 ) DECAF_NOINLINE;
249
250 /**
251  * @brief Multiply a point by DECAF_X448_ENCODE_RATIO,
252  * then encode it like RFC 7748.
253  *
254  * This function is mainly used internally, but is exported in case
255  * it will be useful.
256  *
257  * The ratio is necessary because the internal representation doesn't
258  * track the cofactor information, so on output we must clear the cofactor.
259  * This would multiply by the cofactor, but in fact internally libdecaf's
260  * points are always even, so it multiplies by half the cofactor instead.
261  *
262  * As it happens, this aligns with the base point definitions; that is,
263  * if you pass the Decaf/Ristretto base point to this function, the result
264  * will be DECAF_X448_ENCODE_RATIO times the X448
265  * base point.
266  *
267  * @param [out] out The scaled and encoded point.
268  * @param [in] p The point to be scaled and encoded.
269  */
270 void curve448_point_mul_by_ratio_and_encode_like_x448 (
271     uint8_t out[DECAF_X448_PUBLIC_BYTES],
272     const curve448_point_t p
273 );
274
275 /** The base point for X448 Diffie-Hellman */
276 extern const uint8_t decaf_x448_base_point[DECAF_X448_PUBLIC_BYTES];
277     
278 /**
279  * @brief RFC 7748 Diffie-Hellman base point scalarmul.  This function uses
280  * a different (non-Decaf) encoding.
281  *
282  * Does exactly the same thing as decaf_x448_generate_key,
283  * but has a better name.
284  *
285  * @param [out] scaled The scaled point base*scalar
286  * @param [in] scalar The scalar to multiply by.
287  */
288 void decaf_x448_derive_public_key (
289     uint8_t out[DECAF_X448_PUBLIC_BYTES],
290     const uint8_t scalar[DECAF_X448_PRIVATE_BYTES]
291 ) DECAF_NOINLINE;
292
293
294 /**
295  * @brief Multiply a precomputed base point by a scalar:
296  * scaled = scalar*base.
297  * Some implementations do not include precomputed points; for
298  * those implementations, this function is the same as
299  * curve448_point_scalarmul
300  *
301  * @param [out] scaled The scaled point base*scalar
302  * @param [in] base The point to be scaled.
303  * @param [in] scalar The scalar to multiply by.
304  */
305 void curve448_precomputed_scalarmul (
306     curve448_point_t scaled,
307     const curve448_precomputed_s *base,
308     const curve448_scalar_t scalar
309 ) DECAF_NOINLINE;
310
311
312 /**
313  * @brief Multiply two base points by two scalars:
314  * scaled = scalar1*curve448_point_base + scalar2*base2.
315  *
316  * Otherwise equivalent to curve448_point_double_scalarmul, but may be
317  * faster at the expense of being variable time.
318  *
319  * @param [out] combo The linear combination scalar1*base + scalar2*base2.
320  * @param [in] scalar1 A first scalar to multiply by.
321  * @param [in] base2 A second point to be scaled.
322  * @param [in] scalar2 A second scalar to multiply by.
323  *
324  * @warning: This function takes variable time, and may leak the scalars
325  * used.  It is designed for signature verification.
326  */
327 void curve448_base_double_scalarmul_non_secret (
328     curve448_point_t combo,
329     const curve448_scalar_t scalar1,
330     const curve448_point_t base2,
331     const curve448_scalar_t scalar2
332 ) DECAF_NOINLINE;
333
334 /**
335  * @brief Test that a point is valid, for debugging purposes.
336  *
337  * @param [in] to_test The point to test.
338  * @retval DECAF_TRUE The point is valid.
339  * @retval DECAF_FALSE The point is invalid.
340  */
341 __owur decaf_bool_t curve448_point_valid (
342     const curve448_point_t to_test
343 ) DECAF_NOINLINE;
344
345 /**
346  * @brief Overwrite scalar with zeros.
347  */
348 void curve448_scalar_destroy (
349     curve448_scalar_t scalar
350 );
351
352 /**
353  * @brief Overwrite point with zeros.
354  */
355 void curve448_point_destroy (
356     curve448_point_t point
357 );
358
359 #ifdef __cplusplus
360 } /* extern "C" */
361 #endif
362
363 #endif /* __DECAF_POINT_448_H__ */