bench_oram_readwrite.oc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <obliv.oh>
  2. #include "oram.oh"
  3. #include <copy.oh>
  4. #include "test_generic.h"
  5. static const char TESTNAME[] = "oram_readwrite_benchmark";
  6. #define TEXT_HELP_SUPPLEMENTARY "\
  7. -e \x1b[4mNUMBER\x1b[0m, --element-count=\x1b[4mNUMBER\x1b[0m \n\t\tuse ORAMs of \x1b[4mNUMBER\x1b[0m elements\n\n\
  8. -s \x1b[4mNUMBER\x1b[0m, --element-size=\x1b[4mNUMBER\x1b[0m \n\t\tuse ORAMs with elements containing \x1b[4mNUMBER\x1b[0m 32-bit words\n\n\
  9. -o \x1b[4mTYPE\x1b[0m, --oram-type=\x1b[4mTYPE\x1b[0m \n\t\tforce all ORAMs to be \x1b[4mTYPE\x1b[0m ORAMs. Valid types are \033[1msqrt\033[0m, \033[1mcircuit\033[0m, \033[1mfssl\033[0m, \033[1mfssl_cprg\033[0m, and \033[1mlinear\033[0m.\n\n\
  10. -i \x1b[4mNUMBER\x1b[0m, --samples=\x1b[4mNUMBER\x1b[0m \n\t\trun \x1b[4mNUMBER\x1b[0m iterations of the benchmark\n\n"
  11. static const char options_string[] = "e:s:o:i:";
  12. static struct option long_options[] = {
  13. {"element-count", required_argument, NULL, 'e'},
  14. {"element-size", required_argument, NULL, 's'},
  15. {"oram-type", required_argument, NULL, 'o'},
  16. {"samples", required_argument, NULL, 'i'},
  17. {0, 0, 0, 0}
  18. };
  19. char* get_test_name() {
  20. return TESTNAME;
  21. }
  22. char* get_supplementary_options_string() {
  23. return options_string;
  24. }
  25. struct option* get_long_options() {
  26. return long_options;
  27. }
  28. void print_supplementary_help() {
  29. fprintf(stderr, TEXT_HELP_SUPPLEMENTARY);
  30. }
  31. void test_main(void*varg) {
  32. #ifdef ORAM_OVERRIDE
  33. oram_set_default_type(ORAM_OVERRIDE);
  34. #endif
  35. size_t elct = 4;
  36. size_t elsz = 1;
  37. int samples = 1;
  38. args_t * args_pass = varg;
  39. int arg;
  40. optind = 0; // this allows us to getopt a second time
  41. while ((arg = getopt_long(args_pass->argc, args_pass->argv, options_string, long_options, NULL)) != -1) {
  42. if (arg == 'e') {
  43. elct = atoll(optarg);
  44. if (elct <= 0) {
  45. fprintf (stderr, "Argument for -%c must be positive.\n", arg);
  46. return;
  47. }
  48. } else if (arg == 's') {
  49. elsz = atoll(optarg);
  50. if (elsz <= 0) {
  51. fprintf (stderr, "Argument for -%c must be positive.\n", arg);
  52. return;
  53. }
  54. } else if (arg == 'o') {
  55. if (strcmp(optarg,"sqrt") == 0) {
  56. oram_set_default_type(ORAM_TYPE_SQRT);
  57. } else if (strcmp(optarg,"circuit") == 0) {
  58. oram_set_default_type(ORAM_TYPE_CIRCUIT);
  59. } else if (strcmp(optarg,"linear") == 0) {
  60. oram_set_default_type(ORAM_TYPE_LINEAR);
  61. } else if (strcmp(optarg,"fssl") == 0) {
  62. oram_set_default_type(ORAM_TYPE_FSSL);
  63. } else if (strcmp(optarg,"fssl_cprg") == 0) {
  64. oram_set_default_type(ORAM_TYPE_FSSL_CPRG);
  65. } else {
  66. fprintf (stderr, "Invalid argument for -%c.\n", arg);
  67. return;
  68. }
  69. } else if (arg == 'i') {
  70. samples = atoi(optarg);
  71. if (samples < 0) {
  72. fprintf (stderr, "Argument for -%c must be nonnegative.\n", arg);
  73. return;
  74. }
  75. } else if (arg == '?' || arg == ':') {
  76. if (optopt == 'e' || optopt == 's' || optopt == 'o' || optopt == 'i') {
  77. fprintf (stderr, "Option -%c requires an argument.\n", optopt);
  78. return;
  79. } else {
  80. fprintf (stderr, "Option -%c not recognized.\n", optopt);
  81. return;
  82. }
  83. } else {
  84. abort();
  85. }
  86. }
  87. OcCopy cpy = ocCopyIntN(elsz);
  88. uint64_t tally = 0;
  89. uint64_t tallygates = 0;
  90. uint64_t tallybytes = 0;
  91. obliv uint32_t * input = calloc(1, elsz * sizeof(obliv uint32_t));
  92. int64_t startuptime = -current_timestamp();
  93. int64_t startupgates = -yaoGateCount();
  94. int64_t startupbytes = -tcp2PBytesSent(ocCurrentProto());
  95. oram * o = oram_new(ORAM_TYPE_AUTO, &cpy, elct);
  96. startuptime += current_timestamp();
  97. startupgates += yaoGateCount();
  98. startupbytes += tcp2PBytesSent(ocCurrentProto());
  99. #ifndef ORAM_PROFILE_SCHEDULING
  100. fprintf(stdout, "# ORAM ACCESS (SETUP) (element count, element size, startup microseconds, startup gates, startup bytes)\n");
  101. fprintf(stdout, "%lld,%lld", elct, elsz);
  102. fprintf(stdout, ",%llu,%llu,%llu\n", startuptime,startupgates,startupbytes);
  103. fprintf(stdout, "# ORAM ACCESS (READ/WRITE) (element count, element size, sample 1 microseconds, sample 1 gates, sample 1 bytes, ...)\n");
  104. fprintf(stdout, "%lld,%lld", elct, elsz);
  105. fflush(stdout);
  106. #endif
  107. for (int ii = 0; ii < samples; ii++) {
  108. uint32_t index_raw = ocBroadcastInt(rand() % elct, 2);
  109. obliv uint32_t index = feedOblivInt(index_raw, 2);
  110. uint32_t rindex_raw = ocBroadcastInt(rand() % elct, 2);
  111. obliv uint32_t rindex = feedOblivInt(rindex_raw, 2);
  112. for (int kk = 0; kk < elsz; kk++) input[kk] = feedOblivInt(rand(), 1);
  113. int64_t runtime = -current_timestamp();
  114. int64_t rungates = -yaoGateCount();
  115. int64_t runbytes = -tcp2PBytesSent(ocCurrentProto());
  116. oram_write(o, input, index);
  117. oram_read(input, o, rindex);
  118. runtime += current_timestamp();
  119. rungates += yaoGateCount();
  120. runbytes += tcp2PBytesSent(ocCurrentProto());
  121. #ifndef ORAM_PROFILE_SCHEDULING
  122. fprintf(stdout, ",%llu,%llu,%llu", runtime,rungates, runbytes);
  123. fflush(stdout);
  124. #endif
  125. tally += runtime;
  126. tallygates += rungates;
  127. tallybytes += runbytes;
  128. }
  129. free(input);
  130. oram_free(o);
  131. #ifndef ORAM_PROFILE_SCHEDULING
  132. fprintf(stdout, "\n");
  133. #endif
  134. if (samples > 0) {
  135. fprintf(stderr, "ORAM Write (count:%lld, size: %lld): %llu microseconds avg, %llu gates avg, %llu bytes avg\n", elct, elsz, tally / samples, tallygates/samples, tallybytes/samples);
  136. }
  137. }