From 43f28695d0699e666a71e15a2236a47c11c8c251 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Thu, 5 Dec 2019 17:01:31 +0100 Subject: [PATCH] pls --- .../balancedBlockClient.java | 78 +++++++++++++++++++ .../balancedBlockServer.java | 49 ++++++++++++ 2 files changed, 127 insertions(+) diff --git a/pir/src/main/java/dk/au/pir/protocols/balancedBlockScheme/balancedBlockClient.java b/pir/src/main/java/dk/au/pir/protocols/balancedBlockScheme/balancedBlockClient.java index 4bf7380..0cb9edd 100644 --- a/pir/src/main/java/dk/au/pir/protocols/balancedBlockScheme/balancedBlockClient.java +++ b/pir/src/main/java/dk/au/pir/protocols/balancedBlockScheme/balancedBlockClient.java @@ -1,4 +1,82 @@ package dk.au.pir.protocols.balancedBlockScheme; +import dk.au.pir.databases.Database; +import dk.au.pir.databases.MemoryDatabase; +import dk.au.pir.profilers.Profiler; +import dk.au.pir.protocols.simple.SimpleClient; +import dk.au.pir.protocols.simple.SimpleServer; +import dk.au.pir.settings.PIRSettings; + +import java.util.Arrays; +import java.util.Random; + public class balancedBlockClient { + + + private final PIRSettings settings; + private final balancedBlockServer[] servers; + private final int sqrtSize; + private Profiler profiler; + + public balancedBlockClient(PIRSettings settings, balancedBlockServer[] servers, Profiler profiler) { + this.settings = settings; + this.servers = servers; + this.profiler = profiler; + this.sqrtSize = (int) Math.ceil(Math.sqrt(settings.getDatabaseSize())); + } + + public int[] selectIndexes(int n) { + int[] indexes = new int[n]; + Random rand = new Random(); + for (int i=0; i < n; i++) { + indexes[i] = rand.nextInt(2); + } + return indexes; + } + + public int receiveBit(int index) { + /** + * PLAN: + * Divide n into sqrt(n) + * Compute which index we want find this within a block + * Send block + */ + + int[] S1 = selectIndexes(this.sqrtSize); + int[] S2 = S1.clone(); + + + + int impBlock = index % this.sqrtSize; + System.out.println("ImpBlock: " + impBlock); + if (S1[index % this.sqrtSize] == 1) { + S2[index % this.sqrtSize] = 0; // Remove the index, if it's contained in S. + } else { + S2[index % this.sqrtSize] = 1; + } + + System.out.println("S1: " + Arrays.toString(S1)); + System.out.println("S2: " + Arrays.toString(S2)); + + + int[] resBit1 = this.servers[0].computeBit(S1); + int[] resBit2 = this.servers[1].computeBit(S2); + + return ((resBit1[impBlock] + resBit2[impBlock]) % 2); + + } + + public static void main(String[] args) { + PIRSettings settings = new PIRSettings(16, 2, 1); + balancedBlockServer[] servers = new balancedBlockServer[settings.getNumServers()]; + + Database database = new MemoryDatabase(settings, new int[] {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}); + + for (int i = 0; i < settings.getNumServers(); i++) { + servers[i] = new balancedBlockServer(database, settings); + } + balancedBlockClient client = new balancedBlockClient(settings, servers, null); + System.out.println(client.receiveBit(6)); + } } + diff --git a/pir/src/main/java/dk/au/pir/protocols/balancedBlockScheme/balancedBlockServer.java b/pir/src/main/java/dk/au/pir/protocols/balancedBlockScheme/balancedBlockServer.java index a2b4961..cd61086 100644 --- a/pir/src/main/java/dk/au/pir/protocols/balancedBlockScheme/balancedBlockServer.java +++ b/pir/src/main/java/dk/au/pir/protocols/balancedBlockScheme/balancedBlockServer.java @@ -1,4 +1,53 @@ package dk.au.pir.protocols.balancedBlockScheme; +import dk.au.pir.databases.Database; +import dk.au.pir.settings.PIRSettings; + +import java.util.Arrays; + public class balancedBlockServer { + + + private final Database database; + private final PIRSettings settings; + private final int sqrtSize; + + public balancedBlockServer(Database database, PIRSettings settings) { + this.database = database; + this.settings = settings; + this.sqrtSize = (int) Math.ceil(Math.sqrt(settings.getDatabaseSize())); + } + + public int[] computeBit(int[] indexes) { + int[] db = database.getX(); + + /* + Divide n in the sqrt(n) size chunks + Get sqrt(n) size array from client, which we cycle through sqrt(n) times + We return a sqrt(n) size list of bits. One from each cycle. + */ + + int[] resList = new int[this.sqrtSize]; + + for (int i = 0; i < this.sqrtSize; i++) { + + int tmpRes = 0; + + for (int j = 0; j < this.sqrtSize; j++) { + try { + boolean test = indexes[j] == 1; + if (test) { + tmpRes = (tmpRes + db[j + (this.sqrtSize * i)]) % 2; + } + + } catch (ArrayIndexOutOfBoundsException e) { + tmpRes = (tmpRes) % 2; + } + } + resList[i] = tmpRes; + } + System.out.println("ResList: " + Arrays.toString(resList)); + + return resList; + } }