Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Aug 7, 2023
2 parents bd500e4 + 8c519c0 commit 7225c68
Show file tree
Hide file tree
Showing 7 changed files with 433 additions and 353 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"prebuild": "yarn typechain"
},
"dependencies": {
"@nucypher/nucypher-core": "^0.10.0",
"@nucypher/nucypher-core": "^0.11.0",
"axios": "^0.21.1",
"deep-equal": "^2.2.1",
"ethers": "^5.4.1",
Expand Down
26 changes: 20 additions & 6 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import {
decryptWithSharedSecret,
EncryptedThresholdDecryptionRequest,
EncryptedThresholdDecryptionResponse,
FerveoVariant,
SessionSharedSecret,
SessionStaticSecret,
ThresholdDecryptionRequest,
} from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { DkgCoordinatorAgent, DkgParticipant } from '../agents/coordinator';
import {
DkgCoordinatorAgent,
DkgParticipant,
DkgRitualState,
} from '../agents/coordinator';
import { ConditionExpression } from '../conditions';
import {
DkgRitual,
FerveoVariant,
getCombineDecryptionSharesFunction,
getVariantClass,
} from '../dkg';
Expand Down Expand Up @@ -73,16 +77,26 @@ export class CbdTDecDecrypter {

// Retrieve decryption shares
public async retrieve(
provider: ethers.providers.Web3Provider,
web3Provider: ethers.providers.Web3Provider,
conditionExpr: ConditionExpression,
variant: FerveoVariant,
ciphertext: Ciphertext
): Promise<DecryptionSharePrecomputed[] | DecryptionShareSimple[]> {
const ritualState = await DkgCoordinatorAgent.getRitualState(
web3Provider,
this.ritualId
);
if (ritualState !== DkgRitualState.FINALIZED) {
throw new Error(
`Ritual with id ${this.ritualId} is not finalized. Ritual state is ${ritualState}.`
);
}

const dkgParticipants = await DkgCoordinatorAgent.getParticipants(
provider,
web3Provider,
this.ritualId
);
const contextStr = await conditionExpr.buildContext(provider).toJson();
const contextStr = await conditionExpr.buildContext(web3Provider).toJson();
const { sharedSecrets, encryptedRequests } = this.makeDecryptionRequests(
this.ritualId,
variant,
Expand Down Expand Up @@ -115,7 +129,7 @@ export class CbdTDecDecrypter {
private makeDecryptionShares(
encryptedResponses: Record<string, EncryptedThresholdDecryptionResponse>,
sessionSharedSecret: Record<string, SessionSharedSecret>,
variant: number,
variant: FerveoVariant,
expectedRitualId: number
) {
const decryptedResponses = Object.entries(encryptedResponses).map(
Expand Down
33 changes: 13 additions & 20 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DecryptionSharePrecomputed,
DecryptionShareSimple,
DkgPublicKey,
FerveoVariant,
SharedSecret,
} from '@nucypher/nucypher-core';
import { ethers } from 'ethers';
Expand All @@ -12,22 +13,15 @@ import { DkgCoordinatorAgent, DkgRitualState } from './agents/coordinator';
import { ChecksumAddress } from './types';
import { bytesEquals, fromHexString, objectEquals } from './utils';

// TODO: Expose from @nucypher/nucypher-core
export enum FerveoVariant {
Simple = 0,
Precomputed = 1,
}

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}`);
if (variant.equals(FerveoVariant.simple)) {
return DecryptionShareSimple;
} else if (variant.equals(FerveoVariant.precomputed)) {
return DecryptionSharePrecomputed;
} else {
throw new Error(`Invalid FerveoVariant: ${variant}`);
}
}

Expand All @@ -36,13 +30,12 @@ export function getCombineDecryptionSharesFunction(
): (
shares: DecryptionShareSimple[] | DecryptionSharePrecomputed[]
) => SharedSecret {
switch (variant) {
case FerveoVariant.Simple:
return combineDecryptionSharesSimple;
case FerveoVariant.Precomputed:
return combineDecryptionSharesPrecomputed;
default:
throw new Error(`Invalid FerveoVariant: ${variant}`);
if (variant.equals(FerveoVariant.simple)) {
return combineDecryptionSharesSimple;
} else if (variant.equals(FerveoVariant.precomputed)) {
return combineDecryptionSharesPrecomputed;
} else {
throw new Error(`Invalid FerveoVariant: ${variant}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as conditions from './conditions';
export { conditions, CustomContextParam };

// DKG
export { FerveoVariant } from './dkg';
export { FerveoVariant } from '@nucypher/nucypher-core';

// SDK
export { Cohort } from './sdk/cohort';
Expand Down
5 changes: 4 additions & 1 deletion test/unit/cbd-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
mockCbdDecrypt,
mockGetExistingRitual,
mockGetParticipants,
mockGetRitualState,
mockGetUrsulas,
mockInitializeRitual,
mockRandomSessionStaticSecret,
Expand All @@ -38,7 +39,7 @@ const ownsNFT = new ERC721Ownership({
});
const conditionExpr = new ConditionExpression(ownsNFT);
const ursulas = fakeUrsulas();
const variant = FerveoVariant.Precomputed;
const variant = FerveoVariant.precomputed;
const ritualId = 0;

const makeCbdStrategy = async () => {
Expand Down Expand Up @@ -131,6 +132,7 @@ describe('CbdDeployedStrategy', () => {
const getParticipantsSpy = mockGetParticipants(participants);
const getUrsulasSpy = mockGetUrsulas(ursulas);
const sessionKeySpy = mockRandomSessionStaticSecret(requesterSessionKey);
const getRitualStateSpy = mockGetRitualState();

const decryptedMessage =
await deployedStrategy.decrypter.retrieveAndDecrypt(
Expand All @@ -142,6 +144,7 @@ describe('CbdDeployedStrategy', () => {
expect(getUrsulasSpy).toHaveBeenCalled();
expect(getParticipantsSpy).toHaveBeenCalled();
expect(sessionKeySpy).toHaveBeenCalled();
expect(getRitualStateSpy).toHaveBeenCalled();
expect(decryptSpy).toHaveBeenCalled();
expect(decryptedMessage).toEqual(toBytes(message));
});
Expand Down
25 changes: 15 additions & 10 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
EncryptedTreasureMap,
EthereumAddress,
ferveoEncrypt,
FerveoVariant,
Keypair,
PublicKey,
reencrypt,
Expand Down Expand Up @@ -50,7 +51,7 @@ import {
RetrieveCFragsResult,
Ursula,
} from '../src/characters/porter';
import { DkgClient, DkgRitual, FerveoVariant } from '../src/dkg';
import { DkgClient, DkgRitual } from '../src/dkg';
import { BlockchainPolicy, PreEnactedPolicy } from '../src/policies/policy';
import { ChecksumAddress } from '../src/types';
import { toBytes, toHexString, zip } from '../src/utils';
Expand Down Expand Up @@ -218,14 +219,14 @@ export const mockDetectEthereumProvider = () => {
};

export const fakeDkgFlow = (
variant: FerveoVariant | FerveoVariant.Precomputed,
variant: FerveoVariant,
ritualId: number,
sharesNum: number,
threshold: number
) => {
if (
variant !== FerveoVariant.Simple &&
variant !== FerveoVariant.Precomputed
!variant.equals(FerveoVariant.simple) &&
!variant.equals(FerveoVariant.precomputed)
) {
throw new Error(`Invalid variant: ${variant}`);
}
Expand Down Expand Up @@ -322,20 +323,22 @@ export const fakeTDecFlow = ({
}

let decryptionShare;
if (variant === FerveoVariant.Precomputed) {
if (variant.equals(FerveoVariant.precomputed)) {
decryptionShare = aggregate.createDecryptionSharePrecomputed(
dkg,
ciphertext,
aad,
keypair
);
} else {
} else if (variant.equals(FerveoVariant.simple)) {
decryptionShare = aggregate.createDecryptionShareSimple(
dkg,
ciphertext,
aad,
keypair
);
} else {
throw new Error(`Invalid variant: ${variant}`);
}
decryptionShares.push(decryptionShare);
});
Expand All @@ -344,10 +347,12 @@ export const fakeTDecFlow = ({
// This part is in the client API

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

// The client should have access to the public parameters of the DKG
Expand Down Expand Up @@ -402,7 +407,7 @@ export const fakeCoordinatorRitual = (
publicKeyHash: string;
totalAggregations: number;
} => {
const ritual = fakeDkgTDecFlowE2e(FerveoVariant.Precomputed);
const ritual = fakeDkgTDecFlowE2e(FerveoVariant.precomputed);
const dkgPkBytes = ritual.dkg.publicKey().toBytes();
return {
id: ritualId,
Expand All @@ -424,7 +429,7 @@ export const fakeCoordinatorRitual = (

export const fakeDkgParticipants = (
ritualId: number,
variant = FerveoVariant.Precomputed
variant = FerveoVariant.precomputed
): {
participants: DkgParticipant[];
participantSecrets: Record<string, SessionStaticSecret>;
Expand Down
Loading

0 comments on commit 7225c68

Please sign in to comment.