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.
 
 
 
 
 

686 lines
21 KiB

  1. from gen_file import gen_file
  2. decaf_h = gen_file(
  3. public = True,
  4. per = "curve",
  5. name = "decaf/%(c_ns)s.h",
  6. doc = """@brief A group of prime order p, based on %(iso_to)s.""",
  7. code = """
  8. #include <decaf/common.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /** @cond internal */
  13. #define %(C_NS)s_SCALAR_LIMBS ((%(scalar_bits)d-1)/DECAF_WORD_BITS+1)
  14. /** @endcond */
  15. /** The number of bits in a scalar */
  16. #define %(C_NS)s_SCALAR_BITS %(scalar_bits)d
  17. /** @cond internal */
  18. #ifndef __DECAF_%(gf_shortname)s_GF_DEFINED__
  19. #define __DECAF_%(gf_shortname)s_GF_DEFINED__ 1
  20. /** @brief Galois field element internal structure */
  21. typedef struct gf_%(gf_shortname)s_s {
  22. decaf_word_t limb[%(gf_impl_bits)d/DECAF_WORD_BITS];
  23. } __attribute__((aligned(32))) gf_%(gf_shortname)s_s, gf_%(gf_shortname)s_t[1];
  24. #endif /* __DECAF_%(gf_shortname)s_GF_DEFINED__ */
  25. /** @endcond */
  26. /** Number of bytes in a serialized point. */
  27. #define %(C_NS)s_SER_BYTES %(ser_bytes)d
  28. /** Number of bytes in a serialized scalar. */
  29. #define %(C_NS)s_SCALAR_BYTES %(scalar_ser_bytes)d
  30. /** Number of bytes in an x%(gf_shortname)s public key */
  31. #define X%(gf_shortname)s_PUBLIC_BYTES %(x_pub_bytes)d
  32. /** Number of bytes in an x%(gf_shortname)s private key */
  33. #define X%(gf_shortname)s_PRIVATE_BYTES %(x_priv_bytes)d
  34. /** Twisted Edwards extended homogeneous coordinates */
  35. typedef struct %(c_ns)s_point_s {
  36. /** @cond internal */
  37. gf_%(gf_shortname)s_t x,y,z,t;
  38. /** @endcond */
  39. } %(c_ns)s_point_t[1];
  40. /** Precomputed table based on a point. Can be trivial implementation. */
  41. struct %(c_ns)s_precomputed_s;
  42. /** Precomputed table based on a point. Can be trivial implementation. */
  43. typedef struct %(c_ns)s_precomputed_s %(c_ns)s_precomputed_s;
  44. /** Size and alignment of precomputed point tables. */
  45. extern const size_t %(c_ns)s_sizeof_precomputed_s API_VIS, %(c_ns)s_alignof_precomputed_s API_VIS;
  46. /** Scalar is stored packed, because we don't need the speed. */
  47. typedef struct %(c_ns)s_scalar_s {
  48. /** @cond internal */
  49. decaf_word_t limb[%(C_NS)s_SCALAR_LIMBS];
  50. /** @endcond */
  51. } %(c_ns)s_scalar_t[1];
  52. /** A scalar equal to 1. */
  53. extern const %(c_ns)s_scalar_t %(c_ns)s_scalar_one API_VIS;
  54. /** A scalar equal to 0. */
  55. extern const %(c_ns)s_scalar_t %(c_ns)s_scalar_zero API_VIS;
  56. /** The identity point on the curve. */
  57. extern const %(c_ns)s_point_t %(c_ns)s_point_identity API_VIS;
  58. /** An arbitrarily chosen base point on the curve.
  59. * @warning TODO: this is subject to change. It is currently
  60. * the preimage of the X%(gf_shortname)s base point. Sometime
  61. * soon, we will merge and finalize support for X%(gf_shortname)s
  62. * and Ed%(gf_shortname)s integration. This might make some
  63. * multiple of the current basepoint (eg twice it, or the cofactor
  64. * times it) more convenient API-wise, and trigger a changeover.
  65. */
  66. extern const %(c_ns)s_point_t %(c_ns)s_point_base API_VIS;
  67. /** Precomputed table for the base point on the curve. */
  68. extern const struct %(c_ns)s_precomputed_s *%(c_ns)s_precomputed_base API_VIS;
  69. /**
  70. * @brief Read a scalar from wire format or from bytes.
  71. *
  72. * @param [in] ser Serialized form of a scalar.
  73. * @param [out] out Deserialized form.
  74. *
  75. * @retval DECAF_SUCCESS The scalar was correctly encoded.
  76. * @retval DECAF_FAILURE The scalar was greater than the modulus,
  77. * and has been reduced modulo that modulus.
  78. */
  79. decaf_error_t %(c_ns)s_scalar_decode (
  80. %(c_ns)s_scalar_t out,
  81. const unsigned char ser[%(C_NS)s_SCALAR_BYTES]
  82. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  83. /**
  84. * @brief Read a scalar from wire format or from bytes. Reduces mod
  85. * scalar prime.
  86. *
  87. * @param [in] ser Serialized form of a scalar.
  88. * @param [in] ser_len Length of serialized form.
  89. * @param [out] out Deserialized form.
  90. */
  91. void %(c_ns)s_scalar_decode_long (
  92. %(c_ns)s_scalar_t out,
  93. const unsigned char *ser,
  94. size_t ser_len
  95. ) API_VIS NONNULL2 NOINLINE;
  96. /**
  97. * @brief Serialize a scalar to wire format.
  98. *
  99. * @param [out] ser Serialized form of a scalar.
  100. * @param [in] s Deserialized scalar.
  101. */
  102. void %(c_ns)s_scalar_encode (
  103. unsigned char ser[%(C_NS)s_SCALAR_BYTES],
  104. const %(c_ns)s_scalar_t s
  105. ) API_VIS NONNULL2 NOINLINE NOINLINE;
  106. /**
  107. * @brief Add two scalars. The scalars may use the same memory.
  108. * @param [in] a One scalar.
  109. * @param [in] b Another scalar.
  110. * @param [out] out a+b.
  111. */
  112. void %(c_ns)s_scalar_add (
  113. %(c_ns)s_scalar_t out,
  114. const %(c_ns)s_scalar_t a,
  115. const %(c_ns)s_scalar_t b
  116. ) API_VIS NONNULL3 NOINLINE;
  117. /**
  118. * @brief Compare two scalars.
  119. * @param [in] a One scalar.
  120. * @param [in] b Another scalar.
  121. * @retval DECAF_TRUE The scalars are equal.
  122. * @retval DECAF_FALSE The scalars are not equal.
  123. */
  124. decaf_bool_t %(c_ns)s_scalar_eq (
  125. const %(c_ns)s_scalar_t a,
  126. const %(c_ns)s_scalar_t b
  127. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  128. /**
  129. * @brief Subtract two scalars. The scalars may use the same memory.
  130. * @param [in] a One scalar.
  131. * @param [in] b Another scalar.
  132. * @param [out] out a-b.
  133. */
  134. void %(c_ns)s_scalar_sub (
  135. %(c_ns)s_scalar_t out,
  136. const %(c_ns)s_scalar_t a,
  137. const %(c_ns)s_scalar_t b
  138. ) API_VIS NONNULL3 NOINLINE;
  139. /**
  140. * @brief Multiply two scalars. The scalars may use the same memory.
  141. * @param [in] a One scalar.
  142. * @param [in] b Another scalar.
  143. * @param [out] out a*b.
  144. */
  145. void %(c_ns)s_scalar_mul (
  146. %(c_ns)s_scalar_t out,
  147. const %(c_ns)s_scalar_t a,
  148. const %(c_ns)s_scalar_t b
  149. ) API_VIS NONNULL3 NOINLINE;
  150. /**
  151. * @brief Invert a scalar. When passed zero, return 0. The input and output may alias.
  152. * @param [in] a A scalar.
  153. * @param [out] out 1/a.
  154. * @return DECAF_SUCCESS The input is nonzero.
  155. */
  156. decaf_error_t %(c_ns)s_scalar_invert (
  157. %(c_ns)s_scalar_t out,
  158. const %(c_ns)s_scalar_t a
  159. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  160. /**
  161. * @brief Copy a scalar. The scalars may use the same memory, in which
  162. * case this function does nothing.
  163. * @param [in] a A scalar.
  164. * @param [out] out Will become a copy of a.
  165. */
  166. static inline void NONNULL2 %(c_ns)s_scalar_copy (
  167. %(c_ns)s_scalar_t out,
  168. const %(c_ns)s_scalar_t a
  169. ) {
  170. *out = *a;
  171. }
  172. /**
  173. * @brief Set a scalar to an unsigned 64-bit integer.
  174. * @param [in] a An integer.
  175. * @param [out] out Will become equal to a.
  176. */
  177. void %(c_ns)s_scalar_set_unsigned (
  178. %(c_ns)s_scalar_t out,
  179. uint64_t a
  180. ) API_VIS NONNULL1;
  181. /**
  182. * @brief Encode a point as a sequence of bytes.
  183. *
  184. * @param [out] ser The byte representation of the point.
  185. * @param [in] pt The point to encode.
  186. */
  187. void %(c_ns)s_point_encode (
  188. uint8_t ser[%(C_NS)s_SER_BYTES],
  189. const %(c_ns)s_point_t pt
  190. ) API_VIS NONNULL2 NOINLINE;
  191. /**
  192. * @brief Decode a point from a sequence of bytes.
  193. *
  194. * Every point has a unique encoding, so not every
  195. * sequence of bytes is a valid encoding. If an invalid
  196. * encoding is given, the output is undefined.
  197. *
  198. * @param [out] pt The decoded point.
  199. * @param [in] ser The serialized version of the point.
  200. * @param [in] allow_identity DECAF_TRUE if the identity is a legal input.
  201. * @retval DECAF_SUCCESS The decoding succeeded.
  202. * @retval DECAF_FAILURE The decoding didn't succeed, because
  203. * ser does not represent a point.
  204. */
  205. decaf_error_t %(c_ns)s_point_decode (
  206. %(c_ns)s_point_t pt,
  207. const uint8_t ser[%(C_NS)s_SER_BYTES],
  208. decaf_bool_t allow_identity
  209. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  210. /**
  211. * @brief Copy a point. The input and output may alias,
  212. * in which case this function does nothing.
  213. *
  214. * @param [out] a A copy of the point.
  215. * @param [in] b Any point.
  216. */
  217. static inline void NONNULL2 %(c_ns)s_point_copy (
  218. %(c_ns)s_point_t a,
  219. const %(c_ns)s_point_t b
  220. ) {
  221. *a=*b;
  222. }
  223. /**
  224. * @brief Test whether two points are equal. If yes, return
  225. * DECAF_TRUE, else return DECAF_FALSE.
  226. *
  227. * @param [in] a A point.
  228. * @param [in] b Another point.
  229. * @retval DECAF_TRUE The points are equal.
  230. * @retval DECAF_FALSE The points are not equal.
  231. */
  232. decaf_bool_t %(c_ns)s_point_eq (
  233. const %(c_ns)s_point_t a,
  234. const %(c_ns)s_point_t b
  235. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  236. /**
  237. * @brief Add two points to produce a third point. The
  238. * input points and output point can be pointers to the same
  239. * memory.
  240. *
  241. * @param [out] sum The sum a+b.
  242. * @param [in] a An addend.
  243. * @param [in] b An addend.
  244. */
  245. void %(c_ns)s_point_add (
  246. %(c_ns)s_point_t sum,
  247. const %(c_ns)s_point_t a,
  248. const %(c_ns)s_point_t b
  249. ) API_VIS NONNULL3;
  250. /**
  251. * @brief Double a point. Equivalent to
  252. * %(c_ns)s_point_add(two_a,a,a), but potentially faster.
  253. *
  254. * @param [out] two_a The sum a+a.
  255. * @param [in] a A point.
  256. */
  257. void %(c_ns)s_point_double (
  258. %(c_ns)s_point_t two_a,
  259. const %(c_ns)s_point_t a
  260. ) API_VIS NONNULL2;
  261. /**
  262. * @brief Subtract two points to produce a third point. The
  263. * input points and output point can be pointers to the same
  264. * memory.
  265. *
  266. * @param [out] diff The difference a-b.
  267. * @param [in] a The minuend.
  268. * @param [in] b The subtrahend.
  269. */
  270. void %(c_ns)s_point_sub (
  271. %(c_ns)s_point_t diff,
  272. const %(c_ns)s_point_t a,
  273. const %(c_ns)s_point_t b
  274. ) API_VIS NONNULL3;
  275. /**
  276. * @brief Negate a point to produce another point. The input
  277. * and output points can use the same memory.
  278. *
  279. * @param [out] nega The negated input point
  280. * @param [in] a The input point.
  281. */
  282. void %(c_ns)s_point_negate (
  283. %(c_ns)s_point_t nega,
  284. const %(c_ns)s_point_t a
  285. ) API_VIS NONNULL2;
  286. /**
  287. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  288. *
  289. * @param [out] scaled The scaled point base*scalar
  290. * @param [in] base The point to be scaled.
  291. * @param [in] scalar The scalar to multiply by.
  292. */
  293. void %(c_ns)s_point_scalarmul (
  294. %(c_ns)s_point_t scaled,
  295. const %(c_ns)s_point_t base,
  296. const %(c_ns)s_scalar_t scalar
  297. ) API_VIS NONNULL3 NOINLINE;
  298. /**
  299. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  300. * This function operates directly on serialized forms.
  301. *
  302. * @warning This function is experimental. It may not be supported
  303. * long-term.
  304. *
  305. * @param [out] scaled The scaled point base*scalar
  306. * @param [in] base The point to be scaled.
  307. * @param [in] scalar The scalar to multiply by.
  308. * @param [in] allow_identity Allow the input to be the identity.
  309. * @param [in] short_circuit Allow a fast return if the input is illegal.
  310. *
  311. * @retval DECAF_SUCCESS The scalarmul succeeded.
  312. * @retval DECAF_FAILURE The scalarmul didn't succeed, because
  313. * base does not represent a point.
  314. */
  315. decaf_error_t %(c_ns)s_direct_scalarmul (
  316. uint8_t scaled[%(C_NS)s_SER_BYTES],
  317. const uint8_t base[%(C_NS)s_SER_BYTES],
  318. const %(c_ns)s_scalar_t scalar,
  319. decaf_bool_t allow_identity,
  320. decaf_bool_t short_circuit
  321. ) API_VIS NONNULL3 WARN_UNUSED NOINLINE;
  322. /**
  323. * @brief RFC 7748 Diffie-Hellman scalarmul. This function uses a different
  324. * (non-Decaf) encoding.
  325. *
  326. * @param [out] scaled The scaled point base*scalar
  327. * @param [in] base The point to be scaled.
  328. * @param [in] scalar The scalar to multiply by.
  329. *
  330. * @retval DECAF_SUCCESS The scalarmul succeeded.
  331. * @retval DECAF_FAILURE The scalarmul didn't succeed, because the base
  332. * point is in a small subgroup.
  333. */
  334. decaf_error_t %(c_ns)s_x_direct_scalarmul ( /* TODO: rename? */
  335. uint8_t out[X%(gf_shortname)s_PUBLIC_BYTES],
  336. const uint8_t base[X%(gf_shortname)s_PUBLIC_BYTES],
  337. const uint8_t scalar[X%(gf_shortname)s_PRIVATE_BYTES]
  338. ) API_VIS NONNULL3 WARN_UNUSED NOINLINE;
  339. /** The base point for X%(gf_shortname)s Diffie-Hellman */
  340. extern const uint8_t %(c_ns)s_x_base_point[X%(gf_shortname)s_PUBLIC_BYTES] API_VIS;
  341. /**
  342. * @brief RFC 7748 Diffie-Hellman base point scalarmul. This function uses
  343. * a different (non-Decaf) encoding.
  344. *
  345. * @param [out] scaled The scaled point base*scalar
  346. * @param [in] scalar The scalar to multiply by.
  347. */
  348. void %(c_ns)s_x_base_scalarmul (
  349. uint8_t out[X%(gf_shortname)s_PUBLIC_BYTES],
  350. const uint8_t scalar[X%(gf_shortname)s_PRIVATE_BYTES]
  351. ) API_VIS NONNULL2 NOINLINE;
  352. /**
  353. * @brief Precompute a table for fast scalar multiplication.
  354. * Some implementations do not include precomputed points; for
  355. * those implementations, this implementation simply copies the
  356. * point.
  357. *
  358. * @param [out] a A precomputed table of multiples of the point.
  359. * @param [in] b Any point.
  360. */
  361. void %(c_ns)s_precompute (
  362. %(c_ns)s_precomputed_s *a,
  363. const %(c_ns)s_point_t b
  364. ) API_VIS NONNULL2 NOINLINE;
  365. /**
  366. * @brief Multiply a precomputed base point by a scalar:
  367. * scaled = scalar*base.
  368. * Some implementations do not include precomputed points; for
  369. * those implementations, this function is the same as
  370. * %(c_ns)s_point_scalarmul
  371. *
  372. * @param [out] scaled The scaled point base*scalar
  373. * @param [in] base The point to be scaled.
  374. * @param [in] scalar The scalar to multiply by.
  375. */
  376. void %(c_ns)s_precomputed_scalarmul (
  377. %(c_ns)s_point_t scaled,
  378. const %(c_ns)s_precomputed_s *base,
  379. const %(c_ns)s_scalar_t scalar
  380. ) API_VIS NONNULL3 NOINLINE;
  381. /**
  382. * @brief Multiply two base points by two scalars:
  383. * scaled = scalar1*base1 + scalar2*base2.
  384. *
  385. * Equivalent to two calls to %(c_ns)s_point_scalarmul, but may be
  386. * faster.
  387. *
  388. * @param [out] combo The linear combination scalar1*base1 + scalar2*base2.
  389. * @param [in] base1 A first point to be scaled.
  390. * @param [in] scalar1 A first scalar to multiply by.
  391. * @param [in] base2 A second point to be scaled.
  392. * @param [in] scalar2 A second scalar to multiply by.
  393. */
  394. void %(c_ns)s_point_double_scalarmul (
  395. %(c_ns)s_point_t combo,
  396. const %(c_ns)s_point_t base1,
  397. const %(c_ns)s_scalar_t scalar1,
  398. const %(c_ns)s_point_t base2,
  399. const %(c_ns)s_scalar_t scalar2
  400. ) API_VIS NONNULL5 NOINLINE;
  401. /**
  402. * Multiply one base point by two scalars:
  403. *
  404. * a1 = scalar1 * base
  405. * a2 = scalar2 * base
  406. *
  407. * Equivalent to two calls to %(c_ns)s_point_scalarmul, but may be
  408. * faster.
  409. *
  410. * @param [out] a1 The first multiple. It may be the same as the input point.
  411. * @param [out] a2 The second multiple. It may be the same as the input point.
  412. * @param [in] base1 A point to be scaled.
  413. * @param [in] scalar1 A first scalar to multiply by.
  414. * @param [in] scalar2 A second scalar to multiply by.
  415. */
  416. void %(c_ns)s_point_dual_scalarmul (
  417. %(c_ns)s_point_t a1,
  418. %(c_ns)s_point_t a2,
  419. const %(c_ns)s_point_t base1,
  420. const %(c_ns)s_scalar_t scalar1,
  421. const %(c_ns)s_scalar_t scalar2
  422. ) API_VIS NONNULL5 NOINLINE;
  423. /**
  424. * @brief Multiply two base points by two scalars:
  425. * scaled = scalar1*%(c_ns)s_point_base + scalar2*base2.
  426. *
  427. * Otherwise equivalent to %(c_ns)s_point_double_scalarmul, but may be
  428. * faster at the expense of being variable time.
  429. *
  430. * @param [out] combo The linear combination scalar1*base + scalar2*base2.
  431. * @param [in] scalar1 A first scalar to multiply by.
  432. * @param [in] base2 A second point to be scaled.
  433. * @param [in] scalar2 A second scalar to multiply by.
  434. *
  435. * @warning: This function takes variable time, and may leak the scalars
  436. * used. It is designed for signature verification.
  437. */
  438. void %(c_ns)s_base_double_scalarmul_non_secret (
  439. %(c_ns)s_point_t combo,
  440. const %(c_ns)s_scalar_t scalar1,
  441. const %(c_ns)s_point_t base2,
  442. const %(c_ns)s_scalar_t scalar2
  443. ) API_VIS NONNULL4 NOINLINE;
  444. /**
  445. * @brief Constant-time decision between two points. If pick_b
  446. * is zero, out = a; else out = b.
  447. *
  448. * @param [out] out The output. It may be the same as either input.
  449. * @param [in] a Any point.
  450. * @param [in] b Any point.
  451. * @param [in] pick_b If nonzero, choose point b.
  452. */
  453. void %(c_ns)s_point_cond_sel (
  454. %(c_ns)s_point_t out,
  455. const %(c_ns)s_point_t a,
  456. const %(c_ns)s_point_t b,
  457. decaf_word_t pick_b
  458. ) API_VIS NONNULL3 NOINLINE;
  459. /**
  460. * @brief Constant-time decision between two scalars. If pick_b
  461. * is zero, out = a; else out = b.
  462. *
  463. * @param [out] out The output. It may be the same as either input.
  464. * @param [in] a Any scalar.
  465. * @param [in] b Any scalar.
  466. * @param [in] pick_b If nonzero, choose scalar b.
  467. */
  468. void %(c_ns)s_scalar_cond_sel (
  469. %(c_ns)s_scalar_t out,
  470. const %(c_ns)s_scalar_t a,
  471. const %(c_ns)s_scalar_t b,
  472. decaf_word_t pick_b
  473. ) API_VIS NONNULL3 NOINLINE;
  474. /**
  475. * @brief Test that a point is valid, for debugging purposes.
  476. *
  477. * @param [in] toTest The point to test.
  478. * @retval DECAF_TRUE The point is valid.
  479. * @retval DECAF_FALSE The point is invalid.
  480. */
  481. decaf_bool_t %(c_ns)s_point_valid (
  482. const %(c_ns)s_point_t toTest
  483. ) API_VIS WARN_UNUSED NONNULL1 NOINLINE;
  484. /**
  485. * @brief Torque a point, for debugging purposes. The output
  486. * will be equal to the input.
  487. *
  488. * @param [out] q The point to torque.
  489. * @param [in] p The point to torque.
  490. */
  491. void %(c_ns)s_point_debugging_torque (
  492. %(c_ns)s_point_t q,
  493. const %(c_ns)s_point_t p
  494. ) API_VIS NONNULL2 NOINLINE;
  495. /**
  496. * @brief Projectively scale a point, for debugging purposes.
  497. * The output will be equal to the input, and will be valid
  498. * even if the factor is zero.
  499. *
  500. * @param [out] q The point to scale.
  501. * @param [in] p The point to scale.
  502. * @param [in] factor Serialized GF factor to scale.
  503. */
  504. void %(c_ns)s_point_debugging_pscale (
  505. %(c_ns)s_point_t q,
  506. const %(c_ns)s_point_t p,
  507. const unsigned char factor[%(C_NS)s_SER_BYTES]
  508. ) API_VIS NONNULL2 NOINLINE;
  509. /**
  510. * @brief Almost-Elligator-like hash to curve.
  511. *
  512. * Call this function with the output of a hash to make a hash to the curve.
  513. *
  514. * This function runs Elligator2 on the %(c_ns)s Jacobi quartic model. It then
  515. * uses the isogeny to put the result in twisted Edwards form. As a result,
  516. * it is safe (cannot produce points of order 4), and would be compatible with
  517. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  518. * Edwards model.
  519. *
  520. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  521. * A factor of 2 due to the isogeny.
  522. * A factor of 2 because we quotient out the 2-torsion.
  523. *
  524. * This makes it about 8:1 overall, or 16:1 overall on curves with cofactor 8.
  525. *
  526. * Negating the input (mod q) results in the same point. Inverting the input
  527. * (mod q) results in the negative point. This is the same as Elligator.
  528. *
  529. * This function isn't quite indifferentiable from a random oracle.
  530. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  531. * Furthermore, calling it twice with independent seeds and adding the results
  532. * is indifferentiable from a random oracle.
  533. *
  534. * @param [in] hashed_data Output of some hash function.
  535. * @param [out] pt The data hashed to the curve.
  536. */
  537. void
  538. %(c_ns)s_point_from_hash_nonuniform (
  539. %(c_ns)s_point_t pt,
  540. const unsigned char hashed_data[%(C_NS)s_SER_BYTES]
  541. ) API_VIS NONNULL2 NOINLINE;
  542. /**
  543. * @brief Indifferentiable hash function encoding to curve.
  544. *
  545. * Equivalent to calling %(c_ns)s_point_from_hash_nonuniform twice and adding.
  546. *
  547. * @param [in] hashed_data Output of some hash function.
  548. * @param [out] pt The data hashed to the curve.
  549. */
  550. void %(c_ns)s_point_from_hash_uniform (
  551. %(c_ns)s_point_t pt,
  552. const unsigned char hashed_data[2*%(C_NS)s_SER_BYTES]
  553. ) API_VIS NONNULL2 NOINLINE;
  554. /**
  555. * @brief Inverse of elligator-like hash to curve.
  556. *
  557. * This function writes to the buffer, to make it so that
  558. * %(c_ns)s_point_from_hash_nonuniform(buffer) = pt if
  559. * possible. Since there may be multiple preimages, the
  560. * "which" parameter chooses between them. To ensure uniform
  561. * inverse sampling, this function succeeds or fails
  562. * independently for different "which" values.
  563. *
  564. * @param [out] recovered_hash Encoded data.
  565. * @param [in] pt The point to encode.
  566. * @param [in] which A value determining which inverse point
  567. * to return.
  568. *
  569. * @retval DECAF_SUCCESS The inverse succeeded.
  570. * @retval DECAF_FAILURE The inverse failed.
  571. */
  572. decaf_error_t
  573. %(c_ns)s_invert_elligator_nonuniform (
  574. unsigned char recovered_hash[%(C_NS)s_SER_BYTES],
  575. const %(c_ns)s_point_t pt,
  576. uint16_t which
  577. ) API_VIS NONNULL2 NOINLINE WARN_UNUSED;
  578. /**
  579. * @brief Inverse of elligator-like hash to curve.
  580. *
  581. * This function writes to the buffer, to make it so that
  582. * %(c_ns)s_point_from_hash_uniform(buffer) = pt if
  583. * possible. Since there may be multiple preimages, the
  584. * "which" parameter chooses between them. To ensure uniform
  585. * inverse sampling, this function succeeds or fails
  586. * independently for different "which" values.
  587. *
  588. * @param [out] recovered_hash Encoded data.
  589. * @param [in] pt The point to encode.
  590. * @param [in] which A value determining which inverse point
  591. * to return.
  592. *
  593. * @retval DECAF_SUCCESS The inverse succeeded.
  594. * @retval DECAF_FAILURE The inverse failed.
  595. */
  596. decaf_error_t
  597. %(c_ns)s_invert_elligator_uniform (
  598. unsigned char recovered_hash[2*%(C_NS)s_SER_BYTES],
  599. const %(c_ns)s_point_t pt,
  600. uint16_t which
  601. ) API_VIS NONNULL2 NOINLINE WARN_UNUSED;
  602. /**
  603. * @brief Overwrite scalar with zeros.
  604. */
  605. void %(c_ns)s_scalar_destroy (
  606. %(c_ns)s_scalar_t scalar
  607. ) NONNULL1 API_VIS;
  608. /**
  609. * @brief Overwrite point with zeros.
  610. * @todo Use this internally.
  611. */
  612. void %(c_ns)s_point_destroy (
  613. %(c_ns)s_point_t point
  614. ) NONNULL1 API_VIS;
  615. /**
  616. * @brief Overwrite precomputed table with zeros.
  617. */
  618. void %(c_ns)s_precomputed_destroy (
  619. %(c_ns)s_precomputed_s *pre
  620. ) NONNULL1 API_VIS;
  621. #ifdef __cplusplus
  622. } /* extern "C" */
  623. #endif
  624. """
  625. )