Skip to content

Commit 3c3cfb4

Browse files
authored
remove GIL release (dashpay#250)
1 parent b634cc4 commit 3c3cfb4

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

python-bindings/pythonbindings.cpp

+29-23
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ using std::vector;
2929

3030
/* This class releases the Python GIL until the end of the scope.
3131
This is different from gil_scoped_release in that it can't be reacquired early. */
32-
class PythonGIL {
33-
public:
34-
PythonGIL() { _save = PyEval_SaveThread(); }
35-
~PythonGIL() { PyEval_RestoreThread(_save); }
36-
private:
37-
PyThreadState *_save;
38-
};
32+
// class PythonGIL {
33+
// public:
34+
// PythonGIL() { _save = PyEval_SaveThread(); }
35+
// ~PythonGIL() { PyEval_RestoreThread(_save); }
36+
// private:
37+
// PyThreadState *_save;
38+
// };
3939

4040
PYBIND11_MODULE(blspy, m)
4141
{
@@ -56,13 +56,13 @@ PYBIND11_MODULE(blspy, m)
5656
"Length of bytes object not equal to PrivateKey::SIZE");
5757
}
5858
auto data_ptr = reinterpret_cast<const uint8_t *>(info.ptr);
59-
PythonGIL release_lock;
59+
// PythonGIL release_lock;
6060
return PrivateKey::FromBytes(Bytes(data_ptr, PrivateKey::PRIVATE_KEY_SIZE));
6161
})
6262
.def(
6363
"__bytes__",
6464
[](const PrivateKey &k) {
65-
PythonGIL release_lock;
65+
// PythonGIL release_lock;
6666
uint8_t *output =
6767
Util::SecAlloc<uint8_t>(PrivateKey::PRIVATE_KEY_SIZE);
6868
k.Serialize(output);
@@ -77,7 +77,10 @@ PYBIND11_MODULE(blspy, m)
7777
[](const PrivateKey &k, const py::object &memo) {
7878
return PrivateKey(k);
7979
})
80-
.def("get_g1", [](const PrivateKey &k) { PythonGIL release_lock; return k.GetG1Element(); })
80+
.def("get_g1", [](const PrivateKey &k) {
81+
// PythonGIL release_lock;
82+
return k.GetG1Element();
83+
})
8184
.def("aggregate", &PrivateKey::Aggregate)
8285
.def(py::self == py::self)
8386
.def(py::self != py::self)
@@ -137,7 +140,7 @@ PYBIND11_MODULE(blspy, m)
137140
const py::bytes &msg,
138141
const G2Element &sig) {
139142
std::string s(msg);
140-
PythonGIL release_lock;
143+
// PythonGIL release_lock;
141144
vector<uint8_t> v(s.begin(), s.end());
142145
return BasicSchemeMPL().Verify(pk, v, sig);
143146
})
@@ -151,7 +154,7 @@ PYBIND11_MODULE(blspy, m)
151154
std::string s(msgs[i]);
152155
vecs[i] = vector<uint8_t>(s.begin(), s.end());
153156
}
154-
PythonGIL release_lock;
157+
// PythonGIL release_lock;
155158
return BasicSchemeMPL().AggregateVerify(pks, vecs, sig);
156159
})
157160
.def(
@@ -178,7 +181,7 @@ PYBIND11_MODULE(blspy, m)
178181
return AugSchemeMPL().KeyGen(inputVec);
179182
})
180183
.def("derive_child_sk", [](const PrivateKey& sk, uint32_t index){
181-
PythonGIL release_lock;
184+
// PythonGIL release_lock;
182185
return AugSchemeMPL().DeriveChildSk(sk, index);
183186
})
184187
.def("derive_child_sk_unhardened", [](const PrivateKey& sk, uint32_t index){
@@ -212,7 +215,7 @@ PYBIND11_MODULE(blspy, m)
212215
const py::bytes &msg,
213216
const G2Element &sig) {
214217
std::string s(msg);
215-
PythonGIL release_lock;
218+
// PythonGIL release_lock;
216219
vector<uint8_t> v(s.begin(), s.end());
217220
return AugSchemeMPL().Verify(pk, v, sig);
218221
})
@@ -226,7 +229,7 @@ PYBIND11_MODULE(blspy, m)
226229
std::string s(msgs[i]);
227230
vecs[i] = vector<uint8_t>(s.begin(), s.end());
228231
}
229-
PythonGIL release_lock;
232+
// PythonGIL release_lock;
230233
return AugSchemeMPL().AggregateVerify(pks, vecs, sig);
231234
})
232235
.def(
@@ -277,7 +280,7 @@ PYBIND11_MODULE(blspy, m)
277280
const py::bytes &msg,
278281
const G2Element &sig) {
279282
std::string s(msg);
280-
PythonGIL release_lock;
283+
// PythonGIL release_lock;
281284
vector<uint8_t> v(s.begin(), s.end());
282285
return PopSchemeMPL().Verify(pk, v, sig);
283286
})
@@ -291,7 +294,7 @@ PYBIND11_MODULE(blspy, m)
291294
std::string s(msgs[i]);
292295
vecs[i] = vector<uint8_t>(s.begin(), s.end());
293296
}
294-
PythonGIL release_lock;
297+
// PythonGIL release_lock;
295298
return PopSchemeMPL().AggregateVerify(pks, vecs, sig);
296299
})
297300
.def(
@@ -309,7 +312,7 @@ PYBIND11_MODULE(blspy, m)
309312
return PopSchemeMPL().PopProve(privateKey);
310313
})
311314
.def("pop_verify", [](const G1Element& pubkey, const G2Element& signature){
312-
PythonGIL release_lock;
315+
// PythonGIL release_lock;
313316
return PopSchemeMPL().PopVerify(pubkey, signature);
314317
})
315318
.def(
@@ -318,7 +321,7 @@ PYBIND11_MODULE(blspy, m)
318321
const py::bytes &msg,
319322
const G2Element &sig) {
320323
std::string s(msg);
321-
PythonGIL release_lock;
324+
// PythonGIL release_lock;
322325
vector<uint8_t> v(s.begin(), s.end());
323326
return PopSchemeMPL().FastAggregateVerify(pks, v, sig);
324327
});
@@ -367,7 +370,7 @@ PYBIND11_MODULE(blspy, m)
367370
"Length of bytes object not equal to G1Element::SIZE");
368371
}
369372
auto data_ptr = reinterpret_cast<const uint8_t *>(info.ptr);
370-
PythonGIL release_lock;
373+
// PythonGIL release_lock;
371374
return G1Element::FromBytes(Bytes(data_ptr, G1Element::SIZE));
372375
})
373376
.def("generator", &G1Element::Generator)
@@ -385,7 +388,10 @@ PYBIND11_MODULE(blspy, m)
385388
})
386389
.def(
387390
"__add__",
388-
[](G1Element &self, G1Element &other) { PythonGIL release_lock; return self + other; },
391+
[](G1Element &self, G1Element &other) {
392+
// PythonGIL release_lock;
393+
return self + other;
394+
},
389395
py::is_operator())
390396
.def(
391397
"__mul__",
@@ -421,9 +427,9 @@ PYBIND11_MODULE(blspy, m)
421427
"__bytes__",
422428
[](const G1Element &ele) {
423429
vector<uint8_t> out;
424-
Py_BEGIN_ALLOW_THREADS
430+
// Py_BEGIN_ALLOW_THREADS
425431
out = ele.Serialize();
426-
Py_END_ALLOW_THREADS
432+
// Py_END_ALLOW_THREADS
427433
py::bytes ans = py::bytes(
428434
reinterpret_cast<const char *>(out.data()), G1Element::SIZE);
429435
return ans;

0 commit comments

Comments
 (0)