From 1dd308df42f22b0413d3c954546aed7241a429de Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Thu, 7 Nov 2019 13:50:52 +0100 Subject: [PATCH] A lot of bullshit --- .../main/java/dk/au/pir/BigIntegerField.java | 7 +- pir/src/main/java/dk/au/pir/Driver.java | 40 +++++++++++- .../interpoly/GeneralInterPolyClient.java | 63 ++++++++++++++++++ .../protocols/interpoly/InterPolyClient.java | 8 +-- .../java/dk/au/pir/settings/PIRSettings.java | 2 +- .../dk/au/pir/utils/ComparableIntList.java | 64 +++++++++++++++++++ .../java/dk/au/pir/utils/FieldElement.java | 22 +++++++ .../java/dk/au/pir/utils/ProtocolUtils.java | 53 +++++++++++++++ 8 files changed, 249 insertions(+), 10 deletions(-) create mode 100644 pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyClient.java create mode 100644 pir/src/main/java/dk/au/pir/utils/ComparableIntList.java create mode 100644 pir/src/main/java/dk/au/pir/utils/FieldElement.java diff --git a/pir/src/main/java/dk/au/pir/BigIntegerField.java b/pir/src/main/java/dk/au/pir/BigIntegerField.java index a7b959e..76d63d6 100644 --- a/pir/src/main/java/dk/au/pir/BigIntegerField.java +++ b/pir/src/main/java/dk/au/pir/BigIntegerField.java @@ -1,5 +1,8 @@ package dk.au.pir; +import dk.au.pir.utils.FieldElement; + +import java.lang.reflect.Field; import java.math.BigInteger; import java.security.SecureRandom; @@ -16,11 +19,11 @@ public class BigIntegerField { this.rand = new SecureRandom(); } - public BigInteger getElement(){ + public FieldElement getElement(){ // TODO: Consider how to make random BigIntegers byte bytes[] = new byte[20]; rand.nextBytes(bytes); - return new BigInteger(bytes).mod(groupOrder); + return new FieldElement(new BigInteger(bytes).mod(groupOrder), groupOrder); } public BigInteger multiply(BigInteger elem1, BigInteger elem2){ diff --git a/pir/src/main/java/dk/au/pir/Driver.java b/pir/src/main/java/dk/au/pir/Driver.java index 246274e..f6b5bf1 100644 --- a/pir/src/main/java/dk/au/pir/Driver.java +++ b/pir/src/main/java/dk/au/pir/Driver.java @@ -4,13 +4,19 @@ import dk.alexandra.fresco.framework.builder.numeric.field.FieldElement; 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.ProtocolUtils; +import org.w3c.dom.ls.LSOutput; import java.math.BigInteger; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; public class Driver { - public static void main(String[] args) { - InterPolyClient client = new InterPolyClient(5); + public static void interPolyTest() { + InterPolyClient client = new InterPolyClient(0); InterPolyServer[] servers = new InterPolyServer[PIRSettings.MOD_BIT_LENGTH + 1]; client.generateSRandomFieldElements(); @@ -19,7 +25,7 @@ public class Driver { BigInteger[] results = new BigInteger[PIRSettings.MOD_BIT_LENGTH + 1]; int[] database = new int[PIRSettings.DATABASE_SIZE]; - database[5] = 1; + database[0] = 1; for (int z = 0; z < PIRSettings.MOD_BIT_LENGTH+1; z++) { servers[z] = new InterPolyServer(database); @@ -29,4 +35,32 @@ public class Driver { BigInteger res = client.receiveResults(results); System.out.println(res); } + + public static void generalInterPolyTest() { + ProtocolUtils utils = new ProtocolUtils(); + int s = utils.findSizeOfS(2, PIRSettings.DATABASE_SIZE); + + int[][] database = new int[PIRSettings.DATABASE_SIZE][s]; + + Set candidates = new HashSet(); + + while (candidates.size() < PIRSettings.DATABASE_SIZE) { + ComparableIntList candidate = utils.insertOnes(new ComparableIntList(s), 2); + candidates.add(candidate); + } + + ArrayList listCandidates = new ArrayList(); + listCandidates.addAll(candidates); + Collections.sort(listCandidates); + + for (ComparableIntList elem : listCandidates) { + System.out.println(elem); + } + + } + + 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 new file mode 100644 index 0000000..35e01c3 --- /dev/null +++ b/pir/src/main/java/dk/au/pir/protocols/general/interpoly/GeneralInterPolyClient.java @@ -0,0 +1,63 @@ +package dk.au.pir.protocols.general.interpoly; + +import dk.au.pir.BigIntegerField; +import dk.au.pir.settings.PIRSettings; +import dk.au.pir.utils.BigIntegerLagrangeInterpolation; +import dk.au.pir.utils.FieldElement; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Random; + +public class GeneralInterPolyClient { + + private final int i; + private Random random = new Random(); + private BigIntegerField field; + + + private FieldElement[] fieldElements = new FieldElement[PIRSettings.MOD_BIT_LENGTH]; + + public GeneralInterPolyClient(int i) { + this.i = i; + this.field = new BigIntegerField(); + } + + public void generateSRandomFieldElements() { + for (int i = 0; i < fieldElements.length; i++) { + this.fieldElements[i] = field.getElement(); + } + } + + 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()); + return field.add(field.multiply(fieldElements[l].getValue(), (BigInteger.valueOf(z))), il); + } + + public BigInteger[] sendGs(int z){ + BigInteger[] gs = new BigInteger[PIRSettings.MOD_BIT_LENGTH]; + for (int l = 0; l < PIRSettings.MOD_BIT_LENGTH; l++) { + gs[l] = g(l, z); + } + return gs; + } + + public FieldElement[] getFieldElements() { + return fieldElements; + } + + + public BigInteger receiveResults(BigInteger[] results) { + BigInteger[] fuckingXs = new BigInteger[PIRSettings.MOD_BIT_LENGTH + 1]; + for (int i = 0; i < PIRSettings.MOD_BIT_LENGTH + 1; i++) { + fuckingXs[i] = BigInteger.valueOf(i); + } + return BigIntegerLagrangeInterpolation.doIt(fuckingXs, results); + } +} + 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 c66a557..ac583e1 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 @@ -3,11 +3,11 @@ package dk.au.pir.protocols.interpoly; import dk.au.pir.BigIntegerField; import dk.au.pir.settings.PIRSettings; import dk.au.pir.utils.BigIntegerLagrangeInterpolation; +import dk.au.pir.utils.FieldElement; import java.math.BigInteger; import java.util.ArrayList; import java.util.Random; -import java.util.concurrent.ThreadLocalRandom; public class InterPolyClient { @@ -16,7 +16,7 @@ public class InterPolyClient { private BigIntegerField field; - private BigInteger[] fieldElements = new BigInteger[PIRSettings.MOD_BIT_LENGTH]; + private FieldElement[] fieldElements = new FieldElement[PIRSettings.MOD_BIT_LENGTH]; public InterPolyClient(int i) { this.i = i; @@ -36,7 +36,7 @@ public class InterPolyClient { char lChar = iString.charAt(iString.length() - 1 - l); // lol String ilString = Character.toString(lChar); BigInteger il = new BigInteger(ilString).mod(field.getGroupOrder()); - return field.add(field.multiply(fieldElements[l], (BigInteger.valueOf(z))), il); + return field.add(field.multiply(fieldElements[l].getValue(), (BigInteger.valueOf(z))), il); } public BigInteger[] sendGs(int z){ @@ -47,7 +47,7 @@ public class InterPolyClient { return gs; } - public BigInteger[] getFieldElements() { + public FieldElement[] getFieldElements() { return fieldElements; } diff --git a/pir/src/main/java/dk/au/pir/settings/PIRSettings.java b/pir/src/main/java/dk/au/pir/settings/PIRSettings.java index a0c3b79..a703ff7 100644 --- a/pir/src/main/java/dk/au/pir/settings/PIRSettings.java +++ b/pir/src/main/java/dk/au/pir/settings/PIRSettings.java @@ -7,7 +7,7 @@ import dk.alexandra.fresco.framework.util.ModulusFinder; import java.math.BigInteger; public class PIRSettings { - public static final int MOD_BIT_LENGTH = 16; // s + public static final int MOD_BIT_LENGTH = 8; // s public static final int DATABASE_SIZE = (int) Math.pow(2, MOD_BIT_LENGTH); // n public static final int MAX_BIT_LENGTH = 512; diff --git a/pir/src/main/java/dk/au/pir/utils/ComparableIntList.java b/pir/src/main/java/dk/au/pir/utils/ComparableIntList.java new file mode 100644 index 0000000..bc6498e --- /dev/null +++ b/pir/src/main/java/dk/au/pir/utils/ComparableIntList.java @@ -0,0 +1,64 @@ +package dk.au.pir.utils; + +import java.util.Arrays; + +public class ComparableIntList implements Comparable { + + private int[] list; + + public ComparableIntList(int size) { + list = new int[size]; + } + + + public int[] getList() { + return list; + } + + public int length() { + return list.length; + } + + public String toString() { + StringBuilder tmp = new StringBuilder(); + for (int i : list) { + tmp.append(i); + } + return tmp.toString(); + } + + @Override + public int hashCode() { + // TODO: Only works for k=1 atm + for (int i = 0; i < list.length; i++) { + if (list[i] == 1) { + return i; + } + } + return -1; + } + + @Override + public boolean equals(Object other) { + ComparableIntList cit = (ComparableIntList) other; + for (int i = 0; i < list.length; i++) { + if (list[i] != cit.list[i]) { + return false; + } + } + return true; + + } + + + public int compareTo(ComparableIntList o) { + for (int i = 0; i < list.length; i++) { + int diff = this.list[i] - o.list[i]; + if (diff == 0) { + continue; + } + return diff; + } + return 0; + } +} diff --git a/pir/src/main/java/dk/au/pir/utils/FieldElement.java b/pir/src/main/java/dk/au/pir/utils/FieldElement.java new file mode 100644 index 0000000..833013a --- /dev/null +++ b/pir/src/main/java/dk/au/pir/utils/FieldElement.java @@ -0,0 +1,22 @@ +package dk.au.pir.utils; + +import java.math.BigInteger; + +public class FieldElement { + private BigInteger value; + private BigInteger modulus; + + public FieldElement(BigInteger value, BigInteger modulus) { + this.value = value; + this.modulus = modulus; + } + + + public BigInteger getValue() { + return value; + } + + public BigInteger getModulus() { + return modulus; + } +} diff --git a/pir/src/main/java/dk/au/pir/utils/ProtocolUtils.java b/pir/src/main/java/dk/au/pir/utils/ProtocolUtils.java index abb5462..bc4ce09 100644 --- a/pir/src/main/java/dk/au/pir/utils/ProtocolUtils.java +++ b/pir/src/main/java/dk/au/pir/utils/ProtocolUtils.java @@ -1,10 +1,63 @@ package dk.au.pir.utils; +import java.util.ArrayList; +import java.util.Random; + public class ProtocolUtils { + + private Random rand; + + public ProtocolUtils() { + this.rand = new Random(); + } + public static int kronecker(int i, int j) { if (i == j) { return 1; } return 0; } + + + public int binop(int n, int k) { + if (k < 0 || k > n) + return 0; + + if (k == 0 || k == n) + return 1; + + k = Math.min(k, n-k); + int c = 1; + + for (int i = 0; i < k; i++) + c = c * (n-i) / (i+1); + + return c; + } + + public int findSizeOfS(int k, int n) { + int s = 0; + while ((binop(s, k-1))< n) { + s++; + } + return s; + } + + public ComparableIntList insertOnes(ComparableIntList sequence, int k) { + + ArrayList picked = new ArrayList(); + int i = 0; + while (i < k-1) { + int randomI = rand.nextInt(sequence.length()); + if (picked.contains(randomI)) { + continue; + } + + picked.add(randomI); + sequence.getList()[randomI] = 1; + i++; + } + + return sequence; + } }