-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Bindings for SSP, USCS and Test for SSP (#49)
* Fix bug while generating random_element * Add Binding for Square Arithmetic Program * Add Binding for USCS * Add USCS examples * Add test for SSP
- Loading branch information
1 parent
d2c873b
commit 533a6fb
Showing
9 changed files
with
260 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/operators.h> | ||
#include <pybind11/stl.h> | ||
#include <libff/algebra/curves/mnt/mnt6/mnt6_pp.hpp> | ||
#include <libff/algebra/fields/field_utils.hpp> | ||
#include <libff/algebra/scalar_multiplication/multiexp.hpp> | ||
#include <libff/common/profiling.hpp> | ||
#include <libff/common/utils.hpp> | ||
#include <libfqfft/evaluation_domain/evaluation_domain.hpp> | ||
#include <map> | ||
#include <memory> | ||
#include <libsnark/relations/arithmetic_programs/ssp/ssp.hpp> | ||
|
||
using namespace std; | ||
namespace py = pybind11; | ||
using namespace libsnark; | ||
using namespace libff; | ||
|
||
// Implementation of interfaces for a SSP ("Square Span Program"). | ||
void init_relations_arithmetic_programs_ssp(py::module &m) | ||
{ | ||
using FieldT = Fp_model<5l, libff::mnt46_modulus_B>; | ||
// A SSP instance. | ||
py::class_<ssp_instance<FieldT>>(m, "ssp_instance") | ||
.def(py::init<const std::shared_ptr<libfqfft::evaluation_domain<FieldT>> &, | ||
const size_t, | ||
const size_t, | ||
const size_t, | ||
const std::vector<std::map<size_t, FieldT>> &>()) | ||
.def(py::init<const std::shared_ptr<libfqfft::evaluation_domain<FieldT>> &, | ||
const size_t, | ||
const size_t, | ||
const size_t, | ||
std::vector<std::map<size_t, FieldT>> &>()) | ||
.def("num_variables", &ssp_instance<FieldT>::num_variables) | ||
.def("degree", &ssp_instance<FieldT>::degree) | ||
.def("num_inputs", &ssp_instance<FieldT>::num_inputs) | ||
.def("is_satisfied", &ssp_instance<FieldT>::is_satisfied, py::arg("witness")); | ||
|
||
// A SSP instance evaluation is a SSP instance that is evaluated at a field element t. | ||
py::class_<ssp_instance_evaluation<FieldT>>(m, "ssp_instance_evaluation") | ||
.def(py::init<const std::shared_ptr<libfqfft::evaluation_domain<FieldT>> &, | ||
const size_t, | ||
const size_t, | ||
const size_t, | ||
const FieldT &, | ||
const std::vector<FieldT> &, | ||
const std::vector<FieldT> &, | ||
const FieldT &>()) | ||
.def(py::init<const std::shared_ptr<libfqfft::evaluation_domain<FieldT>> &, | ||
const size_t, | ||
const size_t, | ||
const size_t, | ||
const FieldT &, | ||
std::vector<FieldT> &&, | ||
std::vector<FieldT> &&, | ||
const FieldT &>()) | ||
.def("num_variables", &ssp_instance_evaluation<FieldT>::num_variables) | ||
.def("degree", &ssp_instance_evaluation<FieldT>::degree) | ||
.def("num_inputs", &ssp_instance_evaluation<FieldT>::num_inputs) | ||
.def("is_satisfied", &ssp_instance_evaluation<FieldT>::is_satisfied, py::arg("witness")); | ||
|
||
//A SSP witness. | ||
py::class_<ssp_witness<FieldT>>(m, "ssp_witness") | ||
.def(py::init<const size_t, | ||
const size_t, | ||
const size_t, | ||
const FieldT &, | ||
const std::vector<FieldT> &, | ||
const std::vector<FieldT> &>()) | ||
.def(py::init<const size_t, | ||
const size_t, | ||
const size_t, | ||
const FieldT &, | ||
const std::vector<FieldT> &, | ||
std::vector<FieldT> &>()) | ||
.def("num_variables", &ssp_witness<FieldT>::num_variables) | ||
.def("degree", &ssp_witness<FieldT>::degree) | ||
.def("num_inputs", &ssp_witness<FieldT>::num_inputs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/PyZPK/relations/constraint_satisfaction_problems/uscs/uscs.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
#include <pybind11/stl_bind.h> | ||
#include <pybind11/cast.h> | ||
#include <pybind11/complex.h> | ||
#include <pybind11/functional.h> | ||
#include <pybind11/chrono.h> | ||
#include <cstdlib> | ||
#include <iostream> | ||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
#include <libff/algebra/fields/bigint.hpp> | ||
#include <libff/algebra/curves/mnt/mnt6/mnt6_pp.hpp> | ||
#include <libff/algebra/fields/field_utils.hpp> | ||
#include <libff/algebra/scalar_multiplication/multiexp.hpp> | ||
#include <libff/common/profiling.hpp> | ||
#include <libff/common/utils.hpp> | ||
#include <libsnark/relations/variable.hpp> | ||
#include <libsnark/relations/constraint_satisfaction_problems/uscs/uscs.hpp> | ||
|
||
using namespace std; | ||
namespace py = pybind11; | ||
using namespace libsnark; | ||
using namespace libff; | ||
|
||
// Interfaces for a USCS constraint, a USCS variable assignment, and a USCS constraint system. | ||
// USCS stands for "Unitary-Square Constraint System". | ||
void init_relations_constraint_satisfaction_problems_uscs(py::module &m) | ||
{ | ||
using FieldT = Fp_model<5l, libff::mnt46_modulus_B>; | ||
py::class_<uscs_constraint_system<FieldT>>(m, "uscs_constraint_system") | ||
.def(py::init<>()) | ||
.def_readwrite("primary_input_size", &uscs_constraint_system<FieldT>::primary_input_size) | ||
.def_readwrite("auxiliary_input_size", &uscs_constraint_system<FieldT>::auxiliary_input_size) | ||
.def_readwrite("constraints", &uscs_constraint_system<FieldT>::constraints) | ||
.def("num_inputs", &uscs_constraint_system<FieldT>::num_inputs) | ||
.def("num_variables", &uscs_constraint_system<FieldT>::num_variables) | ||
.def("num_constraints", &uscs_constraint_system<FieldT>::num_constraints) | ||
// .def("is_valid", &uscs_constraint_system<FieldT>::is_valid) | ||
// valid_vector was not declared in this scope of libsnark | ||
.def("add_constraint", (void (uscs_constraint_system<FieldT>::*)(const uscs_constraint<FieldT> &)) & uscs_constraint_system<FieldT>::add_constraint, py::arg("constraint")) | ||
.def("add_constraint", (void (uscs_constraint_system<FieldT>::*)(const uscs_constraint<FieldT> &, const std::string &)) & uscs_constraint_system<FieldT>::add_constraint, py::arg("constraint"), py::arg("annotation")) | ||
.def("is_satisfied", &uscs_constraint_system<FieldT>::is_satisfied, py::arg("primary_input"), py::arg("auxiliary_input")) | ||
.def( | ||
"__eq__", [](uscs_constraint_system<FieldT> const &self, uscs_constraint_system<FieldT> const &other) { return self == other; }, py::is_operator()) | ||
.def("report_linear_constraint_statistics", &uscs_constraint_system<FieldT>::report_linear_constraint_statistics) | ||
.def("__ostr__", [](uscs_constraint_system<FieldT> const &self) { | ||
std::ostringstream os; | ||
os << self.primary_input_size << "\n"; | ||
os << self.auxiliary_input_size << "\n"; | ||
os << self.num_constraints() << "\n"; | ||
for (const uscs_constraint<FieldT> &c : self.constraints) | ||
{ | ||
os << c; | ||
} | ||
return os; | ||
}) | ||
.def("__istr__", [](uscs_constraint_system<FieldT> &self) { | ||
std::istringstream os; | ||
os >> self.primary_input_size; | ||
os >> self.auxiliary_input_size; | ||
self.constraints.clear(); | ||
size_t s; | ||
os >> s; | ||
char b; | ||
os.read(&b, 1); | ||
self.constraints.reserve(s); | ||
for (size_t i = 0; i < s; ++i) | ||
{ | ||
uscs_constraint<FieldT> c; | ||
os >> c; | ||
self.constraints.emplace_back(c); | ||
} | ||
return os; | ||
}); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/PyZPK/relations/constraint_satisfaction_problems/uscs/uscs_examples.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
#include <pybind11/stl_bind.h> | ||
#include <pybind11/cast.h> | ||
#include <pybind11/complex.h> | ||
#include <vector> | ||
#include <cassert> | ||
#include <libff/algebra/curves/mnt/mnt6/mnt6_pp.hpp> | ||
#include <libff/algebra/fields/field_utils.hpp> | ||
#include <libff/algebra/scalar_multiplication/multiexp.hpp> | ||
#include <libff/common/profiling.hpp> | ||
#include <libff/common/utils.hpp> | ||
#include <libsnark/relations/constraint_satisfaction_problems/uscs/uscs.hpp> | ||
#include <libsnark/relations/constraint_satisfaction_problems/uscs/examples/uscs_examples.hpp> | ||
|
||
using namespace std; | ||
namespace py = pybind11; | ||
using namespace libsnark; | ||
using namespace libff; | ||
|
||
// Declaration of interfaces for a USCS example, as well as functions to sample | ||
// USCS examples with prescribed parameters | ||
void init_relations_constraint_satisfaction_problems_uscs_examples(py::module &m) | ||
{ | ||
using FieldT = Fp_model<5l, libff::mnt46_modulus_B>; | ||
py::class_<uscs_example<FieldT>>(m, "uscs_example") | ||
.def(py::init<const uscs_constraint_system<FieldT> &, | ||
const uscs_primary_input<FieldT> &, | ||
const uscs_auxiliary_input<FieldT> &>()) | ||
.def_readwrite("constraint_system", &uscs_example<FieldT>::constraint_system) | ||
.def_readwrite("primary_input", &uscs_example<FieldT>::primary_input) | ||
.def_readwrite("auxiliary_input", &uscs_example<FieldT>::auxiliary_input); | ||
|
||
m.def("generate_uscs_example_with_field_input", &generate_uscs_example_with_field_input<FieldT>, py::arg("num_constraints"), py::arg("num_inputs")); | ||
m.def("generate_uscs_example_with_binary_input", &generate_uscs_example_with_binary_input<FieldT>, py::arg("num_constraints"), py::arg("num_inputs")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters