Skip to content

Commit

Permalink
Add binding for integer_permutation and set_membership_proof (#44)
Browse files Browse the repository at this point in the history
* 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
gargarchit and chinmayshah99 authored Jun 1, 2020
1 parent 458a4e0 commit 6bf68dc
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
cmake_minimum_required( VERSION 3.10.2 )
project(PyZPK)

# set(CMAKE_CXX_STANDARD 17)
# set(CMAKE_CXX_STANDARD_REQUIRED True)


# The set instruction sets environment variables
# Note: If this environment variable already exist in the cache, it is shadowed by the set value here
Expand Down Expand Up @@ -107,6 +104,5 @@ else()
add_definitions(-DNO_PROCPS)
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(third_party)
add_subdirectory(src)
12 changes: 11 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
add_library(pyzpk SHARED PyZPK/main.cpp)
set(
SRC
"${CMAKE_CURRENT_SOURCE_DIR}/PyZPK/binding.cpp"
)

include_directories(BEFORE ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})

pybind11_add_module(pyzpk ${SRC})
set_target_properties(pyzpk PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED TRUE)
set_target_properties(snark PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
target_link_libraries(pyzpk PRIVATE snark)

target_include_directories(
pyzpk
Expand Down
42 changes: 42 additions & 0 deletions src/PyZPK/binding.cpp
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());
}
53 changes: 52 additions & 1 deletion test_binding.py
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

0 comments on commit 6bf68dc

Please sign in to comment.