Skip to content

Commit

Permalink
Except preprocess & statistics work
Browse files Browse the repository at this point in the history
  • Loading branch information
Snafkin547 committed Apr 8, 2024
1 parent 8a5ddee commit 2b66129
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
3 changes: 2 additions & 1 deletion lib/client/protocols/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Check failure on line 13 in lib/client/protocols/generic.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/client/protocols/generic.js#L13

Require statement not part of import statement.
return typeof o === 'number' || o instanceof BigNumber_;
};

/**
Expand Down
14 changes: 7 additions & 7 deletions lib/ext/jiff-client-bignumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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') {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions lib/ext/jiff-client-fixedpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check failure on line 375 in lib/ext/jiff-client-fixedpoint.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/ext/jiff-client-fixedpoint.js#L375

'max' is not defined.
}
if (share.isConstant(val2)) {
val2 = magnitude.times(val2).floor();
val2 = magnitude.times(val2).decimalPlaces(0, share.jiff.helpers.BigNumber.ROUND_FLOOR).times(max);

Check failure on line 378 in lib/ext/jiff-client-fixedpoint.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/ext/jiff-client-fixedpoint.js#L378

'max' is not defined.
}

var reduce_share = share.legacy.cdivfac(magnitude);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/ext/jiff-server-bignumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ 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);
}
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);
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions tests/regr-tests/arithmetics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
});

0 comments on commit 2b66129

Please sign in to comment.