You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

357 lines
10 KiB

  1. /**
  2. * @file scalarmul.h
  3. * @copyright
  4. * Copyright (c) 2014 Cryptography Research, Inc. \n
  5. * Released under the MIT License. See LICENSE.txt for license information.
  6. * @author Mike Hamburg
  7. */
  8. #ifndef __P448_ALGO_H__
  9. #define __P448_ALGO_H__ 1
  10. #include "ec_point.h"
  11. #include "field.h"
  12. #include "intrinsics.h"
  13. #include "magic.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /**
  18. * A word array containing a scalar
  19. */
  20. typedef word_t scalar_t[SCALAR_WORDS];
  21. /**
  22. * A precomputed table for fixed-base scalar multiplication.
  23. *
  24. * This uses a signed combs format.
  25. */
  26. struct fixed_base_table_t {
  27. /** Comb tables containing multiples of the base point. */
  28. tw_niels_a_t *table;
  29. /** Adjustments to the scalar in even and odd cases, respectively. */
  30. word_t scalar_adjustments[2*SCALAR_WORDS];
  31. /** The number of combs in the table. */
  32. unsigned int n;
  33. /** The number of teeth in each comb. */
  34. unsigned int t;
  35. /** The spacing between the teeth. */
  36. unsigned int s;
  37. /** If nonzero, the table was malloc'd by precompute_for_combs. */
  38. unsigned int own_table;
  39. };
  40. /**
  41. * Full Montgomery ladder in inverse square root format.
  42. *
  43. * Out = [2^n_extra_doubles * scalar] * in, where
  44. * scalar is little-endian and has length $nbits$ bits.
  45. *
  46. * If the scalar is even and/or n_extra_doubles >= 1,
  47. * then this function will reject points which are not
  48. * on the curve by returning MASK_FAILURE.
  49. *
  50. * This function will also reject multiplies which output
  51. * the identity or the point of order 2. It may be worth
  52. * revisiting this decision in the FUTURE. The idea is that
  53. * this can only happen when: the input is the identity or the
  54. * point of order 2; or the input is the point of order 4 on
  55. * the twist; or the scalar is 0 or a multiple of the curve
  56. * order; or the scalar is a multiple of the twist order and
  57. * the input point is on the twist.
  58. *
  59. * This function takes constant time with respect to $*in$
  60. * and $*scalar$, but not of course with respect to nbits or
  61. * n_extra_doubles.
  62. *
  63. * For security, we recommend setting n_extra_doubles = 1.
  64. * Because the cofactor of Goldilocks is 4 and input points
  65. * are always even (when on the curve), this will cancel the
  66. * cofactor.
  67. *
  68. * @param [out] out The output point.
  69. * @param [in] in The base point.
  70. * @param [in] scalar The scalar's little-endian representation.
  71. * @param [in] nbits The number of bits in the scalar. Note that
  72. * unlike in Curve25519, we do not require the top bit to be set.
  73. * @param [in] n_extra_doubles The number of extra doubles to do at
  74. * the end.
  75. *
  76. * @retval MASK_SUCCESS The operation was successful.
  77. * @retval MASK_FAILURE The input point was invalid, or the output
  78. * would be the identity or the point of order 2.
  79. */
  80. mask_t
  81. montgomery_ladder (
  82. field_a_t out,
  83. const field_a_t in,
  84. const word_t *scalar,
  85. unsigned int nbits,
  86. unsigned int n_extra_doubles
  87. ) __attribute__((warn_unused_result));
  88. /**
  89. * Full Montgomery aux ladder in decaf format.
  90. *
  91. * Out = scalar * in, where
  92. * scalar is little-endian and has length $nbits$ bits.
  93. *
  94. * This function (once it's done; TODO) will always reject points
  95. * on the twist.
  96. *
  97. * This function takes constant time with respect to $*in$
  98. * and $*scalar$, but not of course with respect to nbits or
  99. * n_extra_doubles.
  100. *
  101. * @param [out] out The output point.
  102. * @param [in] in The base point.
  103. * @param [in] scalar The scalar's little-endian representation.
  104. * @param [in] nbits The number of bits in the scalar. Note that
  105. * unlike in Curve25519, we do not require the top bit to be set.
  106. *
  107. * @retval MASK_SUCCESS The operation was successful.
  108. * @retval MASK_FAILURE The input point was invalid, or the output
  109. * would be the identity or the point of order 2.
  110. */
  111. mask_t
  112. decaf_montgomery_ladder (
  113. field_a_t out,
  114. const field_a_t in,
  115. const word_t *scalar,
  116. unsigned int nbits
  117. ) __attribute__((warn_unused_result));
  118. /**
  119. * Scalar multiply a twisted Edwards-form point.
  120. *
  121. * This function takes constant time.
  122. *
  123. * Currently the scalar is always exactly 448 bits long.
  124. *
  125. * @param [inout] working The point to multply.
  126. * @param [in] scalar The scalar, in little-endian form.
  127. */
  128. void
  129. scalarmul (
  130. tw_extensible_a_t working,
  131. const word_t scalar[SCALAR_WORDS]
  132. /* TODO? int nbits */
  133. );
  134. /**
  135. * Scalar multiply a twisted Edwards-form point. Use the same
  136. * algorithm as scalarmul(), but uses variable array indices.
  137. *
  138. * Currently the scalar is always exactly 448 bits long.
  139. *
  140. * @warning This function uses variable array indices,
  141. * so it is insecure against cache-timing attacks. It is intended
  142. * for microbenchmarking, to see how much constant-time arithmetic
  143. * costs us.
  144. *
  145. * @param [inout] working The point to multply.
  146. * @param [in] scalar The scalar, in little-endian form.
  147. */
  148. void
  149. scalarmul_vlook (
  150. tw_extensible_a_t working,
  151. const word_t scalar[SCALAR_WORDS]
  152. );
  153. /**
  154. * Precompute a table to accelerate fixed-point scalar
  155. * multiplication using the "multiple signed combs" approach.
  156. *
  157. * This function computes $n$ "comb" tables, each containing
  158. * 2^(t-1) points in tw_niels_t format. You must have
  159. * n * t * s >= SCALAR_BITS = 446 for complete coverage.
  160. *
  161. * The scalar multiplication algorithm may adjust the scalar by
  162. * a multiple of q. Therefore, we strongly recommend to use base
  163. * points in the q-torsion group (i.e. doubly even points).
  164. *
  165. * @param [out] out The table to compute.
  166. * @param [in] base The base point.
  167. * @param [in] n The number of combs in the table.
  168. * @param [in] t The number of teeth in each comb.
  169. * @param [in] s The spacing between the teeth.
  170. * @param [out] prealloc An optional preallocated array containing
  171. * space for n<<(t-1) values of type tw_niels_t.
  172. *
  173. * @retval MASK_SUCCESS Success.
  174. * @retval MASK_FAILURE Failure, most likely because we are out
  175. * of memory.
  176. */
  177. mask_t
  178. precompute_fixed_base (
  179. struct fixed_base_table_t *out,
  180. const tw_extensible_a_t base,
  181. unsigned int n,
  182. unsigned int t,
  183. unsigned int s,
  184. tw_niels_a_t *prealloc
  185. ) __attribute__((warn_unused_result));
  186. /**
  187. * Destroy a fixed-base table. Frees any memory that we allocated
  188. * for the combs.
  189. *
  190. * @param [in] table The table to destroy.
  191. */
  192. void
  193. destroy_fixed_base (
  194. struct fixed_base_table_t *table
  195. );
  196. /**
  197. * Scalar multiplication with precomputation. Set working to
  198. * to [scalar] * Base, where Base is the base point passed to
  199. * precompute_for_combs().
  200. *
  201. * The scalar may be adjusted by a multiple of q, so this routine
  202. * can be wrong by a cofactor if the base has cofactor components.
  203. *
  204. * @param [out] out The output point.
  205. * @param [in] scalar The scalar.
  206. * @param [in] nbits The number of bits in the scalar. Must be <= n*t*s.
  207. * @param [in] table The precomputed table.
  208. *
  209. * @retval MASK_SUCCESS Success.
  210. * @retval MASK_FAILURE Failure, because n*t*s < nbits
  211. */
  212. mask_t
  213. scalarmul_fixed_base (
  214. tw_extensible_a_t out,
  215. const word_t *scalar,
  216. unsigned int nbits,
  217. const struct fixed_base_table_t *table
  218. );
  219. /**
  220. * Variable-time scalar multiplication.
  221. *
  222. * @warning This function takes variable time. It is intended for
  223. * microbenchmarking.
  224. *
  225. * @param [inout] working The input and output point.
  226. * @param [in] scalar The scalar.
  227. * @param [in] nbits The number of bits in the scalar
  228. */
  229. void
  230. scalarmul_vt (
  231. tw_extensible_a_t working,
  232. const word_t *scalar,
  233. unsigned int nbits
  234. );
  235. /**
  236. * Precompute a table to accelerate fixed-point scalar
  237. * multiplication (and, more importantly, linear combos)
  238. * using the "windowed non-adjacent form" approach.
  239. *
  240. * @param [out] out The output table. Must have room for 1<<i entries.
  241. * @param [in] base The base point.
  242. * @param [in] tbits The number of bits to put in the table.
  243. *
  244. * @retval MASK_SUCCESS Success.
  245. * @retval MASK_FAILURE Failure, most likely because we are out
  246. * of memory.
  247. */
  248. mask_t
  249. precompute_fixed_base_wnaf (
  250. tw_niels_a_t *out,
  251. const tw_extensible_a_t base,
  252. unsigned int tbits
  253. ) __attribute__((warn_unused_result));
  254. /**
  255. * Variable-time scalar multiplication with precomputed WNAF
  256. * tables.
  257. *
  258. * @warning This function takes variable time. It is intended for
  259. * microbenchmarking.
  260. *
  261. * @param [out] out The output point.
  262. * @param [in] scalar The scalar.
  263. * @param [in] nbits The number of bits in the scalar.
  264. * @param [in] precmp The precomputed WNAF table.
  265. * @param [in] table_bits The number of bits in the WNAF table.
  266. */
  267. void
  268. scalarmul_fixed_base_wnaf_vt (
  269. tw_extensible_a_t out,
  270. const word_t *scalar,
  271. unsigned int nbits,
  272. const tw_niels_a_t *precmp,
  273. unsigned int table_bits
  274. );
  275. /**
  276. * Variable-time scalar linear combination of two points: one
  277. * variable, and one fixed (with fixed-base WNAF tables)
  278. *
  279. * @warning This function takes variable time. It is intended for
  280. * signature verification.
  281. *
  282. * @param [inout] working The output point, and also the variable input.
  283. * @param [in] scalar_var The scalar for the variable input.
  284. * @param [in] nbits_var The number of bits in scalar_var.
  285. * @param [in] scalar_pre The scalar for the fixed input.
  286. * @param [in] nbits_pre The number of bits in scalar_pre.
  287. * @param [in] precmp The precomputed WNAF table.
  288. * @param [in] table_bits_pre The number of bits in the WNAF table.
  289. */
  290. void
  291. linear_combo_var_fixed_vt (
  292. tw_extensible_a_t working,
  293. const word_t scalar_var[SCALAR_WORDS],
  294. unsigned int nbits_var,
  295. const word_t scalar_pre[SCALAR_WORDS],
  296. unsigned int nbits_pre,
  297. const tw_niels_a_t *precmp,
  298. unsigned int table_bits_pre
  299. );
  300. /**
  301. * Variable-time scalar linear combination of two fixed points.
  302. *
  303. * @warning This function takes variable time. It is intended for
  304. * signature verification.
  305. *
  306. * @param [out] working The output point.
  307. * @param [in] scalar1 The first scalar.
  308. * @param [in] nbits1 The number of bits in the first scalar.
  309. * @param [in] table1 The first precomputed table.
  310. * @param [in] scalar2 The second scalar.
  311. * @param [in] nbits1 The number of bits in the second scalar.
  312. * @param [in] table1 The second precomputed table.
  313. *
  314. * @retval MASK_SUCCESS Success.
  315. * @retval MASK_FAILURE Failure, because eg the tables are too small.
  316. */
  317. mask_t
  318. linear_combo_combs_vt (
  319. tw_extensible_a_t out,
  320. const word_t scalar1[SCALAR_WORDS],
  321. unsigned int nbits1,
  322. const struct fixed_base_table_t *table1,
  323. const word_t scalar2[SCALAR_WORDS],
  324. unsigned int nbits2,
  325. const struct fixed_base_table_t *table2
  326. );
  327. #ifdef __cplusplus
  328. };
  329. #endif
  330. #endif /* __P448_ALGO_H__ */