diff --git a/lib/client/protocols/generic.js b/lib/client/protocols/generic.js index fec95d141..658337949 100644 --- a/lib/client/protocols/generic.js +++ b/lib/client/protocols/generic.js @@ -10,7 +10,8 @@ module.exports = function (SecretShare) { * @return {boolean} true if o is a valid constant, false otherwise. */ SecretShare.prototype.isConstant = function (o) { - return typeof o === 'number'; + let BigNumber_ = require('bignumber.js'); + return typeof o === 'number' || o instanceof BigNumber_; }; /** diff --git a/lib/ext/jiff-client-bignumber.js b/lib/ext/jiff-client-bignumber.js index 9ef217ab5..37412f4b1 100644 --- a/lib/ext/jiff-client-bignumber.js +++ b/lib/ext/jiff-client-bignumber.js @@ -221,7 +221,7 @@ return jiff.helpers.BigNumber(v1).eq(v2); }; jiff.share_helpers['floor/'] = function (v1, v2) { - return v1.div(v2).floor(); + return v1.div(v2).decimalPlaces(0, jiff.helpers.BigNumber.ROUND_FLOOR); }; jiff.share_helpers['pow'] = function (v1, v2) { return self.jiff.helpers.BigNumber(v1).pow(v2); @@ -233,7 +233,7 @@ if (typeof v === 'number') { return Math.floor(v); } - return v.floor(); + return v.decimalPlaces(0, jiff.helpers._BigNumber.ROUND_FLOOR); }; jiff.share_helpers['ceil'] = function (v) { if (typeof v === 'number') { @@ -287,7 +287,7 @@ jiff.helpers.mod = function (x, y) { x = jiff.helpers.BigNumber(x); y = jiff.helpers.BigNumber(y); - if (x.isNeg()) { + if (x.isNegative()) { return x.mod(y).plus(y); } return x.mod(y); @@ -310,7 +310,7 @@ var x = temp[0]; var y = temp[1]; var d = temp[2]; - return [y, x.minus(y.times(a.div(b).floor())), d]; + return [y, x.minus(y.times(a.div(b).decimalPlaces(0, jiff.helpers.BigNumber.ROUND_FLOOR))), d]; })(a, b); }; @@ -339,12 +339,12 @@ var precision = max.toString().length; // eslint-disable-next-line no-undef var magnitude = jiff.helpers.BigNumber(10).pow(precision); - var multiple = magnitude.div(max).floor().times(max); + var multiple = magnitude.div(max).decimalPlaces(0, jiff.helpers._BigNumber.ROUND_FLOOR).times(max); var rand; do { // eslint-disable-next-line no-undef - rand = jiff.helpers._BigNumber.random(precision).times(magnitude).floor(); + rand = jiff.helpers._BigNumber.random(precision).times(magnitude).decimalPlaces(0, jiff.helpers.BigNumber.ROUND_FLOOR); } while (rand.gte(multiple)); return rand.mod(max); @@ -378,7 +378,7 @@ /* SHARE CHECKS */ jiff.share = function (secret, threshold, receivers_list, senders_list, Zp, share_id) { secret = secret != null ? jiff.helpers.BigNumber(secret) : secret; - if (secret != null && (!secret.floor().eq(secret) || secret.lt(0))) { + if (secret != null && (!secret.decimalPlaces(0, jiff.helpers.BigNumber.ROUND_FLOOR).eq(secret) || secret.lt(0))) { throw new Error("secret '" + secret + "' must be a non-negative whole number"); } if (secret != null && secret.gte(Zp == null ? jiff.Zp : Zp)) { diff --git a/lib/ext/jiff-client-fixedpoint.js b/lib/ext/jiff-client-fixedpoint.js index 318850a8c..0ce08226d 100644 --- a/lib/ext/jiff-client-fixedpoint.js +++ b/lib/ext/jiff-client-fixedpoint.js @@ -372,10 +372,10 @@ // optimized if_else share.if_else = function (val1, val2, op_id) { if (share.isConstant(val1)) { - val1 = magnitude.times(val1).floor(); + val1 = magnitude.times(val1).decimalPlaces(0, share.jiff.helpers.BigNumber.ROUND_FLOOR).times(max); } if (share.isConstant(val2)) { - val2 = magnitude.times(val2).floor(); + val2 = magnitude.times(val2).decimalPlaces(0, share.jiff.helpers.BigNumber.ROUND_FLOOR).times(max); } var reduce_share = share.legacy.cdivfac(magnitude); @@ -445,7 +445,7 @@ /* HELPERS */ base_instance.helpers.magnitude = function (m) { - return base_instance.helpers.BigNumber(10).pow(m).floor(); + return base_instance.helpers.BigNumber(10).pow(m).decimalPlaces(0, base_instance.helpers.BigNumber.ROUND_FLOOR); }; base_instance.helpers.fits_in_digits = function (num) { var magnitude = base_instance.helpers.magnitude(base_instance.decimal_digits + base_instance.integer_digits); @@ -466,7 +466,7 @@ throw new Error('Fixedpoint share: integer part is too big'); } var magnitude = base_instance.helpers.magnitude(base_instance.decimal_digits); - return magnitude.times(v).floor(); + return magnitude.times(v).decimalPlaces(0, base_instance.helpers.BigNumber.ROUND_FLOOR); }; base_instance.helpers.to_fixed = function (v) { v = base_instance.helpers.BigNumber(v); diff --git a/lib/ext/jiff-server-bignumber.js b/lib/ext/jiff-server-bignumber.js index 5a254c02a..a6ad5db9d 100644 --- a/lib/ext/jiff-server-bignumber.js +++ b/lib/ext/jiff-server-bignumber.js @@ -7,11 +7,11 @@ BigNumber.config({ CRYPTO: true }); function secureRandom(max) { var precision = max.toString().length; var magnitude = new BigNumber(10).pow(precision); - var multiple = magnitude.div(max).floor().times(max); + var multiple = magnitude.div(max).decimalPlaces(0, BigNumber.ROUND_FLOOR).times(max); var rand; do { - rand = BigNumber.random(precision).times(magnitude).floor(); + rand = BigNumber.random(precision).times(magnitude).decimalPlaces(0, BigNumber.ROUND_FLOOR); } while (rand.gte(multiple)); return rand.mod(max); @@ -19,7 +19,7 @@ function secureRandom(max) { function mod(x, y) { x = new BigNumber(x); y = new BigNumber(y); - if (x.isNeg()) { + if (x.isNegative()) { return x.mod(y).plus(y); } return x.mod(y); @@ -78,7 +78,7 @@ function default_preprocessing() { quotient: function (jiff, computation_id, receivers_list, threshold, Zp, params) { var constant = jiff.helpers.BigNumber(params['constant']); var noise = jiff.helpers.random(Zp); - var quotient = noise.div(constant).floor(); + var quotient = noise.div(constant).decimalPlaces(0, BigNumber.ROUND_FLOOR); return { secrets: [noise, quotient] }; }, numbers: function (jiff, computation_id, receivers_list, threshold, Zp, params) { diff --git a/tests/regr-tests/arithmetics.test.ts b/tests/regr-tests/arithmetics.test.ts index d1c671ba9..4819efcae 100644 --- a/tests/regr-tests/arithmetics.test.ts +++ b/tests/regr-tests/arithmetics.test.ts @@ -92,7 +92,7 @@ describe('JIFF Arithmetic Operations', () => { }); it('should correctly multiply numbers 60.05 * 60.05 = 3606.0025', async () => { - async function subtraction(jiffClient: any, id: number) { + async function multiplication(jiffClient: any, id: number) { return new Promise((resolve, reject) => { jiffClient.wait_for([1, 2], async () => { try { @@ -107,12 +107,12 @@ describe('JIFF Arithmetic Operations', () => { }); } - const results = await Promise.all(jiffClients.map((client, idx) => subtraction(client, idx + 1))); + const results = await Promise.all(jiffClients.map((client, idx) => multiplication(client, idx + 1))); results.map((res) => expect(res).toEqual('3606.0025')); }); it('should correctly divide numbers 60.05 / 60.05 = 1', async () => { - async function subtraction(jiffClient: any, id: number) { + async function division(jiffClient: any, id: number) { return new Promise((resolve, reject) => { jiffClient.wait_for([1, 2], async () => { try { @@ -127,7 +127,7 @@ describe('JIFF Arithmetic Operations', () => { }); } - const results = await Promise.all(jiffClients.map((client, idx) => subtraction(client, idx + 1))); + const results = await Promise.all(jiffClients.map((client, idx) => division(client, idx + 1))); results.map((res) => expect(res).toEqual('1')); }, 35000); });