Skip to content

Commit

Permalink
I think I should let Strings be sent over between Alice and Bob. More…
Browse files Browse the repository at this point in the history
… importantly, expose read and write object, especially since I have input validation already given. I can probably use this for easier use in my PPDT stuff
  • Loading branch information
AndrewQuijano committed Oct 23, 2023
1 parent af803ad commit 71b1f69
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 71 deletions.
45 changes: 21 additions & 24 deletions src/main/java/security/socialistmillionaire/alice.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void set_socket(Socket socket) throws IOException {
java.lang.Number.class,
security.elgamal.ElGamal_Ciphertext.class,
java.util.HashMap.class,
java.lang.Long.class
java.lang.Long.class,
java.lang.String.class
);
this.fromBob.accept("[B");
this.fromBob.accept("[L*");
Expand Down Expand Up @@ -78,7 +79,7 @@ public boolean Protocol1(BigInteger x)
BigInteger [] XOR;

// Step 1: Get Y bits from Bob
in = fromBob.readObject();
in = readObject();
if (in instanceof BigInteger[]) {
Encrypted_Y = (BigInteger []) in;
}
Expand Down Expand Up @@ -153,7 +154,7 @@ protected boolean decrypt_protocol_one(int delta_a) throws IOException, ClassNot
// delta_B, party A computes the encryption of delta as
// 1- delta = delta_b if delta_a = 0
// 2- delta = 1 - delta_b otherwise if delta_a = 1.
o = fromBob.readObject();
o = readObject();
if (o instanceof BigInteger) {
if (delta_a == 0) {
delta = (BigInteger) o;
Expand All @@ -171,14 +172,14 @@ protected boolean decrypt_protocol_one(int delta_a) throws IOException, ClassNot
* Send him the encrypted answer!
* Alice and Bob know now without revealing x or y!
*
* You can blind it for safety, but I will assume Bob is nice
* You can blind it for safety, but I will assume Bob is nice,
* Plus the info doesn't really reveal anything to Bob.
*/
// blind = NTL.RandomBnd(dgk_public.getU());
// Blind = NTL.RandomBnd(dgk_public.getU());
toBob.writeObject(DGKOperations.add_plaintext(delta, blind, dgk_public));
toBob.flush();

o = fromBob.readObject();
o = readObject();
if (o instanceof BigInteger) {
delta = (BigInteger) o;
delta = delta.subtract(blind);
Expand All @@ -194,7 +195,7 @@ protected boolean decrypt_protocol_two(BigInteger result) throws IOException {
toBob.writeObject(result);
toBob.flush();
comparison = fromBob.readInt();// x <= y
// IF SOMETHING HAPPENS...GET POST MORTEM HERE
// IF SOMETHING HAPPENS...GET THE POST MORTEM HERE
if (comparison != 0 && comparison != 1) {
throw new IllegalArgumentException("Invalid Comparison output! --> " + comparison);
}
Expand Down Expand Up @@ -223,7 +224,7 @@ public boolean Protocol2(BigInteger x, BigInteger y)

// Step 1: 0 <= r < N
// Pick Number of l + 1 + sigma bits
// Considering DGK is an option, just stick with size of Zu
// Considering DGK is an option, stick with the size of Zu
if (isDGK) {
throw new IllegalArgumentException("Protocol 2 is NOT allowed with DGK! Used Protocol 4!");
}
Expand Down Expand Up @@ -275,7 +276,7 @@ public boolean Protocol2(BigInteger x, BigInteger y)
}

// Step 5B: Bob sends z/2^l
bob = fromBob.readObject();
bob = readObject();
if (bob instanceof BigInteger) {
zdiv2L = (BigInteger) bob;
}
Expand Down Expand Up @@ -347,7 +348,7 @@ public BigInteger division(BigInteger x, long d)
}

// Step 4: Bob computes c and Alice receives it
in = fromBob.readObject();
in = readObject();
if (in instanceof BigInteger) {
c = (BigInteger) in;
}
Expand Down Expand Up @@ -408,21 +409,21 @@ public BigInteger multiplication(BigInteger x, BigInteger y)
// Step 2

// Step 3
in = fromBob.readObject();
in = readObject();
if (in instanceof BigInteger) {
// (x + a)(y + b) = xy + xb + ya + ab
// xy = (x + a)(y + b) - xb - ya - ab
result = (BigInteger) in;
if(isDGK) {
result = DGKOperations.subtract(result, DGKOperations.multiply(x, b, dgk_public), dgk_public);
result = DGKOperations.subtract(result, DGKOperations.multiply(y, a, dgk_public), dgk_public);
// To avoid throwing an exception to myself of encrypt range [0, U), mod it now!
// To avoid throwing an exception to myself of encrypted range [0, U), mod it now!
result = DGKOperations.subtract_plaintext(result, a.multiply(b).mod(dgk_public.getU()), dgk_public);
}
else {
result = PaillierCipher.subtract(result, PaillierCipher.multiply(x, b, paillier_public), paillier_public);
result = PaillierCipher.subtract(result, PaillierCipher.multiply(y, a, paillier_public), paillier_public);
// To avoid throwing an exception to myself of encrypt range [0, N), mod it now!
// To avoid throwing an exception to myself of encrypted range [0, N), mod it now!
result = PaillierCipher.subtract_plaintext(result, a.multiply(b).mod(paillier_public.getN()), paillier_public);
}
}
Expand All @@ -435,7 +436,7 @@ public BigInteger multiplication(BigInteger x, BigInteger y)
public void receivePublicKeys()
throws IOException, ClassNotFoundException {
Object x;
x = fromBob.readObject();
x = readObject();
if (x instanceof DGKPublicKey) {
System.out.println("Alice Received DGK Public key from Bob");
this.setDGKPublicKey((DGKPublicKey) x);
Expand All @@ -444,7 +445,7 @@ public void receivePublicKeys()
dgk_public = null;
}

x = fromBob.readObject();
x = readObject();
if(x instanceof PaillierPublicKey) {
System.out.println("Alice Received Paillier Public key from Bob");
this.setPaillierPublicKey((PaillierPublicKey) x);
Expand All @@ -453,7 +454,7 @@ public void receivePublicKeys()
paillier_public = null;
}

x = fromBob.readObject();
x = readObject();
if(x instanceof ElGamalPublicKey) {
System.out.println("Alice Received ElGamal Public key from Bob");
this.setElGamalPublicKey((ElGamalPublicKey) x);
Expand All @@ -477,8 +478,7 @@ public BigInteger[] getKValues(BigInteger [] input, int k, boolean smallest_firs
boolean activation;
for (int i = 0; i < k; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
toBob.writeBoolean(true);
toBob.flush();
writeBoolean(true);
// Might need a K-Max test as well!
activation = this.Protocol2(arr[j], arr[j + 1]);
if (smallest_first) {
Expand Down Expand Up @@ -506,8 +506,7 @@ public BigInteger[] getKValues(BigInteger [] input, int k, boolean smallest_firs
}

// Close Bob
toBob.writeBoolean(false);
toBob.flush();
writeBoolean(false);
return sorted_k;
}

Expand All @@ -525,8 +524,7 @@ public BigInteger[] getKValues(List<BigInteger> input, int k, boolean smallest_
boolean activation;
for (int i = 0; i < k; i++) {
for (int j = 0; j < arr.size() - i - 1; j++) {
toBob.writeBoolean(true);
toBob.flush();
writeBoolean(true);
activation = this.Protocol2(arr.get(j), arr.get(j + 1));

if(smallest_first) {
Expand Down Expand Up @@ -554,8 +552,7 @@ public BigInteger[] getKValues(List<BigInteger> input, int k, boolean smallest_
}

// Close Bob
toBob.writeBoolean(false);
toBob.flush();
writeBoolean(false);
return sorted_k;
}
}
12 changes: 6 additions & 6 deletions src/main/java/security/socialistmillionaire/alice_elgamal.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ElGamal_Ciphertext addition(ElGamal_Ciphertext x, ElGamal_Ciphertext y)
// Step 2

// Step 3
in = fromBob.readObject();
in = readObject();
if (in instanceof ElGamal_Ciphertext) {
result = (ElGamal_Ciphertext) in;
result = ElGamalCipher.divide(result, a ,el_gamal_public);
Expand Down Expand Up @@ -84,7 +84,7 @@ public ElGamal_Ciphertext multiplication(ElGamal_Ciphertext x, ElGamal_Ciphertex
// Step 2

// Step 3
in = fromBob.readObject();
in = readObject();
if (in instanceof ElGamal_Ciphertext) {
result = (ElGamal_Ciphertext) in;
result = ElGamalCipher.subtract(result, ElGamalCipher.multiply_scalar(x, b, el_gamal_public), el_gamal_public);
Expand Down Expand Up @@ -128,7 +128,7 @@ public ElGamal_Ciphertext division(ElGamal_Ciphertext x, long d)
}

// Step 4: Bob computes c and Alice receives it
in = fromBob.readObject();
in = readObject();
if (in instanceof ElGamal_Ciphertext) {
c = (ElGamal_Ciphertext) in;
}
Expand Down Expand Up @@ -213,7 +213,7 @@ public boolean Protocol4(ElGamal_Ciphertext x, ElGamal_Ciphertext y)
deltaB = 1;
}

bob = fromBob.readObject();
bob = readObject();
if (bob instanceof ElGamal_Ciphertext) {
zeta_one = (ElGamal_Ciphertext) bob;
}
Expand All @@ -222,7 +222,7 @@ public boolean Protocol4(ElGamal_Ciphertext x, ElGamal_Ciphertext y)
throw new IllegalArgumentException("Protocol 4, Step 5: BigInteger z_1 not found!");
}

bob = fromBob.readObject();
bob = readObject();
if (bob instanceof ElGamal_Ciphertext) {
zeta_two = (ElGamal_Ciphertext) bob;
}
Expand Down Expand Up @@ -263,7 +263,7 @@ protected boolean decrypt_protocol_two(ElGamal_Ciphertext result) throws IOExcep
toBob.writeObject(result);
toBob.flush();
comparison = fromBob.readInt();
// IF SOMETHING HAPPENS...GET POST MORTEM HERE
// IF SOMETHING HAPPENS...GET THE POST MORTEM HERE
if (comparison != 0 && comparison != 1) {
throw new IllegalArgumentException("Invalid Comparison result --> " + comparison);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/security/socialistmillionaire/alice_joye.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public alice_joye() {
/*
public boolean Protocol1(BigInteger x) throws IOException, ClassNotFoundException, HomomorphicException {
// Step 1 by Bob
Object o = fromBob.readObject();
Object o = readObject();
BigInteger little_y_star;
int answer;
int delta_a;
Expand Down Expand Up @@ -109,7 +109,7 @@ private boolean Protocol0(BigInteger x, int delta_a) throws IOException, ClassNo
List<Integer> set_l = new ArrayList<>();

// Step 1: Get Y bits from Bob
Object in = fromBob.readObject();
Object in = readObject();
if (in instanceof BigInteger[]) {
Encrypted_Y = (BigInteger []) in;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/security/socialistmillionaire/alice_veugen.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ boolean Protocol3(BigInteger x, int deltaA)
BigInteger [] Encrypted_Y;

//Step 1: Receive y_i bits from Bob
in = fromBob.readObject();
in = readObject();
if (in instanceof BigInteger[]) {
Encrypted_Y = (BigInteger []) in;
}
Expand Down Expand Up @@ -171,7 +171,7 @@ boolean Modified_Protocol3(BigInteger alpha, BigInteger r, int deltaA)
}

// Step A: get d from Bob
in = fromBob.readObject();
in = readObject();
if (in instanceof BigInteger) {
d = (BigInteger) in;
}
Expand All @@ -181,7 +181,7 @@ boolean Modified_Protocol3(BigInteger alpha, BigInteger r, int deltaA)
}

// Step B: get beta_bits from Bob
in = fromBob.readObject();
in = readObject();
if (in instanceof BigInteger[]) {
beta_bits = (BigInteger []) in;
}
Expand Down Expand Up @@ -385,15 +385,15 @@ public boolean Protocol2(BigInteger x, BigInteger y)
deltaB = 1;
}

bob = fromBob.readObject();
bob = readObject();
if (bob instanceof BigInteger) {
zeta_one = (BigInteger) bob;
}
else {
throw new IllegalArgumentException("Protocol 4, Step 5: BigInteger z_1 not found, Invalid object: " + bob.getClass().getName());
}

bob = fromBob.readObject();
bob = readObject();
if (bob instanceof BigInteger) {
zeta_two = (BigInteger) bob;
}
Expand Down
Loading

0 comments on commit 71b1f69

Please sign in to comment.