Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cypress Implemented for binary search on a secret array #276

Merged
merged 57 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
e128434
cypress installed
Snafkin547 Apr 11, 2024
f532a29
Baseline Cypress components created
Snafkin547 Apr 12, 2024
6d97c8c
Baseline Cypress components created
Snafkin547 Apr 12, 2024
bdfb56e
Baseline Cypress test created but ws not working properly
Snafkin547 Apr 12, 2024
43d2c9a
Baseline Cypress test created but input not appropriate
Snafkin547 Apr 12, 2024
22c7bc1
Web worker worked
Snafkin547 Apr 17, 2024
7a347d6
Web worker worked
Snafkin547 Apr 17, 2024
97f5e6d
Web worker worked
Snafkin547 Apr 17, 2024
b29fe85
Refactored
Snafkin547 Apr 17, 2024
cb3ca5f
ts config for cypress done
Snafkin547 Apr 17, 2024
914cdb5
Type conflict resolved
Snafkin547 Apr 17, 2024
417cb59
prettier
Snafkin547 Apr 17, 2024
8f12bbe
bubble sort worked
Snafkin547 Apr 18, 2024
4259c89
refactored binary search
Snafkin547 Apr 18, 2024
9325f9b
Refactored Array Binary Search
Snafkin547 Apr 18, 2024
697711d
array concat works
Snafkin547 Apr 18, 2024
302583f
merge sort worked
Snafkin547 Apr 18, 2024
c56c803
Array Shuffle worked
Snafkin547 Apr 18, 2024
2df792c
Sustring Search Test done
Snafkin547 Apr 18, 2024
6bd9512
Modified some comments in test.cy
Snafkin547 Apr 18, 2024
ec6c1e9
Valentines day sample wokred
Snafkin547 Apr 18, 2024
d8b69b5
Updated it descriptions
Snafkin547 Apr 18, 2024
9142d27
Shell sort worked
Snafkin547 Apr 19, 2024
af44c20
Test workflow worked in array mpc as service
Snafkin547 Apr 22, 2024
93f285f
Update socket.js
Snafkin547 Apr 23, 2024
78f87e3
Update socket.js
Snafkin547 Apr 23, 2024
399040a
redoing investigation adftr connect
Snafkin547 Apr 23, 2024
80f5ff5
Merge branch 'cypress' of github.com:multiparty/jiff into cypress
Snafkin547 Apr 23, 2024
b9303ee
Retrieving previous state
Snafkin547 Apr 23, 2024
54be72d
Retrieving previous state
Snafkin547 Apr 23, 2024
a2bc4a8
Retrieving previous state
Snafkin547 Apr 23, 2024
13f9e30
retrieved prev jiff-client-restful
Snafkin547 Apr 23, 2024
10c1923
axios removed
Snafkin547 Apr 23, 2024
baadda0
axios removed
Snafkin547 Apr 23, 2024
a77868d
array-concat tests modified
Snafkin547 Apr 25, 2024
96cd726
cypress added
Snafkin547 Apr 25, 2024
e0431cd
renamed cypress
Snafkin547 Apr 25, 2024
3613e87
file dir changed
Snafkin547 Apr 25, 2024
f0e7443
Update test.sh
Snafkin547 Apr 25, 2024
390d0fd
wait time between tests added
Snafkin547 Apr 25, 2024
700bd71
added more wait time for shell sort
Snafkin547 Apr 25, 2024
5fdee85
Moved cypress to demos
Snafkin547 Apr 25, 2024
97b9162
cypress path updated in github action
Snafkin547 Apr 25, 2024
a242f74
demo files saved directry in demos
Snafkin547 Apr 25, 2024
3a2625d
cypress github action amended
Snafkin547 Apr 25, 2024
bffc0a6
Increased time wait for shell sort
Snafkin547 Apr 25, 2024
14a6dbb
valentine vote retrieved
Snafkin547 Apr 25, 2024
47e5e7c
Increased time wait for shell sort
Snafkin547 Apr 25, 2024
5dcefc0
Increased time wait for concat
Snafkin547 Apr 25, 2024
b42af0a
Increased time wait for concat
Snafkin547 Apr 25, 2024
b8c6168
modified concat test back to orgnal
Snafkin547 Apr 25, 2024
5111bd2
j appended
Snafkin547 Apr 25, 2024
933c58b
j appended
Snafkin547 Apr 25, 2024
a69767d
Retrieved arr-mpc as service
Snafkin547 Apr 25, 2024
3233ff2
readme retrieved
Snafkin547 Apr 25, 2024
79fca7d
Wrongly referenced path corrected
Snafkin547 Apr 25, 2024
b2f92d8
Wrongly referenced path corrected
Snafkin547 Apr 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'cypress';

export default defineConfig({
e2e: {
baseUrl: 'http://localhost:8080/cypress/e2e'
}
});
7 changes: 7 additions & 0 deletions cypress/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'cypress';

export default defineConfig({

Check failure on line 3 in cypress/cypress.config.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cypress/cypress.config.ts#L3

Unsafe call of an `any` typed value.
e2e: {
baseUrl: 'http://localhost:8080/cypress/e2e'
}
});
58 changes: 58 additions & 0 deletions cypress/e2e/array-binary-search/client.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!-- Basic UI for running the demo in the browser -->

<html>
<head>
<title>Binary Search Tree</title>
<style>
.error {
color: #FF0000;
}
</style>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
Fixed Show fixed Hide fixed
<script src="../../../dist/jiff-client.js"></script>
<script src="../../../dist/jiff-client-websockets.js"></script>

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

<!-- Contains the MPC implementation -->
<script type="text/javascript" src="./mpc.js"></script>
</head>
<body>
<h1>Connect JIFF</h1>
<label for="computation_id">Computation ID</label><input id="computation_id" value="test"></input><br/><br/>
<label for="role">Choose Role</label>
<button id="connectButton" onclick="connect(1);connect(2)">Connect</button>
<br/><br/>
<hr/>
<div class="container" id="input1" style="display: block">
<div class="row">
<h1>Input Sorted Array</h1>
</div>
<div>
<p>Enter an array of sorted positive integers, each smaller than 13.</p>
</div>
<textarea rows="4" cols="50" id="inputText1">[1, 2, 7, 10, 11, 12]</textarea>
<br/><br/>
<div class="row">
<button class="btn btn-secondary" id="submit1" onclick="submit(1);">Submit</button>
</div>
</div>

<div class="container" id="input2" style="display: block">
<div class="row">
<h1>Input Search Element.</h1>
</div>
<div>
<p>Must be less than 13</p>
</div>
<input id="inputText2" pattern="[0-9]*"/>
<br/><br/>
<div class="row">
<button class="btn btn-secondary" id="submit2" onclick="submit(2);">Submit</button>
</div>
</div>

<div id="output"></div>
</body>
</html>
71 changes: 71 additions & 0 deletions cypress/e2e/array-binary-search/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
let worker = new Worker('./web-worker.js');

function connect(party_id) {

Check failure on line 3 in cypress/e2e/array-binary-search/client.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cypress/e2e/array-binary-search/client.js#L3

'connect' is defined but never used.
$('#connectButton').prop('disabled', true);
let computation_id = $('#computation_id').val();

let options = { party_id: party_id, party_count: 2, Zp: 13 };

let hostname = window.location.hostname.trim();
let 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
worker.postMessage({
type: 'init_' + String(party_id),
hostname: hostname,
computation_id: computation_id,
options: options
});
}

worker.onmessage = function (e) {
if (e.data.type === 'result1' || e.data.type === 'result2') {
const msg = e.data.result === 1 ? 'Element Found' : 'Element Does Not Exist';
document.querySelector('#output').innerHTML += `<p>${msg}</p>`;
}
};

// eslint-disable-next-line no-unused-vars
function submit(party_id) {
let arr;
if (party_id == 1) {
arr = JSON.parse(document.getElementById('inputText' + String(party_id)).value);
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= 13) {
alert('All numbers must be less than 13');
return;
}
}
} else {
console.log(party_id);
arr = parseInt($('#inputText' + String(party_id)).val());
console.log(arr);
if (arr == null || isNaN(arr)) {
alert('Element must be a whole number');
return;
}
if (arr < 0 || arr >= 13) {
alert('Element must be between 0 and 13 exclusive');
return;
}
}

worker.postMessage({
type: 'compute' + String(party_id),
input: arr
});
}
75 changes: 75 additions & 0 deletions cypress/e2e/array-binary-search/mpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
(function (exports, node) {
/**
* Connect to the server and initialize the jiff instance
*/
exports.connect = function (hostname, computation_id, options) {
let opt = Object.assign({}, options);
// Added options goes here
opt.crypto_provider = true;

if (node) {
// eslint-disable-next-line no-undef
JIFFClient = require('../../lib/jiff-client');
// eslint-disable-next-line no-undef,no-global-assign
$ = require('jquery-deferred');
// eslint-disable-next-line no-undef
jiff_websockets = require('../../lib/ext/jiff-client-websockets.js');
}

// eslint-disable-next-line no-undef
let jiff_instance = new JIFFClient(hostname, computation_id, opt);
// eslint-disable-next-line no-undef
jiff_instance.apply_extension(jiff_websockets, opt);

return jiff_instance;
};

exports.compute = async function (input, jiff_instance) {
if (jiff_instance.id === 1) {
input.sort(function (a, b) {
return a - b;
});
}
return new Promise((resolve, reject) => {
jiff_instance.wait_for([1, 2], async () => {
try {
const inputs = await jiff_instance.share_array(input);

const array = inputs[1];
const elem = inputs[2];

const occurrences = await binary_search(array, elem);
result = await jiff_instance.open(occurrences);
resolve(result);
} catch (error) {
reject(error);
}
});
});
};

function binary_search(array, element) {
if (array.length === 1) {
return array[0].seq(element);
}

// comparison
let mid = Math.floor(array.length / 2);
let cmp = element.slt(array[mid]);

// Slice array in half, choose slice depending on cmp
let nArray = [];
for (let i = 0; i < mid; i++) {
let c1 = array[i];
let c2 = array[mid + i];
nArray[i] = cmp.if_else(c1, c2);
}

// watch out for off by 1 errors if length is odd.
if (2 * mid < array.length) {
nArray[mid] = array[2 * mid];
}

return binary_search(nArray, element);
}
})(typeof exports === 'undefined' ? (this.mpc = {}) : exports, typeof exports !== 'undefined');
33 changes: 33 additions & 0 deletions cypress/e2e/array-binary-search/test.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe('Array Binary Search', () => {
before(() => {
// Load the fixture data before the tests
cy.fixture('mpc_input.json').as('inputData');
});

it('Search Output', () => {
// Visit the HTML page
cy.visit('array-binary-search/client.html');

// Load the input data and interact with the UI
cy.get('@inputData').then((inputData) => {
const arrayInput = (inputData as any)['array-binary-search']['1'] as number[];
const elementInput = (inputData as any)['array-binary-search']['2'] as number;

// Ensure the correct role is selected and the inputs are visible
cy.get('#connectButton').click();

// Input the array by contributor 1
cy.get('#input1').should('be.visible');
cy.get('#inputText1').clear().type(JSON.stringify(arrayInput));
cy.get('#submit1').click();

// Input the element to be searched by contributor 2
cy.get('#input2').should('be.visible');
cy.get('#inputText2').clear().type(`${elementInput}`);
cy.get('#submit2').click();

// Check the output
cy.get('#output').should('contain', 'Element Found');
});
});
});
27 changes: 27 additions & 0 deletions cypress/e2e/array-binary-search/web-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
importScripts('./mpc.js', '../../../dist/jiff-client.js', '../../../dist/jiff-client-websockets.js');

let instance1;
let instance2;

self.onmessage = function (event) {
const data = event.data;

switch (data.type) {
case 'init_1':
instance1 = mpc.connect(data.hostname, data.computation_id, data.options);
break;
case 'init_2':
instance2 = mpc.connect(data.hostname, data.computation_id, data.options);
break;
case 'compute1':
mpc.compute(data.input, instance1).then((result) => {
self.postMessage({ result: result, type: 'result1' });
});
break;
case 'compute2':
mpc.compute(data.input, instance2).then((result) => {
self.postMessage({ result: result, type: 'result2' });
});
break;
}
};
35 changes: 35 additions & 0 deletions cypress/e2e/array-bubble-sort/client.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!-- Basic UI for running the demo in the browser -->

<html>
<head>
<title>Bubble Sort</title>
<style>
.error {
color: #FF0000;
}
</style>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
<script src="../../../dist/jiff-client.js"></script>
<script src="../../../dist/jiff-client-websockets.js"></script>

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

<!-- Contains the MPC implementation -->
<script type="text/javascript" src="./mpc.js"></script>
</head>
<body>
<h1>Connect JIFF</h1>
<label for="computation_id">Computation ID</label><input id="computation_id" value="test"></input><br/><br/>
<label for="count">Party Count<label> <input id="count" pattern="[0-9]*" value="2"> &nbsp; <button id="connectButton" onclick="connect(1);connect(2);">Connect</button>
<br/><br/>
<hr/>
<h1>Bubble Sort</h1>
<h2>Array of numbers will be added to all the other parties input arrays (element-wise), then sorted using bubble sort.</h2>

<label for="number">Enter your array - all input arrays must be of the same length: </label> <br/><br/>
<textarea id="inputText1" rows="5" cols="35">[1,3,2]</textarea> &nbsp; <button onclick="submit(1);" id="submit1">Start</button><br/>
<textarea id="inputText2" rows="5" cols="35">[1,3,2]</textarea> &nbsp; <button onclick="submit(2);" id="submit2">Start</button><br/>
<div id="output"></div>
</body>
</html>
64 changes: 64 additions & 0 deletions cypress/e2e/array-bubble-sort/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
let worker = new Worker('./web-worker.js');
function connect(party_id) {
$('#connectButton').prop('disabled', true);
var computation_id = $('#computation_id').val();
var party_count = parseInt($('#count').val());

Check warning on line 5 in cypress/e2e/array-bubble-sort/client.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cypress/e2e/array-bubble-sort/client.js#L5

Always provide a base when using parseInt() functions

if (isNaN(party_count)) {
$('#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 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
worker.postMessage({
type: 'init_' + String(party_id),
hostname: hostname,
computation_id: computation_id,
options: options
});
}
}

worker.onmessage = function (e) {
if (e.data.type === 'result1' || e.data.type === 'result2') {
$('#output').append('<p>Result is: ' + e.data.result + '</p>');

Check warning on line 42 in cypress/e2e/array-bubble-sort/client.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cypress/e2e/array-bubble-sort/client.js#L42

HTML passed in to function '$('#output').append'
$('#button').attr('disabled', false);
}
};

// eslint-disable-next-line no-unused-vars
function submit(party_id) {
$('#submit' + String(party_id)).attr('disabled', true);
let arr = JSON.parse(document.getElementById('inputText' + String(party_id)).value);

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

// eslint-disable-next-line no-undef
worker.postMessage({
type: 'compute' + String(party_id),
input: arr
});
}
Loading
Loading