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.

196 lines
4.7 KiB

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