-
-
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 binding for integer_permutation and set_membership_proof (#44)
* Add pybind basic binding * Add bindings for integer_permutation * Add Binding for set_membership_proof Co-authored-by: Chinmay Shah <[email protected]>
- Loading branch information
1 parent
458a4e0
commit 6bf68dc
Showing
4 changed files
with
105 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/operators.h> | ||
#include <pybind11/stl.h> | ||
#include <algorithm> | ||
#include <cassert> | ||
#include <numeric> | ||
#include <unordered_set> | ||
#include <iostream> | ||
|
||
#include <libff/common/serialization.hpp> | ||
#include <libsnark/common/data_structures/integer_permutation.hpp> | ||
#include <libsnark/common/data_structures/set_commitment.hpp> | ||
|
||
using namespace std; | ||
using namespace libsnark; | ||
namespace py = pybind11; | ||
|
||
PYBIND11_MODULE(pyzpk, m) | ||
{ | ||
m.doc() = "Python wrapper for open source Zero Proof Knowledge Library"; | ||
|
||
// Binding for common/data_structures/integer_permutation.cpp | ||
py::class_<integer_permutation>(m, "integer_permutation") | ||
.def(py::init<const size_t>()) | ||
.def(py::init<const size_t, const size_t>()) | ||
.def("set", &integer_permutation::set) | ||
.def("get", &integer_permutation::get) | ||
.def("size", &integer_permutation::size) | ||
.def("is_valid", &integer_permutation::is_valid) | ||
.def("inverse", &integer_permutation::inverse) | ||
.def("slice", &integer_permutation::slice) | ||
.def("next_permutation", &integer_permutation::next_permutation) | ||
.def("random_shuffle", &integer_permutation::random_shuffle) | ||
.def( | ||
"__eq__", [](integer_permutation const &self, integer_permutation const &other) { return self == other; }, py::is_operator()); | ||
|
||
// Binding for common/data_structures/set_commitment.cpp // set_membership_proof | ||
py::class_<set_membership_proof>(m, "set_membership_proof") | ||
.def("size_in_bits", &set_membership_proof::size_in_bits) | ||
.def( | ||
"__eq__", [](set_membership_proof const &self, set_membership_proof const &other) { return self == other; }, py::is_operator()); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,54 @@ | ||
import pyzpk | ||
|
||
print(pyzpk.add(2,3)) | ||
min_element = 0 | ||
max_element = 7 | ||
|
||
# intiallize vector and populate from 0 to 7 of size 8 using std::iota | ||
vec = pyzpk.integer_permutation(min_element,max_element) | ||
|
||
assert vec.get(2) == 2 | ||
assert vec.get(1) == 1 | ||
assert vec.get(5) == 5 | ||
assert vec.get(7) == 7 | ||
|
||
# slice from a to b | ||
vec1 = vec.slice(3,5) | ||
assert vec1.get(5) == 5 | ||
|
||
# Permutating the vector | ||
vec.set(0,7) | ||
vec.set(1,4) | ||
vec.set(2,3) | ||
vec.set(3,0) | ||
vec.set(4,5) | ||
vec.set(5,1) | ||
vec.set(6,2) | ||
vec.set(7,6) | ||
|
||
assert vec.get(2) == 3 | ||
assert vec.get(1) == 4 | ||
assert vec.get(5) == 1 | ||
assert vec.get(7) == 6 | ||
|
||
# .size() | ||
assert vec.size() == 8 | ||
|
||
# Check is_valid permute | ||
assert vec.is_valid() == True | ||
|
||
# Random shuffle elements of vector | ||
vec.random_shuffle() | ||
assert vec.is_valid() == True | ||
|
||
# Finding next permute | ||
vec.next_permutation() | ||
assert vec.is_valid() == True | ||
|
||
# Finding next permute | ||
vec2 = vec.inverse() | ||
assert vec2.is_valid() == True | ||
|
||
# set a number above max_element for idx 5 | ||
vec.set(5,10) | ||
# Check is_valid permute | ||
assert vec.is_valid() == False |