Skip to content

Commit

Permalink
Booleans class made (#291)
Browse files Browse the repository at this point in the history
* Booleans class made

* resolving codacy warning
  • Loading branch information
Snafkin547 authored Jun 6, 2024
1 parent 8dbe3ea commit 9df48c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
30 changes: 16 additions & 14 deletions lib/client/protocols/booleans/boolean.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Generic version of operations
module.exports = function (SecretShare) {
class Booleans {
/**
* bitwise-XOR with a constant (BOTH BITS).
* @method cxor_bit
Expand All @@ -8,7 +8,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.cxor_bit = function (cst) {
cxor_bit(cst) {
if (!this.isConstant(cst)) {
throw new Error('parameter should be a number (^)');
}
Expand All @@ -17,7 +17,7 @@ module.exports = function (SecretShare) {
}

return this.icadd(cst).issub(this.icmult(cst).icmult(2));
};
}

/**
* bitwise-OR with a constant (BOTH BITS).
Expand All @@ -27,7 +27,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.cor_bit = function (cst) {
cor_bit(cst) {
if (!this.isConstant(cst)) {
throw new Error('parameter should be a number (|)');
}
Expand All @@ -36,7 +36,7 @@ module.exports = function (SecretShare) {
}

return this.icadd(cst).issub(this.icmult(cst));
};
}

/**
* bitwise-XOR of two secret shares OF BITS.
Expand All @@ -50,7 +50,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.sxor_bit = function (o, op_id) {
sxor_bit(o, op_id) {
if (!(o.jiff === this.jiff)) {
throw new Error('shares do not belong to the same instance (^)');
}
Expand All @@ -65,7 +65,7 @@ module.exports = function (SecretShare) {
}

return this.isadd(o).issub(this.ismult(o, op_id + ':smult1').icmult(2));
};
}

/**
* OR of two secret shares OF BITS.
Expand All @@ -79,7 +79,7 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.sor_bit = function (o, op_id) {
sor_bit(o, op_id) {
if (!(o.jiff === this.jiff)) {
throw new Error('shares do not belong to the same instance (|)');
}
Expand All @@ -94,7 +94,7 @@ module.exports = function (SecretShare) {
}

return this.isadd(o).issub(this.ismult(o, op_id + ':smult1'));
};
}

/**
* Negation of a bit.
Expand All @@ -104,9 +104,9 @@ module.exports = function (SecretShare) {
* @memberof module:jiff-client~JIFFClient#SecretShare
* @instance
*/
SecretShare.prototype.not = function () {
not() {
return this.icmult(-1).icadd(1);
};
}

/**
* Simulate an oblivious If-else statement with a single return value.
Expand All @@ -131,7 +131,7 @@ module.exports = function (SecretShare) {
* // max is set to the greater value, without revealing the value or the result of the inequality
* let max = cmp.if_else(a, b);
*/
SecretShare.prototype.if_else = function (trueVal, falseVal, op_id) {
if_else(trueVal, falseVal, op_id) {
if (op_id == null) {
op_id = this.jiff.counters.gen_op_id('if_else', this.holders);
}
Expand All @@ -149,5 +149,7 @@ module.exports = function (SecretShare) {
} else {
return this.ismult(trueVal.issub(falseVal), op_id + ':smult').isadd(falseVal);
}
};
};
}
}

module.exports = Booleans;
8 changes: 6 additions & 2 deletions lib/client/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const genericProtocols = require('./protocols/generic.js');
const arithmetic = require('./protocols/numbers/arithmetic.js');
const comparison = require('./protocols/numbers/comparison.js');
const protocols = require('./protocols/numbers/protocols.js');
const booleans = require('./protocols/booleans/boolean.js');
const Booleans = require('./protocols/booleans/boolean.js');

// a metaclass that creates SecretShare classes when given a jiff instance
// alternatively, we can think of this as a factory for a secret share prototypes/constructors given a jiff instance
Expand Down Expand Up @@ -212,7 +212,11 @@ module.exports = function (jiff) {
arithmetic(SecretShare);
comparison(SecretShare);
protocols(SecretShare);
booleans(SecretShare);

// Attach all methods from Booleans class to SecretShare prototype
Object.getOwnPropertyNames(Booleans.prototype).forEach((method) => {
SecretShare.prototype[String(method)] = Booleans.prototype[String(method)];
});

// internal variant of primitives, to use internally by other primitives
const internals = [
Expand Down

0 comments on commit 9df48c6

Please sign in to comment.