Skip to content

Commit

Permalink
cancellation file support for bluebox (#212)
Browse files Browse the repository at this point in the history
* cancellation file support for bluebox
* filter out EINTR thanks arvid
  • Loading branch information
wjblanke authored Oct 8, 2024
1 parent 5349251 commit 32319db
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/c_bindings/c_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern "C" {
std::vector<uint8_t> challenge_hash_bytes(challenge_hash, challenge_hash + challenge_size);
integer discriminant = CreateDiscriminant(challenge_hash_bytes, discriminant_size_bits);
form x = DeserializeForm(discriminant, x_s, x_s_size);
std::vector<uint8_t> result = ProveSlow(discriminant, x, num_iterations);
std::vector<uint8_t> result = ProveSlow(discriminant, x, num_iterations, "");

// Allocate memory for the result and copy data
uint8_t* resultData = new uint8_t[result.size()];
Expand Down
17 changes: 16 additions & 1 deletion src/prover_slow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "nucomp.h"
#include "picosha2.h"
#include "proof_common.h"
#include <sys/stat.h>


// TODO: Refactor to use 'Prover' class once new_vdf is merged in.
Expand Down Expand Up @@ -78,7 +79,7 @@ form GenerateWesolowski(form &y, form &x_init,
return x;
}

std::vector<uint8_t> ProveSlow(integer& D, form& x, uint64_t num_iterations) {
std::vector<uint8_t> ProveSlow(integer& D, form& x, uint64_t num_iterations, std::string shutdown_file_path) {
integer L = root(-D, 4);
PulmarkReducer reducer;
form y = form::from_abd(x.a, x.b, D);
Expand All @@ -100,6 +101,20 @@ std::vector<uint8_t> ProveSlow(integer& D, form& x, uint64_t num_iterations) {
}
nudupl_form(y, y, D, L);
reducer.reduce(y);

// Check for cancellation every 65535 interations
if ((i&0xffff)==0) {
// Only if we have a shutdown path
if (shutdown_file_path!="") {
struct stat buffer;

int statrst = stat(shutdown_file_path.c_str(), &buffer);
if ((statrst != 0) && (errno != EINTR)) {
// shutdown file doesn't exist, abort out
return {};
}
}
}
}

form proof = GenerateWesolowski(y, x, D, reducer, intermediates, num_iterations, k, l);
Expand Down
4 changes: 2 additions & 2 deletions src/python_bindings/fastvdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ PYBIND11_MODULE(chiavdf, m) {
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) {
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) {
std::string challenge_hash_str(challenge_hash);
std::string x_s_copy(x_s);
std::vector<uint8_t> result;
Expand All @@ -66,7 +66,7 @@ PYBIND11_MODULE(chiavdf, m) {
discriminant_size_bits
);
form x = DeserializeForm(D, (const uint8_t *) x_s_copy.data(), x_s_copy.size());
result = ProveSlow(D, x, num_iterations);
result = ProveSlow(D, x, num_iterations, shutdown_file_path);
}
py::bytes ret = py::bytes(reinterpret_cast<char*>(result.data()), result.size());
return ret;
Expand Down
4 changes: 2 additions & 2 deletions tests/test_n_weso_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def prove_n_weso(discriminant_challenge, x, discriminant_size, form_size, iters,
partials = []
discriminant = create_discriminant(discriminant_challenge, discriminant_size)
for _ in range(witness):
result = prove(discriminant_challenge, x, discriminant_size, iters_chunk)
result = prove(discriminant_challenge, x, discriminant_size, iters_chunk, "")
y = result[:form_size]
proof = result[form_size : 2 * form_size]
partials.append((x, y, proof))
x = y
iters -= iters_chunk * witness
result = prove(discriminant_challenge, x, discriminant_size, iters)
result = prove(discriminant_challenge, x, discriminant_size, iters, "")
y_result = result[:form_size]
y_proof = result[form_size : 2 * form_size]
assert verify_wesolowski(discriminant, x, y_result, y_proof, iters)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_prove_and_verify():

iters = 1000000
t1 = time.time()
result = prove(discriminant_challenge, initial_el, discriminant_size, iters)
result = prove(discriminant_challenge, initial_el, discriminant_size, iters, "")
t2 = time.time()
print(f"IPS: {iters / (t2 - t1)}")
result_y = result[:form_size]
Expand All @@ -36,6 +36,7 @@ def test_prove_and_verify():
result_y,
discriminant_size,
iters_2,
""
)
t2 = time.time()
print(f"IPS: {iters_2 / (t2 - t1)}")
Expand Down

0 comments on commit 32319db

Please sign in to comment.