Finish
This commit is contained in:
parent
96e529294f
commit
fd92333095
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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); */
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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])));
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user