From 4ec05cf83a506e3d76cc54caad8da238d4d986b6 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Sun, 1 Dec 2019 13:46:06 +0100 Subject: [PATCH] fixed stuff --- pir/src/main/java/dk/au/pir/Driver.java | 39 ++++++++++++++++- .../protocols/interpoly/InterPolyClient.java | 42 +++++++++++++++++++ .../protocols/interpoly/InterPolyServer.java | 13 ++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/pir/src/main/java/dk/au/pir/Driver.java b/pir/src/main/java/dk/au/pir/Driver.java index ac7002e..e4f514c 100644 --- a/pir/src/main/java/dk/au/pir/Driver.java +++ b/pir/src/main/java/dk/au/pir/Driver.java @@ -9,6 +9,24 @@ import java.util.Arrays; public class Driver { 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()]; + 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; + } + + private static int[] generalBlockInterPolyTest(int index) { PIRSettings settings = new PIRSettings(8, 3, 2); int s = settings.getS(); System.out.println("s is: " + s); @@ -26,8 +44,27 @@ public class Driver { return res; } + 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); + } + + InterPolyClient client = new InterPolyClient(settings, servers); + int[] res = client.receiveBlock(record); + System.out.println("res: " + Arrays.toString(res)); + return res; + } + + public static void main(String[] args) { - generalInterPolyTest(1); + generalBlockInterPolyTestButBetter(1); /* int sum = 0; for (int i = 0; i < 1; i++) { 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 a632c05..56dcb20 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 @@ -6,6 +6,7 @@ import dk.au.pir.utils.FieldElement; import dk.au.pir.utils.FieldElementLagrange; import java.math.BigInteger; +import java.util.Arrays; import static dk.au.pir.utils.ProtocolUtils.printIntArrayArray; @@ -35,6 +36,15 @@ public class InterPolyClient { return fieldElements; } + private FieldElement[][] getRandomFieldElementsBlock() { + FieldElement[][] fieldElements = new FieldElement[this.settings.getBlocksize()][this.s]; + + for (int i = 0; i < this.settings.getBlocksize(); i++) { + fieldElements[i] = getRandomFieldElements(); + } + return fieldElements; + } + private FieldElement[] getGs(int index, int serverNumber, FieldElement[] random) { FieldElement[] gs = new FieldElement[this.s]; int[] i = this.sequences[index]; @@ -63,4 +73,36 @@ public class InterPolyClient { } return results; } + + public int[] receiveBlock(int record) { + int[] results = new int[settings.getBlocksize()]; + FieldElement[][] randoms = this.getRandomFieldElementsBlock(); + FieldElement[][] Fs = new FieldElement[this.servers.length][settings.getBlocksize()]; + + /** + * 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++) { + Gs[i] = this.getGs(record*settings.getBlocksize() + i, z+1, randoms[i]); + } + Fs[z] = this.servers[z].FBlock(Gs); + } + + + for (int i = 0; i < settings.getBlocksize(); i++) { + FieldElement[] tmp = new FieldElement[this.servers.length]; + + for (int z = 0; z < this.servers.length; z++) { + tmp[z] = Fs[z][i]; + } + FieldElement res = FieldElementLagrange.interpolate(this.field, tmp); + results[i] = res.getValue().intValue(); + } + + return results; + } } 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 423616f..999c61e 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 @@ -30,4 +30,17 @@ 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++) { + sum[i] = this.field.valueOf(0); + } + + for (int i = 0; i < this.settings.getBlocksize(); i++) { + sum[i] = F(gs[i]); + } + return sum; + } }