Skip to content

Commit

Permalink
Instantiate Prover in main.cpp and use it in both manual mode and ser…
Browse files Browse the repository at this point in the history
…ver mode
  • Loading branch information
fractasy committed Jan 7, 2022
1 parent 3927ed2 commit a35a7dd
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 115 deletions.
1 change: 1 addition & 0 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//#define LOG_FILENAME // If defined, logs ROM compilation file name and line number
#define LOG_TIME // If defined, logs time differences to measure performance
//#define LOG_TXS
#define LOG_SERVICE

#define DEBUG
#ifdef DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Executor {
void checkFinalState(Context &ctx);
};

/* Declare Context ctx to use rom[i].A0 */
/* Declare Context ctx to use rom[i].inA */
#define rom romData.romData

#endif
41 changes: 23 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ using json = nlohmann::json;

int main (int argc, char** argv)
{
#ifdef RUN_GRPC_SERVER
RawFr fr;
ZkServer server(fr);
server.run();
#else

TimerStart(WHOLE_PROCESS);
TimerStart(PARSE_JSON_FILES);

Expand Down Expand Up @@ -195,13 +189,15 @@ int main (int argc, char** argv)
}
}

#ifndef RUN_GRPC_SERVER
// Check that at least we got the input JSON file argument
if ( pInputFile == NULL )
{
cerr << "Error: You need to specify an input file name" << endl;
cout << pUsage << endl;
exit(-1);
}
#endif

// Log parsed arguments and/or default file names
cout << "Input file=" << pInputFile << endl;
Expand Down Expand Up @@ -261,9 +257,9 @@ int main (int argc, char** argv)
scriptStream.close();

// Output and input file names
string outputFile(pOutputFile);
string constantsFile(pConstantsFile);
string constantsTreeFile(pConstantsTreeFile);
string cmPolsOutputFile(pOutputFile);
string constPolsInputFile(pConstantsFile);
string constTreePolsInputFile(pConstantsTreeFile);

TimerStopAndLog(PARSE_JSON_FILES);

Expand All @@ -284,15 +280,15 @@ int main (int argc, char** argv)
Pil pil;
pil.parse(pilJson);

// Load committed polynomials into memory, mapped to a newly created output file, filled by executor
Pols cmPols;
cmPols.load(pil.cmPols);
cmPols.mapToOutputFile(outputFile);

// Load constant polynomials into memory, and map them to an existing input file containing their values
Pols constPols;
constPols.load(pil.constPols);
constPols.mapToInputFile(constantsFile);
constPols.mapToInputFile(constPolsInputFile);

// Load constants tree into memory
// TODO: Get memory pointer
//Tree tree;
//tree.mapToInputFile(constantsTreeFile);

TimerStopAndLog(LOAD_POLS_TO_MEMORY);

Expand All @@ -316,14 +312,24 @@ int main (int argc, char** argv)
script.parse(scriptJson);
TimerStopAndLog(SCRIPT_PARSE);


// Create the prover
Prover prover(fr, romData, script);
Prover prover(fr, romData, script, pil, constPols, cmPolsOutputFile);

#ifdef RUN_GRPC_SERVER
// Create server instance, passing all constant data
ZkServer server(fr, prover);

// Run the server
server.run(); // Internally, it calls prover.prove() for every input data received, in order to generate the proof and return it to the client
#else
// Call the prover
TimerStart(PROVE);
prover.prove(input, cmPols, constPols);
prover.prove(input);
TimerStopAndLog(PROVE);

#endif

// Unload the ROM data
TimerStart(ROM_UNLOAD);
romData.unload();
Expand All @@ -333,5 +339,4 @@ int main (int argc, char** argv)

cout << "Done" << endl;

#endif
}
13 changes: 10 additions & 3 deletions src/prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
#include "mem.hpp"
#include "batchmachine_executor.hpp"

void Prover::prove (Input &input, Pols &cmPols, Pols &constPols)
void Prover::prove (Input &input)
{
/************/
/* Executor */
/************/

// Load committed polynomials into memory, mapped to a newly created output file, filled by executor
Pols cmPols;
cmPols.load(pil.cmPols);
cmPols.mapToOutputFile(cmPolsOutputFile);

// Execute the program
TimerStart(EXECUTOR_EXECUTE);
executor.execute(input, cmPols);
Expand Down Expand Up @@ -67,6 +76,4 @@ void Prover::prove (Input &input, Pols &cmPols, Pols &constPols)

MemFree(mem);
cmPols.unmap();
constPols.unmap();

}
9 changes: 7 additions & 2 deletions src/prover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ class Prover
Rom &romData;
Executor executor;
Script &script;
Pil &pil;
Pols &constPols;
string &cmPolsOutputFile;
public:
Prover(RawFr &fr, Rom &romData, Script &script) : fr(fr), romData(romData), executor(fr, romData), script(script) {};
void prove (Input &input, Pols &cmPols, Pols &constPols);
Prover(RawFr &fr, Rom &romData, Script &script, Pil &pil, Pols &constPols, string &cmPolsOutputFile) :
fr(fr), romData(romData), executor(fr, romData), script(script), pil(pil), constPols(constPols), cmPolsOutputFile(cmPolsOutputFile) {};

void prove (Input &input);
};

#endif
2 changes: 1 addition & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using grpc::Status;
void ZkServer::run (void)
{
ServerBuilder builder;
ZKProverServiceImpl service(fr);
ZKProverServiceImpl service(fr, prover);

std::string server_address("0.0.0.0:50051");

Expand Down
23 changes: 3 additions & 20 deletions src/server.hpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
#ifndef ZKPROVER_SERVER_HPP
#define ZKPROVER_SERVER_HPP

/*#include <grpcpp/grpcpp.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include "service.hpp"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using zkprover::ZKProver;
using zkprover::State;
using zkprover::PublicInputs;
using zkprover::PublicInputsExtended;
using zkprover::InputProver;
using zkprover::Proof;
using zkprover::ProofX;
using zkprover::NoParams;
*/
#include "ffiasm/fr.hpp"
#include "prover.hpp"

class ZkServer
{
RawFr &fr;
Prover &prover;
public:
ZkServer(RawFr &fr) : fr(fr) {};
ZkServer(RawFr &fr, Prover &prover) : fr(fr), prover(prover) {};
void run (void);
};

Expand Down
Loading

0 comments on commit a35a7dd

Please sign in to comment.