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.
 
 
 
 
 

718 lines
25 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. typedef uint32_t GroupId;
  47. static const GroupId Ed448Goldilocks = 448;
  48. /** @brief An exception for when crypto (ie point decode) has failed. */
  49. class CryptoException : public std::exception {
  50. public:
  51. /** @return "CryptoException" */
  52. virtual const char * what() const NOEXCEPT { return "CryptoException"; }
  53. };
  54. /** @brief An exception for when crypto (ie point decode) has failed. */
  55. class LengthException : public std::exception {
  56. public:
  57. /** @return "CryptoException" */
  58. virtual const char * what() const NOEXCEPT { return "LengthException"; }
  59. };
  60. /**
  61. * Securely erase contents of memory.
  62. */
  63. static inline void really_bzero(void *data, size_t size) { decaf_bzero(data,size); }
  64. /** Block object */
  65. class Block {
  66. protected:
  67. unsigned char *data_;
  68. size_t size_;
  69. public:
  70. /** Empty init */
  71. inline Block() NOEXCEPT : data_(NULL), size_(0) {}
  72. /** Init from C string */
  73. inline Block(const char *data) NOEXCEPT : data_((unsigned char *)data), size_(strlen(data)) {}
  74. /** Unowned init */
  75. inline Block(const unsigned char *data, size_t size) NOEXCEPT : data_((unsigned char *)data), size_(size) {}
  76. /** Block from std::string */
  77. inline Block(const std::string &s) : data_((unsigned char *)GET_DATA(s)), size_(s.size()) {}
  78. /** Get const data */
  79. inline const unsigned char *data() const NOEXCEPT { return data_; }
  80. /** Get the size */
  81. inline size_t size() const NOEXCEPT { return size_; }
  82. /** Autocast to const unsigned char * */
  83. inline operator const unsigned char*() const NOEXCEPT { return data_; }
  84. /** Convert to C++ string */
  85. inline std::string get_string() const {
  86. return std::string((const char *)data_,size_);
  87. }
  88. /** Slice the buffer*/
  89. inline Block slice(size_t off, size_t length) const throw(LengthException) {
  90. if (off > size() || length > size() - off)
  91. throw LengthException();
  92. return Block(data()+off, length);
  93. }
  94. /** Virtual destructor for SecureBlock. TODO: probably means vtable? Make bool? */
  95. inline virtual ~Block() {};
  96. };
  97. class TmpBuffer;
  98. class Buffer : public Block {
  99. public:
  100. /** Null init */
  101. inline Buffer() NOEXCEPT : Block() {}
  102. /** Unowned init */
  103. inline Buffer(unsigned char *data, size_t size) NOEXCEPT : Block(data,size) {}
  104. /** Get unconst data */
  105. inline unsigned char *data() NOEXCEPT { return data_; }
  106. /** Get const data */
  107. inline const unsigned char *data() const NOEXCEPT { return data_; }
  108. /** Autocast to const unsigned char * */
  109. inline operator const unsigned char*() const NOEXCEPT { return data_; }
  110. /** Autocast to unsigned char */
  111. inline operator unsigned char*() NOEXCEPT { return data_; }
  112. /** Slice the buffer*/
  113. inline TmpBuffer slice(size_t off, size_t length) throw(LengthException);
  114. };
  115. class TmpBuffer : public Buffer {
  116. public:
  117. /** Unowned init */
  118. inline TmpBuffer(unsigned char *data, size_t size) NOEXCEPT : Buffer(data,size) {}
  119. };
  120. TmpBuffer Buffer::slice(size_t off, size_t length) throw(LengthException) {
  121. if (off > size() || length > size() - off) throw LengthException();
  122. return TmpBuffer(data()+off, length);
  123. }
  124. /** A self-erasing block of data */
  125. class SecureBuffer : public Buffer {
  126. public:
  127. /** Null secure block */
  128. inline SecureBuffer() NOEXCEPT : Buffer() {}
  129. /** Construct empty from size */
  130. inline SecureBuffer(size_t size) {
  131. data_ = new unsigned char[size_ = size];
  132. memset(data_,0,size);
  133. }
  134. /** Construct from data */
  135. inline SecureBuffer(const unsigned char *data, size_t size){
  136. data_ = new unsigned char[size_ = size];
  137. memcpy(data_, data, size);
  138. }
  139. /** Copy constructor */
  140. inline SecureBuffer(const Block &copy) : Buffer() { *this = copy; }
  141. /** Copy-assign constructor */
  142. inline SecureBuffer& operator=(const Block &copy) throw(std::bad_alloc) {
  143. if (&copy == this) return *this;
  144. clear();
  145. data_ = new unsigned char[size_ = copy.size()];
  146. memcpy(data_,copy.data(),size_);
  147. return *this;
  148. }
  149. /** Copy-assign constructor */
  150. inline SecureBuffer& operator=(const SecureBuffer &copy) throw(std::bad_alloc) {
  151. if (&copy == this) return *this;
  152. clear();
  153. data_ = new unsigned char[size_ = copy.size()];
  154. memcpy(data_,copy.data(),size_);
  155. return *this;
  156. }
  157. /** Destructor erases data */
  158. ~SecureBuffer() NOEXCEPT { clear(); }
  159. /** Clear data */
  160. inline void clear() NOEXCEPT {
  161. if (data_ == NULL) return;
  162. really_bzero(data_,size_);
  163. delete[] data_;
  164. data_ = NULL;
  165. size_ = 0;
  166. }
  167. #if __cplusplus >= 201103L
  168. /** Move constructor */
  169. inline SecureBuffer(SecureBuffer &&move) { *this = move; }
  170. /** Move non-constructor */
  171. inline SecureBuffer(Block &&move) { *this = (Block &)move; }
  172. /** Move-assign constructor. TODO: check that this actually gets used.*/
  173. inline SecureBuffer& operator=(SecureBuffer &&move) {
  174. clear();
  175. data_ = move.data_; move.data_ = NULL;
  176. size_ = move.size_; move.size_ = 0;
  177. return *this;
  178. }
  179. /** C++11-only explicit cast */
  180. inline explicit operator std::string() const { return get_string(); }
  181. #endif
  182. };
  183. /** @brief Passed to constructors to avoid (conservative) initialization */
  184. struct NOINIT {};
  185. /**@cond internal*/
  186. /** Forward-declare sponge RNG object */
  187. class SpongeRng;
  188. /**@endcond*/
  189. /**
  190. * @brief Group with prime order.
  191. * @todo Move declarations of functions up here?
  192. */
  193. template<GroupId group = Ed448Goldilocks> struct decaf;
  194. /**
  195. * @brief Ed448-Goldilocks/Decaf instantiation of group.
  196. */
  197. template<> struct decaf<Ed448Goldilocks> {
  198. /** @cond internal */
  199. class Point;
  200. class Precomputed;
  201. /** @endcond */
  202. /**
  203. * @brief A scalar modulo the curve order.
  204. * Supports the usual arithmetic operations, all in constant time.
  205. */
  206. class Scalar {
  207. public:
  208. /** @brief Size of a serialized element */
  209. static const size_t SER_BYTES = DECAF_448_SCALAR_BYTES;
  210. /** @brief access to the underlying scalar object */
  211. decaf_448_scalar_t s;
  212. /** @brief Don't initialize. */
  213. inline Scalar(const NOINIT &) {}
  214. /** @brief Set to an unsigned word */
  215. inline Scalar(const decaf_word_t w) NOEXCEPT { *this = w; }
  216. /** @brief Set to a signed word */
  217. inline Scalar(const int w) NOEXCEPT { *this = w; }
  218. /** @brief Construct from RNG */
  219. inline explicit Scalar(SpongeRng &rng);
  220. /** @brief Construct from decaf_scalar_t object. */
  221. inline Scalar(const decaf_448_scalar_t &t = decaf_448_scalar_zero) NOEXCEPT { decaf_448_scalar_copy(s,t); }
  222. /** @brief Copy constructor. */
  223. inline Scalar(const Scalar &x) NOEXCEPT { *this = x; }
  224. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  225. inline Scalar(const Block &buffer) NOEXCEPT { *this = buffer; }
  226. /** @brief Assignment. */
  227. inline Scalar& operator=(const Scalar &x) NOEXCEPT { decaf_448_scalar_copy(s,x.s); return *this; }
  228. /** @brief Assign from unsigned word. */
  229. inline Scalar& operator=(decaf_word_t w) NOEXCEPT { decaf_448_scalar_set(s,w); return *this; }
  230. /** @brief Assign from signed int. */
  231. inline Scalar& operator=(int w) {
  232. Scalar t(-(decaf_word_t)INT_MIN);
  233. decaf_448_scalar_set(s,(decaf_word_t)w - (decaf_word_t)INT_MIN);
  234. *this -= t;
  235. return *this;
  236. }
  237. /** Destructor securely erases the scalar. */
  238. inline ~Scalar() NOEXCEPT { decaf_448_scalar_destroy(s); }
  239. /** @brief Assign from arbitrary-length little-endian byte sequence in a Block. */
  240. inline Scalar &operator=(const Block &bl) NOEXCEPT {
  241. decaf_448_scalar_decode_long(s,bl.data(),bl.size()); return *this;
  242. }
  243. /**
  244. * @brief Decode from correct-length little-endian byte sequence.
  245. * @return DECAF_FAILURE if the scalar is greater than or equal to the group order q.
  246. */
  247. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  248. Scalar &sc, const unsigned char buffer[SER_BYTES]
  249. ) NOEXCEPT {
  250. return decaf_448_scalar_decode(sc.s,buffer);
  251. }
  252. /** @brief Decode from correct-length little-endian byte sequence in C++ string. */
  253. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  254. Scalar &sc, const Block &buffer
  255. ) NOEXCEPT {
  256. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  257. return decaf_448_scalar_decode(sc.s,buffer);
  258. }
  259. /** @brief Encode to fixed-length string */
  260. inline EXPLICIT_CON operator SecureBuffer() const NOEXCEPT {
  261. SecureBuffer buf(SER_BYTES); decaf_448_scalar_encode(buf,s); return buf;
  262. }
  263. /** @brief Encode to fixed-length buffer */
  264. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  265. decaf_448_scalar_encode(buffer, s);
  266. }
  267. /** Add. */
  268. inline Scalar operator+ (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_add(r.s,s,q.s); return r; }
  269. /** Add to this. */
  270. inline Scalar &operator+=(const Scalar &q) NOEXCEPT { decaf_448_scalar_add(s,s,q.s); return *this; }
  271. /** Subtract. */
  272. inline Scalar operator- (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_sub(r.s,s,q.s); return r; }
  273. /** Subtract from this. */
  274. inline Scalar &operator-=(const Scalar &q) NOEXCEPT { decaf_448_scalar_sub(s,s,q.s); return *this; }
  275. /** Multiply */
  276. inline Scalar operator* (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_mul(r.s,s,q.s); return r; }
  277. /** Multiply into this. */
  278. inline Scalar &operator*=(const Scalar &q) NOEXCEPT { decaf_448_scalar_mul(s,s,q.s); return *this; }
  279. /** Negate */
  280. inline Scalar operator- () const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_sub(r.s,decaf_448_scalar_zero,s); return r; }
  281. /** @brief Invert with Fermat's Little Theorem (slow!). If *this == 0, return 0. */
  282. inline Scalar inverse() const NOEXCEPT { Scalar r; decaf_448_scalar_invert(r.s,s); return r; }
  283. /** @brief Divide by inverting q. If q == 0, return 0. */
  284. inline Scalar operator/ (const Scalar &q) const NOEXCEPT { return *this * q.inverse(); }
  285. /** @brief Divide by inverting q. If q == 0, return 0. */
  286. inline Scalar &operator/=(const Scalar &q) NOEXCEPT { return *this *= q.inverse(); }
  287. /** @brief Compare in constant time */
  288. inline bool operator!=(const Scalar &q) const NOEXCEPT { return !(*this == q); }
  289. /** @brief Compare in constant time */
  290. inline bool operator==(const Scalar &q) const NOEXCEPT { return !!decaf_448_scalar_eq(s,q.s); }
  291. /** @brief Scalarmul with scalar on left. */
  292. inline Point operator* (const Point &q) const NOEXCEPT { return q * (*this); }
  293. /** @brief Scalarmul-precomputed with scalar on left. */
  294. inline Point operator* (const Precomputed &q) const NOEXCEPT { return q * (*this); }
  295. /** @brief Direct scalar multiplication. */
  296. inline SecureBuffer direct_scalarmul(
  297. const Block &in,
  298. decaf_bool_t allow_identity=DECAF_FALSE,
  299. decaf_bool_t short_circuit=DECAF_TRUE
  300. ) const throw(CryptoException) {
  301. SecureBuffer out(/*FIXME Point::*/SER_BYTES);
  302. if (!decaf_448_direct_scalarmul(out, in.data(), s, allow_identity, short_circuit))
  303. throw CryptoException();
  304. return out;
  305. }
  306. };
  307. /**
  308. * @brief Element of prime-order group.
  309. */
  310. class Point {
  311. public:
  312. /** @brief Size of a serialized element */
  313. static const size_t SER_BYTES = DECAF_448_SER_BYTES;
  314. /** @brief Bytes required for hash */
  315. static const size_t HASH_BYTES = DECAF_448_SER_BYTES;
  316. /** The c-level object. */
  317. decaf_448_point_t p;
  318. /** @brief Don't initialize. */
  319. inline Point(const NOINIT &) {}
  320. /** @brief Constructor sets to identity by default. */
  321. inline Point(const decaf_448_point_t &q = decaf_448_point_identity) { decaf_448_point_copy(p,q); }
  322. /** @brief Copy constructor. */
  323. inline Point(const Point &q) { decaf_448_point_copy(p,q.p); }
  324. /** @brief Assignment. */
  325. inline Point& operator=(const Point &q) { decaf_448_point_copy(p,q.p); return *this; }
  326. /** @brief Destructor securely erases the point. */
  327. inline ~Point() { decaf_448_point_destroy(p); }
  328. /** @brief Construct from RNG */
  329. inline explicit Point(SpongeRng &rng, bool uniform = true);
  330. /**
  331. * @brief Initialize from C++ fixed-length byte string.
  332. * The all-zero string maps to the identity.
  333. *
  334. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  335. * or was the identity and allow_identity was DECAF_FALSE.
  336. */
  337. inline explicit Point(const Block &s, decaf_bool_t allow_identity=DECAF_TRUE) throw(CryptoException) {
  338. if (!decode(*this,s,allow_identity)) throw CryptoException();
  339. }
  340. /**
  341. * @brief Initialize from C fixed-length byte string.
  342. * The all-zero string maps to the identity.
  343. *
  344. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  345. * or was the identity and allow_identity was DECAF_FALSE.
  346. */
  347. inline explicit Point(const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE)
  348. throw(CryptoException) { if (!decode(*this,buffer,allow_identity)) throw CryptoException(); }
  349. /**
  350. * @brief Initialize from C fixed-length byte string.
  351. * The all-zero string maps to the identity.
  352. *
  353. * @retval DECAF_SUCCESS the string was successfully decoded.
  354. * @return DECAF_FAILURE the string wasn't the encoding of a point, or was the identity
  355. * and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  356. */
  357. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  358. Point &p, const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE
  359. ) NOEXCEPT {
  360. return decaf_448_point_decode(p.p,buffer,allow_identity);
  361. }
  362. /**
  363. * @brief Initialize from C++ fixed-length byte string.
  364. * The all-zero string maps to the identity.
  365. *
  366. * @retval DECAF_SUCCESS the string was successfully decoded.
  367. * @return DECAF_FAILURE the string was the wrong length, or wasn't the encoding of a point,
  368. * or was the identity and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  369. */
  370. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  371. Point &p, const Block &buffer, decaf_bool_t allow_identity=DECAF_TRUE
  372. ) NOEXCEPT {
  373. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  374. return decaf_448_point_decode(p.p,buffer.data(),allow_identity);
  375. }
  376. /**
  377. * @brief Map uniformly to the curve from a hash buffer.
  378. * The empty or all-zero string maps to the identity, as does the string "\x01".
  379. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  380. * but the buffer will be zero-padded on the right.
  381. */
  382. static inline Point from_hash ( const Block &s ) NOEXCEPT {
  383. Point p((NOINIT())); p.set_to_hash(s); return p;
  384. }
  385. /**
  386. * @brief Map to the curve from a hash buffer.
  387. * The empty or all-zero string maps to the identity, as does the string "\x01".
  388. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  389. * but the buffer will be zero-padded on the right.
  390. */
  391. inline void set_to_hash( const Block &s ) NOEXCEPT {
  392. if (s.size() < HASH_BYTES) {
  393. SecureBuffer b(HASH_BYTES);
  394. memcpy(b.data(), s.data(), s.size());
  395. decaf_448_point_from_hash_nonuniform(p,b);
  396. } else if (s.size() == HASH_BYTES) {
  397. decaf_448_point_from_hash_nonuniform(p,s);
  398. } else if (s.size() < 2*HASH_BYTES) {
  399. SecureBuffer b(2*HASH_BYTES);
  400. memcpy(b.data(), s.data(), s.size());
  401. decaf_448_point_from_hash_uniform(p,b);
  402. } else {
  403. decaf_448_point_from_hash_uniform(p,s);
  404. }
  405. }
  406. /**
  407. * @brief Encode to string. The identity encodes to the all-zero string.
  408. */
  409. inline EXPLICIT_CON operator SecureBuffer() const NOEXCEPT {
  410. SecureBuffer buffer(SER_BYTES);
  411. decaf_448_point_encode(buffer, p);
  412. return buffer;
  413. }
  414. /**
  415. * @brief Encode to a C buffer. The identity encodes to all zeros.
  416. */
  417. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  418. decaf_448_point_encode(buffer, p);
  419. }
  420. /** @brief Point add. */
  421. inline Point operator+ (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_448_point_add(r.p,p,q.p); return r; }
  422. /** @brief Point add. */
  423. inline Point &operator+=(const Point &q) NOEXCEPT { decaf_448_point_add(p,p,q.p); return *this; }
  424. /** @brief Point subtract. */
  425. inline Point operator- (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_448_point_sub(r.p,p,q.p); return r; }
  426. /** @brief Point subtract. */
  427. inline Point &operator-=(const Point &q) NOEXCEPT { decaf_448_point_sub(p,p,q.p); return *this; }
  428. /** @brief Point negate. */
  429. inline Point operator- () const NOEXCEPT { Point r((NOINIT())); decaf_448_point_negate(r.p,p); return r; }
  430. /** @brief Double the point out of place. */
  431. inline Point times_two () const NOEXCEPT { Point r((NOINIT())); decaf_448_point_double(r.p,p); return r; }
  432. /** @brief Double the point in place. */
  433. inline Point &double_in_place() NOEXCEPT { decaf_448_point_double(p,p); return *this; }
  434. /** @brief Constant-time compare. */
  435. inline bool operator!=(const Point &q) const NOEXCEPT { return ! decaf_448_point_eq(p,q.p); }
  436. /** @brief Constant-time compare. */
  437. inline bool operator==(const Point &q) const NOEXCEPT { return !!decaf_448_point_eq(p,q.p); }
  438. /** @brief Scalar multiply. */
  439. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r((NOINIT())); decaf_448_point_scalarmul(r.p,p,s.s); return r; }
  440. /** @brief Scalar multiply in place. */
  441. inline Point &operator*=(const Scalar &s) NOEXCEPT { decaf_448_point_scalarmul(p,p,s.s); return *this; }
  442. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  443. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  444. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  445. inline Point &operator/=(const Scalar &s) NOEXCEPT { return (*this) *= s.inverse(); }
  446. /** @brief Validate / sanity check */
  447. inline bool validate() const NOEXCEPT { return !!decaf_448_point_valid(p); }
  448. /** @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster. */
  449. static inline Point double_scalarmul (
  450. const Point &q, const Scalar &qs, const Point &r, const Scalar &rs
  451. ) NOEXCEPT {
  452. Point p((NOINIT())); decaf_448_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  453. }
  454. /**
  455. * @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster.
  456. * For those who like their scalars before the point.
  457. */
  458. static inline Point double_scalarmul (
  459. const Scalar &qs, const Point &q, const Scalar &rs, const Point &r
  460. ) NOEXCEPT {
  461. Point p((NOINIT())); decaf_448_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  462. }
  463. /**
  464. * @brief Double-scalar multiply: this point by the first scalar and base by the second scalar.
  465. * @warning This function takes variable time, and may leak the scalars (or points, but currently
  466. * it doesn't).
  467. */
  468. inline Point non_secret_combo_with_base(const Scalar &s, const Scalar &s_base) {
  469. Point r((NOINIT())); decaf_448_base_double_scalarmul_non_secret(r.p,s_base.s,p,s.s); return r;
  470. }
  471. /** @brief Return the base point */
  472. static inline const Point base() NOEXCEPT { return Point(decaf_448_point_base); }
  473. /** @brief Return the identity point */
  474. static inline const Point identity() NOEXCEPT { return Point(decaf_448_point_identity); }
  475. };
  476. /**
  477. * @brief Precomputed table of points.
  478. * Minor difficulties arise here because the decaf API doesn't expose, as a constant, how big such an object is.
  479. * Therefore we have to call malloc() or friends, but that's probably for the best, because you don't want to
  480. * stack-allocate a 15kiB object anyway.
  481. */
  482. class Precomputed {
  483. private:
  484. /** @cond internal */
  485. union {
  486. decaf_448_precomputed_s *mine;
  487. const decaf_448_precomputed_s *yours;
  488. } ours;
  489. bool isMine;
  490. inline void clear() NOEXCEPT {
  491. if (isMine) {
  492. decaf_448_precomputed_destroy(ours.mine);
  493. free(ours.mine);
  494. ours.yours = decaf_448_precomputed_base;
  495. isMine = false;
  496. }
  497. }
  498. inline void alloc() throw(std::bad_alloc) {
  499. if (isMine) return;
  500. int ret = posix_memalign((void**)&ours.mine, alignof_decaf_448_precomputed_s,sizeof_decaf_448_precomputed_s);
  501. if (ret || !ours.mine) {
  502. isMine = false;
  503. throw std::bad_alloc();
  504. }
  505. isMine = true;
  506. }
  507. inline const decaf_448_precomputed_s *get() const NOEXCEPT { return isMine ? ours.mine : ours.yours; }
  508. /** @endcond */
  509. public:
  510. /** Destructor securely erases the memory. */
  511. inline ~Precomputed() NOEXCEPT { clear(); }
  512. /**
  513. * @brief Initialize from underlying type, declared as a reference to prevent
  514. * it from being called with 0, thereby breaking override.
  515. *
  516. * The underlying object must remain valid throughout the lifetime of this one.
  517. *
  518. * By default, initializes to the table for the base point.
  519. *
  520. * @warning The empty initializer makes this equal to base, unlike the empty
  521. * initializer for points which makes this equal to the identity.
  522. */
  523. inline Precomputed(
  524. const decaf_448_precomputed_s &yours = *decaf_448_precomputed_base
  525. ) NOEXCEPT {
  526. ours.yours = &yours;
  527. isMine = false;
  528. }
  529. /**
  530. * @brief Assign. This may require an allocation and memcpy.
  531. */
  532. inline Precomputed &operator=(const Precomputed &it) throw(std::bad_alloc) {
  533. if (this == &it) return *this;
  534. if (it.isMine) {
  535. alloc();
  536. memcpy(ours.mine,it.ours.mine,sizeof_decaf_448_precomputed_s);
  537. } else {
  538. clear();
  539. ours.yours = it.ours.yours;
  540. }
  541. isMine = it.isMine;
  542. return *this;
  543. }
  544. /**
  545. * @brief Initilaize from point. Must allocate memory, and may throw.
  546. */
  547. inline Precomputed &operator=(const Point &it) throw(std::bad_alloc) {
  548. alloc();
  549. decaf_448_precompute(ours.mine,it.p);
  550. return *this;
  551. }
  552. /**
  553. * @brief Copy constructor.
  554. */
  555. inline Precomputed(const Precomputed &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  556. /**
  557. * @brief Constructor which initializes from point.
  558. */
  559. inline explicit Precomputed(const Point &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  560. #if __cplusplus >= 201103L
  561. inline Precomputed &operator=(Precomputed &&it) NOEXCEPT {
  562. if (this == &it) return *this;
  563. clear();
  564. ours = it.ours;
  565. isMine = it.isMine;
  566. it.isMine = false;
  567. it.ours.yours = decaf_448_precomputed_base;
  568. return *this;
  569. }
  570. inline Precomputed(Precomputed &&it) NOEXCEPT : isMine(false) { *this = it; }
  571. #endif
  572. /** @brief Fixed base scalarmul. */
  573. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r; decaf_448_precomputed_scalarmul(r.p,get(),s.s); return r; }
  574. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  575. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  576. /** @brief Return the table for the base point. */
  577. static inline const Precomputed base() NOEXCEPT { return Precomputed(*decaf_448_precomputed_base); }
  578. };
  579. }; /* struct decaf<448> */
  580. #undef NOEXCEPT
  581. #undef EXPLICIT_CON
  582. #undef GET_DATA
  583. } /* namespace decaf */
  584. #endif /* __DECAF_448_HXX__ */