Percy++
A C++ implementation of Private Information Retrieval (PIR) protocols
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
itparams.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 __ITPARAMS_H__
20 #define __ITPARAMS_H__
21 
22 #include <iostream>
23 #include <NTL/ZZ_p.h>
24 #include <vector>
25 #ifdef SPIR_SUPPORT
26 #include <PolyCommitCommon.h>
27 #endif
28 #include "percytypes.h"
29 #include "percyparams.h"
30 
31 NTL_CLIENT
32 
33 class ZZ_pParams : public PercyParams {
34 public:
35  // Contructor for ZZ_p
36  ZZ_pParams (dbsize_t num_blocks, dbsize_t block_size, dbsize_t word_size,
37  ZZ modulus, nservers_t tau = 0, char * pcparams_file = NULL,
38  bool do_spir = false, dbsize_t virtual_block_size = 1);
39 
40  // Generate a new public/private key pair of the given keysize
41  ZZ_pParams (dbsize_t num_blocks, dbsize_t block_size, dbsize_t word_size,
42  unsigned long modulus_bits, nservers_t tau = 0,
43  dbsize_t virtual_block_size = 1);
44 
45  // Use the given factors to generate a public/private key pair
46  ZZ_pParams (dbsize_t num_blocks, dbsize_t block_size, dbsize_t word_size,
47  ZZ p, ZZ q, nservers_t tau = 0, dbsize_t virtual_block_size = 1);
48 
49  virtual ~ZZ_pParams ();
50 
51  unsigned long modulus_bytes() const {
52  return NumBytes(modulus);
53  }
54  unsigned long modulussq_bytes() const {
55  return NumBytes(modulus*modulus);
56  }
57  void mod_modulus() const {
58  modctx.restore();
59  }
60  void mod_modulussq() const {
61  modsqctx.restore();
62  }
63  const ZZ &get_modulus() const {
64  return modulus;
65  }
66  const ZZ_p &get_g() const {
67  return g;
68  }
69  bool hybrid() const {
70  return hybrid_protection;
71  }
72  bool spir() const {
73  return do_spir;
74  }
75  bool modulus_match(ZZ testmod) const {
76  return modulus == testmod;
77  }
78  char * get_pcparams_filename() const {
79  return pcparams_filename;
80  }
81 #ifdef SPIR_SUPPORT
82  PolyCommitParams * get_pcparamsp() const {
83  return pcparamsp;
84  }
85 #endif
86  virtual dbsize_t server_block_size () const {
87  return (_tau ? modulus_bytes() * words_per_block() : _block_size);
88  }
89  // Encrypt the given plaintext. The current ZZ_p context must be
90  // modsqctx.
91  ZZ_p encrypt(ZZ plaintext) const {
92  ZZ_p r;
93  random(r);
94  return power(g, plaintext) * power(r, modulus);
95  }
96  // Decrypt the given ciphertext. This routine will change the
97  // current ZZ_p context to modctx.
98  ZZ_p decrypt(ZZ_p ciphertext) const {
99  modsqctx.restore();
100  ZZ Lval = rep(power(ciphertext, lambda) - 1) / modulus;
101  modctx.restore();
102  ZZ_p ret = to_ZZ_p(Lval) * mu;
103  return ret;
104  }
105  ZZ get_p1() const { return p1; }
106  ZZ get_p2() const { return p2; }
107 
108  // Return the size of the request/response
109  virtual dbsize_t request_size (nqueries_t num_queries = 1) const;
110  virtual dbsize_t response_size (nqueries_t num_queries = 1) const;
111 
112  // For use in distributed computation
113  virtual std::vector<const PercyParams*> create_worker_params (
114  std::vector<Dimension> worker_dims) const;
115 
116 protected:
117  // Write the parameters to a stream to check compatibility
118  virtual void write (std::ostream &os) const;
119 
120  // Read the parameters from a stream (as written by write()) and check that
121  // they are compatible with these parameters.
122  virtual bool check_compatible (std::istream &is) const;
123 
124  void create_ZZ_pContexts();
125  bool hybrid_protection;
126  bool do_spir;
127  ZZ_pContext modctx, modsqctx;
128  // Paillier public key
129  ZZ modulus;
130  ZZ_p g; // mod modulus^2
131  char * pcparams_filename;
132 #ifdef SPIR_SUPPORT
133  PolyCommitParams * pcparamsp;
134 #endif
135 
136  void init_hybrid(ZZ p, ZZ q);
137  // Paillier private key
138  ZZ lambda;
139  ZZ_p mu; // mod modulus
140  ZZ p1, p2;
141 };
142 
143 
144 class GF2EParams : public PercyParams {
145 public:
146  GF2EParams (dbsize_t num_blocks, dbsize_t block_size,
147  dbsize_t word_size = 8, nservers_t tau = 0,
148  dbsize_t virtual_block_size = 1);
149 
150  virtual ~GF2EParams () {}
151 
152  // Return the size of the request/response
153  virtual dbsize_t request_size (nqueries_t num_queries = 1) const;
154  virtual dbsize_t response_size (nqueries_t num_queries = 1) const;
155 
156  // For use in distributed computation
157  virtual std::vector<const PercyParams*> create_worker_params (
158  std::vector<Dimension> worker_dims) const;
159 };
160 
161 
162 class ChorParams : public PercyParams {
163 public:
164  ChorParams (dbsize_t num_blocks, dbsize_t block_size,
165  dbsize_t virtual_block_size = 1);
166 
167  virtual ~ChorParams () {}
168 
169  // Return the size of the request/response
170  virtual dbsize_t request_size (nqueries_t num_queries = 1) const;
171  virtual dbsize_t response_size (nqueries_t num_queries = 1) const;
172 
173  // For use in distributed computation
174  virtual std::vector<const PercyParams*> create_worker_params (
175  std::vector<Dimension> worker_dims) const;
176 };
177 
178 #endif
nservers_t tau() const
Get the level of tau-independence.
Definition: percyparams.h:117
dbsize_t word_size() const
Get the word size used to split blocks.
Definition: percyparams.h:110
virtual dbsize_t request_size(nqueries_t num_queries=1) const
Get the size of a client to server request.
virtual std::vector< const PercyParams * > create_worker_params(std::vector< Dimension > worker_dims) const
Create protocol parameters for threads/workers.
virtual std::vector< const PercyParams * > create_worker_params(std::vector< Dimension > worker_dims) const
Create protocol parameters for threads/workers.
This header contains typedefs for seamless switching between 32- and 64-bit builds of Percy++...
virtual dbsize_t response_size(nqueries_t num_queries=1) const
Get the size of a server to client response.
virtual dbsize_t response_size(nqueries_t num_queries=1) const
Get the size of a server to client response.
dbsize_t _block_size
Size of database blocks in bytes.
Definition: percyparams.h:170
dbsize_t words_per_block() const
Get the number of words per database block.
Definition: percyparams.h:112
virtual dbsize_t response_size(nqueries_t num_queries=1) const
Get the size of a server to client response.
virtual void write(std::ostream &os) const
Write the parameters to a stream to check compatibility.
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
An abstract base class for a protocol's parameters.
Definition: percyparams.h:80
Definition: itparams.h:162
virtual dbsize_t request_size(nqueries_t num_queries=1) const
Get the size of a client to server request.
Definition: itparams.h:33
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...
virtual dbsize_t request_size(nqueries_t num_queries=1) const
Get the size of a client to server request.
virtual std::vector< const PercyParams * > create_worker_params(std::vector< Dimension > worker_dims) const
Create protocol parameters for threads/workers.
virtual dbsize_t server_block_size() const
Get the size of each block in the database that the datastore will actually use.
Definition: itparams.h:86
nservers_t _tau
Level of tau-independence.
Definition: percyparams.h:178
dbsize_t block_size() const
Get the size of each block in the database in bytes.
Definition: percyparams.h:104
Defines the basic structure of protocol parameters (PercyParams), client parameters (PercyClientParam...
Definition: itparams.h:144