utils.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. \file utils.h
  3. \author
  4. \copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
  5. Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. ABY 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 Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. \brief utils
  17. */
  18. #ifndef _UTILS_H__
  19. #define _UTILS_H__
  20. #include <cstdint>
  21. #include <gmp.h>
  22. #include <unistd.h>
  23. #ifdef WIN32
  24. #define SleepMiliSec(x) Sleep(x)
  25. #else
  26. #define SleepMiliSec(x) usleep((x)<<10)
  27. #endif
  28. #define two_pow(e) (((uint64_t) 1) << (e))
  29. #define pad_to_power_of_two(e) ( ((uint64_t) 1) << (ceil_log2(e)) )
  30. /*compute (a-b) mod (m+1) as: b > a ? (m) - (b-1) + a : a - b */
  31. #define MOD_SUB(a, b, m) (( ((b) > (a))? (m) - ((b) -1 ) + a : a - b))
  32. #define ceil_divide(x, y) (( ((x) + (y)-1)/(y)))
  33. #define bits_in_bytes(bits) (ceil_divide((bits), 8))
  34. #define pad_to_multiple(x, y) ( ceil_divide(x, y) * (y))
  35. #define PadToRegisterSize(x) (PadToMultiple(x, OTEXT_BLOCK_SIZE_BITS))
  36. #define PadToMultiple(x, y) ( ceil_divide(x, y) * (y))
  37. //TODO: this is bad, fix occurrences of ceil_log2 and replace by ceil_log2_min1 where log(1) = 1 is necessary. For all else use ceil_log2_real
  38. uint32_t ceil_log2(int bits);
  39. uint32_t ceil_log2_min1(int bits);
  40. uint32_t ceil_log2_real(int bits);
  41. uint32_t floor_log2(int bits);
  42. /**
  43. * returns a 4-byte value from dev/random
  44. */
  45. uint32_t aby_rand();
  46. /**
  47. * returns a random mpz_t with bitlen len generated from dev/urandom
  48. */
  49. void aby_prng(mpz_t rnd, mp_bitcnt_t len);
  50. #endif // _UTILS_H__