Skip to content

Commit

Permalink
preprocess/api done (#296)
Browse files Browse the repository at this point in the history
* preprocess/api done

* preprocess/daemon done

* codacy error resolved

* codacy error resolved

* codacy error resolved

* preprocess/map done

* preprocess/handlers done

* preprocess/handlers done

* codacy resolved

* trying lint disable
  • Loading branch information
Snafkin547 authored Jun 6, 2024
1 parent c4133ea commit b6e3c1a
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 297 deletions.
93 changes: 49 additions & 44 deletions lib/client/preprocessing/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const LinkedList = require('../../../build/linkedlist.js');

module.exports = function (jiffClient) {
let isRunning = false;
const userCallbacks = [];
const preprocessingTasks = [new LinkedList()];
class PreprocessingAPI {
constructor(jiffClient) {
this.jiffClient = jiffClient;
this.isRunning = false;
this.userCallbacks = [];
this.preprocessingTasks = [new LinkedList()];
}

/**
* Checks if the given operation uses preprocessed values
Expand All @@ -13,15 +16,15 @@ module.exports = function (jiffClient) {
* @param {string} op - name of the operation to check
* @return {boolean} true if the op uses preprocessing, false otherwise
*/
jiffClient.has_preprocessing = function (op) {
for (let i = 0; i < jiffClient.extensions.length; i++) {
if (jiffClient.preprocessing_function_map[jiffClient.extensions[i]][op] != null) {
has_preprocessing(op) {
for (let i = 0; i < this.jiffClient.extensions.length; i++) {
if (this.jiffClient.preprocessing_function_map[String(this.jiffClient.extensions[parseInt(i, 10)])][String(op)] != null) {
return true;
}
}

return false;
};
}

/**
* Get a preprocessed share/value by associated op_id. If value does not exist
Expand All @@ -32,16 +35,16 @@ module.exports = function (jiffClient) {
* @param {string} op_id - the op_id associated with the preprocessed value/share
* @return {object} the preprocessed share(s)
*/
jiffClient.get_preprocessing = function (op_id) {
const values = jiffClient.preprocessing_table[op_id];
get_preprocessing(op_id) {
const values = this.jiffClient.preprocessing_table[String(op_id)];
if (values != null) {
return values;
}
if (jiffClient.crypto_provider === true) {
if (this.jiffClient.crypto_provider === true) {
return null;
}
throw new Error('No preprocessed value(s) that correspond to the op_id "' + op_id + '"');
};
}

/**
* Store a pair of op_id and associated pre-processed value/share
Expand All @@ -52,11 +55,11 @@ module.exports = function (jiffClient) {
* @param {string} op_id - the op_id associated with the preprocessed value/share
* @param {SecretShare} share - the share/value to store
*/
jiffClient.store_preprocessing = function (op_id, share) {
store_preprocessing(op_id, share) {
if (share != null) {
jiffClient.preprocessing_table[op_id] = share;
this.jiffClient.preprocessing_table[String(op_id)] = share;
}
};
}

/**
* Generate values used for JIFF operations in advance of the computation.
Expand All @@ -81,39 +84,39 @@ module.exports = function (jiffClient) {
* @return {promise} a promise that is resolved when preprocessing is completed, null if this is called by a party that is neither a compute nor receiver party
* @see {@link module:jiff-client~JIFFClient#executePreprocessing}
*/
jiffClient.preprocessing = function (dependent_op, count, protocols, threshold, receivers_list, compute_list, Zp, id_list, params) {
preprocessing(dependent_op, count, protocols, threshold, receivers_list, compute_list, Zp, id_list, params) {
// defaults!
if (receivers_list == null) {
receivers_list = [];
for (let p = 1; p <= jiffClient.party_count; p++) {
for (let p = 1; p <= this.jiffClient.party_count; p++) {
receivers_list.push(p);
}
} else {
jiffClient.helpers.sort_ids(receivers_list);
this.jiffClient.helpers.sort_ids(receivers_list);
}
if (compute_list == null) {
compute_list = [];
for (let c = 1; c <= jiffClient.party_count; c++) {
for (let c = 1; c <= this.jiffClient.party_count; c++) {
compute_list.push(c);
}
} else {
jiffClient.helpers.sort_ids(compute_list);
this.jiffClient.helpers.sort_ids(compute_list);
}

// not a receiver nor a sender
if (receivers_list.indexOf(jiffClient.id) === -1 && compute_list.indexOf(jiffClient.id) === -1) {
if (receivers_list.indexOf(this.jiffClient.id) === -1 && compute_list.indexOf(this.jiffClient.id) === -1) {
return null;
}

// more defaults
if (Zp == null) {
Zp = jiffClient.Zp;
Zp = this.jiffClient.Zp;
}
if (threshold == null) {
threshold = receivers_list.length;
}
if (protocols == null) {
protocols = jiffClient.default_preprocessing_protocols;
protocols = this.jiffClient.default_preprocessing_protocols;
}

// actual preprocessing
Expand All @@ -124,7 +127,7 @@ module.exports = function (jiffClient) {
params = {};
}
if (params['namespace'] == null) {
params['namespace'] = jiffClient.extensions[jiffClient.extensions.length - 1];
params['namespace'] = this.jiffClient.extensions[this.jiffClient.extensions.length - 1];
}

// Create preprocessing tasks
Expand All @@ -139,13 +142,13 @@ module.exports = function (jiffClient) {
id: null,
params: params,
protocols: protocols,
deferred: jiffClient.helpers.createDeferred()
deferred: new this.jiffClient.helpers.Deferred()
};

preprocessingTasks[preprocessingTasks.length - 1].add(task);
this.preprocessingTasks[this.preprocessingTasks.length - 1].add(task);

return task.deferred.promise;
};
}

/**
* Ask JIFF to start executing preprocessing for tasks previously added by {@link module:jiff-client~JIFFClient#preprocessing}.
Expand All @@ -158,34 +161,36 @@ module.exports = function (jiffClient) {
* @param callback {!Function} - the callback to execute when preprocessing is finished.
* {@link module:jiff-client~JIFFClient#preprocessing}
*/
jiffClient.executePreprocessing = function (callback) {
userCallbacks.push(callback);
preprocessingTasks.push(new LinkedList());
executePreprocessing(callback) {
this.userCallbacks.push(callback);
this.preprocessingTasks.push(new LinkedList());

if (!isRunning) {
__executePreprocessing();
if (!this.isRunning) {
this.__executePreprocessing();
}
};
}

// called only when preprocessing can run RIGHT NOW
const __executePreprocessing = function () {
isRunning = true;
__executePreprocessing() {
this.isRunning = true;

jiffClient.currentPreprocessingTasks = preprocessingTasks.shift();
const currentCallback = userCallbacks.shift();
this.jiffClient.currentPreprocessingTasks = this.preprocessingTasks.shift();
const currentCallback = this.userCallbacks.shift();

jiffClient.preprocessingCallback = function () {
this.jiffClient.preprocessingCallback = () => {
//Check
if (currentCallback != null) {
currentCallback.apply(null, currentCallback);
}

if (userCallbacks.length > 0) {
__executePreprocessing();
if (this.userCallbacks.length > 0) {
this.__executePreprocessing();
} else {
isRunning = false;
this.isRunning = false;
}
};

jiffClient.preprocessingDaemon();
};
};
this.jiffClient.preprocess.daemon.preprocessingDaemon();
}
}
module.exports = PreprocessingAPI;
Loading

0 comments on commit b6e3c1a

Please sign in to comment.