-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Improve zkey loading (#27)
Co-authored-by: nixw <>
Showing
12 changed files
with
630 additions
and
323 deletions.
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
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 |
---|---|---|
@@ -1,103 +1,76 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <gmp.h> | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
#include <stdexcept> | ||
#include <nlohmann/json.hpp> | ||
|
||
#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) | ||
#include "prover.h" | ||
#include "fileloader.hpp" | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 5) { | ||
std::cerr << "Invalid number of parameters:\n"; | ||
std::cerr << "Usage: prover <circuit.zkey> <witness.wtns> <proof.json> <public.json>\n"; | ||
std::cerr << "Invalid number of parameters" << std::endl; | ||
std::cerr << "Usage: prover <circuit.zkey> <witness.wtns> <proof.json> <public.json>" << std::endl; | ||
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]; | ||
|
||
auto zkey = BinFileUtils::openExisting(zkeyFilename, "zkey", 1); | ||
auto zkeyHeader = ZKeyUtils::loadHeader(zkey.get()); | ||
|
||
std::string proofStr; | ||
if (mpz_cmp(zkeyHeader->rPrime, altBbn128r) != 0) { | ||
throw std::invalid_argument( "zkey curve not supported" ); | ||
const std::string zkeyFilename = argv[1]; | ||
const std::string wtnsFilename = argv[2]; | ||
const std::string proofFilename = argv[3]; | ||
const std::string publicFilename = argv[4]; | ||
|
||
BinFileUtils::FileLoader zkeyFile(zkeyFilename); | ||
BinFileUtils::FileLoader wtnsFile(wtnsFilename); | ||
std::vector<char> publicBuffer; | ||
std::vector<char> proofBuffer; | ||
unsigned long long publicSize = 0; | ||
unsigned long long proofSize = 0; | ||
char errorMsg[1024]; | ||
|
||
int error = groth16_public_size_for_zkey_buf( | ||
zkeyFile.dataBuffer(), | ||
zkeyFile.dataSize(), | ||
&publicSize, | ||
errorMsg, | ||
sizeof(errorMsg)); | ||
|
||
if (error != PROVER_OK) { | ||
throw std::runtime_error(errorMsg); | ||
} | ||
|
||
auto wtns = BinFileUtils::openExisting(wtnsFilename, "wtns", 2); | ||
auto wtnsHeader = WtnsUtils::loadHeader(wtns.get()); | ||
|
||
if (mpz_cmp(wtnsHeader->prime, altBbn128r) != 0) { | ||
throw std::invalid_argument( "different wtns curve" ); | ||
groth16_proof_size(&proofSize); | ||
|
||
publicBuffer.resize(publicSize); | ||
proofBuffer.resize(proofSize); | ||
|
||
error = groth16_prover( | ||
zkeyFile.dataBuffer(), | ||
zkeyFile.dataSize(), | ||
wtnsFile.dataBuffer(), | ||
wtnsFile.dataSize(), | ||
proofBuffer.data(), | ||
&proofSize, | ||
publicBuffer.data(), | ||
&publicSize, | ||
errorMsg, | ||
sizeof(errorMsg)); | ||
|
||
if (error != PROVER_OK) { | ||
throw std::runtime_error(errorMsg); | ||
} | ||
|
||
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 << proof->toJson(); | ||
proofFile.close(); | ||
std::ofstream proofFile(proofFilename); | ||
proofFile.write(proofBuffer.data(), proofSize); | ||
|
||
std::ofstream publicFile; | ||
publicFile.open (publicFilename); | ||
|
||
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)); | ||
} | ||
std::ofstream publicFile(publicFilename); | ||
publicFile.write(publicBuffer.data(), publicSize); | ||
|
||
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'; | ||
std::cerr << "Error: " << e.what() << std::endl; | ||
return EXIT_FAILURE; | ||
|
||
} | ||
|
||
mpz_clear(altBbn128r); | ||
exit(EXIT_SUCCESS); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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