Skip to content

Commit 2bfab78

Browse files
committed
gil release
1 parent f31b096 commit 2bfab78

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

src/python_bindings/fastvdf.cpp

+35-24
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ PYBIND11_MODULE(chiavdf, m) {
1111
// Creates discriminant.
1212
m.def("create_discriminant", [] (const py::bytes& challenge_hash, int discriminant_size_bits) {
1313
std::string challenge_hash_str(challenge_hash);
14-
py::gil_scoped_release release;
15-
auto challenge_hash_bits = std::vector<uint8_t>(challenge_hash_str.begin(), challenge_hash_str.end());
16-
integer D = CreateDiscriminant(
17-
challenge_hash_bits,
18-
discriminant_size_bits
19-
);
14+
integer D;
15+
{
16+
py::gil_scoped_release release;
17+
auto challenge_hash_bits = std::vector<uint8_t>(challenge_hash_str.begin(), challenge_hash_str.end());
18+
D = CreateDiscriminant(
19+
challenge_hash_bits,
20+
discriminant_size_bits
21+
);
22+
}
2023
return D.to_string();
2124
});
2225

@@ -29,13 +32,14 @@ PYBIND11_MODULE(chiavdf, m) {
2932
std::string x_s_copy(x_s);
3033
std::string y_s_copy(y_s);
3134
std::string proof_s_copy(proof_s);
32-
py::gil_scoped_release release;
33-
form x = DeserializeForm(D, (const uint8_t *)x_s_copy.data(), x_s_copy.size());
34-
form y = DeserializeForm(D, (const uint8_t *)y_s_copy.data(), y_s_copy.size());
35-
form proof = DeserializeForm(D, (const uint8_t *)proof_s_copy.data(), proof_s_copy.size());
36-
3735
bool is_valid = false;
38-
VerifyWesolowskiProof(D, x, y, proof, num_iterations, is_valid);
36+
{
37+
py::gil_scoped_release release;
38+
form x = DeserializeForm(D, (const uint8_t *)x_s_copy.data(), x_s_copy.size());
39+
form y = DeserializeForm(D, (const uint8_t *)y_s_copy.data(), y_s_copy.size());
40+
form proof = DeserializeForm(D, (const uint8_t *)proof_s_copy.data(), proof_s_copy.size());
41+
VerifyWesolowskiProof(D, x, y, proof, num_iterations, is_valid);
42+
}
3943
return is_valid;
4044
});
4145

@@ -47,17 +51,22 @@ PYBIND11_MODULE(chiavdf, m) {
4751
std::string discriminant_copy(discriminant);
4852
std::string x_s_copy(x_s);
4953
std::string proof_blob_copy(proof_blob);
50-
py::gil_scoped_release release;
5154
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_copy.data());
5255
int proof_blob_size = proof_blob_copy.size();
53-
54-
return CheckProofOfTimeNWesolowski(integer(discriminant_copy), (const uint8_t *)x_s_copy.data(), proof_blob_ptr, proof_blob_size, num_iterations, disc_size_bits, recursion);
56+
bool is_valid = false;
57+
{
58+
py::gil_scoped_release release;
59+
is_valid=CheckProofOfTimeNWesolowski(integer(discriminant_copy), (const uint8_t *)x_s_copy.data(), proof_blob_ptr, proof_blob_size, num_iterations, disc_size_bits, recursion);
60+
}
61+
return is_valid;
5562
});
5663

64+
5765
m.def("prove", [] (const py::bytes& challenge_hash, const string& x_s, int discriminant_size_bits, uint64_t num_iterations, const string& shutdown_file_path) {
5866
std::string challenge_hash_str(challenge_hash);
5967
std::string x_s_copy(x_s);
6068
std::vector<uint8_t> result;
69+
std::string shutdown_file_path_copy(shutdown_file_path);
6170
{
6271
py::gil_scoped_release release;
6372
std::vector<uint8_t> challenge_hash_bytes(challenge_hash_str.begin(), challenge_hash_str.end());
@@ -66,7 +75,7 @@ PYBIND11_MODULE(chiavdf, m) {
6675
discriminant_size_bits
6776
);
6877
form x = DeserializeForm(D, (const uint8_t *) x_s_copy.data(), x_s_copy.size());
69-
result = ProveSlow(D, x, num_iterations, shutdown_file_path);
78+
result = ProveSlow(D, x, num_iterations, shutdown_file_path_copy);
7079
}
7180
py::bytes ret = py::bytes(reinterpret_cast<char*>(result.data()), result.size());
7281
return ret;
@@ -78,12 +87,12 @@ PYBIND11_MODULE(chiavdf, m) {
7887
const string& x_s,
7988
const string& proof_blob,
8089
const uint64_t num_iterations, const uint64_t recursion) {
90+
std::string discriminant_copy(discriminant);
91+
std::string B_copy(B);
92+
std::string x_s_copy(x_s);
93+
std::string proof_blob_copy(proof_blob);
8194
std::pair<bool, std::vector<uint8_t>> result;
8295
{
83-
std::string discriminant_copy(discriminant);
84-
std::string B_copy(B);
85-
std::string x_s_copy(x_s);
86-
std::string proof_blob_copy(proof_blob);
8796
py::gil_scoped_release release;
8897
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_copy.data());
8998
int proof_blob_size = proof_blob_copy.size();
@@ -101,10 +110,12 @@ PYBIND11_MODULE(chiavdf, m) {
101110
std::string discriminant_copy(discriminant);
102111
std::string x_s_copy(x_s);
103112
std::string proof_blob_copy(proof_blob);
104-
py::gil_scoped_release release;
105-
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_copy.data());
106-
int proof_blob_size = proof_blob_copy.size();
107-
integer B = GetBFromProof(integer(discriminant_copy), (const uint8_t *)x_s_copy.data(), proof_blob_ptr, proof_blob_size, num_iterations, recursion);
113+
{
114+
py::gil_scoped_release release;
115+
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_copy.data());
116+
int proof_blob_size = proof_blob_copy.size();
117+
integer B = GetBFromProof(integer(discriminant_copy), (const uint8_t *)x_s_copy.data(), proof_blob_ptr, proof_blob_size, num_iterations, recursion);
118+
}
108119
return B.to_string();
109120
});
110121
}

0 commit comments

Comments
 (0)