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

Add function to calculate the required size of the buffer for public signals #11

Merged
merged 4 commits into from
Feb 7, 2024
Merged
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
72 changes: 59 additions & 13 deletions src/prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,57 @@ std::string BuildPublicString(AltBn128::FrElement *wtnsData, size_t nPublic)
return jsonPublic.dump();
}

unsigned long CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_size) {
int
groth16_public_size_for_zkey_buf(const void *zkey_buffer, unsigned long zkey_size,
size_t *public_size,
char *error_msg, unsigned long error_msg_maxsize) {
try {
BinFileUtils::BinFile zkey(zkey_buffer, zkey_size, "zkey", 1);
auto zkeyHeader = ZKeyUtils::loadHeader(&zkey);
return PublicBufferMinSize(zkeyHeader->nPublic);
*public_size = PublicBufferMinSize(zkeyHeader->nPublic);
return PROVER_OK;
} 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_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);
return PROVER_OK;
} 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;
}
}

int groth16_prover(const void *zkey_buffer, unsigned long zkey_size,
const void *wtns_buffer, unsigned long wtns_size,
char *proof_buffer, unsigned long *proof_size,
char *public_buffer, unsigned long *public_size,
char *error_msg, unsigned long error_msg_maxsize) {
int
groth16_prover(const void *zkey_buffer, unsigned long zkey_size,
const void *wtns_buffer, unsigned long wtns_size,
char *proof_buffer, unsigned long *proof_size,
char *public_buffer, unsigned long *public_size,
char *error_msg, unsigned long error_msg_maxsize)
{
try {
BinFileUtils::BinFile zkey(zkey_buffer, zkey_size, "zkey", 1);
auto zkeyHeader = ZKeyUtils::loadHeader(&zkey);
Expand All @@ -90,6 +125,16 @@ int 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 Expand Up @@ -160,11 +205,12 @@ int groth16_prover(const void *zkey_buffer, unsigned long zkey_size,
return PROVER_OK;
}

int groth16_prover_zkey_file(const char *zkey_file_path,
const void *wtns_buffer, unsigned long wtns_size,
char *proof_buffer, unsigned long *proof_size,
char *public_buffer, unsigned long *public_size,
char *error_msg, unsigned long error_msg_maxsize) {
int
groth16_prover_zkey_file(const char *zkey_file_path,
const void *wtns_buffer, unsigned long wtns_size,
char *proof_buffer, unsigned long *proof_size,
char *public_buffer, unsigned long *public_size,
char *error_msg, unsigned long error_msg_maxsize) {

std::string zkey_filename(zkey_file_path);

Expand Down
21 changes: 19 additions & 2 deletions src/prover.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@ extern "C" {

/**
* Calculates buffer size to output public signals as json string
* @returns buffer size in bytes or 0 in case of an error
* @returns PROVER_OK in case of success, and the size of public buffer is written to public_size
*/
unsigned long CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_size);
int
groth16_public_size_for_zkey_buf(const void *zkey_buffer, unsigned long zkey_size,
size_t *public_size,
char *error_msg, unsigned long error_msg_maxsize);

/**
* 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
Expand Down
Loading