1
0
Fork 0
This commit is contained in:
Thomas 2019-11-15 11:11:01 +01:00
parent 96e529294f
commit fd92333095
7 changed files with 41 additions and 12 deletions

View file

@ -10,13 +10,13 @@ public class BigIntegerField {
private final SecureRandom rand; private final SecureRandom rand;
public BigIntegerField() { public BigIntegerField() {
this.groupOrder = new BigInteger("5"); this.groupOrder = new BigInteger("29");
this.rand = new SecureRandom(new byte[] {12}); // TODO: DOnt hardcode seed, lmao this.rand = new SecureRandom(new byte[] {12}); // TODO: DOnt hardcode seed, lmao
} }
public FieldElement getElement(){ public FieldElement getElement(){
// TODO: Consider how to make random BigIntegers // TODO: Consider how to make random BigIntegers
byte bytes[] = new byte[20]; byte[] bytes = new byte[20];
rand.nextBytes(bytes); rand.nextBytes(bytes);
return new FieldElement(new BigInteger(bytes), groupOrder); return new FieldElement(new BigInteger(bytes), groupOrder);
} }

View file

@ -7,7 +7,7 @@ import dk.au.pir.settings.PIRSettings;
public class Driver { public class Driver {
private static int generalInterPolyTest(int ip) { private static int generalInterPolyTest(int ip) {
PIRSettings settings = new PIRSettings(4, 2); PIRSettings settings = new PIRSettings(8, 3);
int s = settings.getS(); int s = settings.getS();
System.out.println("s is: " + s); System.out.println("s is: " + s);
@ -19,12 +19,14 @@ public class Driver {
} }
InterPolyClient client = new InterPolyClient(settings, servers); InterPolyClient client = new InterPolyClient(settings, servers);
int res = client.receive(0); int res = client.receive(2);
System.out.println("res: " + res); System.out.println("res: " + res);
return res; return res;
} }
public static void main(String[] args) { public static void main(String[] args) {
generalInterPolyTest(0);
/*
int sum = 0; int sum = 0;
for (int i = 0; i < 1; i++) { for (int i = 0; i < 1; i++) {
int res = generalInterPolyTest(i); int res = generalInterPolyTest(i);
@ -32,6 +34,6 @@ public class Driver {
sum += 1; sum += 1;
} }
} }
System.out.println("sum: " + sum); System.out.println("sum: " + sum); */
} }
} }

View file

@ -7,6 +7,8 @@ import dk.au.pir.utils.FieldElementLagrange;
import java.math.BigInteger; import java.math.BigInteger;
import static dk.au.pir.utils.ProtocolUtils.printIntArrayArray;
public class InterPolyClient { public class InterPolyClient {
private PIRSettings settings; private PIRSettings settings;
private InterPolyServer[] servers; private InterPolyServer[] servers;
@ -21,6 +23,8 @@ public class InterPolyClient {
this.s = settings.getS(); this.s = settings.getS();
this.field = settings.getField(); this.field = settings.getField();
this.sequences = settings.getSequences(); this.sequences = settings.getSequences();
System.out.println("Sequences");
printIntArrayArray(this.sequences);
} }
private FieldElement[] getRandomFieldElements() { private FieldElement[] getRandomFieldElements() {
@ -31,11 +35,12 @@ public class InterPolyClient {
return fieldElements; 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]; FieldElement[] gs = new FieldElement[this.s];
int[] i = this.sequences[index]; int[] i = this.sequences[index];
for (int l = 0; l < this.s; l++) { 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; return gs;
@ -45,7 +50,7 @@ public class InterPolyClient {
FieldElement[] randoms = this.getRandomFieldElements(); FieldElement[] randoms = this.getRandomFieldElements();
FieldElement[] Fs = new FieldElement[this.servers.length]; FieldElement[] Fs = new FieldElement[this.servers.length];
for (int z = 0; z < this.servers.length; z++) { 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); FieldElement res = FieldElementLagrange.interpolate(this.field, Fs);
return res.getValue().intValue(); return res.getValue().intValue();

View file

@ -2,12 +2,20 @@ package dk.au.pir.protocols.interpoly;
import dk.au.pir.settings.PIRSettings; import dk.au.pir.settings.PIRSettings;
import static dk.au.pir.utils.ProtocolUtils.printIntArrayArray;
public class InterPolyDatabase { public class InterPolyDatabase {
private final int[] x; private final int[] x;
public InterPolyDatabase(PIRSettings settings) { public InterPolyDatabase(PIRSettings settings) {
this.x = new int[settings.getDatabaseSize()]; 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() { public int[] getX() {

View file

@ -23,7 +23,7 @@ public class InterPolyServer {
for (int l = 0; l < this.settings.getS(); l++) { for (int l = 0; l < this.settings.getS(); l++) {
if (this.settings.getSequences()[j][l] == 1) { if (this.settings.getSequences()[j][l] == 1) {
product = product.multiply(gs[l]); 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]))); sum = sum.add(product.multiply(this.field.valueOf(this.database.getX()[j])));

View file

@ -27,6 +27,20 @@ public class FieldElement {
return new FieldElement(this.value.divide(other.value).mod(this.modulus), this.modulus); 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() { public String toString() {
return "" + value.intValue(); return "" + value.intValue();
} }

View file

@ -7,7 +7,6 @@ import java.util.Arrays;
public class FieldElementLagrange { public class FieldElementLagrange {
public static FieldElement interpolate(BigIntegerField field, FieldElement[] y) { public static FieldElement interpolate(BigIntegerField field, FieldElement[] y) {
System.out.println("y: " + Arrays.deepToString(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 xPoint = field.valueOf(0); // we want to find f(0), so xpoint=0
FieldElement sum = field.valueOf(0); FieldElement sum = field.valueOf(0);
FieldElement product = field.valueOf(1); FieldElement product = field.valueOf(1);
@ -16,7 +15,7 @@ public class FieldElementLagrange {
if (j != i) { if (j != i) {
FieldElement a = xPoint.subtract(field.valueOf(j+1)); FieldElement a = xPoint.subtract(field.valueOf(j+1));
FieldElement b = field.valueOf(i+1).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])); sum = sum.add(product.multiply(y[i]));
@ -25,3 +24,4 @@ public class FieldElementLagrange {
return sum; return sum;
} }
} }