2 * @file decaf/point_448.h
6 * Copyright (c) 2015-2016 Cryptography Research, Inc. \n
7 * Released under the MIT License. See LICENSE.txt for license information.
9 * @brief A group of prime order p, based on Ed448-Goldilocks.
11 * @warning This file was automatically generated in Python.
12 * Please do not edit it.
15 #ifndef __DECAF_POINT_448_H__
16 #define __DECAF_POINT_448_H__ 1
18 #include "curve448utils.h"
26 #define DECAF_448_SCALAR_LIMBS ((446-1)/DECAF_WORD_BITS+1)
29 /** The number of bits in a scalar */
30 #define DECAF_448_SCALAR_BITS 446
32 /** Number of bytes in a serialized point. */
33 #define DECAF_448_SER_BYTES 56
35 /** Number of bytes in an elligated point. For now set the same as SER_BYTES
36 * but could be different for other curves.
38 #define DECAF_448_HASH_BYTES 56
40 /** Number of bytes in a serialized scalar. */
41 #define DECAF_448_SCALAR_BYTES 56
43 /** Number of bits in the "which" field of an elligator inverse */
44 #define DECAF_448_INVERT_ELLIGATOR_WHICH_BITS 3
46 /** The cofactor the curve would have, if we hadn't removed it */
47 #define DECAF_448_REMOVED_COFACTOR 4
49 /** X448 encoding ratio. */
50 #define DECAF_X448_ENCODE_RATIO 2
52 /** Number of bytes in an x448 public key */
53 #define DECAF_X448_PUBLIC_BYTES 56
55 /** Number of bytes in an x448 private key */
56 #define DECAF_X448_PRIVATE_BYTES 56
58 /** Twisted Edwards extended homogeneous coordinates */
59 typedef struct curve448_point_s {
63 } curve448_point_t[1];
65 /** Precomputed table based on a point. Can be trivial implementation. */
66 struct curve448_precomputed_s;
68 /** Precomputed table based on a point. Can be trivial implementation. */
69 typedef struct curve448_precomputed_s curve448_precomputed_s;
71 /** Scalar is stored packed, because we don't need the speed. */
72 typedef struct curve448_scalar_s {
74 decaf_word_t limb[DECAF_448_SCALAR_LIMBS];
76 } curve448_scalar_t[1];
78 /** A scalar equal to 1. */
79 extern const curve448_scalar_t curve448_scalar_one DECAF_API_VIS;
81 /** A scalar equal to 0. */
82 extern const curve448_scalar_t curve448_scalar_zero DECAF_API_VIS;
84 /** The identity point on the curve. */
85 extern const curve448_point_t curve448_point_identity DECAF_API_VIS;
87 /** An arbitrarily chosen base point on the curve. */
88 extern const curve448_point_t curve448_point_base DECAF_API_VIS;
90 /** Precomputed table for the base point on the curve. */
91 extern const struct curve448_precomputed_s *curve448_precomputed_base DECAF_API_VIS;
94 * @brief Read a scalar from wire format or from bytes.
96 * @param [in] ser Serialized form of a scalar.
97 * @param [out] out Deserialized form.
99 * @retval DECAF_SUCCESS The scalar was correctly encoded.
100 * @retval DECAF_FAILURE The scalar was greater than the modulus,
101 * and has been reduced modulo that modulus.
103 decaf_error_t curve448_scalar_decode (
104 curve448_scalar_t out,
105 const unsigned char ser[DECAF_448_SCALAR_BYTES]
106 ) DECAF_API_VIS DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
109 * @brief Read a scalar from wire format or from bytes. Reduces mod
112 * @param [in] ser Serialized form of a scalar.
113 * @param [in] ser_len Length of serialized form.
114 * @param [out] out Deserialized form.
116 void curve448_scalar_decode_long (
117 curve448_scalar_t out,
118 const unsigned char *ser,
120 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
123 * @brief Serialize a scalar to wire format.
125 * @param [out] ser Serialized form of a scalar.
126 * @param [in] s Deserialized scalar.
128 void curve448_scalar_encode (
129 unsigned char ser[DECAF_448_SCALAR_BYTES],
130 const curve448_scalar_t s
131 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE DECAF_NOINLINE;
134 * @brief Add two scalars. The scalars may use the same memory.
135 * @param [in] a One scalar.
136 * @param [in] b Another scalar.
137 * @param [out] out a+b.
139 void curve448_scalar_add (
140 curve448_scalar_t out,
141 const curve448_scalar_t a,
142 const curve448_scalar_t b
143 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
146 * @brief Compare two scalars.
147 * @param [in] a One scalar.
148 * @param [in] b Another scalar.
149 * @retval DECAF_TRUE The scalars are equal.
150 * @retval DECAF_FALSE The scalars are not equal.
152 decaf_bool_t curve448_scalar_eq (
153 const curve448_scalar_t a,
154 const curve448_scalar_t b
155 ) DECAF_API_VIS DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
158 * @brief Subtract two scalars. The scalars may use the same memory.
159 * @param [in] a One scalar.
160 * @param [in] b Another scalar.
161 * @param [out] out a-b.
163 void curve448_scalar_sub (
164 curve448_scalar_t out,
165 const curve448_scalar_t a,
166 const curve448_scalar_t b
167 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
170 * @brief Multiply two scalars. The scalars may use the same memory.
171 * @param [in] a One scalar.
172 * @param [in] b Another scalar.
173 * @param [out] out a*b.
175 void curve448_scalar_mul (
176 curve448_scalar_t out,
177 const curve448_scalar_t a,
178 const curve448_scalar_t b
179 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
182 * @brief Halve a scalar. The scalars may use the same memory.
183 * @param [in] a A scalar.
184 * @param [out] out a/2.
186 void curve448_scalar_halve (
187 curve448_scalar_t out,
188 const curve448_scalar_t a
189 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
192 * @brief Invert a scalar. When passed zero, return 0. The input and output may alias.
193 * @param [in] a A scalar.
194 * @param [out] out 1/a.
195 * @return DECAF_SUCCESS The input is nonzero.
197 decaf_error_t curve448_scalar_invert (
198 curve448_scalar_t out,
199 const curve448_scalar_t a
200 ) DECAF_API_VIS DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
203 * @brief Copy a scalar. The scalars may use the same memory, in which
204 * case this function does nothing.
205 * @param [in] a A scalar.
206 * @param [out] out Will become a copy of a.
208 static inline void DECAF_NONNULL curve448_scalar_copy (
209 curve448_scalar_t out,
210 const curve448_scalar_t a
216 * @brief Set a scalar to an unsigned 64-bit integer.
217 * @param [in] a An integer.
218 * @param [out] out Will become equal to a.
220 void curve448_scalar_set_unsigned (
221 curve448_scalar_t out,
223 ) DECAF_API_VIS DECAF_NONNULL;
226 * @brief Copy a point. The input and output may alias,
227 * in which case this function does nothing.
229 * @param [out] a A copy of the point.
230 * @param [in] b Any point.
232 static inline void DECAF_NONNULL curve448_point_copy (
234 const curve448_point_t b
240 * @brief Test whether two points are equal. If yes, return
241 * DECAF_TRUE, else return DECAF_FALSE.
243 * @param [in] a A point.
244 * @param [in] b Another point.
245 * @retval DECAF_TRUE The points are equal.
246 * @retval DECAF_FALSE The points are not equal.
248 decaf_bool_t curve448_point_eq (
249 const curve448_point_t a,
250 const curve448_point_t b
251 ) DECAF_API_VIS DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
254 * @brief Double a point. Equivalent to
255 * curve448_point_add(two_a,a,a), but potentially faster.
257 * @param [out] two_a The sum a+a.
258 * @param [in] a A point.
260 void curve448_point_double (
261 curve448_point_t two_a,
262 const curve448_point_t a
263 ) DECAF_API_VIS DECAF_NONNULL;
266 * @brief RFC 7748 Diffie-Hellman scalarmul. This function uses a different
267 * (non-Decaf) encoding.
269 * @param [out] scaled The scaled point base*scalar
270 * @param [in] base The point to be scaled.
271 * @param [in] scalar The scalar to multiply by.
273 * @retval DECAF_SUCCESS The scalarmul succeeded.
274 * @retval DECAF_FAILURE The scalarmul didn't succeed, because the base
275 * point is in a small subgroup.
277 decaf_error_t decaf_x448 (
278 uint8_t out[DECAF_X448_PUBLIC_BYTES],
279 const uint8_t base[DECAF_X448_PUBLIC_BYTES],
280 const uint8_t scalar[DECAF_X448_PRIVATE_BYTES]
281 ) DECAF_API_VIS DECAF_NONNULL DECAF_WARN_UNUSED DECAF_NOINLINE;
284 * @brief Multiply a point by DECAF_X448_ENCODE_RATIO,
285 * then encode it like RFC 7748.
287 * This function is mainly used internally, but is exported in case
290 * The ratio is necessary because the internal representation doesn't
291 * track the cofactor information, so on output we must clear the cofactor.
292 * This would multiply by the cofactor, but in fact internally libdecaf's
293 * points are always even, so it multiplies by half the cofactor instead.
295 * As it happens, this aligns with the base point definitions; that is,
296 * if you pass the Decaf/Ristretto base point to this function, the result
297 * will be DECAF_X448_ENCODE_RATIO times the X448
300 * @param [out] out The scaled and encoded point.
301 * @param [in] p The point to be scaled and encoded.
303 void curve448_point_mul_by_ratio_and_encode_like_x448 (
304 uint8_t out[DECAF_X448_PUBLIC_BYTES],
305 const curve448_point_t p
306 ) DECAF_API_VIS DECAF_NONNULL;
308 /** The base point for X448 Diffie-Hellman */
309 extern const uint8_t decaf_x448_base_point[DECAF_X448_PUBLIC_BYTES] DECAF_API_VIS;
312 * @brief RFC 7748 Diffie-Hellman base point scalarmul. This function uses
313 * a different (non-Decaf) encoding.
315 * Does exactly the same thing as decaf_x448_generate_key,
316 * but has a better name.
318 * @param [out] scaled The scaled point base*scalar
319 * @param [in] scalar The scalar to multiply by.
321 void decaf_x448_derive_public_key (
322 uint8_t out[DECAF_X448_PUBLIC_BYTES],
323 const uint8_t scalar[DECAF_X448_PRIVATE_BYTES]
324 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
326 /* FUTURE: uint8_t curve448_encode_like_curve448) */
329 * @brief Precompute a table for fast scalar multiplication.
330 * Some implementations do not include precomputed points; for
331 * those implementations, this implementation simply copies the
334 * @param [out] a A precomputed table of multiples of the point.
335 * @param [in] b Any point.
337 void curve448_precompute (
338 curve448_precomputed_s *a,
339 const curve448_point_t b
340 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
343 * @brief Multiply a precomputed base point by a scalar:
344 * scaled = scalar*base.
345 * Some implementations do not include precomputed points; for
346 * those implementations, this function is the same as
347 * curve448_point_scalarmul
349 * @param [out] scaled The scaled point base*scalar
350 * @param [in] base The point to be scaled.
351 * @param [in] scalar The scalar to multiply by.
353 void curve448_precomputed_scalarmul (
354 curve448_point_t scaled,
355 const curve448_precomputed_s *base,
356 const curve448_scalar_t scalar
357 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
361 * @brief Multiply two base points by two scalars:
362 * scaled = scalar1*curve448_point_base + scalar2*base2.
364 * Otherwise equivalent to curve448_point_double_scalarmul, but may be
365 * faster at the expense of being variable time.
367 * @param [out] combo The linear combination scalar1*base + scalar2*base2.
368 * @param [in] scalar1 A first scalar to multiply by.
369 * @param [in] base2 A second point to be scaled.
370 * @param [in] scalar2 A second scalar to multiply by.
372 * @warning: This function takes variable time, and may leak the scalars
373 * used. It is designed for signature verification.
375 void curve448_base_double_scalarmul_non_secret (
376 curve448_point_t combo,
377 const curve448_scalar_t scalar1,
378 const curve448_point_t base2,
379 const curve448_scalar_t scalar2
380 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
383 * @brief Constant-time decision between two scalars. If pick_b
384 * is zero, out = a; else out = b.
386 * @param [out] out The output. It may be the same as either input.
387 * @param [in] a Any scalar.
388 * @param [in] b Any scalar.
389 * @param [in] pick_b If nonzero, choose scalar b.
391 void curve448_scalar_cond_sel (
392 curve448_scalar_t out,
393 const curve448_scalar_t a,
394 const curve448_scalar_t b,
396 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
399 * @brief Test that a point is valid, for debugging purposes.
401 * @param [in] to_test The point to test.
402 * @retval DECAF_TRUE The point is valid.
403 * @retval DECAF_FALSE The point is invalid.
405 decaf_bool_t curve448_point_valid (
406 const curve448_point_t to_test
407 ) DECAF_API_VIS DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
411 * @brief Almost-Elligator-like hash to curve.
413 * Call this function with the output of a hash to make a hash to the curve.
415 * This function runs Elligator2 on the curve448 Jacobi quartic model. It then
416 * uses the isogeny to put the result in twisted Edwards form. As a result,
417 * it is safe (cannot produce points of order 4), and would be compatible with
418 * hypothetical other implementations of Decaf using a Montgomery or untwisted
421 * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
422 * A factor of 2 due to the isogeny.
423 * A factor of 2 because we quotient out the 2-torsion.
425 * This makes it about 8:1 overall, or 16:1 overall on curves with cofactor 8.
427 * Negating the input (mod q) results in the same point. Inverting the input
428 * (mod q) results in the negative point. This is the same as Elligator.
430 * This function isn't quite indifferentiable from a random oracle.
431 * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
432 * Furthermore, calling it twice with independent seeds and adding the results
433 * is indifferentiable from a random oracle.
435 * @param [in] hashed_data Output of some hash function.
436 * @param [out] pt The data hashed to the curve.
439 curve448_point_from_hash_nonuniform (
441 const unsigned char hashed_data[DECAF_448_HASH_BYTES]
442 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
445 * @brief Indifferentiable hash function encoding to curve.
447 * Equivalent to calling curve448_point_from_hash_nonuniform twice and adding.
449 * @param [in] hashed_data Output of some hash function.
450 * @param [out] pt The data hashed to the curve.
452 void curve448_point_from_hash_uniform (
454 const unsigned char hashed_data[2*DECAF_448_HASH_BYTES]
455 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE;
458 * @brief Inverse of elligator-like hash to curve.
460 * This function writes to the buffer, to make it so that
461 * curve448_point_from_hash_nonuniform(buffer) = pt if
462 * possible. Since there may be multiple preimages, the
463 * "which" parameter chooses between them. To ensure uniform
464 * inverse sampling, this function succeeds or fails
465 * independently for different "which" values.
467 * This function isn't guaranteed to find every possible
468 * preimage, but it finds all except a small finite number.
469 * In particular, when the number of bits in the modulus isn't
470 * a multiple of 8 (i.e. for curve25519), it sets the high bits
471 * independently, which enables the generated data to be uniform.
472 * But it doesn't add p, so you'll never get exactly p from this
473 * function. This might change in the future, especially if
474 * we ever support eg Brainpool curves, where this could cause
475 * real nonuniformity.
477 * @param [out] recovered_hash Encoded data.
478 * @param [in] pt The point to encode.
479 * @param [in] which A value determining which inverse point
482 * @retval DECAF_SUCCESS The inverse succeeded.
483 * @retval DECAF_FAILURE The inverse failed.
486 curve448_invert_elligator_nonuniform (
487 unsigned char recovered_hash[DECAF_448_HASH_BYTES],
488 const curve448_point_t pt,
490 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE DECAF_WARN_UNUSED;
493 * @brief Inverse of elligator-like hash to curve.
495 * This function writes to the buffer, to make it so that
496 * curve448_point_from_hash_uniform(buffer) = pt if
497 * possible. Since there may be multiple preimages, the
498 * "which" parameter chooses between them. To ensure uniform
499 * inverse sampling, this function succeeds or fails
500 * independently for different "which" values.
502 * @param [out] recovered_hash Encoded data.
503 * @param [in] pt The point to encode.
504 * @param [in] which A value determining which inverse point
507 * @retval DECAF_SUCCESS The inverse succeeded.
508 * @retval DECAF_FAILURE The inverse failed.
511 curve448_invert_elligator_uniform (
512 unsigned char recovered_hash[2*DECAF_448_HASH_BYTES],
513 const curve448_point_t pt,
515 ) DECAF_API_VIS DECAF_NONNULL DECAF_NOINLINE DECAF_WARN_UNUSED;
518 * @brief Overwrite scalar with zeros.
520 void curve448_scalar_destroy (
521 curve448_scalar_t scalar
522 ) DECAF_NONNULL DECAF_API_VIS;
525 * @brief Overwrite point with zeros.
527 void curve448_point_destroy (
528 curve448_point_t point
529 ) DECAF_NONNULL DECAF_API_VIS;
535 #endif /* __DECAF_POINT_448_H__ */