Fix a travis failure in the curve448 code
[openssl.git] / crypto / ec / curve448 / point_448.h
1 /*
2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2015-2016 Cryptography Research, Inc.
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  *
10  * Originally written by Mike Hamburg
11  */
12
13 #ifndef HEADER_POINT_448_H
14 # define HEADER_POINT_448_H
15
16 # include "curve448utils.h"
17 # include "field.h"
18
19 /* Comb config: number of combs, n, t, s. */
20 #define COMBS_N 5
21 #define COMBS_T 5
22 #define COMBS_S 18
23
24 /* Projective Niels coordinates */
25 typedef struct {
26     gf a, b, c;
27 } niels_s, niels_t[1];
28 typedef struct {
29     niels_t n;
30     gf z;
31 } pniels_t[1];
32
33 /* Precomputed base */
34 struct curve448_precomputed_s {
35     niels_t table[COMBS_N << (COMBS_T - 1)];
36 };
37
38 # define C448_SCALAR_LIMBS ((446-1)/C448_WORD_BITS+1)
39
40 /* The number of bits in a scalar */
41 # define C448_SCALAR_BITS 446
42
43 /* Number of bytes in a serialized scalar. */
44 # define C448_SCALAR_BYTES 56
45
46 /* X448 encoding ratio. */
47 # define X448_ENCODE_RATIO 2
48
49 /* Number of bytes in an x448 public key */
50 # define X448_PUBLIC_BYTES 56
51
52 /* Number of bytes in an x448 private key */
53 # define X448_PRIVATE_BYTES 56
54
55 /* Twisted Edwards extended homogeneous coordinates */
56 typedef struct curve448_point_s {
57     gf x, y, z, t;
58 } curve448_point_t[1];
59
60 /* Precomputed table based on a point.  Can be trivial implementation. */
61 struct curve448_precomputed_s;
62
63 /* Precomputed table based on a point.  Can be trivial implementation. */
64 typedef struct curve448_precomputed_s curve448_precomputed_s;
65
66 /* Scalar is stored packed, because we don't need the speed. */
67 typedef struct curve448_scalar_s {
68     c448_word_t limb[C448_SCALAR_LIMBS];
69 } curve448_scalar_t[1];
70
71 /* A scalar equal to 1. */
72 extern const curve448_scalar_t curve448_scalar_one;
73
74 /* A scalar equal to 0. */
75 extern const curve448_scalar_t curve448_scalar_zero;
76
77 /* The identity point on the curve. */
78 extern const curve448_point_t curve448_point_identity;
79
80 /* Precomputed table for the base point on the curve. */
81 extern const struct curve448_precomputed_s *curve448_precomputed_base;
82 extern const niels_t *curve448_wnaf_base;
83
84 /*
85  * Read a scalar from wire format or from bytes.
86  *
87  * ser (in): Serialized form of a scalar.
88  * out (out): Deserialized form.
89  *
90  * Returns:
91  * C448_SUCCESS: The scalar was correctly encoded.
92  * C448_FAILURE: The scalar was greater than the modulus, and has been reduced
93  * modulo that modulus.
94  */
95 __owur c448_error_t curve448_scalar_decode(
96                             curve448_scalar_t out,
97                             const unsigned char ser[C448_SCALAR_BYTES]);
98
99 /*
100  * Read a scalar from wire format or from bytes.  Reduces mod scalar prime.
101  *
102  * ser (in): Serialized form of a scalar.
103  * ser_len (in): Length of serialized form.
104  * out (out): Deserialized form.
105  */
106 void curve448_scalar_decode_long(curve448_scalar_t out,
107                                  const unsigned char *ser, size_t ser_len);
108
109 /*
110  * Serialize a scalar to wire format.
111  *
112  * ser (out): Serialized form of a scalar.
113  * s (in): Deserialized scalar.
114  */
115 void curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
116                             const curve448_scalar_t s);
117
118 /*
119  * Add two scalars. |a|, |b| and |out| may alias each other.
120  * 
121  * a (in): One scalar.
122  * b (in): Another scalar.
123  * out (out): a+b.
124  */
125 void curve448_scalar_add(curve448_scalar_t out,
126                          const curve448_scalar_t a, const curve448_scalar_t b);
127
128 /*
129  * Subtract two scalars.  |a|, |b| and |out| may alias each other.
130  * a (in): One scalar.
131  * b (in): Another scalar.
132  * out (out): a-b.
133  */
134 void curve448_scalar_sub(curve448_scalar_t out,
135                          const curve448_scalar_t a, const curve448_scalar_t b);
136
137 /*
138  * Multiply two scalars. |a|, |b| and |out| may alias each other.
139  * 
140  * a (in): One scalar.
141  * b (in): Another scalar.
142  * out (out): a*b.
143  */
144 void curve448_scalar_mul(curve448_scalar_t out,
145                          const curve448_scalar_t a, const curve448_scalar_t b);
146
147 /*
148 * Halve a scalar.  |a| and |out| may alias each other.
149
150 * a (in): A scalar.
151 * out (out): a/2.
152 */
153 void curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a);
154
155 /*
156  * Copy a scalar.  The scalars may use the same memory, in which case this
157  * function does nothing.
158  * 
159  * a (in): A scalar.
160  * out (out): Will become a copy of a.
161  */
162 static ossl_inline void curve448_scalar_copy(curve448_scalar_t out,
163                                              const curve448_scalar_t a)
164 {
165     *out = *a;
166 }
167
168 /*
169  * Copy a point.  The input and output may alias, in which case this function
170  * does nothing.
171  *
172  * a (out): A copy of the point.
173  * b (in): Any point.
174  */
175 static ossl_inline void curve448_point_copy(curve448_point_t a,
176                                             const curve448_point_t b)
177 {
178     *a = *b;
179 }
180
181 /*
182  * Test whether two points are equal.  If yes, return C448_TRUE, else return
183  * C448_FALSE.
184  *
185  * a (in): A point.
186  * b (in): Another point.
187  * 
188  * Returns:
189  * C448_TRUE: The points are equal.
190  * C448_FALSE: The points are not equal.
191  */
192 __owur c448_bool_t curve448_point_eq(const curve448_point_t a,
193                                      const curve448_point_t b);
194
195 /*
196  * Double a point. Equivalent to curve448_point_add(two_a,a,a), but potentially
197  * faster.
198  *
199  * two_a (out): The sum a+a.
200  * a (in): A point.
201  */
202 void curve448_point_double(curve448_point_t two_a, const curve448_point_t a);
203
204 /*
205  * RFC 7748 Diffie-Hellman scalarmul.  This function uses a different
206  * (non-Decaf) encoding.
207  *
208  * out (out): The scaled point base*scalar
209  * base (in): The point to be scaled.
210  * scalar (in): The scalar to multiply by.
211  *
212  * Returns:
213  * C448_SUCCESS: The scalarmul succeeded.
214  * C448_FAILURE: The scalarmul didn't succeed, because the base point is in a
215  * small subgroup.
216  */
217 __owur c448_error_t x448_int(uint8_t out[X448_PUBLIC_BYTES],
218                              const uint8_t base[X448_PUBLIC_BYTES],
219                              const uint8_t scalar[X448_PRIVATE_BYTES]);
220
221 /*
222  * Multiply a point by X448_ENCODE_RATIO, then encode it like RFC 7748.
223  *
224  * This function is mainly used internally, but is exported in case
225  * it will be useful.
226  *
227  * The ratio is necessary because the internal representation doesn't
228  * track the cofactor information, so on output we must clear the cofactor.
229  * This would multiply by the cofactor, but in fact internally points are always
230  * even, so it multiplies by half the cofactor instead.
231  *
232  * As it happens, this aligns with the base point definitions; that is,
233  * if you pass the Decaf/Ristretto base point to this function, the result
234  * will be X448_ENCODE_RATIO times the X448
235  * base point.
236  *
237  * out (out): The scaled and encoded point.
238  * p (in): The point to be scaled and encoded.
239  */
240 void curve448_point_mul_by_ratio_and_encode_like_x448(
241                                         uint8_t out[X448_PUBLIC_BYTES],
242                                         const curve448_point_t p);
243
244 /*
245  * RFC 7748 Diffie-Hellman base point scalarmul.  This function uses a different
246  * (non-Decaf) encoding.
247  * 
248  * out (out): The scaled point base*scalar
249  * scalar (in): The scalar to multiply by.
250  */
251 void x448_derive_public_key(uint8_t out[X448_PUBLIC_BYTES],
252                             const uint8_t scalar[X448_PRIVATE_BYTES]);
253
254 /*
255  * Multiply a precomputed base point by a scalar: out = scalar*base.
256  *
257  * scaled (out): The scaled point base*scalar
258  * base (in): The point to be scaled.
259  * scalar (in): The scalar to multiply by.
260  */
261 void curve448_precomputed_scalarmul(curve448_point_t scaled,
262                                     const curve448_precomputed_s * base,
263                                     const curve448_scalar_t scalar);
264
265 /*
266  * Multiply two base points by two scalars:
267  * combo = scalar1*curve448_point_base + scalar2*base2.
268  *
269  * Otherwise equivalent to curve448_point_double_scalarmul, but may be
270  * faster at the expense of being variable time.
271  *
272  * combo (out): The linear combination scalar1*base + scalar2*base2.
273  * scalar1 (in): A first scalar to multiply by.
274  * base2 (in): A second point to be scaled.
275  * scalar2 (in) A second scalar to multiply by.
276  *
277  * Warning: This function takes variable time, and may leak the scalars used. 
278  * It is designed for signature verification.
279  */
280 void curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
281                                                const curve448_scalar_t scalar1,
282                                                const curve448_point_t base2,
283                                                const curve448_scalar_t scalar2);
284
285 /*
286  * Test that a point is valid, for debugging purposes.
287  *
288  * to_test (in): The point to test.
289  *
290  * Returns:
291  * C448_TRUE The point is valid.
292  * C448_FALSE The point is invalid.
293  */
294 __owur c448_bool_t curve448_point_valid(const curve448_point_t to_test);
295
296 /* Overwrite scalar with zeros. */
297 void curve448_scalar_destroy(curve448_scalar_t scalar);
298
299 /* Overwrite point with zeros. */
300 void curve448_point_destroy(curve448_point_t point);
301
302 #endif                          /* HEADER_POINT_448_H */