Percy++
A C++ implementation of Private Information Retrieval (PIR) protocols
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
agparams.h
1 // Percy++ Copyright 2007,2012,2013,2014
2 // Ian Goldberg <iang@cs.uwaterloo.ca>,
3 // Casey Devet <cjdevet@uwaterloo.ca>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of version 2 of the GNU General Public License as
7 // published by the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // There is a copy of the GNU General Public License in the COPYING file
15 // packaged with this plugin; if you cannot find it, write to the Free
16 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 // 02110-1301 USA
18 
19 #ifndef __AGPARAMS_H__
20 #define __AGPARAMS_H__
21 
22 #include "percyparams.h"
23 #include "recursiveparams.h"
24 
25 #ifdef NEED_UINT128
26 #include <NTL/ZZ.h>
27 
28 NTL_CLIENT
29 
30 typedef ZZ AG_Element;
31 
32 #define AGELT_TO_ULONG(x) (to_ulong(x))
33 #define AGELT_TO_UINT64(x) ( \
34  (uint64_t)(to_ulong(x)&0xFFFFFFFFUL) + \
35  ((uint64_t)(to_ulong(x>>32)&0xFFFFFFFFUL)<<32) )
36 inline AG_Element UINT64_TO_AGELT(uint64_t x) {
37  unsigned long lo = ((unsigned long)x)&0xFFFFFFFFUL;
38  unsigned long hi = (x>>32)&0xFFFFFFFFUL;
39  AG_Element r = to_ZZ(hi);
40  AG_Element loz = to_ZZ(lo);
41  r <<= 32;
42  r += loz;
43  return r;
44 }
45 inline void PERCY_WRITE_LE_AGELT(std::ostream &os, const AG_Element &elt) {
46  unsigned char buf[16];
47  BytesFromZZ(buf, elt, 16);
48  os.write((const char *)buf, 16);
49 }
50 inline void PERCY_READ_LE_AGELT(std::istream &is, AG_Element &elt) {
51  unsigned char buf[16];
52  is.read((char *)buf, 16);
53  ZZFromBytes(elt, buf, 16);
54 }
55 #define ULONG_TO_AGELT(x) (to_ZZ(x))
56 
57 #else
58 typedef __uint128_t AG_Element;
59 
60 #define AGELT_TO_ULONG(x) ((unsigned long)(x))
61 #define AGELT_TO_UINT64(x) ((uint64_t)(x))
62 #define UINT64_TO_AGELT(x) ((AG_Element)(x))
63 #define ULONG_TO_AGELT(x) ((AG_Element)(x))
64 inline void swap(AG_Element &a, AG_Element &b) {
65  AG_Element tmp = a;
66  a = b;
67  b = tmp;
68 }
69 #define PERCY_WRITE_LE_AGELT(os, elt) (os).write((char*)(&(elt)), 16)
70 #define PERCY_READ_LE_AGELT(is, elt) (is).read((char*)(&(elt)), 16)
71 #endif
72 
73 class AGParams : public PercyParams {
74 public:
75  AGParams(dbsize_t num_blocks, dbsize_t block_size, dbsize_t N = 50,
76  dbsize_t word_size = 20, dbsize_t virtual_block_size = 1);
77 
78  virtual ~AGParams () {}
79 
80  // Accessors
81  dbsize_t N () const { return _N; }
82  AG_Element p () const { return _p; }
83  AG_Element q () const { return _q; }
84  dbsize_t block_rows () const { return _block_rows; }
85 
86 /*
87  // Helpful methods
88  std::vector<dbsize_t> iteration_num_subsets () const { return iter_subsets; }
89  dbsize_t iteration_num_subsets (nqueries_t i) const { return iter_subsets[i]; }
90  dbsize_t iteration_subset_size (nqueries_t i) const { return iter_subset_sizes[i]; }
91 */
92 
93  // Return the size of the request/response
94  virtual dbsize_t request_size (nqueries_t num_queries = 1) const;
95  virtual dbsize_t response_size (nqueries_t num_queries = 1) const;
96 
97  // Prints the mode-specfic paramaters. Meant to be overloaded by
98  // mode-specific classes
99  virtual void print_mode_specific (std::ostream& os) const;
100 
101  // For use in distributed computation
102  virtual std::vector<const PercyParams*> create_worker_params (
103  std::vector<Dimension> worker_dims) const;
104 
105 protected:
106  // Write the parameters to a stream to check compatibility
107  virtual void write (std::ostream &os) const;
108 
109  // Read the parameters from a stream (as written by write()) and check that
110  // they are compatible with these parameters.
111  virtual bool check_compatible (std::istream &is) const;
112 
113  dbsize_t _N;
114  AG_Element _p;
115  AG_Element _q;
116  dbsize_t _block_rows;
117 };
118 
119 
121 public:
122  RecursiveAGParams (dbsize_t num_blocks, dbsize_t block_size,
123  nqueries_t depth = 0, dbsize_t N = 50, dbsize_t word_size = 20);
124 
125  virtual ~RecursiveAGParams ();
126 
127  // Accessors
128  dbsize_t N () const { return _N; }
129 
130  // Prints the mode-specfic paramaters. Meant to be overloaded by
131  // mode-specific classes
132  virtual void print_mode_specific (std::ostream& os) const;
133 
134 protected:
135  dbsize_t _N;
136 
137 private:
138  void init_iterations (std::vector<dbsize_t> iteration_num_blocks);
139 };
140 
141 #endif
dbsize_t word_size() const
Get the word size used to split blocks.
Definition: percyparams.h:110
Definition: recursiveparams.h:24
virtual void write(std::ostream &os) const
Write the parameters to a stream to check compatibility.
virtual void print_mode_specific(std::ostream &os) const
Print mode-specific parameters.
virtual std::vector< const PercyParams * > create_worker_params(std::vector< Dimension > worker_dims) const
Create protocol parameters for threads/workers.
dbsize_t virtual_block_size() const
Get the number of actual blocks in a virtual block when being used as one iteration of a recursive pr...
Definition: percyparams.h:123
dbsize_t num_blocks() const
Get the number of blocks in the database.
Definition: percyparams.h:102
virtual dbsize_t request_size(nqueries_t num_queries=1) const
Get the size of a client to server request.
virtual bool check_compatible(std::istream &is) const
Read the parameters from a stream (as written by write()) and check that they are compatible with the...
Definition: agparams.h:73
An abstract base class for a protocol's parameters.
Definition: percyparams.h:80
Definition: agparams.h:120
dbsize_t block_size() const
Get the size of each block in the database in bytes.
Definition: percyparams.h:104
virtual dbsize_t response_size(nqueries_t num_queries=1) const
Get the size of a server to client response.
Defines the basic structure of protocol parameters (PercyParams), client parameters (PercyClientParam...
virtual void print_mode_specific(std::ostream &os) const
Print mode-specific parameters.