From bffa9d5dcdf06eb493c75c0c2cdc1630f5f3912f Mon Sep 17 00:00:00 2001 From: Snafkin547 <62607343+Snafkin547@users.noreply.github.com> Date: Thu, 6 Jun 2024 16:13:05 -0400 Subject: [PATCH] Class/protocols/numbers (#294) * aritmetic done * comparison done * protocols done * boolean added * merge conflict resolved --- lib/client/preprocessing/api.js | 2 +- lib/client/preprocessing/daemon.js | 6 +-- lib/client/protocols/numbers/arithmetic.js | 54 ++++++++++---------- lib/client/protocols/numbers/comparison.js | 58 +++++++++++----------- lib/client/protocols/numbers/protocols.js | 14 +++--- lib/client/share.js | 13 +++-- 6 files changed, 79 insertions(+), 68 deletions(-) diff --git a/lib/client/preprocessing/api.js b/lib/client/preprocessing/api.js index 9728bd9b8..fd24f676b 100644 --- a/lib/client/preprocessing/api.js +++ b/lib/client/preprocessing/api.js @@ -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); diff --git a/lib/client/preprocessing/daemon.js b/lib/client/preprocessing/daemon.js index 846dda427..949f40059 100644 --- a/lib/client/preprocessing/daemon.js +++ b/lib/client/preprocessing/daemon.js @@ -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; @@ -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); diff --git a/lib/client/protocols/numbers/arithmetic.js b/lib/client/protocols/numbers/arithmetic.js index 1294b36a0..0e019edaa 100644 --- a/lib/client/protocols/numbers/arithmetic.js +++ b/lib/client/protocols/numbers/arithmetic.js @@ -1,5 +1,5 @@ // Arithmetic operations on shares -module.exports = function (SecretShare) { +class Arithmetic { /** * Addition with a constant. * @method cadd @@ -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 (+)'); } @@ -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. @@ -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 (-)'); } @@ -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. @@ -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 (*)'); } @@ -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. @@ -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)'); } @@ -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. @@ -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 (+)'); } @@ -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. @@ -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 (-)'); } @@ -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. @@ -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 (*)'); } @@ -231,7 +231,7 @@ module.exports = function (SecretShare) { } return result; - }; + } /** * Multiplication of two secret shares through BGW protocol. @@ -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*)'); } @@ -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) @@ -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 (!=)'); } @@ -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). @@ -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 (/)'); } @@ -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) @@ -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 (!=)'); } @@ -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. @@ -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 (*)'); } @@ -505,5 +505,7 @@ module.exports = function (SecretShare) { } return evens.ismult(odds, op_id + ':smult0:' + i); - }; -}; + } +} + +module.exports = Arithmetic; diff --git a/lib/client/protocols/numbers/comparison.js b/lib/client/protocols/numbers/comparison.js index c362e64b9..cc5b1feeb 100644 --- a/lib/client/protocols/numbers/comparison.js +++ b/lib/client/protocols/numbers/comparison.js @@ -1,5 +1,5 @@ // Comparison operations on shares -module.exports = function (SecretShare) { +class Comparison { /** * Greater than or equal with another share. * @method sgteq @@ -11,7 +11,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.sgteq = function (o, op_id) { + sgteq(o, op_id) { if (!(o.jiff === this.jiff)) { throw new Error('shares do not belong to the same instance (>=)'); } @@ -27,7 +27,7 @@ module.exports = function (SecretShare) { } return this.islt(o, op_id).inot(); - }; + } /** * Greater than with another share. @@ -40,7 +40,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.sgt = function (o, op_id) { + sgt(o, op_id) { if (!(o.jiff === this.jiff)) { throw new Error('shares do not belong to the same instance (>)'); } @@ -56,7 +56,7 @@ module.exports = function (SecretShare) { } return o.islt(this, op_id); - }; + } /** * Less than or equal with another share. @@ -69,7 +69,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.slteq = function (o, op_id) { + slteq(o, op_id) { if (!(o.jiff === this.jiff)) { throw new Error('shares do not belong to the same instance (<=)'); } @@ -85,7 +85,7 @@ module.exports = function (SecretShare) { } return o.islt(this, op_id).inot(); - }; + } /** * Less than with another share. @@ -98,7 +98,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.slt = function (o, op_id) { + slt(o, op_id) { if (!(o.jiff === this.jiff)) { throw new Error('shares do not belong to the same instance (<)'); } @@ -138,7 +138,7 @@ module.exports = function (SecretShare) { }); return result; - }; + } /** * Greater than or equal with a constant. @@ -151,7 +151,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.cgteq = function (cst, op_id) { + cgteq(cst, op_id) { if (!this.isConstant(cst)) { throw new Error('parameter should be a number (>=)'); } @@ -161,7 +161,7 @@ module.exports = function (SecretShare) { } return this.iclt(cst, op_id).inot(); - }; + } /** * Greater than with a constant. @@ -175,7 +175,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.cgt = function (cst, op_id) { + cgt(cst, op_id) { if (!this.isConstant(cst)) { throw new Error('parameter should be a number (>)'); } @@ -210,7 +210,7 @@ module.exports = function (SecretShare) { }); return result; - }; + } /** * Less than or equal with a constant. @@ -223,7 +223,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.clteq = function (cst, op_id) { + clteq(cst, op_id) { if (!this.isConstant(cst)) { throw new Error('parameter should be a number (<=)'); } @@ -233,7 +233,7 @@ module.exports = function (SecretShare) { } return this.icgt(cst, op_id).inot(); - }; + } /** * Less than with a constant. @@ -246,7 +246,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.clt = function (cst, op_id) { + clt(cst, op_id) { if (!this.isConstant(cst)) { throw new Error('parameter should be a number (<)'); } @@ -277,7 +277,7 @@ module.exports = function (SecretShare) { }); return result; - }; + } /** * Equality test with two shares. @@ -290,7 +290,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.seq = function (o, op_id) { + seq(o, op_id) { if (!(o.jiff === this.jiff)) { throw new Error('shares do not belong to the same instance (==)'); } @@ -305,7 +305,7 @@ module.exports = function (SecretShare) { } return this.isneq(o, op_id).inot(); - }; + } /** * Unequality test with two shares. @@ -318,7 +318,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.sneq = function (o, op_id) { + sneq(o, op_id) { if (!(o.jiff === this.jiff)) { throw new Error('shares do not belong to the same instance (!=)'); } @@ -333,7 +333,7 @@ module.exports = function (SecretShare) { } return this.issub(o).icpow(this.jiff.share_helpers['-'](this.Zp, 1), op_id + ':cpow'); - }; + } /** * Equality test with a constant. @@ -346,7 +346,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.ceq = function (cst, op_id) { + ceq(cst, op_id) { if (!this.isConstant(cst)) { throw new Error('parameter should be a number (==)'); } @@ -355,7 +355,7 @@ module.exports = function (SecretShare) { } return this.icneq(cst, op_id).inot(); - }; + } /** * Unequality test with a constant. @@ -368,7 +368,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.cneq = function (cst, op_id) { + cneq(cst, op_id) { if (!this.isConstant(cst)) { throw new Error('parameter should be a number (!=)'); } @@ -377,7 +377,7 @@ module.exports = function (SecretShare) { } return this.icsub(cst).icpow(this.jiff.share_helpers['-'](this.Zp, 1), op_id + ':cpow'); - }; + } /** * Checks whether the share is less than half the field size. @@ -389,7 +389,7 @@ module.exports = function (SecretShare) { * ensure that corresponding instructions across different parties are matched correctly. * @return {module:jiff-client~JIFFClient#SecretShare} this party's share of the result. */ - SecretShare.prototype.lt_halfprime = function (op_id) { + lt_halfprime(op_id) { if (op_id == null) { op_id = this.jiff.counters.gen_op_id('lt_halfprime', this.holders); } @@ -443,5 +443,7 @@ module.exports = function (SecretShare) { } return result; - }; -}; + } +} + +module.exports = Comparison; diff --git a/lib/client/protocols/numbers/protocols.js b/lib/client/protocols/numbers/protocols.js index 9ecdcea9d..195d9eedf 100644 --- a/lib/client/protocols/numbers/protocols.js +++ b/lib/client/protocols/numbers/protocols.js @@ -1,5 +1,5 @@ // general arithmetic protocols -module.exports = function (SecretShare) { +class Protocols { /** * Reshares/refreshes the sharing of this number, used before opening to keep the share secret. * @method refresh @@ -10,7 +10,7 @@ module.exports = function (SecretShare) { * @memberof module:jiff-client~JIFFClient#SecretShare * @instance */ - SecretShare.prototype.refresh = function (op_id) { + refresh(op_id) { if (op_id == null) { op_id = this.jiff.counters.gen_op_id('refresh', this.holders); } @@ -41,7 +41,7 @@ module.exports = function (SecretShare) { } return result; - }; + } /** * Bit Decomposition: Transform existing share to an array of bit shares. @@ -54,7 +54,7 @@ module.exports = function (SecretShare) { * @returns {module:jiff-client~JIFFClient#SecretShare[]} an array of secret shares of bits of length [ceil(log_2(this.Zp))], where * index 0 represents the least significant bit. */ - SecretShare.prototype.bit_decomposition = function (op_id) { + bit_decomposition(op_id) { if (op_id == null) { op_id = this.jiff.counters.gen_op_id('bit_decomposition', this.holders); } @@ -105,5 +105,7 @@ module.exports = function (SecretShare) { } return result; - }; -}; + } +} + +module.exports = Protocols; diff --git a/lib/client/share.js b/lib/client/share.js index 92062f42e..0b0c2c110 100644 --- a/lib/client/share.js +++ b/lib/client/share.js @@ -209,14 +209,19 @@ module.exports = function (jiff) { // Complex protocols in prototype of SecretShare genericProtocols(SecretShare); - arithmetic(SecretShare); - comparison(SecretShare); - protocols(SecretShare); - // Attach all methods from Booleans class to SecretShare prototype Object.getOwnPropertyNames(Booleans.prototype).forEach((method) => { SecretShare.prototype[String(method)] = Booleans.prototype[String(method)]; }); + Object.getOwnPropertyNames(arithmetic.prototype).forEach((method) => { + SecretShare.prototype[String(method)] = arithmetic.prototype[String(method)]; + }); + Object.getOwnPropertyNames(comparison.prototype).forEach((method) => { + SecretShare.prototype[String(method)] = comparison.prototype[String(method)]; + }); + Object.getOwnPropertyNames(protocols.prototype).forEach((method) => { + SecretShare.prototype[String(method)] = protocols.prototype[String(method)]; + }); // internal variant of primitives, to use internally by other primitives const internals = [