From 549419a105c3b7400c75dc7dd6a58de6bda19409 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Thu, 14 Nov 2019 10:27:15 +0100 Subject: [PATCH] more --- .../main/java/dk/au/pir/BigIntegerField.java | 11 ++++- pir/src/main/java/dk/au/pir/Driver.java | 45 +++++++++++-------- .../interpoly/GeneralInterPolyClient.java | 26 ++++++----- .../interpoly/GeneralInterPolyServer.java | 40 ++++++++++++----- .../java/dk/au/pir/settings/PIRSettings.java | 5 ++- .../dk/au/pir/utils/BigIntegerLagrange.java | 3 ++ .../java/dk/au/pir/utils/FieldElement.java | 4 ++ 7 files changed, 89 insertions(+), 45 deletions(-) diff --git a/pir/src/main/java/dk/au/pir/BigIntegerField.java b/pir/src/main/java/dk/au/pir/BigIntegerField.java index 76d63d6..00e1046 100644 --- a/pir/src/main/java/dk/au/pir/BigIntegerField.java +++ b/pir/src/main/java/dk/au/pir/BigIntegerField.java @@ -15,8 +15,8 @@ public class BigIntegerField { private static final BigInteger THREE = BigInteger.valueOf(3); public BigIntegerField(){ - this.groupOrder = new BigInteger("65519"); - this.rand = new SecureRandom(); + this.groupOrder = new BigInteger("5"); + this.rand = new SecureRandom(new byte[] {12}); } public FieldElement getElement(){ @@ -27,14 +27,21 @@ public class BigIntegerField { } public BigInteger multiply(BigInteger elem1, BigInteger elem2){ + return elem1.multiply(elem2).mod(groupOrder); } public BigInteger add(BigInteger elem1, BigInteger elem2){ + return elem1.add(elem2).mod(groupOrder); } + public BigInteger subtract(BigInteger elem1, BigInteger elem2){ + return elem1.subtract(elem2).mod(groupOrder); + } + public BigInteger getGroupOrder(){ + return groupOrder; } diff --git a/pir/src/main/java/dk/au/pir/Driver.java b/pir/src/main/java/dk/au/pir/Driver.java index e0979fa..4e6f67d 100644 --- a/pir/src/main/java/dk/au/pir/Driver.java +++ b/pir/src/main/java/dk/au/pir/Driver.java @@ -12,14 +12,11 @@ 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; +import java.util.*; public class Driver { public static void interPolyTest() { - InterPolyClient client = new InterPolyClient(0); + InterPolyClient client = new InterPolyClient(1); InterPolyServer[] servers = new InterPolyServer[PIRSettings.MOD_BIT_LENGTH + 1]; client.generateSRandomFieldElements(); @@ -39,17 +36,20 @@ public class Driver { System.out.println(res); } - public static void generalInterPolyTest() { + private static int generalInterPolyTest(int ip) { ProtocolUtils utils = new ProtocolUtils(); - int s = utils.findSizeOfS(2, PIRSettings.DATABASE_SIZE); - GeneralInterPolyClient client = new GeneralInterPolyClient(22, s); + int s = utils.findSizeOfS(PIRSettings.NUM_SERVERS, PIRSettings.DATABASE_SIZE); + System.out.println("s: "+s); + + GeneralInterPolyClient client = new GeneralInterPolyClient(1, s); int[] database = new int[PIRSettings.DATABASE_SIZE]; + System.out.println("db_size: " +PIRSettings.DATABASE_SIZE); - GeneralInterPolyServer[] servers = new GeneralInterPolyServer[PIRSettings.MOD_BIT_LENGTH + 1]; + GeneralInterPolyServer[] servers = new GeneralInterPolyServer[PIRSettings.NUM_SERVERS]; client.generateSRandomFieldElements(); - database[24] = 1; + database[1] = 1; int[][] sequences = new int[PIRSettings.DATABASE_SIZE][s]; @@ -60,25 +60,34 @@ public class Driver { candidates.add(candidate); } - ArrayList listCandidates = new ArrayList(); - listCandidates.addAll(candidates); + ArrayList listCandidates = new ArrayList(candidates); Collections.sort(listCandidates); - BigInteger[] results = new BigInteger[PIRSettings.MOD_BIT_LENGTH + 1]; + for (int i = 0; i < sequences.length; i++) { + sequences[i] = listCandidates.get(i).getList(); + } + BigInteger[] results = new BigInteger[PIRSettings.NUM_SERVERS]; - for (int z = 0; z < PIRSettings.MOD_BIT_LENGTH+1; z++) { - servers[z] = new GeneralInterPolyServer(database, sequences); + for (int z = 0; z < PIRSettings.NUM_SERVERS; z++) { + servers[z] = new GeneralInterPolyServer(database, sequences, s); servers[z].receiveGs(client.sendGs(z)); results[z] = servers[z].F(); } - BigInteger res = client.receiveResults(results); + BigInteger res = client.receiveResults(results).mod(client.getFieldElements()[0].getModulus()); // lol System.out.println(res); - + return res.intValue(); } public static void main(String[] args) { - generalInterPolyTest(); + int sum = 0; + for (int i = 0; i < 1; i++) { + int res = generalInterPolyTest(i); + if (res == 1) { + sum += 1; + } + } + System.out.println("sum: " + sum); } } 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 412e463..fbc45d5 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 @@ -7,12 +7,12 @@ import dk.au.pir.utils.FieldElement; import dk.au.pir.utils.IntegerUtils; import java.math.BigInteger; -import java.util.Random; +import java.util.Arrays; public class GeneralInterPolyClient { private final int i; - private Random random = new Random(); + private final int s; private BigIntegerField field; @@ -21,23 +21,25 @@ public class GeneralInterPolyClient { public GeneralInterPolyClient(int i, int s) { this.i = i; fieldElements = new FieldElement[s]; + this.s = s; this.field = new BigIntegerField(); } public void generateSRandomFieldElements() { - for (int i = 0; i < fieldElements.length; i++) { - this.fieldElements[i] = field.getElement(); + for (int j = 0; j < fieldElements.length; j++) { + this.fieldElements[j] = field.getElement(); } + System.out.println("randoms: "+Arrays.deepToString(fieldElements)); } private BigInteger g(int l, int z) { - BigInteger il = BigInteger.valueOf(IntegerUtils.leastSignificantBit(i, l)).mod(field.getGroupOrder()); - return field.add(field.multiply(fieldElements[l].getValue(), (BigInteger.valueOf(z))), il); + BigInteger il = BigInteger.valueOf(IntegerUtils.leastSignificantBit(this.i, l)); + return field.add(field.multiply(fieldElements[l].getValue(), (BigInteger.valueOf(z+1))), il); } public BigInteger[] sendGs(int z){ - BigInteger[] gs = new BigInteger[PIRSettings.MOD_BIT_LENGTH]; - for (int l = 0; l < PIRSettings.MOD_BIT_LENGTH; l++) { + BigInteger[] gs = new BigInteger[this.s]; + for (int l = 0; l < this.s; l++) { gs[l] = g(l, z); } return gs; @@ -49,11 +51,11 @@ public class GeneralInterPolyClient { 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); + BigInteger[] xs = new BigInteger[PIRSettings.NUM_SERVERS]; + for (int j = 0; j < PIRSettings.NUM_SERVERS; j++) { + xs[j] = BigInteger.valueOf(j+1); } - return BigIntegerLagrange.interpolate(fuckingXs, results); + return BigIntegerLagrange.interpolate(xs, results); } } 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 index 5bcc2a3..e675b4e 100644 --- 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 @@ -1,45 +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.IntegerUtils; - import java.math.BigInteger; public class GeneralInterPolyServer { + private final int sequenceLength; private BigInteger[] gs; private int[][] sequences; private int[] database; + private BigIntegerField field; - public GeneralInterPolyServer(int[] database, int[][] sequences) { - this.sequences = sequences; this.database = database; + public GeneralInterPolyServer(int[] database, int[][] sequences, int sequenceLength) { + this.sequences = sequences; this.database = database;; this.sequenceLength = sequenceLength; + this.field = new BigIntegerField(); } public void receiveGs(BigInteger[] gs){ this.gs = gs; } - public BigInteger f(int j){ + private BigInteger f(int j){ BigInteger product = BigInteger.ONE; + boolean changed = false; - for (int l = 0; l < PIRSettings.MOD_BIT_LENGTH; l++) { + for (int l = 0; l < sequenceLength; l++) { if (sequences[j][l] == 1) { + changed = true; // 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])))); + BigInteger a = BigInteger.valueOf(sequences[j][l]); + BigInteger b = gs[l]; + BigInteger c = field.subtract(BigInteger.ONE, BigInteger.valueOf(sequences[j][l])); + BigInteger d = field.subtract(BigInteger.ONE, gs[l]); - product = product.multiply(tmp); + BigInteger x = field.multiply(a, b); + BigInteger y = field.multiply(c, d); + BigInteger z = field.add(x, y); + + //product = field.multiply(product, z); + product = field.multiply(product, b); + System.out.println("gs: "+gs[l]); } - } + } + if (!changed) + return BigInteger.ZERO; + System.out.println("product: "+product); 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]))); + BigInteger plz = field.multiply(f(j), BigInteger.valueOf(this.database[j])); + sum = field.add(sum, plz); + //System.out.println(sum.longValue()); } + System.out.println("pls"); return sum; } } 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 a703ff7..fd80cd7 100644 --- a/pir/src/main/java/dk/au/pir/settings/PIRSettings.java +++ b/pir/src/main/java/dk/au/pir/settings/PIRSettings.java @@ -7,9 +7,10 @@ import dk.alexandra.fresco.framework.util.ModulusFinder; import java.math.BigInteger; public class PIRSettings { - public static final int MOD_BIT_LENGTH = 8; // s + public static final int MOD_BIT_LENGTH = 2; // s public static final int DATABASE_SIZE = (int) Math.pow(2, MOD_BIT_LENGTH); // n public static final int MAX_BIT_LENGTH = 512; + public static final int NUM_SERVERS = 2; - public static FieldDefinition FIELD_DEFINITION = new BigIntegerFieldDefinition(ModulusFinder.findSuitableModulus(MOD_BIT_LENGTH)); + //public static FieldDefinition FIELD_DEFINITION = new BigIntegerFieldDefinition(ModulusFinder.findSuitableModulus(MOD_BIT_LENGTH)); } diff --git a/pir/src/main/java/dk/au/pir/utils/BigIntegerLagrange.java b/pir/src/main/java/dk/au/pir/utils/BigIntegerLagrange.java index 47c35df..1158602 100644 --- a/pir/src/main/java/dk/au/pir/utils/BigIntegerLagrange.java +++ b/pir/src/main/java/dk/au/pir/utils/BigIntegerLagrange.java @@ -1,9 +1,12 @@ package dk.au.pir.utils; import java.math.BigInteger; +import java.util.Arrays; public class BigIntegerLagrange { public static BigInteger interpolate(BigInteger[] x, BigInteger[] y) { + System.out.println("x: " + Arrays.deepToString(x)); + System.out.println("y: " + Arrays.deepToString(y)); // https://stackoverflow.com/questions/16375163/lagrange-interpolation-in-java BigInteger xPoint = BigInteger.ZERO; // we want to find f(0), so xpoint=0 BigInteger sum = BigInteger.ZERO; diff --git a/pir/src/main/java/dk/au/pir/utils/FieldElement.java b/pir/src/main/java/dk/au/pir/utils/FieldElement.java index 833013a..39aeed9 100644 --- a/pir/src/main/java/dk/au/pir/utils/FieldElement.java +++ b/pir/src/main/java/dk/au/pir/utils/FieldElement.java @@ -12,6 +12,10 @@ public class FieldElement { } + public String toString() { + return "" + value.intValue(); + } + public BigInteger getValue() { return value; }