Browse Source

Make a PRG API

Ian Goldberg 2 years ago
parent
commit
6fc956c2eb
5 changed files with 47 additions and 2 deletions
  1. 1 0
      Makefile
  2. 5 0
      aes.hpp
  3. 1 0
      bitutils.hpp
  4. 28 0
      prg.hpp
  5. 12 2
      rdpf.cpp

+ 1 - 0
Makefile

@@ -29,3 +29,4 @@ 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 aes.hpp
+rdpf.o: prg.hpp

+ 5 - 0
aes.hpp

@@ -1,3 +1,6 @@
+#ifndef __AES_HPP__
+#define __AES_HPP__
+
 /* Based on reference code from the Intel AES-NI whitepaper
  * http://www.intel.com/content/dam/doc/white-paper/advanced-encryption-standard-new-instructions-set-paper.pdf
  */
@@ -70,3 +73,5 @@ static inline void AES_ECB_encrypt(__m128i &ciphertext, __m128i plaintext, const
     tmp = _mm_aesenclast_si128 (tmp,key[j]);
     ciphertext=tmp;
 }
+
+#endif

+ 1 - 0
bitutils.hpp

@@ -5,6 +5,7 @@
 #ifndef __BITUTILS_HPP__
 #define __BITUTILS_HPP__
 
+#include <cstdint>
 #include <x86intrin.h>  // SSE and AVX intrinsics
 
 static const __m128i bool128_mask[2] = {

+ 28 - 0
prg.hpp

@@ -0,0 +1,28 @@
+#ifndef __PRG_HPP__
+#define __PRG_HPP__
+
+#include "bitutils.hpp"
+#include "aes.hpp"
+
+static const struct PRGkey {
+    AESkey k;
+    PRGkey(__m128i key = _mm_set_epi64x(314159265, 271828182)) {
+        AES_128_Key_Expansion(k, key);
+    }
+} prgkey;
+
+// Compute one of the children of node seed; whichchild=0 for
+// the left child, 1 for the right child
+static inline void prg(__m128i &out, __m128i seed, bool whichchild)
+{
+    AES_ECB_encrypt(out, set_lsb(seed, whichchild), prgkey.k);
+}
+
+// Compute both children of node seed
+static inline void prgboth(__m128i &left, __m128i &right, __m128i seed)
+{
+    AES_ECB_encrypt(left, set_lsb(seed, 0), prgkey.k);
+    AES_ECB_encrypt(right, set_lsb(seed, 1), prgkey.k);
+}
+
+#endif

+ 12 - 2
rdpf.cpp

@@ -3,6 +3,7 @@
 #include "rdpf.hpp"
 #include "bitutils.hpp"
 #include "aes.hpp"
+#include "prg.hpp"
 
 // Construct a DPF of the given depth to be used for random-access
 // memory reads and writes.  The DPF is construction collaboratively by
@@ -26,7 +27,16 @@ void rdpf_gen(MPCTIO &tio, yield_t &yield,
     AES_128_Key_Expansion(prgkey, key);
     __m128i left, right;
     AES_ECB_encrypt(left, set_lsb(seed, 0), prgkey);
-    printf("left: "); for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&left)[15-i]); } printf("\n");
     AES_ECB_encrypt(right, set_lsb(seed, 1), prgkey);
-    printf("rght: "); for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&right)[15-i]); } printf("\n");
+
+    __m128i nleft, nright, oleft, oright;
+    prg(nleft, seed, 0);
+    prg(nright, seed, 1);
+    prgboth(oleft, oright, seed);
+    printf("left : "); for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&left)[15-i]); } printf("\n");
+    printf("nleft: "); for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&nleft)[15-i]); } printf("\n");
+    printf("oleft: "); for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&oleft)[15-i]); } printf("\n");
+    printf("rght : "); for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&right)[15-i]); } printf("\n");
+    printf("nrght: "); for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&nright)[15-i]); } printf("\n");
+    printf("orght: "); for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&oright)[15-i]); } printf("\n");
 }