-
Notifications
You must be signed in to change notification settings - Fork 1
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
zi0Black
wants to merge
2
commits into
main
Choose a base branch
from
sec-review-zi0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,12 @@ static size_t PublicBufferMinSize(size_t count) | |
return count * 82 + 4; | ||
} | ||
|
||
/** | ||
* Verifies the given prime numbers. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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, | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).