1
0
Fork 0

A lot of bullshit

This commit is contained in:
Alexander Munch-Hansen 2019-11-07 13:50:52 +01:00
parent 82cfc32f93
commit 1dd308df42
8 changed files with 249 additions and 10 deletions

View file

@ -1,5 +1,8 @@
package dk.au.pir;
import dk.au.pir.utils.FieldElement;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.security.SecureRandom;
@ -16,11 +19,11 @@ public class BigIntegerField {
this.rand = new SecureRandom();
}
public BigInteger getElement(){
public FieldElement getElement(){
// TODO: Consider how to make random BigIntegers
byte bytes[] = new byte[20];
rand.nextBytes(bytes);
return new BigInteger(bytes).mod(groupOrder);
return new FieldElement(new BigInteger(bytes).mod(groupOrder), groupOrder);
}
public BigInteger multiply(BigInteger elem1, BigInteger elem2){

View file

@ -4,13 +4,19 @@ import dk.alexandra.fresco.framework.builder.numeric.field.FieldElement;
import dk.au.pir.protocols.interpoly.InterPolyClient;
import dk.au.pir.protocols.interpoly.InterPolyServer;
import dk.au.pir.settings.PIRSettings;
import dk.au.pir.utils.ComparableIntList;
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;
public class Driver {
public static void main(String[] args) {
InterPolyClient client = new InterPolyClient(5);
public static void interPolyTest() {
InterPolyClient client = new InterPolyClient(0);
InterPolyServer[] servers = new InterPolyServer[PIRSettings.MOD_BIT_LENGTH + 1];
client.generateSRandomFieldElements();
@ -19,7 +25,7 @@ public class Driver {
BigInteger[] results = new BigInteger[PIRSettings.MOD_BIT_LENGTH + 1];
int[] database = new int[PIRSettings.DATABASE_SIZE];
database[5] = 1;
database[0] = 1;
for (int z = 0; z < PIRSettings.MOD_BIT_LENGTH+1; z++) {
servers[z] = new InterPolyServer(database);
@ -29,4 +35,32 @@ public class Driver {
BigInteger res = client.receiveResults(results);
System.out.println(res);
}
public static void generalInterPolyTest() {
ProtocolUtils utils = new ProtocolUtils();
int s = utils.findSizeOfS(2, PIRSettings.DATABASE_SIZE);
int[][] database = new int[PIRSettings.DATABASE_SIZE][s];
Set<ComparableIntList> candidates = new HashSet<ComparableIntList>();
while (candidates.size() < PIRSettings.DATABASE_SIZE) {
ComparableIntList candidate = utils.insertOnes(new ComparableIntList(s), 2);
candidates.add(candidate);
}
ArrayList<ComparableIntList> listCandidates = new ArrayList<ComparableIntList>();
listCandidates.addAll(candidates);
Collections.sort(listCandidates);
for (ComparableIntList elem : listCandidates) {
System.out.println(elem);
}
}
public static void main(String[] args) {
generalInterPolyTest();
}
}

View file

@ -0,0 +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.BigIntegerLagrangeInterpolation;
import dk.au.pir.utils.FieldElement;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Random;
public class GeneralInterPolyClient {
private final int i;
private Random random = new Random();
private BigIntegerField field;
private FieldElement[] fieldElements = new FieldElement[PIRSettings.MOD_BIT_LENGTH];
public GeneralInterPolyClient(int i) {
this.i = i;
this.field = new BigIntegerField();
}
public void generateSRandomFieldElements() {
for (int i = 0; i < fieldElements.length; i++) {
this.fieldElements[i] = field.getElement();
}
}
public BigInteger g(int l, int z) {
String iString = Integer.toBinaryString(i);
String pads = new String(new char[PIRSettings.MOD_BIT_LENGTH - iString.length()]).replace("\0", "0");
iString = pads + iString;
char lChar = iString.charAt(iString.length() - 1 - l); // lol
String ilString = Character.toString(lChar);
BigInteger il = new BigInteger(ilString).mod(field.getGroupOrder());
return field.add(field.multiply(fieldElements[l].getValue(), (BigInteger.valueOf(z))), il);
}
public BigInteger[] sendGs(int z){
BigInteger[] gs = new BigInteger[PIRSettings.MOD_BIT_LENGTH];
for (int l = 0; l < PIRSettings.MOD_BIT_LENGTH; l++) {
gs[l] = g(l, z);
}
return gs;
}
public FieldElement[] getFieldElements() {
return fieldElements;
}
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);
}
return BigIntegerLagrangeInterpolation.doIt(fuckingXs, results);
}
}

View file

@ -3,11 +3,11 @@ package dk.au.pir.protocols.interpoly;
import dk.au.pir.BigIntegerField;
import dk.au.pir.settings.PIRSettings;
import dk.au.pir.utils.BigIntegerLagrangeInterpolation;
import dk.au.pir.utils.FieldElement;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class InterPolyClient {
@ -16,7 +16,7 @@ public class InterPolyClient {
private BigIntegerField field;
private BigInteger[] fieldElements = new BigInteger[PIRSettings.MOD_BIT_LENGTH];
private FieldElement[] fieldElements = new FieldElement[PIRSettings.MOD_BIT_LENGTH];
public InterPolyClient(int i) {
this.i = i;
@ -36,7 +36,7 @@ public class InterPolyClient {
char lChar = iString.charAt(iString.length() - 1 - l); // lol
String ilString = Character.toString(lChar);
BigInteger il = new BigInteger(ilString).mod(field.getGroupOrder());
return field.add(field.multiply(fieldElements[l], (BigInteger.valueOf(z))), il);
return field.add(field.multiply(fieldElements[l].getValue(), (BigInteger.valueOf(z))), il);
}
public BigInteger[] sendGs(int z){
@ -47,7 +47,7 @@ public class InterPolyClient {
return gs;
}
public BigInteger[] getFieldElements() {
public FieldElement[] getFieldElements() {
return fieldElements;
}

View file

@ -7,7 +7,7 @@ import dk.alexandra.fresco.framework.util.ModulusFinder;
import java.math.BigInteger;
public class PIRSettings {
public static final int MOD_BIT_LENGTH = 16; // s
public static final int MOD_BIT_LENGTH = 8; // s
public static final int DATABASE_SIZE = (int) Math.pow(2, MOD_BIT_LENGTH); // n
public static final int MAX_BIT_LENGTH = 512;

View file

@ -0,0 +1,64 @@
package dk.au.pir.utils;
import java.util.Arrays;
public class ComparableIntList implements Comparable<ComparableIntList> {
private int[] list;
public ComparableIntList(int size) {
list = new int[size];
}
public int[] getList() {
return list;
}
public int length() {
return list.length;
}
public String toString() {
StringBuilder tmp = new StringBuilder();
for (int i : list) {
tmp.append(i);
}
return tmp.toString();
}
@Override
public int hashCode() {
// TODO: Only works for k=1 atm
for (int i = 0; i < list.length; i++) {
if (list[i] == 1) {
return i;
}
}
return -1;
}
@Override
public boolean equals(Object other) {
ComparableIntList cit = (ComparableIntList) other;
for (int i = 0; i < list.length; i++) {
if (list[i] != cit.list[i]) {
return false;
}
}
return true;
}
public int compareTo(ComparableIntList o) {
for (int i = 0; i < list.length; i++) {
int diff = this.list[i] - o.list[i];
if (diff == 0) {
continue;
}
return diff;
}
return 0;
}
}

View file

@ -0,0 +1,22 @@
package dk.au.pir.utils;
import java.math.BigInteger;
public class FieldElement {
private BigInteger value;
private BigInteger modulus;
public FieldElement(BigInteger value, BigInteger modulus) {
this.value = value;
this.modulus = modulus;
}
public BigInteger getValue() {
return value;
}
public BigInteger getModulus() {
return modulus;
}
}

View file

@ -1,10 +1,63 @@
package dk.au.pir.utils;
import java.util.ArrayList;
import java.util.Random;
public class ProtocolUtils {
private Random rand;
public ProtocolUtils() {
this.rand = new Random();
}
public static int kronecker(int i, int j) {
if (i == j) {
return 1;
}
return 0;
}
public int binop(int n, int k) {
if (k < 0 || k > n)
return 0;
if (k == 0 || k == n)
return 1;
k = Math.min(k, n-k);
int c = 1;
for (int i = 0; i < k; i++)
c = c * (n-i) / (i+1);
return c;
}
public int findSizeOfS(int k, int n) {
int s = 0;
while ((binop(s, k-1))< n) {
s++;
}
return s;
}
public ComparableIntList insertOnes(ComparableIntList sequence, int k) {
ArrayList<Integer> picked = new ArrayList<Integer>();
int i = 0;
while (i < k-1) {
int randomI = rand.nextInt(sequence.length());
if (picked.contains(randomI)) {
continue;
}
picked.add(randomI);
sequence.getList()[randomI] = 1;
i++;
}
return sequence;
}
}