Botan  2.7.0
Crypto and TLS for C++11
donna128.h
Go to the documentation of this file.
1 /*
2 * A minimal 128-bit integer type for curve25519-donna
3 * (C) 2014 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_CURVE25519_DONNA128_H_
9 #define BOTAN_CURVE25519_DONNA128_H_
10 
11 #include <botan/mul128.h>
12 
13 namespace Botan {
14 
15 class donna128 final
16  {
17  public:
18  donna128(uint64_t ll = 0, uint64_t hh = 0) { l = ll; h = hh; }
19 
20  donna128(const donna128&) = default;
21  donna128& operator=(const donna128&) = default;
22 
23  friend donna128 operator>>(const donna128& x, size_t shift)
24  {
25  donna128 z = x;
26  if(shift > 0)
27  {
28  const uint64_t carry = z.h << (64 - shift);
29  z.h = (z.h >> shift);
30  z.l = (z.l >> shift) | carry;
31  }
32  return z;
33  }
34 
35  friend donna128 operator<<(const donna128& x, size_t shift)
36  {
37  donna128 z = x;
38  if(shift > 0)
39  {
40  const uint64_t carry = z.l >> (64 - shift);
41  z.l = (z.l << shift);
42  z.h = (z.h << shift) | carry;
43  }
44  return z;
45  }
46 
47  friend uint64_t operator&(const donna128& x, uint64_t mask)
48  {
49  return x.l & mask;
50  }
51 
52  uint64_t operator&=(uint64_t mask)
53  {
54  h = 0;
55  l &= mask;
56  return l;
57  }
58 
60  {
61  l += x.l;
62  h += x.h;
63 
64  const uint64_t carry = (l < x.l);
65  h += carry;
66  return *this;
67  }
68 
69  donna128& operator+=(uint64_t x)
70  {
71  l += x;
72  const uint64_t carry = (l < x);
73  h += carry;
74  return *this;
75  }
76 
77  uint64_t lo() const { return l; }
78  uint64_t hi() const { return h; }
79  private:
80  uint64_t h = 0, l = 0;
81  };
82 
83 inline donna128 operator*(const donna128& x, uint64_t y)
84  {
85  BOTAN_ARG_CHECK(x.hi() == 0, "High 64 bits of donna128 set to zero during multiply");
86 
87  uint64_t lo = 0, hi = 0;
88  mul64x64_128(x.lo(), y, &lo, &hi);
89  return donna128(lo, hi);
90  }
91 
92 inline donna128 operator*(uint64_t y, const donna128& x)
93  {
94  return x * y;
95  }
96 
97 inline donna128 operator+(const donna128& x, const donna128& y)
98  {
99  donna128 z = x;
100  z += y;
101  return z;
102  }
103 
104 inline donna128 operator+(const donna128& x, uint64_t y)
105  {
106  donna128 z = x;
107  z += y;
108  return z;
109  }
110 
111 inline donna128 operator|(const donna128& x, const donna128& y)
112  {
113  return donna128(x.lo() | y.lo(), x.hi() | y.hi());
114  }
115 
116 inline uint64_t carry_shift(const donna128& a, size_t shift)
117  {
118  return (a >> shift).lo();
119  }
120 
121 inline uint64_t combine_lower(const donna128& a, size_t s1,
122  const donna128& b, size_t s2)
123  {
124  donna128 z = (a >> s1) | (b << s2);
125  return z.lo();
126  }
127 
128 #if defined(BOTAN_TARGET_HAS_NATIVE_UINT128)
129 inline uint64_t carry_shift(const uint128_t a, size_t shift)
130  {
131  return static_cast<uint64_t>(a >> shift);
132  }
133 
134 inline uint64_t combine_lower(const uint128_t a, size_t s1,
135  const uint128_t b, size_t s2)
136  {
137  return static_cast<uint64_t>((a >> s1) | (b << s2));
138  }
139 #endif
140 
141 }
142 
143 #endif
donna128(uint64_t ll=0, uint64_t hh=0)
Definition: donna128.h:18
void mul64x64_128(uint64_t a, uint64_t b, uint64_t *lo, uint64_t *hi)
Definition: mul128.h:83
uint64_t lo() const
Definition: donna128.h:77
void carry(int64_t &h0, int64_t &h1)
friend uint64_t operator &(const donna128 &x, uint64_t mask)
Definition: donna128.h:47
friend donna128 operator<<(const donna128 &x, size_t shift)
Definition: donna128.h:35
donna128 & operator=(const donna128 &)=default
uint64_t operator &=(uint64_t mask)
Definition: donna128.h:52
friend donna128 operator>>(const donna128 &x, size_t shift)
Definition: donna128.h:23
uint64_t carry_shift(const donna128 &a, size_t shift)
Definition: donna128.h:116
uint64_t combine_lower(const donna128 &a, size_t s1, const donna128 &b, size_t s2)
Definition: donna128.h:121
uint64_t hi() const
Definition: donna128.h:78
Definition: alg_id.cpp:13
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:37
BigInt operator*(const BigInt &x, const BigInt &y)
Definition: big_ops3.cpp:103
OID operator+(const OID &oid, uint32_t component)
Definition: asn1_oid.cpp:87
donna128 & operator+=(uint64_t x)
Definition: donna128.h:69
donna128 & operator+=(const donna128 &x)
Definition: donna128.h:59
ECIES_Flags operator|(ECIES_Flags a, ECIES_Flags b)
Definition: ecies.h:45