1
0
Fork 0

Profiling.

This commit is contained in:
Casper V. Kristensen 2019-12-02 22:50:20 +01:00
parent 73aecc6420
commit 577c793470
Signed by: caspervk
GPG key ID: 289CA03790535054
16 changed files with 301 additions and 229 deletions

View file

@ -1,140 +1,139 @@
package dk.au.pir; package dk.au.pir;
import dk.au.pir.protocols.evenSimpler.evenSimplerClient; import dk.au.pir.databases.Database;
import dk.au.pir.protocols.evenSimpler.evenSimplerDatabase; import dk.au.pir.databases.MemoryDatabase;
import dk.au.pir.protocols.evenSimpler.evenSimplerServer; 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.InterPolyClient;
import dk.au.pir.protocols.interpoly.InterPolyDatabase;
import dk.au.pir.protocols.interpoly.InterPolyServer; import dk.au.pir.protocols.interpoly.InterPolyServer;
import dk.au.pir.protocols.simple.SimpleClient; 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.protocols.simple.SimpleServer;
import dk.au.pir.settings.PIRSettings; import dk.au.pir.settings.PIRSettings;
import java.util.Arrays;
public class Driver { public class Driver {
private static void testEvenSimplerScheme(PIRSettings settings, Database database, Profiler profiler) {
private static int[] evenSimplerScheme(int record) { EvenSimplerServer[] servers = new EvenSimplerServer[settings.getNumServers()];
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()];
for (int i = 0; i < settings.getNumServers(); i++) { 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, profiler);
evenSimplerClient client = new evenSimplerClient(settings, servers); profiler.start();
client.receiveBits(1);
int[] res = client.receiveBits(record); profiler.stop();
System.out.println("res: " + Arrays.toString(res));
return res;
} }
private static int simpleScheme(int index) { private static void testSimpleScheme(PIRSettings settings, Database database, Profiler profiler) {
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()]; SimpleServer[] servers = new SimpleServer[settings.getNumServers()];
for (int i = 0; i < settings.getNumServers(); i++) { for (int i = 0; i < settings.getNumServers(); i++) {
servers[i] = new SimpleServer(database, settings); servers[i] = new SimpleServer(database, settings);
} }
SimpleClient client = new SimpleClient(settings, servers, profiler);
SimpleClient client = new SimpleClient(settings, servers); profiler.start();
client.receiveBit(1);
int res = client.receiveBit(index); profiler.stop();
System.out.println("res: " + res);
return res;
} }
private static int[] simpleBlockScheme(int record) { private static void testSimpleBlockScheme(PIRSettings settings, Database database, Profiler profiler) {
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()]; SimpleServer[] servers = new SimpleServer[settings.getNumServers()];
for (int i = 0; i < settings.getNumServers(); i++) { for (int i = 0; i < settings.getNumServers(); i++) {
servers[i] = new SimpleServer(database, settings); servers[i] = new SimpleServer(database, settings);
} }
SimpleClient client = new SimpleClient(settings, servers, profiler);
SimpleClient client = new SimpleClient(settings, servers); profiler.start();
client.receiveBits(1);
int[] res = client.receiveBits(record); profiler.stop();
System.out.println("res: " + Arrays.toString(res));
return res;
} }
private static void testGeneralInterPolyScheme(PIRSettings settings, Database database, Profiler profiler) {
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});
InterPolyServer[] servers = new InterPolyServer[settings.getNumServers()]; InterPolyServer[] servers = new InterPolyServer[settings.getNumServers()];
for (int i = 0; i < settings.getNumServers(); i++) { for (int i = 0; i < settings.getNumServers(); i++) {
servers[i] = new InterPolyServer(database, settings); servers[i] = new InterPolyServer(database, settings);
} }
InterPolyClient client = new InterPolyClient(settings, servers, profiler);
InterPolyClient client = new InterPolyClient(settings, servers); profiler.start();
int[] res = client.receive(index); client.receive(1);
System.out.println("res: " + Arrays.toString(res)); profiler.stop();
return res;
} }
private static int[] generalBlockInterPolyTest(int index) { private static void testGeneralInterPolyBlockScheme(PIRSettings settings, Database database, Profiler profiler) {
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});
InterPolyServer[] servers = new InterPolyServer[settings.getNumServers()]; InterPolyServer[] servers = new InterPolyServer[settings.getNumServers()];
for (int i = 0; i < settings.getNumServers(); i++) { for (int i = 0; i < settings.getNumServers(); i++) {
servers[i] = new InterPolyServer(database, settings); servers[i] = new InterPolyServer(database, settings);
} }
InterPolyClient client = new InterPolyClient(settings, servers, profiler);
InterPolyClient client = new InterPolyClient(settings, servers); profiler.start();
int[] res = client.receive(index); client.receiveBlock(1);
System.out.println("res: " + Arrays.toString(res)); profiler.stop();
return res;
} }
private static int[] generalBlockInterPolyTestButBetter(int record) { public static void runTests() {
PIRSettings settings = new PIRSettings(8, 3, 2); for (int numServers = 2; numServers <= 8; numServers = (int) Math.pow(2, ++numServers)) {
int s = settings.getS(); for (int databaseSize = 2; databaseSize <= 32; databaseSize = (int) Math.pow(2, ++databaseSize)) {
System.out.println("s is: " + s); for (int blockSize = 1; blockSize <= Math.min(4, databaseSize); blockSize = (int) Math.pow(2, ++blockSize)) {
for (int latency = 0; latency <= 50; latency = latency + 10) {
InterPolyDatabase database = new InterPolyDatabase(settings, new int[] {0,0, 1,1, 0,0, 0,1}); for (int bandwidth = 1024; bandwidth <= 2048; bandwidth = (int) Math.pow(2, ++bandwidth)) {
runTest(numServers, databaseSize, blockSize, latency, bandwidth);
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); public static void runTest(int numServers, int databaseSize, int blockSize, int latency, int bandwidth) {
int[] res = client.receiveBlock(record); PIRSettings settings = new PIRSettings(databaseSize, numServers, blockSize);
System.out.println("res: " + Arrays.toString(res)); int[] x = new int[databaseSize];
return res; 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) { public static void main(String[] args) {
// generalBlockInterPolyTestButBetter(1); runTests(); // warm-up
//simpleBlockScheme(1); System.out.println("================");
evenSimplerScheme(1); runTests();
/*
int sum = 0;
for (int i = 0; i < 1; i++) {
int res = generalInterPolyTest(i);
if (res == 1) {
sum += 1;
}
}
System.out.println("sum: " + sum); */
} }
} }

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; import dk.au.pir.settings.PIRSettings;
public class SimpleDatabase { public class MemoryDatabase implements Database{
private final int[] x; private final int[] x;
public SimpleDatabase(PIRSettings settings, int[] x) { public MemoryDatabase(PIRSettings settings, int[] x) {
this.x = 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; package dk.au.pir.protocols.interpoly;
import dk.au.pir.BigIntegerField; import dk.au.pir.BigIntegerField;
import dk.au.pir.profilers.Profiler;
import dk.au.pir.settings.PIRSettings; import dk.au.pir.settings.PIRSettings;
import dk.au.pir.utils.FieldElement; import dk.au.pir.utils.FieldElement;
import dk.au.pir.utils.FieldElementLagrange; import dk.au.pir.utils.FieldElementLagrange;
@ -16,16 +17,16 @@ public class InterPolyClient {
private final int s; private final int s;
private final BigIntegerField field; private final BigIntegerField field;
private final int[][] sequences; 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.settings = settings;
this.servers = servers; this.servers = servers;
this.s = settings.getS(); this.s = settings.getS();
this.field = settings.getField(); this.field = settings.getField();
this.sequences = settings.getSequences(); this.sequences = settings.getSequences();
System.out.println("Sequences"); this.profiler = profiler;
printIntArrayArray(this.sequences);
} }
private FieldElement[] getRandomFieldElements() { private FieldElement[] getRandomFieldElements() {
@ -59,8 +60,9 @@ public class InterPolyClient {
private int receiveBit(int index) { private int receiveBit(int index) {
FieldElement[] randoms = this.getRandomFieldElements(); FieldElement[] randoms = this.getRandomFieldElements();
FieldElement[] Fs = new FieldElement[this.servers.length]; FieldElement[] Fs = new FieldElement[this.servers.length];
this.profiler.addNetworkDelay(2);
for (int z = 0; z < this.servers.length; z++) { 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); FieldElement res = FieldElementLagrange.interpolate(this.field, Fs);
return res.getValue().intValue(); 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 * 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++) { for (int z = 0; z < this.servers.length; z++) {
FieldElement[][] Gs = new FieldElement[settings.getBlocksize()][this.s]; FieldElement[][] Gs = new FieldElement[settings.getBlocksize()][this.s];
for (int i = 0; i < settings.getBlocksize(); i++) { for (int i = 0; i < settings.getBlocksize(); i++) {
@ -92,7 +92,6 @@ public class InterPolyClient {
Fs[z] = this.servers[z].FBlock(Gs); Fs[z] = this.servers[z].FBlock(Gs);
} }
for (int i = 0; i < settings.getBlocksize(); i++) { for (int i = 0; i < settings.getBlocksize(); i++) {
FieldElement[] tmp = new FieldElement[this.servers.length]; 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; package dk.au.pir.protocols.interpoly;
import dk.au.pir.BigIntegerField; import dk.au.pir.BigIntegerField;
import dk.au.pir.databases.Database;
import dk.au.pir.settings.PIRSettings; import dk.au.pir.settings.PIRSettings;
import dk.au.pir.utils.FieldElement; import dk.au.pir.utils.FieldElement;
public class InterPolyServer { public class InterPolyServer {
private InterPolyDatabase database; private Database database;
private PIRSettings settings; private PIRSettings settings;
private final BigIntegerField field; private final BigIntegerField field;
public InterPolyServer(InterPolyDatabase database, PIRSettings settings) { public InterPolyServer(Database database, PIRSettings settings) {
this.database = database; this.database = database;
this.settings = settings; this.settings = settings;
@ -31,7 +32,6 @@ public class InterPolyServer {
return sum; return sum;
} }
public FieldElement[] FBlock(FieldElement[][] gs) { public FieldElement[] FBlock(FieldElement[][] gs) {
FieldElement[] sum = new FieldElement[this.settings.getBlocksize()]; FieldElement[] sum = new FieldElement[this.settings.getBlocksize()];
for (int i = 0; i < sum.length; i++) { for (int i = 0; i < sum.length; i++) {

View file

@ -1,19 +1,20 @@
package dk.au.pir.protocols.simple; package dk.au.pir.protocols.simple;
import dk.au.pir.profilers.Profiler;
import dk.au.pir.settings.PIRSettings; import dk.au.pir.settings.PIRSettings;
import java.util.Random; import java.util.Random;
public class SimpleClient { public class SimpleClient {
private final PIRSettings settings; private final PIRSettings settings;
private final SimpleServer[] servers; 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.settings = settings;
this.servers = servers; this.servers = servers;
this.profiler = profiler;
} }
public int[] selectIndexes() { public int[] selectIndexes() {
@ -25,21 +26,20 @@ public class SimpleClient {
return indexes; return indexes;
} }
public int receiveBit(int index) { public int receiveBit(int index) {
int[] S1 = selectIndexes(); int[] S1 = selectIndexes();
int[] S2 = S1.clone(); int[] S2 = S1.clone();
if (S1[index] == 1) { if (S1[index] == 1) {
// Remove the index, if it's contained in S. S2[index] = 0; // Remove the index, if it's contained in S.
S2[index] = 0;
} else { } else {
S2[index] = 1; S2[index] = 1;
} }
// TODO: Hardcoded, should be a loop // TODO: Hardcoded, should be a loop
int resBit1 = this.servers[0].computeBit(S1); this.profiler.addNetworkDelay(2);
int resBit2 = this.servers[1].computeBit(S2); 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); return ((resBit1 + resBit2) % 2);
} }
@ -62,15 +62,14 @@ public class SimpleClient {
} }
} }
this.profiler.addNetworkDelay(2);
int[] resBit1 = this.servers[0].computeBits(S1s); int[] resBit1 = this.profiler.clientReceive(this.servers[0].computeBits(this.profiler.clientSend(S1s)));
int[] resBit2 = this.servers[1].computeBits(S2s); int[] resBit2 = this.profiler.clientReceive(this.servers[1].computeBits(this.profiler.clientSend(S2s)));
for (int i = 0; i < settings.getBlocksize(); i++) { for (int i = 0; i < settings.getBlocksize(); i++) {
result[i] = (resBit1[i] + resBit2[i]) % 2; result[i] = (resBit1[i] + resBit2[i]) % 2;
} }
return result; return result;
} }
} }

View file

@ -1,31 +1,26 @@
package dk.au.pir.protocols.simple; package dk.au.pir.protocols.simple;
import dk.au.pir.databases.Database;
import dk.au.pir.settings.PIRSettings; import dk.au.pir.settings.PIRSettings;
import java.util.ArrayList;
public class SimpleServer { public class SimpleServer {
private final Database database;
private final SimpleDatabase database;
private final PIRSettings settings; private final PIRSettings settings;
private final int[] x;
public SimpleServer(SimpleDatabase database, PIRSettings settings) { public SimpleServer(Database database, PIRSettings settings) {
this.database = database; this.database = database;
this.settings = settings; this.settings = settings;
this.x = database.getX();
} }
public int computeBit(int[] indexes) { public int computeBit(int[] indexes) {
int res = x[indexes[0]]; int res = database.getX()[indexes[0]];
for (int i=1; i<indexes.length; i++) { for (int i=1; i<indexes.length; i++) {
if (indexes[i] == 1) if (indexes[i] == 1)
res = (res + x[i]) % 2; res = (res + database.getX()[i]) % 2;
} }
return res; return res;
} }
public int[] computeBits(int[][] indexes) { public int[] computeBits(int[][] indexes) {
int[] res = new int[settings.getBlocksize()]; int[] res = new int[settings.getBlocksize()];
for (int i = 0; i < settings.getBlocksize(); i++) { for (int i = 0; i < settings.getBlocksize(); i++) {
@ -33,5 +28,4 @@ public class SimpleServer {
} }
return res; return res;
} }
} }

View file

@ -7,9 +7,9 @@ import dk.au.pir.utils.ProtocolUtils;
public class PIRSettings { public class PIRSettings {
private final int databaseSize; private final int databaseSize;
private final int numServers; private final int numServers;
private final int s; private int s;
private final int[][] sequences; private int[][] sequences;
private final BigIntegerField field; private final BigIntegerField field;
private final int blocksize; private final int blocksize;
@ -17,28 +17,30 @@ public class PIRSettings {
public PIRSettings(int databaseSize, int numServers, int blocksize) { public PIRSettings(int databaseSize, int numServers, int blocksize) {
this.databaseSize = databaseSize; this.databaseSize = databaseSize;
this.numServers = numServers; this.numServers = numServers;
this.s = calculateS(numServers, databaseSize);
this.blocksize = blocksize; this.blocksize = blocksize;
try {
// TODO: lol this.s = calculateS(numServers, databaseSize);
if (numServers > 1) { // TODO: lol
this.sequences = ProtocolUtils.createSequences(s, numServers, databaseSize); if (numServers > 1) {
} else { this.sequences = ProtocolUtils.createSequences(s, numServers, databaseSize);
this.sequences = ProtocolUtils.createSequences(calculateS(2, databaseSize), 2, databaseSize); } else {
this.sequences = ProtocolUtils.createSequences(calculateS(2, databaseSize), 2, databaseSize);
}
} catch (IllegalArgumentException error) {
this.s = 0;
} }
this.field = new BigIntegerField(); 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++) { for (int s = k-1; s <= n; s++) {
if (MathUtils.binomial(s, k-1) >= n) { if (MathUtils.binomial(s, k-1) >= n) {
return s; return s;
} }
} }
return -1; throw new IllegalArgumentException();
} }
public int getDatabaseSize() { public int getDatabaseSize() {

View file

@ -7,7 +7,7 @@ import java.util.Arrays;
public class FieldElementLagrange { public class FieldElementLagrange {
public static FieldElement interpolate(BigIntegerField field, FieldElement[] y) { public static FieldElement interpolate(BigIntegerField field, FieldElement[] y) {
// https://stackoverflow.com/questions/16375163/lagrange-interpolation-in-java // 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 xPoint = field.valueOf(0); // we want to find f(0), so xpoint=0
FieldElement sum = field.valueOf(0); FieldElement sum = field.valueOf(0);
FieldElement product = field.valueOf(1); FieldElement product = field.valueOf(1);