-
Notifications
You must be signed in to change notification settings - Fork 51
/
mpc.js
71 lines (58 loc) · 1.98 KB
/
mpc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(function (exports, node) {
var saved_instance;
/**
* Connect to the server and initialize the jiff instance
*/
exports.connect = function (hostname, computation_id, options) {
var opt = Object.assign({}, options);
// Added options goes here
if (node) {
// eslint-disable-next-line no-undef
jiff = require('../../lib/jiff-client');
// eslint-disable-next-line no-undef,no-global-assign
$ = require('jquery-deferred');
}
// eslint-disable-next-line no-undef
saved_instance = jiff.make_jiff(hostname, computation_id, opt);
// if you need any extensions, put them here
return saved_instance;
};
/**
* The MPC computation
*/
exports.compute = function (input, jiff_instance) {
if (jiff_instance == null) {
jiff_instance = saved_instance;
}
// Convert the input string into an array of numbers
// each number is the ascii encoding of the character at the same index
var arr = [];
for (var p= 0; p < input.length; p++) {
arr.push(input.charCodeAt(p));
}
var final_deferred = $.Deferred();
var final_promise = final_deferred.promise();
var promise = jiff_instance.share_array(arr);
promise.then(function (shares) {
var result = [];
for (var p = 1; p <= jiff_instance.party_count; p++) {
result = result.concat(shares[p]);
}
var promises = [];
for (var i = 0; i < result.length; i++) {
promises.push(jiff_instance.open(result[i]));
}
// Handle the results
Promise.all(promises).then(function (results) {
// convert each opened number to a character
// and add it to the final strings
var string = '';
for (var i = 0; i < results.length; i++) {
string += String.fromCharCode(results[i]);
}
final_deferred.resolve(string);
});
});
return final_promise;
};
}((typeof exports === 'undefined' ? this.mpc = {} : exports), typeof exports !== 'undefined'));