Botan 3.0.0-alpha0
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 {
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
83inline 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
92inline donna128 operator*(uint64_t y, const donna128& x)
93 {
94 return x * y;
95 }
96
97inline donna128 operator+(const donna128& x, const donna128& y)
98 {
99 donna128 z = x;
100 z += y;
101 return z;
102 }
103
104inline donna128 operator+(const donna128& x, uint64_t y)
105 {
106 donna128 z = x;
107 z += y;
108 return z;
109 }
110
111inline donna128 operator|(const donna128& x, const donna128& y)
112 {
113 return donna128(x.lo() | y.lo(), x.hi() | y.hi());
114 }
115
116inline uint64_t carry_shift(const donna128& a, size_t shift)
117 {
118 return (a >> shift).lo();
119 }
120
121inline 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)
129inline uint64_t carry_shift(const uint128_t a, size_t shift)
130 {
131 return static_cast<uint64_t>(a >> shift);
132 }
133
134inline 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
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:36
friend donna128 operator<<(const donna128 &x, size_t shift)
Definition: donna128.h:35
uint64_t operator&=(uint64_t mask)
Definition: donna128.h:52
uint64_t lo() const
Definition: donna128.h:77
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:23
donna128 & operator+=(const donna128 &x)
Definition: donna128.h:59
donna128(uint64_t ll=0, uint64_t hh=0)
Definition: donna128.h:18
uint64_t hi() const
Definition: donna128.h:78
donna128 & operator+=(uint64_t x)
Definition: donna128.h:69
donna128 & operator=(const donna128 &)=default
donna128(const donna128 &)=default
int(* final)(unsigned char *, CTX *)
PolynomialVector b
Definition: kyber.cpp:821
Definition: alg_id.cpp:13
ASN1_Type operator|(ASN1_Type x, ASN1_Type y)
Definition: asn1_obj.h:71
BigInt operator*(const BigInt &x, const BigInt &y)
Definition: big_ops3.cpp:48
void carry(int64_t &h0, int64_t &h1)
uint64_t carry_shift(const donna128 &a, size_t shift)
Definition: donna128.h:116
OID operator+(const OID &oid, uint32_t new_comp)
Definition: asn1_oid.cpp:120
uint64_t combine_lower(const donna128 &a, size_t s1, const donna128 &b, size_t s2)
Definition: donna128.h:121
void mul64x64_128(uint64_t a, uint64_t b, uint64_t *lo, uint64_t *hi)
Definition: mul128.h:38