Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
uint1.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <type_traits>
5
6namespace bb::avm2 {
7
15class uint1_t {
16 public:
17 uint1_t(const uint1_t& other) = default;
18 uint1_t& operator=(const uint1_t& other) = default;
19
20 // Default constructor initializes to 0.
21 constexpr uint1_t() noexcept
22 : value_(false)
23 {}
24
25 // Constructor from bool.
26 constexpr uint1_t(bool b) noexcept
27 : value_(b)
28 {}
29
30 // Constructor from integral types. Zero becomes 0, non-zero becomes 1.
31 template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
32 constexpr uint1_t(T v) noexcept
33 : value_(v != 0)
34 {}
35
36 // Arithmetic operators.
37 constexpr uint1_t operator+(const uint1_t& other) const noexcept { return *this ^ other; }
38 constexpr uint1_t operator-(const uint1_t& other) const noexcept { return *this + other; }
39 constexpr uint1_t operator*(const uint1_t& other) const noexcept { return *this & other; }
40 constexpr uint1_t operator/(const uint1_t& other) const noexcept { return uint1_t(value() / other.value()); }
41 constexpr uint1_t operator-() const noexcept { return uint1_t(!value_); }
42
43 // Bitwise operators.
44 constexpr uint1_t operator&(const uint1_t& other) const noexcept { return uint1_t(value_ && other.value_); }
45 constexpr uint1_t operator|(const uint1_t& other) const noexcept { return uint1_t(value_ || other.value_); }
46 constexpr uint1_t operator^(const uint1_t& other) const noexcept { return uint1_t(value_ != other.value_); }
47 constexpr uint1_t operator~() const noexcept { return uint1_t(!value_); }
48
49 // Shifts are a bit special.
50 constexpr uint1_t operator<<(const uint1_t& other) const noexcept { return (*this - (*this * other)); }
51 constexpr uint1_t operator>>(const uint1_t& other) const noexcept { return (*this - (*this * other)); }
52
53 // Comparison operators.
54 constexpr bool operator==(const uint1_t& other) const noexcept = default;
55 constexpr bool operator!=(const uint1_t& other) const noexcept = default;
56 constexpr bool operator<(const uint1_t& other) const noexcept { return value_ < other.value_; }
57 constexpr bool operator<=(const uint1_t& other) const noexcept { return value_ <= other.value_; }
58 constexpr bool operator>(const uint1_t& other) const noexcept { return value_ > other.value_; }
59 constexpr bool operator>=(const uint1_t& other) const noexcept { return value_ >= other.value_; }
60
61 // Get the raw value. Guaranteed to be 0 or 1.
62 constexpr uint8_t value() const noexcept { return value_ ? 1 : 0; }
63 constexpr operator uint8_t() const noexcept { return value(); }
64
65 private:
66 bool value_;
67};
68
69} // namespace bb::avm2
A 1-bit unsigned integer type.
Definition uint1.hpp:15
constexpr uint1_t operator*(const uint1_t &other) const noexcept
Definition uint1.hpp:39
constexpr uint1_t operator^(const uint1_t &other) const noexcept
Definition uint1.hpp:46
constexpr uint1_t operator|(const uint1_t &other) const noexcept
Definition uint1.hpp:45
constexpr bool operator<(const uint1_t &other) const noexcept
Definition uint1.hpp:56
constexpr bool operator==(const uint1_t &other) const noexcept=default
uint1_t & operator=(const uint1_t &other)=default
constexpr bool operator>(const uint1_t &other) const noexcept
Definition uint1.hpp:58
constexpr uint1_t(T v) noexcept
Definition uint1.hpp:32
constexpr uint1_t operator/(const uint1_t &other) const noexcept
Definition uint1.hpp:40
constexpr bool operator>=(const uint1_t &other) const noexcept
Definition uint1.hpp:59
constexpr uint1_t operator&(const uint1_t &other) const noexcept
Definition uint1.hpp:44
constexpr uint1_t(bool b) noexcept
Definition uint1.hpp:26
constexpr uint1_t operator-() const noexcept
Definition uint1.hpp:41
constexpr uint1_t operator~() const noexcept
Definition uint1.hpp:47
constexpr uint1_t operator+(const uint1_t &other) const noexcept
Definition uint1.hpp:37
constexpr bool operator<=(const uint1_t &other) const noexcept
Definition uint1.hpp:57
constexpr uint1_t operator-(const uint1_t &other) const noexcept
Definition uint1.hpp:38
constexpr bool operator!=(const uint1_t &other) const noexcept=default
constexpr uint1_t operator<<(const uint1_t &other) const noexcept
Definition uint1.hpp:50
constexpr uint8_t value() const noexcept
Definition uint1.hpp:62
constexpr uint1_t operator>>(const uint1_t &other) const noexcept
Definition uint1.hpp:51
constexpr uint1_t() noexcept
Definition uint1.hpp:21
uint1_t(const uint1_t &other)=default
FF b