From edf79615b577bfbaf1a9dd13c1790f14518fe0db Mon Sep 17 00:00:00 2001 From: Oleg Lomaka Date: Fri, 9 Feb 2024 03:35:16 -0500 Subject: [PATCH 1/5] Add a new function named witnesscalc_dat that takes the filename of a .dat file as an argument instead of a bytes buffer. --- src/internal.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 41 ++--------------------------------------- src/witnesscalc.cpp | 23 +++++++++++++++++++---- src/witnesscalc.h | 7 +++++++ 4 files changed, 72 insertions(+), 43 deletions(-) create mode 100644 src/internal.hpp diff --git a/src/internal.hpp b/src/internal.hpp new file mode 100644 index 0000000..8b6574b --- /dev/null +++ b/src/internal.hpp @@ -0,0 +1,44 @@ +#ifndef WITNESSCALC_INTERNAL_H +#define WITNESSCALC_INTERNAL_H + +#include +#include +#include +#include +#include +#include + +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 diff --git a/src/main.cpp b/src/main.cpp index b82eac2..cda0be3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,47 +1,11 @@ #include -#include -#include -#include -#include #include #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; @@ -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)); diff --git a/src/witnesscalc.cpp b/src/witnesscalc.cpp index 70a212c..74805a9 100644 --- a/src/witnesscalc.cpp +++ b/src/witnesscalc.cpp @@ -1,7 +1,7 @@ #include "witnesscalc.h" +#include "internal.hpp" #include "calcwit.hpp" #include "circom.hpp" -#include "fr.hpp" #include #include #include @@ -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; } @@ -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); @@ -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 diff --git a/src/witnesscalc.h b/src/witnesscalc.h index 86c1124..9395b24 100644 --- a/src/witnesscalc.h +++ b/src/witnesscalc.h @@ -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 From ada845a80a4e39acc2e0cd2e315e482a46b393dd Mon Sep 17 00:00:00 2001 From: Oleg Lomaka Date: Fri, 9 Feb 2024 03:39:45 -0500 Subject: [PATCH 2/5] Rename function witnesscalc_dat to witnesscalc_from_dat_file and add some docs --- src/main.cpp | 2 +- src/witnesscalc.cpp | 2 +- src/witnesscalc.h | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cda0be3..6b80fa1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,7 +43,7 @@ int main (int argc, char *argv[]) { FileMapLoader jsonLoader(jsonfile); - int error = CIRCUIT_NAME::witnesscalc_dat(datfile.c_str(), + int error = CIRCUIT_NAME::witnesscalc_from_dat_file(datfile.c_str(), jsonLoader.buffer, jsonLoader.size, WitnessBuffer, &witnessSize, errorMessage, sizeof(errorMessage)); diff --git a/src/witnesscalc.cpp b/src/witnesscalc.cpp index 74805a9..2666288 100644 --- a/src/witnesscalc.cpp +++ b/src/witnesscalc.cpp @@ -321,7 +321,7 @@ int witnesscalc( return WITNESSCALC_OK; } -int witnesscalc_dat( +int witnesscalc_from_dat_file( const char *dat_fname, const char *json_buffer, unsigned long json_size, char *wtns_buffer, unsigned long *wtns_size, diff --git a/src/witnesscalc.h b/src/witnesscalc.h index 9395b24..d663cc9 100644 --- a/src/witnesscalc.h +++ b/src/witnesscalc.h @@ -28,8 +28,12 @@ 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_dat( +witnesscalc_from_dat_file( const char *dat_fname, const char *json_buffer, unsigned long json_size, char *wtns_buffer, unsigned long *wtns_size, From 616f303e1d60eedaa216ea06735027cedfdca399 Mon Sep 17 00:00:00 2001 From: Oleg Lomaka Date: Fri, 9 Feb 2024 05:09:29 -0500 Subject: [PATCH 3/5] fix circuits keys paths --- .github/workflows/build.yml | 2 +- run_tests.sh | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7da6275..40afd6c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -107,7 +107,7 @@ jobs: with: path: | circuits - key: circuits-ci-3.zip + key: circuits-ci-4.zip - name: Get circuits run: | diff --git a/run_tests.sh b/run_tests.sh index a20a25a..3d845f5 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -72,12 +72,12 @@ 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 @@ -85,12 +85,12 @@ 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 @@ -98,12 +98,12 @@ 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 @@ -111,11 +111,11 @@ 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 From a08cb786dd8f2ab729cae07a052915d294f92b09 Mon Sep 17 00:00:00 2001 From: Oleg Lomaka Date: Fri, 9 Feb 2024 05:59:21 -0500 Subject: [PATCH 4/5] Delete the copy and assignment constructors prevert copying of mamory mapped region pointer --- src/internal.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/internal.hpp b/src/internal.hpp index 8b6574b..f6b05ab 100644 --- a/src/internal.hpp +++ b/src/internal.hpp @@ -37,6 +37,9 @@ class 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; }; From 85adfc102ecae3641fb4dbfacc09a6349eed5387 Mon Sep 17 00:00:00 2001 From: Oleg Lomaka Date: Fri, 16 Feb 2024 08:27:39 -0500 Subject: [PATCH 5/5] Rename file internal.hpp to filemaploader.hpp --- src/{internal.hpp => filemaploader.hpp} | 0 src/main.cpp | 2 +- src/witnesscalc.cpp | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename src/{internal.hpp => filemaploader.hpp} (100%) diff --git a/src/internal.hpp b/src/filemaploader.hpp similarity index 100% rename from src/internal.hpp rename to src/filemaploader.hpp diff --git a/src/main.cpp b/src/main.cpp index 6b80fa1..5586eb9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include #include #include "witnesscalc.h" -#include "internal.hpp" +#include "filemaploader.hpp" #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) diff --git a/src/witnesscalc.cpp b/src/witnesscalc.cpp index 2666288..226a708 100644 --- a/src/witnesscalc.cpp +++ b/src/witnesscalc.cpp @@ -1,5 +1,5 @@ #include "witnesscalc.h" -#include "internal.hpp" +#include "filemaploader.hpp" #include "calcwit.hpp" #include "circom.hpp" #include