Skip to content

Commit

Permalink
feat!: replace variant parameter with a default simple variant
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Aug 14, 2023
1 parent e4f824d commit 36fd43e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 93 deletions.
32 changes: 7 additions & 25 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Ciphertext,
combineDecryptionSharesSimple,
Context,
DecryptionSharePrecomputed,
DecryptionShareSimple,
decryptWithSharedSecret,
EncryptedThresholdDecryptionRequest,
Expand All @@ -15,11 +15,7 @@ import { ethers } from 'ethers';

import { DkgCoordinatorAgent, DkgParticipant } from '../agents/coordinator';
import { ConditionExpression } from '../conditions';
import {
DkgRitual,
getCombineDecryptionSharesFunction,
getVariantClass,
} from '../dkg';
import { DkgRitual } from '../dkg';
import { PorterClient } from '../porter';
import { fromJSON, toJSON } from '../utils';

Expand Down Expand Up @@ -50,19 +46,15 @@ export class ThresholdDecrypter {
public async retrieveAndDecrypt(
provider: ethers.providers.Web3Provider,
conditionExpr: ConditionExpression,
variant: FerveoVariant,
ciphertext: Ciphertext
): Promise<Uint8Array> {
const decryptionShares = await this.retrieve(
provider,
conditionExpr,
variant,
ciphertext
);

const combineDecryptionSharesFn =
getCombineDecryptionSharesFunction(variant);
const sharedSecret = combineDecryptionSharesFn(decryptionShares);
const sharedSecret = combineDecryptionSharesSimple(decryptionShares);
return decryptWithSharedSecret(
ciphertext,
conditionExpr.asAad(),
Expand All @@ -74,17 +66,15 @@ export class ThresholdDecrypter {
public async retrieve(
provider: ethers.providers.Web3Provider,
conditionExpr: ConditionExpression,
variant: FerveoVariant,
ciphertext: Ciphertext
): Promise<DecryptionSharePrecomputed[] | DecryptionShareSimple[]> {
): Promise<DecryptionShareSimple[]> {
const dkgParticipants = await DkgCoordinatorAgent.getParticipants(
provider,
this.ritualId
);
const contextStr = await conditionExpr.buildContext(provider).toJson();
const { sharedSecrets, encryptedRequests } = this.makeDecryptionRequests(
this.ritualId,
variant,
ciphertext,
conditionExpr,
contextStr,
Expand All @@ -106,15 +96,13 @@ export class ThresholdDecrypter {
return this.makeDecryptionShares(
encryptedResponses,
sharedSecrets,
variant,
this.ritualId
);
}

private makeDecryptionShares(
encryptedResponses: Record<string, EncryptedThresholdDecryptionResponse>,
sessionSharedSecret: Record<string, SessionSharedSecret>,
variant: FerveoVariant,
expectedRitualId: number
) {
const decryptedResponses = Object.entries(encryptedResponses).map(
Expand All @@ -128,19 +116,13 @@ export class ThresholdDecrypter {
);
}

const decryptionShares = decryptedResponses.map(
({ decryptionShare }) => decryptionShare
);

const DecryptionShareType = getVariantClass(variant);
return decryptionShares.map((share) =>
DecryptionShareType.fromBytes(share)
return decryptedResponses.map(({ decryptionShare }) =>
DecryptionShareSimple.fromBytes(decryptionShare)
);
}

private makeDecryptionRequests(
ritualId: number,
variant: FerveoVariant,
ciphertext: Ciphertext,
conditionExpr: ConditionExpression,
contextStr: string,
Expand All @@ -151,7 +133,7 @@ export class ThresholdDecrypter {
} {
const decryptionRequest = new ThresholdDecryptionRequest(
ritualId,
variant,
FerveoVariant.simple,
ciphertext,
conditionExpr.toWASMConditions(),
new Context(contextStr)
Expand Down
36 changes: 1 addition & 35 deletions src/dkg.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
import {
combineDecryptionSharesPrecomputed,
combineDecryptionSharesSimple,
DecryptionSharePrecomputed,
DecryptionShareSimple,
DkgPublicKey,
FerveoVariant,
SharedSecret,
} from '@nucypher/nucypher-core';
import { DkgPublicKey } from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { DkgCoordinatorAgent, DkgRitualState } from './agents/coordinator';
import { ChecksumAddress } from './types';
import { fromHexString, objectEquals } from './utils';

export function getVariantClass(
variant: FerveoVariant
): typeof DecryptionShareSimple | typeof DecryptionSharePrecomputed {
if (variant.equals(FerveoVariant.simple)) {
return DecryptionShareSimple;
} else if (variant.equals(FerveoVariant.precomputed)) {
return DecryptionSharePrecomputed;
} else {
throw new Error(`Invalid FerveoVariant: ${variant}`);
}
}

export function getCombineDecryptionSharesFunction(
variant: FerveoVariant
): (
shares: DecryptionShareSimple[] | DecryptionSharePrecomputed[]
) => SharedSecret {
if (variant.equals(FerveoVariant.simple)) {
return combineDecryptionSharesSimple;
} else if (variant.equals(FerveoVariant.precomputed)) {
return combineDecryptionSharesPrecomputed;
} else {
throw new Error(`Invalid FerveoVariant: ${variant}`);
}
}

export type DkgRitualParameters = {
sharesNum: number;
threshold: number;
Expand Down
2 changes: 0 additions & 2 deletions test/unit/cbd-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ describe('CbdDeployedStrategy', () => {
// Setup mocks for `retrieveAndDecrypt`
const { decryptionShares } = fakeTDecFlow({
...mockedDkg,
variant,
message: toBytes(message),
aad,
ciphertext,
Expand All @@ -136,7 +135,6 @@ describe('CbdDeployedStrategy', () => {
await deployedStrategy.decrypter.retrieveAndDecrypt(
aliceProvider,
conditionExpr,
variant,
ciphertext
);
expect(getUrsulasSpy).toHaveBeenCalled();
Expand Down
38 changes: 7 additions & 31 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Capsule,
CapsuleFrag,
Ciphertext,
combineDecryptionSharesPrecomputed,
combineDecryptionSharesSimple,
DecryptionSharePrecomputed,
DecryptionShareSimple,
Expand Down Expand Up @@ -287,7 +286,6 @@ interface FakeDkgRitualFlow {
sharesNum: number;
threshold: number;
receivedMessages: ValidatorMessage[];
variant: FerveoVariant;
ciphertext: Ciphertext;
aad: Uint8Array;
dkg: Dkg;
Expand All @@ -301,7 +299,6 @@ export const fakeTDecFlow = ({
sharesNum,
threshold,
receivedMessages,
variant,
ciphertext,
aad,
message,
Expand All @@ -319,38 +316,18 @@ export const fakeTDecFlow = ({
throw new Error('Transcript is invalid');
}

let decryptionShare;
if (variant.equals(FerveoVariant.precomputed)) {
decryptionShare = aggregate.createDecryptionSharePrecomputed(
dkg,
ciphertext,
aad,
keypair
);
} else if (variant.equals(FerveoVariant.simple)) {
decryptionShare = aggregate.createDecryptionShareSimple(
dkg,
ciphertext,
aad,
keypair
);
} else {
throw new Error(`Invalid variant: ${variant}`);
}
const decryptionShare = aggregate.createDecryptionShareSimple(
dkg,
ciphertext,
aad,
keypair
);
decryptionShares.push(decryptionShare);
});

// Now, the decryption share can be used to decrypt the ciphertext
// This part is in the client API

let sharedSecret;
if (variant.equals(FerveoVariant.precomputed)) {
sharedSecret = combineDecryptionSharesPrecomputed(decryptionShares);
} else if (variant.equals(FerveoVariant.simple)) {
sharedSecret = combineDecryptionSharesSimple(decryptionShares);
} else {
throw new Error(`Invalid variant: ${variant}`);
}
const sharedSecret = combineDecryptionSharesSimple(decryptionShares);

// The client should have access to the public parameters of the DKG
const plaintext = decryptWithSharedSecret(ciphertext, aad, sharedSecret);
Expand All @@ -374,7 +351,6 @@ export const fakeDkgTDecFlowE2e = (
const ciphertext = ferveoEncrypt(message, aad, ritual.dkg.publicKey());
const { decryptionShares } = fakeTDecFlow({
...ritual,
variant,
ciphertext,
aad,
message,
Expand Down

1 comment on commit 36fd43e

@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: 425.78 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: 636.72 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: 433.59 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: 644.53 KB
build: 1.26 MB

Please sign in to comment.