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

python-bindings: Add pickle support for G1Element #265

Merged
merged 1 commit into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions python-bindings/pythonbindings.cpp
Expand Up @@ -373,6 +373,16 @@ PYBIND11_MODULE(blspy, m)
// PythonGIL release_lock;
return G1Element::FromBytes(Bytes(data_ptr, G1Element::SIZE));
})
.def(py::pickle(
[](const G1Element &dp) { // __getstate__
return py::make_tuple(dp.Serialize());
},
[](py::tuple t) { // __setstate__
if (t.size() != 1)
throw std::runtime_error("Invalid state!");
auto vecBytes = t[0].cast<std::vector<uint8_t>>();
return G1Element::FromByteVector(vecBytes);
}))
.def("generator", &G1Element::Generator)
.def("from_message", py::overload_cast<const std::vector<uint8_t>&, const uint8_t*, int>(&G1Element::FromMessage))
.def("pair", &G1Element::Pair)
Expand Down
14 changes: 14 additions & 0 deletions python-bindings/test.py
@@ -1,5 +1,6 @@
# flake8: noqa: E501
import binascii
import pickle
from copy import deepcopy

from blspy import (
Expand Down Expand Up @@ -335,11 +336,24 @@ def test_aggregate_verify_zero_items():
assert AugSchemeMPL.aggregate_verify([], [], G2Element())


def test_pickle_support():
seed = bytes([
0, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192, 19, 18, 12, 89, 6,
220, 18, 102, 58, 209, 82, 12, 62, 89, 110, 182, 9, 44, 20, 254, 22
])
Copy link
Contributor

Choose a reason for hiding this comment

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

With this change, This binary format is part of our state that would need to be migrated if it ever changes. Is pickle a stable format? If it can change between python versions it seems risky to depend on.

There should probably be a comment here saying that these bytes may not change. If they do we'll have a migration task as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is only used as parameter for AugSchemeMPL.key_gen to generate a PrivateKey. I don't understand your concerns about changing bytes here. Can you elaborate?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I didn't read this very carefully :/

I was reading this as the pickled bytes and the test to ensure it was de-pickled correctly. Since the binary format for this needs to be stable, I think a test like that would be good. It would alert us of any changes that alters the file format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm dropped it again because it appears to create different results on CI for whatever reason.. i spent too much time with this stuff already and i don't think its super important to have this test but if you have something which works locally and on CI feel free to provide a commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

You might consider a few more tests of round-trips, especially of the infinity value (since it serializes in a unique way).

key: PrivateKey = AugSchemeMPL.key_gen(seed)
g1: G1Element = key.get_g1()
g1_data = pickle.dumps(g1)
g1_restored = pickle.loads(g1_data)
assert g1 == g1_restored


test_schemes()
test_vectors_invalid()
test_vectors_valid()
test_readme()
test_aggregate_verify_zero_items()
test_pickle_support()

print("\nAll tests passed.")

Expand Down