Skip to content

Commit

Permalink
fix: add missing overloaded load method for cluster keys
Browse files Browse the repository at this point in the history
  • Loading branch information
lapets committed Feb 3, 2025
1 parent daaf710 commit 7cf0126
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/nilql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,16 @@ class ClusterKey extends SecretKey {

return clusterKey as ClusterKey;
}

/**
* Create an instance from its JSON-compatible object representation.
*/
public static load(object: object): ClusterKey {
const secretKey = SecretKey.load(object);
const clusterKey = new ClusterKey(secretKey.cluster, secretKey.operations);
clusterKey.material = secretKey.material;
return clusterKey;
}
}

/**
Expand Down
16 changes: 15 additions & 1 deletion tests/nilql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe("methods of cryptographic key classes", () => {
expect(decrypted).toEqual(plaintext);
});

test("generate, dump, JSONify, and load key for sum operation with multiple nodes", async () => {
test("generate, dump, JSONify, and load secret key for sum operation with multiple nodes", async () => {
const cluster = { nodes: [{}, {}, {}] };
const secretKey = await nilql.SecretKey.generate(cluster, { sum: true });

Expand All @@ -151,6 +151,20 @@ describe("methods of cryptographic key classes", () => {
expect(decrypted).toEqual(plaintext);
});

test("generate, dump, JSONify, and load cluster key for sum operation with multiple nodes", async () => {
const cluster = { nodes: [{}, {}, {}] };
const clusterKey = await nilql.ClusterKey.generate(cluster, { sum: true });

const clusterKeyObject = JSON.parse(JSON.stringify(clusterKey.dump()));
const clusterKeyLoaded = nilql.ClusterKey.load(clusterKeyObject);
expect(clusterKeyLoaded instanceof nilql.ClusterKey).toEqual(true);

const plaintext = BigInt(123);
const ciphertext = await nilql.encrypt(clusterKey, plaintext);
const decrypted = await nilql.decrypt(clusterKeyLoaded, ciphertext);
expect(decrypted).toEqual(plaintext);
});

test("generate key from seed for store operation with single node", async () => {
const secretKeyFromSeed = await nilql.SecretKey.generate(
{ nodes: [{}] },
Expand Down

0 comments on commit 7cf0126

Please sign in to comment.