arithmtest.cpp
Go to the documentation of this file.
1 
30 #include <cstdlib>
31 #include <cstring>
32 #include <iostream>
33 #include <fstream>
34 
35 #include "arithm.hpp"
36 
37 using namespace arithm;
38 
39 #define PATH_TO_INPUT_FILE "./input.txt"
40 #define PATH_TO_OUTPUT_FILE "./actual_output.txt"
41 #define STATISTICAL_ERROR 100
42 #define N_RAND_ARRAYS 3
43 
44 
56 void test_ModPGrp_toByteTree(std::ifstream &input,
57  std::ofstream &output)
58 {
59  verifierUtils::ByteTree * bt = NULL;
60  std::string line;
61  while (input>>line)
62  {
64  ModPGrp modpgrp(bt);
65  output<<modpgrp.toByteTree()->toString()<<std::endl;
66  }
67  free(bt);
68 }
69 
70 
78 void test_ModPGrp_exponentiation(std::ifstream &input,
79  std::ofstream &output)
80 {
81  mpz_class exp;
83  std::string line;
84  while (input>>line)
85  {
87  ModPGrp modpgrp(bt);
88 
90  Elmt elm = modpgrp.getElmt(bt);
91 
93  Elmt exp(bt->toInteger(), NULL);
94 
95  elm = modpgrp.exponentiation(elm,exp);
96  bt = new verifierUtils::Leaf(elm.getValue());
97 
98  output<<bt->toString()<<std::endl;
99  }
100  free(bt);
101 }
102 
103 
111 void test_ModPGrp_multiplication(std::ifstream &input,
112  std::ofstream &output)
113 {
115  std::string line;
116  while (input>>line)
117  {
119  ModPGrp modpgrp(bt);
120 
122  Elmt elm1 = modpgrp.getElmt(bt);
123 
125  Elmt elm2 = modpgrp.getElmt(bt);
126 
127  Elmt res = modpgrp.multiplication(elm1,elm2);
128  bt = new verifierUtils::Leaf(res.getValue());
129  output<<bt->toString()<<std::endl;
130  }
131  free(bt);
132 }
133 
134 
143  std::ifstream &input, std::ofstream &output)
144 {
146  std::vector<uint8_t> seed(32,0);
147  cryptoTools::SHAx * hash = new cryptoTools::SHA256();
148  cryptoTools::PRG * prg = new cryptoTools::PRG(hash);
149  std::string line;
150  while (input>>line)
151  {
152  // reading group
154  ModPGrp modpgrp(bt);
155 
156  // reading seed
157  for (unsigned int i=0; i<seed.size(); i++)
158  {
159  unsigned int read;
160  input>>read;
161  seed[i] = read;
162  }
163  prg->initialize(seed);
164  // generating an array in the ModPGrp using the seed
165  for (unsigned int i=0; i<N_RAND_ARRAYS; i++)
166  {
167  unsigned int size;
168  input>>size;
169  ArrayOfElmts arr = modpgrp.getRandArray(prg,STATISTICAL_ERROR,size);
170  for (unsigned int j=0; j<arr.size(); j++)
171  output<<arr.getElmt(j).toByteTree()->toString()<<std::endl;
172  }
173  }
174  free(bt);
175 }
176 
177 
185 void test_ModPGrp_product(std::ifstream &input,
186  std::ofstream &output)
187 {
189  std::string line;
190  while (input>>line)
191  {
193  ModPGrp modpgrp(bt);
194 
196  ArrayOfElmts ar = modpgrp.getArray(bt);
197 
198  bt = modpgrp.product(ar).toByteTree();
199  output<<bt->toString()<<std::endl;
200  }
201 }
202 
203 
211 void test_ModPGrp_expProduct(std::ifstream &input,
212  std::ofstream &output)
213 {
215  std::string line;
216  while (input>>line)
217  {
219  ModPGrp modpgrp(bt);
220  ModField modfield(bt->getChild(0)->toInteger());
221 
223  ArrayOfElmts ar = modpgrp.getArray(bt);
224 
226  ArrayOfElmts ex = modfield.getArray(bt);
227 
228  Elmt res = modfield.expProduct(ar,ex);
229  output<<res.toByteTree()->toString()<<std::endl;
230  }
231 }
232 
233 
238 void printHelp()
239 {
240  std::cout<<"Run ./arithm_test <function to test> and it will parse"
241  << "the"<<PATH_TO_INPUT_FILE<< "file to produce the "
242  <<PATH_TO_OUTPUT_FILE<<" file. If this is giberish to you,"
243  <<"you\nshould read the documentation ^^"<<"\n\n"
244  <<"Available functions are:"<<"\n"
245  <<" - ModPGrp.toByteTree\n"
246  <<" - ModPGrp.exponentiation\n"
247  <<" - ModPGrp.multiplication\n"
248  <<" - ModPGrp.getRandArray\n"
249  <<" - ModPGrp.product\n"
250  <<" - ModPGrp.expProduct\n"
251  <<"\n"<<std::endl;
252 }
253 
254 
255 int main(int argc, char ** argv)
256 {
257  if (argc<2)
258  printHelp();
259  else
260  {
261  std::ifstream input(PATH_TO_INPUT_FILE,
262  std::ifstream::in);
263  std::ofstream output(PATH_TO_OUTPUT_FILE);
264  if (strcmp(argv[1],"ModPGrp.toByteTree") == 0)
265  test_ModPGrp_toByteTree(input,output);
266  else if (strcmp(argv[1], "ModPGrp.exponentiation") == 0)
267  test_ModPGrp_exponentiation(input,output);
268  else if (strcmp(argv[1], "ModPGrp.multiplication") == 0)
269  test_ModPGrp_multiplication(input,output);
270  else if (strcmp(argv[1], "ModPGrp.getRandArray") == 0)
271  test_ModPGrp_getRandArray(input,output);
272  else if (strcmp(argv[1], "ModPGrp.product") == 0)
273  test_ModPGrp_product(input,output);
274  else if (strcmp(argv[1], "ModPGrp.expProduct") == 0)
275  test_ModPGrp_expProduct(input,output);
276  else
277  printHelp();
278  output.close();
279  }
280  return 0;
281 }