Skip to content

Commit

Permalink
xearl4's ApproximateParameter fix and GIL related parameter copies (#215
Browse files Browse the repository at this point in the history
)

* duplicating xearl4 pr to test ci more

* copy

* more copies to appease the gil
  • Loading branch information
wjblanke authored Oct 8, 2024
1 parent 816356b commit c89d080
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class OneWesolowskiCallback: public WesolowskiCallback {
uint32_t k, l;
this->wanted_iter = wanted_iter;
if (wanted_iter >= (1 << 16)) {
ApproximateParameters(wanted_iter, k, l);
ApproximateParameters(wanted_iter, l, k);
} else {
k = 10;
l = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/provers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OneWesolowskiProver : public Prover {
{
this->intermediates = intermediates;
if (num_iterations >= (1 << 16)) {
ApproximateParameters(num_iterations, k, l);
ApproximateParameters(num_iterations, l, k);
} else {
k = 10;
l = 1;
Expand Down
45 changes: 28 additions & 17 deletions src/python_bindings/fastvdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ PYBIND11_MODULE(chiavdf, m) {
const string& x_s, const string& y_s,
const string& proof_s,
uint64_t num_iterations) {
py::gil_scoped_release release;
integer D(discriminant);
form x = DeserializeForm(D, (const uint8_t *)x_s.data(), x_s.size());
form y = DeserializeForm(D, (const uint8_t *)y_s.data(), y_s.size());
form proof = DeserializeForm(D, (const uint8_t *)proof_s.data(), proof_s.size());
std::string x_s_copy(x_s);
std::string y_s_copy(y_s);
std::string proof_s_copy(proof_s);
py::gil_scoped_release release;
form x = DeserializeForm(D, (const uint8_t *)x_s_copy.data(), x_s_copy.size());
form y = DeserializeForm(D, (const uint8_t *)y_s_copy.data(), y_s_copy.size());
form proof = DeserializeForm(D, (const uint8_t *)proof_s_copy.data(), proof_s_copy.size());

bool is_valid = false;
VerifyWesolowskiProof(D, x, y, proof, num_iterations, is_valid);
Expand All @@ -41,16 +44,19 @@ PYBIND11_MODULE(chiavdf, m) {
const string& x_s,
const string& proof_blob,
const uint64_t num_iterations, const uint64_t disc_size_bits, const uint64_t recursion) {
std::string discriminant_copy(discriminant);
std::string x_s_copy(x_s);
std::string proof_blob_copy(proof_blob);
py::gil_scoped_release release;
std::string proof_blob_str(proof_blob);
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_str.data());
int proof_blob_size = proof_blob.size();
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_copy.data());
int proof_blob_size = proof_blob_copy.size();

return CheckProofOfTimeNWesolowski(integer(discriminant), (const uint8_t *)x_s.data(), proof_blob_ptr, proof_blob_size, num_iterations, disc_size_bits, recursion);
return CheckProofOfTimeNWesolowski(integer(discriminant_copy), (const uint8_t *)x_s_copy.data(), proof_blob_ptr, proof_blob_size, num_iterations, disc_size_bits, recursion);
});

m.def("prove", [] (const py::bytes& challenge_hash, const string& x_s, int discriminant_size_bits, uint64_t num_iterations) {
std::string challenge_hash_str(challenge_hash);
std::string x_s_copy(x_s);
std::vector<uint8_t> result;
{
py::gil_scoped_release release;
Expand All @@ -59,7 +65,7 @@ PYBIND11_MODULE(chiavdf, m) {
challenge_hash_bytes,
discriminant_size_bits
);
form x = DeserializeForm(D, (const uint8_t *) x_s.data(), x_s.size());
form x = DeserializeForm(D, (const uint8_t *) x_s_copy.data(), x_s_copy.size());
result = ProveSlow(D, x, num_iterations);
}
py::bytes ret = py::bytes(reinterpret_cast<char*>(result.data()), result.size());
Expand All @@ -74,11 +80,14 @@ PYBIND11_MODULE(chiavdf, m) {
const uint64_t num_iterations, const uint64_t recursion) {
std::pair<bool, std::vector<uint8_t>> result;
{
std::string discriminant_copy(discriminant);
std::string B_copy(B);
std::string x_s_copy(x_s);
std::string proof_blob_copy(proof_blob);
py::gil_scoped_release release;
std::string proof_blob_str(proof_blob);
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_str.data());
int proof_blob_size = proof_blob.size();
result = CheckProofOfTimeNWesolowskiWithB(integer(discriminant), integer(B), (const uint8_t *)x_s.data(), proof_blob_ptr, proof_blob_size, num_iterations, recursion);
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_copy.data());
int proof_blob_size = proof_blob_copy.size();
result = CheckProofOfTimeNWesolowskiWithB(integer(discriminant_copy), integer(B_copy), (const uint8_t *)x_s_copy.data(), proof_blob_ptr, proof_blob_size, num_iterations, recursion);
}
py::bytes res_bytes = py::bytes(reinterpret_cast<char*>(result.second.data()), result.second.size());
py::tuple res_tuple = py::make_tuple(result.first, res_bytes);
Expand All @@ -89,11 +98,13 @@ PYBIND11_MODULE(chiavdf, m) {
const string& x_s,
const string& proof_blob,
const uint64_t num_iterations, const uint64_t recursion) {
std::string discriminant_copy(discriminant);
std::string x_s_copy(x_s);
std::string proof_blob_copy(proof_blob);
py::gil_scoped_release release;
std::string proof_blob_str(proof_blob);
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_str.data());
int proof_blob_size = proof_blob.size();
integer B = GetBFromProof(integer(discriminant), (const uint8_t *)x_s.data(), proof_blob_ptr, proof_blob_size, num_iterations, recursion);
uint8_t *proof_blob_ptr = reinterpret_cast<uint8_t *>(proof_blob_copy.data());
int proof_blob_size = proof_blob_copy.size();
integer B = GetBFromProof(integer(discriminant_copy), (const uint8_t *)x_s_copy.data(), proof_blob_ptr, proof_blob_size, num_iterations, recursion);
return B.to_string();
});
}

0 comments on commit c89d080

Please sign in to comment.