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.
 
 
 
 
 

87 lines
3.9 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++ version.
  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. * This library may support multiple curves eventually. The Ed448-Goldilocks
  23. * specific identifiers are prefixed with DECAF_448 or decaf_448.
  24. */
  25. #ifndef __DECAF_448_HXX__
  26. #define __DECAF_448_HXX__ 1
  27. #include "decaf.h"
  28. template<unsigned int bits = 448> struct decaf;
  29. /* TODO: document */
  30. /* TODO: This is incomplete */
  31. template<> struct decaf<448> {
  32. class Scalar {
  33. public:
  34. decaf_448_scalar_t s;
  35. inline Scalar() {}
  36. inline Scalar(const decaf_word_t w) { decaf_448_scalar_set(s,w); }
  37. inline Scalar(const decaf_448_scalar_t &t) { decaf_448_scalar_copy(s,t); }
  38. inline Scalar(const Scalar &x) { decaf_448_scalar_copy(s,x.s); }
  39. inline Scalar& operator=(const Scalar &x) { decaf_448_scalar_copy(s,x.s); return *this; }
  40. inline ~Scalar() { decaf_448_scalar_destroy(s); }
  41. inline Scalar operator+ (const Scalar &q) { Scalar r; decaf_448_scalar_add(r.s,s,q.s); return r; }
  42. inline Scalar operator+=(const Scalar &q) { decaf_448_scalar_add(s,s,q.s); return *this; }
  43. inline Scalar operator- (const Scalar &q) { Scalar r; decaf_448_scalar_sub(r.s,s,q.s); return r; }
  44. inline Scalar operator-=(const Scalar &q) { decaf_448_scalar_sub(s,s,q.s); return *this; }
  45. inline Scalar operator* (const Scalar &q) { Scalar r; decaf_448_scalar_mul(r.s,s,q.s); return r; }
  46. inline Scalar operator*=(const Scalar &q) { decaf_448_scalar_mul(s,s,q.s); return *this; }
  47. inline Scalar operator-() { Scalar r; decaf_448_scalar_sub(r.s,decaf_448_scalar_zero,s); return r; }
  48. inline bool operator==(const Scalar &q) { return !!decaf_448_scalar_eq(s,q.s); }
  49. };
  50. class Point {
  51. public:
  52. decaf_448_point_t p;
  53. inline Point() {}
  54. inline Point(const decaf_448_point_t &q) { decaf_448_point_copy(p,q); } /* TODO: not memcpy? */
  55. inline Point(const Point &q) { decaf_448_point_copy(p,q.p); }
  56. inline Point& operator=(const Point &q) { decaf_448_point_copy(p,q.p); return *this; }
  57. inline ~Point() { decaf_448_point_destroy(p); }
  58. inline Point operator+(const Point &q) { Point r; decaf_448_point_add(r.p,p,q.p); return r; }
  59. inline Point operator+=(const Point &q) { decaf_448_point_add(p,p,q.p); return *this; }
  60. inline Point operator-(const Point &q) { Point r; decaf_448_point_sub(r.p,p,q.p); return r; }
  61. inline Point operator-=(const Point &q) { decaf_448_point_sub(p,p,q.p); return *this; }
  62. inline Point operator-() { Point r; decaf_448_point_negate(r.p,p); return r; }
  63. inline Point operator*(const Scalar &s) { Point r; decaf_448_point_scalarmul(r.p,p,s.s); return r; }
  64. inline Point operator*=(const Scalar &s) { decaf_448_point_scalarmul(p,p,s.s); return *this; }
  65. inline Point times_two() { Point r; decaf_448_point_double(r.p,p); return r; }
  66. inline Point &double_in_place() { decaf_448_point_double(p,p); return *this; }
  67. inline bool operator==(const Point &q) { return !!decaf_448_point_eq(p,q.p); }
  68. static inline Point double_scalar_mul(
  69. const Point &q, const Scalar &qs, const Point &r, const Scalar &rs
  70. ) {
  71. Point p; decaf_448_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  72. }
  73. };
  74. }; /* struct decaf<448> */
  75. #endif /* __DECAF_448_HXX__ */