Skip to content

Commit

Permalink
Add a new function named witnesscalc_dat that takes the filename of a…
Browse files Browse the repository at this point in the history
… .dat file as an argument instead of a bytes buffer.
  • Loading branch information
olomix committed Feb 9, 2024
1 parent ff65b99 commit edf7961
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
44 changes: 44 additions & 0 deletions src/internal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef WITNESSCALC_INTERNAL_H
#define WITNESSCALC_INTERNAL_H

#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string>
#include <iostream>

class FileMapLoader
{
public:
explicit FileMapLoader(const std::string &datFileName)
{
int fd;
struct stat sb;

fd = open(datFileName.c_str(), O_RDONLY);
if (fd == -1) {
std::cout << ".dat file not found: " << datFileName << "\n";
throw std::system_error(errno, std::generic_category(), "open");
}

if (fstat(fd, &sb) == -1) { /* To obtain file size */
close(fd);
throw std::system_error(errno, std::generic_category(), "fstat");
}

size = sb.st_size;
buffer = (char*)mmap(NULL, size, PROT_READ , MAP_PRIVATE, fd, 0);
close(fd);
}

~FileMapLoader()
{
munmap(buffer, size);
}

char *buffer;
size_t size;
};

#endif //WITNESSCALC_INTERNAL_H
41 changes: 2 additions & 39 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,11 @@
#include <iostream>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <chrono>
#include "witnesscalc.h"

#include "internal.hpp"

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

class FileMapLoader
{
public:
FileMapLoader(std::string const &datFileName)
{
int fd;
struct stat sb;

fd = open(datFileName.c_str(), O_RDONLY);
if (fd == -1) {
std::cout << ".dat file not found: " << datFileName << "\n";
throw std::system_error(errno, std::generic_category(), "open");
}

if (fstat(fd, &sb) == -1) { /* To obtain file size */
throw std::system_error(errno, std::generic_category(), "fstat");
}

size = sb.st_size;
buffer = (char*)mmap(NULL, size, PROT_READ , MAP_PRIVATE, fd, 0);
close(fd);
}

~FileMapLoader()
{
munmap(buffer, size);
}

char *buffer;
size_t size;
};

void writeBinWitness(char *witnessBuffer, unsigned long witnessSize, std::string wtnsFileName)
{
FILE *write_ptr;
Expand Down Expand Up @@ -77,10 +41,9 @@ int main (int argc, char *argv[]) {
size_t witnessSize = sizeof(WitnessBuffer);
char errorMessage[256];

FileMapLoader datLoader(datfile);
FileMapLoader jsonLoader(jsonfile);

int error = CIRCUIT_NAME::witnesscalc(datLoader.buffer, datLoader.size,
int error = CIRCUIT_NAME::witnesscalc_dat(datfile.c_str(),
jsonLoader.buffer, jsonLoader.size,
WitnessBuffer, &witnessSize,
errorMessage, sizeof(errorMessage));
Expand Down
23 changes: 19 additions & 4 deletions src/witnesscalc.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "witnesscalc.h"
#include "internal.hpp"
#include "calcwit.hpp"
#include "circom.hpp"
#include "fr.hpp"
#include <nlohmann/json.hpp>
#include <sstream>
#include <memory>
Expand Down Expand Up @@ -85,7 +85,7 @@ Circom_Circuit* loadCircuit(const void *buffer, unsigned long buffer_size) {
templateInsId2IOSignalInfo1[index[i]] = p;
}
}
circuit->templateInsId2IOSignalInfo = move(templateInsId2IOSignalInfo1);
circuit->templateInsId2IOSignalInfo = std::move(templateInsId2IOSignalInfo1);

return circuit;
}
Expand Down Expand Up @@ -283,10 +283,10 @@ int witnesscalc(

loadJson(ctx.get(), json_buffer, json_size);

if (ctx.get()->getRemaingInputsToBeSet() != 0) {
if (ctx->getRemaingInputsToBeSet() != 0) {
std::stringstream stream;
stream << "Not all inputs have been set. Only "
<< get_main_input_signal_no()-ctx.get()->getRemaingInputsToBeSet()
<< get_main_input_signal_no()-ctx->getRemaingInputsToBeSet()
<< " out of " << get_main_input_signal_no();

strncpy(error_msg, stream.str().c_str(), error_msg_maxsize);
Expand Down Expand Up @@ -321,4 +321,19 @@ int witnesscalc(
return WITNESSCALC_OK;
}

int witnesscalc_dat(
const char *dat_fname,
const char *json_buffer, unsigned long json_size,
char *wtns_buffer, unsigned long *wtns_size,
char *error_msg, unsigned long error_msg_maxsize)
{

std::string s(dat_fname);
FileMapLoader dat(dat_fname);
return witnesscalc(dat.buffer, dat.size, json_buffer,
json_size, wtns_buffer, wtns_size,
error_msg, error_msg_maxsize);
}


} // namespace
7 changes: 7 additions & 0 deletions src/witnesscalc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ witnesscalc(
char *wtns_buffer, unsigned long *wtns_size,
char *error_msg, unsigned long error_msg_maxsize);

int
witnesscalc_dat(
const char *dat_fname,
const char *json_buffer, unsigned long json_size,
char *wtns_buffer, unsigned long *wtns_size,
char *error_msg, unsigned long error_msg_maxsize);

} // namespace

#endif // WITNESSCALC_H

0 comments on commit edf7961

Please sign in to comment.