Skip to content

Commit

Permalink
Add function to calculate the required size of the buffer for public …
Browse files Browse the repository at this point in the history
…signals.
  • Loading branch information
olomix committed Feb 1, 2024
1 parent ea72e69 commit 1829c1e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ unsigned long CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_s
return 0;
}

int
groth16_public_size_for_zkey_file(const char *zkey_fname,
unsigned long *public_size,
char *error_msg, unsigned long error_msg_maxsize) {
try {
auto zkey = BinFileUtils::openExisting(zkey_fname, "zkey", 1);
auto zkeyHeader = ZKeyUtils::loadHeader(zkey.get());
*public_size = PublicBufferMinSize(zkeyHeader->nPublic);
} catch (std::exception& e) {
if (error_msg) {
strncpy(error_msg, e.what(), error_msg_maxsize);
}
return PROVER_ERROR;
} catch (...) {
if (error_msg) {
strncpy(error_msg, "unknown error", error_msg_maxsize);
}
return PROVER_ERROR;
}

return 0;
}

int
groth16_prover(const void *zkey_buffer, unsigned long zkey_size,
const void *wtns_buffer, unsigned long wtns_size,
Expand All @@ -91,6 +114,16 @@ groth16_prover(const void *zkey_buffer, unsigned long zkey_size,

if (*proof_size < proofMinSize || *public_size < publicMinSize) {

if (*proof_size < proofMinSize) {
snprintf(error_msg, error_msg_maxsize,
"Proof buffer is too short. Minimum size: %lu, actual size: %lu",
proofMinSize, *proof_size);
} else {
snprintf(error_msg, error_msg_maxsize,
"Public buffer is too short. Minimum size: %lu, actual size: %lu",
publicMinSize, *public_size);
}

*proof_size = proofMinSize;
*public_size = publicMinSize;

Expand Down
14 changes: 14 additions & 0 deletions src/prover.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ extern "C" {
*/
unsigned long CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_size);

/**
* groth16_public_size_for_zkey_file calculates minimum buffer size for
* JSON-formatted public signals. The calculated buffer size is written
* to the public_size variable.
*
* @return error code:
* PROVER_OK (0) - in case of success
* PROVER_ERROR - in case of an error, error_msg contains the error message
*/
int
groth16_public_size_for_zkey_file(const char *zkey_fname,
unsigned long *public_size,
char *error_msg, unsigned long error_msg_maxsize);

/**
* groth16_prover
* @return error code:
Expand Down

0 comments on commit 1829c1e

Please sign in to comment.