Skip to content

Commit

Permalink
Add typedoc comments for HE modules
Browse files Browse the repository at this point in the history
  • Loading branch information
platfowner committed Sep 21, 2023
1 parent c29f333 commit 2be3e5c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 28 deletions.
51 changes: 27 additions & 24 deletions src/he/desilo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
const SEAL = require('node-seal');

/**
* wrapper Error class for DesiloSeal
* A wrapper Error class for DesiloSeal.
*/
class DesiloSealError extends Error {
/**
* Creates a new DesiloSealError object with an error message.
* @param {String} message The error message.
*/
constructor(message) {
super(message);
this.name = this.constructor.name;
Expand All @@ -16,12 +20,12 @@ class DesiloSealError extends Error {
}

/**
* node-seal wrapper class
* A wrapper class for the Desilo's HE solution.
*/
class DesiloSeal {

/**
* Constructor
* Creates a new DesiloSeal object.
* @param {Number} polyModulusDegree
* @param {Int32Array} coeffModulusArray
* @param {Number} scaleBits
Expand All @@ -33,7 +37,7 @@ class DesiloSeal {
}

/**
* Initializes new SEALContext
* Initializes new SEALContext.
*/
async initContext() {
this.seal = await SEAL();
Expand All @@ -58,9 +62,7 @@ class DesiloSeal {
}

/**
* Makes a new keyset
* @param {string} secretKeyStr - secret key to load
* @param {string} publicKeyStr - public key to load
* Generates a new key set.
*/
initKeySet() {
const keyGenerator = this.seal.KeyGenerator(this.context);
Expand All @@ -78,9 +80,9 @@ class DesiloSeal {
}

/**
* Loads secret key
* @param {string} secretKeyStr - secret key to load
* @param {string} publicKeyStr - public key to load
* Loads a key set.
* @param {string} secretKeyStr The secret key to load.
* @param {string} publicKeyStr The public key to load.
*/
loadKeySet(secretKeyStr, publicKeyStr = undefined) {
const secretKey = this.seal.SecretKey();
Expand All @@ -104,7 +106,7 @@ class DesiloSeal {
}

/**
* Initialize encryptor, decryptor, evaluator
* Initializes encryptor, decryptor, and evaluator objects.
*/
initClasses() {
this.encryptor = this.seal.Encryptor(this.context, this.keys.publicKey);
Expand All @@ -113,8 +115,9 @@ class DesiloSeal {
}

/**
* gets entire keyset
* @returns {object} keys
* Returns the entire key set.
* It temporarily returns the secret key only due to memory issues.
* @returns {Object} The key set.
*/
getKeys() {
// hidden for now, due to memory issue
Expand All @@ -132,18 +135,18 @@ class DesiloSeal {
}

/**
* gets secretKey
* @returns {seal.SecretKey} secretKey
* Returns the secret key.
* @returns {any} The secret key.
*/
getSecretKey() {
const secretKey = this.keys.secretKey ? this.keys.secretKey.save() : '';
return secretKey;
}

/**
* encrypts a length-fixed array into a ciphertext
* @param {Float64Array} - array of length poly_mod_degree / 2
* @returns {CipherText}
* Encrypts a length-fixed array into a ciphertext.
* @param {Float64Array} array The array of length poly_mod_degree / 2.
* @returns {CipherText} The cipertext encrypted.
*/
encrypt(array) {
const plaintext = this.encoder.encode(array, this.scale);
Expand All @@ -155,9 +158,9 @@ class DesiloSeal {
}

/**
* Decrypt ciphertext
* @param {string::Ciphertext} cipherStr - ciphertext string
* @returns {Float64Array}
* Decrypts a ciphertext to an float64 array.
* @param {Ciphertext} cipherStr The ciphertext.
* @returns {Float64Array} The float64 array decrypted.
*/
decrypt(cipherStr) {
const uploadedCipherText = this.seal.CipherText();
Expand All @@ -169,9 +172,9 @@ class DesiloSeal {
}

/**
* Factory method for DesiloSeal class
* @param {Object} keys - object containing sk, pk, galois-key, relin-key strings
* @returns {DesiloSeal} seal - Desilo SEAL wrapper class
* A factory method for DesiloSeal class.
* @param {Object} keys The object containing sk, pk, galois-key, and relin-key strings.
* @returns {DesiloSeal} The newly created DesiloSeal object.
*/
async function DesiloSealFactory(keys, params) {
const { polyModulusDegree, coeffModulusArray, scaleBit } = params;
Expand Down
46 changes: 42 additions & 4 deletions src/he/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,29 @@ const DEFAULT_PARAMS: HomomorphicEncryptionParams = {
coeffModulusArray: Int32Array.from([60, 40, 40, 60]),
scaleBit: 40
};

/**
* A class for homorphic encryption based on the Desilo's HE solution.
*/
export default class HomomorphicEncryption {
/** The DesiloSeal object. */
public seal: any;
/** Whether the class is initialized or not. */
private _initialized: boolean;

/**
* Creates a new HomorphicEncryption obect.
*/
constructor() {
this.seal = null;
this._initialized = false;
}

/**
* Initializes the class with keys and parameters.
* @param {HomomorphicEncryptionSecretKey | null} keys The secret key.
* @param {HomomorphicEncryptionParams | null} params The homorphic encryption parameters.
*/
async init(keys?: HomomorphicEncryptionSecretKey | null, params?: HomomorphicEncryptionParams | null) {
this.seal = await DesiloSealFactory(keys, params ? params : DEFAULT_PARAMS);
if (!this.seal) {
Expand All @@ -31,32 +45,56 @@ export default class HomomorphicEncryption {
this._initialized = true;
}

/**
* Getter for _initialized.
*/
get initialized() {
return this._initialized;
}

TEST_getKeys() {
/**
* Returns the key set currently in use.
* It temporarily returns the secret key only due to memory issues.
* This is a method for test purposes only.
* @returns {Object} The key set.
*/
TEST_getKeys(): Object {
if (!this.initialized) {
throw new Error('Cannot encode before initializing.');
}
return this.seal.getKeys();
}

encrypt(array: Float64Array) {
/**
* Encrypts a length-fixed array into a ciphertext.
* @param {Float64Array} array The array of length poly_mod_degree / 2.
* @returns {CipherText} The cipertext encrypted.
*/
encrypt(array: Float64Array): CipherText {
if (!this.initialized) {
throw new Error('Cannot encrypt before initializing.');
}
return this.seal.encrypt(array);
}

decrypt(cipherText: CipherText) {
/**
* Decrypts a ciphertext to an float64 array.
* @param {CipherText} cipherText The ciphertext.
* @returns {Float64Array} The float64 array decrypted.
*/
decrypt(cipherText: CipherText): Float64Array {
if (!this.initialized) {
throw new Error('Cannot decrypt before initializing.');
}
return this.seal.decrypt(cipherText);
}

TEST_evaluate_double(cipherText: CipherText) {
/**
* Doubles an input cipher text and performs a homorphic calculation on it.
* This is a method for test purposes only.
* @returns {Object} The result of the homorphic calculation.
*/
TEST_evaluate_double(cipherText: CipherText): Object {
if (!this.initialized) {
throw new Error('Cannot evaluate before initializing.');
}
Expand Down

0 comments on commit 2be3e5c

Please sign in to comment.