Skip to content

Commit

Permalink
Port config.hashDBSingleton functionality to all forks, native code
Browse files Browse the repository at this point in the history
  • Loading branch information
fractasy committed May 13, 2024
1 parent 486cf80 commit ce144dd
Show file tree
Hide file tree
Showing 35 changed files with 443 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,9 +1048,9 @@ bool Config::check (void)
inputFile = "testvectors/diagnostic/input.json";
}

if (hashDBSingleton && (databaseURL != "local"))
if (!hashDBSingleton && (databaseURL != "local"))
{
zklog.error("hashDBSingleton=true but databaseURL!=local");
zklog.error("hashDBSingleton=false but databaseURL!=local");
bError = true;
}

Expand Down
9 changes: 9 additions & 0 deletions src/main_sm/fork_1/main/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ffiasm/fnec.hpp"
#include "prover_request.hpp"
#include "hashdb_interface.hpp"
#include "hashdb_factory.hpp"

using namespace std;
using json = nlohmann::json;
Expand Down Expand Up @@ -139,6 +140,14 @@ class Context
lastStep(0)
{}; // Constructor, setting references

~Context()
{
if (!config.hashDBSingleton)
{
HashDBClientFactory::freeHashDBClient(pHashDB);
}
}

// Evaluations data
uint64_t * pZKPC; // Zero-knowledge program counter
uint64_t * pStep; // Iteration, instruction execution loop counter, polynomial evaluation counter
Expand Down
37 changes: 32 additions & 5 deletions src/main_sm/fork_1/main/main_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,18 @@ MainExecutor::MainExecutor (Goldilocks &fr, PoseidonGoldilocks &poseidon, const
pthread_mutex_init(&flushMutex, NULL);

/* Get a HashDBInterface interface, according to the configuration */
pHashDB = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDB == NULL)
if (config.hashDBSingleton)
{
zklog.error("MainExecutor::MainExecutor() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
pHashDSingleton = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDSingleton == NULL)
{
zklog.error("MainExecutor::MainExecutor() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
}
}
else
{
pHashDSingleton = NULL;
}

TimerStopAndLog(ROM_LOAD);
Expand All @@ -114,7 +121,11 @@ MainExecutor::~MainExecutor ()
}
flushUnlock();

HashDBClientFactory::freeHashDBClient(pHashDB);
if (config.hashDBSingleton)
{
zkassertpermanent(pHashDSingleton != NULL);
HashDBClientFactory::freeHashDBClient(pHashDSingleton);
}

TimerStopAndLog(MAIN_EXECUTOR_DESTRUCTOR_fork_1);
}
Expand Down Expand Up @@ -142,6 +153,22 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,
return;
}

// Get a HashDB interface
HashDBInterface *pHashDB;
if (config.hashDBSingleton)
{
pHashDB = pHashDSingleton;
}
else
{
pHashDB = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDB == NULL)
{
zklog.error("MainExecutor::execute() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
}
}

// Create context and store a finite field reference in it
Context ctx(fr, config, fec, fnec, pols, rom, proverRequest, pHashDB);

Expand Down
4 changes: 2 additions & 2 deletions src/main_sm/fork_1/main/main_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class MainExecutor {
uint64_t finalizeExecutionLabel;
uint64_t checkAndSaveFromLabel;

// HashDB
HashDBInterface *pHashDB;
// HashDB singleton
HashDBInterface *pHashDSingleton;

// When we reach this zkPC, state root (SR) will be consolidated (from virtual to real state root)
const uint64_t consolidateStateRootZKPC = 4835;
Expand Down
9 changes: 9 additions & 0 deletions src/main_sm/fork_10/main/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ffiasm/fnec.hpp"
#include "prover_request.hpp"
#include "hashdb_interface.hpp"
#include "hashdb_factory.hpp"

using namespace std;
using json = nlohmann::json;
Expand Down Expand Up @@ -225,6 +226,14 @@ class Context
N(0),
forceMode384(false){}; // Constructor, setting references

~Context()
{
if (!config.hashDBSingleton)
{
HashDBClientFactory::freeHashDBClient(pHashDB);
}
}

// HashK database, used in Keccak-f hash instructions hashK, hashK1, hashKLen and hashKDigest
unordered_map< uint64_t, HashValue > hashK;

Expand Down
37 changes: 32 additions & 5 deletions src/main_sm/fork_10/main/main_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,18 @@ MainExecutor::MainExecutor (Goldilocks &fr, PoseidonGoldilocks &poseidon, const
pthread_mutex_init(&labelsMutex, NULL);

/* Get a HashDBInterface interface, according to the configuration */
pHashDB = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDB == NULL)
if (config.hashDBSingleton)
{
zklog.error("MainExecutor::MainExecutor() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
pHashDSingleton = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDSingleton == NULL)
{
zklog.error("MainExecutor::MainExecutor() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
}
}
else
{
pHashDSingleton = NULL;
}

TimerStopAndLog(ROM_LOAD);
Expand All @@ -132,7 +139,11 @@ MainExecutor::~MainExecutor ()
{
TimerStart(MAIN_EXECUTOR_DESTRUCTOR_fork_10);

HashDBClientFactory::freeHashDBClient(pHashDB);
if (config.hashDBSingleton)
{
zkassertpermanent(pHashDSingleton != NULL);
HashDBClientFactory::freeHashDBClient(pHashDSingleton);
}

TimerStopAndLog(MAIN_EXECUTOR_DESTRUCTOR_fork_10);
}
Expand Down Expand Up @@ -196,6 +207,22 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,
return;
}

// Get a HashDB interface
HashDBInterface *pHashDB;
if (config.hashDBSingleton)
{
pHashDB = pHashDSingleton;
}
else
{
pHashDB = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDB == NULL)
{
zklog.error("MainExecutor::execute() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
}
}

// Create context and store a finite field reference in it
Context ctx(fr, config, fec, fnec, pols, rom, proverRequest, pHashDB);

Expand Down
4 changes: 2 additions & 2 deletions src/main_sm/fork_10/main/main_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class MainExecutor {
// Labels lock
pthread_mutex_t labelsMutex; // Mutex to protect the labels vector

// HashDB
HashDBInterface *pHashDB;
// HashDB singleton
HashDBInterface *pHashDSingleton;

// When we reach this zkPC, state root (SR) will be consolidated (from virtual to real state root)
const uint64_t consolidateStateRootZKPC = 4928;
Expand Down
9 changes: 9 additions & 0 deletions src/main_sm/fork_10_blob/main/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ffiasm/fnec.hpp"
#include "prover_request.hpp"
#include "hashdb_interface.hpp"
#include "hashdb_factory.hpp"

using namespace std;
using json = nlohmann::json;
Expand Down Expand Up @@ -226,6 +227,14 @@ class Context
N(0),
forceMode384(false){}; // Constructor, setting references

~Context()
{
if (!config.hashDBSingleton)
{
HashDBClientFactory::freeHashDBClient(pHashDB);
}
}

// HashK database, used in Keccak-f hash instructions hashK, hashK1, hashKLen and hashKDigest
unordered_map< uint64_t, HashValue > hashK;

Expand Down
37 changes: 32 additions & 5 deletions src/main_sm/fork_10_blob/main/main_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,18 @@ MainExecutor::MainExecutor (Goldilocks &fr, PoseidonGoldilocks &poseidon, const
pthread_mutex_init(&labelsMutex, NULL);

/* Get a HashDBInterface interface, according to the configuration */
pHashDB = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDB == NULL)
if (config.hashDBSingleton)
{
zklog.error("MainExecutor::MainExecutor() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
pHashDSingleton = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDSingleton == NULL)
{
zklog.error("MainExecutor::MainExecutor() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
}
}
else
{
pHashDSingleton = NULL;
}

TimerStopAndLog(ROM_LOAD);
Expand All @@ -132,7 +139,11 @@ MainExecutor::~MainExecutor ()
{
TimerStart(MAIN_EXECUTOR_DESTRUCTOR_fork_10_blob);

HashDBClientFactory::freeHashDBClient(pHashDB);
if (config.hashDBSingleton)
{
zkassertpermanent(pHashDSingleton != NULL);
HashDBClientFactory::freeHashDBClient(pHashDSingleton);
}

TimerStopAndLog(MAIN_EXECUTOR_DESTRUCTOR_fork_10_blob);
}
Expand Down Expand Up @@ -196,6 +207,22 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,
return;
}

// Get a HashDB interface
HashDBInterface *pHashDB;
if (config.hashDBSingleton)
{
pHashDB = pHashDSingleton;
}
else
{
pHashDB = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDB == NULL)
{
zklog.error("MainExecutor::execute() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
}
}

// Create context and store a finite field reference in it
Context ctx(fr, config, fec, fnec, pols, rom, proverRequest, pHashDB);

Expand Down
4 changes: 2 additions & 2 deletions src/main_sm/fork_10_blob/main/main_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class MainExecutor {
// Labels lock
pthread_mutex_t labelsMutex; // Mutex to protect the labels vector

// HashDB
HashDBInterface *pHashDB;
// HashDB singleton
HashDBInterface *pHashDSingleton;

// When we reach this zkPC, state root (SR) will be consolidated (from virtual to real state root)
const uint64_t consolidateStateRootZKPC = 4928;
Expand Down
9 changes: 9 additions & 0 deletions src/main_sm/fork_2/main/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ffiasm/fnec.hpp"
#include "prover_request.hpp"
#include "hashdb_interface.hpp"
#include "hashdb_factory.hpp"

using namespace std;
using json = nlohmann::json;
Expand Down Expand Up @@ -139,6 +140,14 @@ class Context
lastStep(0)
{}; // Constructor, setting references

~Context()
{
if (!config.hashDBSingleton)
{
HashDBClientFactory::freeHashDBClient(pHashDB);
}
}

// Evaluations data
uint64_t * pZKPC; // Zero-knowledge program counter
uint64_t * pStep; // Iteration, instruction execution loop counter, polynomial evaluation counter
Expand Down
37 changes: 32 additions & 5 deletions src/main_sm/fork_2/main/main_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,18 @@ MainExecutor::MainExecutor (Goldilocks &fr, PoseidonGoldilocks &poseidon, const
opcodeAddressInit(romJson["labels"]);

/* Get a HashDBInterface interface, according to the configuration */
pHashDB = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDB == NULL)
if (config.hashDBSingleton)
{
zklog.error("MainExecutor::MainExecutor() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
pHashDSingleton = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDSingleton == NULL)
{
zklog.error("MainExecutor::MainExecutor() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
}
}
else
{
pHashDSingleton = NULL;
}

TimerStopAndLog(ROM_LOAD);
Expand All @@ -105,7 +112,11 @@ MainExecutor::~MainExecutor ()
{
TimerStart(MAIN_EXECUTOR_DESTRUCTOR_fork_2);

HashDBClientFactory::freeHashDBClient(pHashDB);
if (config.hashDBSingleton)
{
zkassertpermanent(pHashDSingleton != NULL);
HashDBClientFactory::freeHashDBClient(pHashDSingleton);
}

TimerStopAndLog(MAIN_EXECUTOR_DESTRUCTOR_fork_2);
}
Expand Down Expand Up @@ -133,6 +144,22 @@ void MainExecutor::execute (ProverRequest &proverRequest, MainCommitPols &pols,
return;
}

// Get a HashDB interface
HashDBInterface *pHashDB;
if (config.hashDBSingleton)
{
pHashDB = pHashDSingleton;
}
else
{
pHashDB = HashDBClientFactory::createHashDBClient(fr, config);
if (pHashDB == NULL)
{
zklog.error("MainExecutor::execute() failed calling HashDBClientFactory::createHashDBClient()");
exitProcess();
}
}

// Create context and store a finite field reference in it
Context ctx(fr, config, fec, fnec, pols, rom, proverRequest, pHashDB);

Expand Down
4 changes: 2 additions & 2 deletions src/main_sm/fork_2/main/main_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class MainExecutor {
uint64_t finalizeExecutionLabel;
uint64_t checkAndSaveFromLabel;

// HashDB
HashDBInterface *pHashDB;
// HashDB singleton
HashDBInterface *pHashDSingleton;

// When we reach this zkPC, state root (SR) will be consolidated (from virtual to real state root)
const uint64_t consolidateStateRootZKPC = 4854;
Expand Down
Loading

0 comments on commit ce144dd

Please sign in to comment.