@dedis/kyber
Version:
A typescript implementation of Kyber interfaces
63 lines (62 loc) • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Masks are used alongside with aggregated signatures to announce which peer
* has actually signed the message. It's essentially a bit mask.
*/
class Mask {
constructor(publics, mask) {
if (publics.length === 0 || publics.length <= (mask.length - 1) * 8) {
throw new Error("length of the public keys and the mask don't match");
}
this.publics = publics.slice();
this.mask = Buffer.from(mask);
this.aggregate = publics[0].clone().null();
for (let i = 0; i < publics.length; i++) {
const k = i >> 3;
const msk = 1 << (i & 7);
if ((mask[k] & msk) !== 0) {
this.aggregate.add(this.aggregate, publics[i]);
}
}
}
/**
* Return the number of participants, in other words the number of 1s in the mask
*
* @return the number of participants
*/
getCountEnabled() {
let hw = 0;
for (let i = 0; i < this.publics.length; i++) {
const k = i >> 3;
const msk = 1 << (i & 7);
if ((this.mask[k] & msk) !== 0) {
hw++;
}
}
return hw;
}
/**
* Return the total number of public keys assigned to the mask
*
* @return the total number of public keys
*/
getCountTotal() {
return this.publics.length;
}
/**
* Return true if the bit at the given index is enabled
*
* @param i The index
* @return true if the bit is enabled, false otherwise
*/
isIndexEnabled(i) {
if (i < 0 || i >= this.publics.length) {
throw new Error("index out of bound");
}
const k = i >> 3;
const msk = 1 << (i & 7);
return (this.mask[k] & msk) !== 0;
}
}
exports.default = Mask;