2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2015-2016 Cryptography Research, Inc.
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
10 * Originally written by Mike Hamburg
13 #ifndef __DECAF_POINT_448_H__
14 # define __DECAF_POINT_448_H__ 1
16 # include "curve448utils.h"
23 # define DECAF_448_SCALAR_LIMBS ((446-1)/DECAF_WORD_BITS+1)
25 /* The number of bits in a scalar */
26 # define DECAF_448_SCALAR_BITS 446
28 /* Number of bytes in a serialized point. */
29 # define DECAF_448_SER_BYTES 56
32 * Number of bytes in an elligated point. For now set the same as SER_BYTES
33 * but could be different for other curves.
35 # define DECAF_448_HASH_BYTES 56
37 /* Number of bytes in a serialized scalar. */
38 # define DECAF_448_SCALAR_BYTES 56
40 /* Number of bits in the "which" field of an elligator inverse */
41 # define DECAF_448_INVERT_ELLIGATOR_WHICH_BITS 3
43 /* The cofactor the curve would have, if we hadn't removed it */
44 # define DECAF_448_REMOVED_COFACTOR 4
46 /* X448 encoding ratio. */
47 # define DECAF_X448_ENCODE_RATIO 2
49 /* Number of bytes in an x448 public key */
50 # define DECAF_X448_PUBLIC_BYTES 56
52 /* Number of bytes in an x448 private key */
53 # define DECAF_X448_PRIVATE_BYTES 56
55 /* Twisted Edwards extended homogeneous coordinates */
56 typedef struct curve448_point_s {
58 } curve448_point_t[1];
60 /* Precomputed table based on a point. Can be trivial implementation. */
61 struct curve448_precomputed_s;
63 /* Precomputed table based on a point. Can be trivial implementation. */
64 typedef struct curve448_precomputed_s curve448_precomputed_s;
66 /* Scalar is stored packed, because we don't need the speed. */
67 typedef struct curve448_scalar_s {
68 decaf_word_t limb[DECAF_448_SCALAR_LIMBS];
69 } curve448_scalar_t[1];
71 /* A scalar equal to 1. */
72 extern const curve448_scalar_t curve448_scalar_one;
74 /* A scalar equal to 0. */
75 extern const curve448_scalar_t curve448_scalar_zero;
77 /* The identity point on the curve. */
78 extern const curve448_point_t curve448_point_identity;
80 /* An arbitrarily chosen base point on the curve. */
81 extern const curve448_point_t curve448_point_base;
83 /* Precomputed table for the base point on the curve. */
84 extern const struct curve448_precomputed_s *curve448_precomputed_base;
87 * Read a scalar from wire format or from bytes.
89 * ser (in): Serialized form of a scalar.
90 * out (out): Deserialized form.
93 * DECAF_SUCCESS: The scalar was correctly encoded.
94 * DECAF_FAILURE: The scalar was greater than the modulus, and has been reduced
95 * modulo that modulus.
97 __owur decaf_error_t curve448_scalar_decode(
98 curve448_scalar_t out,
99 const unsigned char ser[DECAF_448_SCALAR_BYTES]);
102 * Read a scalar from wire format or from bytes. Reduces mod scalar prime.
104 * ser (in): Serialized form of a scalar.
105 * ser_len (in): Length of serialized form.
106 * out (out): Deserialized form.
108 void curve448_scalar_decode_long(curve448_scalar_t out,
109 const unsigned char *ser, size_t ser_len);
112 * Serialize a scalar to wire format.
114 * ser (out): Serialized form of a scalar.
115 * s (in): Deserialized scalar.
117 void curve448_scalar_encode(unsigned char ser[DECAF_448_SCALAR_BYTES],
118 const curve448_scalar_t s);
121 * Add two scalars. The scalars may use the same memory.
123 * a (in): One scalar.
124 * b (in): Another scalar.
127 void curve448_scalar_add(curve448_scalar_t out,
128 const curve448_scalar_t a, const curve448_scalar_t b);
131 * Subtract two scalars. The scalars may use the same memory.
132 * a (in): One scalar.
133 * b (in): Another scalar.
136 void curve448_scalar_sub(curve448_scalar_t out,
137 const curve448_scalar_t a, const curve448_scalar_t b);
140 * Multiply two scalars. The scalars may use the same memory.
142 * a (in): One scalar.
143 * b (in): Another scalar.
146 void curve448_scalar_mul(curve448_scalar_t out,
147 const curve448_scalar_t a, const curve448_scalar_t b);
150 * Halve a scalar. The scalars may use the same memory.
155 void curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a);
158 * Copy a scalar. The scalars may use the same memory, in which case this
159 * function does nothing.
162 * out (out): Will become a copy of a.
164 static ossl_inline void curve448_scalar_copy(curve448_scalar_t out,
165 const curve448_scalar_t a)
171 * Copy a point. The input and output may alias, in which case this function
174 * a (out): A copy of the point.
177 static ossl_inline void curve448_point_copy(curve448_point_t a,
178 const curve448_point_t b)
184 * Test whether two points are equal. If yes, return DECAF_TRUE, else return
188 * b (in): Another point.
191 * DECAF_TRUE: The points are equal.
192 * DECAF_FALSE: The points are not equal.
194 __owur decaf_bool_t curve448_point_eq(const curve448_point_t a,
195 const curve448_point_t b);
198 * Double a point. Equivalent to curve448_point_add(two_a,a,a), but potentially
201 * two_a (out): The sum a+a.
204 void curve448_point_double(curve448_point_t two_a, const curve448_point_t a);
207 * RFC 7748 Diffie-Hellman scalarmul. This function uses a different
208 * (non-Decaf) encoding.
210 * out (out): The scaled point base*scalar
211 * base (in): The point to be scaled.
212 * scalar (in): The scalar to multiply by.
215 * DECAF_SUCCESS: The scalarmul succeeded.
216 * DECAF_FAILURE: The scalarmul didn't succeed, because the base point is in a
219 __owur decaf_error_t decaf_x448(uint8_t out[DECAF_X448_PUBLIC_BYTES],
220 const uint8_t base[DECAF_X448_PUBLIC_BYTES],
221 const uint8_t scalar[DECAF_X448_PRIVATE_BYTES]);
224 * Multiply a point by DECAF_X448_ENCODE_RATIO, then encode it like RFC 7748.
226 * This function is mainly used internally, but is exported in case
229 * The ratio is necessary because the internal representation doesn't
230 * track the cofactor information, so on output we must clear the cofactor.
231 * This would multiply by the cofactor, but in fact internally libdecaf's
232 * points are always even, so it multiplies by half the cofactor instead.
234 * As it happens, this aligns with the base point definitions; that is,
235 * if you pass the Decaf/Ristretto base point to this function, the result
236 * will be DECAF_X448_ENCODE_RATIO times the X448
239 * out (out): The scaled and encoded point.
240 * p (in): The point to be scaled and encoded.
242 void curve448_point_mul_by_ratio_and_encode_like_x448(
243 uint8_t out[DECAF_X448_PUBLIC_BYTES],
244 const curve448_point_t p);
246 /* The base point for X448 Diffie-Hellman */
247 extern const uint8_t decaf_x448_base_point[DECAF_X448_PUBLIC_BYTES];
250 * RFC 7748 Diffie-Hellman base point scalarmul. This function uses a different
251 * (non-Decaf) encoding.
253 * out (out): The scaled point base*scalar
254 * scalar (in): The scalar to multiply by.
256 void decaf_x448_derive_public_key(
257 uint8_t out[DECAF_X448_PUBLIC_BYTES],
258 const uint8_t scalar[DECAF_X448_PRIVATE_BYTES]);
261 * Multiply a precomputed base point by a scalar: out = scalar*base.
263 * scaled (out): The scaled point base*scalar
264 * base (in): The point to be scaled.
265 * scalar (in): The scalar to multiply by.
267 void curve448_precomputed_scalarmul(curve448_point_t scaled,
268 const curve448_precomputed_s * base,
269 const curve448_scalar_t scalar);
272 * Multiply two base points by two scalars:
273 * combo = scalar1*curve448_point_base + scalar2*base2.
275 * Otherwise equivalent to curve448_point_double_scalarmul, but may be
276 * faster at the expense of being variable time.
278 * combo (out): The linear combination scalar1*base + scalar2*base2.
279 * scalar1 (in): A first scalar to multiply by.
280 * base2 (in): A second point to be scaled.
281 * scalar2 (in) A second scalar to multiply by.
283 * Warning: This function takes variable time, and may leak the scalars used.
284 * It is designed for signature verification.
286 void curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
287 const curve448_scalar_t scalar1,
288 const curve448_point_t base2,
289 const curve448_scalar_t scalar2);
292 * Test that a point is valid, for debugging purposes.
294 * to_test (in): The point to test.
297 * DECAF_TRUE The point is valid.
298 * DECAF_FALSE The point is invalid.
300 __owur decaf_bool_t curve448_point_valid(const curve448_point_t to_test);
302 /* Overwrite scalar with zeros. */
303 void curve448_scalar_destroy(curve448_scalar_t scalar);
305 /* Overwrite point with zeros. */
306 void curve448_point_destroy(curve448_point_t point);
312 #endif /* __DECAF_POINT_448_H__ */