Skip to content

Commit

Permalink
self review
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 9, 2023
1 parent f9c43d0 commit 03eb03e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 46 deletions.
33 changes: 14 additions & 19 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import {
SessionSharedSecret,
SessionStaticKey,
SessionStaticSecret,
SharedSecret,
ThresholdDecryptionRequest,
} from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { DkgCoordinatorAgent, DkgParticipant } from '../agents/coordinator';
import { ConditionSet } from '../conditions';
import { combineDecryptionSharesMap, DkgRitual, variantMap } from '../dkg';
import {
DkgRitual,
getCombineDecryptionSharesFunction,
getVariantClass,
} from '../dkg';
import { fromHexString, fromJSON, toJSON } from '../utils';

import { Porter } from './porter';
Expand All @@ -29,12 +32,11 @@ export type CbdTDecDecrypterJSON = {
export class CbdTDecDecrypter {
private readonly porter: Porter;

// private readonly verifyingKey: Keyring;

constructor(porterUri: string, private readonly threshold: number) {
this.porter = new Porter(porterUri);
}

// Retrieve and decrypt ciphertext using provider and condition set
public async retrieveAndDecrypt(
provider: ethers.providers.Web3Provider,
conditionSet: ConditionSet,
Expand All @@ -51,13 +53,9 @@ export class CbdTDecDecrypter {
ciphertext
);

let sharedSecret: SharedSecret;
const combineDecryptionSharesFn = combineDecryptionSharesMap[variant];
if (combineDecryptionSharesFn) {
sharedSecret = combineDecryptionSharesFn(decryptionShares);
} else {
throw new Error(`Unknown variant ${variant}`);
}
const combineDecryptionSharesFn =
getCombineDecryptionSharesFunction(variant);
const sharedSecret = combineDecryptionSharesFn(decryptionShares);

const plaintext = decryptWithSharedSecret(
ciphertext,
Expand All @@ -68,6 +66,7 @@ export class CbdTDecDecrypter {
return [plaintext];
}

// Retrieve decryption shares
public async retrieve(
provider: ethers.providers.Web3Provider,
conditionSet: ConditionSet,
Expand Down Expand Up @@ -129,14 +128,10 @@ export class CbdTDecDecrypter {
({ decryptionShare }) => decryptionShare
);

const DecryptionShareType = variantMap[variant];
if (DecryptionShareType) {
return decryptionShares.map((share) =>
DecryptionShareType.fromBytes(share)
);
} else {
throw new Error(`Unknown variant ${variant}`);
}
const DecryptionShareType = getVariantClass(variant);
return decryptionShares.map((share) =>
DecryptionShareType.fromBytes(share)
);
}

private makeDecryptionRequests(
Expand Down
46 changes: 27 additions & 19 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,32 @@ export enum FerveoVariant {
Precomputed = 1,
}

// TODO: Replace with a factory method
export const variantMap: {
[key: number]:
| typeof DecryptionShareSimple
| typeof DecryptionSharePrecomputed;
} = {
[FerveoVariant.Simple]: DecryptionShareSimple,
[FerveoVariant.Precomputed]: DecryptionSharePrecomputed,
};

// TODO: Replace with a factory method
export const combineDecryptionSharesMap: {
[key: number]: (
shares: DecryptionShareSimple[] | DecryptionSharePrecomputed[]
) => SharedSecret;
} = {
[FerveoVariant.Simple]: combineDecryptionSharesSimple,
[FerveoVariant.Precomputed]: combineDecryptionSharesPrecomputed,
};
export function getVariantClass(
variant: FerveoVariant
): typeof DecryptionShareSimple | typeof DecryptionSharePrecomputed {
switch (variant) {
case FerveoVariant.Simple:
return DecryptionShareSimple;
case FerveoVariant.Precomputed:
return DecryptionSharePrecomputed;
default:
throw new Error(`Invalid FerveoVariant: ${variant}`);
}
}
export function getCombineDecryptionSharesFunction(
variant: FerveoVariant
): (
shares: DecryptionShareSimple[] | DecryptionSharePrecomputed[]
) => SharedSecret {
switch (variant) {
case FerveoVariant.Simple:
return combineDecryptionSharesSimple;
case FerveoVariant.Precomputed:
return combineDecryptionSharesPrecomputed;
default:
throw new Error(`Invalid FerveoVariant: ${variant}`);
}
}

export interface DkgRitualJSON {
id: number;
Expand Down Expand Up @@ -95,6 +102,7 @@ export class DkgClient {
// TODO: Create a new DKG ritual here
throw new Error('Not implemented');
}

// TODO: Without Validator public key in Coordinator, we cannot verify the
// transcript. We need to add it to the Coordinator.
// public async verifyRitual(ritualId: number): Promise<boolean> {
Expand Down
12 changes: 6 additions & 6 deletions src/sdk/strategy/cbd-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ export class CbdStrategy {
const conditionSetEquals =
this.conditionSet && other.conditionSet
? this.conditionSet.equals(other.conditionSet)
: false;
: this.conditionSet === other.conditionSet;
return this.cohort.equals(other.cohort) && conditionSetEquals;
}
}

export class DeployedCbdStrategy {
constructor(
public cohort: Cohort,
public dkgRitual: DkgRitual,
public encrypter: Enrico,
public decrypter: CbdTDecDecrypter,
public conditionSet?: ConditionSet
public readonly cohort: Cohort,
public readonly dkgRitual: DkgRitual,
public readonly encrypter: Enrico,
public readonly decrypter: CbdTDecDecrypter,
public readonly conditionSet?: ConditionSet
) {}

public static fromJSON(json: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/sdk/strategy/pre-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class PreStrategy {
const conditionSetEquals =
this.conditionSet && other.conditionSet
? this.conditionSet.equals(other.conditionSet)
: false;
: this.conditionSet === other.conditionSet;
return (
this.cohort.equals(other.cohort) &&
// TODO: Add equality to WASM bindings
Expand Down Expand Up @@ -274,7 +274,7 @@ export class DeployedPreStrategy {
const conditionSetEquals =
this.conditionSet && other.conditionSet
? this.conditionSet.equals(other.conditionSet)
: false;
: this.conditionSet === other.conditionSet;
return (
this.label === other.label &&
this.cohort.equals(other.cohort) &&
Expand Down

0 comments on commit 03eb03e

Please sign in to comment.