From 577c793470cc73c1b76ec5aeb6b1b5c3fb5ad34b Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Mon, 2 Dec 2019 22:50:20 +0100 Subject: [PATCH] Profiling. --- pir/src/main/java/dk/au/pir/Driver.java | 187 +++++++++--------- .../java/dk/au/pir/databases/Database.java | 5 + .../MemoryDatabase.java} | 7 +- .../java/dk/au/pir/profilers/Profiler.java | 114 +++++++++++ .../evenSimpler/EvenSimplerClient.java | 30 +++ .../evenSimpler/EvenSimplerServer.java | 16 ++ .../evenSimpler/evenSimplerClient.java | 30 --- .../evenSimpler/evenSimplerDatabase.java | 17 -- .../evenSimpler/evenSimplerServer.java | 22 --- .../protocols/interpoly/InterPolyClient.java | 13 +- .../interpoly/InterPolyDatabase.java | 16 -- .../protocols/interpoly/InterPolyServer.java | 6 +- .../au/pir/protocols/simple/SimpleClient.java | 23 ++- .../au/pir/protocols/simple/SimpleServer.java | 16 +- .../java/dk/au/pir/settings/PIRSettings.java | 26 +-- .../dk/au/pir/utils/FieldElementLagrange.java | 2 +- 16 files changed, 301 insertions(+), 229 deletions(-) create mode 100644 pir/src/main/java/dk/au/pir/databases/Database.java rename pir/src/main/java/dk/au/pir/{protocols/simple/SimpleDatabase.java => databases/MemoryDatabase.java} (51%) create mode 100644 pir/src/main/java/dk/au/pir/profilers/Profiler.java create mode 100644 pir/src/main/java/dk/au/pir/protocols/evenSimpler/EvenSimplerClient.java create mode 100644 pir/src/main/java/dk/au/pir/protocols/evenSimpler/EvenSimplerServer.java delete mode 100644 pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerClient.java delete mode 100644 pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerDatabase.java delete mode 100644 pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerServer.java delete mode 100644 pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyDatabase.java diff --git a/pir/src/main/java/dk/au/pir/Driver.java b/pir/src/main/java/dk/au/pir/Driver.java index 6f0374c..122de81 100644 --- a/pir/src/main/java/dk/au/pir/Driver.java +++ b/pir/src/main/java/dk/au/pir/Driver.java @@ -1,140 +1,139 @@ package dk.au.pir; -import dk.au.pir.protocols.evenSimpler.evenSimplerClient; -import dk.au.pir.protocols.evenSimpler.evenSimplerDatabase; -import dk.au.pir.protocols.evenSimpler.evenSimplerServer; +import dk.au.pir.databases.Database; +import dk.au.pir.databases.MemoryDatabase; +import dk.au.pir.profilers.Profiler; +import dk.au.pir.protocols.evenSimpler.EvenSimplerClient; +import dk.au.pir.protocols.evenSimpler.EvenSimplerServer; 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[] evenSimplerScheme(int record) { - PIRSettings settings = new PIRSettings(8, 1, 2); - evenSimplerDatabase database = new evenSimplerDatabase(settings, new int[] {0, 0, 0, 1, 0, 0, 0, 0}); - - evenSimplerServer[] servers = new evenSimplerServer[settings.getNumServers()]; - + private static void testEvenSimplerScheme(PIRSettings settings, Database database, Profiler profiler) { + EvenSimplerServer[] servers = new EvenSimplerServer[settings.getNumServers()]; for (int i = 0; i < settings.getNumServers(); i++) { - servers[i] = new evenSimplerServer(database, settings); + servers[i] = new EvenSimplerServer(database, settings); } - - evenSimplerClient client = new evenSimplerClient(settings, servers); - - int[] res = client.receiveBits(record); - System.out.println("res: " + Arrays.toString(res)); - return res; + EvenSimplerClient client = new EvenSimplerClient(settings, servers, profiler); + profiler.start(); + client.receiveBits(1); + profiler.stop(); } - 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}); - + private static void testSimpleScheme(PIRSettings settings, Database database, Profiler profiler) { 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; + SimpleClient client = new SimpleClient(settings, servers, profiler); + profiler.start(); + client.receiveBit(1); + profiler.stop(); } - 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}); - + private static void testSimpleBlockScheme(PIRSettings settings, Database database, Profiler profiler) { 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; + SimpleClient client = new SimpleClient(settings, servers, profiler); + profiler.start(); + client.receiveBits(1); + profiler.stop(); } - - private static int[] generalInterPolyTest(int index) { - PIRSettings settings = new PIRSettings(8, 3, 1); - int s = settings.getS(); - System.out.println("s is: " + s); - - InterPolyDatabase database = new InterPolyDatabase(settings, new int[] {0,0, 1,0, 0,0, 0,1}); - + private static void testGeneralInterPolyScheme(PIRSettings settings, Database database, Profiler profiler) { InterPolyServer[] servers = new InterPolyServer[settings.getNumServers()]; for (int i = 0; i < settings.getNumServers(); i++) { servers[i] = new InterPolyServer(database, settings); } - - InterPolyClient client = new InterPolyClient(settings, servers); - int[] res = client.receive(index); - System.out.println("res: " + Arrays.toString(res)); - return res; + InterPolyClient client = new InterPolyClient(settings, servers, profiler); + profiler.start(); + client.receive(1); + profiler.stop(); } - private static int[] generalBlockInterPolyTest(int index) { - PIRSettings settings = new PIRSettings(8, 3, 2); - int s = settings.getS(); - System.out.println("s is: " + s); - - InterPolyDatabase database = new InterPolyDatabase(settings, new int[] {0,0, 1,0, 0,0, 0,1}); - + private static void testGeneralInterPolyBlockScheme(PIRSettings settings, Database database, Profiler profiler) { InterPolyServer[] servers = new InterPolyServer[settings.getNumServers()]; for (int i = 0; i < settings.getNumServers(); i++) { servers[i] = new InterPolyServer(database, settings); } - - InterPolyClient client = new InterPolyClient(settings, servers); - int[] res = client.receive(index); - System.out.println("res: " + Arrays.toString(res)); - return res; + InterPolyClient client = new InterPolyClient(settings, servers, profiler); + profiler.start(); + client.receiveBlock(1); + profiler.stop(); } - private static int[] generalBlockInterPolyTestButBetter(int record) { - PIRSettings settings = new PIRSettings(8, 3, 2); - int s = settings.getS(); - System.out.println("s is: " + s); - - InterPolyDatabase database = new InterPolyDatabase(settings, new int[] {0,0, 1,1, 0,0, 0,1}); - - InterPolyServer[] servers = new InterPolyServer[settings.getNumServers()]; - for (int i = 0; i < settings.getNumServers(); i++) { - servers[i] = new InterPolyServer(database, settings); + public static void runTests() { + for (int numServers = 2; numServers <= 8; numServers = (int) Math.pow(2, ++numServers)) { + for (int databaseSize = 2; databaseSize <= 32; databaseSize = (int) Math.pow(2, ++databaseSize)) { + for (int blockSize = 1; blockSize <= Math.min(4, databaseSize); blockSize = (int) Math.pow(2, ++blockSize)) { + for (int latency = 0; latency <= 50; latency = latency + 10) { + for (int bandwidth = 1024; bandwidth <= 2048; bandwidth = (int) Math.pow(2, ++bandwidth)) { + runTest(numServers, databaseSize, blockSize, latency, bandwidth); + } + } + } + } } + } - InterPolyClient client = new InterPolyClient(settings, servers); - int[] res = client.receiveBlock(record); - System.out.println("res: " + Arrays.toString(res)); - return res; + public static void runTest(int numServers, int databaseSize, int blockSize, int latency, int bandwidth) { + PIRSettings settings = new PIRSettings(databaseSize, numServers, blockSize); + int[] x = new int[databaseSize]; + for (int i = 0; i < x.length; i++) { + x[i] = (int) (Math.random()*2); // 0 or 1 + } + Database database = new MemoryDatabase(settings, x); + Profiler profiler = new Profiler(latency, bandwidth/10, bandwidth); + + + profiler.reset(); + testEvenSimplerScheme(settings, database, profiler); + reportResult(numServers, databaseSize, blockSize, latency, bandwidth, profiler, "EvenSimplerScheme"); + + profiler.reset(); + testSimpleScheme(settings, database, profiler); + reportResult(numServers, databaseSize, blockSize, latency, bandwidth, profiler, "SimpleScheme"); + + profiler.reset(); + testSimpleBlockScheme(settings, database, profiler); + reportResult(numServers, databaseSize, blockSize, latency, bandwidth, profiler, "SimpleBlockScheme"); + + if (settings.getS() != 0) { + profiler.reset(); + testGeneralInterPolyScheme(settings, database, profiler); + reportResult(numServers, databaseSize, blockSize, latency, bandwidth, profiler, "GeneralInterPolyScheme"); + + profiler.reset(); + testGeneralInterPolyBlockScheme(settings, database, profiler); + reportResult(numServers, databaseSize, blockSize, latency, bandwidth, profiler, "GeneralInterPolyBlockScheme"); + } + } + + public static void reportResult(int numServers, int databaseSize, int blockSize, int latency, int bandwidth, Profiler profiler, String protocolName) { + System.out.println( + numServers + " " + + databaseSize + " " + + blockSize + " " + + latency + " " + + bandwidth + " " + + protocolName + " " + + profiler.getTotalCPUTime() + " " + + profiler.getSent() + " " + + profiler.getReceived() + " " + + profiler.getTotalNetworkTime() + " " + ); } public static void main(String[] args) { - // generalBlockInterPolyTestButBetter(1); - //simpleBlockScheme(1); - evenSimplerScheme(1); - /* - int sum = 0; - for (int i = 0; i < 1; i++) { - int res = generalInterPolyTest(i); - if (res == 1) { - sum += 1; - } - } - System.out.println("sum: " + sum); */ + runTests(); // warm-up + System.out.println("================"); + runTests(); } } diff --git a/pir/src/main/java/dk/au/pir/databases/Database.java b/pir/src/main/java/dk/au/pir/databases/Database.java new file mode 100644 index 0000000..5f4f571 --- /dev/null +++ b/pir/src/main/java/dk/au/pir/databases/Database.java @@ -0,0 +1,5 @@ +package dk.au.pir.databases; + +public interface Database { + public int[] getX(); +} diff --git a/pir/src/main/java/dk/au/pir/protocols/simple/SimpleDatabase.java b/pir/src/main/java/dk/au/pir/databases/MemoryDatabase.java similarity index 51% rename from pir/src/main/java/dk/au/pir/protocols/simple/SimpleDatabase.java rename to pir/src/main/java/dk/au/pir/databases/MemoryDatabase.java index e400b3d..7340e5e 100644 --- a/pir/src/main/java/dk/au/pir/protocols/simple/SimpleDatabase.java +++ b/pir/src/main/java/dk/au/pir/databases/MemoryDatabase.java @@ -1,12 +1,11 @@ -package dk.au.pir.protocols.simple; +package dk.au.pir.databases; import dk.au.pir.settings.PIRSettings; -public class SimpleDatabase { - +public class MemoryDatabase implements Database{ private final int[] x; - public SimpleDatabase(PIRSettings settings, int[] x) { + public MemoryDatabase(PIRSettings settings, int[] x) { this.x = x; } diff --git a/pir/src/main/java/dk/au/pir/profilers/Profiler.java b/pir/src/main/java/dk/au/pir/profilers/Profiler.java new file mode 100644 index 0000000..8bb6114 --- /dev/null +++ b/pir/src/main/java/dk/au/pir/profilers/Profiler.java @@ -0,0 +1,114 @@ +package dk.au.pir.profilers; + +import dk.au.pir.utils.FieldElement; +import dk.au.pir.utils.MathUtils; + +public class Profiler { + private final int latency; + private final int sendBandwidth; + private final int receiveBandwidth; + + private int sent; + private int received; + private int networkTime; + private long startTime; + private long stopTime; + + public Profiler(int latency, int sendBandwidth, int receiveBandwidth) { + this.latency = latency; + this.sendBandwidth = sendBandwidth; + this.receiveBandwidth = receiveBandwidth; + reset(); + } + + public void start() { + reset(); + this.startTime = System.currentTimeMillis(); + } + + public void stop() { + this.stopTime = System.currentTimeMillis(); + } + + public void reset() { + this.sent = 0; + this.received = 0; + this.networkTime = 0; + this.startTime = 0; + this.stopTime = 0; + } + + public void addNetworkDelay() { + this.addNetworkDelay(1); + } + + public void addNetworkDelay(int n) { + this.networkTime += latency * n; + } + + public int clientSend(int number) { + this.sent += log2(number); + return number; + } + + public int[] clientSend(int[] numbers) { + for (int n: numbers) { + this.clientSend(n); + } + return numbers; + } + + public int[][] clientSend(int[][] numbersArrays) { + for (int[] numbers: numbersArrays) { + this.clientSend(numbers); + } + return numbersArrays; + } + + public FieldElement[] clientSend(FieldElement[] elements) { + for (FieldElement element : elements) { + this.sent += element.getValue().bitLength(); + } + return elements; + } + + public int clientReceive(int number) { + this.received += log2(number); + return number; + } + + public int[] clientReceive(int[] numbers) { + for (int n: numbers) { + this.clientReceive(n); + } + return numbers; + } + + public FieldElement clientReceive(FieldElement element) { + this.received += element.getValue().bitLength(); + return element; + } + + public int getSent() { + return this.sent; + } + + public int getReceived() { + return this.received; + } + + public long getTotalCPUTime() { + return this.stopTime - this.startTime; + } + + public int getTotalNetworkTime() { + return networkTime + (this.sent/this.sendBandwidth) + (this.received/this.receiveBandwidth); + } + + public int log2(int n) { + if (n == 0) { + return 1; // technically incorrect but for the sake of profiling, a 0-bit requires 1 bit of space + } + return Integer.SIZE - Integer.numberOfLeadingZeros(n); + } +} diff --git a/pir/src/main/java/dk/au/pir/protocols/evenSimpler/EvenSimplerClient.java b/pir/src/main/java/dk/au/pir/protocols/evenSimpler/EvenSimplerClient.java new file mode 100644 index 0000000..cf63d56 --- /dev/null +++ b/pir/src/main/java/dk/au/pir/protocols/evenSimpler/EvenSimplerClient.java @@ -0,0 +1,30 @@ +package dk.au.pir.protocols.evenSimpler; + +import dk.au.pir.profilers.Profiler; +import dk.au.pir.settings.PIRSettings; + +public class EvenSimplerClient { + private final PIRSettings settings; + private final EvenSimplerServer[] servers; + private final Profiler profiler; + + public EvenSimplerClient(PIRSettings settings, EvenSimplerServer[] servers, Profiler profiler) { + this.settings = settings; + this.servers = servers; + this.profiler = profiler; + } + + public int receiveBit(int index) { + this.profiler.addNetworkDelay(); + int[] data = this.profiler.clientReceive(this.servers[0].giveDatabase()); + return data[index]; + } + + public int[] receiveBits(int record) { + int[] res = new int[settings.getBlocksize()]; + this.profiler.addNetworkDelay(); + int[] data = this.profiler.clientReceive(this.servers[0].giveDatabase()); + System.arraycopy(data, (record * settings.getBlocksize()), res, 0, settings.getBlocksize()); + return res; + } +} diff --git a/pir/src/main/java/dk/au/pir/protocols/evenSimpler/EvenSimplerServer.java b/pir/src/main/java/dk/au/pir/protocols/evenSimpler/EvenSimplerServer.java new file mode 100644 index 0000000..71856d0 --- /dev/null +++ b/pir/src/main/java/dk/au/pir/protocols/evenSimpler/EvenSimplerServer.java @@ -0,0 +1,16 @@ +package dk.au.pir.protocols.evenSimpler; + +import dk.au.pir.databases.Database; +import dk.au.pir.settings.PIRSettings; + +public class EvenSimplerServer { + private final Database database; + + public EvenSimplerServer(Database database, PIRSettings settings) { + this.database = database; + } + + public int[] giveDatabase() { + return this.database.getX(); // lol + } +} diff --git a/pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerClient.java b/pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerClient.java deleted file mode 100644 index fe8342a..0000000 --- a/pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerClient.java +++ /dev/null @@ -1,30 +0,0 @@ -package dk.au.pir.protocols.evenSimpler; - -import dk.au.pir.protocols.simple.SimpleServer; -import dk.au.pir.settings.PIRSettings; - -public class evenSimplerClient { - - - private final PIRSettings settings; - private final evenSimplerServer[] servers; - - public evenSimplerClient(PIRSettings settings, evenSimplerServer[] servers) { - this.settings = settings; - this.servers = servers; - } - - public int receiveBit(int index) { - int[] data = this.servers[0].giveDatabase(); - return data[index]; - } - - public int[] receiveBits(int record) { - int[] res = new int[settings.getBlocksize()]; - int[] data = this.servers[0].giveDatabase(); - - System.arraycopy(data, (record * settings.getBlocksize()), res, 0, settings.getBlocksize()); - - return res; - } -} diff --git a/pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerDatabase.java b/pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerDatabase.java deleted file mode 100644 index 78da06e..0000000 --- a/pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerDatabase.java +++ /dev/null @@ -1,17 +0,0 @@ -package dk.au.pir.protocols.evenSimpler; - - -import dk.au.pir.settings.PIRSettings; - -public class evenSimplerDatabase { - - private final int[] x; - - public evenSimplerDatabase(PIRSettings settings, int[] x) { - this.x = x; - } - - public int[] getX() { - return x; - } -} diff --git a/pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerServer.java b/pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerServer.java deleted file mode 100644 index 5bab8ce..0000000 --- a/pir/src/main/java/dk/au/pir/protocols/evenSimpler/evenSimplerServer.java +++ /dev/null @@ -1,22 +0,0 @@ -package dk.au.pir.protocols.evenSimpler; - -import dk.au.pir.protocols.simple.SimpleDatabase; -import dk.au.pir.settings.PIRSettings; - -public class evenSimplerServer { - - private final evenSimplerDatabase database; - private final PIRSettings settings; - private final int[] x; - - public evenSimplerServer(evenSimplerDatabase database, PIRSettings settings) { - this.database = database; - this.settings = settings; - this.x = database.getX(); - } - - public int[] giveDatabase() { - return this.x; - } - -} diff --git a/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyClient.java b/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyClient.java index 56dcb20..126213c 100644 --- a/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyClient.java +++ b/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyClient.java @@ -1,6 +1,7 @@ package dk.au.pir.protocols.interpoly; import dk.au.pir.BigIntegerField; +import dk.au.pir.profilers.Profiler; import dk.au.pir.settings.PIRSettings; import dk.au.pir.utils.FieldElement; import dk.au.pir.utils.FieldElementLagrange; @@ -16,16 +17,16 @@ public class InterPolyClient { private final int s; private final BigIntegerField field; private final int[][] sequences; + private Profiler profiler; - public InterPolyClient(PIRSettings settings, InterPolyServer[] servers) { + public InterPolyClient(PIRSettings settings, InterPolyServer[] servers, Profiler profiler) { this.settings = settings; this.servers = servers; this.s = settings.getS(); this.field = settings.getField(); this.sequences = settings.getSequences(); - System.out.println("Sequences"); - printIntArrayArray(this.sequences); + this.profiler = profiler; } private FieldElement[] getRandomFieldElements() { @@ -59,8 +60,9 @@ public class InterPolyClient { private int receiveBit(int index) { FieldElement[] randoms = this.getRandomFieldElements(); FieldElement[] Fs = new FieldElement[this.servers.length]; + this.profiler.addNetworkDelay(2); for (int z = 0; z < this.servers.length; z++) { - Fs[z] = this.servers[z].F(this.getGs(index, z+1, randoms)); + Fs[z] = this.profiler.clientReceive(this.servers[z].F(this.profiler.clientSend(this.getGs(index, z+1, randoms)))); } FieldElement res = FieldElementLagrange.interpolate(this.field, Fs); return res.getValue().intValue(); @@ -82,8 +84,6 @@ public class InterPolyClient { /** * 1) Compute all the Gs for each server, s.t. the first index should be the blocksize and it should contain all the Gs for the given index */ - - for (int z = 0; z < this.servers.length; z++) { FieldElement[][] Gs = new FieldElement[settings.getBlocksize()][this.s]; for (int i = 0; i < settings.getBlocksize(); i++) { @@ -92,7 +92,6 @@ public class InterPolyClient { Fs[z] = this.servers[z].FBlock(Gs); } - for (int i = 0; i < settings.getBlocksize(); i++) { FieldElement[] tmp = new FieldElement[this.servers.length]; diff --git a/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyDatabase.java b/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyDatabase.java deleted file mode 100644 index 9c96c59..0000000 --- a/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyDatabase.java +++ /dev/null @@ -1,16 +0,0 @@ -package dk.au.pir.protocols.interpoly; - -import dk.au.pir.settings.PIRSettings; - -public class InterPolyDatabase { - private final int[] x; - - public InterPolyDatabase(PIRSettings settings, int[] x) { - //this.x = new int[settings.getDatabaseSize() * settings.getBlocksize()]; - this.x = x; - } - - public int[] getX() { - return x; - } -} diff --git a/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyServer.java b/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyServer.java index 999c61e..1474794 100644 --- a/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyServer.java +++ b/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyServer.java @@ -1,15 +1,16 @@ package dk.au.pir.protocols.interpoly; import dk.au.pir.BigIntegerField; +import dk.au.pir.databases.Database; import dk.au.pir.settings.PIRSettings; import dk.au.pir.utils.FieldElement; public class InterPolyServer { - private InterPolyDatabase database; + private Database database; private PIRSettings settings; private final BigIntegerField field; - public InterPolyServer(InterPolyDatabase database, PIRSettings settings) { + public InterPolyServer(Database database, PIRSettings settings) { this.database = database; this.settings = settings; @@ -31,7 +32,6 @@ public class InterPolyServer { return sum; } - public FieldElement[] FBlock(FieldElement[][] gs) { FieldElement[] sum = new FieldElement[this.settings.getBlocksize()]; for (int i = 0; i < sum.length; 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 1d31918..ae2a1d2 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 @@ -1,19 +1,20 @@ package dk.au.pir.protocols.simple; +import dk.au.pir.profilers.Profiler; import dk.au.pir.settings.PIRSettings; import java.util.Random; public class SimpleClient { - - private final PIRSettings settings; private final SimpleServer[] servers; + private Profiler profiler; - public SimpleClient(PIRSettings settings, SimpleServer[] servers) { + public SimpleClient(PIRSettings settings, SimpleServer[] servers, Profiler profiler) { this.settings = settings; this.servers = servers; + this.profiler = profiler; } public int[] selectIndexes() { @@ -25,21 +26,20 @@ public class SimpleClient { return indexes; } - public int receiveBit(int index) { int[] S1 = selectIndexes(); int[] S2 = S1.clone(); if (S1[index] == 1) { - // Remove the index, if it's contained in S. - S2[index] = 0; + S2[index] = 0; // Remove the index, if it's contained in S. } else { S2[index] = 1; } // TODO: Hardcoded, should be a loop - int resBit1 = this.servers[0].computeBit(S1); - int resBit2 = this.servers[1].computeBit(S2); + this.profiler.addNetworkDelay(2); + int resBit1 = this.profiler.clientReceive(this.servers[0].computeBit(this.profiler.clientSend(S1))); + int resBit2 = this.profiler.clientReceive(this.servers[1].computeBit(this.profiler.clientSend(S2))); return ((resBit1 + resBit2) % 2); } @@ -62,15 +62,14 @@ public class SimpleClient { } } - - int[] resBit1 = this.servers[0].computeBits(S1s); - int[] resBit2 = this.servers[1].computeBits(S2s); + this.profiler.addNetworkDelay(2); + int[] resBit1 = this.profiler.clientReceive(this.servers[0].computeBits(this.profiler.clientSend(S1s))); + int[] resBit2 = this.profiler.clientReceive(this.servers[1].computeBits(this.profiler.clientSend(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 ddd9ab8..76b19c3 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 @@ -1,31 +1,26 @@ package dk.au.pir.protocols.simple; +import dk.au.pir.databases.Database; import dk.au.pir.settings.PIRSettings; -import java.util.ArrayList; - public class SimpleServer { - - private final SimpleDatabase database; + private final Database database; private final PIRSettings settings; - private final int[] x; - public SimpleServer(SimpleDatabase database, PIRSettings settings) { + public SimpleServer(Database database, PIRSettings settings) { this.database = database; this.settings = settings; - this.x = database.getX(); } public int computeBit(int[] indexes) { - int res = x[indexes[0]]; + int res = database.getX()[indexes[0]]; for (int i=1; i 1) { - this.sequences = ProtocolUtils.createSequences(s, numServers, databaseSize); - } else { - this.sequences = ProtocolUtils.createSequences(calculateS(2, databaseSize), 2, databaseSize); + try { + this.s = calculateS(numServers, databaseSize); + // TODO: lol + if (numServers > 1) { + this.sequences = ProtocolUtils.createSequences(s, numServers, databaseSize); + } else { + this.sequences = ProtocolUtils.createSequences(calculateS(2, databaseSize), 2, databaseSize); + } + } catch (IllegalArgumentException error) { + this.s = 0; } - this.field = new BigIntegerField(); } - private int calculateS(int k, int n) { + private int calculateS(int k, int n) throws IllegalArgumentException { for (int s = k-1; s <= n; s++) { if (MathUtils.binomial(s, k-1) >= n) { return s; } } - return -1; + throw new IllegalArgumentException(); } public int getDatabaseSize() { diff --git a/pir/src/main/java/dk/au/pir/utils/FieldElementLagrange.java b/pir/src/main/java/dk/au/pir/utils/FieldElementLagrange.java index 16ee8f6..670edcd 100644 --- a/pir/src/main/java/dk/au/pir/utils/FieldElementLagrange.java +++ b/pir/src/main/java/dk/au/pir/utils/FieldElementLagrange.java @@ -7,7 +7,7 @@ import java.util.Arrays; public class FieldElementLagrange { public static FieldElement interpolate(BigIntegerField field, FieldElement[] y) { // https://stackoverflow.com/questions/16375163/lagrange-interpolation-in-java - System.out.println("y: " + Arrays.deepToString(y)); + //System.out.println("y: " + Arrays.deepToString(y)); FieldElement xPoint = field.valueOf(0); // we want to find f(0), so xpoint=0 FieldElement sum = field.valueOf(0); FieldElement product = field.valueOf(1);