simple scheme

This commit is contained in:
Alexander Munch-Hansen 2019-12-01 14:32:52 +01:00
parent 4ec05cf83a
commit 71f71b299f
4 changed files with 118 additions and 1 deletions

View File

@ -3,11 +3,33 @@ package dk.au.pir;
import dk.au.pir.protocols.interpoly.InterPolyClient;
import dk.au.pir.protocols.interpoly.InterPolyDatabase;
import dk.au.pir.protocols.interpoly.InterPolyServer;
import dk.au.pir.protocols.simple.SimpleClient;
import dk.au.pir.protocols.simple.SimpleDatabase;
import dk.au.pir.protocols.simple.SimpleServer;
import dk.au.pir.settings.PIRSettings;
import java.util.Arrays;
public class Driver {
private static int simpleScheme(int index) {
PIRSettings settings = new PIRSettings(8, 2, 1);
SimpleDatabase database = new SimpleDatabase(settings, new int[] {0, 0, 1, 0, 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.receiveBit(index);
System.out.println("res: " + res);
return res;
}
private static int[] generalInterPolyTest(int index) {
PIRSettings settings = new PIRSettings(8, 3, 1);
int s = settings.getS();
@ -64,7 +86,8 @@ public class Driver {
public static void main(String[] args) {
generalBlockInterPolyTestButBetter(1);
// generalBlockInterPolyTestButBetter(1);
simpleScheme(2);
/*
int sum = 0;
for (int i = 0; i < 1; i++) {

View File

@ -0,0 +1,52 @@
package dk.au.pir.protocols.simple;
import dk.au.pir.protocols.simple.SimpleServer;
import dk.au.pir.settings.PIRSettings;
import java.util.ArrayList;
import java.util.Random;
import static dk.au.pir.utils.ProtocolUtils.printIntArrayArray;
public class SimpleClient {
private final PIRSettings settings;
private final SimpleServer[] servers;
public SimpleClient(PIRSettings settings, SimpleServer[] servers) {
this.settings = settings;
this.servers = servers;
}
public ArrayList<Integer> selectIndexes() {
ArrayList<Integer> indexes = new ArrayList<>();
Random rand = new Random();
for (int i=0; i < settings.getDatabaseSize(); i++) {
if (rand.nextInt(2) == 1) {
indexes.add(i);
}
}
return indexes;
}
public int receiveBit(int index) {
ArrayList<Integer> S1 = selectIndexes();
ArrayList<Integer> S2 = (ArrayList<Integer>) S1.clone();
if (S1.contains(index)) {
// Remove the index, if it's contained in S.
S2.remove((Integer) index);
} else {
S2.add(index);
}
// TODO: Hardcoded, should be a loop
int resBit1 = this.servers[0].computeBit(S1);
int resBit2 = this.servers[1].computeBit(S2);
return ((resBit1 + resBit2) % 2);
}
}

View File

@ -0,0 +1,16 @@
package dk.au.pir.protocols.simple;
import dk.au.pir.settings.PIRSettings;
public class SimpleDatabase {
private final int[] x;
public SimpleDatabase(PIRSettings settings, int[] x) {
this.x = x;
}
public int[] getX() {
return x;
}
}

View File

@ -0,0 +1,26 @@
package dk.au.pir.protocols.simple;
import dk.au.pir.settings.PIRSettings;
import java.util.ArrayList;
public class SimpleServer {
private final SimpleDatabase database;
private final PIRSettings settings;
private final int[] x;
public SimpleServer(SimpleDatabase database, PIRSettings settings) {
this.database = database;
this.settings = settings;
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;
}
return res;
}
}