You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.0 KiB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <iomanip>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/mman.h>
  9. #include <fcntl.h>
  10. #include <gmp.h>
  11. #include <unistd.h>
  12. #include <nlohmann/json.hpp>
  13. using json = nlohmann::json;
  14. #include "calcwit.h"
  15. #include "circom.h"
  16. #include "utils.h"
  17. #define handle_error(msg) \
  18. do { perror(msg); exit(EXIT_FAILURE); } while (0)
  19. void loadBin(Circom_CalcWit *ctx, std::string filename) {
  20. int fd;
  21. struct stat sb;
  22. // map input
  23. fd = open(filename.c_str(), O_RDONLY);
  24. if (fd == -1)
  25. handle_error("open");
  26. if (fstat(fd, &sb) == -1) /* To obtain file size */
  27. handle_error("fstat");
  28. u8 *in;
  29. in = (u8 *)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  30. if (in == MAP_FAILED)
  31. handle_error("mmap");
  32. close(fd);
  33. BigInt v;
  34. mpz_init2(v, 256);
  35. u8 *p = in;
  36. for (int i=0; i<_circuit.NInputs; i++) {
  37. int len = *(u8 *)p;
  38. p++;
  39. mpz_import(v,len , -1 , 1, 0, 0, p);
  40. p+=len;
  41. ctx->setSignal(0, 0, _circuit.wit2sig[1 + _circuit.NOutputs + i], &v);
  42. }
  43. }
  44. typedef void (*ItFunc)(Circom_CalcWit *ctx, int idx, json val);
  45. void iterateArr(Circom_CalcWit *ctx, int o, Circom_Sizes sizes, json jarr, ItFunc f) {
  46. if (!jarr.is_array()) {
  47. assert((sizes[0] == 1)&&(sizes[1] == 0));
  48. f(ctx, o, jarr);
  49. } else {
  50. int n = sizes[0] / sizes[1];
  51. for (int i=0; i<n; i++) {
  52. iterateArr(ctx, o + i*sizes[1], sizes+1, jarr[i], f);
  53. }
  54. }
  55. }
  56. void itFunc(Circom_CalcWit *ctx, int o, json val) {
  57. BigInt v;
  58. mpz_init2(v, 256);
  59. std::string s;
  60. if (val.is_string()) {
  61. s = val.get<std::string>();
  62. } else if (val.is_number()) {
  63. double vd = val.get<double>();
  64. std::stringstream stream;
  65. stream << std::fixed << std::setprecision(0) << vd;
  66. s = stream.str();
  67. } else {
  68. handle_error("Invalid JSON type");
  69. }
  70. mpz_set_str (v, s.c_str(), 10);
  71. ctx->setSignal(0, 0, o, &v);
  72. }
  73. void loadJson(Circom_CalcWit *ctx, std::string filename) {
  74. std::ifstream inStream(filename);
  75. json j;
  76. inStream >> j;
  77. for (json::iterator it = j.begin(); it != j.end(); ++it) {
  78. // std::cout << it.key() << " => " << it.value() << '\n';
  79. u64 h = fnv1a(it.key());
  80. int o;
  81. try {
  82. o = ctx->getSignalOffset(0, h);
  83. } catch (std::runtime_error e) {
  84. std::ostringstream errStrStream;
  85. errStrStream << "Error loadin variable: " << it.key() << "\n" << e.what();
  86. throw std::runtime_error(errStrStream.str() );
  87. }
  88. Circom_Sizes sizes = ctx->getSignalSizes(0, h);
  89. iterateArr(ctx, o, sizes, it.value(), itFunc);
  90. }
  91. }
  92. void writeOutBin(Circom_CalcWit *ctx, std::string filename) {
  93. FILE *write_ptr;
  94. write_ptr = fopen(filename.c_str(),"wb");
  95. BigInt v;
  96. mpz_init2(v, 256);
  97. u8 buffOut[256];
  98. for (int i=0;i<_circuit.NVars;i++) {
  99. size_t size=256;
  100. ctx->getWitness(i, &v);
  101. mpz_export(buffOut+1, &size, -1, 1, -1, 0, v);
  102. *buffOut = (u8)size;
  103. fwrite(buffOut, size+1, 1, write_ptr);
  104. }
  105. fclose(write_ptr);
  106. }
  107. void writeOutJson(Circom_CalcWit *ctx, std::string filename) {
  108. std::ofstream outFile;
  109. outFile.open (filename);
  110. outFile << "[\n";
  111. BigInt v;
  112. mpz_init2(v, 256);
  113. char pcV[256];
  114. for (int i=0;i<_circuit.NVars;i++) {
  115. ctx->getWitness(i, &v);
  116. mpz_get_str(pcV, 10, v);
  117. std::string sV = std::string(pcV);
  118. outFile << (i ? "," : " ") << "\"" << sV << "\"\n";
  119. }
  120. outFile << "]\n";
  121. outFile.close();
  122. }
  123. bool hasEnding (std::string const &fullString, std::string const &ending) {
  124. if (fullString.length() >= ending.length()) {
  125. return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
  126. } else {
  127. return false;
  128. }
  129. }
  130. int main(int argc, char *argv[]) {
  131. if (argc!=3) {
  132. std::string cl = argv[0];
  133. std::string base_filename = cl.substr(cl.find_last_of("/\\") + 1);
  134. std::cout << "Usage: " << base_filename << " <input.<bin|json>> <output.<bin|json>>\n";
  135. } else {
  136. // open output
  137. Circom_CalcWit *ctx = new Circom_CalcWit(&_circuit);
  138. std::string infilename = argv[1];
  139. if (hasEnding(infilename, std::string(".bin"))) {
  140. loadBin(ctx, infilename);
  141. } else if (hasEnding(infilename, std::string(".json"))) {
  142. loadJson(ctx, infilename);
  143. } else {
  144. handle_error("Invalid input extension (.bin / .json)");
  145. }
  146. ctx->join();
  147. std::string outfilename = argv[2];
  148. if (hasEnding(outfilename, std::string(".bin"))) {
  149. writeOutBin(ctx, outfilename);
  150. } else if (hasEnding(outfilename, std::string(".json"))) {
  151. writeOutJson(ctx, outfilename);
  152. } else {
  153. handle_error("Invalid output extension (.bin / .json)");
  154. }
  155. delete ctx;
  156. exit(EXIT_SUCCESS);
  157. }
  158. }