Skip to content

Commit

Permalink
protocols/arrays (#290)
Browse files Browse the repository at this point in the history
* protocols/arrays/open.js done

* protoco/open.js done

* protoco/share.js done?

* protoco/share.js solving codacy warning

* protoco/share.js solving codacy warning

* protoco/share.js solving codacy warning

* deferred modified
  • Loading branch information
Snafkin547 authored Jun 6, 2024
1 parent bffa9d5 commit f5a22d8
Show file tree
Hide file tree
Showing 3 changed files with 398 additions and 398 deletions.
23 changes: 13 additions & 10 deletions lib/client/protocols/arrays/api.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const share = require('./share.js');
const open = require('./open.js');
const _ArrayShare = require('./share.js');
const _ArrayOpen = require('./open.js');
const util = require('./util.js');

const ArrayOpen = new _ArrayOpen();
const ArrayShare = new _ArrayShare();

module.exports = {
jiff_share_array: share.share_array,
jiff_share_2D_array: share.share_2D_array,
jiff_share_ND_array: share.share_ND_array,
jiff_share_ND_array_static: share.share_ND_array_static,
jiff_share_ND_array_deferred: share.share_ND_array_deferred,
jiff_share_array: ArrayShare.share_array,
jiff_share_2D_array: ArrayShare.share_2D_array,
jiff_share_ND_array: ArrayShare.share_ND_array,
jiff_share_ND_array_static: ArrayShare.share_ND_array_static,
jiff_share_ND_array_deferred: ArrayShare.share_ND_array_deferred,

jiff_skeleton_of: util.skeleton_of,

jiff_open_array: open.open_array,
jiff_open_ND_array: open.open_ND_array,
jiff_receive_open_ND_array: open.receive_open_ND_array
jiff_open_array: ArrayOpen.open_array,
jiff_open_ND_array: ArrayOpen.open_ND_array,
jiff_receive_open_ND_array: ArrayOpen.receive_open_ND_array
};
180 changes: 89 additions & 91 deletions lib/client/protocols/arrays/open.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,113 @@
const open_array = function (jiff, shares, parties, op_ids) {
// A base operation id is provided to use for all opens.
if (typeof op_ids === 'string' || typeof op_ids === 'number') {
const tmp = { s1: op_ids };
for (let i = 1; i <= jiff.party_count; i++) {
tmp[i] = op_ids;
class ArrayOpen {
open_array = (jiff, shares, parties, op_ids) => {
// A base operation id is provided to use for all opens.
if (typeof op_ids === 'string' || typeof op_ids === 'number') {
const tmp = { s1: op_ids };
for (let i = 1; i <= jiff.party_count; i++) {
tmp[i] = op_ids;
}
op_ids = tmp;
}
op_ids = tmp;
}

return open_ND_array(jiff, shares, parties, null, op_ids);
};
return this.open_ND_array(jiff, shares, parties, null, op_ids);
};

const open_ND_array = function (jiff, shares, receivers_list, senders_list, op_ids) {
if (senders_list == null) {
senders_list = [];
for (let i = 1; i <= jiff.party_count; i++) {
senders_list.push(i);
}
if (receivers_list == null) {
receivers_list = Array.from(senders_list);
open_ND_array = (jiff, shares, receivers_list, senders_list, op_ids) => {
if (senders_list == null) {
senders_list = [];
for (let i = 1; i <= jiff.party_count; i++) {
senders_list.push(i);
}
if (receivers_list == null) {
receivers_list = Array.from(senders_list);
}
}
}
const is_sending = senders_list.indexOf(jiff.id) > -1;
const is_sending = senders_list.indexOf(jiff.id) > -1;

// Compute operation id
/*
*if (op_ids == null) {
* op_ids = jiff.counters.gen_op_id('open_ND_array', receivers_list.concat(senders_list));
*}
*/
// Compute operation id
/*
*if (op_ids == null) {
* op_ids = jiff.counters.gen_op_id('open_ND_array', receivers_list.concat(senders_list));
*}
*/

const final_deferred = jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
const resolve_open = function (shares) {
final_deferred.resolve(
(function __open_ND_array(shares, parties, op_ids) {
if (typeof shares.length === 'undefined') {
return jiff.internal_open(shares, parties);
} else if (shares.length === 0) {
return Promise.resolve([]);
} else {
const promised_array = [];
for (let i = 0; i < shares.length; i++) {
promised_array.push(__open_ND_array(shares[i], parties, op_ids + ':' + i));
const final_deferred = jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
function resolve_open(shares) {
final_deferred.resolve(
(function __open_ND_array(shares, parties, op_ids) {
if (typeof shares.length === 'undefined') {
return jiff.internal_open(shares, parties);
} else if (shares.length === 0) {
return Promise.resolve([]);
} else {
const promised_array = [];
for (let i = 0; i < shares.length; i++) {
promised_array.push(__open_ND_array(shares[i], parties, op_ids + ':' + i));
}
return Promise.all(promised_array);
}
return Promise.all(promised_array);
}
})(shares, receivers_list, op_ids)
);
};

if (is_sending) {
// Must emit the skeleton for any parties that are not holders but are receiving the open
const skeleton = (function __unwipe(nd_array, replace) {
if (!(typeof nd_array.length === 'undefined') || nd_array.length === 0) {
const unwiped_array = [];
for (let k = 0; k < nd_array.length; k++) {
unwiped_array.push(__unwipe(nd_array[k], replace));
}
return unwiped_array;
}
return replace;
})(shares, null);
jiff.emit(op_ids + 'skeleton', receivers_list, JSON.stringify(skeleton));
})(shares, receivers_list, op_ids)
);
}

resolve_open(shares);
} else {
// Populate skeleton with imitation shares
const revive_shares = function (skeleton) {
const share = new jiff.SecretShare({}, senders_list, senders_list.length, jiff.Zp);
return (function __unwipe(nd_array, replace) {
if (nd_array != null && !(typeof nd_array.length === 'undefined' && nd_array.length > 0)) {
if (is_sending) {
// Must emit the skeleton for any parties that are not holders but are receiving the open
const skeleton = (function __unwipe(nd_array, replace) {
if (!(typeof nd_array.length === 'undefined') || nd_array.length === 0) {
const unwiped_array = [];
for (let k = 0; k < nd_array.length; k++) {
unwiped_array.push(__unwipe(nd_array[k], replace));
}
return unwiped_array;
}
return replace;
})(skeleton, share);
};
})(shares, null);
jiff.emit(op_ids + 'skeleton', receivers_list, JSON.stringify(skeleton));

// If this party is not a sender, then the variable `shares` may be a skeleton
if (shares != null) {
// Use existing shares as skeleton to revive
shares = revive_shares(shares);
resolve_open(shares);
} else {
// Receive skeleton from senders
jiff.listen(op_ids + 'skeleton', function (sender, skeleton) {
jiff.remove_listener(op_ids + 'skeleton'); // This doesn't seem to work

if (typeof skeleton === 'string') {
skeleton = JSON.parse(skeleton);
}
// Populate skeleton with imitation shares
const revive_shares = function (skeleton) {
const share = new jiff.SecretShare({}, senders_list, senders_list.length, jiff.Zp);
return (function __unwipe(nd_array, replace) {
if (nd_array != null && !(typeof nd_array.length === 'undefined' && nd_array.length > 0)) {
const unwiped_array = [];
for (let k = 0; k < nd_array.length; k++) {
unwiped_array.push(__unwipe(nd_array[k], replace));
}
return unwiped_array;
}
return replace;
})(skeleton, share);
};

shares = revive_shares(skeleton);
// If this party is not a sender, then the variable `shares` may be a skeleton
if (shares != null) {
// Use existing shares as skeleton to revive
shares = revive_shares(shares);
resolve_open(shares);
});
} else {
// Receive skeleton from senders
jiff.listen(op_ids + 'skeleton', function (sender, skeleton) {
jiff.remove_listener(op_ids + 'skeleton'); // This doesn't seem to work

if (typeof skeleton === 'string') {
skeleton = JSON.parse(skeleton);
}

shares = revive_shares(skeleton);
resolve_open(shares);
});
}
}
}

return final_promise;
};
return final_promise;
};

const receive_open_ND_array = function (jiff, receivers_list, senders_list, threshold, Zp, op_ids) {
return open_ND_array(jiff, null, receivers_list, senders_list, op_ids);
};
receive_open_ND_array = (jiff, receivers_list, senders_list, threshold, Zp, op_ids) => {
return this.open_ND_array(jiff, null, receivers_list, senders_list, op_ids);
};
}

module.exports = {
open_array: open_array,
open_ND_array: open_ND_array,
receive_open_ND_array: receive_open_ND_array
};
module.exports = ArrayOpen;
Loading

0 comments on commit f5a22d8

Please sign in to comment.