Skip to content

Commit

Permalink
did some slight refactoring all Alices use XOR and have the same ineq…
Browse files Browse the repository at this point in the history
…ual bit check. Will work on building comparing inequality. I might need to update the protocol so Bob does NOT know the answer of encrypted inequality too tbh. I fail to see why Bob would need to know, or at least I can show I already thought of this
  • Loading branch information
Andrew Quijano committed Mar 3, 2024
1 parent 8100edf commit f636734
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ codecov

# Outputs from gradle creation
gradle_user_home
bin/

#Any IntelliJ stuff
.idea
Expand Down
103 changes: 103 additions & 0 deletions src/main/java/security/socialistmillionaire/alice.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.math.BigInteger;
import java.net.Socket;
import java.util.ArrayList;
import javax.net.ssl.SSLSocket;

import org.apache.commons.io.serialization.ValidatingObjectInputStream;
import security.dgk.DGKOperations;
Expand Down Expand Up @@ -54,6 +55,42 @@ public void set_socket(Socket socket) throws IOException {
this.fromBob.accept("[L*");
}

public void set_socket(SSLSocket socket) throws IOException {
toBob = new ObjectOutputStream(socket.getOutputStream());
fromBob = new ValidatingObjectInputStream(socket.getInputStream());
this.fromBob.accept(
security.paillier.PaillierPublicKey.class,
security.dgk.DGKPublicKey.class,
security.elgamal.ElGamalPublicKey.class,
security.gm.GMPublicKey.class,
java.math.BigInteger.class,
java.lang.Number.class,
security.elgamal.ElGamal_Ciphertext.class,
java.util.HashMap.class,
java.lang.Long.class,
java.lang.String.class
);
this.fromBob.accept("[B");
this.fromBob.accept("[L*");
this.tls_socket_in_use = true;
}

/*
* Review "Protocol 1 EQT-1"
* from the paper "Secure Equality Testing Protocols in the Two-Party Setting"
*/
public void encrypted_equals(BigInteger a, BigInteger b) {
// Party A generates a sufficiently large (ℓ + 1 + κ bits) random
// value r , computes [x] ← [a − b + r ], and sends [x] to B.
int delta_a = rnd.nextInt(2);


}

public void private_equals(BigInteger x) {

}

/**
* Please see Protocol 1 with Bob which has parameter y
* Computes the truth value of X <= Y
Expand Down Expand Up @@ -555,4 +592,70 @@ public BigInteger[] getKValues(List<BigInteger> input, int k, boolean smallest_
writeBoolean(false);
return sorted_k;
}

// ---------------------- Everything here is essentially utility functions all Alices will need ----------------

// All comparison protocols seem to require encrypted xor, so just make it into a function
// Note, Encrypted Y is encrypted bits from Bob. x is an unencrytped number we must compare y with.
protected BigInteger [] encrypted_xor(BigInteger x, BigInteger [] Encrypted_Y) throws HomomorphicException {
BigInteger [] xor_bits = new BigInteger[Encrypted_Y.length];
for (int i = 0; i < Encrypted_Y.length; i++) {
if (NTL.bit(x, i) == 1) {
xor_bits[i] = DGKOperations.subtract(dgk_public.ONE, Encrypted_Y[i], dgk_public);
}
else {
xor_bits[i] = Encrypted_Y[i];
}
}
return xor_bits;
}

/*
* This is an issue that can occur with all private integer comparison protocols.
* What to do if the bit values are NOT the same?
* Technically, there is a risk of timing attack as this protocol terminates early if bits aren't
* the same. But this is beyond my paygrade. Feel free to PR if you have an idea.
*
* Currently by design of the program
* 1- Alice KNOWS that bob will assume deltaB = 0.
*
* Case 1:
* y has more bits than x IMPLIES that y is bigger
* x <= y is 1 (true)
* given deltaB is 0 by default...
* deltaA must be 1
* answer = 1 XOR 0 = 1
*
* Case 2:
* x has more bits than x IMPLIES that x is bigger
* x <= y is 0 (false)
* given deltaB is 0 by default...
* deltaA must be 0
* answer = 0 XOR 0 = 0
*/
protected BigInteger unequal_bit_check(BigInteger x, BigInteger [] Encrypted_Y) throws HomomorphicException, IOException {

// Case 1, delta B is ALWAYS INITIALIZED TO 0
// y has more bits -> y is bigger
if (x.bitLength() < Encrypted_Y.length) {
writeObject(BigInteger.ONE);
// x <= y -> 1 (true)
System.out.println("Shouldn't be here: x <= y bits");
return BigInteger.ONE;
}

// Case 2 delta B is 0
// x has more bits -> x is bigger
else if(x.bitLength() > Encrypted_Y.length) {
writeObject(BigInteger.ZERO);
// x <= y -> 0 (false)
System.out.println("Shouldn't be here: x > y bits");
return BigInteger.ZERO;
}

// Yay the bits are equal! Continue with the protocol!
else {
return TWO;
}
}
}
36 changes: 10 additions & 26 deletions src/main/java/security/socialistmillionaire/alice_joye.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class alice_joye extends alice_veugen {

public alice_joye() {

super();
}
/*
public boolean Protocol1(BigInteger x) throws IOException, ClassNotFoundException, HomomorphicException {
Expand Down Expand Up @@ -244,22 +244,15 @@ private boolean Protocol0(BigInteger x, int delta_a) throws IOException, ClassNo
toBob.writeInt(delta_a);
toBob.flush();

// Let bob know the correct delta b
if (x.bitLength() < Encrypted_Y.length) {
// x <= y is true, so I need delta_a XOR delta_b == 1
toBob.writeObject(BigInteger.ONE);
//I also need to tell what my delta_a is so Bob can correctly pick delta_b
toBob.flush();

BigInteger early_terminate = unequal_bit_check(x, Encrypted_Y);
if (early_terminate.equals(BigInteger.ONE)) {
return true;
}
else if(x.bitLength() > Encrypted_Y.length) {
// x <= y is false, so I need delta_a XOR delta_b == 0
toBob.writeObject(BigInteger.ZERO);
//I also need to tell what my delta_a is so Bob can correctly pick delta_b
toBob.flush();
//System.out.println("Shouldn't be here: x > y bits");
else if (early_terminate.equals(BigInteger.ZERO)) {
return false;
}

int floor_t_div_two = (int) Math.floor((float) Encrypted_Y.length/2);

// Step 3: Form Set L
Expand All @@ -286,15 +279,7 @@ else if(x.bitLength() > Encrypted_Y.length) {

// if equal bits, proceed!
// Step 2: compute Encrypted X XOR Y
XOR = new BigInteger[Encrypted_Y.length];
for (int i = 0; i < Encrypted_Y.length; i++) {
if (NTL.bit(x, i) == 1) {
XOR[i] = DGKOperations.subtract(dgk_public.ONE, Encrypted_Y[i], dgk_public);
}
else {
XOR[i] = Encrypted_Y[i];
}
}
XOR = encrypted_xor(x, Encrypted_Y);

int first_term;
BigInteger second_term;
Expand Down Expand Up @@ -330,9 +315,8 @@ else if(x.bitLength() > Encrypted_Y.length) {

// Step 4: send shuffled bits to Bob
C = shuffle_bits(C);
toBob.writeObject(C);
toBob.flush();

writeObject(C);

// Get Delta B from Bob
delta_b = fromBob.readInt();
int answer = delta_a ^ delta_b;
Expand All @@ -359,7 +343,7 @@ else if (hamming_weight < ceiling) {
return delta_a;
}

private static int hamming_weight(BigInteger value) throws HomomorphicException{
private static int hamming_weight(BigInteger value) throws HomomorphicException {
if (value.signum() < 0) {
throw new HomomorphicException("I'm unsure if Hamming weight is defined for negative");
}
Expand Down
53 changes: 5 additions & 48 deletions src/main/java/security/socialistmillionaire/alice_veugen.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import java.io.IOException;
import java.math.BigInteger;


public class alice_veugen extends alice {

public alice_veugen() {

super();
}

/**
Expand Down Expand Up @@ -45,59 +44,17 @@ boolean Protocol3(BigInteger x, int deltaA)
throw new IllegalArgumentException("Protocol 3 Step 1: Missing Y-bits!");
}

/*
* Currently by design of the program
* 1- Alice KNOWS that bob will assume deltaB = 0.
*
* Alice knows the protocol should be paillier_private if
* the bit length is NOT equal.
*
* Case 1:
* y has more bits than x IMPLIES that y is bigger
* x <= y is 1 (true)
* given deltaB is 0 by default...
* deltaA must be 1
* answer = 1 XOR 0 = 1
*
* Case 2:
* x has more bits than x IMPLIES that x is bigger
* x <= y is 0 (false)
* given deltaB is 0 by default...
* deltaA must be 0
* answer = 0 XOR 0 = 0
*/

// Case 1, delta B is ALWAYS INITIALIZED TO 0
// y has more bits -> y is bigger
if (x.bitLength() < Encrypted_Y.length) {
toBob.writeObject(BigInteger.ONE);
toBob.flush();
// x <= y -> 1 (true)
System.out.println("Shouldn't be here: x <= y bits");
BigInteger early_terminate = unequal_bit_check(x, Encrypted_Y);
if (early_terminate.equals(BigInteger.ONE)) {
return true;
}

// Case 2 delta B is 0
// x has more bits -> x is bigger
else if(x.bitLength() > Encrypted_Y.length) {
toBob.writeObject(BigInteger.ZERO);
toBob.flush();
// x <= y -> 0 (false)
System.out.println("Shouldn't be here: x > y bits");
else if (early_terminate.equals(BigInteger.ZERO)) {
return false;
}

// if equal bits, proceed!
// Step 2: compute Encrypted X XOR Y
XOR = new BigInteger[Encrypted_Y.length];
for (int i = 0; i < Encrypted_Y.length; i++) {
if (NTL.bit(x, i) == 1) {
XOR[i] = DGKOperations.subtract(dgk_public.ONE, Encrypted_Y[i], dgk_public);
}
else {
XOR[i] = Encrypted_Y[i];
}
}
XOR = encrypted_xor(x, Encrypted_Y);

// Step 3: delta A is computed on initialization, it is 0 or 1.

Expand Down
49 changes: 38 additions & 11 deletions src/main/java/security/socialistmillionaire/bob.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.math.BigInteger;
import java.net.Socket;
import java.security.KeyPair;
import javax.net.ssl.SSLSocket;

import org.apache.commons.io.serialization.ValidatingObjectInputStream;
import security.dgk.DGKOperations;
Expand All @@ -23,17 +24,9 @@ public class bob extends socialist_millionaires implements bob_interface
public bob(KeyPair first, KeyPair second, KeyPair third) {
parse_key_pairs(first, second, third);
}
/**
* Create a bob instance for running extending protocols such as comparing
* encrypted numbers
* @throws IllegalArgumentException
* If first is not a Paillier Keypair or second is not a DGK key pair or third is not ElGamal Keypair
*/
public bob (Socket socket,
KeyPair first, KeyPair second, KeyPair third)
throws IOException, IllegalArgumentException {
set_socket(socket);
parse_key_pairs(first, second, third);

public bob(KeyPair first, KeyPair second) {
parse_key_pairs(first, second, null);
}

private void parse_key_pairs(KeyPair first, KeyPair second, KeyPair third) {
Expand Down Expand Up @@ -93,6 +86,28 @@ public void set_socket(Socket socket) throws IOException {
throw new NullPointerException("Client Socket is null!");
}
}

public void set_socket(SSLSocket socket) throws IOException {
if(socket != null) {
this.toAlice = new ObjectOutputStream(socket.getOutputStream());
this.fromAlice = new ValidatingObjectInputStream(socket.getInputStream());
this.fromAlice.accept(
java.math.BigInteger.class,
java.lang.Number.class,
java.util.HashMap.class,
java.lang.Long.class,
security.elgamal.ElGamal_Ciphertext.class,
java.lang.String.class
);
this.fromAlice.accept("[B");
this.fromAlice.accept("[L*");
}
else {
throw new NullPointerException("Client Socket is null!");
}
this.tls_socket_in_use = true;
}

/**
* if Alice wants to sort a list of encrypted numbers, use this method if you
* will consistently sort using Protocol 2
Expand All @@ -108,6 +123,18 @@ public void sort()
System.out.println("Protocol 2 was used " + counter + " times!");
System.out.println("Protocol 2 completed in " + (System.nanoTime() - start_time)/BILLION + " seconds!");
}

/*
* Review "Protocol 1 EQT-1"
* from the paper "Secure Equality Testing Protocols in the Two-Party Setting"
*/
public void encrypted_equals() {

}

public void private_equals(BigInteger x) {

}

/**
* Please review "Improving the DGK comparison protocol" - Protocol 1
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/security/socialistmillionaire/bob_joye.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public bob_joye(KeyPair a, KeyPair b, KeyPair c) throws IllegalArgumentException
super(a, b, c);
}

public bob_joye(KeyPair a, KeyPair b) throws IllegalArgumentException {
super(a, b);
}

/*
public boolean Protocol1(BigInteger y) throws IOException, HomomorphicException, ClassNotFoundException {
// Step 1 by Bob
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/security/socialistmillionaire/bob_veugen.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public bob_veugen(KeyPair a, KeyPair b, KeyPair c) throws IllegalArgumentExcepti
super(a, b, c);
}

public bob_veugen(KeyPair a, KeyPair b) throws IllegalArgumentException {
super(a, b);
}

// Use this for Using Modified Protocol3 within Protocol 4
boolean Modified_Protocol3(BigInteger beta, BigInteger z)
throws IOException, ClassNotFoundException, IllegalArgumentException, HomomorphicException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public abstract class socialist_millionaires implements CipherConstants
protected ObjectOutputStream toAlice = null;
protected ValidatingObjectInputStream fromAlice = null;

// Confirm if using TLS sockets (encryption in transit for last few steps)
protected boolean tls_socket_in_use = false;

public void setDGKMode(boolean isDGK) {
this.isDGK = isDGK;
}
Expand Down

0 comments on commit f636734

Please sign in to comment.