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.
 
 
 
 
 

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