diff --git a/lib/client/api/sharing.js b/lib/client/api/sharing.js index 5c56aee27..0e7378324 100644 --- a/lib/client/api/sharing.js +++ b/lib/client/api/sharing.js @@ -1,8 +1,11 @@ -const shareProtocol = require('../protocols/shamir/share.js'); -const openProtocol = require('../protocols/shamir/open.js'); +const _shareProtocol = require('../protocols/shamir/share.js'); +const _openProtocol = require('../protocols/shamir/open.js'); const reshareProtocol = require('../protocols/shamir/reshare.js'); const arraysSharing = require('../protocols/arrays/api.js'); +const openProtocol = new _openProtocol(); +const shareProtocol = new _shareProtocol(); + module.exports = function (jiffClient) { /** * Share a secret input diff --git a/lib/client/arch/hooks.js b/lib/client/arch/hooks.js index 7b58cb0da..7189acb59 100644 --- a/lib/client/arch/hooks.js +++ b/lib/client/arch/hooks.js @@ -43,8 +43,8 @@ function Hooks(jiffClient) { * @param threshold {number} - threshold of sharing * @param Zp {number} - the field prime */ -Hooks.prototype.computeShares = shamir_share.jiff_compute_shares; -Hooks.prototype.reconstructShare = shamir_open.jiff_lagrange; +Hooks.prototype.computeShares = shamir_share.prototype.jiff_compute_shares; +Hooks.prototype.reconstructShare = shamir_open.prototype.jiff_lagrange; // Crypto hooks Hooks.prototype.encryptSign = function (jiffClient, message) { diff --git a/lib/client/protocols/shamir/open.js b/lib/client/protocols/shamir/open.js index e7de40c47..551bfcc80 100644 --- a/lib/client/protocols/shamir/open.js +++ b/lib/client/protocols/shamir/open.js @@ -25,7 +25,7 @@ const jiff_broadcast = function (jiff, share, parties, op_id) { } }; -module.exports = { +class ShamirOpen { /** * Open up the given share to the participating parties. * @function jiff_open @@ -37,7 +37,7 @@ module.exports = { * @returns {?promise} a (JQuery) promise to the open value of the secret, or null if the calling party is not a receiving party * */ - jiff_open: function (jiff, share, parties, op_id) { + jiff_open = (jiff, share, parties, op_id) => { if (!(share.jiff === jiff)) { throw 'share does not belong to given instance'; } @@ -111,7 +111,7 @@ module.exports = { } return null; - }, + }; /** * Uses Lagrange polynomials to interpolate the polynomial * described by the given shares (points) @@ -122,7 +122,7 @@ module.exports = { * @returns {number} the value of the polynomial at x=0 (the secret value) * */ - jiff_lagrange: function (jiff, shares) { + jiff_lagrange(jiff, shares) { const lagrange_coeff = []; // will contain shares.length many elements. // Compute the Langrange coefficients at 0. @@ -150,4 +150,6 @@ module.exports = { return recons_secret; } -}; +} + +module.exports = ShamirOpen; diff --git a/lib/client/protocols/shamir/share.js b/lib/client/protocols/shamir/share.js index 3f6e74556..694c2afd1 100644 --- a/lib/client/protocols/shamir/share.js +++ b/lib/client/protocols/shamir/share.js @@ -1,4 +1,4 @@ -module.exports = { +class ShamirShare { /** * Default way of computing shares (can be overridden using hooks). * Compute the shares of the secret (as many shares as parties) using Shamir secret sharing @@ -13,7 +13,7 @@ module.exports = { * point from the polynomial. * */ - jiff_compute_shares: function (jiff, secret, parties_list, threshold, Zp) { + jiff_compute_shares(jiff, secret, parties_list, threshold, Zp) { const shares = {}; // Keeps the shares // Each player's random polynomial f must have @@ -45,7 +45,7 @@ module.exports = { } return shares; - }, + } /** * Share given secret to the participating parties. * @ignore @@ -68,7 +68,7 @@ module.exports = { * if the party that calls this function is not a receiver then the map * will be empty. */ - jiff_share: function (jiff, secret, threshold, receivers_list, senders_list, Zp, share_id) { + jiff_share = (jiff, secret, threshold, receivers_list, senders_list, Zp, share_id) => { let p_id; // defaults @@ -147,8 +147,8 @@ module.exports = { } let _remaining = senders_list.length; - for (i = 0; i < senders_list.length; i++) { - p_id = senders_list[i]; + for (let i = 0; i < senders_list.length; i++) { + p_id = senders_list[parseInt(i, 10)]; if (p_id === jiff.id) { // Keep party's own share var my_share = jiff.hooks.execute_array_hooks('receiveShare', [jiff, p_id, shares[p_id]], 2); @@ -182,5 +182,7 @@ module.exports = { } return result; - } -}; + }; +} + +module.exports = ShamirShare; diff --git a/lib/server/hooks.js b/lib/server/hooks.js index 27c4602bf..06018fb88 100644 --- a/lib/server/hooks.js +++ b/lib/server/hooks.js @@ -98,7 +98,7 @@ ServerHooks.prototype.onInitializeUsedId = function (jiff, computation_id, party }; // Computing hooks -ServerHooks.prototype.computeShares = shamir_share.jiff_compute_shares; +ServerHooks.prototype.computeShares = shamir_share.prototype.jiff_compute_shares; // Crypto hooks ServerHooks.prototype.generateKeyPair = function (jiff) {