All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.clarussecure.dataoperations.homomorphic.testing.HomomorphicCloud Maven / Gradle / Ivy

The newest version!
package eu.clarussecure.dataoperations.homomorphic.testing;

import eu.clarussecure.dataoperations.Criteria;
import eu.clarussecure.dataoperations.homomorphic.HomomorphicRemoteOperationCommand;
import eu.clarussecure.dataoperations.homomorphic.operators.Select;
import eu.clarussecure.encryption.paillier.EncryptedInteger;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class HomomorphicCloud {
    // Dommy implementation for a cloud
    // The implementation is a table, saving columns and rows.

    private final List data;
    private final String[] columns; // attributes

    public HomomorphicCloud(String[] columns) {
        this.columns = columns;
        this.data = new ArrayList<>();
    }

    public void addRow(String[] row) {
        // It is assumed that the given arrays contains the columns in order!
        this.data.add(row);
    }

    public void addRows(String[][] rows) {
        // Each array will be added to the data
        this.data.addAll(Arrays.asList(rows));
    }

    public String[][] getRows(String[] protectedAttribNames, Criteria[] criteria) {
        // Select the columns regarding the required attribute names
        List results = new ArrayList<>();

        // First, parse the selection criteria and prepare the Select instances
        Map> selectorsSet = new HashMap<>();

        // Initialize the selection criteria
        if (criteria == null) {
            // There is no criteria, use the Identity Function
            List selectors = selectorsSet.get(crit.getAttributeName());
                // Create the list of it does not exist
                if (selectors == null) {
                    selectors = new ArrayList<>();
                    selectorsSet.put(crit.getAttributeName(), selectors);
                }
                // Add the current selector to the list
                selectors.add(Select.getInstance(crit.getOperator(), crit.getValue()));
            }
        }

        // Second, process each column that will be selected
        for (String[] row : data) { // Select each row of the loaded data
            int p = 0; // column position in the results table
            String[] selectedRow = new String[protectedAttribNames.length]; // new result row
            boolean selected = true; // to decide if the row should be included in teh result or not

            for (int i = 0; i < columns.length; i++) { // for each stored column name
                // Get the selectors of this attribute
                List