github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/StadiumForWaterCarver/src/WaterCarver.cpp (about)

     1  #include "WaterCarver.h"
     2  
     3  #include "CipherTable.h"
     4  #include "Functions.h"
     5  #include "Utils.h"
     6  #include "RemoteShuffler.h"
     7  #include "FakeZZ.h"
     8  #include "SchnorrProof.h"
     9  #include "Pedersen.h"
    10  #include <iostream>
    11  #include <thread>
    12  #include <chrono>
    13  #include <vector>
    14  
    15  extern G_q G;
    16  extern vector<long> num;
    17  static Mod_p G_h; //G_h is used for Pedersen Commitment
    18  static bool G_hIsInit = false;
    19  
    20  using namespace ::std;
    21  
    22  void init_G_h()
    23  {
    24      init();
    25      if (true == G_hIsInit)
    26      {
    27          return;
    28      }
    29      bool b = true;
    30      ZZ ran;
    31      Mod_p temp;
    32      while (b)
    33      {
    34          ran = RandomBnd(G.get_ord());
    35  
    36  #if USE_REAL_POINTS
    37          ZZ mod = G.get_mod();
    38          CurvePoint x;
    39          basepoint_scalarmult(x, ran);
    40          temp = Mod_p(x, mod);
    41  #else
    42          temp = G.get_gen().expo(to_ZZ(ran));
    43  #endif
    44          if (G.is_generator(temp))
    45          {
    46              G_h = temp;
    47              b = false;
    48          }
    49      }
    50      G_hIsInit = true;
    51      return;
    52  }
    53  
    54  //	MulMod(temp_1,b.get_u(),c.get_u(),mod);
    55  //a= Cipher_elg(temp_1,mod);
    56  
    57  void genCommitments(vector<Mod_p> *vec, Pedersen *ped, long N)
    58  {
    59      int i = 0;
    60      for (i = 0; i < N; i++)
    61      {
    62          ZZ x = RandomBnd(G.get_ord());
    63          ZZ r = RandomBnd(G.get_ord());
    64          // ZZ r2 = RandomBnd(G.get_ord());
    65          CurvePoint g;
    66          basepoint_scalarmult(g, x);
    67          //   Mod_p commit(g, G.get_mod());
    68          Mod_p commit = ped->commit(x, r);
    69          //cout<< "commit = " <<commit <<endl;
    70          (*vec)[i] = commit;
    71      }
    72      //std::cout << "The vector size =" << vec->size() << std::endl; //Todo:remove this line
    73      return;
    74  }
    75  
    76  void genRanNum(vector<ZZ> *ranNumVec, long N)
    77  {
    78      int i = 0;
    79      for (i = 0; i < N; i++)
    80      {
    81          ZZ x = RandomBnd(G.get_ord());
    82          (*ranNumVec)[i] = x;
    83      }
    84      return;
    85  }
    86  
    87  CipherTable *arrangeTable(vector<Mod_p> *commitVector, long m, long n)
    88  {
    89      //cout << "arrangeTable0 m = " << m << endl;
    90      init();
    91      vector<vector<Cipher_elg> *> *ciphers = new vector<vector<Cipher_elg> *>(m);
    92      //First generate vector<vector<Cipher_elg>* >* ciphers
    93      int i = 0;
    94      int j = 0;
    95      for (i = 0; i < m; i++)
    96      {
    97          vector<Cipher_elg> *v = new vector<Cipher_elg>(n);
    98          for (j = 0; j < n; j++)
    99          {
   100              Cipher_elg ce((*commitVector)[i * n + j]);
   101              (*v)[j] = ce;
   102          }
   103          (*ciphers)[i] = v;
   104      }
   105      //cout << "arrangeTable m = " << m << endl;
   106      CipherTable *ret = new CipherTable(ciphers, m);
   107      ret->set_dimentions(m, n);
   108      return ret;
   109  }
   110  
   111  void watercarver()
   112  {
   113      auto tstart = high_resolution_clock::now();
   114      //cout << "Hello, World!" << endl;
   115      //First generate m*n pedersen commitments
   116      //should allow input m*n numbers, like several options
   117      //should allow input g and h for pedersen
   118      init();
   119      init_G_h();
   120      srand((unsigned int)time(NULL));
   121  
   122      resetM_N(64, 64);
   123  
   124      long m = getM();
   125      long n = 64;
   126      long N = m * n;
   127  
   128      vector<Mod_p> *commitVector;
   129      commitVector = new vector<Mod_p>(N);
   130      Mod_p gen[2] = {G.get_gen(), G_h};
   131      //    char Gstr[32];
   132      //    G.get_gen().get_val().serialize(Gstr);
   133  
   134      Pedersen ped(gen, 2);
   135      ped.set_omega(4, 5, 6);
   136      //Generate N Pedersen commitments with same G and H and each with a random number
   137      genCommitments(commitVector, &ped, N);
   138  
   139      vector<ZZ> *ranNumVec;
   140      ranNumVec = new vector<ZZ>(N);
   141      genRanNum(ranNumVec, N);
   142      //cout << "arrangeTable00 m = " << m << endl;
   143      CipherTable *ciphers = arrangeTable(commitVector, m, n);
   144  
   145      //end from Utils.cpp
   146      time_t parse_start = time(NULL);
   147      // cout << "parsing input1" << endl;
   148      string shuffle_input(ciphers->encode_all_ciphers());
   149      //cout << "done parsing. " << time(NULL) - parse_start << endl;
   150  
   151      char *shuffled_ciphers;
   152      int shuffled_ciphers_len;
   153      char *proof;
   154      int proof_len;
   155      int *permutation;
   156      int permutation_len;
   157      char *public_randoms;
   158      int public_randoms_len;
   159      //cout << "G order:" << G.get_ord() << endl;
   160      //cout << "shuffle begins!" << endl;
   161      ElGammal *elgammal = (ElGammal *)create_pub_key_use_dero_H();
   162      //cout << "after create_pub_key" << endl;
   163      time_t shuffle_time = time(NULL);
   164      char *input = (char *)shuffle_input.c_str();
   165      auto pi = new vector<vector<vector<long> *> *>(m);
   166      Permutation::perm_matrix(pi, n, m);
   167      auto R = new vector<vector<ZZ> *>(m);
   168      Functions::randomEl(R, m, n);
   169      void *cached_shuffle = shuffle_internal(elgammal, input, shuffle_input.size(), m * n,
   170                                              &shuffled_ciphers, &shuffled_ciphers_len, &permutation, &permutation_len, pi, R);
   171      //cout << "shuffle is done! In " << time(NULL) - shuffle_time << endl;
   172  
   173      //cout << "prove begins!" << endl;
   174      tstart = high_resolution_clock::now();
   175      prove(cached_shuffle, &proof, &proof_len, &public_randoms, &public_randoms_len);
   176      auto tstop = high_resolution_clock::now();
   177      auto time_di = duration<double>(tstop - tstart).count();
   178      //cout << "To calculate the proof took " << time_di << " sec." << endl;
   179  
   180      //cout << "verify begins!" << endl;
   181      tstart = high_resolution_clock::now();
   182      int ret = verify(1, proof, proof_len, input, shuffle_input.size(), shuffled_ciphers, shuffled_ciphers_len, nullptr, 0);
   183      tstop = high_resolution_clock::now();
   184      time_di = duration<double>(tstop - tstart).count();
   185      //cout << "To calculate the verify took " << time_di << " sec." << endl;
   186      //cout << "ret = " << ret << endl;
   187  
   188      //end from Utils.cpp
   189  
   190      delete ciphers;
   191      delete commitVector;
   192      delete ranNumVec;
   193      //std::cout << "Mension Accomplish!" << std::endl;
   194  
   195      //Shuffle the commitments with given random numbers
   196      //and pass the permutation to the prover
   197  
   198      return;
   199  }
   200  
   201  void testPoint()
   202  {
   203      cout << "Hello, Point!" << endl;
   204      init();
   205  
   206      init_G_h();
   207      srand((unsigned int)time(NULL));
   208  
   209      long m = 2;
   210      long n = 2;
   211      long N = m * n;
   212  
   213      vector<Mod_p> *commitVector;
   214      commitVector = new vector<Mod_p>(N);
   215      Mod_p gen[2] = {G.get_gen(), G_h};
   216  
   217      Pedersen ped(gen, 2);
   218      ped.set_omega(4, 5, 6);
   219  
   220      //Generate N Pedersen commitments with same G and H and each with a random number
   221      genCommitments(commitVector, &ped, N);
   222      uint8_t buffer[32];
   223      uint8_t buffer2[32];
   224      cout << "test here before getval" << endl;
   225      CurvePoint cp = (*commitVector)[0].get_val();
   226      cout << "test here after getval" << endl;
   227      edgamal_compress_point(buffer, &(cp.P));
   228      cout << "test here after cp" << endl;
   229      int i = 0;
   230      cout << "buffer = ";
   231      while (i < 32)
   232      {
   233          cout << unsigned(buffer[i]);
   234          i++;
   235      }
   236      cout << endl;
   237      cout << "highest bit is " << unsigned(buffer[31]) << endl;
   238      edgamal_curve_point out;
   239      edgamal_decompress_point(&out, buffer);
   240      edgamal_compress_point(buffer2, &out);
   241      i = 0;
   242      cout << "buffer2 = ";
   243      while (i < 32)
   244      {
   245          cout << unsigned(buffer[i]);
   246          i++;
   247      }
   248      cout << endl;
   249      cout << "highest bit is " << unsigned(buffer[31]) << endl;
   250  
   251      cout << "another test!" << endl;
   252      char buffer3[128];
   253      cp.serialize(buffer3);
   254      CurvePoint recPt;
   255      recPt.deserialize(buffer3);
   256  
   257      delete commitVector;
   258      return;
   259  }