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.
 
 
 
 
 

773 lines
25 KiB

  1. /** @brief A group of prime order p, based on $(iso_to). */
  2. #include <decaf/common.h>
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /** @cond internal */
  7. #define $(C_NS)_SCALAR_LIMBS (($(scalar_bits)-1)/DECAF_WORD_BITS+1)
  8. /** @endcond */
  9. /** The number of bits in a scalar */
  10. #define $(C_NS)_SCALAR_BITS $(scalar_bits)
  11. /** @cond internal */
  12. #ifndef __DECAF_$(gf_shortname)_GF_DEFINED__
  13. #define __DECAF_$(gf_shortname)_GF_DEFINED__ 1
  14. /** @brief Galois field element internal structure */
  15. typedef struct gf_$(gf_shortname)_s {
  16. decaf_word_t limb[$(gf_impl_bits)/DECAF_WORD_BITS];
  17. } __attribute__((aligned(32))) gf_$(gf_shortname)_s, gf_$(gf_shortname)_t[1];
  18. #endif /* __DECAF_$(gf_shortname)_GF_DEFINED__ */
  19. /** @endcond */
  20. /** Number of bytes in a serialized point. */
  21. #define $(C_NS)_SER_BYTES $((gf_bits-2)//8 + 1)
  22. /** Number of bytes in an elligated point. For now set the same as SER_BYTES
  23. * but could be different for other curves.
  24. */
  25. #define $(C_NS)_HASH_BYTES $((gf_bits-2)//8 + 1)
  26. /** Number of bytes in a serialized scalar. */
  27. #define $(C_NS)_SCALAR_BYTES $((scalar_bits-1)//8 + 1)
  28. /** Number of bits in the "which" field of an elligator inverse */
  29. #define $(C_NS)_INVERT_ELLIGATOR_WHICH_BITS $(ceil_log2(cofactor) + 7 + elligator_onto - ((gf_bits-2) % 8))
  30. /** The cofactor the curve would have, if we hadn't removed it */
  31. #define $(C_NS)_REMOVED_COFACTOR $(cofactor)
  32. /** X$(gf_shortname) encoding ratio. */
  33. #define DECAF_X$(gf_shortname)_ENCODE_RATIO $(x_encode_ratio)
  34. /** Number of bytes in an x$(gf_shortname) public key */
  35. #define DECAF_X$(gf_shortname)_PUBLIC_BYTES $((gf_bits-1)//8 + 1)
  36. /** Number of bytes in an x$(gf_shortname) private key */
  37. #define DECAF_X$(gf_shortname)_PRIVATE_BYTES $((gf_bits-1)//8 + 1)
  38. /** Representation of a point on the elliptic curve. */
  39. typedef struct $(c_ns)_point_s {
  40. /** @cond internal */
  41. gf_$(gf_shortname)_t x,y,z,t; /* Twisted extended homogeneous coordinates */
  42. /** @endcond */
  43. } $(c_ns)_point_t[1];
  44. /** Precomputed table based on a point. Can be trivial implementation. */
  45. struct $(c_ns)_precomputed_s;
  46. /** Precomputed table based on a point. Can be trivial implementation. */
  47. typedef struct $(c_ns)_precomputed_s $(c_ns)_precomputed_s;
  48. /** Size and alignment of precomputed point tables. */
  49. DECAF_API_VIS extern const size_t $(c_ns)_sizeof_precomputed_s, $(c_ns)_alignof_precomputed_s;
  50. /** Representation of an element of the scalar field. */
  51. typedef struct $(c_ns)_scalar_s {
  52. /** @cond internal */
  53. decaf_word_t limb[$(C_NS)_SCALAR_LIMBS];
  54. /** @endcond */
  55. } $(c_ns)_scalar_t[1];
  56. #if defined _MSC_VER
  57. /** The scalar 1. */
  58. extern const $(c_ns)_scalar_t DECAF_API_VIS $(c_ns)_scalar_one;
  59. /** The scalar 0. */
  60. extern const $(c_ns)_scalar_t DECAF_API_VIS $(c_ns)_scalar_zero;
  61. /** The identity (zero) point on the curve. */
  62. extern const $(c_ns)_point_t DECAF_API_VIS $(c_ns)_point_identity;
  63. /** An arbitrarily-chosen base point on the curve. */
  64. extern const $(c_ns)_point_t DECAF_API_VIS $(c_ns)_point_base;
  65. /** Precomputed table of multiples of the base point on the curve. */
  66. extern const struct DECAF_API_VIS $(c_ns)_precomputed_s *$(c_ns)_precomputed_base;
  67. #else // _MSC_VER
  68. /** The scalar 1. */
  69. DECAF_API_VIS extern const $(c_ns)_scalar_t $(c_ns)_scalar_one;
  70. /** The scalar 0. */
  71. DECAF_API_VIS extern const $(c_ns)_scalar_t $(c_ns)_scalar_zero;
  72. /** The identity (zero) point on the curve. */
  73. DECAF_API_VIS extern const $(c_ns)_point_t $(c_ns)_point_identity;
  74. /** An arbitrarily-chosen base point on the curve. */
  75. DECAF_API_VIS extern const $(c_ns)_point_t $(c_ns)_point_base;
  76. /** Precomputed table of multiples of the base point on the curve. */
  77. DECAF_API_VIS extern const struct $(c_ns)_precomputed_s *$(c_ns)_precomputed_base;
  78. #endif // _MSC_VER
  79. /**
  80. * @brief Read a scalar from wire format or from bytes.
  81. *
  82. * @param [in] ser Serialized form of a scalar.
  83. * @param [out] out Deserialized form.
  84. *
  85. * @retval DECAF_SUCCESS The scalar was correctly encoded.
  86. * @retval DECAF_FAILURE The scalar was greater than the modulus,
  87. * and has been reduced modulo that modulus.
  88. */
  89. decaf_error_t DECAF_API_VIS $(c_ns)_scalar_decode (
  90. $(c_ns)_scalar_t out,
  91. const unsigned char ser[$(C_NS)_SCALAR_BYTES]
  92. ) DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
  93. /**
  94. * @brief Read a scalar from wire format or from bytes. Reduces mod
  95. * scalar prime.
  96. *
  97. * @param [in] ser Serialized form of a scalar.
  98. * @param [in] ser_len Length of serialized form.
  99. * @param [out] out Deserialized form.
  100. */
  101. void DECAF_API_VIS $(c_ns)_scalar_decode_long (
  102. $(c_ns)_scalar_t out,
  103. const unsigned char *ser,
  104. size_t ser_len
  105. ) DECAF_NONNULL DECAF_NOINLINE;
  106. /**
  107. * @brief Serialize a scalar to wire format.
  108. *
  109. * @param [out] ser Serialized form of a scalar.
  110. * @param [in] s Deserialized scalar.
  111. */
  112. void DECAF_API_VIS $(c_ns)_scalar_encode (
  113. unsigned char ser[$(C_NS)_SCALAR_BYTES],
  114. const $(c_ns)_scalar_t s
  115. ) DECAF_NONNULL DECAF_NOINLINE DECAF_NOINLINE;
  116. /**
  117. * @brief Add two scalars. The scalars may use the same memory.
  118. * @param [in] a One scalar.
  119. * @param [in] b Another scalar.
  120. * @param [out] out a+b.
  121. */
  122. void DECAF_API_VIS $(c_ns)_scalar_add (
  123. $(c_ns)_scalar_t out,
  124. const $(c_ns)_scalar_t a,
  125. const $(c_ns)_scalar_t b
  126. ) DECAF_NONNULL DECAF_NOINLINE;
  127. /**
  128. * @brief Compare two scalars.
  129. * @param [in] a One scalar.
  130. * @param [in] b Another scalar.
  131. * @retval DECAF_TRUE The scalars are equal.
  132. * @retval DECAF_FALSE The scalars are not equal.
  133. */
  134. decaf_bool_t DECAF_API_VIS $(c_ns)_scalar_eq (
  135. const $(c_ns)_scalar_t a,
  136. const $(c_ns)_scalar_t b
  137. ) DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
  138. /**
  139. * @brief Subtract two scalars. The scalars may use the same memory.
  140. * @param [in] a One scalar.
  141. * @param [in] b Another scalar.
  142. * @param [out] out a-b.
  143. */
  144. void DECAF_API_VIS $(c_ns)_scalar_sub (
  145. $(c_ns)_scalar_t out,
  146. const $(c_ns)_scalar_t a,
  147. const $(c_ns)_scalar_t b
  148. ) DECAF_NONNULL DECAF_NOINLINE;
  149. /**
  150. * @brief Multiply two scalars. The scalars may use the same memory.
  151. * @param [in] a One scalar.
  152. * @param [in] b Another scalar.
  153. * @param [out] out a*b.
  154. */
  155. void DECAF_API_VIS $(c_ns)_scalar_mul (
  156. $(c_ns)_scalar_t out,
  157. const $(c_ns)_scalar_t a,
  158. const $(c_ns)_scalar_t b
  159. ) DECAF_NONNULL DECAF_NOINLINE;
  160. /**
  161. * @brief Halve a scalar. The scalars may use the same memory.
  162. * @param [in] a A scalar.
  163. * @param [out] out a/2.
  164. */
  165. void DECAF_API_VIS $(c_ns)_scalar_halve (
  166. $(c_ns)_scalar_t out,
  167. const $(c_ns)_scalar_t a
  168. ) DECAF_NONNULL DECAF_NOINLINE;
  169. /**
  170. * @brief Invert a scalar. When passed zero, return 0. The input and output may alias.
  171. * @param [in] a A scalar.
  172. * @param [out] out 1/a.
  173. * @return DECAF_SUCCESS The input is nonzero.
  174. */
  175. decaf_error_t DECAF_API_VIS $(c_ns)_scalar_invert (
  176. $(c_ns)_scalar_t out,
  177. const $(c_ns)_scalar_t a
  178. ) DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
  179. /**
  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.
  184. */
  185. static inline void DECAF_NONNULL $(c_ns)_scalar_copy (
  186. $(c_ns)_scalar_t out,
  187. const $(c_ns)_scalar_t a
  188. ) {
  189. *out = *a;
  190. }
  191. /**
  192. * @brief Set a scalar to an unsigned 64-bit integer.
  193. * @param [in] a An integer.
  194. * @param [out] out Will become equal to a.
  195. */
  196. void DECAF_API_VIS $(c_ns)_scalar_set_unsigned (
  197. $(c_ns)_scalar_t out,
  198. uint64_t a
  199. ) DECAF_NONNULL;
  200. /**
  201. * @brief Encode a point as a sequence of bytes.
  202. *
  203. * @param [out] ser The byte representation of the point.
  204. * @param [in] pt The point to encode.
  205. */
  206. void DECAF_API_VIS $(c_ns)_point_encode (
  207. uint8_t ser[$(C_NS)_SER_BYTES],
  208. const $(c_ns)_point_t pt
  209. ) DECAF_NONNULL DECAF_NOINLINE;
  210. /**
  211. * @brief Decode a point from a sequence of bytes.
  212. *
  213. * Every point has a unique encoding, so not every
  214. * sequence of bytes is a valid encoding. If an invalid
  215. * encoding is given, the output is undefined.
  216. *
  217. * @param [out] pt The decoded point.
  218. * @param [in] ser The serialized version of the point.
  219. * @param [in] allow_identity DECAF_TRUE if the identity is a legal input.
  220. * @retval DECAF_SUCCESS The decoding succeeded.
  221. * @retval DECAF_FAILURE The decoding didn't succeed, because
  222. * ser does not represent a point.
  223. */
  224. decaf_error_t DECAF_API_VIS $(c_ns)_point_decode (
  225. $(c_ns)_point_t pt,
  226. const uint8_t ser[$(C_NS)_SER_BYTES],
  227. decaf_bool_t allow_identity
  228. ) DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
  229. /**
  230. * @brief Copy a point. The input and output may alias,
  231. * in which case this function does nothing.
  232. *
  233. * @param [out] a A copy of the point.
  234. * @param [in] b Any point.
  235. */
  236. static inline void DECAF_NONNULL $(c_ns)_point_copy (
  237. $(c_ns)_point_t a,
  238. const $(c_ns)_point_t b
  239. ) {
  240. *a=*b;
  241. }
  242. /**
  243. * @brief Test whether two points are equal. If yes, return
  244. * DECAF_TRUE, else return DECAF_FALSE.
  245. *
  246. * @param [in] a A point.
  247. * @param [in] b Another point.
  248. * @retval DECAF_TRUE The points are equal.
  249. * @retval DECAF_FALSE The points are not equal.
  250. */
  251. decaf_bool_t DECAF_API_VIS $(c_ns)_point_eq (
  252. const $(c_ns)_point_t a,
  253. const $(c_ns)_point_t b
  254. ) DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
  255. /**
  256. * @brief Add two points to produce a third point. The
  257. * input points and output point can be pointers to the same
  258. * memory.
  259. *
  260. * @param [out] sum The sum a+b.
  261. * @param [in] a An addend.
  262. * @param [in] b An addend.
  263. */
  264. void DECAF_API_VIS $(c_ns)_point_add (
  265. $(c_ns)_point_t sum,
  266. const $(c_ns)_point_t a,
  267. const $(c_ns)_point_t b
  268. ) DECAF_NONNULL;
  269. /**
  270. * @brief Double a point. Equivalent to
  271. * $(c_ns)_point_add(two_a,a,a), but potentially faster.
  272. *
  273. * @param [out] two_a The sum a+a.
  274. * @param [in] a A point.
  275. */
  276. void DECAF_API_VIS $(c_ns)_point_double (
  277. $(c_ns)_point_t two_a,
  278. const $(c_ns)_point_t a
  279. ) DECAF_NONNULL;
  280. /**
  281. * @brief Subtract two points to produce a third point. The
  282. * input points and output point can be pointers to the same
  283. * memory.
  284. *
  285. * @param [out] diff The difference a-b.
  286. * @param [in] a The minuend.
  287. * @param [in] b The subtrahend.
  288. */
  289. void DECAF_API_VIS $(c_ns)_point_sub (
  290. $(c_ns)_point_t diff,
  291. const $(c_ns)_point_t a,
  292. const $(c_ns)_point_t b
  293. ) DECAF_NONNULL;
  294. /**
  295. * @brief Negate a point to produce another point. The input
  296. * and output points can use the same memory.
  297. *
  298. * @param [out] nega The negated input point
  299. * @param [in] a The input point.
  300. */
  301. void DECAF_API_VIS $(c_ns)_point_negate (
  302. $(c_ns)_point_t nega,
  303. const $(c_ns)_point_t a
  304. ) DECAF_NONNULL;
  305. /**
  306. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  307. *
  308. * @param [out] scaled The scaled point base*scalar
  309. * @param [in] base The point to be scaled.
  310. * @param [in] scalar The scalar to multiply by.
  311. */
  312. void DECAF_API_VIS $(c_ns)_point_scalarmul (
  313. $(c_ns)_point_t scaled,
  314. const $(c_ns)_point_t base,
  315. const $(c_ns)_scalar_t scalar
  316. ) DECAF_NONNULL DECAF_NOINLINE;
  317. /**
  318. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  319. * This function operates directly on serialized forms.
  320. *
  321. * @warning This function is experimental. It may not be supported
  322. * long-term.
  323. *
  324. * @param [out] scaled The scaled point base*scalar
  325. * @param [in] base The point to be scaled.
  326. * @param [in] scalar The scalar to multiply by.
  327. * @param [in] allow_identity Allow the input to be the identity.
  328. * @param [in] short_circuit Allow a fast return if the input is illegal.
  329. *
  330. * @retval DECAF_SUCCESS The scalarmul succeeded.
  331. * @retval DECAF_FAILURE The scalarmul didn't succeed, because
  332. * base does not represent a point.
  333. */
  334. decaf_error_t DECAF_API_VIS $(c_ns)_direct_scalarmul (
  335. uint8_t scaled[$(C_NS)_SER_BYTES],
  336. const uint8_t base[$(C_NS)_SER_BYTES],
  337. const $(c_ns)_scalar_t scalar,
  338. decaf_bool_t allow_identity,
  339. decaf_bool_t short_circuit
  340. ) DECAF_NONNULL DECAF_WARN_UNUSED DECAF_NOINLINE;
  341. /**
  342. * @brief RFC 7748 Diffie-Hellman scalarmul, used to compute shared secrets.
  343. * This function uses a different (non-Decaf) encoding.
  344. *
  345. * @param [out] shared The shared secret base*scalar
  346. * @param [in] base The other party's public key, used as the base of the scalarmul.
  347. * @param [in] scalar The private scalar to multiply by.
  348. *
  349. * @retval DECAF_SUCCESS The scalarmul succeeded.
  350. * @retval DECAF_FAILURE The scalarmul didn't succeed, because the base
  351. * point is in a small subgroup.
  352. */
  353. decaf_error_t DECAF_API_VIS decaf_x$(gf_shortname) (
  354. uint8_t shared[DECAF_X$(gf_shortname)_PUBLIC_BYTES],
  355. const uint8_t base[DECAF_X$(gf_shortname)_PUBLIC_BYTES],
  356. const uint8_t scalar[DECAF_X$(gf_shortname)_PRIVATE_BYTES]
  357. ) DECAF_NONNULL DECAF_WARN_UNUSED DECAF_NOINLINE;
  358. /**
  359. * @brief Multiply a point by DECAF_X$(gf_shortname)_ENCODE_RATIO,
  360. * then encode it like RFC 7748.
  361. *
  362. * This function is mainly used internally, but is exported in case
  363. * it will be useful.
  364. *
  365. * The ratio is necessary because the internal representation doesn't
  366. * track the cofactor information, so on output we must clear the cofactor.
  367. * This would multiply by the cofactor, but in fact internally libdecaf's
  368. * points are always even, so it multiplies by half the cofactor instead.
  369. *
  370. * As it happens, this aligns with the base point definitions; that is,
  371. * if you pass the Decaf/Ristretto base point to this function, the result
  372. * will be DECAF_X$(gf_shortname)_ENCODE_RATIO times the X$(gf_shortname)
  373. * base point.
  374. *
  375. * @param [out] out The scaled and encoded point.
  376. * @param [in] p The point to be scaled and encoded.
  377. */
  378. void DECAF_API_VIS $(c_ns)_point_mul_by_ratio_and_encode_like_x$(gf_shortname) (
  379. uint8_t out[DECAF_X$(gf_shortname)_PUBLIC_BYTES],
  380. const $(c_ns)_point_t p
  381. ) DECAF_NONNULL;
  382. /** The base point for X$(gf_shortname) Diffie-Hellman */
  383. extern const uint8_t
  384. #ifndef DOXYGEN
  385. /* For some reason Doxygen chokes on this despite the defense in common.h... */
  386. DECAF_API_VIS
  387. #endif
  388. decaf_x$(gf_shortname)_base_point[DECAF_X$(gf_shortname)_PUBLIC_BYTES];
  389. /**
  390. * @brief RFC 7748 Diffie-Hellman base point scalarmul. This function uses
  391. * a different (non-Decaf) encoding.
  392. *
  393. * @deprecated Renamed to decaf_x$(gf_shortname)_derive_public_key.
  394. * I have no particular timeline for removing this name.
  395. *
  396. * @param [out] out The public key base*scalar.
  397. * @param [in] scalar The private scalar.
  398. */
  399. void DECAF_API_VIS decaf_x$(gf_shortname)_generate_key (
  400. uint8_t out[DECAF_X$(gf_shortname)_PUBLIC_BYTES],
  401. const uint8_t scalar[DECAF_X$(gf_shortname)_PRIVATE_BYTES]
  402. ) DECAF_NONNULL DECAF_NOINLINE DECAF_DEPRECATED("Renamed to decaf_x$(gf_shortname)_derive_public_key");
  403. /**
  404. * @brief RFC 7748 Diffie-Hellman base point scalarmul. This function uses
  405. * a different (non-Decaf) encoding.
  406. *
  407. * Does exactly the same thing as decaf_x$(gf_shortname)_generate_key,
  408. * but has a better name.
  409. *
  410. * @param [out] out The public key base*scalar
  411. * @param [in] scalar The private scalar.
  412. */
  413. void DECAF_API_VIS decaf_x$(gf_shortname)_derive_public_key (
  414. uint8_t out[DECAF_X$(gf_shortname)_PUBLIC_BYTES],
  415. const uint8_t scalar[DECAF_X$(gf_shortname)_PRIVATE_BYTES]
  416. ) DECAF_NONNULL DECAF_NOINLINE;
  417. /* FUTURE: uint8_t $(c_ns)_encode_like_curve$(gf_shortname)) */
  418. /**
  419. * @brief Precompute a table for fast scalar multiplication.
  420. * Some implementations do not include precomputed points; for
  421. * those implementations, this implementation simply copies the
  422. * point.
  423. *
  424. * @param [out] a A precomputed table of multiples of the point.
  425. * @param [in] b Any point.
  426. */
  427. void DECAF_API_VIS $(c_ns)_precompute (
  428. $(c_ns)_precomputed_s *a,
  429. const $(c_ns)_point_t b
  430. ) DECAF_NONNULL DECAF_NOINLINE;
  431. /**
  432. * @brief Multiply a precomputed base point by a scalar:
  433. * scaled = scalar*base.
  434. * Some implementations do not include precomputed points; for
  435. * those implementations, this function is the same as
  436. * $(c_ns)_point_scalarmul
  437. *
  438. * @param [out] scaled The scaled point base*scalar
  439. * @param [in] base The point to be scaled.
  440. * @param [in] scalar The scalar to multiply by.
  441. */
  442. void DECAF_API_VIS $(c_ns)_precomputed_scalarmul (
  443. $(c_ns)_point_t scaled,
  444. const $(c_ns)_precomputed_s *base,
  445. const $(c_ns)_scalar_t scalar
  446. ) DECAF_NONNULL DECAF_NOINLINE;
  447. /**
  448. * @brief Multiply two base points by two scalars:
  449. * scaled = scalar1*base1 + scalar2*base2.
  450. *
  451. * Equivalent to two calls to $(c_ns)_point_scalarmul, but may be
  452. * faster.
  453. *
  454. * @param [out] combo The linear combination scalar1*base1 + scalar2*base2.
  455. * @param [in] base1 A first point to be scaled.
  456. * @param [in] scalar1 A first scalar to multiply by.
  457. * @param [in] base2 A second point to be scaled.
  458. * @param [in] scalar2 A second scalar to multiply by.
  459. */
  460. void DECAF_API_VIS $(c_ns)_point_double_scalarmul (
  461. $(c_ns)_point_t combo,
  462. const $(c_ns)_point_t base1,
  463. const $(c_ns)_scalar_t scalar1,
  464. const $(c_ns)_point_t base2,
  465. const $(c_ns)_scalar_t scalar2
  466. ) DECAF_NONNULL DECAF_NOINLINE;
  467. /**
  468. * Multiply one base point by two scalars:
  469. *
  470. * a1 = scalar1 * base
  471. * a2 = scalar2 * base
  472. *
  473. * Equivalent to two calls to $(c_ns)_point_scalarmul, but may be
  474. * faster.
  475. *
  476. * @param [out] a1 The first multiple. It may be the same as the input point.
  477. * @param [out] a2 The second multiple. It may be the same as the input point.
  478. * @param [in] base1 A point to be scaled.
  479. * @param [in] scalar1 A first scalar to multiply by.
  480. * @param [in] scalar2 A second scalar to multiply by.
  481. */
  482. void DECAF_API_VIS $(c_ns)_point_dual_scalarmul (
  483. $(c_ns)_point_t a1,
  484. $(c_ns)_point_t a2,
  485. const $(c_ns)_point_t base1,
  486. const $(c_ns)_scalar_t scalar1,
  487. const $(c_ns)_scalar_t scalar2
  488. ) DECAF_NONNULL DECAF_NOINLINE;
  489. /**
  490. * @brief Multiply two base points by two scalars:
  491. * scaled = scalar1*$(c_ns)_point_base + scalar2*base2.
  492. *
  493. * Otherwise equivalent to $(c_ns)_point_double_scalarmul, but may be
  494. * faster at the expense of being variable time.
  495. *
  496. * @param [out] combo The linear combination scalar1*base + scalar2*base2.
  497. * @param [in] scalar1 A first scalar to multiply by.
  498. * @param [in] base2 A second point to be scaled.
  499. * @param [in] scalar2 A second scalar to multiply by.
  500. *
  501. * @warning: This function takes variable time, and may leak the scalars
  502. * used. It is designed for signature verification.
  503. */
  504. void DECAF_API_VIS $(c_ns)_base_double_scalarmul_non_secret (
  505. $(c_ns)_point_t combo,
  506. const $(c_ns)_scalar_t scalar1,
  507. const $(c_ns)_point_t base2,
  508. const $(c_ns)_scalar_t scalar2
  509. ) DECAF_NONNULL DECAF_NOINLINE;
  510. /**
  511. * @brief Constant-time decision between two points. If pick_b
  512. * is zero, out = a; else out = b.
  513. *
  514. * @param [out] out The output. It may be the same as either input.
  515. * @param [in] a Any point.
  516. * @param [in] b Any point.
  517. * @param [in] pick_b If nonzero, choose point b.
  518. */
  519. void DECAF_API_VIS $(c_ns)_point_cond_sel (
  520. $(c_ns)_point_t out,
  521. const $(c_ns)_point_t a,
  522. const $(c_ns)_point_t b,
  523. decaf_word_t pick_b
  524. ) DECAF_NONNULL DECAF_NOINLINE;
  525. /**
  526. * @brief Constant-time decision between two scalars. If pick_b
  527. * is zero, out = a; else out = b.
  528. *
  529. * @param [out] out The output. It may be the same as either input.
  530. * @param [in] a Any scalar.
  531. * @param [in] b Any scalar.
  532. * @param [in] pick_b If nonzero, choose scalar b.
  533. */
  534. void DECAF_API_VIS $(c_ns)_scalar_cond_sel (
  535. $(c_ns)_scalar_t out,
  536. const $(c_ns)_scalar_t a,
  537. const $(c_ns)_scalar_t b,
  538. decaf_word_t pick_b
  539. ) DECAF_NONNULL DECAF_NOINLINE;
  540. /**
  541. * @brief Test that a point is valid, for debugging purposes.
  542. *
  543. * @param [in] to_test The point to test.
  544. * @retval DECAF_TRUE The point is valid.
  545. * @retval DECAF_FALSE The point is invalid.
  546. */
  547. decaf_bool_t DECAF_API_VIS $(c_ns)_point_valid (
  548. const $(c_ns)_point_t to_test
  549. ) DECAF_WARN_UNUSED DECAF_NONNULL DECAF_NOINLINE;
  550. /**
  551. * @brief Torque a point, for debugging purposes. The output
  552. * will be equal to the input.
  553. *
  554. * @param [out] q The point to torque.
  555. * @param [in] p The point to torque.
  556. */
  557. void DECAF_API_VIS $(c_ns)_point_debugging_torque (
  558. $(c_ns)_point_t q,
  559. const $(c_ns)_point_t p
  560. ) DECAF_NONNULL DECAF_NOINLINE;
  561. /**
  562. * @brief Projectively scale a point, for debugging purposes.
  563. * The output will be equal to the input, and will be valid
  564. * even if the factor is zero.
  565. *
  566. * @param [out] q The point to scale.
  567. * @param [in] p The point to scale.
  568. * @param [in] factor Serialized GF factor to scale.
  569. */
  570. void DECAF_API_VIS $(c_ns)_point_debugging_pscale (
  571. $(c_ns)_point_t q,
  572. const $(c_ns)_point_t p,
  573. const unsigned char factor[$(C_NS)_SER_BYTES]
  574. ) DECAF_NONNULL DECAF_NOINLINE;
  575. /**
  576. * @brief Almost-Elligator-like hash to curve.
  577. *
  578. * Call this function with the output of a hash to make a hash to the curve.
  579. *
  580. * This function runs Elligator2 on the $(c_ns) Jacobi quartic model. It then
  581. * uses the isogeny to put the result in twisted Edwards form. As a result,
  582. * it is safe (cannot produce points of order 4), and would be compatible with
  583. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  584. * Edwards model.
  585. *
  586. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  587. * A factor of 2 due to the isogeny.
  588. * A factor of 2 because we quotient out the 2-torsion.
  589. *
  590. * This makes it about 8:1 overall, or 16:1 overall on curves with cofactor 8.
  591. *
  592. * Negating the input (mod q) results in the same point. Inverting the input
  593. * (mod q) results in the negative point. This is the same as Elligator.
  594. *
  595. * This function isn't quite indifferentiable from a random oracle.
  596. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  597. * Furthermore, calling it twice with independent seeds and adding the results
  598. * is indifferentiable from a random oracle.
  599. *
  600. * @param [in] hashed_data Output of some hash function.
  601. * @param [out] pt The data hashed to the curve.
  602. */
  603. void DECAF_API_VIS
  604. $(c_ns)_point_from_hash_nonuniform (
  605. $(c_ns)_point_t pt,
  606. const unsigned char hashed_data[$(C_NS)_HASH_BYTES]
  607. ) DECAF_NONNULL DECAF_NOINLINE;
  608. /**
  609. * @brief Indifferentiable hash function encoding to curve.
  610. *
  611. * Equivalent to calling $(c_ns)_point_from_hash_nonuniform twice and adding.
  612. *
  613. * @param [in] hashed_data Output of some hash function.
  614. * @param [out] pt The data hashed to the curve.
  615. */
  616. void DECAF_API_VIS $(c_ns)_point_from_hash_uniform (
  617. $(c_ns)_point_t pt,
  618. const unsigned char hashed_data[2*$(C_NS)_HASH_BYTES]
  619. ) DECAF_NONNULL DECAF_NOINLINE;
  620. /**
  621. * @brief Inverse of elligator-like hash to curve.
  622. *
  623. * This function writes to the buffer, to make it so that
  624. * $(c_ns)_point_from_hash_nonuniform(buffer) = pt if
  625. * possible. Since there may be multiple preimages, the
  626. * "which" parameter chooses between them. To ensure uniform
  627. * inverse sampling, this function succeeds or fails
  628. * independently for different "which" values.
  629. *
  630. * This function isn't guaranteed to find every possible
  631. * preimage, but it finds all except a small finite number.
  632. * In particular, when the number of bits in the modulus isn't
  633. * a multiple of 8 (i.e. for curve25519), it sets the high bits
  634. * independently, which enables the generated data to be uniform.
  635. * But it doesn't add p, so you'll never get exactly p from this
  636. * function. This might change in the future, especially if
  637. * we ever support eg Brainpool curves, where this could cause
  638. * real nonuniformity.
  639. *
  640. * @param [out] recovered_hash Encoded data.
  641. * @param [in] pt The point to encode.
  642. * @param [in] which A value determining which inverse point
  643. * to return.
  644. *
  645. * @retval DECAF_SUCCESS The inverse succeeded.
  646. * @retval DECAF_FAILURE The inverse failed.
  647. */
  648. decaf_error_t DECAF_API_VIS
  649. $(c_ns)_invert_elligator_nonuniform (
  650. unsigned char recovered_hash[$(C_NS)_HASH_BYTES],
  651. const $(c_ns)_point_t pt,
  652. uint32_t which
  653. ) DECAF_NONNULL DECAF_NOINLINE DECAF_WARN_UNUSED;
  654. /**
  655. * @brief Inverse of elligator-like hash to curve.
  656. *
  657. * This function writes to the buffer, to make it so that
  658. * $(c_ns)_point_from_hash_uniform(buffer) = pt if
  659. * possible. Since there may be multiple preimages, the
  660. * "which" parameter chooses between them. To ensure uniform
  661. * inverse sampling, this function succeeds or fails
  662. * independently for different "which" values.
  663. *
  664. * @param [out] recovered_hash Encoded data.
  665. * @param [in] pt The point to encode.
  666. * @param [in] which A value determining which inverse point
  667. * to return.
  668. *
  669. * @retval DECAF_SUCCESS The inverse succeeded.
  670. * @retval DECAF_FAILURE The inverse failed.
  671. */
  672. decaf_error_t DECAF_API_VIS
  673. $(c_ns)_invert_elligator_uniform (
  674. unsigned char recovered_hash[2*$(C_NS)_HASH_BYTES],
  675. const $(c_ns)_point_t pt,
  676. uint32_t which
  677. ) DECAF_NONNULL DECAF_NOINLINE DECAF_WARN_UNUSED;
  678. /** Securely erase a scalar. */
  679. void DECAF_API_VIS $(c_ns)_scalar_destroy (
  680. $(c_ns)_scalar_t scalar
  681. ) DECAF_NONNULL;
  682. /** Securely erase a point by overwriting it with zeros.
  683. * @warning This causes the point object to become invalid.
  684. */
  685. void DECAF_API_VIS $(c_ns)_point_destroy (
  686. $(c_ns)_point_t point
  687. ) DECAF_NONNULL;
  688. /** Securely erase a precomputed table by overwriting it with zeros.
  689. * @warning This causes the table object to become invalid.
  690. */
  691. void DECAF_API_VIS $(c_ns)_precomputed_destroy (
  692. $(c_ns)_precomputed_s *pre
  693. ) DECAF_NONNULL;
  694. #ifdef __cplusplus
  695. } /* extern "C" */
  696. #endif