From 12bd4864d09607ee61f612a545524bf2b99970c0 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Wed, 6 Nov 2019 11:12:07 +0100 Subject: [PATCH] Hvad fuck er l. --- pir/pom.xml | 12 +++ .../numeric/field/BigIntegerFieldElement.java | 86 +++++++++++++++++++ pir/src/main/java/dk/au/pir/Driver.java | 13 +++ .../protocols/interpoly/InterPolyClient.java | 31 ++++++- .../protocols/interpoly/InterPolyServer.java | 1 + .../java/dk/au/pir/settings/PIRSettings.java | 9 +- .../java/dk/au/pir/utils/ProtocolUtils.java | 10 +++ 7 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 pir/src/main/java/dk/alexandra/fresco/framework/builder/numeric/field/BigIntegerFieldElement.java create mode 100644 pir/src/main/java/dk/au/pir/Driver.java create mode 100644 pir/src/main/java/dk/au/pir/utils/ProtocolUtils.java diff --git a/pir/pom.xml b/pir/pom.xml index 7a73e86..4fdae28 100644 --- a/pir/pom.xml +++ b/pir/pom.xml @@ -7,6 +7,18 @@ dk.au.pir pir 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + diff --git a/pir/src/main/java/dk/alexandra/fresco/framework/builder/numeric/field/BigIntegerFieldElement.java b/pir/src/main/java/dk/alexandra/fresco/framework/builder/numeric/field/BigIntegerFieldElement.java new file mode 100644 index 0000000..93b774e --- /dev/null +++ b/pir/src/main/java/dk/alexandra/fresco/framework/builder/numeric/field/BigIntegerFieldElement.java @@ -0,0 +1,86 @@ +package dk.alexandra.fresco.framework.builder.numeric.field; + +import dk.alexandra.fresco.framework.util.MathUtils; +import java.math.BigInteger; + +/** + * An element in a field defined by a {@link BigIntegerModulus}. + */ +final public class BigIntegerFieldElement implements FieldElement { + + private static final long serialVersionUID = -6786266947587799652L; + + private final BigInteger value; + + private final BigIntegerModulus modulus; + private BigIntegerFieldElement(BigInteger value, BigIntegerModulus modulus) { + this.value = modulus.reduceModThis(value); + this.modulus = modulus; + } + + private FieldElement create(BigInteger value) { + return create(value, this.modulus); + } + + static FieldElement create(BigInteger value, BigIntegerModulus modulus) { + return new BigIntegerFieldElement(value, modulus); + } + + static FieldElement create(long value, BigIntegerModulus modulus) { + return create(BigInteger.valueOf(value), modulus); + } + + static FieldElement create(String string, BigIntegerModulus modulus) { + return create(new BigInteger(string), modulus); + } + + @Override + public FieldElement add(FieldElement operand) { + return create(value.add(extractValue(operand))); + } + + @Override + public FieldElement subtract(FieldElement operand) { + return create(value.subtract(extractValue(operand))); + } + + @Override + public FieldElement negate() { + return create(getModulus().subtract(value)); + } + + @Override + public FieldElement multiply(FieldElement operand) { + return create(value.multiply(extractValue(operand))); + } + + @Override + public FieldElement sqrt() { + return create(MathUtils.modularSqrt(value, getModulus())); + } + + @Override + public FieldElement modInverse() { + return create(value.modInverse(getModulus())); + } + + static BigInteger extractValue(FieldElement element) { + return ((BigIntegerFieldElement) element).value; + } + + public BigInteger getValue() { + return value; + } + + private BigInteger getModulus() { + return modulus.getBigInteger(); + } + + @Override + public String toString() { + return "BigIntegerFieldElement{" + + "value=" + value + + ", modulus=" + modulus + + '}'; + } +} diff --git a/pir/src/main/java/dk/au/pir/Driver.java b/pir/src/main/java/dk/au/pir/Driver.java new file mode 100644 index 0000000..2d8f3f1 --- /dev/null +++ b/pir/src/main/java/dk/au/pir/Driver.java @@ -0,0 +1,13 @@ +package dk.au.pir; + +import dk.alexandra.fresco.framework.builder.numeric.field.FieldElement; +import dk.au.pir.protocols.interpoly.InterPolyClient; + +public class Driver { + public static void main(String[] args) { + InterPolyClient client = new InterPolyClient(); + for (FieldElement elem: client.getSRandomFieldElements()) { + System.out.println(elem); + } + } +} 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 01b6459..e1febd3 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 @@ -1,10 +1,35 @@ package dk.au.pir.protocols.interpoly; +import dk.alexandra.fresco.framework.builder.numeric.field.BigIntegerFieldElement; +import dk.alexandra.fresco.framework.builder.numeric.field.FieldElement; import dk.au.pir.settings.PIRSettings; +import java.math.BigInteger; +import java.util.Random; + public class InterPolyClient { - public static void main(String[] args) { - System.out.println("Client!"); - System.out.println(PIRSettings.FIELD_DEFINITION.getBitLength()); + private final int i; + private Random random = new Random(); + private BigIntegerFieldElement[] fieldElements = new BigIntegerFieldElement[PIRSettings.MOD_BIT_LENGTH]; + + public InterPolyClient(int i) { + this.i = i; + } + + public void generateSRandomFieldElements() { + for (int i = 0; i < fieldElements.length; i++) { + this.fieldElements[i] = (BigIntegerFieldElement) PIRSettings.FIELD_DEFINITION.createElement(new BigInteger(PIRSettings.MAX_BIT_LENGTH, random)); + } + } + + public FieldElement g(int l, BigIntegerFieldElement z) { + String iString = Integer.toBinaryString(i); + char lChar = iString.charAt(iString.length() - 1 - l); // lol + BigIntegerFieldElement il = (BigIntegerFieldElement) PIRSettings.FIELD_DEFINITION.createElement(Character.toString(lChar)); + return fieldElements[l].multiply(z).add(il); + } + + public FieldElement[] getFieldElements() { + return fieldElements; } } 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 cfa3018..fa2efa2 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 @@ -1,4 +1,5 @@ package dk.au.pir.protocols.interpoly; public class InterPolyServer { + } diff --git a/pir/src/main/java/dk/au/pir/settings/PIRSettings.java b/pir/src/main/java/dk/au/pir/settings/PIRSettings.java index ebd7738..a0c3b79 100644 --- a/pir/src/main/java/dk/au/pir/settings/PIRSettings.java +++ b/pir/src/main/java/dk/au/pir/settings/PIRSettings.java @@ -4,7 +4,12 @@ import dk.alexandra.fresco.framework.builder.numeric.field.BigIntegerFieldDefini import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.ModulusFinder; +import java.math.BigInteger; + public class PIRSettings { - private static final int MOD_BIT_LENGTH = 16; - public static FieldDefinition FIELD_DEFINITION = new BigIntegerFieldDefinition(ModulusFinder.findSuitableModulus(MOD_BIT_LENGTH)); + public static final int MOD_BIT_LENGTH = 16; // 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 FieldDefinition FIELD_DEFINITION = new BigIntegerFieldDefinition(ModulusFinder.findSuitableModulus(MOD_BIT_LENGTH)); } diff --git a/pir/src/main/java/dk/au/pir/utils/ProtocolUtils.java b/pir/src/main/java/dk/au/pir/utils/ProtocolUtils.java new file mode 100644 index 0000000..abb5462 --- /dev/null +++ b/pir/src/main/java/dk/au/pir/utils/ProtocolUtils.java @@ -0,0 +1,10 @@ +package dk.au.pir.utils; + +public class ProtocolUtils { + public static int kronecker(int i, int j) { + if (i == j) { + return 1; + } + return 0; + } +}