Skip to content

Commit

Permalink
draft light encryption method
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Aug 16, 2023
1 parent 8e7e634 commit b2cf2e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
42 changes: 32 additions & 10 deletions src/taco.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Ciphertext, ferveoEncrypt } from '@nucypher/nucypher-core';
import {
Ciphertext,
DkgPublicKey,
ferveoEncrypt,
} from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { ThresholdDecrypter } from './characters/cbd-recipient';
Expand All @@ -9,7 +13,10 @@ import { toBytes } from './utils';
export interface TacoMessageKit {
ciphertext: Ciphertext;
aad: Uint8Array;
// TODO: How do we get rid of these two fields? We need them for decrypting
// We ritualId in order to fetch the DKG participants and create DecryptionRequests for them
ritualId: number;
// We need to know the threshold in order to create DecryptionRequests
threshold: number;
}

Expand All @@ -20,17 +27,30 @@ export const encrypt = async (
ritualId: number
): Promise<TacoMessageKit> => {
const dkgRitual = await DkgClient.getFinalizedRitual(web3Provider, ritualId);
const aad = conditions.asAad();
const ciphertext = ferveoEncrypt(
toBytes(message),
aad,
dkgRitual.dkgPublicKey
return await encryptLight(
message,
conditions,
dkgRitual.dkgPublicKey,
dkgRitual.dkgParams.threshold,
ritualId
);
};

export const encryptLight = async (
message: string,
conditions: ConditionExpression,
dkgPublicKey: DkgPublicKey,
// TODO: Remove these parameters after fixing TacoMessageKit
threshold: number,
ritualId: number
): Promise<TacoMessageKit> => {
const aad = conditions.asAad();
const ciphertext = ferveoEncrypt(toBytes(message), aad, dkgPublicKey);
return {
ciphertext,
aad,
threshold,
ritualId,
threshold: dkgRitual.dkgParams.threshold,
};
};

Expand All @@ -45,9 +65,10 @@ export const decrypt = async (
messageKit.threshold
);
const condExpr = ConditionExpression.fromAad(messageKit.aad);
// TODO: Need web3Provider to fetch participants from Coordinator to make decryption requests.
// Should we put them into the message kit instead?
// Consider case where participants are changing over time. Is that an issue we should consider now?
// TODO: We need web3Provider to fetch participants from Coordinator to make decryption requests.
// Removing this dependency is tied to release of ThresholdMessageKit
// Blocked by changes to nucypher-core and nucypher:
// https://github.com/nucypher/nucypher/pull/3194
return decrypter.retrieveAndDecrypt(
web3Provider,
condExpr,
Expand All @@ -57,5 +78,6 @@ export const decrypt = async (

export const taco = {
encrypt,
encryptLight,
decrypt,
};
1 change: 0 additions & 1 deletion test/unit/taco.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const ownsNFT = new ERC721Ownership({
});
const conditionExpr = new ConditionExpression(ownsNFT);
const variant = FerveoVariant.precomputed;
// const ritualId = 0;
const message = 'this is a secret';

describe('taco', () => {
Expand Down

1 comment on commit b2cf2e4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bundled size for the package is listed below:

build/module/src/kits: 19.53 KB
build/module/src/characters: 74.22 KB
build/module/src/policies: 19.53 KB
build/module/src/agents: 39.06 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions: 156.25 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 42.97 KB
build/module/src: 437.50 KB
build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/test: 42.97 KB
build/module: 691.41 KB
build/main/src/kits: 19.53 KB
build/main/src/characters: 74.22 KB
build/main/src/policies: 19.53 KB
build/main/src/agents: 39.06 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions: 156.25 KB
build/main/src/sdk/strategy: 35.16 KB
build/main/src/sdk: 46.88 KB
build/main/src: 449.22 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/test: 46.88 KB
build/main: 707.03 KB
build: 1.37 MB

Please sign in to comment.