From a1d1a0a16c5e91bf3832ea793adef7d10fb46507 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Mon, 11 Nov 2019 00:44:29 +0100 Subject: [PATCH] Kinda returns 1 all the time --- pir/src/main/java/dk/au/pir/Driver.java | 25 +++++++++-- .../interpoly/GeneralInterPolyClient.java | 8 +--- .../interpoly/GeneralInterPolyServer.java | 45 +++++++++++++++++++ .../protocols/interpoly/InterPolyClient.java | 8 +--- .../protocols/interpoly/InterPolyServer.java | 9 +--- .../java/dk/au/pir/utils/IntegerUtils.java | 9 ++++ 6 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyServer.java create mode 100644 pir/src/main/java/dk/au/pir/utils/IntegerUtils.java diff --git a/pir/src/main/java/dk/au/pir/Driver.java b/pir/src/main/java/dk/au/pir/Driver.java index f6b5bf1..248d98b 100644 --- a/pir/src/main/java/dk/au/pir/Driver.java +++ b/pir/src/main/java/dk/au/pir/Driver.java @@ -1,10 +1,13 @@ package dk.au.pir; import dk.alexandra.fresco.framework.builder.numeric.field.FieldElement; +import dk.au.pir.protocols.general.interpoly.GeneralInterPolyClient; +import dk.au.pir.protocols.general.interpoly.GeneralInterPolyServer; import dk.au.pir.protocols.interpoly.InterPolyClient; import dk.au.pir.protocols.interpoly.InterPolyServer; import dk.au.pir.settings.PIRSettings; import dk.au.pir.utils.ComparableIntList; +import dk.au.pir.utils.IntegerUtils; import dk.au.pir.utils.ProtocolUtils; import org.w3c.dom.ls.LSOutput; @@ -38,9 +41,16 @@ public class Driver { public static void generalInterPolyTest() { ProtocolUtils utils = new ProtocolUtils(); + GeneralInterPolyClient client = new GeneralInterPolyClient(22); int s = utils.findSizeOfS(2, PIRSettings.DATABASE_SIZE); + int[] database = new int[PIRSettings.DATABASE_SIZE]; - int[][] database = new int[PIRSettings.DATABASE_SIZE][s]; + GeneralInterPolyServer[] servers = new GeneralInterPolyServer[PIRSettings.MOD_BIT_LENGTH + 1]; + client.generateSRandomFieldElements(); + + database[24] = 1; + + int[][] sequences = new int[PIRSettings.DATABASE_SIZE][s]; Set candidates = new HashSet(); @@ -53,14 +63,21 @@ public class Driver { listCandidates.addAll(candidates); Collections.sort(listCandidates); - for (ComparableIntList elem : listCandidates) { - System.out.println(elem); + BigInteger[] results = new BigInteger[PIRSettings.MOD_BIT_LENGTH + 1]; + + + for (int z = 0; z < PIRSettings.MOD_BIT_LENGTH+1; z++) { + servers[z] = new GeneralInterPolyServer(database, sequences); + servers[z].receiveGs(client.sendGs(z)); + results[z] = servers[z].F(); } + BigInteger res = client.receiveResults(results); + System.out.println(res); + } public static void main(String[] args) { - generalInterPolyTest(); } } diff --git a/pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyClient.java b/pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyClient.java index e60b4ce..a5d335f 100644 --- a/pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyClient.java +++ b/pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyClient.java @@ -4,6 +4,7 @@ import dk.au.pir.BigIntegerField; import dk.au.pir.settings.PIRSettings; import dk.au.pir.utils.BigIntegerLagrange; import dk.au.pir.utils.FieldElement; +import dk.au.pir.utils.IntegerUtils; import java.math.BigInteger; import java.util.Random; @@ -29,12 +30,7 @@ public class GeneralInterPolyClient { } public BigInteger g(int l, int z) { - String iString = Integer.toBinaryString(i); - String pads = new String(new char[PIRSettings.MOD_BIT_LENGTH - iString.length()]).replace("\0", "0"); - iString = pads + iString; - char lChar = iString.charAt(iString.length() - 1 - l); // lol - String ilString = Character.toString(lChar); - BigInteger il = new BigInteger(ilString).mod(field.getGroupOrder()); + BigInteger il = BigInteger.valueOf(IntegerUtils.leastSignificantBit(i, l)).mod(field.getGroupOrder()); return field.add(field.multiply(fieldElements[l].getValue(), (BigInteger.valueOf(z))), il); } diff --git a/pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyServer.java b/pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyServer.java new file mode 100644 index 0000000..5bcc2a3 --- /dev/null +++ b/pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyServer.java @@ -0,0 +1,45 @@ +package dk.au.pir.protocols.general.interpoly; + +import dk.au.pir.settings.PIRSettings; +import dk.au.pir.utils.IntegerUtils; + +import java.math.BigInteger; + +public class GeneralInterPolyServer { + private BigInteger[] gs; + private int[][] sequences; + private int[] database; + + public GeneralInterPolyServer(int[] database, int[][] sequences) { + this.sequences = sequences; this.database = database; + } + + public void receiveGs(BigInteger[] gs){ + this.gs = gs; + } + + public BigInteger f(int j){ + + BigInteger product = BigInteger.ONE; + + + for (int l = 0; l < PIRSettings.MOD_BIT_LENGTH; l++) { + if (sequences[j][l] == 1) { + // j(l) * g_l(z) + ((1 - j(l)) * (1 - g_l(z))) + BigInteger tmp = gs[l].multiply(BigInteger.valueOf(sequences[j][l])).add((BigInteger.ONE.subtract(BigInteger.valueOf(sequences[j][l])).multiply(BigInteger.ONE.subtract(gs[l])))); + + product = product.multiply(tmp); + } + } + + return product; + } + public BigInteger F() { + BigInteger sum = BigInteger.ZERO; + for (int j = 0; j < PIRSettings.DATABASE_SIZE; j++) { + // Database consist of 0's or 1' + sum = sum.add(f(j).multiply(BigInteger.valueOf(database[j]))); + } + return sum; + } +} 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 c671391..cf26541 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 @@ -4,6 +4,7 @@ import dk.au.pir.BigIntegerField; import dk.au.pir.settings.PIRSettings; import dk.au.pir.utils.BigIntegerLagrange; import dk.au.pir.utils.FieldElement; +import dk.au.pir.utils.IntegerUtils; import java.math.BigInteger; import java.util.Random; @@ -29,12 +30,7 @@ public class InterPolyClient { } public BigInteger g(int l, int z) { - String iString = Integer.toBinaryString(i); - String pads = new String(new char[PIRSettings.MOD_BIT_LENGTH - iString.length()]).replace("\0", "0"); - iString = pads + iString; - char lChar = iString.charAt(iString.length() - 1 - l); // lol - String ilString = Character.toString(lChar); - BigInteger il = new BigInteger(ilString).mod(field.getGroupOrder()); + BigInteger il = BigInteger.valueOf(IntegerUtils.leastSignificantBit(i, l)).mod(field.getGroupOrder()); return field.add(field.multiply(fieldElements[l].getValue(), (BigInteger.valueOf(z))), il); } 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 5e3ca8f..c3d9c5c 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,6 +1,7 @@ package dk.au.pir.protocols.interpoly; import dk.au.pir.settings.PIRSettings; +import dk.au.pir.utils.IntegerUtils; import java.math.BigInteger; @@ -20,13 +21,7 @@ public class InterPolyServer { public BigInteger f(int j){ BigInteger product = BigInteger.ONE; for (int l = 0; l < PIRSettings.MOD_BIT_LENGTH; l++) { - String iString = Integer.toBinaryString(j); - String pads = new String(new char[PIRSettings.MOD_BIT_LENGTH - iString.length()]).replace("\0", "0"); - iString = pads + iString; - char lChar = iString.charAt(iString.length() - 1 - l); // lol - String jlString = Character.toString(lChar); - - if (jlString.equals("1")){ + if (IntegerUtils.leastSignificantBit(j, l) == 1){ product = product.multiply(gs[l]); } else { product = product.multiply(BigInteger.ONE.subtract(gs[l])); diff --git a/pir/src/main/java/dk/au/pir/utils/IntegerUtils.java b/pir/src/main/java/dk/au/pir/utils/IntegerUtils.java new file mode 100644 index 0000000..a56dcac --- /dev/null +++ b/pir/src/main/java/dk/au/pir/utils/IntegerUtils.java @@ -0,0 +1,9 @@ +package dk.au.pir.utils; + +import dk.au.pir.settings.PIRSettings; + +public class IntegerUtils { + public static int leastSignificantBit(int integer, int index) { + return (integer >> index) & 1; + } +}