Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
pure_alu.cpp
Go to the documentation of this file.
2
3#include <cstdint>
4#include <stdexcept>
5
8
9namespace bb::avm2::simulation {
10
12{
13 try {
14 return a + b; // This will throw if the tags do not match.
15 } catch (const TagMismatchException& e) {
16 throw AluException("ADD, " + std::string(e.what()));
17 }
18}
19
21{
22 try {
23 return a - b; // This will throw if the tags do not match.
24 } catch (const TagMismatchException& e) {
25 throw AluException("SUB, " + std::string(e.what()));
26 }
27}
28
30{
31 try {
32 return a * b; // This will throw if the tags do not match.
33 } catch (const TagMismatchException& e) {
34 throw AluException("MUL, " + std::string(e.what()));
35 }
36}
37
39{
40 try {
41 if (a.get_tag() == MemoryTag::FF) {
42 // DIV on a field is not a valid operation
43 throw TagMismatchException("Cannot perform integer division on a field element");
44 }
45 return a / b; // This will throw if the tags do not match or if we divide by 0.
46 } catch (const TagMismatchException& e) {
47 throw AluException("DIV, " + std::string(e.what()));
48 } catch (const DivisionByZero& e) {
49 throw AluException("DIV, " + std::string(e.what()));
50 }
51}
52
54{
55 try {
56 if (a.get_tag() != MemoryTag::FF) {
57 throw TagMismatchException("Cannot perform field division on an integer");
58 }
59 return a / b; // This will throw if the tags do not match or if we divide by 0.
60 } catch (const TagMismatchException& e) {
61 throw AluException("FDIV, " + std::string(e.what()));
62 } catch (const DivisionByZero& e) {
63 throw AluException("FDIV, " + std::string(e.what()));
64 }
65}
66
68{
69 // Brillig semantic enforces that tags match for EQ.
70 if (a.get_tag() != b.get_tag()) {
71 throw AluException("EQ, Tag mismatch between operands.");
72 }
73 return MemoryValue::from<uint1_t>(a == b ? 1 : 0);
74}
75
77{
78 // Brillig semantic enforces that tags match for LT.
79 if (a.get_tag() != b.get_tag()) {
80 throw AluException("LT, Tag mismatch between operands.");
81 }
82 // Use the built-in comparison operators
83 bool res = a < b;
84 return MemoryValue::from<uint1_t>(res);
85}
86
88{
89 // Brillig semantic enforces that tags match for LTE.
90 if (a.get_tag() != b.get_tag()) {
91 throw AluException("LTE, Tag mismatch between operands.");
92 }
93 // Use the built-in comparison operators
94 bool res = a <= b;
95 return MemoryValue::from<uint1_t>(res);
96}
97
99{
100 try {
101 return ~a; // Throws if the tag is not compatible with NOT operation (i.e. it is an FF type).
102 } catch (const InvalidOperationTag& e) {
103 throw AluException("NOT, " + std::string(e.what()));
104 }
105}
106
108{
109 try {
110 return a << b; // This will throw if the tags do not match or are FF.
111 } catch (const TagMismatchException& e) {
112 throw AluException("SHL, " + std::string(e.what()));
113 } catch (const InvalidOperationTag& e) {
114 throw AluException("SHL, " + std::string(e.what()));
115 }
116}
117
119{
120 try {
121 return a >> b; // This will throw if the tags do not match or are FF.
122 } catch (const TagMismatchException& e) {
123 throw AluException("SHR, " + std::string(e.what()));
124 } catch (const InvalidOperationTag& e) {
125 throw AluException("SHR, " + std::string(e.what()));
126 }
127}
128
133
134} // namespace bb::avm2::simulation
MemoryTag dst_tag
static TaggedValue from_tag_truncating(ValueTag tag, FF value)
MemoryValue lt(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:76
MemoryValue mul(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:29
MemoryValue lte(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:87
MemoryValue fdiv(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:53
MemoryValue shr(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:118
MemoryValue truncate(const FF &a, MemoryTag dst_tag) override
Definition pure_alu.cpp:129
MemoryValue div(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:38
MemoryValue eq(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:67
MemoryValue add(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:11
MemoryValue sub(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:20
MemoryValue shl(const MemoryValue &a, const MemoryValue &b) override
Definition pure_alu.cpp:107
MemoryValue op_not(const MemoryValue &a) override
Definition pure_alu.cpp:98
FF a
FF b
AvmFlavorSettings::FF FF
Definition field.hpp:10