Skip to content

Commit

Permalink
Fix/Preprocess immaturity (#287)
Browse files Browse the repository at this point in the history
* id assignment properly awaited, but preprocess does not complete

* Unnecessary await inside the innerprod func removed, which wa salso a blocker

* Unnecessary await inside the innerprod func removed, which wa salso a blocker

* Unnecessary await inside the innerprod func removed, which wa salso a blocker

* preprocess worked, but bit-prepro not

* bit-preprocess worked

* bit-preprocess works & createClient modularized
  • Loading branch information
Snafkin547 authored May 28, 2024
1 parent 71bb3af commit 05a44ff
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 28 deletions.
4 changes: 3 additions & 1 deletion lib/client/handlers/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = function (jiffClient) {
* @memberof handlers
*/
jiffClient.handlers.connected = function () {
console.log('Connected!', jiffClient.id); // TODO: remove debugging
console.log('Connected!', jiffClient.id); // TODO: remove debugging - the id is null unless id set in advance
jiffClient.initialization_counter++;

if (jiffClient.secret_key == null && jiffClient.public_key == null) {
Expand Down Expand Up @@ -91,6 +91,8 @@ module.exports = function (jiffClient) {

// store the received public keys and resolve wait callbacks
jiffClient.handlers.store_public_keys(msg.public_keys);

jiffClient.resolveInit(); // This ensures the id assignment
};

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/jiff-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ function JIFFClient(hostname, computation_id, options) {
// set up counters for op_ids
counters(this);

this.initPromise = new Promise((resolve, reject) => {
this.resolveInit = resolve;
this.rejectInit = reject;
});

this.socketConnect = function (JIFFClientInstance) {
if (options.__internal_socket == null) {
/**
Expand Down
4 changes: 2 additions & 2 deletions tests/regr-tests/arithmetics.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('JIFF Arithmetic Operations', () => {
const init_server = require('./server');
const createClient = require('./common');
const jiff_s_bignumber = require('../../lib/ext/jiff-server-bignumber.js');
const JIFFClient = require('../../lib/jiff-client.js');
const jiff_bignumber = require('../../lib/ext/jiff-client-bignumber.js');
const jiff_fixedpoint = require('../../lib/ext/jiff-client-fixedpoint.js');
const jiff_negativenumber = require('../../lib/ext/jiff-client-negativenumber.js');
Expand Down Expand Up @@ -30,7 +30,7 @@ describe('JIFF Arithmetic Operations', () => {
Zp: 562949948117
};

jiffClients = Array.from({ length: party_count }, () => new JIFFClient(baseUrl, computation_id, options));
jiffClients = await Promise.all(Array.from({ length: party_count }, (_, index) => createClient(baseUrl, computation_id, options, index)));

async function apply_extension(jiff: any) {
await jiff.apply_extension(jiff_bignumber, options);
Expand Down
4 changes: 2 additions & 2 deletions tests/regr-tests/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('JIFF Array Operations', () => {
const init_server = require('./server');
const JIFFClient = require('../../lib/jiff-client.js');
const createClient = require('./common');

let jiffClients: any[] = [];
let jiffServer: any;
Expand All @@ -24,7 +24,7 @@ describe('JIFF Array Operations', () => {
crypto_provider: true
};

jiffClients = Array.from({ length: party_count }, () => new JIFFClient(baseUrl, computation_id, options));
jiffClients = await Promise.all(Array.from({ length: party_count }, (_, index) => createClient(baseUrl, computation_id, options, index)));
});

afterEach(async () => {
Expand Down
22 changes: 12 additions & 10 deletions tests/regr-tests/bit-preprocess.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('JIFF Preprocessing Operations', () => {
const init_server = require('./server');
const createClient = require('./common');
const jiff_s_bignumber = require('../../lib/ext/jiff-server-bignumber.js');
const JIFFClient = require('../../lib/jiff-client.js');
const jiff_bignumber = require('../../lib/ext/jiff-client-bignumber.js');
const jiff_fixedpoint = require('../../lib/ext/jiff-client-fixedpoint.js');

Expand All @@ -27,7 +27,7 @@ describe('JIFF Preprocessing Operations', () => {
crypto_provider: true
};

jiffClients = Array.from({ length: party_count }, () => new JIFFClient(baseUrl, computation_id, options));
jiffClients = await Promise.all(Array.from({ length: party_count }, (_, index) => createClient(baseUrl, computation_id, options, index)));

async function apply_extension(jiff: any) {
await jiff.apply_extension(jiff_bignumber, options);
Expand All @@ -46,17 +46,19 @@ describe('JIFF Preprocessing Operations', () => {
});

it('should correctly preprocess bitwise operation and return 3000', async () => {
async function bit_smult(jiffClient: any, id: number) {
await jiffClient.preprocessing('smult', 2, 'bits', null, null, null, null, null, { div: false });
await jiffClient.preprocessing('open', 1);
function bit_smult(jiffClient: any, id: number) {
jiffClient.preprocessing('smult', 2, null, null, null, null, null, null, { div: false });
jiffClient.preprocessing('open', 1);
return new Promise((resolve, reject) => {
jiffClient.wait_for([1, 2], async () => {
try {
const jiff_bits = jiffClient.protocols.bits;
const input = await jiff_bits.share(entries[id]);
const sec_ttl = await jiff_bits.smult(input[1], input[2]);
const result = await jiff_bits.open(sec_ttl);
resolve(result.toString(10));
await jiffClient.executePreprocessing(async function () {
const jiff_bits = jiffClient.protocols.bits;
const input = await jiff_bits.share(entries[id]);
const sec_ttl = await jiff_bits.smult(input[1], input[2]);
const result = await jiff_bits.open(sec_ttl);
resolve(result.toString(10));
});
} catch (error) {
reject(error);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/regr-tests/bitwise-comp.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
describe('JIFF bitshare Comparison', () => {
const init_server = require('./server');
const createClient = require('./common');
const jiff_s_bignumber = require('../../lib/ext/jiff-server-bignumber.js');
const JIFFClient = require('../../lib/jiff-client.js');
const jiff_bignumber = require('../../lib/ext/jiff-client-bignumber.js');
Expand Down Expand Up @@ -27,7 +28,7 @@ describe('JIFF bitshare Comparison', () => {
crypto_provider: true
};

jiffClients = Array.from({ length: party_count }, () => new JIFFClient(baseUrl, computation_id, options));
jiffClients = await Promise.all(Array.from({ length: party_count }, (_, index) => createClient(baseUrl, computation_id, options, index)));

async function apply_extension(jiff: any) {
await jiff.apply_extension(jiff_bignumber, options);
Expand Down
4 changes: 2 additions & 2 deletions tests/regr-tests/bitwise-ops.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('JIFF bitwise Arithmetic Operations', () => {
const init_server = require('./server');
const createClient = require('./common');
const jiff_s_bignumber = require('../../lib/ext/jiff-server-bignumber.js');
const JIFFClient = require('../../lib/jiff-client.js');
const jiff_bignumber = require('../../lib/ext/jiff-client-bignumber.js');
const jiff_fixedpoint = require('../../lib/ext/jiff-client-fixedpoint.js');
const jiff_negativenumber = require('../../lib/ext/jiff-client-negativenumber.js');
Expand All @@ -28,7 +28,7 @@ describe('JIFF bitwise Arithmetic Operations', () => {
crypto_provider: true
};

jiffClients = Array.from({ length: party_count }, () => new JIFFClient(baseUrl, computation_id, options));
jiffClients = await Promise.all(Array.from({ length: party_count }, (_, index) => createClient(baseUrl, computation_id, options, index)));

async function apply_extension(jiff: any) {
await jiff.apply_extension(jiff_bignumber, options);
Expand Down
9 changes: 9 additions & 0 deletions tests/regr-tests/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const JIFFClient = require('../../lib/jiff-client.js');

async function createClient(baseUrl: string, computation_id: string, options: any) {
const clientOptions = { ...options };
const client = new JIFFClient(baseUrl, computation_id, clientOptions);
await client.initPromise;
return client;
}
module.exports = createClient;
11 changes: 5 additions & 6 deletions tests/regr-tests/preprocess.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('JIFF Preprocessing Operations', () => {
const init_server = require('./server');
const createClient = require('./common');
const jiff_s_bignumber = require('../../lib/ext/jiff-server-bignumber.js');
const JIFFClient = require('../../lib/jiff-client.js');
const jiff_bignumber = require('../../lib/ext/jiff-client-bignumber.js');
const jiff_fixedpoint = require('../../lib/ext/jiff-client-fixedpoint.js');

Expand All @@ -26,8 +26,7 @@ describe('JIFF Preprocessing Operations', () => {
party_count: party_count,
crypto_provider: true
};

jiffClients = Array.from({ length: party_count }, () => new JIFFClient(baseUrl, computation_id, options));
jiffClients = await Promise.all(Array.from({ length: party_count }, (_, idx) => createClient(baseUrl, computation_id, options)));

async function apply_extension(jiff: any) {
await jiff.apply_extension(jiff_bignumber, options);
Expand All @@ -46,9 +45,9 @@ describe('JIFF Preprocessing Operations', () => {
});

it('should correctly preprocess inner product of the input array and return 329.59', async () => {
async function innerprod(jiffClient: any, id: number) {
await jiffClient.preprocessing('smult', entries[id].length, null, null, null, null, null, null, { div: false });
await jiffClient.preprocessing('open', 1);
function innerprod(jiffClient: any, id: number) {
jiffClient.preprocessing('smult', entries[id].length, null, null, null, null, null, null, { div: false });
jiffClient.preprocessing('open', 1);
return new Promise((resolve, reject) => {
jiffClient.wait_for([1, 2, 3], async () => {
try {
Expand Down
4 changes: 2 additions & 2 deletions tests/regr-tests/statistics.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('JIFF Statistics Operations', () => {
const init_server = require('./server');
const createClient = require('./common');
const jiff_s_bignumber = require('../../lib/ext/jiff-server-bignumber.js');
const JIFFClient = require('../../lib/jiff-client.js');
const jiff_bignumber = require('../../lib/ext/jiff-client-bignumber.js');
const jiff_fixedpoint = require('../../lib/ext/jiff-client-fixedpoint.js');
const jiff_negativenumber = require('../../lib/ext/jiff-client-negativenumber.js');
Expand All @@ -28,7 +28,7 @@ describe('JIFF Statistics Operations', () => {
crypto_provider: true
};

jiffClients = Array.from({ length: party_count }, () => new JIFFClient(baseUrl, computation_id, options));
jiffClients = await Promise.all(Array.from({ length: party_count }, (_, index) => createClient(baseUrl, computation_id, options, index)));

async function apply_extension(jiff: any) {
await jiff.apply_extension(jiff_bignumber, options);
Expand Down
4 changes: 2 additions & 2 deletions tests/regr-tests/vote.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('JIFF Voting', () => {
const init_server = require('./server');
const JIFFClient = require('../../lib/jiff-client.js');
const createClient = require('./common');
const jiff_negativenumber = require('../../lib/ext/jiff-client-negativenumber.js');

let jiffClients: any[] = [];
Expand All @@ -24,7 +24,7 @@ describe('JIFF Voting', () => {
crypto_provider: true
};

jiffClients = Array.from({ length: party_count }, () => new JIFFClient(baseUrl, computation_id, options));
jiffClients = await Promise.all(Array.from({ length: party_count }, (_, index) => createClient(baseUrl, computation_id, options, index)));

async function apply_extension(jiff: any) {
await jiff.apply_extension(jiff_negativenumber, options);
Expand Down

0 comments on commit 05a44ff

Please sign in to comment.