check-correctness.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/bin/bash
  2. #/* Copyright (C) 2014 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  3. # * This file is part of XPIR.
  4. # *
  5. # * XPIR is free software: you can redistribute it and/or modify
  6. # * it under the terms of the GNU General Public License as published by
  7. # * the Free Software Foundation, either version 3 of the License, or
  8. # * (at your option) any later version.
  9. # *
  10. # * XPIR is distributed in the hope that it will be useful,
  11. # * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # * GNU General Public License for more details.
  14. # *
  15. # * You should have received a copy of the GNU General Public License
  16. # * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  17. #*/
  18. ########### CONSTANTS ##############
  19. MAX_REC=3
  20. MIN_ALPHA=0 # Set MIN and MAX to 1 to forbid aggregation
  21. MAX_ALPHA=0
  22. NO_REREAD=1
  23. NO_PIPELINE=0
  24. VERBOSE=0
  25. TEST_PAILLIER=0
  26. TEST_NOCRYPTOGRAPHY=1
  27. TEST_LWE=1
  28. REMOTE=0
  29. IP=169.254.4.46
  30. USER=marco
  31. ONE_KBIT=1024
  32. HUNDRED_KBIT=102400
  33. ONE_MBIT=1024000
  34. TEN_MBIT=10240000
  35. HUNDRED_MBIT=102400000
  36. ONE_GBIT=1024000000
  37. #files: 1kbits, 100kbits, 10mbits 1gbit
  38. #bases: 1Mbits, 10M, 100M, 1G 10G
  39. ########### SUBROUTINES ##############
  40. deal_with_options()
  41. {
  42. if [[ ( $NO_REREAD == 1 ) ]];
  43. then
  44. S_OPTION="-z"
  45. fi
  46. if [[ ( $NO_PIPELINE == 1 ) ]];
  47. then
  48. S_OPTION=$S_OPTION" --no-pipeline"
  49. C_OPTION="--no-pipeline"
  50. echo "Mode --no-pipeline selected"
  51. fi
  52. }
  53. do_a_test()
  54. {
  55. rm -f reception/* 2> /dev/null
  56. if [[ $VERBOSE == 2 ]]; then
  57. echo $BASE_DIR/../server/pir_server $S_OPTION
  58. echo $BASE_DIR/../client/pir_client -r $PARAM $C_OPTION $@ -c
  59. fi
  60. if [[ $N -le 1000 ]];
  61. then
  62. $BASE_DIR/../server/pir_server $S_OPTION > /tmp/checkpirserver.stdout 2>/tmp/checkpirserver.stderr &
  63. else
  64. $BASE_DIR/../server/pir_server $S_OPTION -s $N > /tmp/checkpirserver.stdout 2>/tmp/checkpirserver.stderr &
  65. fi
  66. PID=$!
  67. sleep 1
  68. $BASE_DIR/../client/pir_client -r $PARAM $C_OPTION $@ -c > /tmp/checkpirclient.stdout 2> /tmp/checkpirclient.stderr
  69. }
  70. exploit_results()
  71. {
  72. FILE_RETRIEVED=`ls reception`
  73. MD5_R1=`sha1sum reception/$FILE_RETRIEVED 2>/dev/null |cut -d\ -f1`
  74. MD5_DB=`dd if=db/test1 bs=1 count=$L_BYTE 2>/dev/null |sha1sum 2>/dev/null |cut -d\ -f1`
  75. if [[ ( $NO_REREAD -eq 1 ) ]];
  76. then
  77. # to check no-reread-database, do it a second time
  78. rm -f reception/* 2> /dev/null
  79. $BASE_DIR/../client/pir_client -r $PARAM $C_OPTION $@ -c >> /tmp/checkpirclient.stdout 2>> /tmp/checkpirclient.stderr
  80. MD5_R2=`sha1sum reception/* 2>/dev/null |cut -d\ -f1`
  81. if [[ $FILE_RETRIEVED != "" && ($MD5_DB == $MD5_R1) && ($MD5_DB == $MD5_R2) ]]; then
  82. CORRECT=1;
  83. else
  84. CORRECT=0;
  85. fi
  86. else
  87. if [[ $FILE_RETRIEVED != "" && ($MD5_DB == $MD5_R1) ]]; then
  88. CORRECT=1;
  89. else
  90. CORRECT=0;
  91. fi
  92. fi
  93. if [[ $CORRECT == 1 ]]; then
  94. echo -e "$DB:$L:$PARAM \033[32mCORRECT\033[m"
  95. else
  96. echo -e "$DB:$L:$PARAM \033[31m*************** NOT CORRECT **********\033[m"
  97. if [[ $VERBOSE -ge 1 ]]; then
  98. echo "Database : check.repo/db-$L_BYTE-$N"
  99. echo "Server : $BASE_DIR/../server/pir_server $S_OPTION"
  100. echo "Client : $BASE_DIR/../client/pir_client -r $PARAM $C_OPTION $@ -c "
  101. echo "*************** Server stdout **********"
  102. cat /tmp/checkpirserver.stdout
  103. echo "*************** Server stderr **********"
  104. cat /tmp/checkpirserver.stderr
  105. echo "*************** Client stdout **********"
  106. cat /tmp/checkpirclient.stdout
  107. echo "*************** Client stderr **********"
  108. cat /tmp/checkpirclient.stderr
  109. echo "hit <enter> to continue";read
  110. fi
  111. fi
  112. (kill $PID >/dev/null 2>/dev/null)
  113. # Notify when waiting for kill
  114. # Use ANSI escape sequences to stay on the same line
  115. while [[ `ps -ef|grep pir_server|wc -l` -ne 1 ]] ; do
  116. echo -e "Could not kill pir_server, waiting ..."
  117. echo -e "\033[2A"
  118. sleep 1
  119. done
  120. # Use ANSI escape sequences again to erase and reuse the line
  121. echo " "
  122. echo -e "\033[2A"
  123. rm -f /tmp/checkpir* >/dev/null 2>/dev/null
  124. }
  125. ########### MAIN ##############
  126. echo -e "##########################################################################"
  127. echo -e "This tool tests that pir_server and pir_client run correctly and that an"
  128. echo -e "element can be retrieved without errors. You should obtain CORRECT or "
  129. echo -e "\"Skipping test...\" for all tests. THE FIRST TEST CAN BE QUITE LONG if"
  130. echo -e "performance caches need to be built (first run for the server or client)"
  131. echo -e "##########################################################################"
  132. killall -9 pir_server >/dev/null 2>/dev/null; sleep 1
  133. # Notify when waiting for kill
  134. # Use ANSI escape sequences to stay on the same line
  135. while [[ `ps -ef|grep pir_server|wc -l` -ne 1 ]]; do
  136. echo -e "Could not kill pir_server, waiting ..."
  137. echo -e "\033[2A"
  138. sleep 1
  139. done
  140. # Use ANSI escape sequences again to erase and reuse the line
  141. echo " "
  142. echo -e "\033[2A"
  143. deal_with_options
  144. BASE_DIR=$PWD
  145. cd check.repo
  146. # Paillier tests only for small databases
  147. if [[ TEST_PAILLIER -eq 1 ]]; then
  148. echo -e "\nPaillier tests\n#################\n"
  149. for DB in $ONE_MBIT #$TEN_MBIT
  150. do
  151. for L in $ONE_KBIT $HUNDRED_KBIT $TEN_MBIT $ONE_GBIT
  152. do
  153. N=`python -c"print(int($DB / $L));"`
  154. L_BYTE=`python -c"print(int($L / 8 ));"`
  155. if [[ ( $DB -gt $L ) && ( -f db-$L_BYTE-$N/test1 ) ]];
  156. then
  157. rm -fr db
  158. mkdir reception 2> /dev/null
  159. mkdir exp 2> /dev/null
  160. ln -s db-$L_BYTE-$N db
  161. echo Checking db-$L_BYTE-$N
  162. for QP in "80:1024:2048"
  163. #for QP in "80:1024:2048:1016"
  164. do
  165. for REC in `eval echo {1..$MAX_REC}`
  166. do
  167. # TODO use alpha (aggregation does not work yet)
  168. for ALPHA in `eval echo {$MIN_ALPHA..$MAX_ALPHA}`
  169. do
  170. PARAM="Paillier:$QP --reclvl $REC --alpha $ALPHA "
  171. do_a_test
  172. exploit_results
  173. done
  174. done
  175. done
  176. fi
  177. done
  178. done
  179. fi
  180. echo -e "\n\nOther tests\n#################\n"
  181. for DB in $ONE_MBIT $TEN_MBIT $HUNDRED_MBIT $ONE_GBIT
  182. do
  183. for L in $ONE_KBIT $HUNDRED_KBIT $TEN_MBIT $ONE_GBIT
  184. do
  185. N=`python -c"print(int($DB / $L));"`
  186. L_BYTE=`python -c"print(int($L / 8) );"`
  187. DB_BYTE=`python -c"print(int($DB / 8));"`
  188. # If N <= 10000 a database with different files must exist
  189. # If not a database with a single file to split must exist
  190. if [[ ( ( $N -le 10000 ) && ( -f db-$L_BYTE-$N/test1 ) ) || ( $N -gt 1000 ) && ( -f db-$DB_BYTE/test1 ) ]];
  191. then
  192. rm -fr db
  193. mkdir reception 2> /dev/null
  194. mkdir exp 2> /dev/null
  195. if [[ $N -le 1000 ]];
  196. then
  197. ln -s db-$L_BYTE-$N db
  198. echo Checking db-$L_BYTE-$N
  199. else
  200. ln -s db-$DB_BYTE db
  201. echo Checking db-$DB_BYTE with split_value=$N
  202. fi
  203. if [[ TEST_NOCRYPTOGRAPHY -eq 1 ]]; then
  204. # Test No Cryptography
  205. PARAM="NoCryptography"
  206. do_a_test
  207. exploit_results
  208. fi
  209. # Test LWE
  210. for QP in "4096:180" "2048:120" "1024:60"
  211. #for QP in "180:73" "120:43" "60:13"
  212. do
  213. #TODO use python to compute absorption #math.floor(($Q-math.ceil(math.log($SEC/2,2))-math.ceil(math.log($N,2))-math.ceil(math.log($DEG,2)))/2)
  214. for REC in `eval echo {1..$MAX_REC}`
  215. do
  216. for ALPHA in `eval echo {$MIN_ALPHA..$MAX_ALPHA}`
  217. do
  218. if [[ TEST_LWE -eq 1 ]]; then
  219. PARAM="LWE:.*:$QP --reclvl $REC --alpha $ALPHA "
  220. CIPH_SIZE=`echo $QP|tr : \*`"*2"
  221. QUERY_SIZE="$CIPH_SIZE*$REC*$N.0**(1/$REC.0)"
  222. DB_FFT_SIZE="6*$DB"
  223. if [[ ( `python -c "print(($QUERY_SIZE+$DB_FFT_SIZE)/10**9 > 40);"` == "True" ) || ( `python -c "print(($CIPH_SIZE*$N+$QUERY_SIZE)/10**9 > 40);"` == "True" ) ]]; then
  224. echo "Skipping tests requiring more than 5Gbytes RAM"
  225. else
  226. do_a_test
  227. exploit_results
  228. fi
  229. fi
  230. done
  231. done
  232. done
  233. fi
  234. done
  235. done
  236. cd ..