Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bitwise.cpp
Go to the documentation of this file.
2
3#include <cstdint>
4
7
8namespace bb::avm2::simulation {
9
22{
23 try {
24 MemoryValue c = a & b;
25 events.emit({
26 .operation = BitwiseOperation::AND,
27 .a = a,
28 .b = b,
29 .res = static_cast<uint128_t>(c.as_ff()),
30 .simd_64 = false,
31 });
32 return c;
33 } catch (const TaggedValueException& e) {
34 events.emit({ .operation = BitwiseOperation::AND, .a = a, .b = b, .res = 0, .simd_64 = false });
35 throw BitwiseException("AND, " + std::string(e.what()));
36 }
37}
38
51{
52 try {
53 MemoryValue c = a | b;
54 events.emit({
55 .operation = BitwiseOperation::OR,
56 .a = a,
57 .b = b,
58 .res = static_cast<uint128_t>(c.as_ff()),
59 .simd_64 = false,
60 });
61 return c;
62 } catch (const TaggedValueException& e) {
63 events.emit({ .operation = BitwiseOperation::OR, .a = a, .b = b, .res = 0, .simd_64 = false });
64 throw BitwiseException("OR, " + std::string(e.what()));
65 }
66}
67
80{
81 try {
82 MemoryValue c = a ^ b;
83 events.emit({
84 .operation = BitwiseOperation::XOR,
85 .a = a,
86 .b = b,
87 .res = static_cast<uint128_t>(c.as_ff()),
88 .simd_64 = false,
89 });
90 return c;
91 } catch (const TaggedValueException& e) {
92 events.emit({ .operation = BitwiseOperation::XOR, .a = a, .b = b, .res = 0, .simd_64 = false });
93 throw BitwiseException("XOR, " + std::string(e.what()));
94 }
95}
96
97namespace {
98
99// Pack two U64 lanes (lane 0 = low, lane 1 = high) into a single U128.
100MemoryValue pack_lanes_64(const MemoryValue& lo, const MemoryValue& hi)
101{
102 return MemoryValue::from<uint128_t>(static_cast<uint128_t>(lo.as<uint64_t>()) |
103 (static_cast<uint128_t>(hi.as<uint64_t>()) << 64));
104}
105
106} // namespace
107
117 const MemoryValue& b1,
118 const MemoryValue& a2,
119 const MemoryValue& b2)
120{
122 b1.get_tag() == MemoryTag::U64 && b2.get_tag() == MemoryTag::U64,
123 "SIMD-64 AND operands must have U64 tags");
124
125 const MemoryValue a = pack_lanes_64(a1, a2);
126 const MemoryValue b = pack_lanes_64(b1, b2);
127 const MemoryValue c = a & b;
128 const uint128_t res = static_cast<uint128_t>(c.as_ff());
129 events.emit({ .operation = BitwiseOperation::AND, .a = a, .b = b, .res = res, .simd_64 = true });
130 return { MemoryValue::from<uint64_t>(static_cast<uint64_t>(res)),
131 MemoryValue::from<uint64_t>(static_cast<uint64_t>(res >> 64)) };
132}
133
138 const MemoryValue& b1,
139 const MemoryValue& a2,
140 const MemoryValue& b2)
141{
143 b1.get_tag() == MemoryTag::U64 && b2.get_tag() == MemoryTag::U64,
144 "SIMD-64 XOR operands must have U64 tags");
145
146 const MemoryValue a = pack_lanes_64(a1, a2);
147 const MemoryValue b = pack_lanes_64(b1, b2);
148 const MemoryValue c = a ^ b;
149 const uint128_t res = static_cast<uint128_t>(c.as_ff());
150 events.emit({ .operation = BitwiseOperation::XOR, .a = a, .b = b, .res = res, .simd_64 = true });
151 return { MemoryValue::from<uint64_t>(static_cast<uint64_t>(res)),
152 MemoryValue::from<uint64_t>(static_cast<uint64_t>(res >> 64)) };
153}
154
155} // namespace bb::avm2::simulation
#define BB_ASSERT_DEBUG(expression,...)
Definition assert.hpp:55
ValueTag get_tag() const
std::pair< MemoryValue, MemoryValue > simd_xor_op_64(const MemoryValue &a1, const MemoryValue &b1, const MemoryValue &a2, const MemoryValue &b2) override
Two independent U64 XORs packed into one U128 sub-trace row (SIMD-64). See simd_and_op_64.
Definition bitwise.cpp:137
MemoryValue and_op(const MemoryValue &a, const MemoryValue &b) override
Perform bitwise AND on two tagged memory values.
Definition bitwise.cpp:21
std::pair< MemoryValue, MemoryValue > simd_and_op_64(const MemoryValue &a1, const MemoryValue &b1, const MemoryValue &a2, const MemoryValue &b2) override
Two independent U64 ANDs packed into one U128 sub-trace row (SIMD-64).
Definition bitwise.cpp:116
EventEmitterInterface< BitwiseEvent > & events
Definition bitwise.hpp:38
MemoryValue or_op(const MemoryValue &a, const MemoryValue &b) override
Perform bitwise OR on two tagged memory values.
Definition bitwise.cpp:50
MemoryValue xor_op(const MemoryValue &a, const MemoryValue &b) override
Perform bitwise XOR on two tagged memory values.
Definition bitwise.cpp:79
FF a
FF b
AVM range check gadget for witness generation.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:45