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

Made prototypes in the util dir classes #288

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion lib/client/api/crypto_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = function (jiffClient) {
msg = JSON.stringify(msg);

// Setup deferred to handle receiving the result later.
jiffClient.deferreds[op_id] = new jiffClient.helpers.Deferred();
jiffClient.deferreds[op_id] = jiffClient.helpers.createDeferred();
const result = jiffClient.deferreds[op_id].promise;

// send a request to the server.
Expand Down
7 changes: 4 additions & 3 deletions lib/client/arch/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* @namespace
*/

const crypto = require('../util/crypto.js');
const Crypto = require('../util/crypto.js');
const cryptoInstance = new Crypto();
const shamir_share = require('../protocols/shamir/share.js');
const shamir_open = require('../protocols/shamir/open.js');

Expand Down Expand Up @@ -48,15 +49,15 @@ Hooks.prototype.reconstructShare = shamir_open.jiff_lagrange;
// Crypto hooks
Hooks.prototype.encryptSign = function (jiffClient, message) {
if (jiffClient.sodium_ !== false) {
return crypto.encrypt_and_sign.apply(null, arguments);
return cryptoInstance.encrypt_and_sign.apply(null, arguments);
} else {
return message;
}
};

Hooks.prototype.decryptSign = function (jiffClient, cipher) {
if (jiffClient.sodium_ !== false) {
return crypto.decrypt_and_sign.apply(null, arguments);
return cryptoInstance.decrypt_and_sign.apply(null, arguments);
} else {
return cipher;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/client/handlers/sharing.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function (jiffClient) {
}
if (jiffClient.deferreds[op_id][sender_id] == null) {
// Share is received before deferred was setup, store it.
jiffClient.deferreds[op_id][sender_id] = new jiffClient.helpers.Deferred();
jiffClient.deferreds[op_id][sender_id] = jiffClient.helpers.createDeferred();
}

// Deferred is already setup, resolve it.
Expand Down
2 changes: 1 addition & 1 deletion lib/client/preprocessing/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ module.exports = function (jiffClient) {
id: null,
params: params,
protocols: protocols,
deferred: new jiffClient.helpers.Deferred()
deferred: jiffClient.helpers.createDeferred()
};

preprocessingTasks[preprocessingTasks.length - 1].add(task);
Expand Down
6 changes: 3 additions & 3 deletions lib/client/preprocessing/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = function (jiffClient) {
if (task.count > 1) {
const remainingTasks = Object.assign({}, task);

const deferred1 = new jiffClient.helpers.Deferred();
const deferred2 = new jiffClient.helpers.Deferred();
const deferred1 = jiffClient.helpers.createDeferred();
const deferred2 = jiffClient.helpers.createDeferred();
Promise.all([deferred1.promise, deferred2.promise]).then(task.deferred.resolve);
task.deferred = deferred1;
remainingTasks.deferred = deferred2;
Expand Down Expand Up @@ -165,7 +165,7 @@ module.exports = function (jiffClient) {
id: null,
params: extra_params,
protocols: task.protocols,
deferred: new jiffClient.helpers.Deferred()
deferred: jiffClient.helpers.createDeferred()
};

deferredChain.push(nextTask.deferred.promise);
Expand Down
2 changes: 1 addition & 1 deletion lib/client/protocols/arrays/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const open_ND_array = function (jiff, shares, receivers_list, senders_list, op_i
*}
*/

const final_deferred = new jiff.helpers.Deferred();
const final_deferred = jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
const resolve_open = function (shares) {
final_deferred.resolve(
Expand Down
10 changes: 5 additions & 5 deletions lib/client/protocols/arrays/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const share_2D_array = function (jiff, array, lengths, threshold, receivers_list
}

// wrap around result of share_array
const lengths_deferred = new jiff.helpers.Deferred();
const lengths_deferred = jiff.helpers.createDeferred();
const lengths_promise = lengths_deferred.promise;

// figure out lengths by having each party emit their length publicly
Expand Down Expand Up @@ -122,7 +122,7 @@ const share_2D_array = function (jiff, array, lengths, threshold, receivers_list
}

// Final results
const share_array_deferred = new jiff.helpers.Deferred();
const share_array_deferred = jiff.helpers.createDeferred();
const share_array_promise = share_array_deferred.promise;

// lengths are now set, start sharing
Expand Down Expand Up @@ -213,7 +213,7 @@ const share_array_single_sender = function (jiff, secrets, threshold, receivers_
const isReceiving = receivers_list.indexOf(jiff.id) > -1;
let deferred_array;
if (isReceiving) {
deferred_array = new jiff.helpers.Deferred();
deferred_array = jiff.helpers.createDeferred();
Promise.all(promised_array).then(function (array) {
deferred_array.resolve(array);
});
Expand Down Expand Up @@ -242,7 +242,7 @@ const share_ND_array_deferred = function (jiff, secrets, skeletons, threshold, r
const final_deferreds = [];
for (let i = 0; i < senders_list.length; i++) {
const p_id = senders_list[i];
final_deferreds[p_id] = new jiff.helpers.Deferred();
final_deferreds[p_id] = jiff.helpers.createDeferred();
}

const share_from_skeleton = share_from_skeleton_unbound.bind(null, jiff).bind(null, {
Expand Down Expand Up @@ -277,7 +277,7 @@ const share_ND_array_deferred = function (jiff, secrets, skeletons, threshold, r
}

// Combine all promises and re-index final array map
const final_deferred = new jiff.helpers.Deferred();
const final_deferred = jiff.helpers.createDeferred();
const final_promise = isReceiving ? final_deferred.promise : Promise.resolve({});
Promise.all(
(function () {
Expand Down
10 changes: 5 additions & 5 deletions lib/client/protocols/bits/arithmetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ module.exports = {
result = result.shares;

// Resolve result when ready
const final_deferred = new jiff.helpers.Deferred();
const final_deferred = jiff.helpers.createDeferred();
final_deferred.promise.then(jiff.utils.resolve_many_secrets.bind(null, deferreds));

// get useless share of zero (just for padding)
Expand Down Expand Up @@ -432,7 +432,7 @@ module.exports = {
result = result.shares;

// Resolve result when ready
const final_deferred = new jiff.helpers.Deferred();
const final_deferred = jiff.helpers.createDeferred();
final_deferred.promise.then(jiff.utils.resolve_many_secrets.bind(null, deferreds));

// Loop over *shortest* array one bit at a time
Expand Down Expand Up @@ -507,7 +507,7 @@ module.exports = {
remainder = remainder.shares;

// Resolve result when ready
const final_deferred = new jiff.helpers.Deferred();
const final_deferred = jiff.helpers.createDeferred();
final_deferred.promise.then(function (result) {
jiff.utils.resolve_many_secrets(remainderDeferreds, result);
});
Expand Down Expand Up @@ -613,7 +613,7 @@ module.exports = {
remainder = remainder.shares;

// Resolve result when ready
const final_deferred = new jiff.helpers.Deferred();
const final_deferred = jiff.helpers.createDeferred();
final_deferred.promise.then(jiff.utils.resolve_many_secrets.bind(null, remainderDeferreds));

const initial = []; // initial remainder
Expand Down Expand Up @@ -707,7 +707,7 @@ module.exports = {
remainder = remainder.shares;

// Resolve result when ready
const final_deferred = new jiff.helpers.Deferred();
const final_deferred = jiff.helpers.createDeferred();
final_deferred.promise.then(jiff.utils.resolve_many_secrets.bind(null, remainderDeferreds));

const initial = []; // initial remainder
Expand Down
8 changes: 4 additions & 4 deletions lib/client/protocols/bits/comparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = {
return true;
}

const deferred = new jiff.helpers.Deferred();
const deferred = jiff.helpers.createDeferred();
const result = new jiff.SecretShare(deferred.promise, bits[0].holders, bits[0].threshold, bits[0].Zp);

// big or of bitwise XORs
Expand Down Expand Up @@ -133,7 +133,7 @@ module.exports = {
}

// initialize result
const deferred = new jiff.helpers.Deferred();
const deferred = jiff.helpers.createDeferred();
const result = new jiff.SecretShare(deferred.promise, bits[0].holders, bits[0].threshold, bits[0].Zp);

// Subtract bits2 from bits1, only keeping track of borrow
Expand Down Expand Up @@ -249,7 +249,7 @@ module.exports = {
bits2 = bits2.slice();

// initialize result
const deferred = new jiff.helpers.Deferred();
const deferred = jiff.helpers.createDeferred();
const result = new jiff.SecretShare(deferred.promise, bits1[0].holders, Math.max(bits1[0].threshold, bits2[0].threshold), bits1[0].Zp);

// big or of bitwise XORs
Expand Down Expand Up @@ -311,7 +311,7 @@ module.exports = {
bits2 = bits2.slice();

// initialize result
const deferred = new jiff.helpers.Deferred();
const deferred = jiff.helpers.createDeferred();
const result = new jiff.SecretShare(deferred.promise, bits1[0].holders, Math.max(bits1[0].threshold, bits2[0].threshold), bits1[0].Zp);

// Subtract bits2 from bits1, only keeping track of borrow
Expand Down
4 changes: 2 additions & 2 deletions lib/client/protocols/numbers/arithmetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ module.exports = function (SecretShare) {
}

// final result
const final_deferred = new this.jiff.helpers.Deferred();
const final_deferred = this.jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
const result = new this.jiff.SecretShare(final_promise, this.holders, Math.max(this.threshold, o.threshold), this.Zp);

Expand Down Expand Up @@ -354,7 +354,7 @@ module.exports = function (SecretShare) {
}

// Allocate share for result to which the answer will be resolved once available
const final_deferred = new this.jiff.helpers.Deferred();
const final_deferred = this.jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
const result = new this.jiff.SecretShare(final_promise, this.holders, this.threshold, this.Zp);

Expand Down
8 changes: 4 additions & 4 deletions lib/client/protocols/numbers/comparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ module.exports = function (SecretShare) {
op_id = this.jiff.counters.gen_op_id('slt', this.holders);
}

const final_deferred = new this.jiff.helpers.Deferred();
const final_deferred = this.jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
const result = new this.jiff.SecretShare(final_promise, this.holders, Math.max(this.threshold, o.threshold), this.Zp);

Expand Down Expand Up @@ -184,7 +184,7 @@ module.exports = function (SecretShare) {
op_id = this.jiff.counters.gen_op_id('cgt', this.holders);
}

const final_deferred = new this.jiff.helpers.Deferred();
const final_deferred = this.jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
const result = new this.jiff.SecretShare(final_promise, this.holders, this.threshold, this.Zp);

Expand Down Expand Up @@ -255,7 +255,7 @@ module.exports = function (SecretShare) {
op_id = this.jiff.counters.gen_op_id('clt', this.holders);
}

const final_deferred = new this.jiff.helpers.Deferred();
const final_deferred = this.jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
const result = new this.jiff.SecretShare(final_promise, this.holders, this.threshold, this.Zp);

Expand Down Expand Up @@ -401,7 +401,7 @@ module.exports = function (SecretShare) {
const bitLength = this.jiff.share_helpers['ceil'](this.jiff.helpers.bLog(share.Zp, 2));

// Create result share
const final_deferred = new this.jiff.helpers.Deferred();
const final_deferred = this.jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
const result = new this.jiff.SecretShare(final_promise, this.holders, this.threshold, this.Zp);

Expand Down
2 changes: 1 addition & 1 deletion lib/client/protocols/numbers/protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function (SecretShare) {
}

// final result
const final_deferred = new this.jiff.helpers.Deferred();
const final_deferred = this.jiff.helpers.createDeferred();
const final_promise = final_deferred.promise;
const result = new this.jiff.SecretShare(final_promise, this.holders, this.threshold, this.Zp);

Expand Down
2 changes: 1 addition & 1 deletion lib/client/protocols/shamir/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module.exports = {

// Party is a receiver
if (parties.indexOf(jiff.id) > -1) {
const final_deferred = new jiff.helpers.Deferred(); // will be resolved when the final value is reconstructed
const final_deferred = jiff.helpers.createDeferred(); // will be resolved when the final value is reconstructed
const final_promise = final_deferred.promise;

if (jiff.deferreds[op_id] == null) {
Expand Down
2 changes: 1 addition & 1 deletion lib/client/protocols/shamir/reshare.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = function (jiff, share, threshold, receivers_list, senders_list,
let final_deferred;
let result = null;
if (isReceiver) {
final_deferred = new jiff.helpers.Deferred();
final_deferred = jiff.helpers.createDeferred();
result = new jiff.SecretShare(final_deferred.promise, receivers_list, threshold, Zp);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/client/protocols/shamir/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module.exports = {
// check if a deferred is set up (maybe the message was previously received)
if (jiff.deferreds[share_id][p_id] == null) {
// not ready, setup a deferred
jiff.deferreds[share_id][p_id] = new jiff.helpers.Deferred();
jiff.deferreds[share_id][p_id] = jiff.helpers.createDeferred();
}

const promise = jiff.deferreds[share_id][p_id].promise;
Expand Down
2 changes: 1 addition & 1 deletion lib/client/socket/mailbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const safe_disconnect = function (free, callback) {
}
}

this.empty_deferred = new this.jiffClient.helpers.Deferred();
this.empty_deferred = this.jiffClient.helpers.createDeferred();
this.empty_deferred.promise.then(this.safe_disconnect.bind(this, free, callback));
};

Expand Down
73 changes: 38 additions & 35 deletions lib/client/util/crypto.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
/**
* Encrypts and signs the given message.
* @ignore
* @memberof jiff.utils
* @param {number|string} message - the message to encrypt.
* @param {Uint8Array} encryption_public_key - ascii-armored public key to encrypt with.
* @param {Uint8Array} signing_private_key - the private key of the encrypting party to sign with.
* @returns {object} the signed cipher, includes two properties: 'cipher' and 'nonce'.
*/
exports.encrypt_and_sign = function (jiff, message, encryption_public_key, signing_private_key) {
const nonce = jiff.sodium_.randombytes_buf(jiff.sodium_.crypto_box_NONCEBYTES);
const cipher = jiff.sodium_.crypto_box_easy(message, nonce, encryption_public_key, signing_private_key);
class Crypto {
/**
* Encrypts and signs the given message.
* @ignore
* @memberof jiff.utils
* @param {number|string} message - the message to encrypt.
* @param {Uint8Array} encryption_public_key - ascii-armored public key to encrypt with.
* @param {Uint8Array} signing_private_key - the private key of the encrypting party to sign with.
* @returns {object} the signed cipher, includes two properties: 'cipher' and 'nonce'.
*/
encrypt_and_sign(jiff, message, encryption_public_key, signing_private_key) {
const nonce = jiff.sodium_.randombytes_buf(jiff.sodium_.crypto_box_NONCEBYTES);
const cipher = jiff.sodium_.crypto_box_easy(message, nonce, encryption_public_key, signing_private_key);

const result = { nonce: '[' + nonce.toString() + ']', cipher: '[' + cipher.toString() + ']' };
return result;
};
const result = { nonce: '[' + nonce.toString() + ']', cipher: '[' + cipher.toString() + ']' };
return result;
}

/**
* Decrypts and checks the signature of the given cipher text.
* @ignore
* @memberof jiff.utils
* @param {object} cipher_text - the cipher text to decrypt, includes two properties: 'cipher' and 'nonce'.
* @param {Uint8Array} decryption_secret_key - the secret key to decrypt with.
* @param {Uint8Array} signing_public_key - ascii-armored public key to verify against signature.
* @returns {number|string} the decrypted message if the signature was correct, the decrypted message type should
* the type of operation, such that the returned value has the appropriate type and does
* not need any type modifications.
* @throws error if signature or nonce was forged/incorrect.
*/
exports.decrypt_and_sign = function (jiff, cipher_text, decryption_secret_key, signing_public_key) {
const nonce = new Uint8Array(JSON.parse(cipher_text.nonce));
cipher_text = new Uint8Array(JSON.parse(cipher_text.cipher));
/**
* Decrypts and checks the signature of the given cipher text.
* @ignore
* @memberof jiff.utils
* @param {object} cipher_text - the cipher text to decrypt, includes two properties: 'cipher' and 'nonce'.
* @param {Uint8Array} decryption_secret_key - the secret key to decrypt with.
* @param {Uint8Array} signing_public_key - ascii-armored public key to verify against signature.
* @returns {number|string} the decrypted message if the signature was correct, the decrypted message type should
* the type of operation, such that the returned value has the appropriate type and does
* not need any type modifications.
* @throws error if signature or nonce was forged/incorrect.
*/
decrypt_and_sign(jiff, cipher_text, decryption_secret_key, signing_public_key) {
const nonce = new Uint8Array(JSON.parse(cipher_text.nonce));
cipher_text = new Uint8Array(JSON.parse(cipher_text.cipher));

try {
return jiff.sodium_.crypto_box_open_easy(cipher_text, nonce, signing_public_key, decryption_secret_key, 'text');
} catch (_) {
throw new Error('Bad signature or Bad nonce: Cipher: ' + cipher_text + '. DecSKey: ' + decryption_secret_key + '. SignPKey: ' + signing_public_key);
try {
return jiff.sodium_.crypto_box_open_easy(cipher_text, nonce, signing_public_key, decryption_secret_key, 'text');
} catch (_) {
throw new Error('Bad signature or Bad nonce: Cipher: ' + cipher_text + '. DecSKey: ' + decryption_secret_key + '. SignPKey: ' + signing_public_key);
}
}
};
}
module.exports = Crypto;
Loading
Loading