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

Fix memory allocation and error handling in binfile_utils.cpp and prover.cpp #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/binfile_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ BinFile::BinFile(const void *fileData, size_t fileSize, std::string _type, uint3

size = fileSize;
addr = malloc(size);

if (addr == nullptr) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

A better solution is to use unique_ptr<char[]>, or even better - to not copy the data at all (checkout zi/feedback in my fork).

throw new std::runtime_error("Memory allocation failed for BinFile.");
}

memcpy(addr, fileData, size);

type.assign((const char *)addr, 4);
Expand Down
5 changes: 4 additions & 1 deletion src/fullprover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,11 @@ ProverResponse FullProverImpl::prove(const char *witness_file_path) const {
log_info("constructing metrics struct");
ProverResponseMetrics metrics;
metrics.prover_time = prover_duration.count();

const char *proof_raw = strdup(proof.dump().c_str());
if (proof_raw == nullptr) {
throw std::runtime_error("Failed to allocate memory for proof_raw");
}

log_info("FullProverImpl::prove end");
return ProverResponse(proof_raw, metrics);
Expand Down
42 changes: 39 additions & 3 deletions src/prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ static size_t PublicBufferMinSize(size_t count)
return count * 82 + 4;
}

/**
* Verifies the given prime numbers.
Copy link
Collaborator

Choose a reason for hiding this comment

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

These comments are more noise than useful.

*
* @param zkey_prime A pointer to the first prime number.
* @param wtns_prime A pointer to the second prime number.
*/
static void VerifyPrimes(mpz_srcptr zkey_prime, mpz_srcptr wtns_prime)
{
mpz_t altBbn128r;
Expand All @@ -42,6 +48,13 @@ static void VerifyPrimes(mpz_srcptr zkey_prime, mpz_srcptr wtns_prime)
mpz_clear(altBbn128r);
}

/**
* Builds a JSON string representation of the public data.
*
* @param wtnsData Pointer to an array of AltBn128::FrElement representing the public data.
* @param nPublic The number of elements in the public data array.
* @return A JSON string representation of the public data.
*/
std::string BuildPublicString(AltBn128::FrElement *wtnsData, size_t nPublic)
{
json jsonPublic;
Expand All @@ -54,6 +67,13 @@ std::string BuildPublicString(AltBn128::FrElement *wtnsData, size_t nPublic)
return jsonPublic.dump();
}

/**
* Calculates the size of the public buffer required for the given zkey buffer.
*
* @param zkey_buffer A pointer to the zkey buffer.
* @param zkey_size The size of the zkey buffer.
* @return The size of the public buffer required.
*/
unsigned long CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_size) {
try {
BinFileUtils::BinFile zkey(zkey_buffer, zkey_size, "zkey", 1);
Expand All @@ -65,6 +85,22 @@ unsigned long CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_s
return 0;
}

/**
* Proves a Groth16 proof given the necessary inputs.
*
* @param zkey_buffer Pointer to the buffer containing the zkey data.
* @param zkey_size Size of the zkey buffer.
* @param wtns_buffer Pointer to the buffer containing the wtns data.
* @param wtns_size Size of the wtns buffer.
* @param proof_buffer Pointer to the buffer to store the generated proof.
* @param proof_size Pointer to the size of the proof buffer. On input, it should contain the available size of the buffer. On output, it will be updated with the actual size of the generated proof.
* @param public_buffer Pointer to the buffer to store the generated public data.
* @param public_size Pointer to the size of the public buffer. On input, it should contain the available size of the buffer. On output, it will be updated with the actual size of the generated public data.
* @param error_msg Pointer to the buffer to store any error message that occurs during the proving process.
* @param error_msg_maxsize Maximum size of the error message buffer.
*
* @return Returns PROVER_OK if the proving process is successful. If there is an error, it returns an appropriate error code.
*/
int
groth16_prover(const void *zkey_buffer, unsigned long zkey_size,
const void *wtns_buffer, unsigned long wtns_size,
Expand Down Expand Up @@ -139,21 +175,21 @@ groth16_prover(const void *zkey_buffer, unsigned long zkey_size,
} catch (std::exception& e) {

if (error_msg) {
strncpy(error_msg, e.what(), error_msg_maxsize);
strlcpy(error_msg, e.what(), error_msg_maxsize);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Error messages are already null terminated, no need to double terminate them. But OK to use them I guess.

}
return PROVER_ERROR;

} catch (std::exception *e) {

if (error_msg) {
strncpy(error_msg, e->what(), error_msg_maxsize);
strlcpy(error_msg, e->what(), error_msg_maxsize);
}
delete e;
return PROVER_ERROR;

} catch (...) {
if (error_msg) {
strncpy(error_msg, "unknown error", error_msg_maxsize);
strlcpy(error_msg, "unknown error", error_msg_maxsize);
}
return PROVER_ERROR;
}
Expand Down
Loading