Skip to content

Commit

Permalink
Python bindings (Chia-Network#73)
Browse files Browse the repository at this point in the history
* Attempt to fix travis build

* Add more python bindings and reenable tests
  • Loading branch information
mariano54 authored Aug 15, 2019
1 parent 1093179 commit eb4d21c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
13 changes: 13 additions & 0 deletions python-bindings/pythonbindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ PYBIND11_MODULE(blspy, m) {
const uint8_t* input = reinterpret_cast<const uint8_t*>(str.data());
return k.SignInsecure(input, len(msg));
})
.def("sign_insecure_prehashed", [](const PrivateKey &k, const py::bytes &msg) {
std::string str(msg);
const uint8_t* input = reinterpret_cast<const uint8_t*>(str.data());
return k.SignInsecurePrehashed(input);
})
.def("sign", [](const PrivateKey &k, const py::bytes &msg) {
std::string str(msg);
const uint8_t* input = reinterpret_cast<const uint8_t*>(str.data());
Expand Down Expand Up @@ -199,6 +204,10 @@ PYBIND11_MODULE(blspy, m) {
.def("get_aggregation_info", [](const Signature &sig) {
return *sig.GetAggregationInfo();
})
.def("from_insecure_sig", [](const InsecureSignature &s) {
return Signature::FromInsecureSig(s);
})
.def("get_insecure_sig", &Signature::GetInsecureSig)
.def(py::self == py::self)
.def(py::self != py::self)
.def("__repr__", [](const Signature &sig) {
Expand Down Expand Up @@ -236,6 +245,10 @@ PYBIND11_MODULE(blspy, m) {
}
return sig.Verify(converted_hashes, pks);
})
.def("from_insecure_sig", [](const InsecureSignature &s) {
return PrependSignature::FromInsecureSig(s);
})
.def("get_insecure_sig", &PrependSignature::GetInsecureSig)
.def("aggregate", &PrependSignature::Aggregate)
.def("divide_by", &PrependSignature::DivideBy)
.def(py::self == py::self)
Expand Down
31 changes: 23 additions & 8 deletions python-bindings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,30 @@ def no_throw_bad_sig():
return
assert(False)


# test1()
# test2()
# test_threshold()
# test_vectors()
# test_vectors2()
# test_vectors3()
# test_vectors4()
def additional_python_methods():
private_key = PrivateKey.from_seed(b'123')
s1 = private_key.sign(b'message')
s2 = private_key.sign_prepend(b'message')
assert s1.get_insecure_sig().verify([Util.hash256(b'message')], [private_key.get_public_key()])
assert s2.get_insecure_sig().verify([Util.hash256(private_key.get_public_key().serialize() +
Util.hash256(b'message'))], [private_key.get_public_key()])
s1_b = Signature.from_insecure_sig(s1.get_insecure_sig())
s2_b = PrependSignature.from_insecure_sig(s2.get_insecure_sig())
assert s1 == s1_b and s2 == s2_b

s3 = private_key.sign_insecure_prehashed(Util.hash256(b'456'))
assert s3.verify([Util.hash256(b'456')], [private_key.get_public_key()])


test1()
test2()
test_threshold()
test_vectors()
test_vectors2()
test_vectors3()
test_vectors4()
no_throw_bad_sig()
additional_python_methods()

print("\nAll tests passed.")

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def build_extension(self, ext):

setup(
name='blspy',
version='0.1.9',
version='0.1.10',
author='Mariano Sorgente',
author_email='[email protected]',
description='BLS signatures in c++ (python bindings)',
Expand Down

0 comments on commit eb4d21c

Please sign in to comment.