Skip to content

Commit

Permalink
Class/protocols/numbers (#294)
Browse files Browse the repository at this point in the history
* aritmetic done

* comparison done

* protocols done

* boolean added

* merge conflict resolved
  • Loading branch information
Snafkin547 authored Jun 6, 2024
1 parent b6e3c1a commit bffa9d5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 68 deletions.
2 changes: 1 addition & 1 deletion lib/client/preprocessing/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class PreprocessingAPI {
id: null,
params: params,
protocols: protocols,
deferred: new this.jiffClient.helpers.Deferred()
deferred: this.jiffClient.helpers.createDeferred()
};

this.preprocessingTasks[this.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 @@ -11,8 +11,8 @@ class PreprocessDaemon {
if (task.count > 1) {
const remainingTasks = Object.assign({}, task);

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

deferredChain.push(nextTask.deferred.promise);
Expand Down
54 changes: 28 additions & 26 deletions lib/client/protocols/numbers/arithmetic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Arithmetic operations on shares
module.exports = function (SecretShare) {
class Arithmetic {
/**
* Addition with a constant.
* @method cadd
Expand All @@ -8,7 +8,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.cadd = function (cst) {
cadd(cst) {
if (!this.isConstant(cst)) {
throw new Error('parameter should be a number (+)');
}
Expand All @@ -19,7 +19,7 @@ module.exports = function (SecretShare) {
};

return new this.jiff.SecretShare(this.wThen(ready), this.holders, this.threshold, this.Zp);
};
}

/**
* Subtraction with a constant.
Expand All @@ -29,7 +29,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.csub = function (cst) {
csub(cst) {
if (!this.isConstant(cst)) {
throw new Error('parameter should be a number (-)');
}
Expand All @@ -40,7 +40,7 @@ module.exports = function (SecretShare) {
};

return new this.jiff.SecretShare(this.wThen(ready), this.holders, this.threshold, this.Zp);
};
}

/**
* Multiplication by a constant.
Expand All @@ -50,7 +50,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.cmult = function (cst) {
cmult(cst) {
if (!this.isConstant(cst)) {
throw new Error('parameter should be a number (*)');
}
Expand All @@ -61,7 +61,7 @@ module.exports = function (SecretShare) {
};

return new this.jiff.SecretShare(this.wThen(ready), this.holders, this.threshold, this.Zp);
};
}

/**
* Division by a constant factor of the number represented by the share.
Expand All @@ -71,7 +71,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.cdivfac = function (cst) {
cdivfac(cst) {
if (!this.isConstant(cst)) {
throw new Error('Parameter should be a number (cdivfac)');
}
Expand All @@ -84,7 +84,7 @@ module.exports = function (SecretShare) {
};

return new this.jiff.SecretShare(this.wThen(ready), this.holders, this.threshold, this.Zp);
};
}

/**
* Addition of two secret shares.
Expand All @@ -103,7 +103,7 @@ module.exports = function (SecretShare) {
* }
*
*/
SecretShare.prototype.sadd = function (o) {
sadd(o) {
if (!(o.jiff === this.jiff)) {
throw new Error('shares do not belong to the same instance (+)');
}
Expand All @@ -122,7 +122,7 @@ module.exports = function (SecretShare) {

// promise to execute ready_add when both are ready
return new this.jiff.SecretShare(this.when_both_ready(o, ready), this.holders, Math.max(this.threshold, o.threshold), this.Zp);
};
}

/**
* Subtraction of two secret shares.
Expand All @@ -132,7 +132,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.ssub = function (o) {
ssub(o) {
if (!(o.jiff === this.jiff)) {
throw new Error('shares do not belong to the same instance (-)');
}
Expand All @@ -151,7 +151,7 @@ module.exports = function (SecretShare) {

// promise to execute ready_add when both are ready
return new this.jiff.SecretShare(this.when_both_ready(o, ready), this.holders, Math.max(this.threshold, o.threshold), this.Zp);
};
}

/**
* Multiplication of two secret shares through Beaver Triplets.
Expand All @@ -166,7 +166,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.smult = function (o, op_id) {
smult(o, op_id) {
if (!(o.jiff === this.jiff)) {
throw new Error('shares do not belong to the same instance (*)');
}
Expand Down Expand Up @@ -231,7 +231,7 @@ module.exports = function (SecretShare) {
}

return result;
};
}

/**
* Multiplication of two secret shares through BGW protocol.
Expand All @@ -246,7 +246,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.smult_bgw = function (o, op_id) {
smult_bgw(o, op_id) {
if (!(o.jiff === this.jiff)) {
throw new Error('shares do not belong to the same instance (bgw*)');
}
Expand Down Expand Up @@ -281,7 +281,7 @@ module.exports = function (SecretShare) {
// reshare to reduce threshold and return when ready
const result = new this.jiff.SecretShare(this.when_both_ready(o, ready), this.holders, new_threshold, this.Zp);
return this.jiff.reshare(result, Math.max(this.threshold, o.threshold), result.holders, result.holders, result.Zp, op_id + ':threshold');
};
}

/**
* Integer divison with two shares (this / o)
Expand All @@ -295,7 +295,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.sdiv = function (o, l, op_id) {
sdiv(o, l, op_id) {
if (!(o.jiff === this.jiff)) {
throw new Error('shares do not belong to the same instance (!=)');
}
Expand Down Expand Up @@ -323,7 +323,7 @@ module.exports = function (SecretShare) {

// Convert to number and return
return this.jiff.protocols.bits.bit_composition(quotient_bits);
};
}

/**
* Integer divison with a share and a constant (this / cst).
Expand All @@ -336,7 +336,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.cdiv = function (cst, op_id) {
cdiv(cst, op_id) {
if (!this.isConstant(cst)) {
throw new Error('parameter should be a number (/)');
}
Expand Down Expand Up @@ -408,7 +408,7 @@ module.exports = function (SecretShare) {
// special case, if result is zero, sometimes we will get to -1 due to how correction happens above (.csub(1) and then compare)
const zeroIt = this.iclt(cst, op_id + ':zero_check').inot();
return result.ismult(zeroIt, op_id + ':zero_it');
};
}

/**
* Remainder with two shares (this % o)
Expand All @@ -422,7 +422,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.smod = function (o, l, op_id) {
smod(o, l, op_id) {
if (!(o.jiff === this.jiff)) {
throw new Error('shares do not belong to the same instance (!=)');
}
Expand Down Expand Up @@ -450,7 +450,7 @@ module.exports = function (SecretShare) {

// Convert to number and return
return this.jiff.protocols.bits.bit_composition(remainder_bits);
};
}

/**
* Fast (modular) exponentiation with constant exponent via repeated squaring.
Expand All @@ -463,7 +463,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.cpow = function (cst, op_id) {
cpow(cst, op_id) {
if (!this.isConstant(cst)) {
throw new Error('parameter should be a number (*)');
}
Expand Down Expand Up @@ -505,5 +505,7 @@ module.exports = function (SecretShare) {
}

return evens.ismult(odds, op_id + ':smult0:' + i);
};
};
}
}

module.exports = Arithmetic;
Loading

0 comments on commit bffa9d5

Please sign in to comment.