From fd92333095605758f5870a8be6d78c38cc551fc1 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 15 Nov 2019 11:11:01 +0100 Subject: [PATCH] Finish --- pir/src/main/java/dk/au/pir/BigIntegerField.java | 4 ++-- pir/src/main/java/dk/au/pir/Driver.java | 8 +++++--- .../pir/protocols/interpoly/InterPolyClient.java | 11 ++++++++--- .../pir/protocols/interpoly/InterPolyDatabase.java | 10 +++++++++- .../pir/protocols/interpoly/InterPolyServer.java | 2 +- .../main/java/dk/au/pir/utils/FieldElement.java | 14 ++++++++++++++ .../java/dk/au/pir/utils/FieldElementLagrange.java | 4 ++-- 7 files changed, 41 insertions(+), 12 deletions(-) diff --git a/pir/src/main/java/dk/au/pir/BigIntegerField.java b/pir/src/main/java/dk/au/pir/BigIntegerField.java index a0ac7f3..1bfdbef 100644 --- a/pir/src/main/java/dk/au/pir/BigIntegerField.java +++ b/pir/src/main/java/dk/au/pir/BigIntegerField.java @@ -10,13 +10,13 @@ public class BigIntegerField { private final SecureRandom rand; public BigIntegerField() { - this.groupOrder = new BigInteger("5"); + this.groupOrder = new BigInteger("29"); this.rand = new SecureRandom(new byte[] {12}); // TODO: DOnt hardcode seed, lmao } public FieldElement getElement(){ // TODO: Consider how to make random BigIntegers - byte bytes[] = new byte[20]; + byte[] bytes = new byte[20]; rand.nextBytes(bytes); return new FieldElement(new BigInteger(bytes), groupOrder); } diff --git a/pir/src/main/java/dk/au/pir/Driver.java b/pir/src/main/java/dk/au/pir/Driver.java index 94298f1..481443f 100644 --- a/pir/src/main/java/dk/au/pir/Driver.java +++ b/pir/src/main/java/dk/au/pir/Driver.java @@ -7,7 +7,7 @@ import dk.au.pir.settings.PIRSettings; public class Driver { private static int generalInterPolyTest(int ip) { - PIRSettings settings = new PIRSettings(4, 2); + PIRSettings settings = new PIRSettings(8, 3); int s = settings.getS(); System.out.println("s is: " + s); @@ -19,12 +19,14 @@ public class Driver { } InterPolyClient client = new InterPolyClient(settings, servers); - int res = client.receive(0); + int res = client.receive(2); System.out.println("res: " + res); return res; } public static void main(String[] args) { + generalInterPolyTest(0); + /* int sum = 0; for (int i = 0; i < 1; i++) { int res = generalInterPolyTest(i); @@ -32,6 +34,6 @@ public class Driver { sum += 1; } } - System.out.println("sum: " + sum); + System.out.println("sum: " + 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 bbc0b7d..a1494d3 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 @@ -7,6 +7,8 @@ import dk.au.pir.utils.FieldElementLagrange; import java.math.BigInteger; +import static dk.au.pir.utils.ProtocolUtils.printIntArrayArray; + public class InterPolyClient { private PIRSettings settings; private InterPolyServer[] servers; @@ -21,6 +23,8 @@ public class InterPolyClient { this.s = settings.getS(); this.field = settings.getField(); this.sequences = settings.getSequences(); + System.out.println("Sequences"); + printIntArrayArray(this.sequences); } private FieldElement[] getRandomFieldElements() { @@ -31,11 +35,12 @@ public class InterPolyClient { return fieldElements; } - private FieldElement[] getGs(int index, int serverNumber, FieldElement random) { + private FieldElement[] getGs(int index, int serverNumber, FieldElement[] random) { FieldElement[] gs = new FieldElement[this.s]; int[] i = this.sequences[index]; for (int l = 0; l < this.s; l++) { - gs[l] = random.multiply(this.field.valueOf(serverNumber)).add(this.field.valueOf(i[l])); + + gs[l] = random[l].multiply(this.field.valueOf(serverNumber)).add(this.field.valueOf(i[l])); } return gs; @@ -45,7 +50,7 @@ public class InterPolyClient { FieldElement[] randoms = this.getRandomFieldElements(); FieldElement[] Fs = new FieldElement[this.servers.length]; for (int z = 0; z < this.servers.length; z++) { - Fs[z] = this.servers[z].F(this.getGs(index, z+1, randoms[z])); + Fs[z] = this.servers[z].F(this.getGs(index, z+1, randoms)); } FieldElement res = FieldElementLagrange.interpolate(this.field, Fs); return res.getValue().intValue(); diff --git a/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyDatabase.java b/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyDatabase.java index d2606c8..86ddd74 100644 --- a/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyDatabase.java +++ b/pir/src/main/java/dk/au/pir/protocols/interpoly/InterPolyDatabase.java @@ -2,12 +2,20 @@ package dk.au.pir.protocols.interpoly; import dk.au.pir.settings.PIRSettings; +import static dk.au.pir.utils.ProtocolUtils.printIntArrayArray; + public class InterPolyDatabase { private final int[] x; public InterPolyDatabase(PIRSettings settings) { this.x = new int[settings.getDatabaseSize()]; - this.x[1] = 1; + this.x[2] = 1; + int[][] y = new int[1][settings.getDatabaseSize()]; + y[0]= this.x; + System.out.println("This is the database"); + printIntArrayArray(y); + System.out.println(); + } public int[] getX() { 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 3412cb1..423616f 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 @@ -23,7 +23,7 @@ public class InterPolyServer { for (int l = 0; l < this.settings.getS(); l++) { if (this.settings.getSequences()[j][l] == 1) { product = product.multiply(gs[l]); - System.out.println("gs: " + gs[l]); + //System.out.println("gs: " + gs[l]); } } sum = sum.add(product.multiply(this.field.valueOf(this.database.getX()[j]))); 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 bc2e90e..dd19247 100644 --- a/pir/src/main/java/dk/au/pir/utils/FieldElement.java +++ b/pir/src/main/java/dk/au/pir/utils/FieldElement.java @@ -27,6 +27,20 @@ public class FieldElement { return new FieldElement(this.value.divide(other.value).mod(this.modulus), this.modulus); } + public FieldElement multInverse() { + BigInteger res = BigInteger.ONE; + int j = 0; + for (BigInteger i = BigInteger.ZERO; i.compareTo(this.modulus) < 0; i = i.add(BigInteger.ONE)) { + BigInteger tempRes = this.value.multiply(i).mod(this.modulus); + if (tempRes.equals(BigInteger.ONE)) { + res = i; + break; + } + j++; + } + return new FieldElement(res, this.modulus); + } + public String toString() { return "" + value.intValue(); } diff --git a/pir/src/main/java/dk/au/pir/utils/FieldElementLagrange.java b/pir/src/main/java/dk/au/pir/utils/FieldElementLagrange.java index cd0578b..e06b7f4 100644 --- a/pir/src/main/java/dk/au/pir/utils/FieldElementLagrange.java +++ b/pir/src/main/java/dk/au/pir/utils/FieldElementLagrange.java @@ -7,7 +7,6 @@ import java.util.Arrays; public class FieldElementLagrange { public static FieldElement interpolate(BigIntegerField field, FieldElement[] y) { System.out.println("y: " + Arrays.deepToString(y)); - // https://stackoverflow.com/questions/16375163/lagrange-interpolation-in-java FieldElement xPoint = field.valueOf(0); // we want to find f(0), so xpoint=0 FieldElement sum = field.valueOf(0); FieldElement product = field.valueOf(1); @@ -16,7 +15,7 @@ public class FieldElementLagrange { if (j != i) { FieldElement a = xPoint.subtract(field.valueOf(j+1)); FieldElement b = field.valueOf(i+1).subtract(field.valueOf(j+1)); - product = product.multiply(a.divide(b)); + product = product.multiply(a.multiply(b.multInverse())); } } sum = sum.add(product.multiply(y[i])); @@ -25,3 +24,4 @@ public class FieldElementLagrange { return sum; } } +