2 * Copyright 2017-2018 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 __C448_POINT_448_H__
14 # define __C448_POINT_448_H__ 1
16 # include "curve448utils.h"
23 # define C448_SCALAR_LIMBS ((446-1)/C448_WORD_BITS+1)
25 /* The number of bits in a scalar */
26 # define C448_SCALAR_BITS 446
28 /* Number of bytes in a serialized scalar. */
29 # define C448_SCALAR_BYTES 56
31 /* X448 encoding ratio. */
32 # define X448_ENCODE_RATIO 2
34 /* Number of bytes in an x448 public key */
35 # define X448_PUBLIC_BYTES 56
37 /* Number of bytes in an x448 private key */
38 # define X448_PRIVATE_BYTES 56
40 /* Twisted Edwards extended homogeneous coordinates */
41 typedef struct curve448_point_s {
43 } curve448_point_t[1];
45 /* Precomputed table based on a point. Can be trivial implementation. */
46 struct curve448_precomputed_s;
48 /* Precomputed table based on a point. Can be trivial implementation. */
49 typedef struct curve448_precomputed_s curve448_precomputed_s;
51 /* Scalar is stored packed, because we don't need the speed. */
52 typedef struct curve448_scalar_s {
53 c448_word_t limb[C448_SCALAR_LIMBS];
54 } curve448_scalar_t[1];
56 /* A scalar equal to 1. */
57 extern const curve448_scalar_t curve448_scalar_one;
59 /* A scalar equal to 0. */
60 extern const curve448_scalar_t curve448_scalar_zero;
62 /* The identity point on the curve. */
63 extern const curve448_point_t curve448_point_identity;
65 /* Precomputed table for the base point on the curve. */
66 extern const struct curve448_precomputed_s *curve448_precomputed_base;
69 * Read a scalar from wire format or from bytes.
71 * ser (in): Serialized form of a scalar.
72 * out (out): Deserialized form.
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.
79 __owur c448_error_t curve448_scalar_decode(
80 curve448_scalar_t out,
81 const unsigned char ser[C448_SCALAR_BYTES]);
84 * Read a scalar from wire format or from bytes. Reduces mod scalar prime.
86 * ser (in): Serialized form of a scalar.
87 * ser_len (in): Length of serialized form.
88 * out (out): Deserialized form.
90 void curve448_scalar_decode_long(curve448_scalar_t out,
91 const unsigned char *ser, size_t ser_len);
94 * Serialize a scalar to wire format.
96 * ser (out): Serialized form of a scalar.
97 * s (in): Deserialized scalar.
99 void curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
100 const curve448_scalar_t s);
103 * Add two scalars. The scalars may use the same memory.
105 * a (in): One scalar.
106 * b (in): Another scalar.
109 void curve448_scalar_add(curve448_scalar_t out,
110 const curve448_scalar_t a, const curve448_scalar_t b);
113 * Subtract two scalars. The scalars may use the same memory.
114 * a (in): One scalar.
115 * b (in): Another scalar.
118 void curve448_scalar_sub(curve448_scalar_t out,
119 const curve448_scalar_t a, const curve448_scalar_t b);
122 * Multiply two scalars. The scalars may use the same memory.
124 * a (in): One scalar.
125 * b (in): Another scalar.
128 void curve448_scalar_mul(curve448_scalar_t out,
129 const curve448_scalar_t a, const curve448_scalar_t b);
132 * Halve a scalar. The scalars may use the same memory.
137 void curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a);
140 * Copy a scalar. The scalars may use the same memory, in which case this
141 * function does nothing.
144 * out (out): Will become a copy of a.
146 static ossl_inline void curve448_scalar_copy(curve448_scalar_t out,
147 const curve448_scalar_t a)
153 * Copy a point. The input and output may alias, in which case this function
156 * a (out): A copy of the point.
159 static ossl_inline void curve448_point_copy(curve448_point_t a,
160 const curve448_point_t b)
166 * Test whether two points are equal. If yes, return C448_TRUE, else return
170 * b (in): Another point.
173 * C448_TRUE: The points are equal.
174 * C448_FALSE: The points are not equal.
176 __owur c448_bool_t curve448_point_eq(const curve448_point_t a,
177 const curve448_point_t b);
180 * Double a point. Equivalent to curve448_point_add(two_a,a,a), but potentially
183 * two_a (out): The sum a+a.
186 void curve448_point_double(curve448_point_t two_a, const curve448_point_t a);
189 * RFC 7748 Diffie-Hellman scalarmul. This function uses a different
190 * (non-Decaf) encoding.
192 * out (out): The scaled point base*scalar
193 * base (in): The point to be scaled.
194 * scalar (in): The scalar to multiply by.
197 * C448_SUCCESS: The scalarmul succeeded.
198 * C448_FAILURE: The scalarmul didn't succeed, because the base point is in a
201 __owur c448_error_t x448_int(uint8_t out[X448_PUBLIC_BYTES],
202 const uint8_t base[X448_PUBLIC_BYTES],
203 const uint8_t scalar[X448_PRIVATE_BYTES]);
206 * Multiply a point by X448_ENCODE_RATIO, then encode it like RFC 7748.
208 * This function is mainly used internally, but is exported in case
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.
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 X448_ENCODE_RATIO times the X448
221 * out (out): The scaled and encoded point.
222 * p (in): The point to be scaled and encoded.
224 void curve448_point_mul_by_ratio_and_encode_like_x448(
225 uint8_t out[X448_PUBLIC_BYTES],
226 const curve448_point_t p);
229 * RFC 7748 Diffie-Hellman base point scalarmul. This function uses a different
230 * (non-Decaf) encoding.
232 * out (out): The scaled point base*scalar
233 * scalar (in): The scalar to multiply by.
235 void x448_derive_public_key(uint8_t out[X448_PUBLIC_BYTES],
236 const uint8_t scalar[X448_PRIVATE_BYTES]);
239 * Multiply a precomputed base point by a scalar: out = scalar*base.
241 * scaled (out): The scaled point base*scalar
242 * base (in): The point to be scaled.
243 * scalar (in): The scalar to multiply by.
245 void curve448_precomputed_scalarmul(curve448_point_t scaled,
246 const curve448_precomputed_s * base,
247 const curve448_scalar_t scalar);
250 * Multiply two base points by two scalars:
251 * combo = scalar1*curve448_point_base + scalar2*base2.
253 * Otherwise equivalent to curve448_point_double_scalarmul, but may be
254 * faster at the expense of being variable time.
256 * combo (out): The linear combination scalar1*base + scalar2*base2.
257 * scalar1 (in): A first scalar to multiply by.
258 * base2 (in): A second point to be scaled.
259 * scalar2 (in) A second scalar to multiply by.
261 * Warning: This function takes variable time, and may leak the scalars used.
262 * It is designed for signature verification.
264 void curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
265 const curve448_scalar_t scalar1,
266 const curve448_point_t base2,
267 const curve448_scalar_t scalar2);
270 * Test that a point is valid, for debugging purposes.
272 * to_test (in): The point to test.
275 * C448_TRUE The point is valid.
276 * C448_FALSE The point is invalid.
278 __owur c448_bool_t curve448_point_valid(const curve448_point_t to_test);
280 /* Overwrite scalar with zeros. */
281 void curve448_scalar_destroy(curve448_scalar_t scalar);
283 /* Overwrite point with zeros. */
284 void curve448_point_destroy(curve448_point_t point);
290 #endif /* __C448_POINT_448_H__ */