Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: creating an SMT verification module #1932

Merged
merged 32 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5873b89
smt-verification module migration
Sarkoxed Sep 1, 2023
2415169
SMT-verfication preset added
Sarkoxed Sep 1, 2023
aae5b34
endif
Sarkoxed Sep 1, 2023
0d177fa
msgpack issue fix + reorder includes
Sarkoxed Sep 1, 2023
4673527
Merge branch 'master' into as/smt-verification
Rumata888 Sep 4, 2023
c07584b
default -> clang16
Sarkoxed Sep 5, 2023
d0502aa
MSGpack fix + todos
Sarkoxed Sep 6, 2023
095754b
Clean up
Sarkoxed Sep 6, 2023
a54de13
Modulus inside the solver
Sarkoxed Sep 6, 2023
43e9b9e
No isconst member + fixed the string repr
Sarkoxed Sep 6, 2023
f480356
New Integer Modulo number backend
Sarkoxed Sep 6, 2023
abe7fb7
Just testing
Sarkoxed Sep 6, 2023
518235e
Merge branch 'master' into as/smt-verification
Sarkoxed Sep 6, 2023
bca6189
back
Sarkoxed Sep 6, 2023
aa72f77
Added configuration structure and modulus translation to base10 string
Sarkoxed Sep 7, 2023
18b1c3a
No zero division + Var and Const now are the static members of FFTerm…
Sarkoxed Sep 7, 2023
82e4a52
New integer mod backend
Sarkoxed Sep 7, 2023
ba2e49d
Circuit Class now a template. Added binary gate realaxation.
Sarkoxed Sep 7, 2023
0dfbc1c
Syntax fixes
Sarkoxed Sep 7, 2023
3827a56
unique witness example
Sarkoxed Sep 7, 2023
950dfbb
the only module should remain
Sarkoxed Sep 8, 2023
6ac6993
Template function fixes
Sarkoxed Sep 8, 2023
2e944da
Addition mistake fixed
Sarkoxed Sep 8, 2023
d0df1ca
milliseconds uint64
Sarkoxed Sep 8, 2023
2991fe6
Minor changes
Sarkoxed Sep 8, 2023
4c8832a
Merge branch 'master' into as/smt-verification
Sarkoxed Sep 8, 2023
dd44a10
README update
Sarkoxed Sep 8, 2023
1461d60
Fixing naming and documentation
Sarkoxed Sep 11, 2023
c1b83f0
Fixing documentation
Sarkoxed Sep 11, 2023
f85aa2c
More TODOs
Sarkoxed Sep 11, 2023
2a09362
Merge branch 'master' into as/smt-verification
Sarkoxed Sep 11, 2023
0fd9b7e
Merge branch 'master' into as/smt-verification
Rumata888 Sep 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions circuits/cpp/barretenberg/cpp/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@
"FUZZING": "ON"
}
},
{
"name": "smt-verification",
"displayName": "Build with smt verificaiton",
"description": "Build default preset but with smt library included",
"inherits": "clang16",
"binaryDir": "build-smt",
"cacheVariables": {
"SMT": "ON"
}
},
{
"name": "coverage",
"displayName": "Build with coverage",
Expand Down Expand Up @@ -190,6 +200,11 @@
"inherits": "default",
"configurePreset": "fuzzing"
},
{
"name": "smt-verification",
"inherits": "clang16",
"configurePreset": "smt-verification"
},
{
"name": "coverage",
"inherits": "default",
Expand Down Expand Up @@ -258,6 +273,11 @@
"inherits": "default",
"configurePreset": "fuzzing"
},
{
"name": "smt-verification",
"inherits": "clang16",
"configurePreset": "smt-verification"
},
{
"name": "coverage",
"inherits": "default",
Expand Down
7 changes: 7 additions & 0 deletions circuits/cpp/barretenberg/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/msgpack-c/include)


# I feel this should be limited to ecc, however it's currently used in headers that go across libraries,
# and there currently isn't an easy way to inherit the DDISABLE_SHENANIGANS parameter.
if(DISABLE_ASM)
Expand Down Expand Up @@ -69,6 +70,12 @@ add_subdirectory(barretenberg/wasi)
add_subdirectory(barretenberg/grumpkin_srs_gen)
add_subdirectory(barretenberg/bb)


if(SMT)
include_directories(${CMAKE_CURRENT_SOURCE_DIR} $ENV{HOME}/cvc5/tmp-lib/include)
add_subdirectory(barretenberg/smt_verification)
endif()

if(BENCHMARKS)
add_subdirectory(barretenberg/benchmark)
endif()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/proof_system/arithmetization/arithmetization.hpp"
#include "barretenberg/proof_system/arithmetization/gate_data.hpp"
#include "barretenberg/serialize/cbind.hpp"
#include <utility>

#include <unordered_map>

namespace proof_system {
static constexpr uint32_t DUMMY_TAG = 0;

Expand All @@ -26,6 +29,8 @@ template <typename Arithmetization> class CircuitBuilderBase {

std::vector<uint32_t> public_inputs;
std::vector<FF> variables;
std::unordered_map<uint32_t, std::string> variable_names;

// index of next variable in equivalence class (=REAL_VARIABLE if you're last)
std::vector<uint32_t> next_var_index;
// index of previous variable in equivalence class (=FIRST if you're in a cycle alone)
Expand Down Expand Up @@ -57,6 +62,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
: selector_names_(std::move(selector_names))
{
variables.reserve(size_hint * 3);
variable_names.reserve(size_hint * 3);
next_var_index.reserve(size_hint * 3);
prev_var_index.reserve(size_hint * 3);
real_variable_index.reserve(size_hint * 3);
Expand Down Expand Up @@ -187,6 +193,99 @@ template <typename Arithmetization> class CircuitBuilderBase {
real_variable_tags.emplace_back(DUMMY_TAG);
return index;
}

/**
* Assign a name to a variable(equivalence class). Should be one name per equivalence class.
*
* @param index Index of the variable you want to name.
* @param name Name of the variable.
*
*/
virtual void set_variable_name(uint32_t index, const std::string& name)
{
ASSERT(variables.size() > index);
uint32_t first_idx = get_first_variable_in_class(index);

if (variable_names.contains(first_idx)) {
failure("Attempted to assign a name to a variable that already has a name");
return;
}
variable_names.insert({ first_idx, name });
}

/**
* After assert_equal() merge two class names if present.
* Preserves the first name in class.
*
* @param index Index of the variable you have previously named and used in assert_equal.
*
*/
virtual void update_variable_names(uint32_t index)
{
uint32_t first_idx = get_first_variable_in_class(index);

uint32_t cur_idx = next_var_index[first_idx];
while (cur_idx != REAL_VARIABLE && !variable_names.contains(cur_idx)) {
cur_idx = next_var_index[cur_idx];
}

if (variable_names.contains(first_idx)) {
if (cur_idx != REAL_VARIABLE) {
variable_names.extract(cur_idx);
}
return;
}

if (cur_idx != REAL_VARIABLE) {
std::string var_name = variable_names.find(cur_idx)->second;
variable_names.erase(cur_idx);
variable_names.insert({ first_idx, var_name });
return;
}
failure("No previously assigned names found");
}

/**
* After finishing the circuit can be called for automatic merging
* all existing collisions.
*
*/
virtual void finalize_variable_names()
{
std::vector<uint32_t> keys;
std::vector<uint32_t> firsts;

for (auto& tup : variable_names) {
keys.push_back(tup.first);
firsts.push_back(get_first_variable_in_class(tup.first));
}

for (size_t i = 0; i < keys.size() - 1; i++) {
for (size_t j = i + 1; j < keys.size(); i++) {
uint32_t first_idx_a = firsts[i];
uint32_t first_idx_b = firsts[j];
if (first_idx_a == first_idx_b) {
std::string substr1 = variable_names[keys[i]];
std::string substr2 = variable_names[keys[j]];
failure("Variables from the same equivalence class have separate names: " + substr2 + ", " +
substr2);
update_variable_names(first_idx_b);
}
}
}
}

/**
* Export the existing circuit as msgpack compatible buffer.
*
* @return msgpack compatible buffer
*/
virtual msgpack::sbuffer export_circuit()
{
info("not implemented");
return { 0 };
};

/**
* Add a public variable to variables
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <unordered_map>
#include <unordered_set>

#include "barretenberg/serialize/cbind.hpp"
#include "barretenberg/serialize/msgpack.hpp"

using namespace barretenberg;

namespace proof_system {
Expand Down Expand Up @@ -507,6 +510,56 @@ template <typename FF> bool StandardCircuitBuilder_<FF>::check_circuit()
}
return true;
}

/**
* Export the existing circuit as msgpack compatible buffer.
*
* @return msgpack compatible buffer
*/
template <typename FF> msgpack::sbuffer StandardCircuitBuilder_<FF>::export_circuit()
{
using base = CircuitBuilderBase<arithmetization::Standard<FF>>;
CircuitSchema cir;

uint64_t modulus[4] = {FF::Params::modulus_0, FF::Params::modulus_1, FF::Params::modulus_2, FF::Params::modulus_3};
std::stringstream buf;
buf << std::hex << std::setfill('0')
<< std::setw(16) << modulus[3]
<< std::setw(16) << modulus[2]
<< std::setw(16) << modulus[1]
<< std::setw(16) << modulus[0];

cir.modulus = buf.str();

for (uint32_t i = 0; i < this->get_num_public_inputs(); i++) {
cir.public_inps.push_back(this->real_variable_index[this->public_inputs[i]]);
}

for (auto& tup : base::variable_names) {
cir.vars_of_interest.insert({ this->real_variable_index[tup.first], tup.second });
}

for (auto var : this->variables) {
cir.variables.push_back(var);
}

for (size_t i = 0; i < this->num_gates; i++) {
std::vector<FF> tmp_sel = { q_m[i], q_1[i], q_2[i], q_3[i], q_c[i] };
std::vector<uint32_t> tmp_w = {
this->real_variable_index[w_l[i]],
this->real_variable_index[w_r[i]],
this->real_variable_index[w_o[i]],
};
cir.selectors.push_back(tmp_sel);
cir.wires.push_back(tmp_w);
}

msgpack::sbuffer buffer;
msgpack::pack(buffer, cir);
return buffer;
}

template class StandardCircuitBuilder_<barretenberg::fr>;
template class StandardCircuitBuilder_<grumpkin::fr>;

} // namespace proof_system
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ template <typename FF> class StandardCircuitBuilder_ : public CircuitBuilderBase
size_t get_num_constant_gates() const override { return 0; }

bool check_circuit();

msgpack::sbuffer export_circuit() override;

private:
struct CircuitSchema {
std::string modulus;
std::vector<uint32_t> public_inps;
std::unordered_map<uint32_t, std::string> vars_of_interest;
std::vector<FF> variables;
std::vector<std::vector<FF>> selectors;
std::vector<std::vector<uint32_t>> wires;
MSGPACK_FIELDS(modulus, public_inps, vars_of_interest, variables, selectors, wires);
} circuit_schema;
};

extern template class StandardCircuitBuilder_<barretenberg::fr>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#barretenberg_module(smt_verification common ${CMAKE_SOURCE_DIR}/src/cvc5/tmp-lib/lib/libcvc5.so.1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pleas remove this commented out line

barretenberg_module(smt_verification common proof_system stdlib_primitives $ENV{HOME}/cvc5/tmp-lib/lib/libcvc5.so.1)
Loading