Explorar el Código

Start on the data types to construct a DPF

Ian Goldberg hace 1 año
padre
commit
81331536ac
Se han modificado 6 ficheros con 114 adiciones y 4 borrados
  1. 3 2
      Makefile
  2. 60 0
      bitutils.hpp
  3. 5 2
      preproc.cpp
  4. 22 0
      rdpf.cpp
  5. 15 0
      rdpf.hpp
  6. 9 0
      types.hpp

+ 3 - 2
Makefile

@@ -5,7 +5,7 @@ LDFLAGS=-ggdb
 LDLIBS=-lbsd -lboost_system -lboost_context -lboost_chrono -lboost_thread -lpthread
 
 BIN=prac
-SRCS=prac.cpp mpcio.cpp preproc.cpp online.cpp mpcops.cpp
+SRCS=prac.cpp mpcio.cpp preproc.cpp online.cpp mpcops.cpp rdpf.cpp
 OBJS=$(SRCS:.cpp=.o)
 
 $(BIN): $(OBJS)
@@ -25,6 +25,7 @@ depend:
 
 prac.o: mpcio.hpp types.hpp preproc.hpp online.hpp
 mpcio.o: mpcio.hpp types.hpp
-preproc.o: types.hpp coroutine.hpp mpcio.hpp preproc.hpp
+preproc.o: types.hpp coroutine.hpp mpcio.hpp preproc.hpp rdpf.hpp
 online.o: online.hpp mpcio.hpp types.hpp mpcops.hpp coroutine.hpp
 mpcops.o: mpcops.hpp types.hpp mpcio.hpp coroutine.hpp
+rdpf.o: rdpf.hpp mpcio.hpp types.hpp coroutine.hpp bitutils.hpp

+ 60 - 0
bitutils.hpp

@@ -0,0 +1,60 @@
+/* Adapted from preprocessing/bitutils.h from
+ * https://git-crysp.uwaterloo.ca/avadapal/duoram by Adithya Vadapalli,
+ * itself adapted from code by Ryan Henry */
+
+#ifndef __BITUTILS_HPP__
+#define __BITUTILS_HPP__
+
+#include <x86intrin.h>  // SSE and AVX intrinsics
+
+static const __m128i bool128_mask[2] = {
+    _mm_set_epi64x(0,1),                                        // 0b00...0001
+    _mm_set_epi64x(1,0)                                         // 0b00...0001 << 64
+};
+
+static const __m128i lsb128_mask[4] = {
+    _mm_setzero_si128(),                                        // 0b00...0000
+    _mm_set_epi64x(0,1),                                        // 0b00...0001
+    _mm_set_epi64x(0,2),                                        // 0b00...0010
+    _mm_set_epi64x(0,3)                                         // 0b00...0011
+};
+
+static const __m128i lsb128_mask_inv[4] = {
+    _mm_set1_epi8(-1),                                          // 0b11...1111
+    _mm_set_epi64x(-1,-2),                                      // 0b11...1110
+    _mm_set_epi64x(-1,-3),                                      // 0b11...1101
+    _mm_set_epi64x(-1,-4)                                       // 0b11...1100
+};
+
+static const __m128i if128_mask[2] = {
+    _mm_setzero_si128(),                                        // 0b00...0000
+    _mm_set1_epi8(-1)                                           // 0b11...1111
+};
+
+inline __m128i xor_if(const __m128i & block1, const __m128i & block2, __m128i flag)
+{
+    return _mm_xor_si128(block1, _mm_and_si128(block2, flag));
+}
+
+inline __m128i xor_if(const __m128i & block1, const __m128i & block2, bool flag)
+{
+    return _mm_xor_si128(block1, _mm_and_si128(block2, if128_mask[flag ? 1 : 0]));
+}
+
+inline uint8_t get_lsb(const __m128i & block, uint8_t bits = 0b01)
+{
+    __m128i vcmp = _mm_xor_si128(_mm_and_si128(block, lsb128_mask[bits]), lsb128_mask[bits]);
+    return static_cast<uint8_t>(_mm_testz_si128(vcmp, vcmp));
+}
+
+inline __m128i clear_lsb(const __m128i & block, uint8_t bits = 0b01)
+{
+    return _mm_and_si128(block, lsb128_mask_inv[bits]);
+}
+
+inline __m128i set_lsb(const __m128i & block, const bool val = true)
+{
+    return _mm_or_si128(clear_lsb(block, 0b01), lsb128_mask[val ? 0b01 : 0b00]);
+}
+
+#endif

+ 5 - 2
preproc.cpp

@@ -3,6 +3,7 @@
 #include "types.hpp"
 #include "coroutine.hpp"
 #include "preproc.hpp"
+#include "rdpf.hpp"
 
 // Open a file for writing with name the given prefix, and ".pX.tY"
 // suffix, where X is the (one-digit) player number and Y is the thread
@@ -80,7 +81,8 @@ void preprocessing_comp(MPCIO &mpcio, int num_threads, char **args)
                     for (unsigned int i=0; i<num; ++i) {
                         coroutines.emplace_back(
                             [&](yield_t &yield) {
-                                //rdpf_gen(stio, yield, depth);
+                                RDPF rdpf;
+                                rdpf_gen(tio, yield, rdpf, type);
                             });
                     }
                 }
@@ -162,7 +164,8 @@ void preprocessing_server(MPCServerIO &mpcsrvio, int num_threads, char **args)
                         for (unsigned int i=0; i<num; ++i) {
                             coroutines.emplace_back(
                                 [&](yield_t &yield) {
-                                    //rdpf_gen(stio, yield, depth);
+                                    RDPF rdpf;
+                                    rdpf_gen(stio, yield, rdpf, depth);
                                 });
                         }
                     }

+ 22 - 0
rdpf.cpp

@@ -0,0 +1,22 @@
+#include <bsd/stdlib.h> // arc4random_buf
+
+#include "rdpf.hpp"
+#include "bitutils.hpp"
+
+// Construct a DPF of the given depth to be used for random-access
+// memory reads and writes.  The DPF is construction collaboratively by
+// P0 and P1, with the server P2 helping by providing various kinds of
+// correlated randomness, such as MultTriples and AndTriples.
+void rdpf_gen(MPCTIO &tio, yield_t &yield,
+    RDPF &rdpf, nbits_t depth)
+{
+    int player = tio.player();
+
+    // Choose a random seed
+    DPFnode seed;
+    arc4random_buf(&seed, sizeof(seed));
+    // Ensure the flag bits (the lsb of each node) are different
+    seed = set_lsb(seed, !!player);
+    for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&seed)[15-i]); } printf("\n");
+    rdpf.seed = seed;
+}

+ 15 - 0
rdpf.hpp

@@ -0,0 +1,15 @@
+#ifndef __RDPF_HPP__
+#define __RDPF_HPP__
+
+#include "mpcio.hpp"
+#include "coroutine.hpp"
+#include "types.hpp"
+
+// Construct a DPF of the given depth to be used for random-access
+// memory reads and writes.  The DPF is construction collaboratively by
+// P0 and P1, with the server P2 helping by providing various kinds of
+// correlated randomness, such as MultTriples and AndTriples.
+void rdpf_gen(MPCTIO &tio, yield_t &yield,
+    RDPF &rdpf, nbits_t depth);
+
+#endif

+ 9 - 0
types.hpp

@@ -3,6 +3,7 @@
 
 #include <tuple>
 #include <cstdint>
+#include <x86intrin.h>  // SSE and AVX intrinsics
 #include <bsd/stdlib.h> // arc4random_buf
 
 // The number of bits in an MPC secret-shared memory word
@@ -177,4 +178,12 @@ using MultTriple = std::tuple<value_t, value_t, value_t>;
 
 using HalfTriple = std::tuple<value_t, value_t>;
 
+// The type of nodes in a DPF
+
+using DPFnode = __m128i;
+
+struct RDPF {
+    DPFnode seed;
+};
+
 #endif