Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
term.hpp
Go to the documentation of this file.
1#pragma once
3
4namespace smt_terms {
5using namespace smt_solver;
6
16std::ostream& operator<<(std::ostream& os, TermType type);
17
18enum class OpType : int32_t {
19 ADD,
20 SUB,
21 MUL,
22 DIV,
23 NEG,
24 XOR,
25 AND,
26 OR,
27 GT,
28 GE,
29 LT,
30 LE,
31 MOD,
32 RSH,
33 LSH,
34 ROTR,
35 ROTL,
36 NOT,
37 EXTRACT,
40};
41
49 { { OpType::ADD, cvc5::Kind::FINITE_FIELD_ADD },
50 { OpType::MUL, cvc5::Kind::FINITE_FIELD_MULT },
51 { OpType::NEG, cvc5::Kind::FINITE_FIELD_NEG },
52 // Just a placeholder that marks it supports division
53 { OpType::DIV, cvc5::Kind::FINITE_FIELD_MULT } } },
55 {
56
57 { OpType::ADD, cvc5::Kind::ADD },
58 { OpType::SUB, cvc5::Kind::SUB },
59 { OpType::MUL, cvc5::Kind::MULT },
60 { OpType::NEG, cvc5::Kind::NEG },
61 { OpType::GT, cvc5::Kind::GT },
62 { OpType::GE, cvc5::Kind::GEQ },
63 { OpType::LT, cvc5::Kind::LT },
64 { OpType::LE, cvc5::Kind::LEQ },
65 { OpType::MOD, cvc5::Kind::INTS_MODULUS },
66 // Just a placeholder that marks it supports division
67 { OpType::DIV, cvc5::Kind::MULT } } },
69 { { OpType::ADD, cvc5::Kind::ADD },
70 { OpType::SUB, cvc5::Kind::SUB },
71 { OpType::MUL, cvc5::Kind::MULT },
72 { OpType::NEG, cvc5::Kind::NEG },
73 { OpType::GT, cvc5::Kind::GT },
74 { OpType::GE, cvc5::Kind::GEQ },
75 { OpType::LT, cvc5::Kind::LT },
76 { OpType::LE, cvc5::Kind::LEQ },
77 { OpType::MOD, cvc5::Kind::INTS_MODULUS },
78 { OpType::DIV, cvc5::Kind::INTS_DIVISION } } },
80 {
81
82 { OpType::ADD, cvc5::Kind::BITVECTOR_ADD },
83 { OpType::SUB, cvc5::Kind::BITVECTOR_SUB },
84 { OpType::MUL, cvc5::Kind::BITVECTOR_MULT },
85 { OpType::NEG, cvc5::Kind::BITVECTOR_NEG },
86 { OpType::GT, cvc5::Kind::BITVECTOR_UGT },
87 { OpType::GE, cvc5::Kind::BITVECTOR_UGE },
88 { OpType::LT, cvc5::Kind::BITVECTOR_ULT },
89 { OpType::LE, cvc5::Kind::BITVECTOR_ULE },
90 { OpType::XOR, cvc5::Kind::BITVECTOR_XOR },
91 { OpType::AND, cvc5::Kind::BITVECTOR_AND },
92 { OpType::OR, cvc5::Kind::BITVECTOR_OR },
93 { OpType::RSH, cvc5::Kind::BITVECTOR_LSHR },
94 { OpType::LSH, cvc5::Kind::BITVECTOR_SHL },
95 { OpType::ROTL, cvc5::Kind::BITVECTOR_ROTATE_LEFT },
96 { OpType::ROTR, cvc5::Kind::BITVECTOR_ROTATE_RIGHT },
97 { OpType::MOD, cvc5::Kind::BITVECTOR_UREM },
98 { OpType::DIV, cvc5::Kind::BITVECTOR_UDIV },
99 { OpType::NOT, cvc5::Kind::BITVECTOR_NOT },
100 { OpType::EXTRACT, cvc5::Kind::BITVECTOR_EXTRACT },
101 { OpType::BITVEC_PAD, cvc5::Kind::BITVECTOR_ZERO_EXTEND },
102 } }
103};
104
114class STerm {
115 private:
116 STerm mod() const;
117 STerm normalize() const;
118
119 public:
121 cvc5::Term term;
122
124 std::unordered_map<OpType, cvc5::Kind> operations;
125
127 : solver(nullptr)
128 , term(cvc5::Term())
129 , type(TermType::FFTerm) {};
130
131 STerm(const cvc5::Term& term, Solver* s, TermType type)
132 : solver(s)
133 , term(term)
134 , type(type)
136
137 explicit STerm(
138 const std::string& t, Solver* slv, bool isconst = false, uint32_t base = 16, TermType type = TermType::FFTerm);
139
140 STerm(const STerm& other) = default;
141 STerm(STerm&& other) = default;
142
143 static STerm Var(const std::string& name, Solver* slv, TermType type = TermType::FFTerm);
144 static STerm Const(const std::string& val, Solver* slv, uint32_t base = 16, TermType type = TermType::FFTerm);
145 static STerm Const(const bb::fr& val, Solver* slv, TermType type = TermType::FFTerm);
146
148
149 STerm& operator=(const STerm& right) = default;
150 STerm& operator=(STerm&& right) = default;
151
152 STerm operator+(const STerm& other) const;
153 void operator+=(const STerm& other);
154 STerm operator-(const STerm& other) const;
155 void operator-=(const STerm& other);
156 STerm operator-() const;
157
158 STerm operator*(const STerm& other) const;
159 void operator*=(const STerm& other);
160 STerm operator/(const STerm& other) const;
161 void operator/=(const STerm& other);
162 // NOTE: this is not the same as .mod(). The modulus here can be arbitrary
163 STerm operator%(const STerm& other) const;
164
165 void operator<(const STerm& other) const;
166 void operator<=(const STerm& other) const;
167 void operator>(const STerm& other) const;
168 void operator>=(const STerm& other) const;
169
170 void operator==(const STerm& other) const;
171 void operator!=(const STerm& other) const;
172
173 STerm operator^(const STerm& other) const;
174 void operator^=(const STerm& other);
175 STerm operator&(const STerm& other) const;
176 void operator&=(const STerm& other);
177 STerm operator|(const STerm& other) const;
178 void operator|=(const STerm& other);
179 STerm operator~() const;
180
185 STerm truncate(const uint32_t& to_size);
190 STerm extract_bit(const uint32_t& bit_index);
191
192 operator std::string() const { return this->solver->stringify_term(term); };
193 operator cvc5::Term() const { return term; };
194
195 ~STerm() = default;
196
197 friend std::ostream& operator<<(std::ostream& out, const STerm& term)
198 {
199 return out << static_cast<std::string>(term);
200 };
201
202 static STerm batch_add(const std::vector<STerm>& children)
203 {
204 if (children.size() == 0) {
205 throw std::invalid_argument("Can't use batch_add on empty vector");
206 }
207 Solver* slv = children[0].solver;
208 std::vector<cvc5::Term> terms(children.begin(), children.end());
209 cvc5::Term res = slv->term_manager.mkTerm(children[0].operations.at(OpType::ADD), terms);
210 return { res, slv, children[0].type };
211 }
212
213 static STerm batch_mul(const std::vector<STerm>& children)
214 {
215 if (children.size() == 0) {
216 throw std::invalid_argument("Can't use batch_mul on empty vector");
217 }
218 Solver* slv = children[0].solver;
219 std::vector<cvc5::Term> terms(children.begin(), children.end());
220 cvc5::Term res = slv->term_manager.mkTerm(children[0].operations.at(OpType::MUL), terms);
221 return { res, slv, children[0].type };
222 }
223
224 // arithmetic compatibility with Fr
225
226 STerm operator+(const bb::fr& other) const { return *this + STerm(other, this->solver, this->type); }
227 void operator+=(const bb::fr& other) { *this += STerm(other, this->solver, this->type); }
228 STerm operator-(const bb::fr& other) const { return *this - STerm(other, this->solver, this->type); }
229 void operator-=(const bb::fr& other) { *this -= STerm(other, this->solver, this->type); }
230
231 STerm operator*(const bb::fr& other) const { return *this * STerm(other, this->solver, this->type); }
232 void operator*=(const bb::fr& other) { *this *= STerm(other, this->solver, this->type); }
233 STerm operator/(const bb::fr& other) const { return *this * STerm(other.invert(), this->solver, this->type); }
234 void operator/=(const bb::fr& other) { *this *= STerm(other.invert(), this->solver, this->type); }
235 // NOTE: this is not the same as .mod(). The modulus here can be arbitrary
236 STerm operator%(const bb::fr& other) const { return *this % STerm(other, this->solver, this->type); }
237
238 void operator<(const bb::fr& other) const { *this < STerm(other, this->solver, this->type); };
239 void operator<=(const bb::fr& other) const { *this <= STerm(other, this->solver, this->type); };
240 void operator>(const bb::fr& other) const { *this > STerm(other, this->solver, this->type); };
241 void operator>=(const bb::fr& other) const { *this >= STerm(other, this->solver, this->type); };
242
243 void operator==(const bb::fr& other) const { *this == STerm(other, this->solver, this->type); };
244 void operator!=(const bb::fr& other) const { *this != STerm(other, this->solver, this->type); };
245
246 STerm operator^(const bb::fr& other) const { return *this ^ STerm(other, this->solver, this->type); };
247 void operator^=(const bb::fr& other) { *this ^= STerm(other, this->solver, this->type); };
248 STerm operator&(const bb::fr& other) const { return *this & STerm(other, this->solver, this->type); };
249 void operator&=(const bb::fr& other) { *this &= STerm(other, this->solver, this->type); };
250 STerm operator|(const bb::fr& other) const { return *this | STerm(other, this->solver, this->type); };
251 void operator|=(const bb::fr& other) { *this |= STerm(other, this->solver, this->type); };
252
253 STerm operator<<(const uint32_t& n) const;
254 void operator<<=(const uint32_t& n);
255 STerm operator>>(const uint32_t& n) const;
256 void operator>>=(const uint32_t& n);
257
258 STerm rotr(const uint32_t& n) const;
259 STerm rotl(const uint32_t& n) const;
260
261 friend class Bool;
262};
263
264STerm operator+(const bb::fr& lhs, const STerm& rhs);
265STerm operator-(const bb::fr& lhs, const STerm& rhs);
266STerm operator*(const bb::fr& lhs, const STerm& rhs);
267STerm operator/(const bb::fr& lhs, const STerm& rhs);
268void operator==(const bb::fr& lhs, const STerm& rhs);
269void operator!=(const bb::fr& lhs, const STerm& rhs);
270STerm operator^(const bb::fr& lhs, const STerm& rhs);
271STerm operator&(const bb::fr& lhs, const STerm& rhs);
272STerm operator|(const bb::fr& lhs, const STerm& rhs);
273
274STerm FFVar(const std::string& name, Solver* slv);
275STerm FFConst(const std::string& val, Solver* slv, uint32_t base = 16);
276STerm FFConst(const bb::fr& val, Solver* slv);
277
278STerm FFIVar(const std::string& name, Solver* slv);
279STerm FFIConst(const std::string& val, Solver* slv, uint32_t base = 16);
280STerm FFIConst(const bb::fr& val, Solver* slv);
281
282STerm IVar(const std::string& name, Solver* slv);
283STerm IConst(const std::string& val, Solver* slv, uint32_t base = 16);
284STerm IConst(const bb::fr& val, Solver* slv);
285
286STerm BVVar(const std::string& name, Solver* slv);
287STerm BVConst(const std::string& val, Solver* slv, uint32_t base = 16);
288STerm BVConst(const bb::fr& val, Solver* slv);
289
290} // namespace smt_terms
Class for the solver.
Definition solver.hpp:80
cvc5::Solver solver
Definition solver.hpp:83
cvc5::TermManager term_manager
Definition solver.hpp:82
std::string stringify_term(const cvc5::Term &term, bool parenthesis=false)
Definition solver.cpp:235
Bool element class.
Definition bool.hpp:14
Symbolic term element class.
Definition term.hpp:114
~STerm()=default
void operator<(const STerm &other) const
Definition term.cpp:264
void operator<<=(const uint32_t &n)
Definition term.cpp:408
operator cvc5::Term() const
Definition term.hpp:193
void operator-=(const bb::fr &other)
Definition term.hpp:229
STerm operator&(const bb::fr &other) const
Definition term.hpp:248
void operator!=(const bb::fr &other) const
Definition term.hpp:244
STerm operator*(const STerm &other) const
Definition term.cpp:176
STerm rotl(const uint32_t &n) const
Definition term.cpp:450
friend std::ostream & operator<<(std::ostream &out, const STerm &term)
Definition term.hpp:197
STerm operator+(const bb::fr &other) const
Definition term.hpp:226
TermType type
Definition term.hpp:123
STerm operator*(const bb::fr &other) const
Definition term.hpp:231
void operator^=(const bb::fr &other)
Definition term.hpp:247
void operator/=(const bb::fr &other)
Definition term.hpp:234
STerm operator%(const STerm &other) const
Definition term.cpp:320
void operator+=(const STerm &other)
Definition term.cpp:152
void operator/=(const STerm &other)
Definition term.cpp:217
void operator==(const STerm &other) const
Definition term.cpp:241
STerm rotr(const uint32_t &n) const
Definition term.cpp:439
void operator<=(const STerm &other) const
Definition term.cpp:278
STerm operator/(const bb::fr &other) const
Definition term.hpp:233
std::unordered_map< OpType, cvc5::Kind > operations
Definition term.hpp:124
STerm operator/(const STerm &other) const
Division operation.
Definition term.cpp:197
void operator&=(const bb::fr &other)
Definition term.hpp:249
STerm operator-() const
Definition term.cpp:170
STerm & operator=(STerm &&right)=default
void operator==(const bb::fr &other) const
Definition term.hpp:243
void operator>(const STerm &other) const
Definition term.cpp:292
STerm(const STerm &other)=default
STerm(bb::fr value, Solver *s, TermType type=TermType::FFTerm)
Definition term.hpp:147
void operator^=(const STerm &other)
Definition term.cpp:340
static STerm Var(const std::string &name, Solver *slv, TermType type=TermType::FFTerm)
Definition term.cpp:14
STerm operator^(const STerm &other) const
Definition term.cpp:330
STerm operator-(const bb::fr &other) const
Definition term.hpp:228
void operator*=(const STerm &other)
Definition term.cpp:182
STerm mod() const
Reduce the term modulo circuit prime.
Definition term.cpp:119
void operator<=(const bb::fr &other) const
Definition term.hpp:239
STerm(const cvc5::Term &term, Solver *s, TermType type)
Definition term.hpp:131
cvc5::Term term
Definition term.hpp:121
STerm normalize() const
Reduce the integer symbolic term modulo circuit prime if needed.
Definition term.cpp:140
void operator|=(const bb::fr &other)
Definition term.hpp:251
void operator&=(const STerm &other)
Definition term.cpp:359
STerm & operator=(const STerm &right)=default
Solver * solver
Definition term.hpp:120
void operator>>=(const uint32_t &n)
Definition term.cpp:429
STerm operator|(const bb::fr &other) const
Definition term.hpp:250
void operator+=(const bb::fr &other)
Definition term.hpp:227
void operator>=(const bb::fr &other) const
Definition term.hpp:241
STerm operator&(const STerm &other) const
Definition term.cpp:349
void operator!=(const STerm &other) const
Definition term.cpp:254
STerm operator|(const STerm &other) const
Definition term.cpp:368
void operator*=(const bb::fr &other)
Definition term.hpp:232
void operator<(const bb::fr &other) const
Definition term.hpp:238
STerm operator~() const
Definition term.cpp:387
void operator>=(const STerm &other) const
Definition term.cpp:306
STerm(STerm &&other)=default
void operator|=(const STerm &other)
Definition term.cpp:378
STerm operator>>(const uint32_t &n) const
Definition term.cpp:418
STerm operator%(const bb::fr &other) const
Definition term.hpp:236
static STerm Const(const std::string &val, Solver *slv, uint32_t base=16, TermType type=TermType::FFTerm)
Definition term.cpp:29
STerm operator^(const bb::fr &other) const
Definition term.hpp:246
void operator-=(const STerm &other)
Definition term.cpp:164
void operator>(const bb::fr &other) const
Definition term.hpp:240
static STerm batch_add(const std::vector< STerm > &children)
Definition term.hpp:202
STerm extract_bit(const uint32_t &bit_index)
Returns ith bit of variable.
Definition term.cpp:476
STerm operator+(const STerm &other) const
Definition term.cpp:146
static STerm batch_mul(const std::vector< STerm > &children)
Definition term.hpp:213
STerm truncate(const uint32_t &to_size)
Returns last to_size bits of variable.
Definition term.cpp:461
sym Tuple class
symbolic Array class
symbolic Set class
STerm IVar(const std::string &name, Solver *slv)
Definition term.cpp:600
STerm operator+(const bb::fr &lhs, const STerm &rhs)
Definition term.cpp:491
STerm FFVar(const std::string &name, Solver *slv)
Definition term.cpp:570
STerm IConst(const std::string &val, Solver *slv, uint32_t base)
Definition term.cpp:605
STerm BVVar(const std::string &name, Solver *slv)
Definition term.cpp:615
STerm operator&(const bb::fr &lhs, const STerm &rhs)
Definition term.cpp:511
STerm FFConst(const std::string &val, Solver *slv, uint32_t base)
Definition term.cpp:575
const std::unordered_map< TermType, std::unordered_map< OpType, cvc5::Kind > > typed_operations
precomputed map that contains allowed operations for each of three symbolic types
Definition term.hpp:47
STerm operator|(const bb::fr &lhs, const STerm &rhs)
Definition term.cpp:516
void operator!=(const bb::fr &lhs, const STerm &rhs)
Definition term.cpp:531
STerm operator*(const bb::fr &lhs, const STerm &rhs)
Definition term.cpp:501
STerm FFIVar(const std::string &name, Solver *slv)
Definition term.cpp:585
STerm operator^(const bb::fr &lhs, const STerm &rhs)
Definition term.cpp:506
STerm BVConst(const std::string &val, Solver *slv, uint32_t base)
Definition term.cpp:620
TermType
Allows to define three types of symbolic terms STerm - Symbolic Variables acting like a Finte Field e...
Definition term.hpp:15
STerm operator/(const bb::fr &lhs, const STerm &rhs)
Definition term.cpp:521
STerm operator-(const bb::fr &lhs, const STerm &rhs)
Definition term.cpp:496
STerm FFIConst(const std::string &val, Solver *slv, uint32_t base)
Definition term.cpp:590
std::ostream & operator<<(std::ostream &os, const TermType type)
Definition term.cpp:536
void operator==(const bb::fr &lhs, const STerm &rhs)
Definition term.cpp:526
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
constexpr field invert() const noexcept