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 HEADER_POINT_448_H
14 # define HEADER_POINT_448_H
16 # include "curve448utils.h"
20 # define C448_SCALAR_LIMBS ((446-1)/C448_WORD_BITS+1)
22 /* The number of bits in a scalar */
23 # define C448_SCALAR_BITS 446
25 /* Number of bytes in a serialized scalar. */
26 # define C448_SCALAR_BYTES 56
28 /* X448 encoding ratio. */
29 # define X448_ENCODE_RATIO 2
31 /* Number of bytes in an x448 public key */
32 # define X448_PUBLIC_BYTES 56
34 /* Number of bytes in an x448 private key */
35 # define X448_PRIVATE_BYTES 56
37 /* Twisted Edwards extended homogeneous coordinates */
38 typedef struct curve448_point_s
{
40 } curve448_point_t
[1];
42 /* Precomputed table based on a point. Can be trivial implementation. */
43 struct curve448_precomputed_s
;
45 /* Precomputed table based on a point. Can be trivial implementation. */
46 typedef struct curve448_precomputed_s curve448_precomputed_s
;
48 /* Scalar is stored packed, because we don't need the speed. */
49 typedef struct curve448_scalar_s
{
50 c448_word_t limb
[C448_SCALAR_LIMBS
];
51 } curve448_scalar_t
[1];
53 /* A scalar equal to 1. */
54 extern const curve448_scalar_t curve448_scalar_one
;
56 /* A scalar equal to 0. */
57 extern const curve448_scalar_t curve448_scalar_zero
;
59 /* The identity point on the curve. */
60 extern const curve448_point_t curve448_point_identity
;
62 /* Precomputed table for the base point on the curve. */
63 extern const struct curve448_precomputed_s
*curve448_precomputed_base
;
66 * Read a scalar from wire format or from bytes.
68 * ser (in): Serialized form of a scalar.
69 * out (out): Deserialized form.
72 * C448_SUCCESS: The scalar was correctly encoded.
73 * C448_FAILURE: The scalar was greater than the modulus, and has been reduced
74 * modulo that modulus.
76 __owur c448_error_t
curve448_scalar_decode(
77 curve448_scalar_t out
,
78 const unsigned char ser
[C448_SCALAR_BYTES
]);
81 * Read a scalar from wire format or from bytes. Reduces mod scalar prime.
83 * ser (in): Serialized form of a scalar.
84 * ser_len (in): Length of serialized form.
85 * out (out): Deserialized form.
87 void curve448_scalar_decode_long(curve448_scalar_t out
,
88 const unsigned char *ser
, size_t ser_len
);
91 * Serialize a scalar to wire format.
93 * ser (out): Serialized form of a scalar.
94 * s (in): Deserialized scalar.
96 void curve448_scalar_encode(unsigned char ser
[C448_SCALAR_BYTES
],
97 const curve448_scalar_t s
);
100 * Add two scalars. The scalars may use the same memory.
102 * a (in): One scalar.
103 * b (in): Another scalar.
106 void curve448_scalar_add(curve448_scalar_t out
,
107 const curve448_scalar_t a
, const curve448_scalar_t b
);
110 * Subtract two scalars. The scalars may use the same memory.
111 * a (in): One scalar.
112 * b (in): Another scalar.
115 void curve448_scalar_sub(curve448_scalar_t out
,
116 const curve448_scalar_t a
, const curve448_scalar_t b
);
119 * Multiply two scalars. The scalars may use the same memory.
121 * a (in): One scalar.
122 * b (in): Another scalar.
125 void curve448_scalar_mul(curve448_scalar_t out
,
126 const curve448_scalar_t a
, const curve448_scalar_t b
);
129 * Halve a scalar. The scalars may use the same memory.
134 void curve448_scalar_halve(curve448_scalar_t out
, const curve448_scalar_t a
);
137 * Copy a scalar. The scalars may use the same memory, in which case this
138 * function does nothing.
141 * out (out): Will become a copy of a.
143 static ossl_inline
void curve448_scalar_copy(curve448_scalar_t out
,
144 const curve448_scalar_t a
)
150 * Copy a point. The input and output may alias, in which case this function
153 * a (out): A copy of the point.
156 static ossl_inline
void curve448_point_copy(curve448_point_t a
,
157 const curve448_point_t b
)
163 * Test whether two points are equal. If yes, return C448_TRUE, else return
167 * b (in): Another point.
170 * C448_TRUE: The points are equal.
171 * C448_FALSE: The points are not equal.
173 __owur c448_bool_t
curve448_point_eq(const curve448_point_t a
,
174 const curve448_point_t b
);
177 * Double a point. Equivalent to curve448_point_add(two_a,a,a), but potentially
180 * two_a (out): The sum a+a.
183 void curve448_point_double(curve448_point_t two_a
, const curve448_point_t a
);
186 * RFC 7748 Diffie-Hellman scalarmul. This function uses a different
187 * (non-Decaf) encoding.
189 * out (out): The scaled point base*scalar
190 * base (in): The point to be scaled.
191 * scalar (in): The scalar to multiply by.
194 * C448_SUCCESS: The scalarmul succeeded.
195 * C448_FAILURE: The scalarmul didn't succeed, because the base point is in a
198 __owur c448_error_t
x448_int(uint8_t out
[X448_PUBLIC_BYTES
],
199 const uint8_t base
[X448_PUBLIC_BYTES
],
200 const uint8_t scalar
[X448_PRIVATE_BYTES
]);
203 * Multiply a point by X448_ENCODE_RATIO, then encode it like RFC 7748.
205 * This function is mainly used internally, but is exported in case
208 * The ratio is necessary because the internal representation doesn't
209 * track the cofactor information, so on output we must clear the cofactor.
210 * This would multiply by the cofactor, but in fact internally points are always
211 * even, so it multiplies by half the cofactor instead.
213 * As it happens, this aligns with the base point definitions; that is,
214 * if you pass the Decaf/Ristretto base point to this function, the result
215 * will be X448_ENCODE_RATIO times the X448
218 * out (out): The scaled and encoded point.
219 * p (in): The point to be scaled and encoded.
221 void curve448_point_mul_by_ratio_and_encode_like_x448(
222 uint8_t out
[X448_PUBLIC_BYTES
],
223 const curve448_point_t p
);
226 * RFC 7748 Diffie-Hellman base point scalarmul. This function uses a different
227 * (non-Decaf) encoding.
229 * out (out): The scaled point base*scalar
230 * scalar (in): The scalar to multiply by.
232 void x448_derive_public_key(uint8_t out
[X448_PUBLIC_BYTES
],
233 const uint8_t scalar
[X448_PRIVATE_BYTES
]);
236 * Multiply a precomputed base point by a scalar: out = scalar*base.
238 * scaled (out): The scaled point base*scalar
239 * base (in): The point to be scaled.
240 * scalar (in): The scalar to multiply by.
242 void curve448_precomputed_scalarmul(curve448_point_t scaled
,
243 const curve448_precomputed_s
* base
,
244 const curve448_scalar_t scalar
);
247 * Multiply two base points by two scalars:
248 * combo = scalar1*curve448_point_base + scalar2*base2.
250 * Otherwise equivalent to curve448_point_double_scalarmul, but may be
251 * faster at the expense of being variable time.
253 * combo (out): The linear combination scalar1*base + scalar2*base2.
254 * scalar1 (in): A first scalar to multiply by.
255 * base2 (in): A second point to be scaled.
256 * scalar2 (in) A second scalar to multiply by.
258 * Warning: This function takes variable time, and may leak the scalars used.
259 * It is designed for signature verification.
261 void curve448_base_double_scalarmul_non_secret(curve448_point_t combo
,
262 const curve448_scalar_t scalar1
,
263 const curve448_point_t base2
,
264 const curve448_scalar_t scalar2
);
267 * Test that a point is valid, for debugging purposes.
269 * to_test (in): The point to test.
272 * C448_TRUE The point is valid.
273 * C448_FALSE The point is invalid.
275 __owur c448_bool_t
curve448_point_valid(const curve448_point_t to_test
);
277 /* Overwrite scalar with zeros. */
278 void curve448_scalar_destroy(curve448_scalar_t scalar
);
280 /* Overwrite point with zeros. */
281 void curve448_point_destroy(curve448_point_t point
);
283 #endif /* HEADER_POINT_448_H */