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.
 
 
 
 
 

594 lines
22 KiB

  1. /**
  2. * @file decaf_255.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 Ed255-Goldilocks) and wiping out the cofactor.
  14. *
  15. * The formulas are all complete and have no special cases, except that
  16. * decaf_255_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_255_base_double_scalarmul_non_secret.
  21. */
  22. #ifndef __DECAF_255_HXX__
  23. #define __DECAF_255_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 "secure_buffer.hxx"
  30. #include <string>
  31. #include <sys/types.h>
  32. #include <limits.h>
  33. /* TODO: This is incomplete */
  34. /* TODO: attribute nonnull */
  35. /** @cond internal */
  36. #if __cplusplus >= 201103L
  37. #define NOEXCEPT noexcept
  38. #else
  39. #define NOEXCEPT throw()
  40. #endif
  41. /** @endcond */
  42. namespace decaf {
  43. /**
  44. * @brief Ed255-Goldilocks/Decaf instantiation of group.
  45. */
  46. struct Ed255 {
  47. /** @cond internal */
  48. class Point;
  49. class Precomputed;
  50. /** @endcond */
  51. /**
  52. * @brief A scalar modulo the curve order.
  53. * Supports the usual arithmetic operations, all in constant time.
  54. */
  55. class Scalar {
  56. public:
  57. /** @brief Size of a serialized element */
  58. static const size_t SER_BYTES = DECAF_255_SCALAR_BYTES;
  59. /** @brief access to the underlying scalar object */
  60. decaf_255_scalar_t s;
  61. /** @brief Don't initialize. */
  62. inline Scalar(const NOINIT &) NOEXCEPT {}
  63. /** @brief Set to an unsigned word */
  64. inline Scalar(const decaf_word_t w) NOEXCEPT { *this = w; }
  65. /** @brief Set to a signed word */
  66. inline Scalar(const int w) NOEXCEPT { *this = w; }
  67. /** @brief Construct from RNG */
  68. inline explicit Scalar(Rng &rng) NOEXCEPT {
  69. StackBuffer<SER_BYTES> sb(rng);
  70. *this = sb;
  71. }
  72. /** @brief Construct from decaf_scalar_t object. */
  73. inline Scalar(const decaf_255_scalar_t &t = decaf_255_scalar_zero) NOEXCEPT { decaf_255_scalar_copy(s,t); }
  74. /** @brief Copy constructor. */
  75. inline Scalar(const Scalar &x) NOEXCEPT { *this = x; }
  76. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  77. inline Scalar(const Block &buffer) NOEXCEPT { *this = buffer; }
  78. /** @brief Assignment. */
  79. inline Scalar& operator=(const Scalar &x) NOEXCEPT { decaf_255_scalar_copy(s,x.s); return *this; }
  80. /** @brief Assign from unsigned word. */
  81. inline Scalar& operator=(decaf_word_t w) NOEXCEPT { decaf_255_scalar_set(s,w); return *this; }
  82. /** @brief Assign from signed int. */
  83. inline Scalar& operator=(int w) NOEXCEPT {
  84. Scalar t(-(decaf_word_t)INT_MIN);
  85. decaf_255_scalar_set(s,(decaf_word_t)w - (decaf_word_t)INT_MIN);
  86. *this -= t;
  87. return *this;
  88. }
  89. /** Destructor securely zeorizes the scalar. */
  90. inline ~Scalar() NOEXCEPT { decaf_255_scalar_destroy(s); }
  91. /** @brief Assign from arbitrary-length little-endian byte sequence in a Block. */
  92. inline Scalar &operator=(const Block &bl) NOEXCEPT {
  93. decaf_255_scalar_decode_long(s,bl.data(),bl.size()); return *this;
  94. }
  95. /**
  96. * @brief Decode from correct-length little-endian byte sequence.
  97. * @return DECAF_FAILURE if the scalar is greater than or equal to the group order q.
  98. */
  99. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  100. Scalar &sc, const unsigned char buffer[SER_BYTES]
  101. ) NOEXCEPT {
  102. return decaf_255_scalar_decode(sc.s,buffer);
  103. }
  104. /** @brief Decode from correct-length little-endian byte sequence in C++ string. */
  105. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  106. Scalar &sc, const Block &buffer
  107. ) NOEXCEPT {
  108. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  109. return decaf_255_scalar_decode(sc.s,buffer);
  110. }
  111. /** @brief Encode to fixed-length string */
  112. inline operator SecureBuffer() const NOEXCEPT {
  113. SecureBuffer buf(SER_BYTES); decaf_255_scalar_encode(buf,s); return buf;
  114. }
  115. /** @brief Encode to fixed-length buffer */
  116. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  117. decaf_255_scalar_encode(buffer, s);
  118. }
  119. /** Add. */
  120. inline Scalar operator+ (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_add(r.s,s,q.s); return r; }
  121. /** Add to this. */
  122. inline Scalar &operator+=(const Scalar &q) NOEXCEPT { decaf_255_scalar_add(s,s,q.s); return *this; }
  123. /** Subtract. */
  124. inline Scalar operator- (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_sub(r.s,s,q.s); return r; }
  125. /** Subtract from this. */
  126. inline Scalar &operator-=(const Scalar &q) NOEXCEPT { decaf_255_scalar_sub(s,s,q.s); return *this; }
  127. /** Multiply */
  128. inline Scalar operator* (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_mul(r.s,s,q.s); return r; }
  129. /** Multiply into this. */
  130. inline Scalar &operator*=(const Scalar &q) NOEXCEPT { decaf_255_scalar_mul(s,s,q.s); return *this; }
  131. /** Negate */
  132. inline Scalar operator- () const NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_sub(r.s,decaf_255_scalar_zero,s); return r; }
  133. /** @brief Invert with Fermat's Little Theorem (slow!). If *this == 0, return 0. */
  134. inline Scalar inverse() const NOEXCEPT { Scalar r; decaf_255_scalar_invert(r.s,s); return r; }
  135. /** @brief Divide by inverting q. If q == 0, return 0. */
  136. inline Scalar operator/ (const Scalar &q) const NOEXCEPT { return *this * q.inverse(); }
  137. /** @brief Divide by inverting q. If q == 0, return 0. */
  138. inline Scalar &operator/=(const Scalar &q) NOEXCEPT { return *this *= q.inverse(); }
  139. /** @brief Compare in constant time */
  140. inline bool operator!=(const Scalar &q) const NOEXCEPT { return !(*this == q); }
  141. /** @brief Compare in constant time */
  142. inline bool operator==(const Scalar &q) const NOEXCEPT { return !!decaf_255_scalar_eq(s,q.s); }
  143. /** @brief Scalarmul with scalar on left. */
  144. inline Point operator* (const Point &q) const NOEXCEPT { return q * (*this); }
  145. /** @brief Scalarmul-precomputed with scalar on left. */
  146. inline Point operator* (const Precomputed &q) const NOEXCEPT { return q * (*this); }
  147. /** @brief Direct scalar multiplication. */
  148. inline SecureBuffer direct_scalarmul(
  149. const Block &in,
  150. decaf_bool_t allow_identity=DECAF_FALSE,
  151. decaf_bool_t short_circuit=DECAF_TRUE
  152. ) const throw(CryptoException) {
  153. SecureBuffer out(/*FIXME Point::*/SER_BYTES);
  154. if (!decaf_255_direct_scalarmul(out, in.data(), s, allow_identity, short_circuit))
  155. throw CryptoException();
  156. return out;
  157. }
  158. };
  159. /**
  160. * @brief Element of prime-order group.
  161. */
  162. class Point {
  163. public:
  164. /** @brief Size of a serialized element */
  165. static const size_t SER_BYTES = DECAF_255_SER_BYTES;
  166. /** @brief Size of a stegged element */
  167. static const size_t STEG_BYTES = DECAF_255_SER_BYTES + 8;
  168. /** @brief Bytes required for hash */
  169. static const size_t HASH_BYTES = DECAF_255_SER_BYTES;
  170. /** The c-level object. */
  171. decaf_255_point_t p;
  172. /** @brief Don't initialize. */
  173. inline Point(const NOINIT &) NOEXCEPT {}
  174. /** @brief Constructor sets to identity by default. */
  175. inline Point(const decaf_255_point_t &q = decaf_255_point_identity) NOEXCEPT { decaf_255_point_copy(p,q); }
  176. /** @brief Copy constructor. */
  177. inline Point(const Point &q) NOEXCEPT { *this = q; }
  178. /** @brief Assignment. */
  179. inline Point& operator=(const Point &q) NOEXCEPT { decaf_255_point_copy(p,q.p); return *this; }
  180. /** @brief Destructor securely zeorizes the point. */
  181. inline ~Point() NOEXCEPT { decaf_255_point_destroy(p); }
  182. /** @brief Construct from RNG */
  183. inline explicit Point(Rng &rng, bool uniform = true) NOEXCEPT {
  184. if (uniform) {
  185. StackBuffer<2*HASH_BYTES> b(rng);
  186. set_to_hash(b);
  187. } else {
  188. StackBuffer<HASH_BYTES> b(rng);
  189. set_to_hash(b);
  190. }
  191. }
  192. /**
  193. * @brief Initialize from C++ fixed-length byte string.
  194. * The all-zero string maps to the identity.
  195. *
  196. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  197. * or was the identity and allow_identity was DECAF_FALSE.
  198. */
  199. inline explicit Point(const Block &s, decaf_bool_t allow_identity=DECAF_TRUE) throw(CryptoException) {
  200. if (!decode(*this,s,allow_identity)) throw CryptoException();
  201. }
  202. /**
  203. * @brief Initialize from C fixed-length byte string.
  204. * The all-zero string maps to the identity.
  205. *
  206. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  207. * or was the identity and allow_identity was DECAF_FALSE.
  208. */
  209. inline explicit Point(const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE)
  210. throw(CryptoException) { if (!decode(*this,buffer,allow_identity)) throw CryptoException(); }
  211. /**
  212. * @brief Initialize from C fixed-length byte string.
  213. * The all-zero string maps to the identity.
  214. *
  215. * @retval DECAF_SUCCESS the string was successfully decoded.
  216. * @return DECAF_FAILURE the string wasn't the encoding of a point, or was the identity
  217. * and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  218. */
  219. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  220. Point &p, const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE
  221. ) NOEXCEPT {
  222. return decaf_255_point_decode(p.p,buffer,allow_identity);
  223. }
  224. /**
  225. * @brief Initialize from C++ fixed-length byte string.
  226. * The all-zero string maps to the identity.
  227. *
  228. * @retval DECAF_SUCCESS the string was successfully decoded.
  229. * @return DECAF_FAILURE the string was the wrong length, or wasn't the encoding of a point,
  230. * or was the identity and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  231. */
  232. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  233. Point &p, const Block &buffer, decaf_bool_t allow_identity=DECAF_TRUE
  234. ) NOEXCEPT {
  235. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  236. return decaf_255_point_decode(p.p,buffer.data(),allow_identity);
  237. }
  238. /**
  239. * @brief Map uniformly to the curve from a hash buffer.
  240. * The empty or all-zero string maps to the identity, as does the string "\x01".
  241. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  242. * but the buffer will be zero-padded on the right.
  243. */
  244. static inline Point from_hash ( const Block &s ) NOEXCEPT {
  245. Point p((NOINIT())); p.set_to_hash(s); return p;
  246. }
  247. /**
  248. * @brief Map to the curve from a hash buffer.
  249. * The empty or all-zero string maps to the identity, as does the string "\x01".
  250. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  251. * but the buffer will be zero-padded on the right.
  252. */
  253. inline void set_to_hash( const Block &s ) NOEXCEPT {
  254. if (s.size() < HASH_BYTES) {
  255. SecureBuffer b(HASH_BYTES);
  256. memcpy(b.data(), s.data(), s.size());
  257. decaf_255_point_from_hash_nonuniform(p,b);
  258. } else if (s.size() == HASH_BYTES) {
  259. decaf_255_point_from_hash_nonuniform(p,s);
  260. } else if (s.size() < 2*HASH_BYTES) {
  261. SecureBuffer b(2*HASH_BYTES);
  262. memcpy(b.data(), s.data(), s.size());
  263. decaf_255_point_from_hash_uniform(p,b);
  264. } else {
  265. decaf_255_point_from_hash_uniform(p,s);
  266. }
  267. }
  268. /**
  269. * @brief Encode to string. The identity encodes to the all-zero string.
  270. */
  271. inline operator SecureBuffer() const NOEXCEPT {
  272. SecureBuffer buffer(SER_BYTES);
  273. decaf_255_point_encode(buffer, p);
  274. return buffer;
  275. }
  276. /**
  277. * @brief Encode to a C buffer. The identity encodes to all zeros.
  278. */
  279. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  280. decaf_255_point_encode(buffer, p);
  281. }
  282. /** @brief Point add. */
  283. inline Point operator+ (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_255_point_add(r.p,p,q.p); return r; }
  284. /** @brief Point add. */
  285. inline Point &operator+=(const Point &q) NOEXCEPT { decaf_255_point_add(p,p,q.p); return *this; }
  286. /** @brief Point subtract. */
  287. inline Point operator- (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_255_point_sub(r.p,p,q.p); return r; }
  288. /** @brief Point subtract. */
  289. inline Point &operator-=(const Point &q) NOEXCEPT { decaf_255_point_sub(p,p,q.p); return *this; }
  290. /** @brief Point negate. */
  291. inline Point operator- () const NOEXCEPT { Point r((NOINIT())); decaf_255_point_negate(r.p,p); return r; }
  292. /** @brief Double the point out of place. */
  293. inline Point times_two () const NOEXCEPT { Point r((NOINIT())); decaf_255_point_double(r.p,p); return r; }
  294. /** @brief Double the point in place. */
  295. inline Point &double_in_place() NOEXCEPT { decaf_255_point_double(p,p); return *this; }
  296. /** @brief Constant-time compare. */
  297. inline bool operator!=(const Point &q) const NOEXCEPT { return ! decaf_255_point_eq(p,q.p); }
  298. /** @brief Constant-time compare. */
  299. inline bool operator==(const Point &q) const NOEXCEPT { return !!decaf_255_point_eq(p,q.p); }
  300. /** @brief Scalar multiply. */
  301. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r((NOINIT())); decaf_255_point_scalarmul(r.p,p,s.s); return r; }
  302. /** @brief Scalar multiply in place. */
  303. inline Point &operator*=(const Scalar &s) NOEXCEPT { decaf_255_point_scalarmul(p,p,s.s); return *this; }
  304. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  305. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  306. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  307. inline Point &operator/=(const Scalar &s) NOEXCEPT { return (*this) *= s.inverse(); }
  308. /** @brief Validate / sanity check */
  309. inline bool validate() const NOEXCEPT { return !!decaf_255_point_valid(p); }
  310. /** @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster. */
  311. static inline Point double_scalarmul (
  312. const Point &q, const Scalar &qs, const Point &r, const Scalar &rs
  313. ) NOEXCEPT {
  314. Point p((NOINIT())); decaf_255_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  315. }
  316. /**
  317. * @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster.
  318. * For those who like their scalars before the point.
  319. */
  320. static inline Point double_scalarmul (
  321. const Scalar &qs, const Point &q, const Scalar &rs, const Point &r
  322. ) NOEXCEPT {
  323. Point p((NOINIT())); decaf_255_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  324. }
  325. /**
  326. * @brief Double-scalar multiply: this point by the first scalar and base by the second scalar.
  327. * @warning This function takes variable time, and may leak the scalars (or points, but currently
  328. * it doesn't).
  329. */
  330. inline Point non_secret_combo_with_base(const Scalar &s, const Scalar &s_base) NOEXCEPT {
  331. Point r((NOINIT())); decaf_255_base_double_scalarmul_non_secret(r.p,s_base.s,p,s.s); return r;
  332. }
  333. /** @brief Return a point equal to *this, whose internal data is rotated by a torsion element. */
  334. inline Point debugging_torque() const NOEXCEPT {
  335. Point q;
  336. decaf_255_point_debugging_torque(q.p,p);
  337. return q;
  338. }
  339. /** @brief Return a point equal to *this, whose internal data has a modified representation. */
  340. inline Point debugging_pscale(const uint8_t factor[/*SER_BYTES*/]) const NOEXCEPT {
  341. Point q;
  342. decaf_255_point_debugging_pscale(q.p,p,factor);
  343. return q;
  344. }
  345. /** @brief Return a point equal to *this, whose internal data has a randomized representation. */
  346. inline Point debugging_pscale(Rng &r) const NOEXCEPT {
  347. StackBuffer<SER_BYTES> sb(r);
  348. return debugging_pscale(sb);
  349. }
  350. /**
  351. * Modify buffer so that Point::from_hash(Buffer) == *this, and return true;
  352. * or leave buf unmodified and return false.
  353. */
  354. inline bool invert_elligator (
  355. Buffer &buf, uint16_t hint
  356. ) const NOEXCEPT {
  357. unsigned char buf2[2*HASH_BYTES];
  358. memset(buf2,0,sizeof(buf2));
  359. memcpy(buf2,buf,(buf.size() > 2*HASH_BYTES) ? 2*HASH_BYTES : buf.size());
  360. decaf_bool_t ret;
  361. if (buf.size() > HASH_BYTES) {
  362. ret = decaf_255_invert_elligator_uniform(buf2, p, hint);
  363. } else {
  364. ret = decaf_255_invert_elligator_nonuniform(buf2, p, hint);
  365. }
  366. if (buf.size() < HASH_BYTES) {
  367. ret &= decaf_memeq(&buf2[buf.size()], &buf2[HASH_BYTES], HASH_BYTES - buf.size());
  368. }
  369. if (ret) {
  370. /* TODO: make this constant time?? */
  371. memcpy(buf,buf2,(buf.size() < HASH_BYTES) ? buf.size() : HASH_BYTES);
  372. }
  373. decaf_bzero(buf2,sizeof(buf2));
  374. return !!ret;
  375. }
  376. /** @brief Steganographically encode this */
  377. inline SecureBuffer steg_encode(Rng &rng) const throw(std::bad_alloc) {
  378. SecureBuffer out(STEG_BYTES);
  379. bool done;
  380. do {
  381. rng.read(out.slice(HASH_BYTES-1,STEG_BYTES-HASH_BYTES+1));
  382. done = invert_elligator(out, out[HASH_BYTES-1]);
  383. } while (!done);
  384. return out;
  385. }
  386. /** @brief Return the base point */
  387. static inline const Point base() NOEXCEPT { return Point(decaf_255_point_base); }
  388. /** @brief Return the identity point */
  389. static inline const Point identity() NOEXCEPT { return Point(decaf_255_point_identity); }
  390. };
  391. /**
  392. * @brief Precomputed table of points.
  393. * Minor difficulties arise here because the decaf API doesn't expose, as a constant, how big such an object is.
  394. * Therefore we have to call malloc() or friends, but that's probably for the best, because you don't want to
  395. * stack-allocate a 15kiB object anyway.
  396. */
  397. class Precomputed {
  398. private:
  399. /** @cond internal */
  400. union {
  401. decaf_255_precomputed_s *mine;
  402. const decaf_255_precomputed_s *yours;
  403. } ours;
  404. bool isMine;
  405. inline void clear() NOEXCEPT {
  406. if (isMine) {
  407. decaf_255_precomputed_destroy(ours.mine);
  408. free(ours.mine);
  409. ours.yours = decaf_255_precomputed_base;
  410. isMine = false;
  411. }
  412. }
  413. inline void alloc() throw(std::bad_alloc) {
  414. if (isMine) return;
  415. int ret = posix_memalign((void**)&ours.mine, alignof_decaf_255_precomputed_s,sizeof_decaf_255_precomputed_s);
  416. if (ret || !ours.mine) {
  417. isMine = false;
  418. throw std::bad_alloc();
  419. }
  420. isMine = true;
  421. }
  422. inline const decaf_255_precomputed_s *get() const NOEXCEPT { return isMine ? ours.mine : ours.yours; }
  423. /** @endcond */
  424. public:
  425. /** Destructor securely zeorizes the memory. */
  426. inline ~Precomputed() NOEXCEPT { clear(); }
  427. /**
  428. * @brief Initialize from underlying type, declared as a reference to prevent
  429. * it from being called with 0, thereby breaking override.
  430. *
  431. * The underlying object must remain valid throughout the lifetime of this one.
  432. *
  433. * By default, initializes to the table for the base point.
  434. *
  435. * @warning The empty initializer makes this equal to base, unlike the empty
  436. * initializer for points which makes this equal to the identity.
  437. */
  438. inline Precomputed(
  439. const decaf_255_precomputed_s &yours = *decaf_255_precomputed_base
  440. ) NOEXCEPT {
  441. ours.yours = &yours;
  442. isMine = false;
  443. }
  444. /**
  445. * @brief Assign. This may require an allocation and memcpy.
  446. */
  447. inline Precomputed &operator=(const Precomputed &it) throw(std::bad_alloc) {
  448. if (this == &it) return *this;
  449. if (it.isMine) {
  450. alloc();
  451. memcpy(ours.mine,it.ours.mine,sizeof_decaf_255_precomputed_s);
  452. } else {
  453. clear();
  454. ours.yours = it.ours.yours;
  455. }
  456. isMine = it.isMine;
  457. return *this;
  458. }
  459. /**
  460. * @brief Initilaize from point. Must allocate memory, and may throw.
  461. */
  462. inline Precomputed &operator=(const Point &it) throw(std::bad_alloc) {
  463. alloc();
  464. decaf_255_precompute(ours.mine,it.p);
  465. return *this;
  466. }
  467. /**
  468. * @brief Copy constructor.
  469. */
  470. inline Precomputed(const Precomputed &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  471. /**
  472. * @brief Constructor which initializes from point.
  473. */
  474. inline explicit Precomputed(const Point &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  475. #if __cplusplus >= 201103L
  476. inline Precomputed &operator=(Precomputed &&it) NOEXCEPT {
  477. if (this == &it) return *this;
  478. clear();
  479. ours = it.ours;
  480. isMine = it.isMine;
  481. it.isMine = false;
  482. it.ours.yours = decaf_255_precomputed_base;
  483. return *this;
  484. }
  485. inline Precomputed(Precomputed &&it) NOEXCEPT : isMine(false) { *this = it; }
  486. #endif
  487. /** @brief Fixed base scalarmul. */
  488. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r; decaf_255_precomputed_scalarmul(r.p,get(),s.s); return r; }
  489. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  490. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  491. /** @brief Return the table for the base point. */
  492. static inline const Precomputed base() NOEXCEPT { return Precomputed(*decaf_255_precomputed_base); }
  493. };
  494. }; /* struct Decaf255 */
  495. #undef NOEXCEPT
  496. } /* namespace decaf */
  497. #endif /* __DECAF_255_HXX__ */