Skip to content

Commit

Permalink
Going to create a Java class to store all shared code. Also can confi…
Browse files Browse the repository at this point in the history
…rm I did NOT break the original level-sites approach, seems I got an indexing mistake going on.
  • Loading branch information
AndrewQuijano committed Oct 16, 2023
1 parent 08a88cc commit 24411fc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
8 changes: 2 additions & 6 deletions src/main/java/weka/finito/client.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import security.socialistmillionaire.bob;
import weka.finito.structs.BigIntegers;

import static weka.finito.utils.Shared.hash;

public final class client implements Runnable {
private final String classes_file = "classes.txt";
private final String features_file;
Expand Down Expand Up @@ -179,12 +181,6 @@ public void generate_keys() {
paillier_private_key = (PaillierPrivateKey) paillier.getPrivate();
}

public static String hash(String text) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(text.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hash);
}

private boolean need_keys() {
try {
dgk_public_key = DGKPublicKey.readKey("dgk.pub");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/weka/finito/level_site_thread.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public final void run() {
}

// Level Data is the Node Data...
// it is set to 0 by default...
if (previous_index != null) {
this.level_site_data.set_current_index(Integer.parseInt(previous_index));
}
Expand Down
43 changes: 25 additions & 18 deletions src/main/java/weka/finito/server.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import java.math.BigInteger;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Map.Entry;

Expand All @@ -34,6 +31,8 @@
import weka.finito.structs.level_order_site;
import weka.finito.structs.NodeInfo;

import static weka.finito.utils.Shared.hash;


public final class server implements Runnable {

Expand Down Expand Up @@ -201,34 +200,32 @@ private void evaluate(int server_port) throws IOException, HomomorphicException
Niu.setDGKPublicKey(dgk_public);

List<NodeInfo> node_level_data;
NodeInfo ls;
NodeInfo ls = null;
long start_time = System.nanoTime();
int previous_index = 0;

// Traverse DT until you hit a leaf, the client has to track the index...
for (level_order_site level_site_data : all_level_sites) {
node_level_data = level_site_data.get_node_data();

level_site_data.set_current_index(previous_index);

// Handle at a level...
int node_level_index = 0;
int n = 0;
int next_index = 0;
boolean equalsFound = false;
boolean inequalityHolds = false;
boolean terminalLeafFound = false;

while (!equalsFound) {
ls = node_level_data.get(node_level_index);
System.out.println("j=" + node_level_index);
if (ls.isLeaf()) {
if (n == 2 * level_site_data.get_current_index()
|| n == 2 * level_site_data.get_current_index() + 1) {
// Tell the client the value
to_client_site.writeInt(-1);
to_client_site.writeObject(ls.getVariableName());
to_client_site.flush();
long stop_time = System.nanoTime();
double run_time = (double) (stop_time - start_time);
run_time = run_time / 1000000;
System.out.printf("Total Server-Site run-time took %f ms\n", run_time);
terminalLeafFound = true;
System.out.println("Terminal leaf:" + ls.getVariableName());
break;
}
n += 2;
Expand Down Expand Up @@ -263,19 +260,29 @@ private void evaluate(int server_port) throws IOException, HomomorphicException
}
node_level_index++;
}

if (terminalLeafFound) {
// Tell the client the value
to_client_site.writeInt(-1);
to_client_site.writeObject(ls.getVariableName());
to_client_site.flush();
long stop_time = System.nanoTime();
double run_time = (double) (stop_time - start_time);
run_time = run_time / 1000000;
System.out.printf("Total Server-Site run-time took %f ms\n", run_time);
break;
}
else {
// Remember this for next loop...
previous_index = next_index;
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
serverSocket.close();
}

private static String hash(String text) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(text.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hash);
}

private void client_communication() throws Exception {
ServerSocket serverSocket = new ServerSocket(server_port);
System.out.println("Server ready to get public keys from client");
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/weka/finito/utils/Shared.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package weka.finito.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Shared {
public static String hash(String text) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(text.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hash);
}
}

0 comments on commit 24411fc

Please sign in to comment.