barretenberg
Loading...
Searching...
No Matches
uintx.hpp
1
12#pragma once
13
14#include "../uint256/uint256.hpp"
15#include "barretenberg/common/assert.hpp"
16#include "barretenberg/common/throw_or_abort.hpp"
17#include <cstdint>
18#include <iomanip>
19#include <iostream>
20
21namespace numeric {
22
23template <class base_uint> class uintx {
24 public:
25 constexpr uintx(const uint64_t data = 0)
26 : lo(data)
27 , hi(base_uint(0))
28 {}
29
30 constexpr uintx(const base_uint input_lo)
31 : lo(input_lo)
32 , hi(base_uint(0))
33 {}
34
35 constexpr uintx(const base_uint input_lo, const base_uint input_hi)
36 : lo(input_lo)
37 , hi(input_hi)
38 {}
39
40 constexpr uintx(const uintx& other)
41 : lo(other.lo)
42 , hi(other.hi)
43 {}
44
45 constexpr uintx(uintx&& other) noexcept = default;
46
47 static constexpr size_t length() { return 2 * base_uint::length(); }
48 constexpr uintx& operator=(const uintx& other) = default;
49 constexpr uintx& operator=(uintx&& other) noexcept = default;
50
51 constexpr ~uintx() = default;
52 explicit constexpr operator bool() const { return static_cast<bool>(lo.data[0]); };
53 explicit constexpr operator uint8_t() const { return static_cast<uint8_t>(lo.data[0]); };
54 explicit constexpr operator uint16_t() const { return static_cast<uint16_t>(lo.data[0]); };
55 explicit constexpr operator uint32_t() const { return static_cast<uint32_t>(lo.data[0]); };
56 explicit constexpr operator uint64_t() const { return static_cast<uint64_t>(lo.data[0]); };
57
58 explicit constexpr operator base_uint() const { return lo; }
59
60 [[nodiscard]] constexpr bool get_bit(uint64_t bit_index) const;
61 [[nodiscard]] constexpr uint64_t get_msb() const;
62 constexpr uintx slice(uint64_t start, uint64_t end) const;
63
64 constexpr uintx operator+(const uintx& other) const;
65 constexpr uintx operator-(const uintx& other) const;
66 constexpr uintx operator-() const;
67
68 constexpr uintx operator*(const uintx& other) const;
69 constexpr uintx operator/(const uintx& other) const;
70 constexpr uintx operator%(const uintx& other) const;
71
72 constexpr std::pair<uintx, uintx> mul_extended(const uintx& other) const;
73
74 constexpr uintx operator>>(uint64_t other) const;
75 constexpr uintx operator<<(uint64_t other) const;
76
77 constexpr uintx operator&(const uintx& other) const;
78 constexpr uintx operator^(const uintx& other) const;
79 constexpr uintx operator|(const uintx& other) const;
80 constexpr uintx operator~() const;
81
82 constexpr bool operator==(const uintx& other) const;
83 constexpr bool operator!=(const uintx& other) const;
84 constexpr bool operator!() const;
85
86 constexpr bool operator>(const uintx& other) const;
87 constexpr bool operator<(const uintx& other) const;
88 constexpr bool operator>=(const uintx& other) const;
89 constexpr bool operator<=(const uintx& other) const;
90
91 constexpr uintx& operator+=(const uintx& other)
92 {
93 *this = *this + other;
94 return *this;
95 };
96 constexpr uintx& operator-=(const uintx& other)
97 {
98 *this = *this - other;
99 return *this;
100 };
101 constexpr uintx& operator*=(const uintx& other)
102 {
103 *this = *this * other;
104 return *this;
105 };
106 constexpr uintx& operator/=(const uintx& other)
107 {
108 *this = *this / other;
109 return *this;
110 };
111 constexpr uintx& operator%=(const uintx& other)
112 {
113 *this = *this % other;
114 return *this;
115 };
116
117 constexpr uintx& operator++()
118 {
119 *this += uintx(1);
120 return *this;
121 };
122 constexpr uintx& operator--()
123 {
124 *this -= uintx(1);
125 return *this;
126 };
127
128 constexpr uintx& operator&=(const uintx& other)
129 {
130 *this = *this & other;
131 return *this;
132 };
133 constexpr uintx& operator^=(const uintx& other)
134 {
135 *this = *this ^ other;
136 return *this;
137 };
138 constexpr uintx& operator|=(const uintx& other)
139 {
140 *this = *this | other;
141 return *this;
142 };
143
144 constexpr uintx& operator>>=(const uint64_t other)
145 {
146 *this = *this >> other;
147 return *this;
148 };
149 constexpr uintx& operator<<=(const uint64_t other)
150 {
151 *this = *this << other;
152 return *this;
153 };
154
155 constexpr uintx invmod(const uintx& modulus) const;
156 constexpr uintx unsafe_invmod(const uintx& modulus) const;
157
158 base_uint lo;
159 base_uint hi;
160
161 constexpr std::pair<uintx, uintx> divmod(const uintx& b) const;
162};
163
164template <typename B, typename Params> inline void read(B& it, uintx<Params>& value)
165{
166 using serialize::read;
167 Params a;
168 Params b;
169 read(it, b);
170 read(it, a);
171 value = uintx<Params>(a, b);
172}
173
174template <typename B, typename Params> inline void write(B& it, uintx<Params> const& value)
175{
176 using serialize::write;
177 write(it, value.hi);
178 write(it, value.lo);
179}
180
181template <class base_uint> inline std::ostream& operator<<(std::ostream& os, uintx<base_uint> const& a)
182{
183 os << a.lo << ", " << a.hi << std::endl;
184 return os;
185}
186
187using uint512_t = uintx<numeric::uint256_t>;
188using uint1024_t = uintx<uint512_t>;
189
190} // namespace numeric
191
192#include "./uintx_impl.hpp"
193
194using numeric::uint1024_t; // NOLINT
195using numeric::uint512_t; // NOLINT
Definition: uintx.hpp:23
constexpr uintx unsafe_invmod(const uintx &modulus) const
Definition: uintx_impl.hpp:65
constexpr uintx invmod(const uintx &modulus) const
Definition: uintx_impl.hpp:97
constexpr uintx slice(uint64_t start, uint64_t end) const
Definition: uintx_impl.hpp:117
Definition: field2_declarations.hpp:6