Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new function named witnesscalc_dat that takes the filename of a .dat file as an argument instead of a bytes buffer. #19

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
with:
path: |
circuits
key: circuits-ci-3.zip
key: circuits-ci-4.zip

- name: Get circuits
run: |
Expand Down
16 changes: 8 additions & 8 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,50 +72,50 @@ package/bin/credentialAtomicQueryV3 \
testdata/credentialAtomicQueryV3_Sig_input.json \
temp/credentialAtomicQueryV3_Sig_witness.wtns
snarkjs groth16 prove \
circuits/credentialAtomicQueryV3/circuit_final.zkey \
circuits/credentialAtomicQueryV3-beta.0/circuit_final.zkey \
temp/credentialAtomicQueryV3_Sig_witness.wtns \
temp/credentialAtomicQueryV3_Sig_proof.json \
temp/credentialAtomicQueryV3_Sig_public.json
snarkjs groth16 verify \
circuits/credentialAtomicQueryV3/verification_key.json \
circuits/credentialAtomicQueryV3-beta.0/verification_key.json \
temp/credentialAtomicQueryV3_Sig_public.json \
temp/credentialAtomicQueryV3_Sig_proof.json

package/bin/credentialAtomicQueryV3 \
testdata/credentialAtomicQueryV3_MTP_input.json \
temp/credentialAtomicQueryV3_MTP_witness.wtns
snarkjs groth16 prove \
circuits/credentialAtomicQueryV3/circuit_final.zkey \
circuits/credentialAtomicQueryV3-beta.0/circuit_final.zkey \
temp/credentialAtomicQueryV3_MTP_witness.wtns \
temp/credentialAtomicQueryV3_MTP_proof.json \
temp/credentialAtomicQueryV3_MTP_public.json
snarkjs groth16 verify \
circuits/credentialAtomicQueryV3/verification_key.json \
circuits/credentialAtomicQueryV3-beta.0/verification_key.json \
temp/credentialAtomicQueryV3_MTP_public.json \
temp/credentialAtomicQueryV3_MTP_proof.json

package/bin/credentialAtomicQueryV3OnChain \
testdata/credentialAtomicQueryV3OnChain_Sig_input.json \
temp/credentialAtomicQueryV3OnChain_Sig_witness.wtns
snarkjs groth16 prove \
circuits/credentialAtomicQueryV3OnChain/circuit_final.zkey \
circuits/credentialAtomicQueryV3OnChain-beta.0/circuit_final.zkey \
temp/credentialAtomicQueryV3OnChain_Sig_witness.wtns \
temp/credentialAtomicQueryV3OnChain_Sig_proof.json \
temp/credentialAtomicQueryV3OnChain_Sig_public.json
snarkjs groth16 verify \
circuits/credentialAtomicQueryV3OnChain/verification_key.json \
circuits/credentialAtomicQueryV3OnChain-beta.0/verification_key.json \
temp/credentialAtomicQueryV3OnChain_Sig_public.json \
temp/credentialAtomicQueryV3OnChain_Sig_proof.json

package/bin/credentialAtomicQueryV3OnChain \
testdata/credentialAtomicQueryV3OnChain_MTP_input.json \
temp/credentialAtomicQueryV3OnChain_MTP_witness.wtns
snarkjs groth16 prove \
circuits/credentialAtomicQueryV3OnChain/circuit_final.zkey \
circuits/credentialAtomicQueryV3OnChain-beta.0/circuit_final.zkey \
temp/credentialAtomicQueryV3OnChain_MTP_witness.wtns \
temp/credentialAtomicQueryV3OnChain_MTP_proof.json \
temp/credentialAtomicQueryV3OnChain_MTP_public.json
snarkjs groth16 verify \
circuits/credentialAtomicQueryV3OnChain/verification_key.json \
circuits/credentialAtomicQueryV3OnChain-beta.0/verification_key.json \
temp/credentialAtomicQueryV3OnChain_MTP_public.json \
temp/credentialAtomicQueryV3OnChain_MTP_proof.json
47 changes: 47 additions & 0 deletions src/internal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#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);
}

FileMapLoader(const FileMapLoader&) = delete; // Delete the copy constructor
FileMapLoader& operator=(const FileMapLoader&) = delete; // Delete the copy assignment operator

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"
olomix marked this conversation as resolved.
Show resolved Hide resolved

#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_from_dat_file(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_from_dat_file(
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
11 changes: 11 additions & 0 deletions src/witnesscalc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ witnesscalc(
char *wtns_buffer, unsigned long *wtns_size,
char *error_msg, unsigned long error_msg_maxsize);

/**
* A wrapper function for `witnesscalc` that takes the circuit as a .dat file
* name instead of a buffer.
*/
int
witnesscalc_from_dat_file(
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
Loading