2
0

point_448.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2015-2016 Cryptography Research, Inc.
  4. *
  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
  9. *
  10. * Originally written by Mike Hamburg
  11. */
  12. #ifndef HEADER_POINT_448_H
  13. # define HEADER_POINT_448_H
  14. # include "curve448utils.h"
  15. # include "field.h"
  16. /* Comb config: number of combs, n, t, s. */
  17. #define COMBS_N 5
  18. #define COMBS_T 5
  19. #define COMBS_S 18
  20. /* Projective Niels coordinates */
  21. typedef struct {
  22. gf a, b, c;
  23. } niels_s, niels_t[1];
  24. typedef struct {
  25. niels_t n;
  26. gf z;
  27. } pniels_t[1];
  28. /* Precomputed base */
  29. struct curve448_precomputed_s {
  30. niels_t table[COMBS_N << (COMBS_T - 1)];
  31. };
  32. # define C448_SCALAR_LIMBS ((446-1)/C448_WORD_BITS+1)
  33. /* The number of bits in a scalar */
  34. # define C448_SCALAR_BITS 446
  35. /* Number of bytes in a serialized scalar. */
  36. # define C448_SCALAR_BYTES 56
  37. /* X448 encoding ratio. */
  38. # define X448_ENCODE_RATIO 2
  39. /* Number of bytes in an x448 public key */
  40. # define X448_PUBLIC_BYTES 56
  41. /* Number of bytes in an x448 private key */
  42. # define X448_PRIVATE_BYTES 56
  43. /* Twisted Edwards extended homogeneous coordinates */
  44. typedef struct curve448_point_s {
  45. gf x, y, z, t;
  46. } curve448_point_t[1];
  47. /* Precomputed table based on a point. Can be trivial implementation. */
  48. struct curve448_precomputed_s;
  49. /* Precomputed table based on a point. Can be trivial implementation. */
  50. 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];
  55. /* A scalar equal to 1. */
  56. extern const curve448_scalar_t curve448_scalar_one;
  57. /* A scalar equal to 0. */
  58. extern const curve448_scalar_t curve448_scalar_zero;
  59. /* The identity point on the curve. */
  60. extern const curve448_point_t curve448_point_identity;
  61. /* Precomputed table for the base point on the curve. */
  62. extern const struct curve448_precomputed_s *curve448_precomputed_base;
  63. extern const niels_t *curve448_wnaf_base;
  64. /*
  65. * Read a scalar from wire format or from bytes.
  66. *
  67. * ser (in): Serialized form of a scalar.
  68. * out (out): Deserialized form.
  69. *
  70. * Returns:
  71. * C448_SUCCESS: The scalar was correctly encoded.
  72. * C448_FAILURE: The scalar was greater than the modulus, and has been reduced
  73. * modulo that modulus.
  74. */
  75. c448_error_t curve448_scalar_decode(curve448_scalar_t out,
  76. const unsigned char ser[C448_SCALAR_BYTES]);
  77. /*
  78. * Read a scalar from wire format or from bytes. Reduces mod scalar prime.
  79. *
  80. * ser (in): Serialized form of a scalar.
  81. * ser_len (in): Length of serialized form.
  82. * out (out): Deserialized form.
  83. */
  84. void curve448_scalar_decode_long(curve448_scalar_t out,
  85. const unsigned char *ser, size_t ser_len);
  86. /*
  87. * Serialize a scalar to wire format.
  88. *
  89. * ser (out): Serialized form of a scalar.
  90. * s (in): Deserialized scalar.
  91. */
  92. void curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
  93. const curve448_scalar_t s);
  94. /*
  95. * Add two scalars. |a|, |b| and |out| may alias each other.
  96. *
  97. * a (in): One scalar.
  98. * b (in): Another scalar.
  99. * out (out): a+b.
  100. */
  101. void curve448_scalar_add(curve448_scalar_t out,
  102. const curve448_scalar_t a, const curve448_scalar_t b);
  103. /*
  104. * Subtract two scalars. |a|, |b| and |out| may alias each other.
  105. * a (in): One scalar.
  106. * b (in): Another scalar.
  107. * out (out): a-b.
  108. */
  109. void curve448_scalar_sub(curve448_scalar_t out,
  110. const curve448_scalar_t a, const curve448_scalar_t b);
  111. /*
  112. * Multiply two scalars. |a|, |b| and |out| may alias each other.
  113. *
  114. * a (in): One scalar.
  115. * b (in): Another scalar.
  116. * out (out): a*b.
  117. */
  118. void curve448_scalar_mul(curve448_scalar_t out,
  119. const curve448_scalar_t a, const curve448_scalar_t b);
  120. /*
  121. * Halve a scalar. |a| and |out| may alias each other.
  122. *
  123. * a (in): A scalar.
  124. * out (out): a/2.
  125. */
  126. void curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a);
  127. /*
  128. * Copy a scalar. The scalars may alias each other, in which case this
  129. * function does nothing.
  130. *
  131. * a (in): A scalar.
  132. * out (out): Will become a copy of a.
  133. */
  134. static ossl_inline void curve448_scalar_copy(curve448_scalar_t out,
  135. const curve448_scalar_t a)
  136. {
  137. *out = *a;
  138. }
  139. /*
  140. * Copy a point. The input and output may alias, in which case this function
  141. * does nothing.
  142. *
  143. * a (out): A copy of the point.
  144. * b (in): Any point.
  145. */
  146. static ossl_inline void curve448_point_copy(curve448_point_t a,
  147. const curve448_point_t b)
  148. {
  149. *a = *b;
  150. }
  151. /*
  152. * Test whether two points are equal. If yes, return C448_TRUE, else return
  153. * C448_FALSE.
  154. *
  155. * a (in): A point.
  156. * b (in): Another point.
  157. *
  158. * Returns:
  159. * C448_TRUE: The points are equal.
  160. * C448_FALSE: The points are not equal.
  161. */
  162. __owur c448_bool_t curve448_point_eq(const curve448_point_t a,
  163. const curve448_point_t b);
  164. /*
  165. * Double a point. Equivalent to curve448_point_add(two_a,a,a), but potentially
  166. * faster.
  167. *
  168. * two_a (out): The sum a+a.
  169. * a (in): A point.
  170. */
  171. void curve448_point_double(curve448_point_t two_a, const curve448_point_t a);
  172. /*
  173. * RFC 7748 Diffie-Hellman scalarmul. This function uses a different
  174. * (non-Decaf) encoding.
  175. *
  176. * out (out): The scaled point base*scalar
  177. * base (in): The point to be scaled.
  178. * scalar (in): The scalar to multiply by.
  179. *
  180. * Returns:
  181. * C448_SUCCESS: The scalarmul succeeded.
  182. * C448_FAILURE: The scalarmul didn't succeed, because the base point is in a
  183. * small subgroup.
  184. */
  185. __owur c448_error_t x448_int(uint8_t out[X448_PUBLIC_BYTES],
  186. const uint8_t base[X448_PUBLIC_BYTES],
  187. const uint8_t scalar[X448_PRIVATE_BYTES]);
  188. /*
  189. * Multiply a point by X448_ENCODE_RATIO, then encode it like RFC 7748.
  190. *
  191. * This function is mainly used internally, but is exported in case
  192. * it will be useful.
  193. *
  194. * The ratio is necessary because the internal representation doesn't
  195. * track the cofactor information, so on output we must clear the cofactor.
  196. * This would multiply by the cofactor, but in fact internally points are always
  197. * even, so it multiplies by half the cofactor instead.
  198. *
  199. * As it happens, this aligns with the base point definitions; that is,
  200. * if you pass the Decaf/Ristretto base point to this function, the result
  201. * will be X448_ENCODE_RATIO times the X448
  202. * base point.
  203. *
  204. * out (out): The scaled and encoded point.
  205. * p (in): The point to be scaled and encoded.
  206. */
  207. void curve448_point_mul_by_ratio_and_encode_like_x448(
  208. uint8_t out[X448_PUBLIC_BYTES],
  209. const curve448_point_t p);
  210. /*
  211. * RFC 7748 Diffie-Hellman base point scalarmul. This function uses a different
  212. * (non-Decaf) encoding.
  213. *
  214. * out (out): The scaled point base*scalar
  215. * scalar (in): The scalar to multiply by.
  216. */
  217. void x448_derive_public_key(uint8_t out[X448_PUBLIC_BYTES],
  218. const uint8_t scalar[X448_PRIVATE_BYTES]);
  219. /*
  220. * Multiply a precomputed base point by a scalar: out = scalar*base.
  221. *
  222. * scaled (out): The scaled point base*scalar
  223. * base (in): The point to be scaled.
  224. * scalar (in): The scalar to multiply by.
  225. */
  226. void curve448_precomputed_scalarmul(curve448_point_t scaled,
  227. const curve448_precomputed_s * base,
  228. const curve448_scalar_t scalar);
  229. /*
  230. * Multiply two base points by two scalars:
  231. * combo = scalar1*curve448_point_base + scalar2*base2.
  232. *
  233. * Otherwise equivalent to curve448_point_double_scalarmul, but may be
  234. * faster at the expense of being variable time.
  235. *
  236. * combo (out): The linear combination scalar1*base + scalar2*base2.
  237. * scalar1 (in): A first scalar to multiply by.
  238. * base2 (in): A second point to be scaled.
  239. * scalar2 (in) A second scalar to multiply by.
  240. *
  241. * Warning: This function takes variable time, and may leak the scalars used.
  242. * It is designed for signature verification.
  243. */
  244. void curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
  245. const curve448_scalar_t scalar1,
  246. const curve448_point_t base2,
  247. const curve448_scalar_t scalar2);
  248. /*
  249. * Test that a point is valid, for debugging purposes.
  250. *
  251. * to_test (in): The point to test.
  252. *
  253. * Returns:
  254. * C448_TRUE The point is valid.
  255. * C448_FALSE The point is invalid.
  256. */
  257. __owur c448_bool_t curve448_point_valid(const curve448_point_t to_test);
  258. /* Overwrite scalar with zeros. */
  259. void curve448_scalar_destroy(curve448_scalar_t scalar);
  260. /* Overwrite point with zeros. */
  261. void curve448_point_destroy(curve448_point_t point);
  262. #endif /* HEADER_POINT_448_H */