From 929a01c92c9cbf4b1879a1c1c7115d67bc2b5abe Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Sun, 1 Dec 2019 15:13:08 +0100 Subject: [PATCH] simple block scheme --- pir/src/main/java/dk/au/pir/Driver.java | 19 ++++++- .../au/pir/protocols/simple/SimpleClient.java | 49 ++++++++++++++----- .../au/pir/protocols/simple/SimpleServer.java | 19 +++++-- 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/pir/src/main/java/dk/au/pir/Driver.java b/pir/src/main/java/dk/au/pir/Driver.java index 59ec071..6bd8c4c 100644 --- a/pir/src/main/java/dk/au/pir/Driver.java +++ b/pir/src/main/java/dk/au/pir/Driver.java @@ -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++) { 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 index ca7ebbe..b880c26 100644 --- a/pir/src/main/java/dk/au/pir/protocols/simple/SimpleClient.java +++ b/pir/src/main/java/dk/au/pir/protocols/simple/SimpleClient.java @@ -19,34 +19,61 @@ public class SimpleClient { this.servers = servers; } - public ArrayList selectIndexes() { - ArrayList 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 S1 = selectIndexes(); - ArrayList S2 = (ArrayList) 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; + + } } 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 index b2b1b2a..ddd9ab8 100644 --- a/pir/src/main/java/dk/au/pir/protocols/simple/SimpleServer.java +++ b/pir/src/main/java/dk/au/pir/protocols/simple/SimpleServer.java @@ -16,11 +16,22 @@ public class SimpleServer { 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; + public int computeBit(int[] indexes) { + int res = x[indexes[0]]; + for (int i=1; i