Skip to content

Commit

Permalink
Merge branch '197_GMW' of github.com:multiparty/jiff into 197_GMW
Browse files Browse the repository at this point in the history
  • Loading branch information
p-flock committed Aug 10, 2020
2 parents 2cd7ce0 + 9b2ef1e commit ed23049
Show file tree
Hide file tree
Showing 189 changed files with 148,123 additions and 13,396 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ When contributing to this repository, please first discuss the change you wish t
- Branch formatting:
- issueNumber-initials-description
- ex. 22-LQ-bugFixes
- Demos should be implemented in both the browser and node.js, and accompanied by test cases. Sophisticted demos should include a README as well as a package.json file for managing additional dependencies. *Please Follow the demo template under demos/template closely*.
- Demos should be implemented in both the browser and node.js, and accompanied by test cases. Sophisticated demos should include a README as well as a package.json file for managing additional dependencies. *Please Follow the demo template under demos/template closely*.

## Issue Template
* **I'm submitting a ...**
Expand Down Expand Up @@ -54,7 +54,7 @@ When contributing to this repository, please first discuss the change you wish t
* **Other information** (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow, etc)

## Pull Requests (PRs) ##
* **Please check if the PR fulfills these requirements**
* **Please check if the PR fulfils these requirements**
- [ ] The commit message follows our guidelines
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)
Expand Down
2 changes: 1 addition & 1 deletion demos/array-bubble-sort/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function connect() {
$('#output').append("<p class='error'>Party count must be a valid number!</p>");
$('#connectButton').prop('disabled', false);
} else {
var options = { party_count: party_count};
var options = { party_count: party_count };
options.onError = function (_, error) {
$('#output').append("<p class='error'>"+error+'</p>');
};
Expand Down
43 changes: 11 additions & 32 deletions demos/array-bubble-sort/mpc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(function (exports, node) {
var saved_instance;
var seeds = {};

/**
* Connect to the server and initialize the jiff instance
Expand Down Expand Up @@ -48,41 +47,21 @@
jiff_instance = saved_instance;
}

// Unique prefix seed for op_ids
if (seeds[jiff_instance.id] == null) {
seeds[jiff_instance.id] = 0;
}
var seed = seeds[jiff_instance.id]++;

var final_deferred = $.Deferred();
var final_promise = final_deferred.promise();

// Share the arrays
jiff_instance.share_array(input, input.length).then(function (shares) {
jiff_instance.seed_ids(seed);
var shares = jiff_instance.share_array(input, input.length);

// sum all shared input arrays element wise
var array = shares[1];
for (var p = 2; p <= jiff_instance.party_count; p++) {
for (var i = 0; i < array.length; i++) {
array[i] = array[i].sadd(shares[p][i]);
}
}

// sort new array
var sorted = bubblesort(array);

// Open the array
var allPromises = [];
for (var k = 0; k < sorted.length; k++) {
allPromises.push(jiff_instance.open(sorted[k]));
// Sum all shared input arrays element wise
var array = shares[1];
for (var p = 2; p <= jiff_instance.party_count; p++) {
for (var i = 0; i < array.length; i++) {
array[i] = array[i].sadd(shares[p][i]);
}
}

Promise.all(allPromises).then(function (results) {
final_deferred.resolve(results);
});
});
// Sort new array
var sorted = bubblesort(array);

return final_promise;
// Open the array
return jiff_instance.open_array(sorted);
};
}((typeof exports === 'undefined' ? this.mpc = {} : exports), typeof exports !== 'undefined'));
43 changes: 11 additions & 32 deletions demos/array-merge-sort/mpc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(function (exports, node) {
var saved_instance;
var seeds = {};

/**
* Connect to the server and initialize the jiff instance
Expand Down Expand Up @@ -70,40 +69,20 @@
jiff_instance = saved_instance;
}

// Unique prefix seed for op_ids
if (seeds[jiff_instance.id] == null) {
seeds[jiff_instance.id] = 0;
}
var seed = seeds[jiff_instance.id]++;

var final_deferred = $.Deferred();
var final_promise = final_deferred.promise();

// Share the arrays
jiff_instance.share_array(input, input.length).then(function (shares) {
jiff_instance.seed_ids(seed);
var shares = jiff_instance.share_array(input, input.length);

// sum all shared input arrays element wise
var array = shares[1];
for (var p = 2; p <= jiff_instance.party_count; p++) {
for (var i = 0; i < array.length; i++) {
array[i] = array[i].sadd(shares[p][i]);
}
// Sum all shared input arrays element wise
var array = shares[1];
for (var p = 2; p <= jiff_instance.party_count; p++) {
for (var i = 0; i < array.length; i++) {
array[i] = array[i].sadd(shares[p][i]);
}
// sort new array
oddEvenSort(array, 0, array.length);

// Open the array
var allPromises = [];
for (var k = 0; k < array.length; k++) {
allPromises.push(jiff_instance.open(array[k]));
}

Promise.all(allPromises).then(function (results) {
final_deferred.resolve(results);
});
});
}
// Sort new array
oddEvenSort(array, 0, array.length);

return final_promise;
// Open the array
return jiff_instance.open_array(array);
};
}((typeof exports === 'undefined' ? this.mpc = {} : exports), typeof exports !== 'undefined'));
53 changes: 53 additions & 0 deletions demos/array-mpc-as-a-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#MPC As a Service

In many circumstances, some parties might want to participate in a secure MPC protocol but not have the computational
resources to conduct the full protocol themselves. In this case, they might choose to outsource some of the computation
to third parties who offer MPC as a service. This demo is a simple example of how JIFF might be used in such a scenario.

Here, we have two types of parties: *input* parties who provide input to the computation, and *compute* parties, who do
not provide input but participate in the computation process. In this simplistic example, they compute the sum of the
inputs.

## Running Demo
Unlike many of the other demos provided here, parameters such as party count are not specified as in-line arguments.
Instead, this information as well as the information for which of the parties are input parties and which are compute
parties is set in config.json.

Another different from the other demos is that here only the compute parties may be run through the terminal, while input
parties must be run through a web browser.

As currently configured in config.json, there are 3 compute parties and 2 input parties.

Additionally, config.json specifies whether to use preprocessing or crypto provider. For more on this, please
check the preprocessing tutorial.

1. Running the server:
```shell
node demos/array-mpc-as-a-service/server.js
```

2. Running compute parties:
```shell
# run a single compute party
node demos/array-mpc-as-a-service/compute-party.js
```

3. For the input parties, go to *http://localhost:8080/demos/array-mpc-as-a-service/client.html* in a web browser.

## File structure
The demo consists of the following parts:
1. Server script: *server.js*
2. Web Based input parties:
* *client.html*: UI for the browser.
* *client.js*: Handlers for UI buttons and input validations.
* *client-mpc.js*: MPC protocol for parties that provide input of computation.
3. Compute parties:
* *compute-party.js*: MPC protocol for parties that help compute but do not receive output.
4. Configuration file:
* *config.json*
5. Documentation:
* This *README.md* file.
6. Tests:
* *tests/config-*.json*: configuration files for various tests.
* *tests/test.js*: mocha testing suite.
* *test.sh*: entry point for tests.
57 changes: 57 additions & 0 deletions demos/array-mpc-as-a-service/client-mpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
(function (exports, node) {
if (node) {
// eslint-disable-next-line no-undef
JIFFClient = require('../../lib/jiff-client.js');
// eslint-disable-next-line no-undef
jiff_restAPI = require('../../lib/ext/jiff-client-restful.js');
}

var __jiff_instance, config, all_parties;
exports.connect = function (hostname, computation_id, options, _config) {
config = _config;

all_parties = config.compute_parties.concat(config.input_parties);

var opt = Object.assign({}, options);
opt['crypto_provider'] = config.preprocessing === false;
opt['initialization'] = { role: 'input' };
opt['party_count'] = config.party_count;
opt['autoConnect'] = false;

// eslint-disable-next-line no-undef
__jiff_instance = new JIFFClient(hostname, computation_id, opt);
// eslint-disable-next-line no-undef
__jiff_instance.apply_extension(jiff_restAPI);
__jiff_instance.connect();
return __jiff_instance;
};
exports.compute = function (input, jiff_instance_) {
var jiff_instance = __jiff_instance;
if (jiff_instance_) {
jiff_instance = jiff_instance_;
}

// Set up array sharing
var skeleton = jiff_instance.skeleton_of(input);
var skeletons = {};
var i, p_id;
for (i = 0; i < config.input_parties.length; i++) {
p_id = config.input_parties[i];
skeletons[p_id] = skeleton; // Assume same skeleton for all parties
}

// Share with compute parties
jiff_instance.share_ND_array(input, skeletons, null, config.compute_parties, config.input_parties);

// If this party is still connected after the compute parties are done, it will
// receive the result.

var promise = jiff_instance.receive_open_array(all_parties, config.compute_parties);

promise.then(function (value) {
jiff_instance.disconnect(true, true);
});

return promise;
};
}((typeof exports === 'undefined' ? this.mpc = {} : exports), typeof exports !== 'undefined'));
34 changes: 34 additions & 0 deletions demos/array-mpc-as-a-service/client.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!-- Basic UI for running the demo in the browser -->

<html>
<head>
<title>Sum integers under MPC</title>
<style>
.error {
color: #FF0000;
}
</style>
<script src="/config.js"></script>

<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script src="/dist/jiff-client.js"></script>
<script src="/lib/ext/jiff-client-restful.js"></script>

<!-- Contains UI Handlers and Input Checks -->
<script type="text/javascript" src="./client.js"></script>

<!-- Contains Client MPC code -->
<script type="text/javascript" src="./client-mpc.js"></script>
</head>
<body>
<h1>Connect JIFF</h1>
<label for="computation_id">Computation ID</label><input id="computation_id" value="test"/><br/><br/>
<button id="connectButton" onclick="connect();">Connect</button>
<br/><br/>
<hr/>
<h1>Sum and Dot Numbers under MPC as a service</h1>
<label for="number">Enter your array: </label> <br/><br/>
<textarea id="inputText" rows="5" cols="35">[1, 3, 2]</textarea> &nbsp; <button onclick="submit();" disabled="disabled" id="processButton">Start</button><br/>
<div id="output"></div>
</body>
</html>
74 changes: 74 additions & 0 deletions demos/array-mpc-as-a-service/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Do not modify this file unless you have to.
* This file has UI handlers.
*/

/* global config */

// eslint-disable-next-line no-unused-vars
function connect() {
$('#connectButton').prop('disabled', true);
var computation_id = $('#computation_id').val();

var options = { party_count: config.party_count };
options.onError = function (_, error) {
$('#output').append("<p class='error'>"+error+'</p>');
};

var hostname = window.location.hostname.trim();
var port = window.location.port;
if (port == null || port === '') {
port = '80';
}
if (!(hostname.startsWith('http://') || hostname.startsWith('https://'))) {
hostname = 'http://' + hostname;
}
if (hostname.endsWith('/')) {
hostname = hostname.substring(0, hostname.length-1);
}
if (hostname.indexOf(':') > -1 && hostname.lastIndexOf(':') > hostname.indexOf(':')) {
hostname = hostname.substring(0, hostname.lastIndexOf(':'));
}

hostname = hostname + ':' + port;
// eslint-disable-next-line no-undef
var jiff = mpc.connect(hostname, computation_id, options, config);
jiff.wait_for(config.compute_parties, function () {
$('#processButton').attr('disabled', false); $('#output').append('<p>Connected to the compute parties!</p>');
});
}

// eslint-disable-next-line no-unused-vars
function submit() {
var arr = JSON.parse(document.getElementById('inputText').value);

if (arr.length !== config.input_length) {
alert('Please input an array of length ' + config.input_length + '.');
return;
}

for (var i = 0; i < arr.length; i++) {
if (typeof(arr[i]) !== 'number') {
alert('Please input an array of integers.');
return;
}
}

$('#processButton').attr('disabled', true);
$('#output').append('<p>Starting...</p>');

// eslint-disable-next-line no-undef
var promise = mpc.compute(arr);
promise.then(function (opened_array) {
var results = {
sum: opened_array[0],
product: opened_array[1]
};
handleResult(results);
});
}

function handleResult(results) {
$('#output').append('<p>The sum is ' + results.sum + ' and the inner product is ' + results.product + '.</p>');
$('#button').attr('disabled', false);
}
Loading

0 comments on commit ed23049

Please sign in to comment.