1
0
Fork 0
This commit is contained in:
Alexander Munch-Hansen 2019-11-14 10:27:15 +01:00
parent 16144aa9bd
commit 549419a105
7 changed files with 89 additions and 45 deletions

View file

@ -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;
}

View file

@ -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<ComparableIntList> listCandidates = new ArrayList<ComparableIntList>();
listCandidates.addAll(candidates);
ArrayList<ComparableIntList> listCandidates = new ArrayList<ComparableIntList>(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);
}
}

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -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));
}

View file

@ -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;

View file

@ -12,6 +12,10 @@ public class FieldElement {
}
public String toString() {
return "" + value.intValue();
}
public BigInteger getValue() {
return value;
}