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.
 
 
 
 
 

724 lines
26 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 */
  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 unsigned char *buffer, size_t n) NOEXCEPT { decode(buffer,n); }
  226. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  227. inline Scalar(const Block &buffer) NOEXCEPT { decode(buffer.data(),buffer.size()); }
  228. /** @brief Decode from long buffer. */
  229. inline void decode(const unsigned char *buffer, size_t n) NOEXCEPT { decaf_448_scalar_decode_long(s,buffer,n); }
  230. /** @brief Assignment. */
  231. inline Scalar& operator=(const Scalar &x) NOEXCEPT { decaf_448_scalar_copy(s,x.s); return *this; }
  232. /** @brief Assign from unsigned word. */
  233. inline Scalar& operator=(decaf_word_t w) NOEXCEPT { decaf_448_scalar_set(s,w); return *this; }
  234. /** @brief Assign from signed int. */
  235. inline Scalar& operator=(int w) {
  236. Scalar t(-(decaf_word_t)INT_MIN);
  237. decaf_448_scalar_set(s,(decaf_word_t)w - (decaf_word_t)INT_MIN);
  238. *this -= t;
  239. return *this;
  240. }
  241. /** Destructor securely erases the scalar. */
  242. inline ~Scalar() NOEXCEPT { decaf_448_scalar_destroy(s); }
  243. /** @brief Assign from arbitrary-length little-endian byte sequence in a Block. */
  244. inline Scalar &operator=(const Block &bl) NOEXCEPT {
  245. decaf_448_scalar_decode_long(s,bl.data(),bl.size()); return *this;
  246. }
  247. /**
  248. * @brief Decode from correct-length little-endian byte sequence.
  249. * @return DECAF_FAILURE if the scalar is greater than or equal to the group order q.
  250. */
  251. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  252. Scalar &sc, const unsigned char buffer[SER_BYTES]
  253. ) NOEXCEPT {
  254. return decaf_448_scalar_decode(sc.s,buffer);
  255. }
  256. /** @brief Decode from correct-length little-endian byte sequence in C++ string. */
  257. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  258. Scalar &sc, const Block &buffer
  259. ) NOEXCEPT {
  260. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  261. return decaf_448_scalar_decode(sc.s,buffer);
  262. }
  263. /** @brief Encode to fixed-length string */
  264. inline EXPLICIT_CON operator SecureBuffer() const NOEXCEPT {
  265. SecureBuffer buf(SER_BYTES); decaf_448_scalar_encode(buf,s); return buf;
  266. }
  267. /** @brief Encode to fixed-length buffer */
  268. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  269. decaf_448_scalar_encode(buffer, s);
  270. }
  271. /** Add. */
  272. inline Scalar operator+ (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_add(r.s,s,q.s); return r; }
  273. /** Add to this. */
  274. inline Scalar &operator+=(const Scalar &q) NOEXCEPT { decaf_448_scalar_add(s,s,q.s); return *this; }
  275. /** Subtract. */
  276. inline Scalar operator- (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_sub(r.s,s,q.s); return r; }
  277. /** Subtract from this. */
  278. inline Scalar &operator-=(const Scalar &q) NOEXCEPT { decaf_448_scalar_sub(s,s,q.s); return *this; }
  279. /** Multiply */
  280. inline Scalar operator* (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_mul(r.s,s,q.s); return r; }
  281. /** Multiply into this. */
  282. inline Scalar &operator*=(const Scalar &q) NOEXCEPT { decaf_448_scalar_mul(s,s,q.s); return *this; }
  283. /** Negate */
  284. inline Scalar operator- () const NOEXCEPT { Scalar r((NOINIT())); decaf_448_scalar_sub(r.s,decaf_448_scalar_zero,s); return r; }
  285. /** @brief Invert with Fermat's Little Theorem (slow!). If *this == 0, return 0. */
  286. inline Scalar inverse() const NOEXCEPT { Scalar r; decaf_448_scalar_invert(r.s,s); return r; }
  287. /** @brief Divide by inverting q. If q == 0, return 0. */
  288. inline Scalar operator/ (const Scalar &q) const NOEXCEPT { return *this * q.inverse(); }
  289. /** @brief Divide by inverting q. If q == 0, return 0. */
  290. inline Scalar &operator/=(const Scalar &q) NOEXCEPT { return *this *= q.inverse(); }
  291. /** @brief Compare in constant time */
  292. inline bool operator!=(const Scalar &q) const NOEXCEPT { return !(*this == q); }
  293. /** @brief Compare in constant time */
  294. inline bool operator==(const Scalar &q) const NOEXCEPT { return !!decaf_448_scalar_eq(s,q.s); }
  295. /** @brief Scalarmul with scalar on left. */
  296. inline Point operator* (const Point &q) const NOEXCEPT { return q * (*this); }
  297. /** @brief Scalarmul-precomputed with scalar on left. */
  298. inline Point operator* (const Precomputed &q) const NOEXCEPT { return q * (*this); }
  299. /** @brief Direct scalar multiplication. */
  300. inline SecureBuffer direct_scalarmul(
  301. const Block &in,
  302. decaf_bool_t allow_identity=DECAF_FALSE,
  303. decaf_bool_t short_circuit=DECAF_TRUE
  304. ) const throw(CryptoException) {
  305. SecureBuffer out(/*FIXME Point::*/SER_BYTES);
  306. if (!decaf_448_direct_scalarmul(out, in.data(), s, allow_identity, short_circuit))
  307. throw CryptoException();
  308. return out;
  309. }
  310. };
  311. /**
  312. * @brief Element of prime-order group.
  313. */
  314. class Point {
  315. public:
  316. /** @brief Size of a serialized element */
  317. static const size_t SER_BYTES = DECAF_448_SER_BYTES;
  318. /** @brief Bytes required for hash */
  319. static const size_t HASH_BYTES = DECAF_448_SER_BYTES;
  320. /** The c-level object. */
  321. decaf_448_point_t p;
  322. /** @brief Don't initialize. */
  323. inline Point(const NOINIT &) {}
  324. /** @brief Constructor sets to identity by default. */
  325. inline Point(const decaf_448_point_t &q = decaf_448_point_identity) { decaf_448_point_copy(p,q); }
  326. /** @brief Copy constructor. */
  327. inline Point(const Point &q) { decaf_448_point_copy(p,q.p); }
  328. /** @brief Assignment. */
  329. inline Point& operator=(const Point &q) { decaf_448_point_copy(p,q.p); return *this; }
  330. /** @brief Destructor securely erases the point. */
  331. inline ~Point() { decaf_448_point_destroy(p); }
  332. /** @brief Construct from RNG */
  333. inline explicit Point(SpongeRng &rng, bool uniform = true);
  334. /**
  335. * @brief Initialize from C++ fixed-length byte string.
  336. * The all-zero string maps to the identity.
  337. *
  338. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  339. * or was the identity and allow_identity was DECAF_FALSE.
  340. */
  341. inline explicit Point(const Block &s, decaf_bool_t allow_identity=DECAF_TRUE) throw(CryptoException) {
  342. if (!decode(*this,s,allow_identity)) throw CryptoException();
  343. }
  344. /**
  345. * @brief Initialize from C fixed-length byte string.
  346. * The all-zero string maps to the identity.
  347. *
  348. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  349. * or was the identity and allow_identity was DECAF_FALSE.
  350. */
  351. inline explicit Point(const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE)
  352. throw(CryptoException) { if (!decode(*this,buffer,allow_identity)) throw CryptoException(); }
  353. /**
  354. * @brief Initialize from C fixed-length byte string.
  355. * The all-zero string maps to the identity.
  356. *
  357. * @retval DECAF_SUCCESS the string was successfully decoded.
  358. * @return DECAF_FAILURE the string wasn't the encoding of a point, or was the identity
  359. * and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  360. */
  361. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  362. Point &p, const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE
  363. ) NOEXCEPT {
  364. return decaf_448_point_decode(p.p,buffer,allow_identity);
  365. }
  366. /**
  367. * @brief Initialize from C++ fixed-length byte string.
  368. * The all-zero string maps to the identity.
  369. *
  370. * @retval DECAF_SUCCESS the string was successfully decoded.
  371. * @return DECAF_FAILURE the string was the wrong length, or wasn't the encoding of a point,
  372. * or was the identity and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  373. */
  374. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  375. Point &p, const Block &buffer, decaf_bool_t allow_identity=DECAF_TRUE
  376. ) NOEXCEPT {
  377. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  378. return decaf_448_point_decode(p.p,buffer.data(),allow_identity);
  379. }
  380. /**
  381. * @brief Map uniformly to the curve from a hash buffer.
  382. * The empty or all-zero string maps to the identity, as does the string "\x01".
  383. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  384. * but the buffer will be zero-padded on the right.
  385. */
  386. static inline Point from_hash ( const Block &s ) NOEXCEPT {
  387. Point p((NOINIT())); p.set_to_hash(s); return p;
  388. }
  389. /**
  390. * @brief Map to the curve from a hash buffer.
  391. * The empty or all-zero string maps to the identity, as does the string "\x01".
  392. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  393. * but the buffer will be zero-padded on the right.
  394. */
  395. inline void set_to_hash( const Block &s ) NOEXCEPT {
  396. if (s.size() < HASH_BYTES) {
  397. SecureBuffer b(HASH_BYTES);
  398. memcpy(b.data(), s.data(), s.size());
  399. decaf_448_point_from_hash_nonuniform(p,b);
  400. } else if (s.size() == HASH_BYTES) {
  401. decaf_448_point_from_hash_nonuniform(p,s);
  402. } else if (s.size() < 2*HASH_BYTES) {
  403. SecureBuffer b(2*HASH_BYTES);
  404. memcpy(b.data(), s.data(), s.size());
  405. decaf_448_point_from_hash_uniform(p,b);
  406. } else {
  407. decaf_448_point_from_hash_uniform(p,s);
  408. }
  409. }
  410. /**
  411. * @brief Encode to string. The identity encodes to the all-zero string.
  412. */
  413. inline EXPLICIT_CON operator SecureBuffer() const NOEXCEPT {
  414. SecureBuffer buffer(SER_BYTES);
  415. decaf_448_point_encode(buffer, p);
  416. return buffer;
  417. }
  418. /**
  419. * @brief Encode to a C buffer. The identity encodes to all zeros.
  420. */
  421. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  422. decaf_448_point_encode(buffer, p);
  423. }
  424. /** @brief Point add. */
  425. inline Point operator+ (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_448_point_add(r.p,p,q.p); return r; }
  426. /** @brief Point add. */
  427. inline Point &operator+=(const Point &q) NOEXCEPT { decaf_448_point_add(p,p,q.p); return *this; }
  428. /** @brief Point subtract. */
  429. inline Point operator- (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_448_point_sub(r.p,p,q.p); return r; }
  430. /** @brief Point subtract. */
  431. inline Point &operator-=(const Point &q) NOEXCEPT { decaf_448_point_sub(p,p,q.p); return *this; }
  432. /** @brief Point negate. */
  433. inline Point operator- () const NOEXCEPT { Point r((NOINIT())); decaf_448_point_negate(r.p,p); return r; }
  434. /** @brief Double the point out of place. */
  435. inline Point times_two () const NOEXCEPT { Point r((NOINIT())); decaf_448_point_double(r.p,p); return r; }
  436. /** @brief Double the point in place. */
  437. inline Point &double_in_place() NOEXCEPT { decaf_448_point_double(p,p); return *this; }
  438. /** @brief Constant-time compare. */
  439. inline bool operator!=(const Point &q) const NOEXCEPT { return ! decaf_448_point_eq(p,q.p); }
  440. /** @brief Constant-time compare. */
  441. inline bool operator==(const Point &q) const NOEXCEPT { return !!decaf_448_point_eq(p,q.p); }
  442. /** @brief Scalar multiply. */
  443. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r((NOINIT())); decaf_448_point_scalarmul(r.p,p,s.s); return r; }
  444. /** @brief Scalar multiply in place. */
  445. inline Point &operator*=(const Scalar &s) NOEXCEPT { decaf_448_point_scalarmul(p,p,s.s); return *this; }
  446. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  447. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  448. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  449. inline Point &operator/=(const Scalar &s) NOEXCEPT { return (*this) *= s.inverse(); }
  450. /** @brief Validate / sanity check */
  451. inline bool validate() const NOEXCEPT { return !!decaf_448_point_valid(p); }
  452. /** @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster. */
  453. static inline Point double_scalarmul (
  454. const Point &q, const Scalar &qs, const Point &r, const Scalar &rs
  455. ) NOEXCEPT {
  456. Point p((NOINIT())); decaf_448_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  457. }
  458. /**
  459. * @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster.
  460. * For those who like their scalars before the point.
  461. */
  462. static inline Point double_scalarmul (
  463. const Scalar &qs, const Point &q, const Scalar &rs, const Point &r
  464. ) NOEXCEPT {
  465. Point p((NOINIT())); decaf_448_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  466. }
  467. /**
  468. * @brief Double-scalar multiply: this point by the first scalar and base by the second scalar.
  469. * @warning This function takes variable time, and may leak the scalars (or points, but currently
  470. * it doesn't).
  471. */
  472. inline Point non_secret_combo_with_base(const Scalar &s, const Scalar &s_base) {
  473. Point r((NOINIT())); decaf_448_base_double_scalarmul_non_secret(r.p,s_base.s,p,s.s); return r;
  474. }
  475. /** @brief Return the base point */
  476. static inline const Point base() NOEXCEPT { return Point(decaf_448_point_base); }
  477. /** @brief Return the identity point */
  478. static inline const Point identity() NOEXCEPT { return Point(decaf_448_point_identity); }
  479. };
  480. /**
  481. * @brief Precomputed table of points.
  482. * Minor difficulties arise here because the decaf API doesn't expose, as a constant, how big such an object is.
  483. * Therefore we have to call malloc() or friends, but that's probably for the best, because you don't want to
  484. * stack-allocate a 15kiB object anyway.
  485. */
  486. class Precomputed {
  487. private:
  488. /** @cond internal */
  489. union {
  490. decaf_448_precomputed_s *mine;
  491. const decaf_448_precomputed_s *yours;
  492. } ours;
  493. bool isMine;
  494. inline void clear() NOEXCEPT {
  495. if (isMine) {
  496. decaf_448_precomputed_destroy(ours.mine);
  497. free(ours.mine);
  498. ours.yours = decaf_448_precomputed_base;
  499. isMine = false;
  500. }
  501. }
  502. inline void alloc() throw(std::bad_alloc) {
  503. if (isMine) return;
  504. int ret = posix_memalign((void**)&ours.mine, alignof_decaf_448_precomputed_s,sizeof_decaf_448_precomputed_s);
  505. if (ret || !ours.mine) {
  506. isMine = false;
  507. throw std::bad_alloc();
  508. }
  509. isMine = true;
  510. }
  511. inline const decaf_448_precomputed_s *get() const NOEXCEPT { return isMine ? ours.mine : ours.yours; }
  512. /** @endcond */
  513. public:
  514. /** Destructor securely erases the memory. */
  515. inline ~Precomputed() NOEXCEPT { clear(); }
  516. /**
  517. * @brief Initialize from underlying type, declared as a reference to prevent
  518. * it from being called with 0, thereby breaking override.
  519. *
  520. * The underlying object must remain valid throughout the lifetime of this one.
  521. *
  522. * By default, initializes to the table for the base point.
  523. *
  524. * @warning The empty initializer makes this equal to base, unlike the empty
  525. * initializer for points which makes this equal to the identity.
  526. */
  527. inline Precomputed(
  528. const decaf_448_precomputed_s &yours = *decaf_448_precomputed_base
  529. ) NOEXCEPT {
  530. ours.yours = &yours;
  531. isMine = false;
  532. }
  533. /**
  534. * @brief Assign. This may require an allocation and memcpy.
  535. */
  536. inline Precomputed &operator=(const Precomputed &it) throw(std::bad_alloc) {
  537. if (this == &it) return *this;
  538. if (it.isMine) {
  539. alloc();
  540. memcpy(ours.mine,it.ours.mine,sizeof_decaf_448_precomputed_s);
  541. } else {
  542. clear();
  543. ours.yours = it.ours.yours;
  544. }
  545. isMine = it.isMine;
  546. return *this;
  547. }
  548. /**
  549. * @brief Initilaize from point. Must allocate memory, and may throw.
  550. */
  551. inline Precomputed &operator=(const Point &it) throw(std::bad_alloc) {
  552. alloc();
  553. decaf_448_precompute(ours.mine,it.p);
  554. return *this;
  555. }
  556. /**
  557. * @brief Copy constructor.
  558. */
  559. inline Precomputed(const Precomputed &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  560. /**
  561. * @brief Constructor which initializes from point.
  562. */
  563. inline explicit Precomputed(const Point &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  564. #if __cplusplus >= 201103L
  565. inline Precomputed &operator=(Precomputed &&it) NOEXCEPT {
  566. if (this == &it) return *this;
  567. clear();
  568. ours = it.ours;
  569. isMine = it.isMine;
  570. it.isMine = false;
  571. it.ours.yours = decaf_448_precomputed_base;
  572. return *this;
  573. }
  574. inline Precomputed(Precomputed &&it) NOEXCEPT : isMine(false) { *this = it; }
  575. #endif
  576. /** @brief Fixed base scalarmul. */
  577. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r; decaf_448_precomputed_scalarmul(r.p,get(),s.s); return r; }
  578. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  579. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  580. /** @brief Return the table for the base point. */
  581. static inline const Precomputed base() NOEXCEPT { return Precomputed(*decaf_448_precomputed_base); }
  582. };
  583. }; /* struct decaf<448> */
  584. #undef NOEXCEPT
  585. #undef EXPLICIT_CON
  586. #undef GET_DATA
  587. } /* namespace decaf */
  588. #endif /* __DECAF_448_HXX__ */