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.
 
 
 
 
 

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