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