Skip to content

Commit

Permalink
Move back proof calc to main_prover.cpp to do not deal with public bu…
Browse files Browse the repository at this point in the history
…ffer size. Rename CalcPublicBufferSize function.
  • Loading branch information
OBrezhniev committed Jan 4, 2024
1 parent 3a7d52e commit 033fb29
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
92 changes: 58 additions & 34 deletions src/main_prover.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#include <iostream>
#include <fstream>
#include <gmp.h>
#include <memory>
#include <stdexcept>
#include <nlohmann/json.hpp>

#include "fileloader.hpp"
#include "prover.h"
#include <alt_bn128.hpp>
#include "binfile_utils.hpp"
#include "zkey_utils.hpp"
#include "wtns_utils.hpp"
#include "groth16.hpp"

using json = nlohmann::json;

#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)


const size_t BufferSize = 32768;


int main(int argc, char **argv)
{
if (argc != 5) {
Expand All @@ -20,60 +24,80 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

mpz_t altBbn128r;

mpz_init(altBbn128r);
mpz_set_str(altBbn128r, "21888242871839275222246405745257275088548364400416034343698204186575808495617", 10);

try {
std::string zkeyFilename = argv[1];
std::string wtnsFilename = argv[2];
std::string proofFilename = argv[3];
std::string publicFilename = argv[4];

BinFileUtils::FileLoader zkeyFileLoader(zkeyFilename);
BinFileUtils::FileLoader wtnsFileLoader(wtnsFilename);
auto zkey = BinFileUtils::openExisting(zkeyFilename, "zkey", 1);
auto zkeyHeader = ZKeyUtils::loadHeader(zkey.get());

char proofBuffer[BufferSize];
std::string proofStr;
if (mpz_cmp(zkeyHeader->rPrime, altBbn128r) != 0) {
throw std::invalid_argument( "zkey curve not supported" );
}

size_t publicBufferSize = groth16_CalcPublicBufferSize(
zkeyFileLoader.dataBuffer(),
zkeyFileLoader.dataSize()
);
auto wtns = BinFileUtils::openExisting(wtnsFilename, "wtns", 2);
auto wtnsHeader = WtnsUtils::loadHeader(wtns.get());

char publicBuffer[publicBufferSize];

size_t proofSize = sizeof(proofBuffer);
size_t publicSize = sizeof(publicBuffer);
char errorMessage[256];
int error = 0;

error = groth16_prover(zkeyFileLoader.dataBuffer(), zkeyFileLoader.dataSize(),
wtnsFileLoader.dataBuffer(), wtnsFileLoader.dataSize(),
proofBuffer, &proofSize,
publicBuffer, &publicSize,
errorMessage, sizeof(errorMessage));

if (error == PROVER_ERROR_SHORT_BUFFER) {
std::cerr << "Error: Short buffer for proof or public" << '\n';
return EXIT_FAILURE;
} else if (error) {
std::cerr << errorMessage << '\n';
return EXIT_FAILURE;
if (mpz_cmp(wtnsHeader->prime, altBbn128r) != 0) {
throw std::invalid_argument( "different wtns curve" );
}

auto prover = Groth16::makeProver<AltBn128::Engine>(
zkeyHeader->nVars,
zkeyHeader->nPublic,
zkeyHeader->domainSize,
zkeyHeader->nCoefs,
zkeyHeader->vk_alpha1,
zkeyHeader->vk_beta1,
zkeyHeader->vk_beta2,
zkeyHeader->vk_delta1,
zkeyHeader->vk_delta2,
zkey->getSectionData(4), // Coefs
zkey->getSectionData(5), // pointsA
zkey->getSectionData(6), // pointsB1
zkey->getSectionData(7), // pointsB2
zkey->getSectionData(8), // pointsC
zkey->getSectionData(9) // pointsH1
);
AltBn128::FrElement *wtnsData = (AltBn128::FrElement *)wtns->getSectionData(2);
auto proof = prover->prove(wtnsData);

std::ofstream proofFile;
proofFile.open (proofFilename);
proofFile << proofBuffer;
proofFile << proof->toJson();
proofFile.close();

std::ofstream publicFile;
publicFile.open (publicFilename);
publicFile << publicBuffer;

json jsonPublic;
AltBn128::FrElement aux;
for (int i=1; i<=zkeyHeader->nPublic; i++) {
AltBn128::Fr.toMontgomery(aux, wtnsData[i]);
jsonPublic.push_back(AltBn128::Fr.toString(aux));
}

publicFile << jsonPublic;
publicFile.close();

} catch (std::exception* e) {
mpz_clear(altBbn128r);
std::cerr << e->what() << '\n';
return EXIT_FAILURE;
} catch (std::exception& e) {
mpz_clear(altBbn128r);
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}

mpz_clear(altBbn128r);
exit(EXIT_SUCCESS);
}
2 changes: 1 addition & 1 deletion src/prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::string BuildPublicString(AltBn128::FrElement *wtnsData, size_t nPublic)
return jsonPublic.dump();
}

unsigned long groth16_CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_size) {
unsigned long CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_size) {
try {
BinFileUtils::BinFile zkey(zkey_buffer, zkey_size, "zkey", 1);
auto zkeyHeader = ZKeyUtils::loadHeader(&zkey);
Expand Down
2 changes: 1 addition & 1 deletion src/prover.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern "C" {
* Calculates buffer size to output public signals as json string
* @returns buffer size in bytes or 0 in case of an error
*/
unsigned long groth16_CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_size);
unsigned long CalcPublicBufferSize(const void *zkey_buffer, unsigned long zkey_size);

/**
* groth16_prover
Expand Down

0 comments on commit 033fb29

Please sign in to comment.