Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
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
59 donna128& operator+=(const donna128& x)
60 {
61 l += x.l;
62 h += (l < x.l);
63 h += x.h;
64 return *this;
65 }
66
67 donna128& operator+=(uint64_t x)
68 {
69 l += x;
70 h += (l < x);
71 return *this;
72 }
73
74 uint64_t lo() const { return l; }
75 uint64_t hi() const { return h; }
76 private:
77 uint64_t h = 0, l = 0;
78 };
79
80 inline donna128 operator*(const donna128& x, uint64_t y)
81 {
82 BOTAN_ASSERT(x.hi() == 0, "High 64 bits of donna128 set to zero during multiply");
83
84 uint64_t lo = 0, hi = 0;
85 mul64x64_128(x.lo(), y, &lo, &hi);
86 return donna128(lo, hi);
87 }
88
89 inline donna128 operator+(const donna128& x, const donna128& y)
90 {
91 donna128 z = x;
92 z += y;
93 return z;
94 }
95
96 inline donna128 operator+(const donna128& x, uint64_t y)
97 {
98 donna128 z = x;
99 z += y;
100 return z;
101 }
102
103 inline donna128 operator|(const donna128& x, const donna128& y)
104 {
105 return donna128(x.lo() | y.lo(), x.hi() | y.hi());
106 }
107
108 inline uint64_t carry_shift(const donna128& a, size_t shift)
109 {
110 return (a >> shift).lo();
111 }
112
113 inline uint64_t combine_lower(const donna128& a, size_t s1,
114 const donna128& b, size_t s2)
115 {
116 donna128 z = (a >> s1) | (b << s2);
117 return z.lo();
118 }
119
120 #if defined(BOTAN_TARGET_HAS_NATIVE_UINT128)
121 inline uint64_t carry_shift(const uint128_t a, size_t shift)
122 {
123 return static_cast<uint64_t>(a >> shift);
124 }
125
126 inline uint64_t combine_lower(const uint128_t a, size_t s1,
127 const uint128_t b, size_t s2)
128 {
129 return static_cast<uint64_t>((a >> s1) | (b << s2));
130 }
131 #endif
132
133 }
134
135 #endif
Botan::donna128::donna128
donna128(uint64_t ll=0, uint64_t hh=0)
Definition: donna128.h:18
Botan::mul64x64_128
void mul64x64_128(uint64_t a, uint64_t b, uint64_t *lo, uint64_t *hi)
Definition: mul128.h:83
Botan::donna128::hi
uint64_t hi() const
Definition: donna128.h:75
Botan::donna128::operator&
friend uint64_t operator&(const donna128 &x, uint64_t mask)
Definition: donna128.h:47
Botan::donna128::operator<<
friend donna128 operator<<(const donna128 &x, size_t shift)
Definition: donna128.h:35
Botan::donna128
Definition: donna128.h:15
Botan::donna128::operator=
donna128 & operator=(const donna128 &)=default
Botan::donna128::operator>>
friend donna128 operator>>(const donna128 &x, size_t shift)
Definition: donna128.h:23
Botan::carry_shift
uint64_t carry_shift(const donna128 &a, size_t shift)
Definition: donna128.h:108
Botan::operator+
OID operator+(const OID &oid, uint32_t component)
Definition: asn1_oid.cpp:87
Botan::combine_lower
uint64_t combine_lower(const donna128 &a, size_t s1, const donna128 &b, size_t s2)
Definition: donna128.h:113
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
Botan::donna128::operator&=
uint64_t operator&=(uint64_t mask)
Definition: donna128.h:52
Botan::donna128::lo
uint64_t lo() const
Definition: donna128.h:74
Botan::operator*
BigInt operator*(const BigInt &x, const BigInt &y)
Definition: big_ops3.cpp:84
Botan
Definition: alg_id.cpp:13
Botan::donna128::operator+=
donna128 & operator+=(uint64_t x)
Definition: donna128.h:67
Botan::donna128::operator+=
donna128 & operator+=(const donna128 &x)
Definition: donna128.h:59
Botan::uint128_t
donna128 uint128_t
Definition: donna.cpp:63
Botan::operator|
ECIES_Flags operator|(ECIES_Flags a, ECIES_Flags b)
Definition: ecies.h:45