Botan 3.3.0
Crypto and TLS for C&
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/internal/mul128.h>
12
13namespace Botan {
14
16 public:
17 donna128(uint64_t ll = 0, uint64_t hh = 0) {
18 l = ll;
19 h = hh;
20 }
21
22 donna128(const donna128&) = default;
23 donna128& operator=(const donna128&) = default;
24
25 friend donna128 operator>>(const donna128& x, size_t shift) {
26 donna128 z = x;
27 if(shift > 0) {
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 donna128 z = x;
37 if(shift > 0) {
38 const uint64_t carry = z.l >> (64 - shift);
39 z.l = (z.l << shift);
40 z.h = (z.h << shift) | carry;
41 }
42 return z;
43 }
44
45 friend uint64_t operator&(const donna128& x, uint64_t mask) { return x.l & mask; }
46
47 uint64_t operator&=(uint64_t mask) {
48 h = 0;
49 l &= mask;
50 return l;
51 }
52
54 l += x.l;
55 h += x.h;
56
57 const uint64_t carry = (l < x.l);
58 h += carry;
59 return *this;
60 }
61
62 donna128& operator+=(uint64_t x) {
63 l += x;
64 const uint64_t carry = (l < x);
65 h += carry;
66 return *this;
67 }
68
69 uint64_t lo() const { return l; }
70
71 uint64_t hi() const { return h; }
72
73 private:
74 uint64_t h = 0, l = 0;
75};
76
77inline donna128 operator*(const donna128& x, uint64_t y) {
78 BOTAN_ARG_CHECK(x.hi() == 0, "High 64 bits of donna128 set to zero during multiply");
79
80 uint64_t lo = 0, hi = 0;
81 mul64x64_128(x.lo(), y, &lo, &hi);
82 return donna128(lo, hi);
83}
84
85inline donna128 operator*(uint64_t y, const donna128& x) {
86 return x * y;
87}
88
89inline donna128 operator+(const donna128& x, const donna128& y) {
90 donna128 z = x;
91 z += y;
92 return z;
93}
94
95inline donna128 operator+(const donna128& x, uint64_t y) {
96 donna128 z = x;
97 z += y;
98 return z;
99}
100
101inline donna128 operator|(const donna128& x, const donna128& y) {
102 return donna128(x.lo() | y.lo(), x.hi() | y.hi());
103}
104
105inline uint64_t carry_shift(const donna128& a, size_t shift) {
106 return (a >> shift).lo();
107}
108
109inline uint64_t combine_lower(const donna128& a, size_t s1, const donna128& b, size_t s2) {
110 donna128 z = (a >> s1) | (b << s2);
111 return z.lo();
112}
113
114#if defined(BOTAN_TARGET_HAS_NATIVE_UINT128)
115inline uint64_t carry_shift(const uint128_t a, size_t shift) {
116 return static_cast<uint64_t>(a >> shift);
117}
118
119inline uint64_t combine_lower(const uint128_t a, size_t s1, const uint128_t b, size_t s2) {
120 return static_cast<uint64_t>((a >> s1) | (b << s2));
121}
122#endif
123
124} // namespace Botan
125
126#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
friend donna128 operator<<(const donna128 &x, size_t shift)
Definition donna128.h:35
uint64_t operator&=(uint64_t mask)
Definition donna128.h:47
uint64_t lo() const
Definition donna128.h:69
friend uint64_t operator&(const donna128 &x, uint64_t mask)
Definition donna128.h:45
friend donna128 operator>>(const donna128 &x, size_t shift)
Definition donna128.h:25
donna128 & operator+=(const donna128 &x)
Definition donna128.h:53
donna128(uint64_t ll=0, uint64_t hh=0)
Definition donna128.h:17
uint64_t hi() const
Definition donna128.h:71
donna128 & operator+=(uint64_t x)
Definition donna128.h:62
donna128 & operator=(const donna128 &)=default
donna128(const donna128 &)=default
int(* final)(unsigned char *, CTX *)
ASN1_Type operator|(ASN1_Type x, ASN1_Type y)
Definition asn1_obj.h:74
BigInt operator*(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:46
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
uint64_t carry_shift(const donna128 &a, size_t shift)
Definition donna128.h:105
uint64_t combine_lower(const donna128 &a, size_t s1, const donna128 &b, size_t s2)
Definition donna128.h:109
void carry(int64_t &h0, int64_t &h1)
void mul64x64_128(uint64_t a, uint64_t b, uint64_t *lo, uint64_t *hi)
Definition mul128.h:29