Profiling.

This commit is contained in:
Casper V. Kristensen 2019-12-02 22:50:20 +01:00
parent 73aecc6420
commit 577c793470
No known key found for this signature in database
GPG Key ID: 289CA03790535054
16 changed files with 301 additions and 229 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,5 @@
package dk.au.pir.databases;
public interface Database {
public int[] getX();
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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];

View File

@ -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;
}
}

View File

@ -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++) {

View File

@ -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;
}
}

View File

@ -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<indexes.length; i++) {
if (indexes[i] == 1)
res = (res + x[i]) % 2;
res = (res + database.getX()[i]) % 2;
}
return res;
}
public int[] computeBits(int[][] indexes) {
int[] res = new int[settings.getBlocksize()];
for (int i = 0; i < settings.getBlocksize(); i++) {
@ -33,5 +28,4 @@ public class SimpleServer {
}
return res;
}
}

View File

@ -7,9 +7,9 @@ import dk.au.pir.utils.ProtocolUtils;
public class PIRSettings {
private final int databaseSize;
private final int numServers;
private final int s;
private int s;
private final int[][] sequences;
private int[][] sequences;
private final BigIntegerField field;
private final int blocksize;
@ -17,28 +17,30 @@ public class PIRSettings {
public PIRSettings(int databaseSize, int numServers, int blocksize) {
this.databaseSize = databaseSize;
this.numServers = numServers;
this.s = calculateS(numServers, databaseSize);
this.blocksize = blocksize;
// TODO: lol
if (numServers > 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() {

View File

@ -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);