From 71f71b299f513383b599583ddcc794a1576ecb0b Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Sun, 1 Dec 2019 14:32:52 +0100 Subject: [PATCH] simple scheme --- pir/src/main/java/dk/au/pir/Driver.java | 25 ++++++++- .../au/pir/protocols/simple/SimpleClient.java | 52 +++++++++++++++++++ .../pir/protocols/simple/SimpleDatabase.java | 16 ++++++ .../au/pir/protocols/simple/SimpleServer.java | 26 ++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 pir/src/main/java/dk/au/pir/protocols/simple/SimpleClient.java create mode 100644 pir/src/main/java/dk/au/pir/protocols/simple/SimpleDatabase.java create mode 100644 pir/src/main/java/dk/au/pir/protocols/simple/SimpleServer.java diff --git a/pir/src/main/java/dk/au/pir/Driver.java b/pir/src/main/java/dk/au/pir/Driver.java index e4f514c..59ec071 100644 --- a/pir/src/main/java/dk/au/pir/Driver.java +++ b/pir/src/main/java/dk/au/pir/Driver.java @@ -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++) { diff --git a/pir/src/main/java/dk/au/pir/protocols/simple/SimpleClient.java b/pir/src/main/java/dk/au/pir/protocols/simple/SimpleClient.java new file mode 100644 index 0000000..ca7ebbe --- /dev/null +++ b/pir/src/main/java/dk/au/pir/protocols/simple/SimpleClient.java @@ -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 selectIndexes() { + ArrayList 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 S1 = selectIndexes(); + ArrayList S2 = (ArrayList) 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); + } +} diff --git a/pir/src/main/java/dk/au/pir/protocols/simple/SimpleDatabase.java b/pir/src/main/java/dk/au/pir/protocols/simple/SimpleDatabase.java new file mode 100644 index 0000000..e400b3d --- /dev/null +++ b/pir/src/main/java/dk/au/pir/protocols/simple/SimpleDatabase.java @@ -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; + } +} diff --git a/pir/src/main/java/dk/au/pir/protocols/simple/SimpleServer.java b/pir/src/main/java/dk/au/pir/protocols/simple/SimpleServer.java new file mode 100644 index 0000000..b2b1b2a --- /dev/null +++ b/pir/src/main/java/dk/au/pir/protocols/simple/SimpleServer.java @@ -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 indexes) { + int res = x[indexes.get(0)]; + for (int index : indexes.subList(1, indexes.size())) { + res = (res + x[index]) % 2; + } + return res; + } +}