1
0
Fork 0

simple block scheme

This commit is contained in:
Alexander Munch-Hansen 2019-12-01 15:13:08 +01:00
parent 71f71b299f
commit 929a01c92c
3 changed files with 71 additions and 16 deletions

View file

@ -29,6 +29,23 @@ public class Driver {
return res;
}
private static int[] simpleBlockScheme(int record) {
PIRSettings settings = new PIRSettings(8, 2, 2);
SimpleDatabase database = new SimpleDatabase(settings, new int[] {0, 0, 1, 1, 0, 0, 0, 0});
SimpleServer[] servers = new SimpleServer[settings.getNumServers()];
for (int i = 0; i < settings.getNumServers(); i++) {
servers[i] = new SimpleServer(database, settings);
}
SimpleClient client = new SimpleClient(settings, servers);
int[] res = client.receiveBits(record);
System.out.println("res: " + Arrays.toString(res));
return res;
}
private static int[] generalInterPolyTest(int index) {
PIRSettings settings = new PIRSettings(8, 3, 1);
@ -87,7 +104,7 @@ public class Driver {
public static void main(String[] args) {
// generalBlockInterPolyTestButBetter(1);
simpleScheme(2);
simpleBlockScheme(1);
/*
int sum = 0;
for (int i = 0; i < 1; i++) {

View file

@ -19,34 +19,61 @@ public class SimpleClient {
this.servers = servers;
}
public ArrayList<Integer> selectIndexes() {
ArrayList<Integer> indexes = new ArrayList<>();
public int[] selectIndexes() {
int[] indexes = new int[settings.getDatabaseSize()];
Random rand = new Random();
for (int i=0; i < settings.getDatabaseSize(); i++) {
if (rand.nextInt(2) == 1) {
indexes.add(i);
}
indexes[i] = rand.nextInt(2);
}
return indexes;
}
public int receiveBit(int index) {
ArrayList<Integer> S1 = selectIndexes();
ArrayList<Integer> S2 = (ArrayList<Integer>) S1.clone();
int[] S1 = selectIndexes();
int[] S2 = S1.clone();
if (S1.contains(index)) {
if (S1[index] == 1) {
// Remove the index, if it's contained in S.
S2.remove((Integer) index);
S2[index] = 0;
} else {
S2.add(index);
S2[index] = 1;
}
// TODO: Hardcoded, should be a loop
int resBit1 = this.servers[0].computeBit(S1);
int resBit2 = this.servers[1].computeBit(S2);
return ((resBit1 + resBit2) % 2);
}
public int[] receiveBits(int record) {
int[] result = new int[settings.getBlocksize()];
int[][] S1s = new int[settings.getBlocksize()][settings.getDatabaseSize()];
int[][] S2s = new int[settings.getBlocksize()][settings.getDatabaseSize()];
for (int i = 0; i < settings.getBlocksize(); i++) {
S1s[i] = selectIndexes();
S2s[i] = S1s[i].clone();
if (S1s[i][(record*settings.getBlocksize())+i] == 1) {
// Remove the index, if it's contained in S.
S2s[i][(record*settings.getBlocksize())+i] = 0;
} else {
S2s[i][(record*settings.getBlocksize())+i] = 1;
}
}
int[] resBit1 = this.servers[0].computeBits(S1s);
int[] resBit2 = this.servers[1].computeBits(S2s);
for (int i = 0; i < settings.getBlocksize(); i++) {
result[i] = (resBit1[i] + resBit2[i]) % 2;
}
return result;
}
}

View file

@ -16,11 +16,22 @@ public class SimpleServer {
this.x = database.getX();
}
public int computeBit(ArrayList<Integer> indexes) {
int res = x[indexes.get(0)];
for (int index : indexes.subList(1, indexes.size())) {
res = (res + x[index]) % 2;
public int computeBit(int[] indexes) {
int res = x[indexes[0]];
for (int i=1; i<indexes.length; i++) {
if (indexes[i] == 1)
res = (res + x[i]) % 2;
}
return res;
}
public int[] computeBits(int[][] indexes) {
int[] res = new int[settings.getBlocksize()];
for (int i = 0; i < settings.getBlocksize(); i++) {
res[i] = computeBit(indexes[i]);
}
return res;
}
}