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.
 
 
 
 
 

589 lines
22 KiB

  1. /**
  2. * @file decaf.hxx
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. * @brief A group of prime order p, C++ wrapper.
  10. *
  11. * The Decaf library implements cryptographic operations on a an elliptic curve
  12. * group of prime order p. It accomplishes this by using a twisted Edwards
  13. * curve (isogenous to Ed448-Goldilocks) and wiping out the cofactor.
  14. *
  15. * The formulas are all complete and have no special cases, except that
  16. * decaf_448_decode can fail because not every sequence of bytes is a valid group
  17. * element.
  18. *
  19. * The formulas contain no data-dependent branches, timing or memory accesses,
  20. * except for decaf_448_base_double_scalarmul_non_secret.
  21. */
  22. #ifndef __DECAF_448_HXX__
  23. #define __DECAF_448_HXX__ 1
  24. /** This code uses posix_memalign. */
  25. #define _XOPEN_SOURCE 600
  26. #include <stdlib.h>
  27. #include <string.h> /* for memcpy */
  28. #include "decaf.h"
  29. #include <string>
  30. #include <sys/types.h>
  31. #include <limits.h>
  32. /* TODO: This is incomplete */
  33. /* TODO: attribute nonnull */
  34. /** @cond internal */
  35. #if __cplusplus >= 201103L
  36. #define NOEXCEPT noexcept
  37. #define EXPLICIT_CON explicit
  38. #define GET_DATA(str) ((const unsigned char *)&(str)[0])
  39. #else
  40. #define NOEXCEPT throw()
  41. #define EXPLICIT_CON
  42. #define GET_DATA(str) ((const unsigned char *)((str).data()))
  43. #endif
  44. /** @endcond */
  45. namespace decaf {
  46. /**
  47. * Securely erase contents of memory.
  48. */
  49. static inline void really_bzero(void *data, size_t size) { decaf_bzero(data,size); }
  50. /**
  51. * @brief Group with prime order.
  52. * @todo Move declarations of functions up here?
  53. */
  54. template<unsigned int bits = 448> struct decaf;
  55. /** @brief Passed to constructors to avoid (conservative) initialization */
  56. struct NOINIT {};
  57. /**@cond internal*/
  58. /** Forward-declare sponge RNG object */
  59. class SpongeRng;
  60. /**@endcond*/
  61. /**
  62. * @brief Ed448-Goldilocks/Decaf instantiation of group.
  63. */
  64. template<> struct decaf<448> {
  65. /** @brief An exception for when crypto (ie point decode) has failed. */
  66. class CryptoException : public std::exception {
  67. public:
  68. /** @return "CryptoException" */
  69. virtual const char * what() const NOEXCEPT { return "CryptoException"; }
  70. };
  71. /** @cond internal */
  72. class Point;
  73. class Precomputed;
  74. /** @endcond */
  75. /**
  76. * @brief A scalar modulo the curve order.
  77. * Supports the usual arithmetic operations, all in constant time.
  78. */
  79. class Scalar {
  80. public:
  81. /** @brief Size of a serialized element */
  82. static const size_t SER_BYTES = DECAF_448_SCALAR_BYTES;
  83. /** @brief access to the underlying scalar object */
  84. decaf_448_scalar_t s;
  85. /** @brief Don't initialize. */
  86. inline Scalar(const NOINIT &) {}
  87. /** @brief Set to an unsigned word */
  88. inline Scalar(const decaf_word_t w) NOEXCEPT { *this = w; }
  89. /** @brief Set to a signed word */
  90. inline Scalar(const int w) NOEXCEPT { *this = w; }
  91. /** @brief Construct from RNG */
  92. inline explicit Scalar(SpongeRng &rng);
  93. /** @brief Construct from decaf_scalar_t object. */
  94. inline Scalar(const decaf_448_scalar_t &t = decaf_448_scalar_zero) NOEXCEPT { decaf_448_scalar_copy(s,t); }
  95. /** @brief Copy constructor. */
  96. inline Scalar(const Scalar &x) NOEXCEPT { *this = x; }
  97. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  98. inline explicit Scalar(const std::string &str) NOEXCEPT { *this = str; }
  99. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  100. inline Scalar(const unsigned char *buffer, size_t n) NOEXCEPT { decaf_448_scalar_decode_long(s,buffer,n); }
  101. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  102. inline Scalar(const char *buffer, size_t n) NOEXCEPT { decaf_448_scalar_decode_long(s,(const unsigned char *)buffer,n); }
  103. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  104. inline Scalar(const void *buffer, size_t n) NOEXCEPT { decaf_448_scalar_decode_long(s,(const unsigned char *)buffer,n); }
  105. /** @brief Assignment. */
  106. inline Scalar& operator=(const Scalar &x) NOEXCEPT { decaf_448_scalar_copy(s,x.s); return *this; }
  107. /** @brief Assign from unsigned word. */
  108. inline Scalar& operator=(decaf_word_t w) NOEXCEPT { decaf_448_scalar_set(s,w); return *this; }
  109. /** @brief Assign from signed int. */
  110. inline Scalar& operator=(int w) {
  111. Scalar t(-(decaf_word_t)INT_MIN);
  112. decaf_448_scalar_set(s,(decaf_word_t)w - (decaf_word_t)INT_MIN);
  113. *this -= t;
  114. return *this;
  115. }
  116. /** Destructor securely erases the scalar. */
  117. inline ~Scalar() NOEXCEPT { decaf_448_scalar_destroy(s); }
  118. /** @brief Assign from arbitrary-length little-endian byte sequence in C++ string. */
  119. inline Scalar &operator=(const std::string &str) NOEXCEPT {
  120. decaf_448_scalar_decode_long(s,GET_DATA(str),str.size()); return *this;
  121. }
  122. /**
  123. * @brief Decode from correct-length little-endian byte sequence.
  124. * @return DECAF_FAILURE if the scalar is greater than or equal to the group order q.
  125. */
  126. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  127. Scalar &sc, const unsigned char buffer[SER_BYTES]
  128. ) NOEXCEPT {
  129. return decaf_448_scalar_decode(sc.s,buffer);
  130. }
  131. /** @brief Decode from correct-length little-endian byte sequence in C++ string. */
  132. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  133. Scalar &sc, const std::string buffer
  134. ) NOEXCEPT {
  135. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  136. return decaf_448_scalar_decode(sc.s,GET_DATA(buffer));
  137. }
  138. /** @brief Encode to fixed-length string */
  139. inline EXPLICIT_CON operator std::string() const NOEXCEPT {
  140. unsigned char buffer[SER_BYTES];
  141. decaf_448_scalar_encode(buffer, s);
  142. return std::string((char*)buffer,sizeof(buffer));
  143. }
  144. /** @brief Encode to fixed-length buffer */
  145. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  146. decaf_448_scalar_encode(buffer, s);
  147. }
  148. /** Add. */
  149. inline Scalar operator+ (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_add(r.s,s,q.s); return r; }
  150. /** Add to this. */
  151. inline Scalar &operator+=(const Scalar &q) NOEXCEPT { decaf_448_scalar_add(s,s,q.s); return *this; }
  152. /** Subtract. */
  153. inline Scalar operator- (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_sub(r.s,s,q.s); return r; }
  154. /** Subtract from this. */
  155. inline Scalar &operator-=(const Scalar &q) NOEXCEPT { decaf_448_scalar_sub(s,s,q.s); return *this; }
  156. /** Multiply */
  157. inline Scalar operator* (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_mul(r.s,s,q.s); return r; }
  158. /** Multiply into this. */
  159. inline Scalar &operator*=(const Scalar &q) NOEXCEPT { decaf_448_scalar_mul(s,s,q.s); return *this; }
  160. /** Negate */
  161. inline Scalar operator- () const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_sub(r.s,decaf_448_scalar_zero,s); return r; }
  162. /** @brief Invert with Fermat's Little Theorem (slow!). If *this == 0, return 0. */
  163. inline Scalar inverse() const NOEXCEPT { Scalar r; decaf_448_scalar_invert(r.s,s); return r; }
  164. /** @brief Divide by inverting q. If q == 0, return 0. */
  165. inline Scalar operator/ (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_mul(r.s,s,q.inverse().s); return r; }
  166. /** @brief Divide by inverting q. If q == 0, return 0. */
  167. inline Scalar &operator/=(const Scalar &q) NOEXCEPT { decaf_448_scalar_mul(s,s,q.inverse().s); return *this; }
  168. /** @brief Compare in constant time */
  169. inline bool operator!=(const Scalar &q) const NOEXCEPT { return ! decaf_448_scalar_eq(s,q.s); }
  170. /** @brief Compare in constant time */
  171. inline bool operator==(const Scalar &q) const NOEXCEPT { return !!decaf_448_scalar_eq(s,q.s); }
  172. /** @brief Scalarmul with scalar on left. */
  173. inline Point operator* (const Point &q) const NOEXCEPT { return q * (*this); }
  174. /** @brief Scalarmul-precomputed with scalar on left. */
  175. inline Point operator* (const Precomputed &q) const NOEXCEPT { return q * (*this); }
  176. /** @brief Direct scalar multiplication.
  177. * @todo Fix up bools.
  178. */
  179. inline decaf_bool_t direct_scalarmul(
  180. unsigned char out[SER_BYTES],
  181. const unsigned char in[SER_BYTES],
  182. decaf_bool_t allow_identity=DECAF_FALSE,
  183. decaf_bool_t short_circuit=DECAF_TRUE
  184. ) const NOEXCEPT {
  185. return decaf_448_direct_scalarmul(out, in, s, allow_identity, short_circuit);
  186. }
  187. /** @brief Direct scalar multiplication.
  188. * @todo Fix up bools.
  189. */
  190. inline std::string direct_scalarmul(
  191. const std::string in,
  192. decaf_bool_t allow_identity=DECAF_FALSE,
  193. decaf_bool_t short_circuit=DECAF_TRUE
  194. ) const NOEXCEPT {
  195. unsigned char out[SER_BYTES];
  196. if (decaf_448_direct_scalarmul(out, GET_DATA(in), s, allow_identity, short_circuit)) {
  197. return std::string((char *)out,sizeof(out));
  198. } else {
  199. return "";
  200. }
  201. }
  202. };
  203. /**
  204. * @brief Element of prime-order group.
  205. */
  206. class Point {
  207. public:
  208. /** @brief Size of a serialized element */
  209. static const size_t SER_BYTES = DECAF_448_SER_BYTES;
  210. /** @brief Bytes required for hash */
  211. static const size_t HASH_BYTES = DECAF_448_SER_BYTES;
  212. /** The c-level object. */
  213. decaf_448_point_t p;
  214. /** @brief Don't initialize. */
  215. inline Point(const NOINIT &) {}
  216. /** @brief Constructor sets to identity by default. */
  217. inline Point(const decaf_448_point_t &q = decaf_448_point_identity) { decaf_448_point_copy(p,q); }
  218. /** @brief Copy constructor. */
  219. inline Point(const Point &q) { decaf_448_point_copy(p,q.p); }
  220. /** @brief Assignment. */
  221. inline Point& operator=(const Point &q) { decaf_448_point_copy(p,q.p); return *this; }
  222. /** @brief Destructor securely erases the point. */
  223. inline ~Point() { decaf_448_point_destroy(p); }
  224. /** @brief Construct from RNG */
  225. inline explicit Point(SpongeRng &rng, bool uniform = true);
  226. /**
  227. * @brief Initialize from C++ fixed-length byte string.
  228. * The all-zero string maps to the identity.
  229. *
  230. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  231. * or was the identity and allow_identity was DECAF_FALSE.
  232. */
  233. inline explicit Point(const std::string &s, decaf_bool_t allow_identity=DECAF_TRUE) throw(CryptoException) {
  234. if (!decode(*this,s,allow_identity)) throw CryptoException();
  235. }
  236. /**
  237. * @brief Initialize from C fixed-length byte string.
  238. * The all-zero string maps to the identity.
  239. *
  240. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  241. * or was the identity and allow_identity was DECAF_FALSE.
  242. */
  243. inline explicit Point(const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE)
  244. throw(CryptoException) { if (!decode(*this,buffer,allow_identity)) throw CryptoException(); }
  245. /**
  246. * @brief Initialize from C fixed-length byte string.
  247. * The all-zero string maps to the identity.
  248. *
  249. * @retval DECAF_SUCCESS the string was successfully decoded.
  250. * @return DECAF_FAILURE the string wasn't the encoding of a point, or was the identity
  251. * and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  252. */
  253. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  254. Point &p, const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE
  255. ) NOEXCEPT {
  256. return decaf_448_point_decode(p.p,buffer,allow_identity);
  257. }
  258. /**
  259. * @brief Initialize from C++ fixed-length byte string.
  260. * The all-zero string maps to the identity.
  261. *
  262. * @retval DECAF_SUCCESS the string was successfully decoded.
  263. * @return DECAF_FAILURE the string was the wrong length, or wasn't the encoding of a point,
  264. * or was the identity and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  265. */
  266. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  267. Point &p, const std::string &buffer, decaf_bool_t allow_identity=DECAF_TRUE
  268. ) NOEXCEPT {
  269. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  270. return decaf_448_point_decode(p.p,GET_DATA(buffer),allow_identity);
  271. }
  272. /**
  273. * @brief Map to the curve from a C buffer.
  274. * The all-zero buffer maps to the identity, as does the buffer {1,0...}
  275. */
  276. static inline Point from_hash_nonuniform ( const unsigned char buffer[SER_BYTES] ) NOEXCEPT {
  277. Point p((NOINIT())); decaf_448_point_from_hash_nonuniform(p.p,buffer); return p;
  278. }
  279. /**
  280. * @brief Map to the curve from a C++ string buffer.
  281. * The empty or all-zero string maps to the identity, as does the string "\x01".
  282. * If the buffer is shorter than (TODO) SER_BYTES, it will be zero-padded on the right.
  283. */
  284. static inline Point from_hash_nonuniform ( const std::string &s ) NOEXCEPT {
  285. std::string t = s;
  286. if (t.size() < SER_BYTES) t.insert(t.size(),SER_BYTES-t.size(),0);
  287. Point p((NOINIT())); decaf_448_point_from_hash_nonuniform(p.p,GET_DATA(t)); return p;
  288. }
  289. /**
  290. * @brief Map uniformly to the curve from a C buffer.
  291. * The all-zero buffer maps to the identity, as does the buffer {1,0...}.
  292. */
  293. static inline Point from_hash ( const unsigned char buffer[2*SER_BYTES] ) NOEXCEPT {
  294. Point p((NOINIT())); decaf_448_point_from_hash_uniform(p.p,buffer); return p;
  295. }
  296. /**
  297. * @brief Map uniformly to the curve from a C++ buffer.
  298. * The empty or all-zero string maps to the identity, as does the string "\x01".
  299. * If the buffer is shorter than (TODO) 2*SER_BYTES, well, it won't be as uniform,
  300. * but the buffer will be zero-padded on the right.
  301. */
  302. static inline Point from_hash ( const std::string &s ) NOEXCEPT {
  303. std::string t = s;
  304. if (t.size() <= SER_BYTES) return from_hash_nonuniform(s);
  305. if (t.size() < 2*SER_BYTES) t.insert(t.size(),2*SER_BYTES-t.size(),0);
  306. Point p((NOINIT())); decaf_448_point_from_hash_uniform(p.p,GET_DATA(t)); return p;
  307. }
  308. /**
  309. * @brief Encode to string. The identity encodes to the all-zero string.
  310. */
  311. inline EXPLICIT_CON operator std::string() const NOEXCEPT {
  312. unsigned char buffer[SER_BYTES];
  313. decaf_448_point_encode(buffer, p);
  314. return std::string((char*)buffer,sizeof(buffer));
  315. }
  316. /**
  317. * @brief Encode to a C buffer. The identity encodes to all zeros.
  318. */
  319. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  320. decaf_448_point_encode(buffer, p);
  321. }
  322. /** @brief Point add. */
  323. inline Point operator+ (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_448_point_add(r.p,p,q.p); return r; }
  324. /** @brief Point add. */
  325. inline Point &operator+=(const Point &q) NOEXCEPT { decaf_448_point_add(p,p,q.p); return *this; }
  326. /** @brief Point subtract. */
  327. inline Point operator- (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_448_point_sub(r.p,p,q.p); return r; }
  328. /** @brief Point subtract. */
  329. inline Point &operator-=(const Point &q) NOEXCEPT { decaf_448_point_sub(p,p,q.p); return *this; }
  330. /** @brief Point negate. */
  331. inline Point operator- () const NOEXCEPT { Point r((NOINIT())); decaf_448_point_negate(r.p,p); return r; }
  332. /** @brief Double the point out of place. */
  333. inline Point times_two () const NOEXCEPT { Point r((NOINIT())); decaf_448_point_double(r.p,p); return r; }
  334. /** @brief Double the point in place. */
  335. inline Point &double_in_place() NOEXCEPT { decaf_448_point_double(p,p); return *this; }
  336. /** @brief Constant-time compare. */
  337. inline bool operator!=(const Point &q) const NOEXCEPT { return ! decaf_448_point_eq(p,q.p); }
  338. /** @brief Constant-time compare. */
  339. inline bool operator==(const Point &q) const NOEXCEPT { return !!decaf_448_point_eq(p,q.p); }
  340. /** @brief Scalar multiply. */
  341. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r((NOINIT())); decaf_448_point_scalarmul(r.p,p,s.s); return r; }
  342. /** @brief Scalar multiply in place. */
  343. inline Point &operator*=(const Scalar &s) NOEXCEPT { decaf_448_point_scalarmul(p,p,s.s); return *this; }
  344. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  345. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  346. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  347. inline Point &operator/=(const Scalar &s) NOEXCEPT { return (*this) *= s.inverse(); }
  348. /** @brief Validate / sanity check */
  349. inline bool validate() const NOEXCEPT { return !!decaf_448_point_valid(p); }
  350. /** @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster. */
  351. static inline Point double_scalarmul (
  352. const Point &q, const Scalar &qs, const Point &r, const Scalar &rs
  353. ) NOEXCEPT {
  354. Point p((NOINIT())); decaf_448_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  355. }
  356. /**
  357. * @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster.
  358. * For those who like their scalars before the point.
  359. */
  360. static inline Point double_scalarmul (
  361. const Scalar &qs, const Point &q, const Scalar &rs, const Point &r
  362. ) NOEXCEPT {
  363. Point p((NOINIT())); decaf_448_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  364. }
  365. /**
  366. * @brief Double-scalar multiply: this point by the first scalar and base by the second scalar.
  367. * @warning This function takes variable time, and may leak the scalars (or points, but currently
  368. * it doesn't).
  369. */
  370. inline Point non_secret_combo_with_base(const Scalar &s, const Scalar &s_base) {
  371. Point r((NOINIT())); decaf_448_base_double_scalarmul_non_secret(r.p,s_base.s,p,s.s); return r;
  372. }
  373. /** @brief Return the base point */
  374. static inline const Point base() NOEXCEPT { return Point(decaf_448_point_base); }
  375. /** @brief Return the identity point */
  376. static inline const Point identity() NOEXCEPT { return Point(decaf_448_point_identity); }
  377. };
  378. /**
  379. * @brief Precomputed table of points.
  380. * Minor difficulties arise here because the decaf API doesn't expose, as a constant, how big such an object is.
  381. * Therefore we have to call malloc() or friends, but that's probably for the best, because you don't want to
  382. * stack-allocate a 15kiB object anyway.
  383. */
  384. class Precomputed {
  385. private:
  386. /** @cond internal */
  387. union {
  388. decaf_448_precomputed_s *mine;
  389. const decaf_448_precomputed_s *yours;
  390. } ours;
  391. bool isMine;
  392. inline void clear() NOEXCEPT {
  393. if (isMine) {
  394. decaf_448_precomputed_destroy(ours.mine);
  395. free(ours.mine);
  396. ours.yours = decaf_448_precomputed_base;
  397. isMine = false;
  398. }
  399. }
  400. inline void alloc() throw(std::bad_alloc) {
  401. if (isMine) return;
  402. int ret = posix_memalign((void**)&ours.mine, alignof_decaf_448_precomputed_s,sizeof_decaf_448_precomputed_s);
  403. if (ret || !ours.mine) {
  404. isMine = false;
  405. throw std::bad_alloc();
  406. }
  407. isMine = true;
  408. }
  409. inline const decaf_448_precomputed_s *get() const NOEXCEPT { return isMine ? ours.mine : ours.yours; }
  410. /** @endcond */
  411. public:
  412. /** Destructor securely erases the memory. */
  413. inline ~Precomputed() NOEXCEPT { clear(); }
  414. /**
  415. * @brief Initialize from underlying type, declared as a reference to prevent
  416. * it from being called with 0, thereby breaking override.
  417. *
  418. * The underlying object must remain valid throughout the lifetime of this one.
  419. *
  420. * By default, initializes to the table for the base point.
  421. *
  422. * @warning The empty initializer makes this equal to base, unlike the empty
  423. * initializer for points which makes this equal to the identity.
  424. */
  425. inline Precomputed(
  426. const decaf_448_precomputed_s &yours = *decaf_448_precomputed_base
  427. ) NOEXCEPT {
  428. ours.yours = &yours;
  429. isMine = false;
  430. }
  431. /**
  432. * @brief Assign. This may require an allocation and memcpy.
  433. */
  434. inline Precomputed &operator=(const Precomputed &it) throw(std::bad_alloc) {
  435. if (this == &it) return *this;
  436. if (it.isMine) {
  437. alloc();
  438. memcpy(ours.mine,it.ours.mine,sizeof_decaf_448_precomputed_s);
  439. } else {
  440. clear();
  441. ours.yours = it.ours.yours;
  442. }
  443. isMine = it.isMine;
  444. return *this;
  445. }
  446. /**
  447. * @brief Initilaize from point. Must allocate memory, and may throw.
  448. */
  449. inline Precomputed &operator=(const Point &it) throw(std::bad_alloc) {
  450. alloc();
  451. decaf_448_precompute(ours.mine,it.p);
  452. return *this;
  453. }
  454. /**
  455. * @brief Copy constructor.
  456. */
  457. inline Precomputed(const Precomputed &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  458. /**
  459. * @brief Constructor which initializes from point.
  460. */
  461. inline explicit Precomputed(const Point &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  462. #if __cplusplus >= 201103L
  463. inline Precomputed &operator=(Precomputed &&it) NOEXCEPT {
  464. if (this == &it) return *this;
  465. clear();
  466. ours = it.ours;
  467. isMine = it.isMine;
  468. it.isMine = false;
  469. it.ours.yours = decaf_448_precomputed_base;
  470. return *this;
  471. }
  472. inline Precomputed(Precomputed &&it) NOEXCEPT : isMine(false) { *this = it; }
  473. #endif
  474. /** @brief Fixed base scalarmul. */
  475. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r; decaf_448_precomputed_scalarmul(r.p,get(),s.s); return r; }
  476. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  477. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  478. /** @brief Return the table for the base point. */
  479. static inline const Precomputed base() NOEXCEPT { return Precomputed(*decaf_448_precomputed_base); }
  480. };
  481. }; /* struct decaf<448> */
  482. #undef NOEXCEPT
  483. #undef EXPLICIT_CON
  484. #undef GET_DATA
  485. } /* namespace decaf */
  486. #endif /* __DECAF_448_HXX__ */