Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
solver.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <unordered_map>
4
5#include <cvc5/cvc5.h>
6
8
9namespace smt_solver {
10
27 uint64_t timeout;
28 uint32_t debug;
29
32 std::string ff_solver;
33
35};
36
38 /*produce_models=*/true,
39 /*timeout=*/0,
40 /*debug=*/0,
41 /*ff_elim_disjunctive_bit=*/false,
42 /*ff_bitsum=*/false,
43 /*ff_solver=*/"gb",
44 /*lookup_enabled=*/true
45};
46
48 /*produce_models=*/true,
49 /*timeout=*/0,
50 /*debug=*/0,
51 /*ff_elim_disjunctive_bit=*/false,
52 /*ff_bitsum=*/false,
53 /*ff_solver=*/"gb",
54 /*lookup_enabled=*/true
55};
56
58 /*produce_models=*/true, /*timeout=*/0, /*debug=*/0,
59 /*ff_elim_disjunctive_bit=*/true, /*ff_bitsum=*/true, /*ff_solver=*/"split",
60 /*lookup_enabled=*/true
61};
62
64 /*produce_models=*/true,
65 /*timeout=*/0,
66 /*debug=*/2,
67 /*ff_elim_disjunctive_bit=*/false,
68 /*ff_bitsum=*/false,
69 /*ff_solver=*/"gb",
70 /*lookup_enabled=*/true
71};
72
80class Solver {
81 public:
82 cvc5::TermManager term_manager;
83 cvc5::Solver solver;
84 cvc5::Sort ff_sort;
85 cvc5::Sort bv_sort;
86 std::string modulus; // modulus in base 10
87 bool res = false;
88 cvc5::Result cvc_result;
89 bool checked = false;
90 bool lookup_enabled = false;
91
92 explicit Solver(const std::string& modulus,
94 uint32_t base = 16,
95 uint32_t bvsize = 254)
97 {
98 this->ff_sort = term_manager.mkFiniteFieldSort(modulus, base);
99 this->modulus = ff_sort.getFiniteFieldSize();
100 this->bv_sort = term_manager.mkBitVectorSort(bvsize);
101 if (config.produce_models) {
102 solver.setOption("produce-models", "true");
103 }
104 if (config.timeout > 0) {
105 solver.setOption("tlimit-per", std::to_string(config.timeout));
106 }
107 if (config.debug >= 1) {
108 solver.setOption("verbosity", "5");
109 solver.setOption("stats", "true");
110 }
111 if (config.debug >= 2) {
112 solver.setOption("lang", "smt2");
113 solver.setOption("output", "inst");
114 solver.setOption("output", "learned-lits");
115 solver.setOption("output", "subs");
116 solver.setOption("output", "post-asserts");
117 solver.setOption("output", "trusted-proof-steps");
118 solver.setOption("output", "deep-restart");
119 solver.setOption("output", "timeout-core-benchmark");
120 solver.setOption("output", "unsat-core-benchmark");
121 solver.setOption("stats-all", "true");
122 solver.setOption("stats-internal", "true");
123 }
124
125 // Can be useful when split-gb is used as ff-solver.
126 // Cause bit constraints are part of the split-gb optimization
127 // and without them it will probably perform less efficient
128 // TODO(alex): test this `probably` after finishing the pr sequence
129 if (!config.ff_elim_disjunctive_bit) {
130 solver.setOption("ff-elim-disjunctive-bit", "false");
131 }
132 // split-gb is an updated version of gb ff-solver
133 // It basically SPLITS the polynomials in the system into subsets
134 // and computes a Groebner basis for each of them.
135 // According to the benchmarks, the new decision process in split-gb
136 // brings a significant boost in solver performance
137 if (!config.ff_solver.empty()) {
138 solver.setOption("ff-solver", config.ff_solver);
139 }
140
141 if (config.lookup_enabled) {
142 this->solver.setLogic("ALL");
143 this->solver.setOption("finite-model-find", "true");
144 this->solver.setOption("sets-exp", "true");
145 this->solver.setOption("arrays-exp", "true");
146 this->solver.setOption("arrays-optimize-linear", "true");
147 this->lookup_enabled = true;
148 }
149
150 solver.setOption("output", "incomplete");
151 }
152
153 Solver(const Solver& other) = delete;
154 Solver(Solver&& other) = delete;
155 Solver& operator=(const Solver& other) = delete;
156 Solver& operator=(Solver&& other) = delete;
157
158 void assertFormula(const cvc5::Term& term) const { this->solver.assertFormula(term); }
159
160 bool check();
161
162 [[nodiscard]] const char* getResult() const
163 {
164 if (!checked) {
165 return "No result, yet";
166 }
167 return res ? "SAT" : "UNSAT";
168 }
169
170 cvc5::Term get_symbolic_value(const cvc5::Term& term) const { return this->solver.getValue(term); };
171
172 std::string get(const cvc5::Term& term) const;
173 std::string operator[](const cvc5::Term& term) const { return this->get(term); };
174
177
178 void print_assertions();
179 std::string stringify_term(const cvc5::Term& term, bool parenthesis = false);
180
181 std::string get_array_name(const cvc5::Term& term);
182 // TODO(alex): Keep track of terms names instead of depth. Should cut the recursion
183 std::pair<std::string, size_t> print_array_trace(const cvc5::Term& term, bool is_head = true);
184 std::unordered_map<std::string, size_t> cached_array_traces;
185
186 std::string get_set_name(const cvc5::Term& term);
187 // TODO(alex): Keep track of terms names instead of depth. Should cut the recursion
188 std::pair<std::string, size_t> print_set_trace(const cvc5::Term& term, bool is_head = true);
189 std::unordered_map<std::string, size_t> cached_set_traces;
190
191 ~Solver() = default;
192};
193
194}; // namespace smt_solver
Class for the solver.
Definition solver.hpp:80
std::pair< std::string, size_t > print_set_trace(const cvc5::Term &term, bool is_head=true)
print the trace of SET insertions up to previously printed ones
Definition solver.cpp:168
cvc5::Term get_symbolic_value(const cvc5::Term &term) const
Definition solver.hpp:170
void assertFormula(const cvc5::Term &term) const
Definition solver.hpp:158
const char * getResult() const
Definition solver.hpp:162
Solver(Solver &&other)=delete
std::pair< std::string, size_t > print_array_trace(const cvc5::Term &term, bool is_head=true)
print the trace of array assigments up to previously printed ones
Definition solver.cpp:116
std::string get(const cvc5::Term &term) const
Returns.
Definition solver.cpp:32
std::string modulus
Definition solver.hpp:86
cvc5::Solver solver
Definition solver.hpp:83
void print_assertions()
Definition solver.cpp:421
std::unordered_map< std::string, std::string > model(std::unordered_map< std::string, cvc5::Term > &terms) const
Definition solver.cpp:80
std::string get_array_name(const cvc5::Term &term)
recover the array name from the nested assigments
Definition solver.cpp:147
Solver(const std::string &modulus, const SolverConfiguration &config=default_solver_config, uint32_t base=16, uint32_t bvsize=254)
Definition solver.hpp:92
std::unordered_map< std::string, size_t > cached_array_traces
Definition solver.hpp:184
Solver(const Solver &other)=delete
cvc5::Sort bv_sort
Definition solver.hpp:85
std::unordered_map< std::string, size_t > cached_set_traces
Definition solver.hpp:189
cvc5::TermManager term_manager
Definition solver.hpp:82
Solver & operator=(const Solver &other)=delete
std::string get_set_name(const cvc5::Term &term)
recover the set name from the nested assigments
Definition solver.cpp:210
std::string operator[](const cvc5::Term &term) const
Definition solver.hpp:173
Solver & operator=(Solver &&other)=delete
cvc5::Result cvc_result
Definition solver.hpp:88
cvc5::Sort ff_sort
Definition solver.hpp:84
std::string stringify_term(const cvc5::Term &term, bool parenthesis=false)
Definition solver.cpp:235
const SolverConfiguration default_solver_config
Definition solver.hpp:37
const SolverConfiguration split_gb_solver_config
Definition solver.hpp:57
const SolverConfiguration debug_solver_config
Definition solver.hpp:63
const SolverConfiguration ultra_solver_config
Definition solver.hpp:47
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
Solver configuration.
Definition solver.hpp:25