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;
81 /** A scalar equal to 0. */
82 extern const curve448_scalar_t curve448_scalar_zero;
84 /** The identity point on the curve. */
85 extern const curve448_point_t curve448_point_identity;
87 /** An arbitrarily chosen base point on the curve. */
88 extern const curve448_point_t curve448_point_base;
90 /** Precomputed table for the base point on the curve. */
91 extern const struct curve448_precomputed_s *curve448_precomputed_base;
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 __owur decaf_error_t curve448_scalar_decode (
104 curve448_scalar_t out,
105 const unsigned char ser[DECAF_448_SCALAR_BYTES]
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,
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_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
146 * @brief Subtract two scalars. The scalars may use the same memory.
147 * @param [in] a One scalar.
148 * @param [in] b Another scalar.
149 * @param [out] out a-b.
151 void curve448_scalar_sub (
152 curve448_scalar_t out,
153 const curve448_scalar_t a,
154 const curve448_scalar_t b
158 * @brief Multiply 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_mul (
164 curve448_scalar_t out,
165 const curve448_scalar_t a,
166 const curve448_scalar_t b
170 * @brief Halve a scalar. The scalars may use the same memory.
171 * @param [in] a A scalar.
172 * @param [out] out a/2.
174 void curve448_scalar_halve (
175 curve448_scalar_t out,
176 const curve448_scalar_t a
180 * @brief Copy a scalar. The scalars may use the same memory, in which
181 * case this function does nothing.
182 * @param [in] a A scalar.
183 * @param [out] out Will become a copy of a.
185 static inline void curve448_scalar_copy (
186 curve448_scalar_t out,
187 const curve448_scalar_t a
193 * @brief Copy a point. The input and output may alias,
194 * in which case this function does nothing.
196 * @param [out] a A copy of the point.
197 * @param [in] b Any point.
199 static inline void curve448_point_copy (
201 const curve448_point_t b
207 * @brief Test whether two points are equal. If yes, return
208 * DECAF_TRUE, else return DECAF_FALSE.
210 * @param [in] a A point.
211 * @param [in] b Another point.
212 * @retval DECAF_TRUE The points are equal.
213 * @retval DECAF_FALSE The points are not equal.
215 __owur decaf_bool_t curve448_point_eq (
216 const curve448_point_t a,
217 const curve448_point_t b
221 * @brief Double a point. Equivalent to
222 * curve448_point_add(two_a,a,a), but potentially faster.
224 * @param [out] two_a The sum a+a.
225 * @param [in] a A point.
227 void curve448_point_double (
228 curve448_point_t two_a,
229 const curve448_point_t a
233 * @brief RFC 7748 Diffie-Hellman scalarmul. This function uses a different
234 * (non-Decaf) encoding.
236 * @param [out] scaled The scaled point base*scalar
237 * @param [in] base The point to be scaled.
238 * @param [in] scalar The scalar to multiply by.
240 * @retval DECAF_SUCCESS The scalarmul succeeded.
241 * @retval DECAF_FAILURE The scalarmul didn't succeed, because the base
242 * point is in a small subgroup.
244 __owur decaf_error_t decaf_x448 (
245 uint8_t out[DECAF_X448_PUBLIC_BYTES],
246 const uint8_t base[DECAF_X448_PUBLIC_BYTES],
247 const uint8_t scalar[DECAF_X448_PRIVATE_BYTES]
251 * @brief Multiply a point by DECAF_X448_ENCODE_RATIO,
252 * then encode it like RFC 7748.
254 * This function is mainly used internally, but is exported in case
257 * The ratio is necessary because the internal representation doesn't
258 * track the cofactor information, so on output we must clear the cofactor.
259 * This would multiply by the cofactor, but in fact internally libdecaf's
260 * points are always even, so it multiplies by half the cofactor instead.
262 * As it happens, this aligns with the base point definitions; that is,
263 * if you pass the Decaf/Ristretto base point to this function, the result
264 * will be DECAF_X448_ENCODE_RATIO times the X448
267 * @param [out] out The scaled and encoded point.
268 * @param [in] p The point to be scaled and encoded.
270 void curve448_point_mul_by_ratio_and_encode_like_x448 (
271 uint8_t out[DECAF_X448_PUBLIC_BYTES],
272 const curve448_point_t p
275 /** The base point for X448 Diffie-Hellman */
276 extern const uint8_t decaf_x448_base_point[DECAF_X448_PUBLIC_BYTES];
279 * @brief RFC 7748 Diffie-Hellman base point scalarmul. This function uses
280 * a different (non-Decaf) encoding.
282 * Does exactly the same thing as decaf_x448_generate_key,
283 * but has a better name.
285 * @param [out] scaled The scaled point base*scalar
286 * @param [in] scalar The scalar to multiply by.
288 void decaf_x448_derive_public_key (
289 uint8_t out[DECAF_X448_PUBLIC_BYTES],
290 const uint8_t scalar[DECAF_X448_PRIVATE_BYTES]
295 * @brief Multiply a precomputed base point by a scalar:
296 * scaled = scalar*base.
297 * Some implementations do not include precomputed points; for
298 * those implementations, this function is the same as
299 * curve448_point_scalarmul
301 * @param [out] scaled The scaled point base*scalar
302 * @param [in] base The point to be scaled.
303 * @param [in] scalar The scalar to multiply by.
305 void curve448_precomputed_scalarmul (
306 curve448_point_t scaled,
307 const curve448_precomputed_s *base,
308 const curve448_scalar_t scalar
313 * @brief Multiply two base points by two scalars:
314 * scaled = scalar1*curve448_point_base + scalar2*base2.
316 * Otherwise equivalent to curve448_point_double_scalarmul, but may be
317 * faster at the expense of being variable time.
319 * @param [out] combo The linear combination scalar1*base + scalar2*base2.
320 * @param [in] scalar1 A first scalar to multiply by.
321 * @param [in] base2 A second point to be scaled.
322 * @param [in] scalar2 A second scalar to multiply by.
324 * @warning: This function takes variable time, and may leak the scalars
325 * used. It is designed for signature verification.
327 void curve448_base_double_scalarmul_non_secret (
328 curve448_point_t combo,
329 const curve448_scalar_t scalar1,
330 const curve448_point_t base2,
331 const curve448_scalar_t scalar2
335 * @brief Test that a point is valid, for debugging purposes.
337 * @param [in] to_test The point to test.
338 * @retval DECAF_TRUE The point is valid.
339 * @retval DECAF_FALSE The point is invalid.
341 __owur decaf_bool_t curve448_point_valid (
342 const curve448_point_t to_test
346 * @brief Overwrite scalar with zeros.
348 void curve448_scalar_destroy (
349 curve448_scalar_t scalar
353 * @brief Overwrite point with zeros.
355 void curve448_point_destroy (
356 curve448_point_t point
363 #endif /* __DECAF_POINT_448_H__ */